Ejemplo n.º 1
0
    def test_foo7(self):
        symbol_map = SymbolMap()
        var_idx_a = symbol_map.add_variable(u'a')
        var_idx_b = symbol_map.add_symbol(u'b')

        code = JsCode(symbol_map)
        code.emit('LOAD_VARIABLE', var_idx_a, u'a')
        code.emit('LOAD_VARIABLE', var_idx_b, u'b')
        code.emit('ADD')
        code.emit('STORE', var_idx_b, u'b')
        code.emit('RETURN')

        outer_env = DeclarativeEnvironment()
        outer_env_rec = outer_env.environment_record

        f = JsFunction(u'foo', code)

        ctx = FunctionExecutionContext(f, scope=outer_env)

        lex_env = ctx.variable_environment()
        env_rec = lex_env.environment_record

        env_rec.set_mutable_binding(u'a', _w(21), False)

        outer_env_rec.create_mutuable_binding(u'b', True)
        outer_env_rec.set_mutable_binding(u'b', _w(21), False)

        res = f.run(ctx)

        assert env_rec.get_binding_value(u'a') == _w(21)
        assert outer_env_rec.get_binding_value(u'b') == _w(42)
        assert res.value == _w(42)
Ejemplo n.º 2
0
    def __init__(self, func, names, args, env, strict=False):
        from js.object_space import _w
        W__Object.__init__(self)
        self.strict = strict
        _len = len(args)
        put_property(self, u'length', _w(_len), writable=True, enumerable=False, configurable=True)

        from js.object_space import object_space
        _map = object_space.new_obj()
        mapped_names = new_map()
        jit.promote(_len)
        indx = _len - 1
        while indx >= 0:
            val = args[indx]
            put_property(self, unicode(str(indx)), val, writable=True, enumerable=True, configurable=True)
            if indx < len(names):
                name = names[indx]
                if strict is False and not mapped_names.contains(name):
                    mapped_names = mapped_names.add(name)
                    g = make_arg_getter(name, env)
                    p = make_arg_setter(name, env)
                    desc = PropertyDescriptor(setter=p, getter=g, configurable=True)
                    _map.define_own_property(unicode(str(indx)), desc, False)
            indx = indx - 1

        if not mapped_names.empty():
            self._paramenter_map_ = _map

        if strict is False:
            put_property(self, u'callee', _w(func), writable=True, enumerable=False, configurable=True)
        else:
            # 10.6 14 thrower
            pass
Ejemplo n.º 3
0
    def test_foo8(self):
        symbol_map = SymbolMap()
        var_idx_a = symbol_map.add_variable(u'a')
        var_idx_b = symbol_map.add_variable(u'b')
        var_idx_c = symbol_map.add_variable(u'c')

        code = JsCode(symbol_map)
        code.emit('LOAD_INTCONSTANT', 21)
        code.emit('STORE', var_idx_a, u'a')
        code.emit('POP')
        code.emit('LOAD_INTCONSTANT', 21)
        code.emit('STORE', var_idx_b, u'b')
        code.emit('POP')
        code.emit('LOAD_VARIABLE', var_idx_a, u'a')
        code.emit('LOAD_VARIABLE', var_idx_b, u'b')
        code.emit('ADD')
        code.emit('STORE', var_idx_c, u'c')
        code.emit('RETURN')

        f = JsGlobalCode(code)

        w_global = W_BasicObject()

        ctx = GlobalExecutionContext(f, w_global)
        res = f.run(ctx)

        lex_env = ctx.variable_environment()
        env_rec = lex_env.environment_record

        assert env_rec.get_binding_value(u'a') == _w(21)
        assert env_rec.get_binding_value(u'b') == _w(21)
        assert env_rec.get_binding_value(u'c') == _w(42)
        assert res.value == _w(42)
Ejemplo n.º 4
0
    def test_foo11(self):
        src = u'''
        function f(b) {
            var c = 21;
            return b + c;
        }
        var a = f(21);
        return a;
        '''

        ast = parse_to_ast(src)
        symbol_map = ast.symbol_map
        code = ast_to_bytecode(ast, symbol_map)

        f = JsGlobalCode(code)

        w_global = W_BasicObject()
        ctx = GlobalExecutionContext(f, w_global)
        res = f.run(ctx)

        lex_env = ctx.variable_environment()
        env_rec = lex_env.environment_record

        assert env_rec.get_binding_value(u'a') == _w(42)
        assert env_rec.has_binding(u'b') is False
        assert env_rec.has_binding(u'c') is False
        assert res.value == _w(42)
