From 7c4d921fe0f0affa044885d3fbadcf5cd89fc518 Mon Sep 17 00:00:00 2001 From: "igor%mir2.org" Date: Mon, 17 Feb 2003 17:39:26 +0000 Subject: [PATCH] Store function array inside ScriptOrFnNode as field, not as a node property. --- .../src/org/mozilla/javascript/Context.java | 2 +- .../org/mozilla/javascript/Interpreter.java | 13 +++---- .../mozilla/javascript/NodeTransformer.java | 11 ++---- .../mozilla/javascript/ScriptOrFnNode.java | 32 ++++++++++++++- .../mozilla/javascript/optimizer/Codegen.java | 39 ++++++++++--------- .../javascript/optimizer/OptTransformer.java | 4 +- 6 files changed, 63 insertions(+), 38 deletions(-) diff --git a/js/rhino/src/org/mozilla/javascript/Context.java b/js/rhino/src/org/mozilla/javascript/Context.java index b963f34f2f60..b8889cdc7d6d 100644 --- a/js/rhino/src/org/mozilla/javascript/Context.java +++ b/js/rhino/src/org/mozilla/javascript/Context.java @@ -2021,7 +2021,7 @@ public class Context { if (debugger != null) { if (sourceString == null) Context.codeBug(); - tree.originalSource = sourceString; + tree.setOriginalSource(sourceString); } Object result = compiler.compile(this, scope, tree, diff --git a/js/rhino/src/org/mozilla/javascript/Interpreter.java b/js/rhino/src/org/mozilla/javascript/Interpreter.java index 0ebf584e957b..88300b52cb8b 100644 --- a/js/rhino/src/org/mozilla/javascript/Interpreter.java +++ b/js/rhino/src/org/mozilla/javascript/Interpreter.java @@ -145,15 +145,14 @@ public class Interpreter { } private void generateNestedFunctions(Context cx, Scriptable scope, - Node tree) + ScriptOrFnNode tree) { - ObjArray functionList = (ObjArray) tree.getProp(Node.FUNCTION_PROP); - if (functionList == null) return; + int functionCount = tree.getFunctionCount(); + if (functionCount == 0) return; - int N = functionList.size(); - InterpreterData[] array = new InterpreterData[N]; - for (int i = 0; i != N; i++) { - FunctionNode def = (FunctionNode)functionList.get(i); + InterpreterData[] array = new InterpreterData[functionCount]; + for (int i = 0; i != functionCount; i++) { + FunctionNode def = tree.getFunctionNode(i); Interpreter jsi = new Interpreter(); jsi.itsSourceFile = itsSourceFile; jsi.itsData = new InterpreterData(itsData.securityDomain); diff --git a/js/rhino/src/org/mozilla/javascript/NodeTransformer.java b/js/rhino/src/org/mozilla/javascript/NodeTransformer.java index e06a32311441..bd476ab201b6 100644 --- a/js/rhino/src/org/mozilla/javascript/NodeTransformer.java +++ b/js/rhino/src/org/mozilla/javascript/NodeTransformer.java @@ -112,8 +112,8 @@ public class NodeTransformer { } } else { - FunctionNode fnNode = (FunctionNode) - node.getProp(Node.FUNCTION_PROP); + FunctionNode + fnNode = (FunctionNode)node.getProp(Node.FUNCTION_PROP); if (inFunction) { // Functions containing other functions require // activation objects @@ -127,12 +127,7 @@ public class NodeTransformer { NodeTransformer inner = newInstance(); fnNode = (FunctionNode)inner.transform(fnNode); node.putProp(Node.FUNCTION_PROP, fnNode); - ObjArray fns = (ObjArray) tree.getProp(Node.FUNCTION_PROP); - if (fns == null) { - fns = new ObjArray(); - tree.putProp(Node.FUNCTION_PROP, fns); - } - fns.add(fnNode); + tree.addFunction(fnNode); } break; diff --git a/js/rhino/src/org/mozilla/javascript/ScriptOrFnNode.java b/js/rhino/src/org/mozilla/javascript/ScriptOrFnNode.java index f2e41bfb9f6c..851f572d7d4f 100644 --- a/js/rhino/src/org/mozilla/javascript/ScriptOrFnNode.java +++ b/js/rhino/src/org/mozilla/javascript/ScriptOrFnNode.java @@ -47,12 +47,40 @@ public class ScriptOrFnNode extends Node { public final String getOriginalSource() { return originalSource; } + public final void setOriginalSource(String originalSource) { + // One time action + if (originalSource == null || this.originalSource != null) + Context.codeBug(); + this.originalSource = originalSource; + } + public final String getSourceName() { return sourceName; } public final int getBaseLineno() { return baseLineno; } public final int getEndLineno() { return baseLineno; } + public final int getFunctionCount() { + if (functions == null) { return 0; } + return functions.size(); + } + + public final FunctionNode getFunctionNode(int i) { + return (FunctionNode)functions.get(i); + } + + public final void replaceFunctionNode(int i, FunctionNode fnNode) { + if (fnNode == null) Context.codeBug(); + functions.set(i, fnNode); + } + + public final int addFunction(FunctionNode fnNode) { + if (fnNode == null) Context.codeBug(); + if (functions == null) { functions = new ObjArray(); } + functions.add(fnNode); + return functions.size(); + } + public final int getRegexpCount() { if (regexps == null) { return 0; } return regexps.size() / 2; @@ -67,6 +95,7 @@ public class ScriptOrFnNode extends Node { } public final int addRegexp(String string, String flags) { + if (string == null) Context.codeBug(); if (regexps == null) { regexps = new ObjArray(); } regexps.add(string); regexps.add(flags); @@ -75,10 +104,11 @@ public class ScriptOrFnNode extends Node { VariableTable variableTable; String encodedSource; - String originalSource; + private String originalSource; String sourceName; int baseLineno = -1; int endLineno = -1; + private ObjArray functions; private ObjArray regexps; } diff --git a/js/rhino/src/org/mozilla/javascript/optimizer/Codegen.java b/js/rhino/src/org/mozilla/javascript/optimizer/Codegen.java index bb37e91910dd..1a7531912330 100644 --- a/js/rhino/src/org/mozilla/javascript/optimizer/Codegen.java +++ b/js/rhino/src/org/mozilla/javascript/optimizer/Codegen.java @@ -385,10 +385,10 @@ public class Codegen extends Interpreter { private String generateCode(ScriptOrFnNode tree, ObjArray names, ObjArray classFiles) { - ObjArray fns = (ObjArray) tree.getProp(Node.FUNCTION_PROP); - if (fns != null) { - for (int i = 0; i != fns.size(); ++i) { - OptFunctionNode fn = (OptFunctionNode) fns.get(i); + int functionCount = tree.getFunctionCount(); + if (functionCount != 0) { + for (int i = 0; i != functionCount; ++i) { + OptFunctionNode fn = (OptFunctionNode)tree.getFunctionNode(i); Codegen codegen = new Codegen(this); codegen.generateCode(fn, names, classFiles); } @@ -1235,29 +1235,29 @@ public class Codegen extends Interpreter { classFile.startMethod(getSourceMethodStr, "()Ljava/lang/Object;", (short)flags); - ObjArray fns = (ObjArray) tree.getProp(Node.FUNCTION_PROP); - if (fns == null) { + int functionCount = tree.getFunctionCount(); + if (functionCount == 0) { // generate return ; push(source); } else { - // generate with N = fns.size(): - // Object[] result = new Object[1 + N]; + // generate + // Object[] result = new Object[1 + functionCount]; // result[0] = // result[1] = Class1.getSourcesTreeImpl(); // ... - // result[N] = ClassN.getSourcesTreeImpl(); + // result[functionCount] = ClassN.getSourcesTreeImpl(); // return result; - push(1 + fns.size()); + push(1 + functionCount); addByteCode(ByteCode.ANEWARRAY, "java/lang/Object"); addByteCode(ByteCode.DUP); // dup array reference push(0); push(source); addByteCode(ByteCode.AASTORE); - for (int i = 0; i != fns.size(); ++i) { + for (int i = 0; i != functionCount; ++i) { + OptFunctionNode fn; addByteCode(ByteCode.DUP); // dup array reference push(1 + i); - - OptFunctionNode fn = (OptFunctionNode) fns.get(i); + fn = (OptFunctionNode)tree.getFunctionNode(i); classFile.add(ByteCode.INVOKESTATIC, fn.getClassName(), getSourceMethodStr, @@ -1324,8 +1324,9 @@ public class Codegen extends Interpreter { * @param directParameterCount number of parameters for direct call, * or -1 if not direct call */ - private void generatePrologue(Context cx, Node tree, boolean inFunction, - int directParameterCount) + private void + generatePrologue(Context cx, ScriptOrFnNode tree, boolean inFunction, + int directParameterCount) { funObjLocal = reserveWordLocal(0); contextLocal = reserveWordLocal(1); @@ -1488,10 +1489,10 @@ public class Codegen extends Interpreter { } astore(variableObjectLocal); - ObjArray fns = (ObjArray) tree.getProp(Node.FUNCTION_PROP); - if (fns != null) { - for (int i=0; i < fns.size(); i++) { - OptFunctionNode fn = (OptFunctionNode) fns.get(i); + int functionCount = tree.getFunctionCount(); + if (functionCount != 0) { + for (int i=0; i < functionCount; i++) { + OptFunctionNode fn = (OptFunctionNode)tree.getFunctionNode(i); if (fn.getFunctionType() == FunctionNode.FUNCTION_STATEMENT) { visitFunction(fn, FunctionNode.FUNCTION_STATEMENT); addByteCode(ByteCode.POP); diff --git a/js/rhino/src/org/mozilla/javascript/optimizer/OptTransformer.java b/js/rhino/src/org/mozilla/javascript/optimizer/OptTransformer.java index 01e5d6a74bd1..81d337af8720 100644 --- a/js/rhino/src/org/mozilla/javascript/optimizer/OptTransformer.java +++ b/js/rhino/src/org/mozilla/javascript/optimizer/OptTransformer.java @@ -154,8 +154,8 @@ class OptTransformer extends NodeTransformer { private void collectContainedFunctions(Node node) { for (Node tNode=node; tNode != null; tNode = tNode.getNext()) { if (tNode.getType() == TokenStream.FUNCTION) { - FunctionNode fnNode = (FunctionNode) - tNode.getProp(Node.FUNCTION_PROP); + FunctionNode + fnNode = (FunctionNode)tNode.getProp(Node.FUNCTION_PROP); if (fnNode.getType() == FunctionNode.FUNCTION_STATEMENT) { String name = fnNode.getFunctionName(); if (name.length() != 0) {