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.

This commit is contained in:
igor%mir2.org 2002-12-30 06:49:10 +00:00
Родитель c42854be6b
Коммит 2907a50485
2 изменённых файлов: 24 добавлений и 23 удалений

Просмотреть файл

@ -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,

Просмотреть файл

@ -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;)" +