Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only initialize graal JS context when required. Fixes #1723 #1725

Merged
merged 1 commit into from
May 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -595,14 +595,15 @@ static <T> void evaluateWithCombinationFunction(List<T> inputData,
ExecutionExceptionConsumer<T> onJsEvaluationException,
Consumer<String> jsOutputConsumer) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try (org.graalvm.polyglot.Context context = createContext(out)) {
context.enter();
try {
setupContext(context);
for (int i = 0; i < inputData.size(); i++) {
T element = inputData.get(i);
String jsFuncBody = jsCombinationFunction.apply(element);
if (jsFuncBody != null && !jsFuncBody.isBlank()) {

for (int i = 0; i < inputData.size(); i++) {
T element = inputData.get(i);
String jsFuncBody = jsCombinationFunction.apply(element);
if (jsFuncBody != null && !jsFuncBody.isBlank()) {
try (org.graalvm.polyglot.Context context = createContext(out)) {
context.enter();
try {
setupContext(context);
StringBuilder jsCode = new StringBuilder("const __obj").append(i).append(" = ").append(evaluationInputObject.apply(element)).append(";\n");
jsCode.append("const __func").append(i).append(" = ").append(jsFuncBody).append(";\n");
jsCode.append("__func").append(i).append("(__obj").append(i).append(")");
Expand All @@ -613,19 +614,19 @@ static <T> void evaluateWithCombinationFunction(List<T> inputData,
} catch (PolyglotException e) {
onJsEvaluationException.accept(element, e, jsCode.toString());
}
} else {
nonFuncResultConsumer.accept(element);
} catch (IOException e) {
onJsEvaluationException.accept(null, e, "<init>");
} finally {
context.leave();
}
}
} catch (IOException e) {
onJsEvaluationException.accept(null, e, "<init>");
} finally {
if (out.size() > 0) {
jsOutputConsumer.accept(out.toString(StandardCharsets.UTF_8));
}
context.leave();
} else {
nonFuncResultConsumer.accept(element);
}
}
if (out.size() > 0) {
jsOutputConsumer.accept(out.toString(StandardCharsets.UTF_8));
}
}

private static Context createContext(OutputStream out){
Expand Down
Loading