public void accessFieldsOfJavaObject() { Source src = Source.newBuilder("js", "" + "(function(t) {\n" + " return 3600 * t.hours + 60 * t.minutes + t.seconds;\n" + "})\n", "MomentToSeconds.js").buildLiteral(); final Moment javaMoment = new Moment(6, 30, 10); // Evaluate the JavaScript function definition Value jsFunction = context.eval(src); // Execute the JavaScript function, passing a Java object argument Value jsSeconds = jsFunction.execute(javaMoment); // Convert foreign object result to desired Java type int seconds = jsSeconds.as(Number.class).intValue(); assertEquals(3600 * 6 + 30 * 60 + 10, seconds); } // END: com.oracle.truffle.tck.impl.PolyglotEngineWithJavaScript#accessFieldsOfJavaObject
public void accessFieldsOfJavaObjectWithConverter() { Source src = Source.newBuilder("js", "" + "(function(t) {\n" + " return 3600 * t.hours + 60 * t.minutes + t.seconds;\n" + "})\n", "MomentToSeconds.js").buildLiteral(); final Moment javaMoment = new Moment(6, 30, 10); // Evaluate the JavaScript function definition final Value jsFunction = context.eval(src); // Convert the function to desired Java type MomentConverter converter = jsFunction.as(MomentConverter.class); // Execute the JavaScript function as a Java foreign function int seconds = converter.toSeconds(javaMoment); assertEquals(3600 * 6 + 30 * 60 + 10, seconds); } // END: com.oracle.truffle.tck.impl.PolyglotEngineWithJavaScript#accessFieldsOfJavaObjectWithConverter
public void accessFieldsOfJavaObject() { Source src = Source.newBuilder("js", "" + "(function(t) {\n" + " return 3600 * t.hours + 60 * t.minutes + t.seconds;\n" + "})\n", "MomentToSeconds.js").buildLiteral(); final Moment javaMoment = new Moment(6, 30, 10); // Evaluate the JavaScript function definition Value jsFunction = context.eval(src); // Execute the JavaScript function, passing a Java object argument Value jsSeconds = jsFunction.execute(javaMoment); // Convert foreign object result to desired Java type int seconds = jsSeconds.as(Number.class).intValue(); assertEquals(3600 * 6 + 30 * 60 + 10, seconds); } // END: com.oracle.truffle.tck.impl.PolyglotEngineWithJavaScript#accessFieldsOfJavaObject
public void accessFieldsOfJavaObjectWithConverter() { Source src = Source.newBuilder("js", "" + "(function(t) {\n" + " return 3600 * t.hours + 60 * t.minutes + t.seconds;\n" + "})\n", "MomentToSeconds.js").buildLiteral(); final Moment javaMoment = new Moment(6, 30, 10); // Evaluate the JavaScript function definition final Value jsFunction = context.eval(src); // Convert the function to desired Java type MomentConverter converter = jsFunction.as(MomentConverter.class); // Execute the JavaScript function as a Java foreign function int seconds = converter.toSeconds(javaMoment); assertEquals(3600 * 6 + 30 * 60 + 10, seconds); } // END: com.oracle.truffle.tck.impl.PolyglotEngineWithJavaScript#accessFieldsOfJavaObjectWithConverter