Non-execute messages cannot be sent via JavaInterop.asJavaFunction currently. Sending them directly with help of RootNode/CallTarget.
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
6 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7 * Other names may be trademarks of their respective owners.
9 * The contents of this file are subject to the terms of either the GNU
10 * General Public License Version 2 only ("GPL") or the Common
11 * Development and Distribution License("CDDL") (collectively, the
12 * "License"). You may not use this file except in compliance with the
13 * License. You can obtain a copy of the License at
14 * http://www.netbeans.org/cddl-gplv2.html
15 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16 * specific language governing permissions and limitations under the
17 * License. When distributing the software, include this License Header
18 * Notice in each file and include the License file at
19 * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
20 * particular file as subject to the "Classpath" exception as provided
21 * by Oracle in the GPL Version 2 section of the License file that
22 * accompanied this code. If applicable, add the following below the
23 * License Header, with the fields enclosed by brackets [] replaced by
24 * your own identifying information:
25 * "Portions Copyrighted [year] [name of copyright owner]"
29 * The Original Software is NetBeans. The Initial Developer of the Original
30 * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
32 * If you wish your version of this file to be governed by only the CDDL
33 * or only the GPL Version 2, indicate your decision by adding
34 * "[Contributor] elects to include this software in this distribution
35 * under the [CDDL or GPL Version 2] license." If you do not indicate a
36 * single choice of license, a recipient has the option to distribute
37 * your version of this file under either the CDDL, the GPL Version 2 or
38 * to extend the choice of license to its licensees as provided above.
39 * However, if you add GPL Version 2 code and therefore, elected the GPL
40 * Version 2 license, then the option applies only if the new code is
41 * made subject to such option by the copyright holder.
43 package net.java.html.boot.truffle;
45 import com.oracle.truffle.api.CallTarget;
46 import com.oracle.truffle.api.Truffle;
47 import com.oracle.truffle.api.TruffleLanguage;
48 import com.oracle.truffle.api.interop.TruffleObject;
49 import com.oracle.truffle.api.interop.java.JavaInterop;
50 import com.oracle.truffle.api.source.Source;
51 import com.oracle.truffle.api.vm.PolyglotEngine;
52 import java.io.Closeable;
53 import java.io.IOException;
54 import java.io.Reader;
56 import java.util.ArrayList;
57 import java.util.List;
58 import java.util.concurrent.Executor;
59 import org.netbeans.html.boot.spi.Fn;
60 import org.netbeans.html.boot.spi.Fn.Presenter;
63 * Implementation of {@link Presenter} that delegates to Truffle.
65 * @author Jaroslav Tulach
67 final class TrufflePresenter implements Fn.KeepAlive,
68 Presenter, Fn.FromJavaScript, Fn.ToJavaScript, Executor {
71 private WrapArray copy;
72 private final Executor exc;
73 private final CallTarget isNull;
74 private final CallTarget isArray;
76 TrufflePresenter(Executor exc, TruffleObject eval) {
78 this.eval = eval == null ? null : JavaInterop.asJavaFunction(Eval.class, eval);
79 this.isNull = Truffle.getRuntime().createCallTarget(new IsNullNode());
80 this.isArray = Truffle.getRuntime().createCallTarget(new IsArrayNode());
84 public Fn defineFn(String code, String... names) {
85 return defineImpl(code, names, null);
89 public Fn defineFn(String code, String[] names, boolean[] keepAlive) {
90 return defineImpl(code, names, keepAlive);
93 private FnImpl defineImpl(String code, String[] names, boolean[] keepAlive) {
94 StringBuilder sb = new StringBuilder();
95 sb.append("(function() {\n");
96 sb.append(" return function(");
99 for (String n : names) {
100 sb.append(sep).append(n);
106 sb.append("\n };\n");
109 TruffleObject fn = (TruffleObject) getEval().eval(sb.toString());
110 return new FnImpl(this, fn, names.length);
114 public void displayPage(URL page, Runnable onPageLoad) {
115 if (onPageLoad != null) {
121 public void loadScript(Reader code) throws Exception {
122 Source src = Source.fromReader(code, "unknown.js");
123 getEval().eval(src.getCode());
126 interface WrapArray {
127 public Object copy(Object arr);
131 public Object eval(String code);
134 final Object checkArray(Object val) throws Exception {
135 if (val instanceof TruffleObject) {
136 final TruffleObject truffleObj = (TruffleObject)val;
137 boolean hasSize = (boolean) isArray.call(truffleObj);
139 List<?> list = JavaInterop.asJavaObject(List.class, truffleObj);
140 Object[] arr = list.toArray();
141 for (int i = 0; i < arr.length; i++) {
142 arr[i] = toJava(arr[i]);
151 public Object toJava(Object jsArray) {
152 if (jsArray instanceof JavaValue) {
153 jsArray = ((JavaValue) jsArray).get();
155 if (jsArray instanceof TruffleObject) {
156 boolean checkNull = (boolean) isNull.call(jsArray);
162 return checkArray(jsArray);
163 } catch (Exception ex) {
164 throw new IllegalStateException(ex);
169 public Object toJavaScript(Object conv) {
170 return JavaValue.toJavaScript(conv, getWrap());
174 public void execute(final Runnable command) {
175 if (Fn.activePresenter() == this) {
180 class Wrap implements Runnable {
183 try (Closeable c = Fn.activate(TrufflePresenter.this)) {
185 } catch (IOException ex) {
186 throw new IllegalStateException(ex);
190 final Runnable wrap = new Wrap();
198 private Eval getEval() {
201 PolyglotEngine engine = PolyglotEngine.newBuilder().build();
202 TruffleObject fn = (TruffleObject) engine.eval(
203 Source.fromText("eval.bind(this)", "eval.js").withMimeType("text/javascript")
205 eval = JavaInterop.asJavaFunction(Eval.class, fn);
206 } catch (IOException ex) {
207 throw new IllegalStateException(ex);
213 private WrapArray getWrap() {
215 TruffleObject fn = (TruffleObject) getEval().eval("(function(arr) {\n"
217 + " for (var i = 0; i < arr.length; i++) {\n"
218 + " n[i] = arr[i];\n"
223 copy = JavaInterop.asJavaFunction(WrapArray.class, fn);
228 private class FnImpl extends Fn {
230 private final CallTarget fn;
231 private final boolean[] keepAlive;
233 public FnImpl(Presenter presenter, TruffleObject fn, int arity) {
235 this.fn = Truffle.getRuntime().createCallTarget(new FnRootNode(fn, arity));
236 this.keepAlive = null;
240 public Object invoke(Object thiz, Object... args) throws Exception {
241 List<Object> all = new ArrayList<>(args.length + 1);
242 all.add(thiz == null ? fn : toJavaScript(thiz));
243 for (int i = 0; i < args.length; i++) {
244 Object conv = args[i];
245 conv = toJavaScript(conv);
248 Object ret = fn.call(all.toArray());
249 if (ret instanceof JavaValue) {
250 ret = ((JavaValue)ret).get();
259 static abstract class JavaLang extends TruffleLanguage<Object> {