Skip to content

Commit dbb4185

Browse files
woesselkorchi
authored andcommitted
[GR-58687] Add regression test for Polyglot.eval returning a java.lang.String.
(cherry picked from commit 5178ca8)
1 parent edf6eaf commit dbb4185

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

graal-js/src/com.oracle.truffle.js.test/src/com/oracle/truffle/js/test/polyglot/PolyglotBuiltinTest.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -47,16 +47,24 @@
4747
import static org.junit.Assert.assertTrue;
4848
import static org.junit.Assert.fail;
4949

50+
import java.io.File;
51+
import java.nio.file.Files;
52+
5053
import org.graalvm.polyglot.Context;
5154
import org.graalvm.polyglot.PolyglotAccess;
5255
import org.graalvm.polyglot.PolyglotException;
5356
import org.graalvm.polyglot.Source;
5457
import org.graalvm.polyglot.Value;
58+
import org.graalvm.polyglot.io.IOAccess;
5559
import org.graalvm.polyglot.proxy.ProxyExecutable;
5660
import org.junit.Test;
5761

62+
import com.oracle.truffle.api.CallTarget;
63+
import com.oracle.truffle.api.nodes.RootNode;
5864
import com.oracle.truffle.js.lang.JavaScriptLanguage;
65+
import com.oracle.truffle.js.runtime.Errors;
5966
import com.oracle.truffle.js.runtime.JSContextOptions;
67+
import com.oracle.truffle.js.runtime.JSRuntime;
6068
import com.oracle.truffle.js.test.JSTest;
6169

6270
/**
@@ -279,4 +287,34 @@ public void testEvalInternalLanguage() {
279287
}
280288
}
281289
}
290+
291+
@SuppressWarnings("try")
292+
@Test
293+
public void testEvalReturnValue() throws Exception {
294+
try (AutoCloseable languageScope = TestLanguage.withTestLanguage(new TestLanguage() {
295+
@Override
296+
protected CallTarget parse(ParsingRequest request) throws Exception {
297+
String code = request.getSource().getCharacters().toString();
298+
if (code.startsWith("\"") && code.endsWith("\"")) {
299+
String string = code.substring(1, code.length() - 1);
300+
return RootNode.createConstantNode(string).getCallTarget();
301+
}
302+
throw Errors.createSyntaxError(code);
303+
}
304+
})) {
305+
IOAccess fileAccess = IOAccess.newBuilder().allowHostFileAccess(true).build();
306+
try (Context context = Context.newBuilder().allowIO(fileAccess).allowPolyglotAccess(PolyglotAccess.ALL).build()) {
307+
Value result = context.eval(Source.create(JavaScriptLanguage.ID, "Polyglot.eval('" + TestLanguage.ID + "', '\"something\"');"));
308+
assertTrue(result.isString());
309+
assertEquals("something", result.asString());
310+
311+
File tmpFile = File.createTempFile("polyglot-evalfile-test", null);
312+
tmpFile.deleteOnExit();
313+
Files.writeString(tmpFile.toPath(), "\"nanika\"");
314+
result = context.eval(Source.create(JavaScriptLanguage.ID, "Polyglot.evalFile('" + TestLanguage.ID + "', " + JSRuntime.quote(tmpFile.getPath()) + ");"));
315+
assertTrue(result.isString());
316+
assertEquals("nanika", result.asString());
317+
}
318+
}
319+
}
282320
}

0 commit comments

Comments
 (0)