def test_044_make_setter(self): f = Function(None, None, None) obj = Object() self.run_test_executor([[OpCode.PUSH, obj], [OpCode.PUSH, f], [OpCode.PUSH, 'test'], [OpCode.MAKE_SETTER]], [obj], {}, {}) self.assertTrue(isinstance(obj.test, Property)) self.assertEqual(obj.test.setter, f) self.assertEqual(obj.test.getter, None) f2 = Function(None, None, None) self.run_test_executor([[OpCode.PUSH, obj], [OpCode.PUSH, f2], [OpCode.PUSH, 'test'], [OpCode.MAKE_GETTER]], [obj], {}, {}) self.assertEqual(obj.test.setter, f) self.assertEqual(obj.test.getter, f2)
def visitPropertySetter(self, ctx): name = ctx.children[1].accept(self) argName = ctx.children[3].accept(self) body = ctx.children[6].accept(self) param = Object() param.set = Function([argName], self.environment, body) return (name, param)
def test_024_new(self): def func_new(env): self.assertTrue(env.value("this"), Object) self.assertEqual(env.value('a'), 1.0) self.assertEqual(env.value('b'), 2.0) f = Function(['a', 'b'], Environment(), func_new) self.run_test_executor([[OpCode.PUSH, 1.0], [OpCode.PUSH, 2.0], [OpCode.PUSH, f], [OpCode.NEW, 2]], [Object()], {}, {})
def test_042_make_function(self): args = ['a', 'b'] code = Code() (stack, env) = self.run_test_executor( [[OpCode.PUSH, args], [OpCode.PUSH, code], [OpCode.MAKE_FUNC]], [Function(None, None, None)], {}, {}) self.assertEqual(stack[0].args, args) code.add_instruction(Instruction(OpCode.PUSH, 1.0)) code.add_instruction(Instruction(OpCode.RET)) self.run_test_executor([[OpCode.PUSH, []], [OpCode.PUSH, code], [OpCode.MAKE_FUNC], [OpCode.CALL, 0]], [1.0], {}, {})
def test_023_call(self): global test_014_called test_014_called = False def func_body(env): global test_014_called test_014_called = True f = Function([], Environment(), func_body) self.run_test_executor([[OpCode.PUSH, f], [OpCode.CALL, 0]], [None], {}, {}) self.assertTrue(test_014_called) def func_body_params(env): self.assertEqual(env.value('a'), 1.0) self.assertEqual(env.value('b'), 2.0) f = Function(['a', 'b'], Environment(), func_body) self.run_test_executor([[OpCode.PUSH, 1.0], [OpCode.PUSH, 2.0], [OpCode.PUSH, f], [OpCode.CALL, 2]], [None], {}, {})
def visitFunctionExpression(self, ctx): argumentIndex = 3 # If we are dealing with a lambda. if ctx.children[1].getText() == '(': argumentIndex -= 1 else: name = ctx.children[1].accept(self) if ctx.children[argumentIndex].getText() == ')': arguments = [] argumentIndex -= 1 else: arguments = ctx.children[argumentIndex].accept(self) body = ctx.children[argumentIndex + 3].accept(self) function = Function(arguments, self.environment, body) if ctx.children[1].getText() == '(': return Function(arguments, self.environment, body) else: self.environment.defineVariable(name, function)
def visitPropertySetter(self, ctx): name = ctx.children[1].accept(self) param = ctx.children[3].accept(self) setter_body = ctx.children[-2].children[0] def func(env): previous_env = self.environment self.environment = env return_value = setter_body.accept(self) self.environment = previous_env return return_value return ('set', name, Function([param], self.environment, func))
def test_function(self): # # This test that a simple call works # env = Environment() function = Function(["arg1", "arg2", "finalarg"], env, lambda environment: self.func1(environment)) function(None, 10, 2, 5) function.call(None, None, 10, 2, 5) # # This test that "this" is correctly set # env = Environment() function = Function(["arg1", "arg2"], env, lambda environment: self.func2(environment)) function(10, 2, 5) function.call(None, 10, 2, 5) # # This test that global variables works # env = Environment() env.defineVariable("glob", 4) function = Function(["arg1", "arg2"], env, lambda environment: self.func3(environment)) function(10, 2, 5) self.assertEqual(env.value("glob"), 3) self.assertRaises(Utils.UnknownVariable, env.value, "testator") env.setVariable("glob", 4) function.call(None, 10, 2, 5) self.assertEqual(env.value("glob"), 3) self.assertRaises(Utils.UnknownVariable, env.value, "testator") # # This test that the ReturnException is correctly catched in the call function # env = Environment() function = Function(["arg1", "arg2"], env, lambda environment: self.func4(environment)) self.assertEqual(function(None, 2, 5), 7)
def execute_make_func(self): ''' Execute the MAKE_FUNC instruction ''' code = self.stack.pop() args = self.stack.pop() def fun(env): old_env = self.environment self.environment = env self.execute(code) self.environment = old_env self.stack.push(Function(args, self.environment, fun))
def visitPropertyGetter(self, ctx): name = ctx.children[1].accept(self) getter_body = ctx.children[-2].children[0] def func(env): previous_env = self.environment self.environment = env return_val = getter_body.accept(self) if isinstance(return_val, tuple): return_val = return_val[1] self.environment = previous_env return return_val return ('get', name, Function([], self.environment, func))
def execute_MAKE_FUNC(self): body = self.stack.pop() def body_func(env): self.environment = env retval = self.execute(body_func.body) self.environment = self.environment.parent return retval body_func.body = body arguments = self.stack.pop() function = Function(arguments, self.environment, body_func) self.stack.push(function)
def visitFunctionExpression(self, ctx): # Now uses the Function class # Check if it is an anonomous function if ctx.children[ 1].symbol.type == ECMAScriptLexer.ECMAScriptLexer.OpenParen: # 5 # Check if there are any params if isinstance( ctx.children[2], ECMAScriptParser.ECMAScriptParser. FormalParameterListContext): params = ctx.children[2].accept(self) body = ctx.children[5].accept(self) else: params = [] body = ctx.children[4].accept(self) new_func_to_return = Function(params, self.environment, body) setattr(new_func_to_return, "prototype", new_func_to_return) return new_func_to_return elif ctx.children[ 1].symbol.type == ECMAScriptLexer.ECMAScriptLexer.Identifier: # 100 variable declaration func_name = ctx.children[1].accept(self) # Check if there are any params if isinstance( ctx.children[3], ECMAScriptParser.ECMAScriptParser. FormalParameterListContext): params = ctx.children[3].accept(self) body = ctx.children[6].accept(self) else: params = [] body = ctx.children[5].accept(self) new_func = Function(params, self.environment, body) setattr(new_func, "prototype", new_func) self.environment.defineVariable(func_name, new_func) else: raise Utils.UnimplementedVisitorException(ctx)
def visitPropertyGetter(self, ctx): name = ctx.children[1].accept(self) body = ctx.children[5].accept(self) param = Object() param.get = Function([], self.environment, body) return (name, param)
#!/usr/bin/env python3 from Interpreter.Function import Function from Interpreter.Environment import Environment f = Function(["arg1", "arg2"], Environment(), lambda env: print(env.value("arg1") + env.value("arg2"))) f(None,1,2)