|
1 | 1 | /*
|
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. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
4 | 4 | *
|
5 | 5 | * The Universal Permissive License (UPL), Version 1.0
|
|
47 | 47 | import static org.junit.Assert.assertTrue;
|
48 | 48 | import static org.junit.Assert.fail;
|
49 | 49 |
|
| 50 | +import java.io.File; |
| 51 | +import java.nio.file.Files; |
| 52 | + |
50 | 53 | import org.graalvm.polyglot.Context;
|
51 | 54 | import org.graalvm.polyglot.PolyglotAccess;
|
52 | 55 | import org.graalvm.polyglot.PolyglotException;
|
53 | 56 | import org.graalvm.polyglot.Source;
|
54 | 57 | import org.graalvm.polyglot.Value;
|
| 58 | +import org.graalvm.polyglot.io.IOAccess; |
55 | 59 | import org.graalvm.polyglot.proxy.ProxyExecutable;
|
56 | 60 | import org.junit.Test;
|
57 | 61 |
|
| 62 | +import com.oracle.truffle.api.CallTarget; |
| 63 | +import com.oracle.truffle.api.nodes.RootNode; |
58 | 64 | import com.oracle.truffle.js.lang.JavaScriptLanguage;
|
| 65 | +import com.oracle.truffle.js.runtime.Errors; |
59 | 66 | import com.oracle.truffle.js.runtime.JSContextOptions;
|
| 67 | +import com.oracle.truffle.js.runtime.JSRuntime; |
60 | 68 | import com.oracle.truffle.js.test.JSTest;
|
61 | 69 |
|
62 | 70 | /**
|
@@ -279,4 +287,34 @@ public void testEvalInternalLanguage() {
|
279 | 287 | }
|
280 | 288 | }
|
281 | 289 | }
|
| 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 | + } |
282 | 320 | }
|
0 commit comments