Ejemplo n.º 5
0
    def test_foo9(self):
        src = u'''
        var a = 21;
        var b = 21;
        var c = a + b;
        return c;
        '''

        ast = parse_to_ast(src)
        symbol_map = ast.symbol_map
        code = ast_to_bytecode(ast, symbol_map)

        f = JsGlobalCode(code)

        w_global = W_BasicObject()
        ctx = GlobalExecutionContext(f, w_global)
        res = f.run(ctx)

        lex_env = ctx.variable_environment()
        env_rec = lex_env.environment_record

        assert env_rec.get_binding_value(u'a') == _w(21)
        assert env_rec.get_binding_value(u'b') == _w(21)
        assert env_rec.get_binding_value(u'c') == _w(42)
        assert res.value == _w(42)
Ejemplo n.º 6
0
    def test_foo11(self):
        src = u'''
        function f(b) {
            var c = 21;
            return b + c;
        }
        var a = f(21);
        return a;
        '''

        ast = parse_to_ast(src)
        symbol_map = ast.symbol_map
        code = ast_to_bytecode(ast, symbol_map)

        f = JsGlobalCode(code)

        w_global = W_BasicObject()
        ctx = GlobalExecutionContext(f, w_global)
        res = f.run(ctx)

        lex_env = ctx.variable_environment()
        env_rec = lex_env.environment_record

        assert env_rec.get_binding_value(u'a') == _w(42)
        assert env_rec.has_binding(u'b') is False
        assert env_rec.has_binding(u'c') is False
        assert res.value == _w(42)
Ejemplo n.º 7
0
    def test_foo7(self):
        symbol_map = SymbolMap()
        var_idx_a = symbol_map.add_variable(u'a')
        var_idx_b = symbol_map.add_symbol(u'b')

        code = JsCode(symbol_map)
        code.emit('LOAD_VARIABLE', var_idx_a, u'a')
        code.emit('LOAD_VARIABLE', var_idx_b, u'b')
        code.emit('ADD')
        code.emit('STORE', var_idx_b, u'b')
        code.emit('RETURN')

        outer_env = DeclarativeEnvironment()
        outer_env_rec = outer_env.environment_record

        f = JsFunction(u'foo', code)

        ctx = FunctionExecutionContext(f, scope=outer_env)

        lex_env = ctx.variable_environment()
        env_rec = lex_env.environment_record

        env_rec.set_mutable_binding(u'a', _w(21), False)

        outer_env_rec.create_mutuable_binding(u'b', True)
        outer_env_rec.set_mutable_binding(u'b', _w(21), False)

        res = f.run(ctx)

        assert env_rec.get_binding_value(u'a') == _w(21)
        assert outer_env_rec.get_binding_value(u'b') == _w(42)
        assert res.value == _w(42)
Ejemplo n.º 8
0
    def test_foo8(self):
        symbol_map = SymbolMap()
        var_idx_a = symbol_map.add_variable(u'a')
        var_idx_b = symbol_map.add_variable(u'b')
        var_idx_c = symbol_map.add_variable(u'c')

        code = JsCode(symbol_map)
        code.emit('LOAD_INTCONSTANT', 21)
        code.emit('STORE', var_idx_a, u'a')
        code.emit('POP')
        code.emit('LOAD_INTCONSTANT', 21)
        code.emit('STORE', var_idx_b, u'b')
        code.emit('POP')
        code.emit('LOAD_VARIABLE', var_idx_a, u'a')
        code.emit('LOAD_VARIABLE', var_idx_b, u'b')
        code.emit('ADD')
        code.emit('STORE', var_idx_c, u'c')
        code.emit('RETURN')

        f = JsGlobalCode(code)

        w_global = W_BasicObject()

        ctx = GlobalExecutionContext(f, w_global)
        res = f.run(ctx)

        lex_env = ctx.variable_environment()
        env_rec = lex_env.environment_record

        assert env_rec.get_binding_value(u'a') == _w(21)
        assert env_rec.get_binding_value(u'b') == _w(21)
        assert env_rec.get_binding_value(u'c') == _w(42)
        assert res.value == _w(42)
