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)
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_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)
def Call(self, args=[], this=None, calling_context=None): arg_count = len(args) _args = u'' body = u'' if arg_count == 0: pass elif arg_count == 1: body = args[0].to_string() else: first_arg = args[0].to_string() _args = first_arg k = 2 while k < arg_count: next_arg = args[k - 1].to_string() _args = _args + u',' + next_arg k = k + 1 body = args[k - 1].to_string() src = u'function (' + _args + u') { ' + body + u' };' from js.astbuilder import parse_to_ast from js.jscode import ast_to_bytecode ast = parse_to_ast(src) symbol_map = ast.symbol_map code = ast_to_bytecode(ast, symbol_map) # TODO hackish func = code.opcodes[0].funcobj from js.object_space import object_space scope = object_space.get_global_environment() strict = func.strict params = func.params() w_func = object_space.new_func(func, formal_parameter_list=params, scope=scope, strict=strict) return w_func
def js_eval(ctx): from rpython.rlib.parsing.parsing import ParseError from rpython.rlib.parsing.deterministic import LexerError from js.astbuilder import parse_to_ast from js.jscode import ast_to_bytecode from js.jsobj import W_String from js.functions import JsEvalCode from js.execution_context import EvalExecutionContext from js.astbuilder import FakeParseError from js.exception import JsSyntaxError args = ctx.argv() x = get_arg(args, 0) if not isinstance(x, W_String): from js.completion import NormalCompletion return NormalCompletion(value=x) src = x.to_string() try: ast = parse_to_ast(src) except ParseError, e: #error = e.errorinformation.failure_reasons #error_lineno = e.source_pos.lineno #error_pos = e.source_pos.columnno #raise JsSyntaxError(msg = unicode(error), src = unicode(src), line = error_lineno, column = error_pos) raise JsSyntaxError()
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 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
def test_foo1(self): src = u''' var a = 40; var b = 2; a + b; ''' ast = parse_to_ast(src) interp = Interpreter() res = interp.run_ast(ast) assert res == _w(42)
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)
def run_src(self, src): from js.astbuilder import parse_to_ast from js.runistr import decode_str_utf8 ast = parse_to_ast(decode_str_utf8(src)) return self.run_ast(ast)