Skip to content

Commit

Permalink
[GR-58687] Add regression test for Polyglot.eval returning a java.lan…
Browse files Browse the repository at this point in the history
…g.String.
  • Loading branch information
woess committed Oct 4, 2024
1 parent 9fbb93a commit 5178ca8
Showing 1 changed file with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,24 @@
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.File;
import java.nio.file.Files;

import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.PolyglotAccess;
import org.graalvm.polyglot.PolyglotException;
import org.graalvm.polyglot.Source;
import org.graalvm.polyglot.Value;
import org.graalvm.polyglot.io.IOAccess;
import org.graalvm.polyglot.proxy.ProxyExecutable;
import org.junit.Test;

import com.oracle.truffle.api.CallTarget;
import com.oracle.truffle.api.nodes.RootNode;
import com.oracle.truffle.js.lang.JavaScriptLanguage;
import com.oracle.truffle.js.runtime.Errors;
import com.oracle.truffle.js.runtime.JSContextOptions;
import com.oracle.truffle.js.runtime.JSRuntime;
import com.oracle.truffle.js.test.JSTest;

/**
Expand Down Expand Up @@ -279,4 +287,34 @@ public void testEvalInternalLanguage() {
}
}
}

@SuppressWarnings("try")
@Test
public void testEvalReturnValue() throws Exception {
try (AutoCloseable languageScope = TestLanguage.withTestLanguage(new TestLanguage() {
@Override
protected CallTarget parse(ParsingRequest request) throws Exception {
String code = request.getSource().getCharacters().toString();
if (code.startsWith("\"") && code.endsWith("\"")) {
String string = code.substring(1, code.length() - 1);
return RootNode.createConstantNode(string).getCallTarget();
}
throw Errors.createSyntaxError(code);
}
})) {
IOAccess fileAccess = IOAccess.newBuilder().allowHostFileAccess(true).build();
try (Context context = Context.newBuilder().allowIO(fileAccess).allowPolyglotAccess(PolyglotAccess.ALL).build()) {
Value result = context.eval(Source.create(JavaScriptLanguage.ID, "Polyglot.eval('" + TestLanguage.ID + "', '\"something\"');"));
assertTrue(result.isString());
assertEquals("something", result.asString());

File tmpFile = File.createTempFile("polyglot-evalfile-test", null);
tmpFile.deleteOnExit();
Files.writeString(tmpFile.toPath(), "\"nanika\"");
result = context.eval(Source.create(JavaScriptLanguage.ID, "Polyglot.evalFile('" + TestLanguage.ID + "', " + JSRuntime.quote(tmpFile.getPath()) + ");"));
assertTrue(result.isString());
assertEquals("nanika", result.asString());
}
}
}
}

0 comments on commit 5178ca8

Please sign in to comment.