Ejemplo n.º 9
0
    def test_foo9(self):
        src = u'''
        var a = 21;
        var b = 21;
        var c = a + b;
        return c;
        '''

        ast = parse_to_ast(src)
        symbol_map = ast.symbol_map
        code = ast_to_bytecode(ast, symbol_map)

        f = JsGlobalCode(code)

        w_global = W_BasicObject()
        ctx = GlobalExecutionContext(f, w_global)
        res = f.run(ctx)

        lex_env = ctx.variable_environment()
        env_rec = lex_env.environment_record

        assert env_rec.get_binding_value(u'a') == _w(21)
        assert env_rec.get_binding_value(u'b') == _w(21)
        assert env_rec.get_binding_value(u'c') == _w(42)
        assert res.value == _w(42)
Ejemplo n.º 10
0
    def test_foo5(self):
        symbol_map = SymbolMap()
        var_idx_a = symbol_map.add_variable(u'a')
        var_idx_b = symbol_map.add_parameter(u'b')

        code = JsCode(symbol_map)
        code.emit('LOAD_VARIABLE', var_idx_a, u'a')
        code.emit('LOAD_VARIABLE', var_idx_b, u'b')
        code.emit('ADD')
        code.emit('STORE', var_idx_a, u'a')
        code.emit('RETURN')

        f = JsFunction(u'foo', code)
        ctx = FunctionExecutionContext(f,
                                       formal_parameters=[u'b'],
                                       argv=[_w(21)])

        lex_env = ctx.variable_environment()
        env_rec = lex_env.environment_record
        env_rec.set_mutable_binding(u'a', _w(21), False)

        res = f.run(ctx)

        assert env_rec.get_binding_value(u'a') == _w(42)
        assert res.value == _w(42)
Ejemplo n.º 11
0
 def Call(self, args=[], this=None, calling_context=None):
     from js.object_space import _w, isnull_or_undefined
     if len(args) >= 1 and not isnull_or_undefined(args[0]):
         boolval = args[0].to_boolean()
         return _w(boolval)
     else:
         return _w(False)
Ejemplo n.º 12
0
def js_apply(ctx):
    from js.object_space import isnull_or_undefined
    func = ctx.this_binding()
    args = ctx.argv()

    this_arg = get_arg(args, 0)
    arg_array = get_arg(args, 1)

    if isnull_or_undefined(arg_array):
        res = func.Call(args=[], this=this_arg, calling_context=ctx)
        compl = NormalCompletion(value=_w(res))
        return compl

    from js.jsobj import W_BasicObject
    if not isinstance(arg_array, W_BasicObject):
        raise JsTypeError(u'')

    length = arg_array.get(u'length')
    n = length.ToUInt32()
    arg_list = []
    index = 0
    while index < n:
        index_name = unicode(str(index))
        next_arg = arg_array.get(index_name)
        arg_list.append(next_arg)
        index += 1

    res = func.Call(args=arg_list, this=this_arg, calling_context=ctx)
    compl = NormalCompletion(value=_w(res))
    return compl
Ejemplo n.º 13
0
def setup(global_object):
    from js.builtins import put_native_function, put_property
    from js.object_space import object_space

    #String
    # 15.5.1
    from js.jsobj import W_StringConstructor
    w_String = W_StringConstructor()
    object_space.assign_proto(w_String, object_space.proto_function)
    put_property(w_String, u'length', _w(1), writable=False, enumerable=False, configurable=False)
    put_property(global_object, u'String', w_String)

    # 15.5.4
    w_StringPrototype = W_StringObject(_w(u''))
    object_space.assign_proto(w_StringPrototype, object_space.proto_object)

    # 15.5.3.1
    object_space.proto_string = w_StringPrototype

    put_property(w_String, u'prototype', w_StringPrototype, writable=False, enumerable=False, configurable=False)

    # 15.5.3.2
    put_native_function(w_String, u'fromCharCode', from_char_code, params=[u'char1'])

    # 15.5.4.1
    put_property(w_StringPrototype, u'constructor', w_String)

    # 15.5.4.2
    put_native_function(w_StringPrototype, u'toString', to_string)

    # 15.5.4.3
    put_native_function(w_StringPrototype, u'valueOf', value_of)

    # 15.5.4.4
    put_native_function(w_StringPrototype, u'charAt', char_at, params=[u'pos'])

    # 15.5.4.5
    put_native_function(w_StringPrototype, u'charCodeAt', char_code_at, params=[u'pos'])

    # 15.5.4.6
    put_native_function(w_StringPrototype, u'concat', concat, params=[u'string1'])

    # 15.5.4.7
    put_native_function(w_StringPrototype, u'indexOf', index_of, params=[u'searchstring'])

    # 15.5.4.8
    put_native_function(w_StringPrototype, u'lastIndexOf', last_index_of, params=[u'searchstring'])

    # 15.5.4.14
    put_native_function(w_StringPrototype, u'split', split, params=[u'separator', u'limit'])

    # 15.5.4.15
    put_native_function(w_StringPrototype, u'substring', substring, params=[u'start', u'end'])

    # 15.5.4.16
    put_native_function(w_StringPrototype, u'toLowerCase', to_lower_case)

    # 15.5.4.18
    put_native_function(w_StringPrototype, u'toUpperCase', to_upper_case)
