예제 #1
0
파일: fs.php 프로젝트: mk-pmb/js2php
     }
     $opts->set('append', $append);
     $mode = $append ? 'ab' : 'wb';
     try {
         $stream = fopen($fullPath, $mode);
     } catch (Exception $e) {
         $helpers['handleException']($e, $fullPath);
     }
     //fallback for if set_error_handler didn't do it's thing
     if ($stream === false) {
         $helpers['throwError']('EIO', $fullPath);
     }
     $self->stream = $stream;
     $self->finished = false;
 });
 $prototype = $WriteStream->get('prototype');
 $prototype->setMethods(array('setEncoding' => function ($enc) {
     $self = Func::getContext();
     $self->opts->set('encoding', $enc);
 }, 'write' => function ($data, $enc = null) use(&$helpers) {
     $self = Func::getContext();
     if ($self->finished) {
         return;
     }
     $data = $data instanceof Buffer ? $data->raw : $data;
     write_all($self->stream, $data);
 }, 'end' => function () {
     $self = Func::getContext();
     if ($self->finished) {
         return;
     }
예제 #2
0
파일: tests.php 프로젝트: mk-pmb/js2php
        Test::assert('this is global', $self === Object::$global);
    });
    $fn->call();
    $fn = new Func('foo', function () use($fn, $Array) {
        $self = Func::getContext();
        $arguments = Func::getArguments();
        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 () {