From 7a60bfa6504534ca1942f077f6169db663da6ca6 Mon Sep 17 00:00:00 2001 From: "igor%mir2.org" Date: Mon, 30 Dec 2002 06:49:10 +0000 Subject: [PATCH] Replace ScriptRuntime#main(String scriptClassName, String[] args) by ScriptRuntime#main(Class scriptClass, String[] args) and to optimizer.Codegen#generateMain code to generate call to Class.forName before calling ScriptRuntime#main. In this way script byte code can access Class object if generated script is loaded via different class loader then Rhino classes. git-svn-id: svn://10.0.0.236/trunk@135695 18797224-902f-48f8-a5cc-f745e15eee43 --- .../org/mozilla/javascript/ScriptRuntime.java | 39 +++++++++---------- .../mozilla/javascript/optimizer/Codegen.java | 8 +++- 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/mozilla/js/rhino/src/org/mozilla/javascript/ScriptRuntime.java b/mozilla/js/rhino/src/org/mozilla/javascript/ScriptRuntime.java index ec493b88828..ae144c3288c 100644 --- a/mozilla/js/rhino/src/org/mozilla/javascript/ScriptRuntime.java +++ b/mozilla/js/rhino/src/org/mozilla/javascript/ScriptRuntime.java @@ -1818,34 +1818,31 @@ public class ScriptRuntime { return new ImporterTopLevel(cx); } - public static void main(String scriptClassName, String[] args) + public static void main(Class scriptClass, String[] args) throws JavaScriptException { Context cx = Context.enter(); - ScriptableObject global = getGlobal(cx); - - // get the command line arguments and define "arguments" - // array in the top-level object - Scriptable argsObj = cx.newArray(global, args); - global.defineProperty("arguments", argsObj, - ScriptableObject.DONTENUM); - try { - Class cl = loadClassName(scriptClassName); - Script script = (Script) cl.newInstance(); + Script script = null; + try { + script = (Script)scriptClass.newInstance(); + } catch (InstantiationException e) { + } catch (IllegalAccessException e) { + } + if (script == null) { + throw new RuntimeException("Error creating script object"); + } + ScriptableObject global = getGlobal(cx); + + // get the command line arguments and define "arguments" + // array in the top-level object + Scriptable argsObj = cx.newArray(global, args); + global.defineProperty("arguments", argsObj, + ScriptableObject.DONTENUM); script.exec(cx, global); - return; - } - catch (ClassNotFoundException e) { - } - catch (InstantiationException e) { - } - catch (IllegalAccessException e) { - } - finally { + } finally { Context.exit(); } - throw new RuntimeException("Error creating script object"); } public static Scriptable initScript(Context cx, Scriptable scope, 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 9a11ed446f4..a85e416006c 100644 --- a/mozilla/js/rhino/src/org/mozilla/javascript/optimizer/Codegen.java +++ b/mozilla/js/rhino/src/org/mozilla/javascript/optimizer/Codegen.java @@ -1018,15 +1018,19 @@ public class Codegen extends Interpreter { startNewMethod("main", "([Ljava/lang/String;)V", 1, true, true); push(this.name); // load the name of this class + classFile.add(ByteCode.INVOKESTATIC, + "java/lang/Class", + "forName", + "(Ljava/lang/String;)", + "Ljava/lang/Class;"); addByteCode(ByteCode.ALOAD_0); // load 'args' addScriptRuntimeInvoke("main", - "(Ljava/lang/String;[Ljava/lang/String;)", + "(Ljava/lang/Class;[Ljava/lang/String;)", "V"); addByteCode(ByteCode.RETURN); finishMethod(cx, null); } - private void generateExecute(Context cx) { String signature = "(Lorg/mozilla/javascript/Context;" + "Lorg/mozilla/javascript/Scriptable;)" +