Ejemplo n.º 14
0
    def Call(self, args=[], this=None, calling_context=None):
        from js.object_space import _w, isnull_or_undefined, isundefined

        if len(args) >= 1 and not isnull_or_undefined(args[0]):
            return _w(args[0].ToNumber())
        elif len(args) >= 1 and isundefined(args[0]):
            return _w(NAN)
        else:
            return _w(0.0)
Ejemplo n.º 15
0
    def test_foo13(self):
        def f(this, args):
            a = args[0].ToInteger()
            return _w(a + 1)

        func = JsNativeFunction(f)
        ctx = FunctionExecutionContext(func, argv=[_w(41)])
        res = func.run(ctx)

        assert res.value == _w(42)
Ejemplo n.º 16
0
    def test_foo13(self):
        def f(this, args):
            a = args[0].ToInteger()
            return _w(a + 1)

        func = JsNativeFunction(f)
        ctx = FunctionExecutionContext(func, argv=[_w(41)])
        res = func.run(ctx)

        assert res.value == _w(42)
Ejemplo n.º 17
0
    def test_foo3(self):
        symbol_map = SymbolMap()
        var_idx = symbol_map.add_parameter(u'a')

        code = JsCode(symbol_map)
        code.emit('LOAD_VARIABLE', var_idx, u'a')
        code.emit('RETURN')

        f = JsFunction(u'foo', code)
        ctx = FunctionExecutionContext(f, argv=[_w(42)])

        res = f.run(ctx)
        assert res.value == _w(42)
Ejemplo n.º 18
0
    def test_foo3(self):
        symbol_map = SymbolMap()
        var_idx = symbol_map.add_parameter(u'a')

        code = JsCode(symbol_map)
        code.emit('LOAD_VARIABLE', var_idx, u'a')
        code.emit('RETURN')

        f = JsFunction(u'foo', code)
        ctx = FunctionExecutionContext(f, argv=[_w(42)])

        res = f.run(ctx)
        assert res.value == _w(42)
Ejemplo n.º 19
0
def pop(this, args):
    o = this.ToObject()
    lenVal = o.get(u'length')
    l = lenVal.ToUInt32()

    if l == 0:
        o.put(u'length', _w(0))
        return newundefined()
    else:
        indx = l - 1
        indxs = unicode(str(indx))
        element = o.get(indxs)
        o.delete(indxs, True)
        o.put(u'length', _w(indx))
        return element
Ejemplo n.º 20
0
def pop(this, args):
    o = this.ToObject()
    lenVal = o.get(u'length')
    l = lenVal.ToUInt32()

    if l == 0:
        o.put(u'length', _w(0))
        return newundefined()
    else:
        indx = l - 1
        indxs = unicode(str(indx))
        element = o.get(indxs)
        o.delete(indxs, True)
        o.put(u'length', _w(indx))
        return element
Ejemplo n.º 21
0
def shift(this, args):
    o = this.ToObject()
    l = o.get(u'length').ToUInt32()

    if l == 0:
        o.put(u'length', _w(0))
        return newundefined()
    else:
        new_length = l - 1
        element = o.get(u"0")
        for i in xrange(0, new_length):
            indx = unicode(str(i))
            next_indx = unicode(str(i + 1))
            o.put(indx, o.get(next_indx))
        o.put(u'length', _w(new_length))
        return element
