(ExecutionContext context, Scope scope, Instruction[] instructions)
| 34 | public class Interpreter { |
| 35 | |
| 36 | public static Object execute(ExecutionContext context, Scope scope, Instruction[] instructions) { |
| 37 | //System.out.println("Executing: " + context.getFileName() + ":" + context.getLineNumber()); |
| 38 | Object result = Types.UNDEFINED; |
| 39 | Object[] temps = new Object[scope.getTemporaryVariableSize()]; |
| 40 | int size = instructions.length; |
| 41 | Object value = Types.UNDEFINED; |
| 42 | |
| 43 | int ipc = 0; |
| 44 | while (ipc < size) { |
| 45 | Instruction instr = instructions[ipc]; |
| 46 | ipc++; |
| 47 | //System.out.println("EX: " + instr); |
| 48 | |
| 49 | switch(instr.getOperation()) { |
| 50 | case ADD: |
| 51 | value = add(context, |
| 52 | ((Add) instr).getLHS().retrieve(context, temps), |
| 53 | ((Add) instr).getRHS().retrieve(context, temps)); |
| 54 | break; |
| 55 | case SUB: |
| 56 | value = sub(context, |
| 57 | ((Sub) instr).getLHS().retrieve(context, temps), |
| 58 | ((Sub) instr).getRHS().retrieve(context, temps)); |
| 59 | break; |
| 60 | case RECEIVE_FUNCTION_PARAM: |
| 61 | value = context.getFunctionParameters()[((ReceiveFunctionParameter) instr).getIndex()]; |
| 62 | break; |
| 63 | case COPY: |
| 64 | value = ((Copy) instr).getValue().retrieve(context, temps); |
| 65 | break; |
| 66 | case JUMP: |
| 67 | ipc = ((Jump) instr).getTarget().getTargetIPC(); |
| 68 | break; |
| 69 | case CALL: { |
| 70 | Call call = (Call) instr; |
| 71 | Object ref = call.getIdentifier().retrieve(context, temps); |
| 72 | Object function = Types.getValue(context, ref); |
| 73 | Operand[] opers = call.getArgs(); |
| 74 | Object[] args = new Object[opers.length]; |
| 75 | |
| 76 | if (!(function instanceof JSFunction)) { |
| 77 | throw new ThrowException(context, context.createTypeError(ref + " is not callable")); |
| 78 | } |
| 79 | |
| 80 | for (int i = 0; i < args.length; i++) { |
| 81 | args[i] = opers[i].retrieve(context, temps); |
| 82 | } |
| 83 | |
| 84 | Object thisValue = getThis(ref); |
| 85 | |
| 86 | value = context.call(ref, (JSFunction) function, thisValue, args); |
| 87 | } |
| 88 | break; |
| 89 | case CONSTRUCTOR: { |
| 90 | Constructor constructor = (Constructor) instr; |
| 91 | Object ref = constructor.getIdentifier().retrieve(context, temps); |
| 92 | Object function = Types.getValue(context, ref); |
| 93 | Operand[] opers = constructor.getArgs(); |
no test coverage detected