public void createJavaScriptFactoryForJavaClass() { Source src = Source.newBuilder("js", "" + "(function(Moment) {\n" + " return function(h, m, s) {\n" + " return new Moment(h, m, s);\n" + " };\n" + "})\n", "ConstructMoment.js").buildLiteral(); // Evaluate the JavaScript function definition final Value jsFunction = context.eval(src); // Create a JavaScript factory for the provided Java class final Value jsFactory = jsFunction.execute(Moment.class); // Convert the JavaScript factory to a Java foreign function MomentFactory momentFactory = jsFactory.as(MomentFactory.class); final Moment javaMoment = momentFactory.create(6, 30, 10); assertEquals("Hours", 6, javaMoment.hours); assertEquals("Minutes", 30, javaMoment.minutes); assertEquals("Seconds", 10, javaMoment.seconds); } // END: com.oracle.truffle.tck.impl.PolyglotEngineWithJavaScript#createJavaScriptFactoryForJavaClass
public void createJavaScriptFactoryForJavaClass() { Source src = Source.newBuilder("js", "" + "(function(Moment) {\n" + " return function(h, m, s) {\n" + " return new Moment(h, m, s);\n" + " };\n" + "})\n", "ConstructMoment.js").buildLiteral(); // Evaluate the JavaScript function definition final Value jsFunction = context.eval(src); // Create a JavaScript factory for the provided Java class final Value jsFactory = jsFunction.execute(Moment.class); // Convert the JavaScript factory to a Java foreign function MomentFactory momentFactory = jsFactory.as(MomentFactory.class); final Moment javaMoment = momentFactory.create(6, 30, 10); assertEquals("Hours", 6, javaMoment.hours); assertEquals("Minutes", 30, javaMoment.minutes); assertEquals("Seconds", 10, javaMoment.seconds); } // END: com.oracle.truffle.tck.impl.PolyglotEngineWithJavaScript#createJavaScriptFactoryForJavaClass