Пример #1
0
     }
     $flags = $append ? FILE_APPEND : 0;
     $data = $data instanceof Buffer ? $data->raw : $data;
     try {
         $result = file_put_contents($fullPath, $data, $flags);
     } catch (Exception $e) {
         $helpers['handleException']($e, $fullPath);
     }
     //fallback for if set_error_handler didn't do it's thing
     if ($result === false) {
         $helpers['throwError']('EIO', $fullPath);
     }
 }, 'createReadStream' => function ($path, $opts = null) use(&$helpers, &$ReadStream) {
     return $ReadStream->construct($path, $opts);
 }, 'createWriteStream' => function ($path, $opts = null) use(&$helpers, &$WriteStream) {
     return $WriteStream->construct($path, $opts);
 });
 $helpers = array('basePath' => getcwd(), 'ERR_MAP' => array('EACCES' => "EACCES, permission denied", 'EBADF' => "EBADF, bad file descriptor", 'EEXIST' => "EEXIST, file already exists", 'EIO' => "EIO, input/output error", 'ENOENT' => "ENOENT, no such file or directory", 'ENOTDIR' => "ENOTDIR, not a directory", 'ENOTEMPTY' => "ENOTEMPTY, directory not empty", 'EPERM' => "EPERM, operation not permitted", 'EISDIR' => "EISDIR, is a directory"), 'throwError' => function ($code, $paths = array(), $framesToPop = 0) use(&$helpers) {
     $message = $helpers['ERR_MAP'][$code];
     $paths = is_array($paths) ? $paths : array($paths);
     foreach ($paths as $path) {
         $message .= " '" . $helpers['reverseMapPath']($path) . "'";
     }
     $err = Error::create($message, $framesToPop + 1);
     $err->set('code', $code);
     throw new Ex($err);
 }, 'handleException' => function ($ex, $paths = array()) use(&$helpers) {
     $message = $ex->getMessage();
     $paths = is_array($paths) ? $paths : array($paths);
     //get the error message with the path(s) removed. this prevents words
     // in the path from effecting our parsing below.
Пример #2
0
        Test::assert('arguments length', $arguments->get('length') === 1.0);
        Test::assert('arguments -> args', join(',', $arguments->args) === 'foo');
        Test::assert('this is global', $self === $Array);
    });
    $fn->call($Array, 'foo');
});
Test::suite('Object.create', function () use($Object) {
    $Animal = new Func(function () {
    });
    $Animal->get('prototype')->set('speak', new Func(function () {
        return 'hi';
    }));
    $Dog = new Func(function () {
    });
    $Dog->set('prototype', $Object->callMethod('create', $Animal->get('prototype')));
    $dog = $Dog->construct();
    Test::assert('has method', $dog->get('speak') instanceof Func);
    Test::assert('method call', $dog->callMethod('speak') === 'hi');
    Test::assert('proto inherit', _instanceof($dog, $Dog));
    Test::assert('proto inherit parent', _instanceof($dog, $Animal));
    Test::assert('proto inherit top', _instanceof($dog, $Object));
    $Thing = new Func(function () {
    });
    Test::assert('proto not instance foreign', !_instanceof($dog, $Thing));
    $Dog->get('prototype')->set('speak', new Func(function () {
        return 'woof';
    }));
    Test::assert('method override', $dog->callMethod('speak') === 'woof');
    $animal = $Animal->construct();
    Test::assert('method still on parent', $animal->callMethod('speak') === 'hi');
});