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.interop.java.MethodMessage;
51 import com.oracle.truffle.api.source.Source;
52 import com.oracle.truffle.api.vm.PolyglotEngine;
53 import java.io.Closeable;
54 import java.io.IOException;
55 import java.io.Reader;
57 import java.util.ArrayList;
58 import java.util.List;
59 import java.util.concurrent.Executor;
60 import org.netbeans.html.boot.spi.Fn;
61 import org.netbeans.html.boot.spi.Fn.Presenter;
64 * Implementation of {@link Presenter} that delegates to Truffle.
66 * @author Jaroslav Tulach
68 final class TrufflePresenter implements Fn.KeepAlive,
69 Presenter, Fn.FromJavaScript, Fn.ToJavaScript, Executor {
72 private WrapArray copy;
73 private final Executor exc;
75 TrufflePresenter(Executor exc, TruffleObject eval) {
77 this.eval = eval == null ? null : JavaInterop.asJavaFunction(Eval.class, eval);
81 public Fn defineFn(String code, String... names) {
82 return defineImpl(code, names, null);
86 public Fn defineFn(String code, String[] names, boolean[] keepAlive) {
87 return defineImpl(code, names, keepAlive);
90 private FnImpl defineImpl(String code, String[] names, boolean[] keepAlive) {
91 StringBuilder sb = new StringBuilder();
92 sb.append("(function() {\n");
93 sb.append(" return function(");
96 for (String n : names) {
97 sb.append(sep).append(n);
103 sb.append("\n };\n");
106 TruffleObject fn = (TruffleObject) getEval().eval(sb.toString());
107 return new FnImpl(this, fn, names.length);
111 public void displayPage(URL page, Runnable onPageLoad) {
112 if (onPageLoad != null) {
118 public void loadScript(Reader code) throws Exception {
119 Source src = Source.fromReader(code, "unknown.js");
120 getEval().eval(src.getCode());
124 @MethodMessage(message = "HAS_SIZE")
125 public boolean hasSize();
129 @MethodMessage(message = "IS_NULL")
130 public boolean isNull();
133 interface WrapArray {
134 public Object copy(Object arr);
138 public Object eval(String code);
141 final Object checkArray(Object val) throws Exception {
142 if (val instanceof TruffleObject) {
143 final TruffleObject truffleObj = (TruffleObject)val;
144 IsArray arrayTest = JavaInterop.asJavaObject(IsArray.class, truffleObj);
146 if (arrayTest.hasSize()) {
147 List<?> list = JavaInterop.asJavaObject(List.class, truffleObj);
148 return list.toArray();
150 } catch (NegativeArraySizeException ex) {
158 public Object toJava(Object jsArray) {
159 if (jsArray instanceof JavaValue) {
160 jsArray = ((JavaValue) jsArray).get();
162 if (jsArray instanceof TruffleObject) {
163 IsNull checkNull = JavaInterop.asJavaFunction(IsNull.class, (TruffleObject)jsArray);
165 if (checkNull.isNull()) {
168 } catch (NegativeArraySizeException ex) {
169 System.err.println("negative size for " + jsArray);
170 ex.printStackTrace();
174 return checkArray(jsArray);
175 } catch (Exception ex) {
176 throw new IllegalStateException(ex);
181 public Object toJavaScript(Object conv) {
182 return JavaValue.toJavaScript(conv, getWrap());
186 public void execute(final Runnable command) {
187 if (Fn.activePresenter() == this) {
192 class Wrap implements Runnable {
195 try (Closeable c = Fn.activate(TrufflePresenter.this)) {
197 } catch (IOException ex) {
198 throw new IllegalStateException(ex);
202 final Runnable wrap = new Wrap();
210 private Eval getEval() {
213 PolyglotEngine engine = PolyglotEngine.newBuilder().build();
214 TruffleObject fn = (TruffleObject) engine.eval(
215 Source.fromText("eval.bind(this)", "eval.js").withMimeType("text/javascript")
217 eval = JavaInterop.asJavaFunction(Eval.class, fn);
218 } catch (IOException ex) {
219 throw new IllegalStateException(ex);
225 private WrapArray getWrap() {
227 TruffleObject fn = (TruffleObject) getEval().eval("(function(arr) {\n"
229 + " for (var i = 0; i < arr.length; i++) {\n"
230 + " n[i] = arr[i];\n"
235 copy = JavaInterop.asJavaFunction(WrapArray.class, fn);
240 private class FnImpl extends Fn {
242 private final CallTarget fn;
243 private final boolean[] keepAlive;
245 public FnImpl(Presenter presenter, TruffleObject fn, int arity) {
247 this.fn = Truffle.getRuntime().createCallTarget(new FnRootNode(fn, arity));
248 this.keepAlive = null;
252 public Object invoke(Object thiz, Object... args) throws Exception {
253 List<Object> all = new ArrayList<>(args.length + 1);
254 all.add(thiz == null ? fn : toJavaScript(thiz));
255 for (int i = 0; i < args.length; i++) {
256 Object conv = args[i];
257 conv = toJavaScript(conv);
260 Object ret = fn.call(all.toArray());
261 if (ret instanceof JavaValue) {
262 ret = ((JavaValue)ret).get();
271 static abstract class JavaLang extends TruffleLanguage<Object> {