Skip to content

Commit

Permalink
Only initialize graal JS conetx when required. Fixes #1723
Browse files Browse the repository at this point in the history
  • Loading branch information
johnaohara authored May 22, 2024
1 parent cdad3f6 commit 1e5fd60
Showing 1 changed file with 18 additions and 17 deletions.
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

0 comments on commit 1e5fd60

Please sign in to comment.