Пример #1
0
 /**
  * Creates the global constructor used in user-land
  * @return Func
  */
 static function getGlobalConstructor()
 {
     $Error = new Func(function ($str = null) {
         $error = new self($str);
         $error->stack = debug_backtrace();
         return $error;
     });
     $Error->set('prototype', self::$protoObject);
     $Error->setMethods(self::$classMethods, true, false, true);
     return $Error;
 }
Пример #2
0
 /**
  * Creates the global constructor used in user-land
  * @return Func
  */
 static function getGlobalConstructor()
 {
     $Buffer = new Func('Buffer', function () {
         $self = new Buffer();
         $self->init(func_get_args());
         return $self;
     });
     $Buffer->set('prototype', Buffer::$protoObject);
     $Buffer->setMethods(Buffer::$classMethods, true, false, true);
     return $Buffer;
 }
Пример #3
0
 /**
  * Creates the global constructor used in user-land
  * @return Func
  */
 static function getGlobalConstructor()
 {
     $Boolean = new Func(function ($value = false) {
         $self = Func::getContext();
         if ($self instanceof Bln) {
             $self->value = $value ? true : false;
             return $self;
         } else {
             return $value ? true : false;
         }
     });
     $Boolean->instantiate = function () {
         return new Bln();
     };
     $Boolean->set('prototype', Bln::$protoObject);
     $Boolean->setMethods(Bln::$classMethods, true, false, true);
     return $Boolean;
 }
Пример #4
0
 /**
  * Creates the global constructor used in user-land
  * @return Func
  */
 static function getGlobalConstructor()
 {
     $String = new Func(function ($value = '') {
         $self = Func::getContext();
         if ($self instanceof Str) {
             $self->value = to_string($value);
             return $self;
         } else {
             return to_string($value);
         }
     });
     $String->instantiate = function () {
         return new Str();
     };
     $String->set('prototype', Str::$protoObject);
     $String->setMethods(Str::$classMethods, true, false, true);
     return $String;
 }
Пример #5
0
 /**
  * Creates the global constructor used in user-land
  * @return Func
  */
 static function getGlobalConstructor()
 {
     $Number = new Func(function ($value = 0) {
         $self = Func::getContext();
         if ($self instanceof Number) {
             $self->value = to_number($value);
             return $self;
         } else {
             return to_number($value);
         }
     });
     $Number->instantiate = function () {
         return new Number();
     };
     $Number->set('prototype', Number::$protoObject);
     $Number->setMethods(Number::$classMethods, true, false, true);
     //constants
     $Number->set('NaN', NAN);
     $Number->set('MAX_VALUE', INF);
     $Number->set('MIN_VALUE', -INF);
     $Number->set('NEGATIVE_INFINITY', -INF);
     $Number->set('POSITIVE_INFINITY', INF);
     return $Number;
 }
Пример #6
0
 /**
  * Creates the global constructor used in user-land
  * @return Func
  */
 static function getGlobalConstructor()
 {
     $Date = new Func(function () {
         $date = new Date();
         $date->init(func_get_args());
         return $date;
     });
     $Date->set('prototype', Date::$protoObject);
     $Date->setMethods(Date::$classMethods, true, false, true);
     return $Date;
 }
Пример #7
0
 /**
  * Creates the global constructor used in user-land
  * @return Func
  */
 static function getGlobalConstructor()
 {
     $Array = new Func(function ($value = null) {
         $arr = new Arr();
         $len = func_num_args();
         if ($len === 1 && is_int_or_float($value)) {
             $arr->length = (int) $value;
         } else {
             if ($len > 0) {
                 $arr->init(func_get_args());
             }
         }
         return $arr;
     });
     $Array->set('prototype', Arr::$protoObject);
     $Array->setMethods(Arr::$classMethods, true, false, true);
     return $Array;
 }
Пример #8
0
 /**
  * Creates the global constructor used in user-land
  * @return Func
  */
 static function getGlobalConstructor()
 {
     $Object = new Func(function ($value = null) {
         if ($value === null || $value === Object::$null) {
             return new Object();
         } else {
             return objectify($value);
         }
     });
     $Object->set('prototype', Object::$protoObject);
     $Object->setMethods(Object::$classMethods, true, false, true);
     return $Object;
 }
Пример #9
0
 /**
  * Creates the global constructor used in user-land
  * @return Func
  */
 static function getGlobalConstructor()
 {
     $RegExp = new Func(function () {
         $reg = new RegExp();
         $reg->init(func_get_args());
         return $reg;
     });
     $RegExp->set('prototype', RegExp::$protoObject);
     $RegExp->setMethods(RegExp::$classMethods, true, false, true);
     return $RegExp;
 }
Пример #10
0
        $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 () {
        return 'woof';
    }));
    Test::assert('method override', $dog->callMethod('speak') === 'woof');
    $animal = $Animal->construct();
    Test::assert('method still on parent', $animal->callMethod('speak') === 'hi');
Пример #11
0
 /**
  * Creates the global constructor used in user-land
  * @return Func
  */
 static function getGlobalConstructor()
 {
     $Function = new Func(function ($fn) {
         throw new Ex(Error::create('Cannot construct function at runtime.'));
     });
     $Function->set('prototype', Func::$protoObject);
     $Function->setMethods(Func::$classMethods, true, false, true);
     return $Function;
 }