Skip to content
Merged
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 @@ -13,7 +13,7 @@
import org.mozilla.javascript.ContextFactory;
import org.mozilla.javascript.ScriptRuntime;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.TopLevel;

/**
* Utility class, that search for testcases in "assets/tests".
Expand All @@ -25,7 +25,7 @@
public abstract class TestCase {

protected final String name;
protected final Scriptable global;
protected final TopLevel global;

private static final ContextFactory factory =
new ContextFactory() {
Expand All @@ -47,17 +47,15 @@ protected Context makeContext() {
}
};

public TestCase(String name, Scriptable global) {
public TestCase(String name, TopLevel global) {
this.name = name;
this.global = global;
}

public String run() {
Context cx = factory.enterContext();
try {
Scriptable scope = cx.newObject(global);
scope.setPrototype(global);
scope.setParentScope(null);
Scriptable scope = TopLevel.createIsolate(global);
return ScriptRuntime.toString(runTest(cx, scope));
} finally {
Context.exit();
Expand All @@ -74,7 +72,7 @@ public String toString() {
public static class AssetScript extends TestCase {
protected final AssetManager assetManager;

public AssetScript(String name, Scriptable global, AssetManager assetManager) {
public AssetScript(String name, TopLevel global, AssetManager assetManager) {
super(name, global);
this.assetManager = assetManager;
}
Expand All @@ -94,7 +92,7 @@ public static List<TestCase> getTestCases(android.content.Context context) throw

AssetManager assetManager = context.getAssets();
// define assert object
ScriptableObject global;
TopLevel global;
Context cx = factory.enterContext();
try (InputStream in = assetManager.open("assert.js");
Reader rdr = new InputStreamReader(in, StandardCharsets.UTF_8)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.TopLevel;
import org.mozilla.javascript.lc.type.TypeInfoFactory;
import org.mozilla.javascript.lc.type.impl.factory.ClassValueCacheFactory;

/**
* @author ZZZank
*/
public class TypeInfoFactoryTestCase extends TestCase {
public TypeInfoFactoryTestCase(String name, Scriptable global) {
public TypeInfoFactoryTestCase(String name, TopLevel global) {
super(name, global);
}

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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a small comment here that Builtins are stored as associatedValue on the scope and not thisObj which is now just the GlobalThis object? Not a big deal.

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,17 @@ 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 =
TopLevel.createIsolate(
topLevelScope,
new BindingsObject(sc.getBindings(ScriptContext.GLOBAL_SCOPE)));
return TopLevel.createIsolate(
globalScope, new BindingsObject(sc.getBindings(ScriptContext.ENGINE_SCOPE)));
} else {
return TopLevel.createIsolate(
topLevelScope, 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 @@ -97,9 +97,12 @@ public class Global extends ImporterTopLevel {
private static final 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) {
// that these functions are not part of ECMA.
initStandardObjects(cx, sealedStdLib);
NativeConsole.init(this, sealedStdLib, new ShellConsolePrinter());
defineFunctionProperties(TOP_COMMANDS, Global.class, ScriptableObject.DONTENUM);
defineFunctionProperties(this, TOP_COMMANDS, Global.class, ScriptableObject.DONTENUM);

// Set up "environment" in the global scope to provide access to the
// System environment variables.
Expand Down Expand Up @@ -261,7 +264,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 @@ -295,7 +298,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 @@ -348,7 +352,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 @@ -361,7 +365,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 @@ -251,7 +251,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
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 @@ -137,10 +137,10 @@ public static Object coercibleIterativeMethod(
Object callbackArg = args.length > 0 ? args[0] : Undefined.instance;

Function f = getCallbackArg(cx, callbackArg);
Scriptable parent = ScriptableObject.getTopLevelScope(f);
TopLevel parent = ScriptableObject.getTopLevelScope(f);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a minor thing but in my new Java code I have been getting more judicious about using "var" in cases like this and if we had done so before then perhaps this would not have been necessary to make this particular change.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I've been using var a lot and it would have reduced a bunch of these changes, but I've had enough push back on changing explicit types to var that I'm wary of doing it as part of a complex behavioural change.

Scriptable thisArg;
if (args.length < 2 || args[1] == null || args[1] == Undefined.instance) {
thisArg = parent;
thisArg = parent.getGlobalThis();
} else {
thisArg = ScriptRuntime.toObject(cx, scope, args[1]);
}
Expand Down Expand Up @@ -339,7 +339,8 @@ public static Object reduceMethodWithLength(
throw ScriptRuntime.notFunctionError(callbackArg);
}
Function f = (Function) callbackArg;
Scriptable parent = ScriptableObject.getTopLevelScope(f);
TopLevel parent = ScriptableObject.getTopLevelScope(f);
Scriptable globalThis = parent.getGlobalThis();
// 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 @@ -354,7 +355,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
9 changes: 5 additions & 4 deletions rhino/src/main/java/org/mozilla/javascript/BoundFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,12 @@ Object[] getBoundArgs() {

Scriptable getCallThis(Context cx, Scriptable scope) {
Scriptable callThis = boundThis;
if (callThis == null && ScriptRuntime.hasTopCall(cx)) {
callThis = ScriptRuntime.getTopCallScope(cx);
}
if (callThis == null) {
callThis = getTopLevelScope(scope);
if (ScriptRuntime.hasTopCall(cx)) {
callThis = ScriptRuntime.getTopCallScope(cx).getGlobalThis();
} else {
callThis = getTopLevelScope(scope).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
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ private static class LambdaGetSetPropDesc extends PropDesc {
@Override
void makeProp(Context cx, Scriptable scope, ScriptableObject obj) {
if (name instanceof String) {
obj.defineProperty(cx, (String) name, getter, setter, attributes);
obj.defineProperty(cx, scope, (String) name, getter, setter, attributes);
} else {
obj.defineProperty(cx, (SymbolKey) name, getter, setter, attributes);
obj.defineProperty(cx, scope, (SymbolKey) name, getter, setter, attributes);
}
}
}
Expand Down
Loading