Ejemplo n.º 22
0
def change_wdate(w_date, time_change, local=False):
    t = make_jstime(w_date)

    if time_change.has_year:
        t.set_year(time_change.year, local)

    if time_change.has_month:
        t.set_month(time_change.month, local)

    if time_change.has_day:
        t.set_day(time_change.day, local)

    if time_change.has_hour:
        t.set_hour(time_change.hour, local)

    if time_change.has_minute:
        t.set_min(time_change.minute, local)

    if time_change.has_second:
        t.set_sec(time_change.second, local)

    if time_change.has_msecond:
        t.set_msec(time_change.msecond, local)

    w_t = _w(t.to_msec_epoc())
    w_date.set_primitive_value(w_t)

    return w_t
Ejemplo n.º 23
0
    def eval(self, ctx):
        from js.reference import Reference
        from js.exception import JsSyntaxError

        # 11.4.1
        ref = ctx.get_ref(self.name, self.index)
        if not isinstance(ref, Reference):
            res = True
        if ref.is_unresolvable_reference():
            if ref.is_strict_reference():
                raise JsSyntaxError()
            res = True
        if ref.is_property_reference():
            obj = ref.get_base().ToObject()
            res = obj.delete(ref.get_referenced_name(),
                             ref.is_strict_reference())
        else:
            if ref.is_strict_reference():
                raise JsSyntaxError()
            bindings = ref.base_env
            res = bindings.delete_binding(ref.get_referenced_name())

        if res is True:
            ctx.forget_ref(self.name, self.index)
        ctx.stack_append(_w(res))
Ejemplo n.º 24
0
    def test_foo20(self):
        src = u'''
        ;
        '''

        ast = parse_to_ast(src)
        symbol_map = ast.symbol_map
        code = ast_to_bytecode(ast, symbol_map)

        global_code = JsGlobalCode(code)
        global_object = W_BasicObject()
        global_ctx = GlobalExecutionContext(global_code, global_object)

        src = u'''
        a = 1;
        '''

        ast = parse_to_ast(src)
        symbol_map = ast.symbol_map
        code = ast_to_bytecode(ast, symbol_map)

        f = JsEvalCode(code)

        ctx = EvalExecutionContext(f, calling_context=global_ctx)
        res = f.run(ctx)

        assert res.value == _w(1)
Ejemplo n.º 25
0
    def test_foo19(self):
        src = u'''
        function x() { d=2; return d;}; x();
        '''

        res = self.run_src(src)
        assert res == _w(2)
Ejemplo n.º 26
0
    def test_foo14(self):
        def f(this, args):
            a = args[0].ToInteger()
            return _w(a + 1)

        func = JsNativeFunction(f)

        from js.jsobj import W__Function
        w_func = W__Function(func)

        w_global = W_BasicObject()
        w_global.put(u'f', w_func)

        src = u'''
        return f(41);
        '''

        ast = parse_to_ast(src)
        symbol_map = ast.symbol_map
        code = ast_to_bytecode(ast, symbol_map)

        c = JsGlobalCode(code)
        ctx = GlobalExecutionContext(c, w_global)
        res = c.run(ctx)

        assert res.value == _w(42)
Ejemplo n.º 27
0
        def f(testfile):
            interp = Interpreter({'no-exception-jseval': True})

            shell_src = load_file(str(shellpath))
            interp.run_src(shell_src)
            test_src = load_file(testfile)
            interp.run_src(test_src)

            global_object = interp.global_object
            testcases = global_object.get(u'testcases')

            testcount = testcases.get(u'length').ToInt32()

            run_test_func = global_object.get(u'run_test')

            test_results = []

            for number in xrange(testcount):
                w_test_number = _w(number)
                result_obj = run_test_func.Call(args=[w_test_number])
                result_passed = result_obj.get(u'passed').to_boolean()
                result_reason = str(result_obj.get(u'reason').to_string())
                test_results.append({'number': number, 'passed': result_passed, 'reason': result_reason})

            return test_results
Ejemplo n.º 28
0
def shift(this, args):
    o = this.ToObject()
    l = o.get(u'length').ToUInt32()

    if l == 0:
        o.put(u'length', _w(0))
        return newundefined()
    else:
        new_length = l - 1
        element = o.get(u"0")
        for i in xrange(0, new_length):
            indx = unicode(str(i))
            next_indx = unicode(str(i + 1))
            o.put(indx, o.get(next_indx))
        o.put(u'length', _w(new_length))
        return element
