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_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_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 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_ast(self, ast): symbol_map = ast.symbol_map from js.jscode import ast_to_bytecode code = ast_to_bytecode(ast, symbol_map) return self.run(code)
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 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_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 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 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 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 js_load(ctx): from js.interpreter import load_file from js.jscode import ast_to_bytecode from js.functions import JsEvalCode from js.execution_context import EvalExecutionContext args = ctx.argv() f = get_arg(args, 0) filename = f.to_string() ast = load_file(filename) symbol_map = ast.symbol_map code = ast_to_bytecode(ast, symbol_map) f = JsEvalCode(code) calling_context = ctx._calling_context_ ctx = EvalExecutionContext(f, calling_context=calling_context) f.run(ctx)
def js_load(ctx): from js.interpreter import load_file from js.jscode import ast_to_bytecode from js.functions import JsEvalCode from js.execution_context import EvalExecutionContext args = ctx.argv() f = get_arg(args, 0) filename = f.to_string() ast = load_file(filename) symbol_map = ast.symbol_map code = ast_to_bytecode(ast, symbol_map) f = JsEvalCode(code) calling_context = ctx._calling_context_ ctx = EvalExecutionContext(f, calling_context=calling_context) f.run(ctx)
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 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)
#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() except FakeParseError, e: #raise JsSyntaxError(msg = unicode(e.msg), src = unicode(src)) raise JsSyntaxError() except LexerError, e: #error_lineno = e.source_pos.lineno #error_pos = e.source_pos.columnno error_msg = u'LexerError' #raise JsSyntaxError(msg = error_msg, src = unicode(src), line = error_lineno, column = error_pos) raise JsSyntaxError(msg=error_msg) symbol_map = ast.symbol_map code = ast_to_bytecode(ast, symbol_map) f = JsEvalCode(code) calling_context = ctx._calling_context_ ctx = EvalExecutionContext(f, calling_context=calling_context) res = f.run(ctx) return res def js_load(ctx): from js.interpreter import load_file from js.jscode import ast_to_bytecode from js.functions import JsEvalCode from js.execution_context import EvalExecutionContext
#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() except FakeParseError, e: #raise JsSyntaxError(msg = unicode(e.msg), src = unicode(src)) raise JsSyntaxError() except LexerError, e: #error_lineno = e.source_pos.lineno #error_pos = e.source_pos.columnno error_msg = u'LexerError' #raise JsSyntaxError(msg = error_msg, src = unicode(src), line = error_lineno, column = error_pos) raise JsSyntaxError(msg=error_msg) symbol_map = ast.symbol_map code = ast_to_bytecode(ast, symbol_map) f = JsEvalCode(code) calling_context = ctx._calling_context_ ctx = EvalExecutionContext(f, calling_context=calling_context) res = f.run(ctx) return res def js_load(ctx): from js.interpreter import load_file from js.jscode import ast_to_bytecode from js.functions import JsEvalCode from js.execution_context import EvalExecutionContext