示例#1
0
    def emit(self, bytecode):
        from js.jscode import JsCode
        from js.functions import JsExecutableCode

        trycode = JsCode()
        self.tryblock.emit(trycode)

        tryexec = JsExecutableCode(trycode)

        if self.catchblock:
            catchcode = JsCode()
            self.catchblock.emit(catchcode)
            catchexec = JsExecutableCode(catchcode)
            catchparam = self.catchparam.get_literal()
        else:
            catchexec = None
            catchparam = None

        if self.finallyblock:
            finallycode = JsCode()
            self.finallyblock.emit(finallycode)
            finallyexec = JsExecutableCode(finallycode)
        else:
            finallyexec = None
        bytecode.emit('TRYCATCHBLOCK', tryexec, catchparam, catchexec,
                      finallyexec)
示例#2
0
def test_retlast_undefined_addition():
    jscode = JsCode()
    jsfunc = jscode.make_js_function()
    assert_bytecode_list_eql(jsfunc.opcodes, ['LOAD_UNDEFINED'])

    jscode = JsCode()
    jscode.emit('LOAD_INTCONSTANT', 1)
    jsfunc = jscode.make_js_function()
    assert_bytecode_list_eql(jsfunc.opcodes,
                             ['LOAD_INTCONSTANT 1', 'LOAD_UNDEFINED'])
示例#3
0
def test_retlast_pop_removal():
    jscode = JsCode()
    jscode.emit('POP')
    jsfunc = jscode.make_js_function()
    assert not jsfunc.opcodes

    jscode = JsCode()
    jscode.emit('POP')
    jscode.emit('LABEL', 0)
    jsfunc = jscode.make_js_function()
    assert_bytecode_list_eql(jsfunc.opcodes, ['POP', 'LOAD_UNDEFINED'])
示例#4
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)
示例#5
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)
示例#6
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)
示例#7
0
    def test_foo15(self):
        code = JsCode()
        code.emit('LOAD_INTCONSTANT', 1)
        code.emit('LOAD_INTCONSTANT', 1)
        code.emit('ADD')

        f = JsExecutableCode(code)

        ctx = ExecutionContext(stack_size=f.estimated_stack_size())
        res = f.run(ctx)
        assert res.value == _w(2)
示例#8
0
    def emit(self, bytecode):
        from js.jscode import JsCode
        from js.functions import JsExecutableCode

        self.expr.emit(bytecode)

        body_code = JsCode()
        self.body.emit(body_code)
        body_exec = JsExecutableCode(body_code)

        bytecode.emit('WITH', body_exec)
示例#9
0
    def test_foo2(self):
        code = JsCode()
        code.emit('LOAD_INTCONSTANT', 1)
        code.emit('LOAD_INTCONSTANT', 1)
        code.emit('ADD')
        code.emit('RETURN')

        f = JsFunction(u'foo', code)
        ctx = FunctionExecutionContext(f)
        res = f.run(ctx)

        assert res.value == _w(2)
示例#10
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)
示例#11
0
def test_simple():
    from js.jscode import JsCode
    bytecode = JsCode()
    bytecode.emit('LOAD_FLOATCONSTANT', 2)
    bytecode.emit('LOAD_FLOATCONSTANT', 4)
    bytecode.emit('ADD')

    from js.execution_context import ExecutionContext

    from js.functions import JsExecutableCode
    f = JsExecutableCode(bytecode)

    ctx = ExecutionContext()
    res = f.run(ctx)
    value = res.value
    assert value.ToNumber() == 6.0
示例#12
0
 def compile(self, s):
     ast = self.to_ast(s)
     bytecode = JsCode()
     ast.emit(bytecode)
     return bytecode