Ejemplo n.º 29
0
    def test_foo14(self):
        def f(this, args):
            a = args[0].ToInteger()
            return _w(a + 1)

        func = JsNativeFunction(f)

        from js.jsobj import W__Function
        w_func = W__Function(func)

        w_global = W_BasicObject()
        w_global.put(u'f', w_func)

        src = u'''
        return f(41);
        '''

        ast = parse_to_ast(src)
        symbol_map = ast.symbol_map
        code = ast_to_bytecode(ast, symbol_map)

        c = JsGlobalCode(code)
        ctx = GlobalExecutionContext(c, w_global)
        res = c.run(ctx)

        assert res.value == _w(42)
Ejemplo n.º 30
0
def division(ctx, nleft, nright):
    fleft = nleft.ToNumber()
    fright = nright.ToNumber()
    if isnan(fleft) or isnan(fright):
        return w_NAN

    if isinf(fleft) and isinf(fright):
        return w_NAN

    if isinf(fleft) and fright == 0:
        s = sign_of(fleft, fright)
        return w_signed_inf(s)

    if isinf(fright):
        return _w(0)

    if fleft == 0 and fright == 0:
        return w_NAN

    if fright == 0:
        s = sign_of(fleft, fright)
        return w_signed_inf(s)

    val = fleft / fright
    return W_FloatNumber(val)
Ejemplo n.º 31
0
    def test_foo3(self):
        interp = Interpreter()
        res = interp.run_src('var a = 40;')
        res = interp.run_src('var b = 2;')
        res = interp.run_src('a + b;')

        assert res == _w(42)
Ejemplo n.º 32
0
    def _define_own_idx_property(self, idx, desc, throw=False, current_desc=None, prop=None):
        if current_desc is None:
            current_desc = self._get_idx_property(idx)

        from js.object_space import _w
        old_len_desc = self.get_own_property(u'length')
        assert old_len_desc is not None
        old_len = old_len_desc.value.ToUInt32()

        # a
        index = idx
        # b
        if index >= old_len and old_len_desc.writable is False:
            return _ireject(throw, idx)

        # c
        succeeded = self._define_own_int_property(idx, desc, False, current_desc, prop)
        # d
        if succeeded is False:
            return _ireject(throw, idx)

        # e
        if index >= old_len:
            old_len_desc.value = _w(index + 1)
            res = W_BasicObject.define_own_property(self, u'length', old_len_desc, False)
            assert res is True
        # f
        return True
Ejemplo n.º 33
0
    def test_foo20(self):
        src = u'''
        ;
        '''

        ast = parse_to_ast(src)
        symbol_map = ast.symbol_map
        code = ast_to_bytecode(ast, symbol_map)

        global_code = JsGlobalCode(code)
        global_object = W_BasicObject()
        global_ctx = GlobalExecutionContext(global_code, global_object)

        src = u'''
        a = 1;
        '''

        ast = parse_to_ast(src)
        symbol_map = ast.symbol_map
        code = ast_to_bytecode(ast, symbol_map)

        f = JsEvalCode(code)

        ctx = EvalExecutionContext(f, calling_context=global_ctx)
        res = f.run(ctx)

        assert res.value == _w(1)
Ejemplo n.º 34
0
    def __init__(self, function_body, formal_parameter_list=[], scope=None, strict=False):
        W_BasicFunction.__init__(self)
        from js.object_space import _w, newnull, object_space
        self._function_ = function_body
        self._scope_ = scope
        self._params_ = formal_parameter_list
        self._strict_ = strict

        # 13.2 Creating Function Objects
        # 14.
        _len = len(formal_parameter_list)
        # 15.
        put_property(self, u'length', _w(_len), writable=False, enumerable=False, configurable=False)
        # 16.
        proto_obj = object_space.new_obj()
        # 17.
        put_property(proto_obj, u'constructor', self, writable=True, enumerable=False, configurable=True)
        # 18.
        put_property(self, u'prototype', proto_obj, writable=True, enumerable=False, configurable=False)

        if strict is True:
            raise NotImplementedError()
        else:
            put_property(self, u'caller', newnull(), writable=True, enumerable=False, configurable=False)
            put_property(self, u'arguments', newnull(), writable=True, enumerable=False, configurable=False)
