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 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_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_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_foo4(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('RETURN') f = JsFunction(u'foo', code) ctx = FunctionExecutionContext(f, formal_parameters=[u'b'], argv=[_w(21)]) lex = ctx.variable_environment() env_rec = lex.environment_record env_rec.set_mutable_binding(u'a', _w(21), False) res = f.run(ctx) assert res.value == _w(42)
def emit(self, bytecode): from jscode import ast_to_bytecode body_code = ast_to_bytecode(self.body_ast, self.symbol_map) body_code.emit('LOAD_UNDEFINED') from js.functions import JsFunction name = self.name jsfunc = JsFunction(name, body_code) index = self.index bytecode.emit('LOAD_FUNCTION', jsfunc) if index is not None: bytecode.emit('STORE', index, name)