diff --git a/mozilla/js/rhino/org/mozilla/javascript/ScriptRuntime.java b/mozilla/js/rhino/org/mozilla/javascript/ScriptRuntime.java index 21f92df1114..e5834296891 100644 --- a/mozilla/js/rhino/org/mozilla/javascript/ScriptRuntime.java +++ b/mozilla/js/rhino/org/mozilla/javascript/ScriptRuntime.java @@ -2033,26 +2033,15 @@ public class ScriptRuntime { return scope.getParentScope(); } - public static NativeFunction defineFunction(Scriptable scope, - String className, - String fnName, - Object loadingObject, - Context cx) - throws ClassNotFoundException + public static NativeFunction initFunction(NativeFunction fn, + Scriptable scope, + String fnName, + Context cx) { - NativeFunction fn; - try { - ClassLoader loader = loadingObject.getClass().getClassLoader(); - Class clazz = (loader == null) ? Class.forName(className) - : loader.loadClass(className); - fn = createFunctionObject(scope, clazz, cx); - } - catch (ClassNotFoundException e) { - throw WrappedException.wrapException(e); - } + fn.setPrototype(ScriptableObject.getClassPrototype(scope, "Function")); + fn.setParentScope(scope); if (fnName != null && fnName.length() != 0) - setName(scope, fn, scope, fnName); - + setName(scope, fn, scope, fnName); return fn; } diff --git a/mozilla/js/rhino/org/mozilla/javascript/optimizer/Codegen.java b/mozilla/js/rhino/org/mozilla/javascript/optimizer/Codegen.java index 5967d24dfe2..0a38abde79c 100644 --- a/mozilla/js/rhino/org/mozilla/javascript/optimizer/Codegen.java +++ b/mozilla/js/rhino/org/mozilla/javascript/optimizer/Codegen.java @@ -1395,32 +1395,45 @@ public class Codegen extends Interpreter { Node def = (Node) fns.elementAt(i); Codegen codegen = new Codegen(); String fnClassName = codegen.generateCode(def, results, itsNameHelper); - // initialize a property by that name, and push the object - // as a result + + addByteCode(ByteCode.NEW, fnClassName); + addByteCode(ByteCode.DUP); if (inFunction) { addByteCode(ByteCode.ALOAD_0); // load "this" argument } else { aload(variableObjectLocal); } - push(fnClassName); + aload(contextLocal); // load 'cx' + addSpecialInvoke(fnClassName, "", + "(Lorg/mozilla/javascript/Scriptable;" + + "Lorg/mozilla/javascript/Context;)", + "V"); + + // 'fn' still on stack + // load 'scope' + if (inFunction) { + addByteCode(ByteCode.ALOAD_0); // load "this" argument + } else { + aload(variableObjectLocal); + } + // load 'fnName' String str = def.getString(); if (str != null) { push(str); } else { addByteCode(ByteCode.ACONST_NULL); } - addByteCode(ByteCode.ALOAD_0); // load 'this' - aload(contextLocal); // load 'cx' - addScriptRuntimeInvoke("defineFunction", - "(Lorg/mozilla/javascript/Scriptable;" + - "Ljava/lang/String;" + - "Ljava/lang/String;" + - "Ljava/lang/Object;" + - "Lorg/mozilla/javascript/Context;)", - "Lorg/mozilla/javascript/NativeFunction;"); + // load 'cx' + aload(contextLocal); + addScriptRuntimeInvoke("initFunction", + "(Lorg/mozilla/javascript/NativeFunction;" + + "Lorg/mozilla/javascript/Scriptable;" + + "Ljava/lang/String;" + + "Lorg/mozilla/javascript/Context;)", + "Lorg/mozilla/javascript/NativeFunction;"); def.putProp(Node.FUNCTION_PROP, new Short(i)); addByteCode(ByteCode.AASTORE); // store NativeFunction - // instance to array + // instance to array } // add the array as the nestedFunctions field; array should diff --git a/mozilla/js/rhino/src/org/mozilla/javascript/ScriptRuntime.java b/mozilla/js/rhino/src/org/mozilla/javascript/ScriptRuntime.java index 21f92df1114..e5834296891 100644 --- a/mozilla/js/rhino/src/org/mozilla/javascript/ScriptRuntime.java +++ b/mozilla/js/rhino/src/org/mozilla/javascript/ScriptRuntime.java @@ -2033,26 +2033,15 @@ public class ScriptRuntime { return scope.getParentScope(); } - public static NativeFunction defineFunction(Scriptable scope, - String className, - String fnName, - Object loadingObject, - Context cx) - throws ClassNotFoundException + public static NativeFunction initFunction(NativeFunction fn, + Scriptable scope, + String fnName, + Context cx) { - NativeFunction fn; - try { - ClassLoader loader = loadingObject.getClass().getClassLoader(); - Class clazz = (loader == null) ? Class.forName(className) - : loader.loadClass(className); - fn = createFunctionObject(scope, clazz, cx); - } - catch (ClassNotFoundException e) { - throw WrappedException.wrapException(e); - } + fn.setPrototype(ScriptableObject.getClassPrototype(scope, "Function")); + fn.setParentScope(scope); if (fnName != null && fnName.length() != 0) - setName(scope, fn, scope, fnName); - + setName(scope, fn, scope, fnName); return fn; } diff --git a/mozilla/js/rhino/src/org/mozilla/javascript/optimizer/Codegen.java b/mozilla/js/rhino/src/org/mozilla/javascript/optimizer/Codegen.java index 5967d24dfe2..0a38abde79c 100644 --- a/mozilla/js/rhino/src/org/mozilla/javascript/optimizer/Codegen.java +++ b/mozilla/js/rhino/src/org/mozilla/javascript/optimizer/Codegen.java @@ -1395,32 +1395,45 @@ public class Codegen extends Interpreter { Node def = (Node) fns.elementAt(i); Codegen codegen = new Codegen(); String fnClassName = codegen.generateCode(def, results, itsNameHelper); - // initialize a property by that name, and push the object - // as a result + + addByteCode(ByteCode.NEW, fnClassName); + addByteCode(ByteCode.DUP); if (inFunction) { addByteCode(ByteCode.ALOAD_0); // load "this" argument } else { aload(variableObjectLocal); } - push(fnClassName); + aload(contextLocal); // load 'cx' + addSpecialInvoke(fnClassName, "", + "(Lorg/mozilla/javascript/Scriptable;" + + "Lorg/mozilla/javascript/Context;)", + "V"); + + // 'fn' still on stack + // load 'scope' + if (inFunction) { + addByteCode(ByteCode.ALOAD_0); // load "this" argument + } else { + aload(variableObjectLocal); + } + // load 'fnName' String str = def.getString(); if (str != null) { push(str); } else { addByteCode(ByteCode.ACONST_NULL); } - addByteCode(ByteCode.ALOAD_0); // load 'this' - aload(contextLocal); // load 'cx' - addScriptRuntimeInvoke("defineFunction", - "(Lorg/mozilla/javascript/Scriptable;" + - "Ljava/lang/String;" + - "Ljava/lang/String;" + - "Ljava/lang/Object;" + - "Lorg/mozilla/javascript/Context;)", - "Lorg/mozilla/javascript/NativeFunction;"); + // load 'cx' + aload(contextLocal); + addScriptRuntimeInvoke("initFunction", + "(Lorg/mozilla/javascript/NativeFunction;" + + "Lorg/mozilla/javascript/Scriptable;" + + "Ljava/lang/String;" + + "Lorg/mozilla/javascript/Context;)", + "Lorg/mozilla/javascript/NativeFunction;"); def.putProp(Node.FUNCTION_PROP, new Short(i)); addByteCode(ByteCode.AASTORE); // store NativeFunction - // instance to array + // instance to array } // add the array as the nestedFunctions field; array should