Exemple #1
0
/**
 * @param Func $fn
 * @return Object
 * @throws Exception
 */
function _new($fn)
{
    if (!$fn instanceof Func) {
        throw new Ex(Error::create(_typeof($fn) . " is not a function"));
    }
    $args = array_slice(func_get_args(), 1);
    return call_user_func_array(array($fn, 'construct'), $args);
}
Exemple #2
0
/**
 * @param Object $obj
 * @param string $name
 * @return mixed
 * @throws Exception
 */
function call_method($obj, $name)
{
    if ($obj === null || $obj === Object::$null) {
        throw new Ex(Error::create("Cannot read property '" . $name . "' of " . to_string($obj)));
    }
    $obj = objectify($obj);
    $fn = $obj->get($name);
    if (!$fn instanceof Func) {
        throw new Ex(Error::create(_typeof($fn) . " is not a function"));
    }
    $args = array_slice(func_get_args(), 2);
    return $fn->apply($obj, $args);
}
Exemple #3
0
    Test::assert('to_number "+Infinity"', to_number('+Infinity') === INF);
    Test::assert('to_number "+1234.5678"', to_number('+1234.5678') === to_number('1234.5678'));
    Test::assert('to_number "+1234.5678e90"', to_number("+1234.5678e90") === to_number("1234.5678e90"));
    Test::assert('to_number "+1234.5678E90"', to_number("+1234.5678E90") === to_number("1234.5678E90"));
    Test::assert('to_number "+1234.5678e-90"', to_number("+1234.5678e-90") === to_number("1234.5678e-90"));
    Test::assert('to_number "+1234.5678E-90"', to_number("+1234.5678E-90") === to_number("1234.5678E-90"));
});
Test::suite('Number object', function () use($Number, $Object) {
    $num = $Number->construct(5.0);
    Test::assert('instanceof', $num instanceof Number);
    Test::assert('type is object', _typeof($num) === 'object');
    Test::assert('has value', $num->value === 5.0);
    Test::assert('to string', $num->callMethod('toString') === '5');
    $num = $Number->call(null, '5');
    Test::assert('is not object', !$num instanceof Str);
    Test::assert('primitive', _typeof($num) === 'number');
    Test::assert('can call on primitive', call_method($num, 'toString') === '5');
    $num = $Number->call(null, '');
    Test::assert('empty coerced', $num === 0.0);
    $num = $Number->callMethod('parseInt', '5.x');
    Test::assert('parseInt 1', $num === 5.0);
    $num = $Number->callMethod('parseInt', '+05.1');
    Test::assert('parseInt 2', $num === 5.0);
    $num = $Number->callMethod('parseInt', ' -15.0');
    Test::assert('parseInt 3', $num === -15.0);
    $num = $Number->callMethod('parseInt', 'x');
    Test::assert('parseInt 4', is_nan($num));
    $num = $Number->callMethod('parseFloat', '-05e2x');
    Test::assert('parseFloat 1', $num === -500.0);
    $num = $Number->callMethod('parseFloat', ' +05.0');
    Test::assert('parseFloat 2', $num === 5.0);