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)
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)
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)
def emit(self, bytecode): code = JsCode() if self.body is not None: self.body.emit(code) funcobj = code.make_js_function(self.name, self.params) bytecode.emit('DECLARE_FUNCTION', funcobj) if self.name is None: bytecode.emit('LOAD_FUNCTION', funcobj)
def assertv(code, value): jsint = interpreter.Interpreter() ctx = jsint.w_Global try: bytecode = JsCode() interpreter.load_source(code, '').emit(bytecode) func = bytecode.make_js_function() code_val = func.run(ExecutionContext([ctx])) except ThrowException, excpt: code_val = excpt.exception
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)
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'])
def run(self, script, interactive=False): """run the interpreter""" bytecode = JsCode() script.emit(bytecode) if not we_are_translated(): # debugging self._code = bytecode func = bytecode.make_js_function() if interactive: return func.run(self.global_context) else: func.run(self.global_context)
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)
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'])
def Call(self, ctx, args=[], this=None): tam = len(args) if tam >= 1: fbody = args[tam-1].ToString(ctx) argslist = [] for i in range(tam-1): argslist.append(args[i].ToString(ctx)) fargs = ','.join(argslist) functioncode = "function (%s) {%s}"%(fargs, fbody) else: functioncode = "function () {}" #remove program and sourcelements node funcnode = parse(functioncode).children[0].children[0] ast = ASTBUILDER.dispatch(funcnode) bytecode = JsCode() ast.emit(bytecode) func = bytecode.make_js_function() return func.run(ctx)
def test_foo1(self): code = JsCode() code.emit('LOAD_INTCONSTANT', 1) code.emit('LOAD_INTCONSTANT', 1) code.emit('ADD') code.emit('RETURN') f = JsExecutableCode(code) ctx = ExecutionContext(stack_size=f.estimated_stack_size()) res = f.run(ctx) assert res.value == _w(2)
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)
def test_append(self): code = """ function f() { for(i = 0; i < 100; i++){ } } f(); """ jsint = interpreter.Interpreter() ctx = jsint.w_Global bytecode = JsCode() interpreter.load_source(code, '').emit(bytecode) func = bytecode.make_js_function() def interp_w(c): jitdriver.set_param("inlining", True) code_val = func.run(ExecutionContext([ctx])) interp_w(1) self.meta_interp(interp_w, [6], listcomp=True, backendopt=True, listops=True)
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)
def test_simple(): bytecode = JsCode() bytecode.emit('LOAD_FLOATCONSTANT', 2) bytecode.emit('LOAD_FLOATCONSTANT', 4) bytecode.emit('ADD') bytecode.emit('POP') func = bytecode.make_js_function() res = func.run(ExecutionContext([W_Object()]), check_stack=False) assert res.ToNumber(None) == 6.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
def emit(self, bytecode): # a bit naive operator for now trycode = JsCode() self.tryblock.emit(trycode) tryfunc = trycode.make_js_function() if self.catchblock: catchcode = JsCode() self.catchblock.emit(catchcode) catchfunc = catchcode.make_js_function() else: catchfunc = None if self.finallyblock: finallycode = JsCode() self.finallyblock.emit(finallycode) finallyfunc = finallycode.make_js_function() else: finallyfunc = None bytecode.emit('TRYCATCHBLOCK', tryfunc, self.catchparam.get_literal(), catchfunc, finallyfunc)
def compile(self, s): ast = self.to_ast(s) bytecode = JsCode() ast.emit(bytecode) return bytecode
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)