Ejemplo n.º 35
0
def change_wdate(w_date, time_change, local=False):
    t = make_jstime(w_date)

    if time_change.has_year:
        t.set_year(time_change.year, local)

    if time_change.has_month:
        t.set_month(time_change.month, local)

    if time_change.has_day:
        t.set_day(time_change.day, local)

    if time_change.has_hour:
        t.set_hour(time_change.hour, local)

    if time_change.has_minute:
        t.set_min(time_change.minute, local)

    if time_change.has_second:
        t.set_sec(time_change.second, local)

    if time_change.has_msecond:
        t.set_msec(time_change.msecond, local)

    w_t = _w(t.to_msec_epoc())
    w_date.set_primitive_value(w_t)

    return w_t
Ejemplo n.º 36
0
    def test_foo19(self):
        src = u'''
        function x() { d=2; return d;}; x();
        '''

        res = self.run_src(src)
        assert res == _w(2)
Ejemplo n.º 37
0
    def test_foo18(self):
        src = u'''
        a = 42;
        this.a;
        '''

        res = self.run_src(src)
        assert res == _w(42)
Ejemplo n.º 38
0
def assertv(code, value):
    from js.interpreter import Interpreter
    from js.object_space import _w

    jsint = Interpreter()
    ret_val = jsint.run_src(code)

    assert ret_val == _w(value)
Ejemplo n.º 39
0
 def test_foo16(self):
     src = u'''
     a = 1;
     b = 41;
     a + b;
     '''
     res = self.run_src(src)
     assert res == _w(42)
Ejemplo n.º 40
0
    def test_foo18(self):
        src = u'''
        a = 42;
        this.a;
        '''

        res = self.run_src(src)
        assert res == _w(42)
Ejemplo n.º 41
0
 def eval(self, ctx):
     value = ctx.stack_pop()
     if isint(value):
         num = value
     else:
         num = _w(value.ToNumber())
     newvalue = decrement(ctx, num)
     ctx.stack_append(newvalue)
Ejemplo n.º 42
0
 def test_foo16(self):
     src = u'''
     a = 1;
     b = 41;
     a + b;
     '''
     res = self.run_src(src)
     assert res == _w(42)
Ejemplo n.º 43
0
 def eval(self, ctx):
     rval = ctx.stack_pop()
     lval = ctx.stack_pop()
     from js.jsobj import W_BasicObject
     if not isinstance(rval, W_BasicObject):
         raise JsTypeError(u'TypeError')
     res = rval.has_instance(lval)
     ctx.stack_append(_w(res))
Ejemplo n.º 44
0
 def eval(self, ctx):
     value = ctx.stack_pop()
     if isint(value):
         num = value
     else:
         num = _w(value.ToNumber())
     newvalue = decrement(ctx, num)
     ctx.stack_append(newvalue)
Ejemplo n.º 45
0
 def eval(self, ctx):
     rval = ctx.stack_pop()
     lval = ctx.stack_pop()
     from js.jsobj import W_BasicObject
     if not isinstance(rval, W_BasicObject):
         raise JsTypeError(u'TypeError')
     res = rval.has_instance(lval)
     ctx.stack_append(_w(res))
