Non-execute messages cannot be sent via JavaInterop.asJavaFunction currently. Sending them directly with help of RootNode/CallTarget. Truffle
authorJaroslav Tulach <jtulach@netbeans.org>
Sat, 16 Jul 2016 08:11:49 +0200
branchTruffle
changeset 109820246cbd02c3
parent 1097 62f1d7c47a60
child 1101 1c4559516918
Non-execute messages cannot be sent via JavaInterop.asJavaFunction currently. Sending them directly with help of RootNode/CallTarget.
boot-truffle/src/main/java/net/java/html/boot/truffle/IsArrayNode.java
boot-truffle/src/main/java/net/java/html/boot/truffle/IsNullNode.java
boot-truffle/src/main/java/net/java/html/boot/truffle/TrufflePresenter.java
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/boot-truffle/src/main/java/net/java/html/boot/truffle/IsArrayNode.java	Sat Jul 16 08:11:49 2016 +0200
     1.3 @@ -0,0 +1,68 @@
     1.4 +/**
     1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.6 + *
     1.7 + * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
     1.8 + *
     1.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    1.10 + * Other names may be trademarks of their respective owners.
    1.11 + *
    1.12 + * The contents of this file are subject to the terms of either the GNU
    1.13 + * General Public License Version 2 only ("GPL") or the Common
    1.14 + * Development and Distribution License("CDDL") (collectively, the
    1.15 + * "License"). You may not use this file except in compliance with the
    1.16 + * License. You can obtain a copy of the License at
    1.17 + * http://www.netbeans.org/cddl-gplv2.html
    1.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    1.19 + * specific language governing permissions and limitations under the
    1.20 + * License.  When distributing the software, include this License Header
    1.21 + * Notice in each file and include the License file at
    1.22 + * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    1.23 + * particular file as subject to the "Classpath" exception as provided
    1.24 + * by Oracle in the GPL Version 2 section of the License file that
    1.25 + * accompanied this code. If applicable, add the following below the
    1.26 + * License Header, with the fields enclosed by brackets [] replaced by
    1.27 + * your own identifying information:
    1.28 + * "Portions Copyrighted [year] [name of copyright owner]"
    1.29 + *
    1.30 + * Contributor(s):
    1.31 + *
    1.32 + * The Original Software is NetBeans. The Initial Developer of the Original
    1.33 + * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
    1.34 + *
    1.35 + * If you wish your version of this file to be governed by only the CDDL
    1.36 + * or only the GPL Version 2, indicate your decision by adding
    1.37 + * "[Contributor] elects to include this software in this distribution
    1.38 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
    1.39 + * single choice of license, a recipient has the option to distribute
    1.40 + * your version of this file under either the CDDL, the GPL Version 2 or
    1.41 + * to extend the choice of license to its licensees as provided above.
    1.42 + * However, if you add GPL Version 2 code and therefore, elected the GPL
    1.43 + * Version 2 license, then the option applies only if the new code is
    1.44 + * made subject to such option by the copyright holder.
    1.45 + */
    1.46 +package net.java.html.boot.truffle;
    1.47 +
    1.48 +import com.oracle.truffle.api.TruffleLanguage;
    1.49 +import com.oracle.truffle.api.frame.VirtualFrame;
    1.50 +import com.oracle.truffle.api.interop.ForeignAccess;
    1.51 +import com.oracle.truffle.api.interop.Message;
    1.52 +import com.oracle.truffle.api.interop.TruffleObject;
    1.53 +import com.oracle.truffle.api.nodes.Node;
    1.54 +import com.oracle.truffle.api.nodes.RootNode;
    1.55 +
    1.56 +final class IsArrayNode extends RootNode {
    1.57 +    @Child
    1.58 +    private Node check;
    1.59 +
    1.60 +    IsArrayNode() {
    1.61 +        super(TruffleLanguage.class, null, null);
    1.62 +        this.check = Message.HAS_SIZE.createNode();
    1.63 +    }
    1.64 +
    1.65 +    @Override
    1.66 +    public Object execute(VirtualFrame frame) {
    1.67 +        final Object[] args = frame.getArguments();
    1.68 +        Object result = ForeignAccess.sendIsNull(check, frame, (TruffleObject) args[0]);
    1.69 +        return result;
    1.70 +    }
    1.71 +}
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/boot-truffle/src/main/java/net/java/html/boot/truffle/IsNullNode.java	Sat Jul 16 08:11:49 2016 +0200
     2.3 @@ -0,0 +1,68 @@
     2.4 +/**
     2.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     2.6 + *
     2.7 + * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
     2.8 + *
     2.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    2.10 + * Other names may be trademarks of their respective owners.
    2.11 + *
    2.12 + * The contents of this file are subject to the terms of either the GNU
    2.13 + * General Public License Version 2 only ("GPL") or the Common
    2.14 + * Development and Distribution License("CDDL") (collectively, the
    2.15 + * "License"). You may not use this file except in compliance with the
    2.16 + * License. You can obtain a copy of the License at
    2.17 + * http://www.netbeans.org/cddl-gplv2.html
    2.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    2.19 + * specific language governing permissions and limitations under the
    2.20 + * License.  When distributing the software, include this License Header
    2.21 + * Notice in each file and include the License file at
    2.22 + * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    2.23 + * particular file as subject to the "Classpath" exception as provided
    2.24 + * by Oracle in the GPL Version 2 section of the License file that
    2.25 + * accompanied this code. If applicable, add the following below the
    2.26 + * License Header, with the fields enclosed by brackets [] replaced by
    2.27 + * your own identifying information:
    2.28 + * "Portions Copyrighted [year] [name of copyright owner]"
    2.29 + *
    2.30 + * Contributor(s):
    2.31 + *
    2.32 + * The Original Software is NetBeans. The Initial Developer of the Original
    2.33 + * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
    2.34 + *
    2.35 + * If you wish your version of this file to be governed by only the CDDL
    2.36 + * or only the GPL Version 2, indicate your decision by adding
    2.37 + * "[Contributor] elects to include this software in this distribution
    2.38 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
    2.39 + * single choice of license, a recipient has the option to distribute
    2.40 + * your version of this file under either the CDDL, the GPL Version 2 or
    2.41 + * to extend the choice of license to its licensees as provided above.
    2.42 + * However, if you add GPL Version 2 code and therefore, elected the GPL
    2.43 + * Version 2 license, then the option applies only if the new code is
    2.44 + * made subject to such option by the copyright holder.
    2.45 + */
    2.46 +package net.java.html.boot.truffle;
    2.47 +
    2.48 +import com.oracle.truffle.api.TruffleLanguage;
    2.49 +import com.oracle.truffle.api.frame.VirtualFrame;
    2.50 +import com.oracle.truffle.api.interop.ForeignAccess;
    2.51 +import com.oracle.truffle.api.interop.Message;
    2.52 +import com.oracle.truffle.api.interop.TruffleObject;
    2.53 +import com.oracle.truffle.api.nodes.Node;
    2.54 +import com.oracle.truffle.api.nodes.RootNode;
    2.55 +
    2.56 +final class IsNullNode extends RootNode {
    2.57 +    @Child
    2.58 +    private Node check;
    2.59 +
    2.60 +    IsNullNode() {
    2.61 +        super(TruffleLanguage.class, null, null);
    2.62 +        this.check = Message.IS_NULL.createNode();
    2.63 +    }
    2.64 +
    2.65 +    @Override
    2.66 +    public Object execute(VirtualFrame frame) {
    2.67 +        final Object[] args = frame.getArguments();
    2.68 +        Object result = ForeignAccess.sendIsNull(check, frame, (TruffleObject) args[0]);
    2.69 +        return result;
    2.70 +    }
    2.71 +}
     3.1 --- a/boot-truffle/src/main/java/net/java/html/boot/truffle/TrufflePresenter.java	Wed Jul 06 17:22:52 2016 +0200
     3.2 +++ b/boot-truffle/src/main/java/net/java/html/boot/truffle/TrufflePresenter.java	Sat Jul 16 08:11:49 2016 +0200
     3.3 @@ -47,7 +47,6 @@
     3.4  import com.oracle.truffle.api.TruffleLanguage;
     3.5  import com.oracle.truffle.api.interop.TruffleObject;
     3.6  import com.oracle.truffle.api.interop.java.JavaInterop;
     3.7 -import com.oracle.truffle.api.interop.java.MethodMessage;
     3.8  import com.oracle.truffle.api.source.Source;
     3.9  import com.oracle.truffle.api.vm.PolyglotEngine;
    3.10  import java.io.Closeable;
    3.11 @@ -71,10 +70,14 @@
    3.12      private Eval eval;
    3.13      private WrapArray copy;
    3.14      private final Executor exc;
    3.15 +    private final CallTarget isNull;
    3.16 +    private final CallTarget isArray;
    3.17  
    3.18      TrufflePresenter(Executor exc, TruffleObject eval) {
    3.19          this.exc = exc;
    3.20          this.eval = eval == null ? null : JavaInterop.asJavaFunction(Eval.class, eval);
    3.21 +        this.isNull = Truffle.getRuntime().createCallTarget(new IsNullNode());
    3.22 +        this.isArray = Truffle.getRuntime().createCallTarget(new IsArrayNode());
    3.23      }
    3.24  
    3.25      @Override
    3.26 @@ -120,16 +123,6 @@
    3.27          getEval().eval(src.getCode());
    3.28      }
    3.29  
    3.30 -    interface IsArray {
    3.31 -        @MethodMessage(message = "HAS_SIZE")
    3.32 -        public boolean hasSize();
    3.33 -    }
    3.34 -
    3.35 -    interface IsNull {
    3.36 -        @MethodMessage(message = "IS_NULL")
    3.37 -        public boolean isNull();
    3.38 -    }
    3.39 -
    3.40      interface WrapArray {
    3.41          public Object copy(Object arr);
    3.42      }
    3.43 @@ -141,18 +134,14 @@
    3.44      final Object checkArray(Object val) throws Exception {
    3.45          if (val instanceof TruffleObject) {
    3.46              final TruffleObject truffleObj = (TruffleObject)val;
    3.47 -            IsArray arrayTest = JavaInterop.asJavaObject(IsArray.class, truffleObj);
    3.48 -            try {
    3.49 -                if (arrayTest.hasSize()) {
    3.50 -                    List<?> list = JavaInterop.asJavaObject(List.class, truffleObj);
    3.51 -                    Object[] arr = list.toArray();
    3.52 -                    for (int i = 0; i < arr.length; i++) {
    3.53 -                        arr[i] = toJava(arr[i]);
    3.54 -                    }
    3.55 -                    return arr;
    3.56 +            boolean hasSize = (boolean) isArray.call(truffleObj);
    3.57 +            if (hasSize) {
    3.58 +                List<?> list = JavaInterop.asJavaObject(List.class, truffleObj);
    3.59 +                Object[] arr = list.toArray();
    3.60 +                for (int i = 0; i < arr.length; i++) {
    3.61 +                    arr[i] = toJava(arr[i]);
    3.62                  }
    3.63 -            } catch (NegativeArraySizeException ex) {
    3.64 -                // swallow
    3.65 +                return arr;
    3.66              }
    3.67          }
    3.68          return val;
    3.69 @@ -164,14 +153,9 @@
    3.70              jsArray = ((JavaValue) jsArray).get();
    3.71          }
    3.72          if (jsArray instanceof TruffleObject) {
    3.73 -            IsNull checkNull = JavaInterop.asJavaFunction(IsNull.class, (TruffleObject)jsArray);
    3.74 -            try {
    3.75 -                if (checkNull.isNull()) {
    3.76 -                    return null;
    3.77 -                }
    3.78 -            } catch (NegativeArraySizeException ex) {
    3.79 -                System.err.println("negative size for " + jsArray);
    3.80 -                ex.printStackTrace();
    3.81 +            boolean checkNull = (boolean) isNull.call(jsArray);
    3.82 +            if (checkNull) {
    3.83 +                return null;
    3.84              }
    3.85          }
    3.86          try {