Skip to content

Commit 971d45e

Browse files
committed
[GR-24822] Use Context.parse to implement ScriptEngine.compile.
PullRequest: js/1578
2 parents e7721d3 + af78d48 commit 971d45e

File tree

4 files changed

+12
-39
lines changed

4 files changed

+12
-39
lines changed

graal-js/src/com.oracle.truffle.js.scriptengine.test/src/com/oracle/truffle/js/scriptengine/test/TestScriptEngineInterop.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848

4949
import org.graalvm.polyglot.Engine;
5050
import org.junit.Assert;
51-
import org.junit.Ignore;
5251
import org.junit.Test;
5352

5453
import com.oracle.truffle.js.scriptengine.GraalJSScriptEngine;
@@ -99,7 +98,6 @@ interface TestInterface {
9998

10099
}
101100

102-
@Ignore("GR-21918")
103101
@Test
104102
public void testClose() throws ScriptException {
105103
GraalJSScriptEngine engine = GraalJSScriptEngine.create();

graal-js/src/com.oracle.truffle.js.scriptengine/src/com/oracle/truffle/js/scriptengine/GraalJSScriptEngine.java

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,6 @@ public Builder setOption(Builder builder, Object value) {
239239
private final GraalJSEngineFactory factory;
240240
private final Context.Builder contextConfig;
241241

242-
private volatile boolean closed;
243242
private boolean evalCalled;
244243

245244
GraalJSScriptEngine(GraalJSEngineFactory factory) {
@@ -292,7 +291,6 @@ static Context createDefaultContext(Context.Builder builder) {
292291
@Override
293292
public void close() {
294293
getPolyglotContext().close();
295-
closed = true;
296294
}
297295

298296
/**
@@ -388,12 +386,17 @@ private static Source createSource(String script, ScriptContext ctxt) throws Scr
388386
}
389387
}
390388

389+
private static void updateDelegatingIOStreams(Context polyglotContext, ScriptContext scriptContext) {
390+
Value polyglotBindings = polyglotContext.getPolyglotBindings();
391+
((DelegatingOutputStream) polyglotBindings.getMember(OUT_SYMBOL).asProxyObject()).setWriter(scriptContext.getWriter());
392+
((DelegatingOutputStream) polyglotBindings.getMember(ERR_SYMBOL).asProxyObject()).setWriter(scriptContext.getErrorWriter());
393+
((DelegatingInputStream) polyglotBindings.getMember(IN_SYMBOL).asProxyObject()).setReader(scriptContext.getReader());
394+
}
395+
391396
private Object eval(Source source, ScriptContext scriptContext) throws ScriptException {
392397
GraalJSBindings engineBindings = getOrCreateGraalJSBindings(scriptContext);
393398
Context polyglotContext = engineBindings.getContext();
394-
((DelegatingOutputStream) polyglotContext.getPolyglotBindings().getMember(OUT_SYMBOL).asProxyObject()).setWriter(scriptContext.getWriter());
395-
((DelegatingOutputStream) polyglotContext.getPolyglotBindings().getMember(ERR_SYMBOL).asProxyObject()).setWriter(scriptContext.getErrorWriter());
396-
((DelegatingInputStream) polyglotContext.getPolyglotBindings().getMember(IN_SYMBOL).asProxyObject()).setReader(scriptContext.getReader());
399+
updateDelegatingIOStreams(polyglotContext, scriptContext);
397400
try {
398401
if (!evalCalled) {
399402
jrunscriptInitWorkaround(source, polyglotContext);
@@ -527,18 +530,12 @@ private static <T> T getInterfaceInner(Value thiz, Class<T> iface) {
527530

528531
@Override
529532
public CompiledScript compile(String script) throws ScriptException {
530-
if (closed) {
531-
throw new IllegalStateException("Context already closed.");
532-
}
533533
Source source = createSource(script, getContext());
534534
return compile(source);
535535
}
536536

537537
@Override
538538
public CompiledScript compile(Reader reader) throws ScriptException {
539-
if (closed) {
540-
throw new IllegalStateException("Context already closed.");
541-
}
542539
Source source = createSource(read(reader), getContext());
543540
return compile(source);
544541
}
@@ -559,11 +556,8 @@ public Object eval(ScriptContext ctx) throws ScriptException {
559556
}
560557

561558
private void checkSyntax(Source source) throws ScriptException {
562-
GraalJSBindings engineBindings = getOrCreateGraalJSBindings(context);
563-
Context polyglotContext = engineBindings.getContext();
564-
Value syntaxChecker = polyglotContext.getBindings("js").getMember("checkSyntaxForScriptEngine");
565559
try {
566-
syntaxChecker.execute(source.getCharacters());
560+
getPolyglotContext().parse(source);
567561
} catch (PolyglotException pex) {
568562
throw new ScriptException(pex);
569563
}

graal-js/src/com.oracle.truffle.js/src/com/oracle/truffle/js/builtins/GlobalBuiltins.java

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@
8585
import com.oracle.truffle.api.source.SourceSection;
8686
import com.oracle.truffle.js.builtins.GlobalBuiltinsFactory.GlobalNashornExtensionParseToJSONNodeGen;
8787
import com.oracle.truffle.js.builtins.GlobalBuiltinsFactory.GlobalScriptingEXECNodeGen;
88-
import com.oracle.truffle.js.builtins.GlobalBuiltinsFactory.GlobalSyntaxCheckForScriptEngineNodeGen;
8988
import com.oracle.truffle.js.builtins.GlobalBuiltinsFactory.JSGlobalDecodeURINodeGen;
9089
import com.oracle.truffle.js.builtins.GlobalBuiltinsFactory.JSGlobalEncodeURINodeGen;
9190
import com.oracle.truffle.js.builtins.GlobalBuiltinsFactory.JSGlobalExitNodeGen;
@@ -354,7 +353,6 @@ public enum GlobalNashornScripting implements BuiltinEnum<GlobalNashornScripting
354353
readFully(1),
355354
exec(1), // $EXEC
356355
parseToJSON(3),
357-
checkSyntaxForScriptEngine(1),
358356
importScriptEngineGlobalBindings(1);
359357

360358
private final int length;
@@ -383,8 +381,6 @@ protected Object createNode(JSContext context, JSBuiltin builtin, boolean constr
383381
return GlobalNashornExtensionParseToJSONNodeGen.create(context, builtin, args().fixedArgs(3).createArgumentNodes(context));
384382
case exec:
385383
return GlobalScriptingEXECNodeGen.create(context, builtin, args().fixedArgs(2).createArgumentNodes(context));
386-
case checkSyntaxForScriptEngine:
387-
return GlobalSyntaxCheckForScriptEngineNodeGen.create(context, builtin, args().fixedArgs(1).createArgumentNodes(context));
388384
case importScriptEngineGlobalBindings:
389385
return JSGlobalImportScriptEngineGlobalBindingsNodeGen.create(context, builtin, args().fixedArgs(1).varArgs().createArgumentNodes(context));
390386
}
@@ -1616,18 +1612,4 @@ public boolean set(DynamicObject store, Object value) {
16161612
}
16171613
}
16181614
}
1619-
1620-
public abstract static class GlobalSyntaxCheckForScriptEngineNode extends JSBuiltinNode {
1621-
1622-
public GlobalSyntaxCheckForScriptEngineNode(JSContext context, JSBuiltin builtin) {
1623-
super(context, builtin);
1624-
}
1625-
1626-
@Specialization
1627-
@TruffleBoundary(transferToInterpreterOnException = false)
1628-
protected final boolean checkSyntax(Object code) {
1629-
getContext().getEvaluator().parseScript(getContext(), code.toString());
1630-
return true;
1631-
}
1632-
}
16331615
}

graal-js/src/com.oracle.truffle.js/src/com/oracle/truffle/js/runtime/JSRealm.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,10 +1178,9 @@ public void setupGlobals() {
11781178
removeNashornIncompatibleBuiltins();
11791179
}
11801180
if (context.getContextOptions().isScriptEngineGlobalScopeImport()) {
1181-
for (String builtin : new String[]{"importScriptEngineGlobalBindings", "checkSyntaxForScriptEngine"}) {
1182-
JSObjectUtil.putDataProperty(context, getScriptEngineImportScope(), builtin,
1183-
lookupFunction(GlobalBuiltins.GLOBAL_NASHORN_EXTENSIONS, builtin), JSAttributes.notConfigurableNotEnumerableNotWritable());
1184-
}
1181+
String builtin = "importScriptEngineGlobalBindings";
1182+
JSObjectUtil.putDataProperty(context, getScriptEngineImportScope(), builtin,
1183+
lookupFunction(GlobalBuiltins.GLOBAL_NASHORN_EXTENSIONS, builtin), JSAttributes.notConfigurableNotEnumerableNotWritable());
11851184
}
11861185
if (context.getContextOptions().isPolyglotBuiltin() && (getEnv().isPolyglotEvalAllowed() || getEnv().isPolyglotBindingsAccessAllowed())) {
11871186
setupPolyglot();

0 commit comments

Comments
 (0)