public void callJavaScriptFunctionsWithSharedStateFromJava() { Source src = Source.newBuilder("js", "" + "(function() {\n" + " var seconds = 0;\n" + " function addTime(h, m, s) {\n" + " seconds += 3600 * h;\n" + " seconds += 60 * m;\n" + " seconds += s;\n" + " }\n" + " function time() {\n" + " return seconds;\n" + " }\n" + " return {\n" + " 'addTime': addTime,\n" + " 'timeInSeconds': time\n" + " }\n" + "})\n", "CountSeconds.js").buildLiteral(); // Evaluate JavaScript function definition Value jsFunction = context.eval(src); // Execute the JavaScript function Value jsObject = jsFunction.execute(); // Create Java access to the JavaScript object Counter counter = jsObject.as(Counter.class); counter.addTime(6, 30, 0); counter.addTime(9, 0, 0); counter.addTime(12, 5, 30); assertEquals(99330, counter.timeInSeconds()); } // END: com.oracle.truffle.tck.impl.PolyglotEngineWithJavaScript#callJavaScriptFunctionsWithSharedStateFromJava
public void callJavaScriptFunctionsWithSharedStateFromJava() { Source src = Source.newBuilder("js", "" + "(function() {\n" + " var seconds = 0;\n" + " function addTime(h, m, s) {\n" + " seconds += 3600 * h;\n" + " seconds += 60 * m;\n" + " seconds += s;\n" + " }\n" + " function time() {\n" + " return seconds;\n" + " }\n" + " return {\n" + " 'addTime': addTime,\n" + " 'timeInSeconds': time\n" + " }\n" + "})\n", "CountSeconds.js").buildLiteral(); // Evaluate JavaScript function definition Value jsFunction = context.eval(src); // Execute the JavaScript function Value jsObject = jsFunction.execute(); // Create Java access to the JavaScript object Counter counter = jsObject.as(Counter.class); counter.addTime(6, 30, 0); counter.addTime(9, 0, 0); counter.addTime(12, 5, 30); assertEquals(99330, counter.timeInSeconds()); } // END: com.oracle.truffle.tck.impl.PolyglotEngineWithJavaScript#callJavaScriptFunctionsWithSharedStateFromJava