Ejemplo n.º 46
0
def setup(global_object):
    from js.builtins import put_native_function, put_property
    from js.jsobj import W_Math
    from js.object_space import object_space

    # 15.8
    w_Math = W_Math()
    object_space.assign_proto(w_Math)
    put_property(global_object, u'Math', w_Math)

    put_native_function(w_Math, u'abs', js_abs, params=[u'x'])
    put_native_function(w_Math, u'floor', floor, params=[u'x'])
    put_native_function(w_Math, u'round', js_round, params=[u'x'])
    put_native_function(w_Math, u'random', js_random)
    put_native_function(w_Math, u'min', js_min, params=[u'value1', u'value2'])
    put_native_function(w_Math, u'max', js_max, params=[u'value1', u'value2'])
    put_native_function(w_Math, u'pow', js_pow, params=[u'x', u'y'])
    put_native_function(w_Math, u'sqrt', js_sqrt, params=[u'x'])
    put_native_function(w_Math, u'log', js_log, params=[u'x'])
    put_native_function(w_Math, u'sin', js_sin, params=[u'x'])
    put_native_function(w_Math, u'tan', js_tan, params=[u'x'])
    put_native_function(w_Math, u'acos', js_acos, params=[u'x'])
    put_native_function(w_Math, u'asin', js_asin, params=[u'x'])
    put_native_function(w_Math, u'atan', js_atan, params=[u'x'])
    put_native_function(w_Math, u'atan2', js_atan2, params=[u'y', u'x'])
    put_native_function(w_Math, u'ceil', js_ceil, params=[u'x'])
    put_native_function(w_Math, u'cos', js_cos, params=[u'x'])
    put_native_function(w_Math, u'exp', js_exp, params=[u'x'])

    # 15.8.1

    # 15.8.1.1
    put_property(w_Math, u'E', _w(E), writable=False, enumerable=False, configurable=False)

    # 15.8.1.2
    put_property(w_Math, u'LN10', _w(LN10), writable=False, enumerable=False, configurable=False)

    # 15.8.1.3
    put_property(w_Math, u'LN2', _w(LN2), writable=False, enumerable=False, configurable=False)

    # 15.8.1.4
    put_property(w_Math, u'LOG2E', _w(LOG2E), writable=False, enumerable=False, configurable=False)

    # 15.8.1.5
    put_property(w_Math, u'LOG10E', _w(LOG10E), writable=False, enumerable=False, configurable=False)

    # 15.8.1.6
    put_property(w_Math, u'PI', _w(PI), writable=False, enumerable=False, configurable=False)

    # 15.8.1.7
    put_property(w_Math, u'SQRT1_2', _w(SQRT1_2), writable=False, enumerable=False, configurable=False)

    # 15.8.1.8
    put_property(w_Math, u'SQRT2', _w(SQRT2), writable=False, enumerable=False, configurable=False)
Ejemplo n.º 47
0
def setup(global_object):
    from js.builtins import put_property, put_native_function
    from js.object_space import object_space

    # 15.7.2
    from js.jsobj import W_NumberConstructor
    w_Number = W_NumberConstructor()
    object_space.assign_proto(w_Number, object_space.proto_function)
    put_property(global_object, u'Number', w_Number)

    # 15.7.3
    put_property(w_Number, u'length', _w(1), writable=False, enumerable=False, configurable=False)

    # 15.7.4
    w_NumberPrototype = W_NumericObject(_w(0))
    object_space.assign_proto(w_NumberPrototype, object_space.proto_object)
    object_space.proto_number = w_NumberPrototype

    # 15.7.4.1
    put_property(w_NumberPrototype, u'constructor', w_Number)

    # 15.7.4.2
    put_native_function(w_NumberPrototype, u'toString', to_string)

    # 15.7.4.4
    put_native_function(w_NumberPrototype, u'valueOf', value_of)

    # 15.7.3.1
    put_property(w_Number, u'prototype', w_NumberPrototype, writable=False, enumerable=False, configurable=False)

    # 15.7.3.2
    put_property(w_Number, u'MAX_VALUE', w_MAX_VALUE, writable=False, configurable=False, enumerable=False)

    # 15.7.3.3
    put_property(w_Number, u'MIN_VALUE', w_MIN_VALUE, writable=False, configurable=False, enumerable=False)

    # 15.7.3.4
    put_property(w_Number, u'NaN', w_NAN, writable=False, configurable=False, enumerable=False)

    # 15.7.3.5
    put_property(w_Number, u'POSITIVE_INFINITY', w_POSITIVE_INFINITY, writable=False, configurable=False, enumerable=False)

    # 15.7.3.6
    put_property(w_Number, u'NEGATIVE_INFINITY', w_NEGATIVE_INFINITY, writable=False, configurable=False, enumerable=False)
Ejemplo n.º 48
0
    def eval(self, ctx):
        rval = ctx.stack_pop()
        lval = ctx.stack_pop()

        rnum = rval.ToUInt32()
        lnum = lval.ToInt32()
        shift_count = rnum & 0x1F
        res = lnum >> shift_count

        ctx.stack_append(_w(res))
Ejemplo n.º 49
0
    def eval(self, ctx):
        rval = ctx.stack_pop()
        lval = ctx.stack_pop()

        rnum = rval.ToUInt32()
        lnum = lval.ToInt32()
        shift_count = rnum & 0x1F
        res = lnum >> shift_count

        ctx.stack_append(_w(res))
Ejemplo n.º 50
0
    def run(self, ctx):
        from js.completion import ReturnCompletion

        args = ctx.argv()
        this = ctx.this_binding()
        assert isinstance(self, JsNativeFunction)
        res = self._function_(this, args)
        w_res = _w(res)
        compl = ReturnCompletion(value=w_res)
        return compl