Skip to content

Commit

Permalink
The $262 object does not use LiveConnect any more
Browse files Browse the repository at this point in the history
  • Loading branch information
rPraml committed Sep 30, 2024
1 parent 95f232f commit 00bb44b
Showing 1 changed file with 24 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.nio.file.Path;
Expand All @@ -40,6 +41,7 @@
import org.junit.jupiter.api.parallel.ExecutionMode;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.mozilla.javascript.BaseFunction;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.EvaluatorException;
import org.mozilla.javascript.Kit;
Expand Down Expand Up @@ -432,20 +434,23 @@ public static void tearDownClass() {
/**
* @see https://github.com/tc39/test262/blob/main/INTERPRETING.md#host-defined-functions
*/
public static class $262 {
private ScriptableObject scope;
public static class $262 extends ScriptableObject {

static $262 install(ScriptableObject scope) {
$262 instance = new $262(scope);
static $262 install(ScriptableObject scope, Scriptable parentScope) {
$262 instance = new $262(scope, parentScope);

scope.put("$262", scope, instance);
scope.setAttributes("$262", ScriptableObject.DONTENUM);

return instance;
}

$262(ScriptableObject scope) {
this.scope = scope;
public $262() {
super();
}

$262(Scriptable scope, Scriptable prototype) {
super(scope, prototype);
}

@JSFunction
Expand All @@ -456,21 +461,20 @@ public void gc() {
@JSFunction
public Object evalScript(String source) {
try (Context cx = Context.enter()) {
return cx.evaluateString(this.scope, source, "<evalScript>", 1, null);
return cx.evaluateString(this.getParentScope(), source, "<evalScript>", 1, null);
}
}

@JSGetter
public Object getGlobal() {
return this.scope;
return this.getParentScope();
}

@JSFunction
public $262 createRealm() {
try (Context cx = Context.enter()) {
ScriptableObject realm = cx.initSafeStandardObjects();

return $262.install(realm);
return new $262(realm, getPrototype());
}
}

Expand All @@ -484,10 +488,15 @@ public void detachArrayBuffer() {
public Object getAgent() {
throw new UnsupportedOperationException("$262.agent property not yet implemented");
}

@Override
public String getClassName() {
return "__262__";
}
}

private Scriptable buildScope(Context cx, Test262Case testCase, int optLevel)
throws IOException {
throws InvocationTargetException, IllegalAccessException, InstantiationException {
ScriptableObject scope = cx.initSafeStandardObjects();

for (String harnessFile : testCase.harnessFiles) {
Expand All @@ -509,8 +518,11 @@ private Scriptable buildScope(Context cx, Test262Case testCase, int optLevel)
harnessScript.exec(cx, scope);
}

$262.install(scope);
ScriptableObject.defineClass(scope, $262.class);

// same as cx.evaluateString(scope, "var $262 = new __262__()", "<init>", 1, null);
BaseFunction ctor = (BaseFunction) scope.get("__262__", scope);
$262.install(scope, ctor.construct(cx, scope, new Object[0]));
return scope;
}

Expand Down

0 comments on commit 00bb44b

Please sign in to comment.