Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions examples/src/main/java/Shell.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.mozilla.javascript.JavaScriptException;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.TopLevel;
import org.mozilla.javascript.WrappedException;

/**
Expand Down Expand Up @@ -49,12 +50,13 @@ public static void main(String args[]) {
// Initialize the standard objects (Object, Function, etc.)
// This must be done before scripts can be executed.
Shell shell = new Shell();
cx.initStandardObjects(shell);
TopLevel topLevel = new TopLevel(shell);
cx.initStandardObjects(topLevel);

// Define some global functions particular to the shell. Note
// that these functions are not part of ECMA.
String[] names = {"print", "quit", "version", "load", "help"};
shell.defineFunctionProperties(names, Shell.class, ScriptableObject.DONTENUM);
shell.defineFunctionProperties(topLevel, names, Shell.class, ScriptableObject.DONTENUM);

args = processOptions(cx, args);

Expand Down Expand Up @@ -202,7 +204,7 @@ public static double version(Context cx, Scriptable thisObj, Object[] args, Func
* @param funObj the function object of the invoked JavaScript function
*/
public static void load(Context cx, Scriptable thisObj, Object[] args, Function funObj) {
Shell shell = (Shell) getTopLevelScope(thisObj);
Shell shell = (Shell) getTopLevelScope(thisObj).getGlobalThis();
for (int i = 0; i < args.length; i++) {
shell.processSource(cx, Context.toString(args[i]));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void register(Context cx, ScriptableObject scope, ScriptContext sc) {

private static Object print(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
try {
Builtins self = getSelf(thisObj);
Builtins self = getSelf(scope);
for (Object arg : args) {
self.stdout.write(ScriptRuntime.toString(arg));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.mozilla.javascript.Script;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.TopLevel;

/**
* This is the implementation of the standard ScriptEngine interface for Rhino.
Expand Down Expand Up @@ -76,7 +77,7 @@ public class RhinoScriptEngine extends AbstractScriptEngine implements Compilabl

private final RhinoScriptEngineFactory factory;
private final Builtins builtins;
private ScriptableObject topLevelScope = null;
private TopLevel topLevelScope = null;

RhinoScriptEngine(RhinoScriptEngineFactory factory) {
this.factory = factory;
Expand All @@ -94,18 +95,16 @@ private Scriptable initScope(Context cx, ScriptContext sc) throws ScriptExceptio
builtins.register(cx, topLevelScope, sc);
}

Scriptable engineScope = new BindingsObject(sc.getBindings(ScriptContext.ENGINE_SCOPE));
engineScope.setParentScope(null);
engineScope.setPrototype(topLevelScope);

if (sc.getBindings(ScriptContext.GLOBAL_SCOPE) != null) {
Scriptable globalScope = new BindingsObject(sc.getBindings(ScriptContext.GLOBAL_SCOPE));
globalScope.setParentScope(null);
globalScope.setPrototype(topLevelScope);
engineScope.setPrototype(globalScope);
var globalScope =
topLevelScope.createIsolate(
new BindingsObject(sc.getBindings(ScriptContext.GLOBAL_SCOPE)));
return globalScope.createIsolate(
new BindingsObject(sc.getBindings(ScriptContext.ENGINE_SCOPE)));
} else {
return topLevelScope.createIsolate(
new BindingsObject(sc.getBindings(ScriptContext.ENGINE_SCOPE)));
}

return engineScope;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.mozilla.javascript.ContextFactory;
import org.mozilla.javascript.Kit;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.commonjs.module.ModuleScope;
import org.mozilla.javascript.tools.shell.Global;

Expand Down Expand Up @@ -180,7 +181,7 @@ public static void main(String[] args) {
global.installRequire(cx, List.of(), false);

URI uri = new File(System.getProperty("user.dir")).toURI();
ModuleScope scope = new ModuleScope(global, uri, null);
ScriptableObject scope = ModuleScope.createModuleScope(global, uri, null);

main.setScope(scope);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,12 @@ public class Global extends ImporterTopLevel {
private String[] prompts = {"js> ", " > "};
private HashMap<String, String> doctestCanonicalizations;

public Global() {}
public Global() {
super(true);
}

public Global(Context cx) {
super(true);
init(cx);
}

Expand Down Expand Up @@ -128,7 +131,7 @@ public void init(Context cx) {
"version",
"write"
};
defineFunctionProperties(names, Global.class, ScriptableObject.DONTENUM);
defineFunctionProperties(this, names, Global.class, ScriptableObject.DONTENUM);

// Set up "environment" in the global scope to provide access to the
// System environment variables.
Expand Down Expand Up @@ -253,7 +256,7 @@ public static void load(Context cx, Scriptable thisObj, Object[] args, Function
for (Object arg : args) {
String file = Context.toString(arg);
try {
Main.processFile(cx, thisObj, file);
Main.processFile(cx, funObj.getDeclarationScope(), file);
} catch (IOException ioex) {
String msg =
ToolErrorReporter.getMessage(
Expand Down Expand Up @@ -287,7 +290,8 @@ public static void defineClass(Context cx, Scriptable thisObj, Object[] args, Fu
if (!Scriptable.class.isAssignableFrom(clazz)) {
throw reportRuntimeError("msg.must.implement.Scriptable");
}
ScriptableObject.defineClass(thisObj, (Class<? extends Scriptable>) clazz);
ScriptableObject.defineClass(
funObj.getDeclarationScope(), (Class<? extends Scriptable>) clazz);
}

/**
Expand Down Expand Up @@ -340,7 +344,7 @@ public static void serialize(Context cx, Scriptable thisObj, Object[] args, Func
Object obj = args[0];
String filename = Context.toString(args[1]);
FileOutputStream fos = new FileOutputStream(filename);
Scriptable scope = ScriptableObject.getTopLevelScope(thisObj);
Scriptable scope = ScriptableObject.getTopLevelScope(funObj.getDeclarationScope());
try (ScriptableOutputStream out = new ScriptableOutputStream(fos, scope)) {
out.writeObject(obj);
}
Expand All @@ -353,7 +357,7 @@ public static Object deserialize(Context cx, Scriptable thisObj, Object[] args,
}
String filename = Context.toString(args[0]);
try (FileInputStream fis = new FileInputStream(filename)) {
Scriptable scope = ScriptableObject.getTopLevelScope(thisObj);
Scriptable scope = ScriptableObject.getTopLevelScope(funObj.getDeclarationScope());
try (ObjectInputStream in = new ScriptableInputStream(fis, scope)) {
Object deserialized = in.readObject();
return Context.toObject(deserialized, scope);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ static Scriptable getScope(String path) {
uri = new File(path).toURI();
}
}
return new ModuleScope(global, uri, null);
return ModuleScope.createModuleScope(global, uri, null);
}
return global;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ private void exportToScope(boolean sealed) {
XmlNode.QName.create(XmlNode.Namespace.create(""), ""));

xmlPrototype.exportAsJSClass(sealed);
xmlPrototype.getParentScope().put("__xml_lib__", xmlPrototype, xmlPrototype.getLib());
xmlListPrototype.exportAsJSClass(sealed);
namespacePrototype.exportAsJSClass(sealed);
qnamePrototype.exportAsJSClass(sealed);
Expand Down
2 changes: 1 addition & 1 deletion rhino/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ decycle {
ignoring from: "org.mozilla.javascript.ScriptRuntime", to: "org.mozilla.javascript.lc.type.impl.factory.ConcurrentFactory"

// currently accepted cycles (may be resolved, when it is clear, how to separate the liveconnect stuff)
ignoring from: "org.mozilla.javascript.lc.type.*", to: "org.mozilla.javascript.(Scriptable|ScriptableObject)"
ignoring from: "org.mozilla.javascript.lc.type.*", to: "org.mozilla.javascript.(Scriptable|ScriptableObject|TopLevel)"
ignoring from: "org.mozilla.javascript.lc.type.TypeInfoFactory", to: "org.mozilla.javascript.lc.type.impl.factory.*"
ignoring from: "org.mozilla.javascript.lc.type.TypeInfo", to: "org.mozilla.javascript.lc.type.impl.*"
ignoring from: "org.mozilla.javascript.lc.type.TypeFormatContext", to: "org.mozilla.javascript.lc.type.impl.ClassSignatureFormatContext"
Expand Down
4 changes: 2 additions & 2 deletions rhino/src/main/java/org/mozilla/javascript/AccessorSlot.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public Object getValue(Scriptable start) {

@Override
public Function asGetterFunction(String name, Scriptable scope) {
return member.asGetterFunction(name, scope);
return member.asGetterFunction(name);
}

@Override
Expand Down Expand Up @@ -246,7 +246,7 @@ public boolean setValue(Object value, Scriptable owner, Scriptable start) {

@Override
public Function asSetterFunction(String name, Scriptable scope) {
return member.asSetterFunction(name, scope);
return member.asSetterFunction(name);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ public static Object coercibleIterativeMethod(
} else {
thisArg = ScriptRuntime.toObject(cx, scope, args[1]);
}
if (thisArg instanceof TopLevel) {
thisArg = ((TopLevel) thisArg).getGlobalThis();
}

Scriptable array = null;
if (operation == IterativeOperation.FILTER || operation == IterativeOperation.MAP) {
Expand Down Expand Up @@ -339,6 +342,8 @@ public static Object reduceMethodWithLength(
}
Function f = (Function) callbackArg;
Scriptable parent = ScriptableObject.getTopLevelScope(f);
Scriptable globalThis =
parent instanceof TopLevel ? ((TopLevel) parent).getGlobalThis() : parent;
// hack to serve both reduce and reduceRight with the same loop
boolean movingLeft = operation == ReduceOperation.REDUCE;
Object value = args.length > 1 ? args[1] : NOT_FOUND;
Expand All @@ -353,7 +358,7 @@ public static Object reduceMethodWithLength(
value = elem;
} else {
Object[] innerArgs = {value, elem, index, o};
value = f.call(cx, parent, parent, innerArgs);
value = f.call(cx, parent, globalThis, innerArgs);
}
}
if (value == NOT_FOUND) {
Expand Down
3 changes: 3 additions & 0 deletions rhino/src/main/java/org/mozilla/javascript/BoundFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ Scriptable getCallThis(Context cx, Scriptable scope) {
if (callThis == null) {
callThis = getTopLevelScope(scope);
}
if (callThis instanceof TopLevel) {
callThis = ((TopLevel) callThis).getGlobalThis();
}
return callThis;
}

Expand Down
6 changes: 0 additions & 6 deletions rhino/src/main/java/org/mozilla/javascript/ClassCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,6 @@ public static ClassCache get(Scriptable scope) {
if (cache == null) {
// we expect this to not happen frequently, so computing top scope twice is acceptable
var topScope = ScriptableObject.getTopLevelScope(scope);
if (!(topScope instanceof ScriptableObject)) {
// Note: it's originally a RuntimeException, the super class of
// IllegalArgumentException, so this will not break error catching
throw new IllegalArgumentException(
"top scope have no associated ClassCache and cannot have ClassCache associated due to not being a ScriptableObject");
}
cache = new ClassCache();
cache.associate(((ScriptableObject) topScope));
}
Expand Down
15 changes: 8 additions & 7 deletions rhino/src/main/java/org/mozilla/javascript/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -1093,7 +1093,7 @@ public static EvaluatorException reportRuntimeError(String message) {
*
* @return the initialized scope
*/
public final ScriptableObject initStandardObjects() {
public final TopLevel initStandardObjects() {
return initStandardObjects(null, false);
}

Expand Down Expand Up @@ -1136,7 +1136,7 @@ public final ScriptableObject initSafeStandardObjects() {
* @return the initialized scope. The method returns the value of the scope argument if it is
* not null or newly allocated scope object which is an instance {@link ScriptableObject}.
*/
public final Scriptable initStandardObjects(ScriptableObject scope) {
public final TopLevel initStandardObjects(TopLevel scope) {
return initStandardObjects(scope, false);
}

Expand All @@ -1162,7 +1162,7 @@ public final Scriptable initStandardObjects(ScriptableObject scope) {
* @return the initialized scope. The method returns the value of the scope argument if it is
* not null or newly allocated scope object which is an instance {@link ScriptableObject}.
*/
public final Scriptable initSafeStandardObjects(ScriptableObject scope) {
public final TopLevel initSafeStandardObjects(TopLevel scope) {
return initSafeStandardObjects(scope, false);
}

Expand All @@ -1189,7 +1189,7 @@ public final Scriptable initSafeStandardObjects(ScriptableObject scope) {
* not null or newly allocated scope object.
* @since 1.4R3
*/
public ScriptableObject initStandardObjects(ScriptableObject scope, boolean sealed) {
public TopLevel initStandardObjects(TopLevel scope, boolean sealed) {
return ScriptRuntime.initStandardObjects(this, scope, sealed);
}

Expand Down Expand Up @@ -1222,7 +1222,7 @@ public ScriptableObject initStandardObjects(ScriptableObject scope, boolean seal
* not null or newly allocated scope object.
* @since 1.7.6
*/
public ScriptableObject initSafeStandardObjects(ScriptableObject scope, boolean sealed) {
public TopLevel initSafeStandardObjects(TopLevel scope, boolean sealed) {
return ScriptRuntime.initSafeStandardObjects(this, scope, sealed);
}

Expand Down Expand Up @@ -1251,7 +1251,8 @@ public final Object evaluateString(
Scriptable scope, String source, String sourceName, int lineno, Object securityDomain) {
Script script = compileString(source, sourceName, lineno, securityDomain);
if (script != null) {
return script.exec(this, scope, scope);
return script.exec(
this, scope, ScriptableObject.getTopLevelScope(scope).getGlobalThis());
}
return null;
}
Expand Down Expand Up @@ -2802,7 +2803,7 @@ public static boolean isCurrentContextStrict() {
private boolean sealed;
private Object sealKey;

Scriptable topCallScope;
TopLevel topCallScope;
boolean isContinuationsTopCall;
NativeCall currentActivationCall;
XMLLib cachedXMLLib;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public final class ES6Generator extends ScriptableObject {
private State state = State.SUSPENDED_START;
private Object delegee;

static ES6Generator init(ScriptableObject scope, boolean sealed) {
static ES6Generator init(TopLevel scope, boolean sealed) {

ES6Generator prototype = new ES6Generator();
if (scope != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public abstract class ES6Iterator extends IdScriptableObject {
private static final long serialVersionUID = 2438373029140003950L;

protected static void init(
ScriptableObject scope, boolean sealed, IdScriptableObject prototype, String tag) {
TopLevel scope, boolean sealed, IdScriptableObject prototype, String tag) {
if (scope != null) {
prototype.setParentScope(scope);
prototype.setPrototype(getObjectPrototype(scope));
Expand Down
10 changes: 8 additions & 2 deletions rhino/src/main/java/org/mozilla/javascript/FunctionObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,16 @@ public FunctionObject(String name, Member methodOrConstructor, Scriptable scope)
var typeInfoFactory = TypeInfoFactory.getOrElse(scope, TypeInfoFactory.GLOBAL);

if (methodOrConstructor instanceof Constructor) {
member = new MemberBox((Constructor<?>) methodOrConstructor, typeInfoFactory);
member =
new MemberBox(
getDeclarationScope(),
(Constructor<?>) methodOrConstructor,
typeInfoFactory);
isStatic = true; // well, doesn't take a 'this'
} else {
member = new MemberBox((Method) methodOrConstructor, typeInfoFactory);
member =
new MemberBox(
getDeclarationScope(), (Method) methodOrConstructor, typeInfoFactory);
isStatic = member.isStatic();
}
String methodName = member.getName();
Expand Down
Loading
Loading