+++ /dev/null
-// Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
-package org.ibex.js;
-
-import java.io.*;
-import org.ibex.util.*;
-
-/** A JavaScript function, compiled into bytecode */
-// FIXME: This shouldn't be public, needed for public add/delTrap (which is needed for the Template.java hack)
-public class JSFunction extends JS implements ByteCodes, Tokens, Task {
-
-
- // Fields and Accessors ///////////////////////////////////////////////
-
- int numFormalArgs = 0; ///< the number of formal arguments
-
- String sourceName; ///< the source code file that this block was drawn from
- private int firstLine = -1; ///< the first line of this script
-
- int[] line = new int[10]; ///< the line numbers
- int[] op = new int[10]; ///< the instructions
- Object[] arg = new Object[10]; ///< the arguments to the instructions
- int size = 0; ///< the number of instruction/argument pairs
-
- JSScope parentScope; ///< the default scope to use as a parent scope when executing this
-
-
- // Public //////////////////////////////////////////////////////////////////////////////
-
- // FEATURE: make sure that this can only be called from the Scheduler...
- /** if you enqueue a function, it gets invoked in its own pauseable context */
- public void perform() throws JSExn {
- Interpreter i = new Interpreter(this, true, new Interpreter.JSArgs(this));
- i.resume();
- }
-
- public JSFunction _cloneWithNewParentScope(JSScope s) {
- JSFunction ret = new JSFunction(sourceName, firstLine, s);
- // Reuse the same op, arg, line, and size variables for the new "instance" of the function
- // NOTE: Neither *this* function nor the new function should be modified after this call
- ret.op = this.op;
- ret.arg = this.arg;
- ret.line = this.line;
- ret.size = this.size;
- ret.numFormalArgs = this.numFormalArgs;
- return ret;
- }
-
- /** Note: code gets run in an <i>unpauseable</i> context. */
- public JS call(JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn {
- Interpreter cx = new Interpreter(this, false, new Interpreter.JSArgs(a0,a1,a2,rest,nargs,this));
- return cx.resume();
- }
-
- public JSScope getParentScope() { return parentScope; }
-
- // Adding and Altering Bytecodes ///////////////////////////////////////////////////
-
- JSFunction(String sourceName, int firstLine, JSScope parentScope) {
- this.sourceName = sourceName;
- this.firstLine = firstLine;
- this.parentScope = parentScope;
- }
-
- int get(int pos) { return op[pos]; }
- Object getArg(int pos) { return arg[pos]; }
- void set(int pos, int op_, Object arg_) { op[pos] = op_; arg[pos] = arg_; }
- void set(int pos, Object arg_) { arg[pos] = arg_; }
- int pop() { size--; arg[size] = null; return op[size]; }
- void paste(JSFunction other) { for(int i=0; i<other.size; i++) add(other.line[i], other.op[i], other.arg[i]); }
- JSFunction add(int line, int op_) { return add(line, op_, null); }
- JSFunction add(int line, int op_, Object arg_) {
- if (size == op.length - 1) {
- int[] line2 = new int[op.length * 2]; System.arraycopy(this.line, 0, line2, 0, op.length); this.line = line2;
- Object[] arg2 = new Object[op.length * 2]; System.arraycopy(arg, 0, arg2, 0, arg.length); arg = arg2;
- int[] op2 = new int[op.length * 2]; System.arraycopy(op, 0, op2, 0, op.length); op = op2;
- }
- this.line[size] = line;
- op[size] = op_;
- arg[size] = arg_;
- size++;
- return this;
- }
-
-
- // Debugging //////////////////////////////////////////////////////////////////////
-
- String extendedToString() { return "[" + sourceName + ":" + firstLine + "]"; }
-
- String dump() { return dump(""); }
- private String dump(String prefix) {
- StringBuffer sb = new StringBuffer(1024);
- sb.append("\n" + sourceName + ": " + firstLine + "\n");
- for (int i=0; i < size; i++) {
- sb.append(prefix);
- sb.append(i).append(" (").append(line[i]).append("): ");
- if (op[i] < 0) sb.append(bytecodeToString[-op[i]]);
- else sb.append(codeToString[op[i]]);
- sb.append(" ");
- sb.append(arg[i] == null ? "(no arg)" : arg[i] instanceof JS ? JS.debugToString((JS)arg[i]) : arg[i]);
- if((op[i] == JF || op[i] == JT || op[i] == JMP) && arg[i] != null && arg[i] instanceof Number) {
- sb.append(" jump to ").append(i+((Number) arg[i]).intValue());
- } else if(op[i] == TRY) {
- int[] jmps = (int[]) arg[i];
- sb.append(" catch: ").append(jmps[0] < 0 ? "No catch block" : ""+(i+jmps[0]));
- sb.append(" finally: ").append(jmps[1] < 0 ? "No finally block" : ""+(i+jmps[1]));
- } else if(op[i] == NEWFUNCTION) {
- sb.append(((JSFunction) arg[i]).dump(prefix + " "));
- } else if(op[i] == NEWSCOPE) {
- int n = ((JSNumber)arg[i]).toInt();
- sb.append(" base: " + (n>>>16) + " size: " + (n&0xffff));
- }
- sb.append("\n");
- }
- return sb.toString();
- }
-
-
-}
-