Esempio n. 1
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)
Esempio n. 2
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)
    def test_get_identifier_reference_empty(self):
        obj = W_BasicObject()
        lex_env = ObjectEnvironment(obj)
        ref = lex_env.get_identifier_reference(u'foo')

        assert ref.base_value is None
        assert ref.referenced == 'foo'
Esempio n. 4
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)
Esempio n. 5
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)
Esempio 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)
Esempio n. 7
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)
    def test_get_identifier_reference(self):
        obj = W_BasicObject()
        lex_env = ObjectEnvironment(obj)

        env_rec = lex_env.environment_record
        env_rec.create_mutuable_binding(u'foo', True)

        ref = lex_env.get_identifier_reference(u'foo')
        assert ref.base_env == env_rec
        assert ref.referenced == 'foo'
    def test_get_identifier_reference_from_parent(self):
        outer_lex_env = DeclarativeEnvironment()
        outer_env_rec = outer_lex_env.environment_record
        outer_env_rec.create_mutuable_binding(u'foo', True)

        obj = W_BasicObject()
        lex_env = ObjectEnvironment(obj, outer_lex_env)

        ref = lex_env.get_identifier_reference(u'foo')
        assert ref.base_env == outer_env_rec
        assert ref.referenced == u'foo'
Esempio n. 10
0
    def run_src(self, src):
        ast = parse_to_ast(src)
        symbol_map = ast.symbol_map
        code = ast_to_bytecode(ast, symbol_map)

        c = JsGlobalCode(code)

        w_global = W_BasicObject()
        object_space.global_object = w_global
        ctx = GlobalExecutionContext(c, w_global)
        res = c.run(ctx)
        return res.value
Esempio n. 11
0
    def test_foo12(self):
        src = u'''
        function fib(n) {
            if(n<2) {
                return n;
            } else {
                return fib(n-1) + fib(n-2);
            }
        }
        return fib(10);
        '''

        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)

        assert res.value == _w(55)
Esempio n. 12
0
    def test_define_property(self):
        obj = W_BasicObject()

        desc = PropertyDescriptor(enumerable=True, configurable=True)
        obj.define_own_property(u'foo', desc)
        assert obj.has_property(u'foo') is True
Esempio n. 13
0
    def test_define_property(self):
        obj = W_BasicObject()

        desc = PropertyDescriptor(enumerable=True, configurable=True)
        obj.define_own_property(u'foo', desc)
        assert obj.has_property(u'foo') is True
Esempio n. 14
0
    def test_put(self):
        obj = W_BasicObject()

        obj.put(u'foo', 1)
        assert obj.get(u'foo') == 1
Esempio n. 15
0
    def test_define_data_property(self):
        obj = W_BasicObject()

        desc = PropertyDescriptor(value=1)
        obj.define_own_property(u'foo', desc)
        assert obj.has_property(u'foo') is True
Esempio n. 16
0
    def test_define_data_property(self):
        obj = W_BasicObject()

        desc = PropertyDescriptor(value=1)
        obj.define_own_property(u'foo', desc)
        assert obj.has_property(u'foo') is True
Esempio n. 17
0
    def test_get(self):
        obj = W_BasicObject()

        desc = PropertyDescriptor(value=1, writable=True)
        obj.define_own_property(u'foo', desc)
        assert obj.get(u'foo') == 1
Esempio n. 18
0
    def test_get(self):
        obj = W_BasicObject()

        desc = PropertyDescriptor(value=1, writable=True)
        obj.define_own_property(u'foo', desc)
        assert obj.get(u'foo') == 1
Esempio n. 19
0
 def test_has_property(self):
     obj = W_BasicObject()
     assert obj.has_property(u'foo') is False
Esempio n. 20
0
    def test_put(self):
        obj = W_BasicObject()

        obj.put(u'foo', 1)
        assert obj.get(u'foo') == 1
Esempio n. 21
0
 def test_has_property(self):
     obj = W_BasicObject()
     assert obj.has_property(u'foo') is False
Esempio n. 22
0
def setup_builtins(global_object):
    from js.object_space import object_space

    # 15.2.4 Properties of the Object Prototype Object
    from js.jsobj import W_BasicObject
    w_ObjectPrototype = W_BasicObject()
    object_space.proto_object = w_ObjectPrototype

    # 15.3.2
    from js.jsobj import W_FunctionConstructor
    w_Function = W_FunctionConstructor()
    put_property(global_object, u'Function', w_Function)

    # 15.3.4 Properties of the Function Prototype Object
    from js.functions import JsNativeFunction

    import js.builtins.function
    empty_func = JsNativeFunction(js.builtins.function.empty, u'Empty')
    w_FunctionPrototype = object_space.new_func(empty_func)
    object_space.assign_proto(w_FunctionPrototype, object_space.proto_object)
    object_space.proto_function = w_FunctionPrototype

    # 15.3.3
    object_space.assign_proto(w_Function, object_space.proto_function)

    # 15.2 Object Objects
    # 15.2.3 Properties of the Object Constructor
    from js.jsobj import W_ObjectConstructor
    w_Object = W_ObjectConstructor()
    object_space.assign_proto(w_Object, object_space.proto_function)

    put_property(w_Object, u'length', _w(1))

    put_property(global_object, u'Object', w_Object)

    # 15.2.3.1 Object.prototype
    put_property(w_Object, u'prototype', w_ObjectPrototype, writable=False, configurable=False, enumerable=False)

    # 14.2.4.1 Object.prototype.constructor
    put_property(w_ObjectPrototype, u'constructor', w_Object)

    import js.builtins.object
    # 15.2.4.2 Object.prototype.toString()
    put_native_function(w_ObjectPrototype, u'toString', js.builtins.object.to_string)
    put_native_function(w_ObjectPrototype, u'toLocaleString', js.builtins.object.to_string)

    # 15.2.4.3 Object.prototype.valueOf()
    put_native_function(w_ObjectPrototype, u'valueOf', js.builtins.object.value_of)

    # 15.3 Function Objects
    # 15.3.3 Properties of the Function Constructor

    # 15.3.3.1 Function.prototype
    put_property(w_Function, u'prototype', w_FunctionPrototype, writable=False, configurable=False, enumerable=False)

    # 15.3.3.2 Function.length
    put_property(w_Function, u'length', _w(1), writable=False, configurable=False, enumerable=False)

    # 14.3.4.1 Function.prototype.constructor
    put_property(w_FunctionPrototype, u'constructor', w_Function)

    # 15.3.4.2 Function.prototype.toString()
    put_native_function(w_FunctionPrototype, u'toString', js.builtins.function.to_string)

    # 15.3.4.3 Function.prototype.apply
    put_intimate_function(w_FunctionPrototype, u'apply', js.builtins.function.js_apply)

    # 15.3.4.4 Function.prototype.call
    put_intimate_function(w_FunctionPrototype, u'call', js.builtins.function.js_call)

    import js.builtins.boolean
    js.builtins.boolean.setup(global_object)

    import js.builtins.number
    js.builtins.number.setup(global_object)

    import js.builtins.string
    js.builtins.string.setup(global_object)

    import js.builtins.array
    js.builtins.array.setup(global_object)

    import js.builtins.js_math
    js.builtins.js_math.setup(global_object)

    import js.builtins.date
    js.builtins.date.setup(global_object)

    import js.builtins.js_global
    js.builtins.js_global.setup(global_object)