Merging boot-truffle into main development branch
authorJaroslav Tulach <jtulach@netbeans.org>
Thu, 21 Jul 2016 10:26:00 +0200
changeset 1103d8f27be84171
parent 1100 031e46d048d8
parent 1102 86f56d464357
child 1104 fbb044757eea
Merging boot-truffle into main development branch
boot-truffle/pom.xml
pom.xml
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/boot-truffle/pom.xml	Thu Jul 21 10:26:00 2016 +0200
     1.3 @@ -0,0 +1,139 @@
     1.4 +<?xml version="1.0" encoding="UTF-8"?>
     1.5 +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     1.6 +    <modelVersion>4.0.0</modelVersion>
     1.7 +    <parent>
     1.8 +        <groupId>org.netbeans.html</groupId>
     1.9 +        <artifactId>pom</artifactId>
    1.10 +        <version>2.0-SNAPSHOT</version>
    1.11 +    </parent>
    1.12 +    <name>Presenter via Truffle</name>
    1.13 +    <artifactId>net.java.html.boot.truffle</artifactId>
    1.14 +    <version>2.0-SNAPSHOT</version>
    1.15 +    <packaging>bundle</packaging>
    1.16 +    <properties>
    1.17 +        <netbeans.compile.on.save>NONE</netbeans.compile.on.save>
    1.18 +        <publicPackages>net.java.html.boot.truffle</publicPackages>
    1.19 +        <trufflejs>${java.home}/language/js/trufflejs.jar</trufflejs>
    1.20 +    </properties>
    1.21 +    <build>
    1.22 +      <plugins>
    1.23 +          <plugin>
    1.24 +              <groupId>org.apache.felix</groupId>
    1.25 +              <artifactId>maven-bundle-plugin</artifactId>
    1.26 +              <extensions>true</extensions>
    1.27 +              <configuration>
    1.28 +                  <instructions>
    1.29 +                      <Export-Package>${publicPackages}</Export-Package>
    1.30 +                      <Bundle-SymbolicName>net.java.html.boot.truffle</Bundle-SymbolicName>
    1.31 +                  </instructions>
    1.32 +              </configuration>
    1.33 +          </plugin>
    1.34 +          <plugin>
    1.35 +              <groupId>org.netbeans.html</groupId>
    1.36 +              <artifactId>html4j-maven-plugin</artifactId>
    1.37 +          </plugin>
    1.38 +         <plugin>
    1.39 +            <groupId>org.apache.maven.plugins</groupId>
    1.40 +            <artifactId>maven-compiler-plugin</artifactId>
    1.41 +            <version>2.3.2</version>
    1.42 +            <configuration>
    1.43 +               <source>1.7</source>
    1.44 +               <target>1.7</target>
    1.45 +            </configuration>
    1.46 +         </plugin>
    1.47 +      </plugins>
    1.48 +    </build>
    1.49 +    <dependencies>
    1.50 +        <dependency>
    1.51 +            <groupId>org.netbeans.api</groupId>
    1.52 +            <artifactId>org-openide-util-lookup</artifactId>
    1.53 +            <scope>provided</scope>
    1.54 +        </dependency>
    1.55 +        <dependency>
    1.56 +            <groupId>com.oracle.truffle</groupId>
    1.57 +            <artifactId>truffle-api</artifactId>
    1.58 +            <version>0.15</version>
    1.59 +        </dependency>
    1.60 +        <dependency>
    1.61 +            <groupId>com.oracle.truffle</groupId>
    1.62 +            <artifactId>truffle-dsl-processor</artifactId>
    1.63 +            <version>0.15</version>
    1.64 +            <scope>provided</scope>
    1.65 +        </dependency>
    1.66 +        <dependency>
    1.67 +            <groupId>org.netbeans.html</groupId>
    1.68 +            <artifactId>net.java.html.boot</artifactId>
    1.69 +            <version>${project.version}</version>
    1.70 +            <type>jar</type>
    1.71 +        </dependency>
    1.72 +        <dependency>
    1.73 +            <groupId>org.testng</groupId>
    1.74 +            <artifactId>testng</artifactId>
    1.75 +            <scope>test</scope>
    1.76 +        </dependency>
    1.77 +        <dependency>
    1.78 +            <groupId>${project.groupId}</groupId>
    1.79 +            <artifactId>net.java.html.json.tck</artifactId>
    1.80 +            <version>${project.version}</version>
    1.81 +            <scope>test</scope>
    1.82 +        </dependency>
    1.83 +        <dependency>
    1.84 +            <groupId>org.glassfish.grizzly</groupId>
    1.85 +            <artifactId>grizzly-http-server</artifactId>
    1.86 +            <version>${grizzly.version}</version>
    1.87 +            <scope>test</scope>
    1.88 +        </dependency>
    1.89 +        <dependency>
    1.90 +            <groupId>org.glassfish.grizzly</groupId>
    1.91 +            <artifactId>grizzly-websockets-server</artifactId>
    1.92 +            <version>${grizzly.version}</version>
    1.93 +            <scope>test</scope>
    1.94 +            <type>jar</type>
    1.95 +        </dependency>
    1.96 +        <dependency>
    1.97 +            <groupId>org.glassfish.grizzly</groupId>
    1.98 +            <artifactId>grizzly-http-servlet</artifactId>
    1.99 +            <version>${grizzly.version}</version>
   1.100 +            <scope>test</scope>
   1.101 +        </dependency>    
   1.102 +        <dependency>
   1.103 +            <groupId>javax.servlet</groupId>
   1.104 +            <artifactId>javax.servlet-api</artifactId>
   1.105 +            <scope>test</scope>
   1.106 +            <version>3.1.0</version>
   1.107 +        </dependency>
   1.108 +        <dependency>
   1.109 +            <groupId>org.netbeans.html</groupId>
   1.110 +            <artifactId>ko4j</artifactId>
   1.111 +            <version>${project.version}</version>
   1.112 +            <scope>test</scope>
   1.113 +            <type>jar</type>
   1.114 +        </dependency>
   1.115 +        <dependency>
   1.116 +            <groupId>${project.groupId}</groupId>
   1.117 +            <artifactId>ko-ws-tyrus</artifactId>
   1.118 +            <version>${project.version}</version>
   1.119 +            <scope>test</scope>
   1.120 +            <type>jar</type>
   1.121 +        </dependency>
   1.122 +    </dependencies>
   1.123 +    <profiles>
   1.124 +        <profile>
   1.125 +            <id>graalvm</id>
   1.126 +            <activation>
   1.127 +                <file>
   1.128 +                    <exists>${trufflejs}</exists>
   1.129 +                </file>
   1.130 +            </activation>
   1.131 +            <dependencies>
   1.132 +                <dependency>
   1.133 +                    <groupId>com.oracle.graaljs</groupId>
   1.134 +                    <artifactId>truffle-js</artifactId>
   1.135 +                    <version>0.07</version>
   1.136 +                    <systemPath>${trufflejs}</systemPath>
   1.137 +                    <scope>system</scope>
   1.138 +                </dependency>
   1.139 +            </dependencies>
   1.140 +        </profile>
   1.141 +    </profiles>
   1.142 +</project>
     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/FnRootNode.java	Thu Jul 21 10:26:00 2016 +0200
     2.3 @@ -0,0 +1,80 @@
     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.InteropException;
    2.52 +import com.oracle.truffle.api.interop.Message;
    2.53 +import com.oracle.truffle.api.interop.TruffleObject;
    2.54 +import com.oracle.truffle.api.nodes.Node;
    2.55 +import com.oracle.truffle.api.nodes.RootNode;
    2.56 +
    2.57 +final class FnRootNode extends RootNode {
    2.58 +    @Child
    2.59 +    private Node execute;
    2.60 +    private final TruffleObject fn;
    2.61 +
    2.62 +    FnRootNode(TruffleObject fn, int arity) {
    2.63 +        super(TruffleLanguage.class, null, null);
    2.64 +        // one more argument for this
    2.65 +        this.execute = Message.createExecute(arity + 1).createNode();
    2.66 +        this.fn = fn;
    2.67 +    }
    2.68 +
    2.69 +    @Override
    2.70 +    public Object execute(VirtualFrame frame) {
    2.71 +        try {
    2.72 +            final Object[] args = frame.getArguments();
    2.73 +            Object result = ForeignAccess.sendExecute(execute, frame, fn, args);
    2.74 +            return result;
    2.75 +        } catch (InteropException ex) {
    2.76 +            throw raise(RuntimeException.class, ex);
    2.77 +        }
    2.78 +    }
    2.79 +
    2.80 +    private static <E extends Throwable> E raise(Class<E> castTo, Throwable t) throws E {
    2.81 +        throw (E)t;
    2.82 +    }
    2.83 +}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/boot-truffle/src/main/java/net/java/html/boot/truffle/IsArrayNode.java	Thu Jul 21 10:26:00 2016 +0200
     3.3 @@ -0,0 +1,68 @@
     3.4 +/**
     3.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3.6 + *
     3.7 + * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
     3.8 + *
     3.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    3.10 + * Other names may be trademarks of their respective owners.
    3.11 + *
    3.12 + * The contents of this file are subject to the terms of either the GNU
    3.13 + * General Public License Version 2 only ("GPL") or the Common
    3.14 + * Development and Distribution License("CDDL") (collectively, the
    3.15 + * "License"). You may not use this file except in compliance with the
    3.16 + * License. You can obtain a copy of the License at
    3.17 + * http://www.netbeans.org/cddl-gplv2.html
    3.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    3.19 + * specific language governing permissions and limitations under the
    3.20 + * License.  When distributing the software, include this License Header
    3.21 + * Notice in each file and include the License file at
    3.22 + * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    3.23 + * particular file as subject to the "Classpath" exception as provided
    3.24 + * by Oracle in the GPL Version 2 section of the License file that
    3.25 + * accompanied this code. If applicable, add the following below the
    3.26 + * License Header, with the fields enclosed by brackets [] replaced by
    3.27 + * your own identifying information:
    3.28 + * "Portions Copyrighted [year] [name of copyright owner]"
    3.29 + *
    3.30 + * Contributor(s):
    3.31 + *
    3.32 + * The Original Software is NetBeans. The Initial Developer of the Original
    3.33 + * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
    3.34 + *
    3.35 + * If you wish your version of this file to be governed by only the CDDL
    3.36 + * or only the GPL Version 2, indicate your decision by adding
    3.37 + * "[Contributor] elects to include this software in this distribution
    3.38 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
    3.39 + * single choice of license, a recipient has the option to distribute
    3.40 + * your version of this file under either the CDDL, the GPL Version 2 or
    3.41 + * to extend the choice of license to its licensees as provided above.
    3.42 + * However, if you add GPL Version 2 code and therefore, elected the GPL
    3.43 + * Version 2 license, then the option applies only if the new code is
    3.44 + * made subject to such option by the copyright holder.
    3.45 + */
    3.46 +package net.java.html.boot.truffle;
    3.47 +
    3.48 +import com.oracle.truffle.api.TruffleLanguage;
    3.49 +import com.oracle.truffle.api.frame.VirtualFrame;
    3.50 +import com.oracle.truffle.api.interop.ForeignAccess;
    3.51 +import com.oracle.truffle.api.interop.Message;
    3.52 +import com.oracle.truffle.api.interop.TruffleObject;
    3.53 +import com.oracle.truffle.api.nodes.Node;
    3.54 +import com.oracle.truffle.api.nodes.RootNode;
    3.55 +
    3.56 +final class IsArrayNode extends RootNode {
    3.57 +    @Child
    3.58 +    private Node check;
    3.59 +
    3.60 +    IsArrayNode() {
    3.61 +        super(TruffleLanguage.class, null, null);
    3.62 +        this.check = Message.HAS_SIZE.createNode();
    3.63 +    }
    3.64 +
    3.65 +    @Override
    3.66 +    public Object execute(VirtualFrame frame) {
    3.67 +        final Object[] args = frame.getArguments();
    3.68 +        Object result = ForeignAccess.sendHasSize(check, frame, (TruffleObject) args[0]);
    3.69 +        return result;
    3.70 +    }
    3.71 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/boot-truffle/src/main/java/net/java/html/boot/truffle/IsNullNode.java	Thu Jul 21 10:26:00 2016 +0200
     4.3 @@ -0,0 +1,68 @@
     4.4 +/**
     4.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     4.6 + *
     4.7 + * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
     4.8 + *
     4.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    4.10 + * Other names may be trademarks of their respective owners.
    4.11 + *
    4.12 + * The contents of this file are subject to the terms of either the GNU
    4.13 + * General Public License Version 2 only ("GPL") or the Common
    4.14 + * Development and Distribution License("CDDL") (collectively, the
    4.15 + * "License"). You may not use this file except in compliance with the
    4.16 + * License. You can obtain a copy of the License at
    4.17 + * http://www.netbeans.org/cddl-gplv2.html
    4.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    4.19 + * specific language governing permissions and limitations under the
    4.20 + * License.  When distributing the software, include this License Header
    4.21 + * Notice in each file and include the License file at
    4.22 + * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    4.23 + * particular file as subject to the "Classpath" exception as provided
    4.24 + * by Oracle in the GPL Version 2 section of the License file that
    4.25 + * accompanied this code. If applicable, add the following below the
    4.26 + * License Header, with the fields enclosed by brackets [] replaced by
    4.27 + * your own identifying information:
    4.28 + * "Portions Copyrighted [year] [name of copyright owner]"
    4.29 + *
    4.30 + * Contributor(s):
    4.31 + *
    4.32 + * The Original Software is NetBeans. The Initial Developer of the Original
    4.33 + * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
    4.34 + *
    4.35 + * If you wish your version of this file to be governed by only the CDDL
    4.36 + * or only the GPL Version 2, indicate your decision by adding
    4.37 + * "[Contributor] elects to include this software in this distribution
    4.38 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
    4.39 + * single choice of license, a recipient has the option to distribute
    4.40 + * your version of this file under either the CDDL, the GPL Version 2 or
    4.41 + * to extend the choice of license to its licensees as provided above.
    4.42 + * However, if you add GPL Version 2 code and therefore, elected the GPL
    4.43 + * Version 2 license, then the option applies only if the new code is
    4.44 + * made subject to such option by the copyright holder.
    4.45 + */
    4.46 +package net.java.html.boot.truffle;
    4.47 +
    4.48 +import com.oracle.truffle.api.TruffleLanguage;
    4.49 +import com.oracle.truffle.api.frame.VirtualFrame;
    4.50 +import com.oracle.truffle.api.interop.ForeignAccess;
    4.51 +import com.oracle.truffle.api.interop.Message;
    4.52 +import com.oracle.truffle.api.interop.TruffleObject;
    4.53 +import com.oracle.truffle.api.nodes.Node;
    4.54 +import com.oracle.truffle.api.nodes.RootNode;
    4.55 +
    4.56 +final class IsNullNode extends RootNode {
    4.57 +    @Child
    4.58 +    private Node check;
    4.59 +
    4.60 +    IsNullNode() {
    4.61 +        super(TruffleLanguage.class, null, null);
    4.62 +        this.check = Message.IS_NULL.createNode();
    4.63 +    }
    4.64 +
    4.65 +    @Override
    4.66 +    public Object execute(VirtualFrame frame) {
    4.67 +        final Object[] args = frame.getArguments();
    4.68 +        Object result = ForeignAccess.sendIsNull(check, frame, (TruffleObject) args[0]);
    4.69 +        return result;
    4.70 +    }
    4.71 +}
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/boot-truffle/src/main/java/net/java/html/boot/truffle/JavaArray.java	Thu Jul 21 10:26:00 2016 +0200
     5.3 @@ -0,0 +1,102 @@
     5.4 +/**
     5.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     5.6 + *
     5.7 + * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
     5.8 + *
     5.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    5.10 + * Other names may be trademarks of their respective owners.
    5.11 + *
    5.12 + * The contents of this file are subject to the terms of either the GNU
    5.13 + * General Public License Version 2 only ("GPL") or the Common
    5.14 + * Development and Distribution License("CDDL") (collectively, the
    5.15 + * "License"). You may not use this file except in compliance with the
    5.16 + * License. You can obtain a copy of the License at
    5.17 + * http://www.netbeans.org/cddl-gplv2.html
    5.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    5.19 + * specific language governing permissions and limitations under the
    5.20 + * License.  When distributing the software, include this License Header
    5.21 + * Notice in each file and include the License file at
    5.22 + * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    5.23 + * particular file as subject to the "Classpath" exception as provided
    5.24 + * by Oracle in the GPL Version 2 section of the License file that
    5.25 + * accompanied this code. If applicable, add the following below the
    5.26 + * License Header, with the fields enclosed by brackets [] replaced by
    5.27 + * your own identifying information:
    5.28 + * "Portions Copyrighted [year] [name of copyright owner]"
    5.29 + *
    5.30 + * Contributor(s):
    5.31 + *
    5.32 + * The Original Software is NetBeans. The Initial Developer of the Original
    5.33 + * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
    5.34 + *
    5.35 + * If you wish your version of this file to be governed by only the CDDL
    5.36 + * or only the GPL Version 2, indicate your decision by adding
    5.37 + * "[Contributor] elects to include this software in this distribution
    5.38 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
    5.39 + * single choice of license, a recipient has the option to distribute
    5.40 + * your version of this file under either the CDDL, the GPL Version 2 or
    5.41 + * to extend the choice of license to its licensees as provided above.
    5.42 + * However, if you add GPL Version 2 code and therefore, elected the GPL
    5.43 + * Version 2 license, then the option applies only if the new code is
    5.44 + * made subject to such option by the copyright holder.
    5.45 + */
    5.46 +package net.java.html.boot.truffle;
    5.47 +
    5.48 +import com.oracle.truffle.api.interop.ForeignAccess;
    5.49 +import com.oracle.truffle.api.interop.MessageResolution;
    5.50 +import com.oracle.truffle.api.interop.Resolve;
    5.51 +import com.oracle.truffle.api.interop.TruffleObject;
    5.52 +import com.oracle.truffle.api.nodes.Node;
    5.53 +import java.lang.reflect.Array;
    5.54 +
    5.55 +@MessageResolution(receiverType = JavaArray.class, language = TrufflePresenter.JavaLang.class)
    5.56 +final class JavaArray extends JavaValue implements TruffleObject {
    5.57 +    final TrufflePresenter.WrapArray wrap;
    5.58 +    final Object arr;
    5.59 +
    5.60 +    public JavaArray(TrufflePresenter.WrapArray wrap, Object arr) {
    5.61 +        this.arr = arr;
    5.62 +        this.wrap = wrap;
    5.63 +    }
    5.64 +
    5.65 +    @Override
    5.66 +    public ForeignAccess getForeignAccess() {
    5.67 +        return JavaArrayForeign.ACCESS;
    5.68 +    }
    5.69 +
    5.70 +    @Override
    5.71 +    public Object get() {
    5.72 +        return arr;
    5.73 +    }
    5.74 +
    5.75 +    static boolean isInstance(TruffleObject obj) {
    5.76 +        return obj instanceof JavaArray;
    5.77 +    }
    5.78 +
    5.79 +    static boolean isArray(Object obj) {
    5.80 +        return obj != null && obj.getClass().getComponentType() != null;
    5.81 +    }
    5.82 +
    5.83 +    @Resolve(message = "READ")
    5.84 +    static abstract class ReadNode extends Node {
    5.85 +        protected Object access(JavaArray arr, int index) {
    5.86 +            Object obj = Array.get(arr.arr, index);
    5.87 +            return toJavaScript(obj, arr.wrap);
    5.88 +        }
    5.89 +    }
    5.90 +
    5.91 +    @Resolve(message = "HAS_SIZE")
    5.92 +    static abstract class HasSizeNode extends Node {
    5.93 +        protected boolean access(JavaArray arr) {
    5.94 +            return true;
    5.95 +        }
    5.96 +    }
    5.97 +
    5.98 +    @Resolve(message = "GET_SIZE")
    5.99 +    static abstract class GetSizeNode extends Node {
   5.100 +        protected int access(JavaArray arr) {
   5.101 +            return Array.getLength(arr.arr);
   5.102 +        }
   5.103 +    }
   5.104 +
   5.105 +}
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/boot-truffle/src/main/java/net/java/html/boot/truffle/JavaObject.java	Thu Jul 21 10:26:00 2016 +0200
     6.3 @@ -0,0 +1,82 @@
     6.4 +/**
     6.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     6.6 + *
     6.7 + * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
     6.8 + *
     6.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    6.10 + * Other names may be trademarks of their respective owners.
    6.11 + *
    6.12 + * The contents of this file are subject to the terms of either the GNU
    6.13 + * General Public License Version 2 only ("GPL") or the Common
    6.14 + * Development and Distribution License("CDDL") (collectively, the
    6.15 + * "License"). You may not use this file except in compliance with the
    6.16 + * License. You can obtain a copy of the License at
    6.17 + * http://www.netbeans.org/cddl-gplv2.html
    6.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    6.19 + * specific language governing permissions and limitations under the
    6.20 + * License.  When distributing the software, include this License Header
    6.21 + * Notice in each file and include the License file at
    6.22 + * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    6.23 + * particular file as subject to the "Classpath" exception as provided
    6.24 + * by Oracle in the GPL Version 2 section of the License file that
    6.25 + * accompanied this code. If applicable, add the following below the
    6.26 + * License Header, with the fields enclosed by brackets [] replaced by
    6.27 + * your own identifying information:
    6.28 + * "Portions Copyrighted [year] [name of copyright owner]"
    6.29 + *
    6.30 + * Contributor(s):
    6.31 + *
    6.32 + * The Original Software is NetBeans. The Initial Developer of the Original
    6.33 + * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
    6.34 + *
    6.35 + * If you wish your version of this file to be governed by only the CDDL
    6.36 + * or only the GPL Version 2, indicate your decision by adding
    6.37 + * "[Contributor] elects to include this software in this distribution
    6.38 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
    6.39 + * single choice of license, a recipient has the option to distribute
    6.40 + * your version of this file under either the CDDL, the GPL Version 2 or
    6.41 + * to extend the choice of license to its licensees as provided above.
    6.42 + * However, if you add GPL Version 2 code and therefore, elected the GPL
    6.43 + * Version 2 license, then the option applies only if the new code is
    6.44 + * made subject to such option by the copyright holder.
    6.45 + */
    6.46 +package net.java.html.boot.truffle;
    6.47 +
    6.48 +import com.oracle.truffle.api.interop.ForeignAccess;
    6.49 +import com.oracle.truffle.api.interop.MessageResolution;
    6.50 +import com.oracle.truffle.api.interop.Resolve;
    6.51 +import com.oracle.truffle.api.interop.TruffleObject;
    6.52 +import com.oracle.truffle.api.nodes.Node;
    6.53 +
    6.54 +@MessageResolution(receiverType = JavaObject.class, language = TrufflePresenter.JavaLang.class)
    6.55 +final class JavaObject extends JavaValue implements TruffleObject {
    6.56 +    final Object obj;
    6.57 +
    6.58 +    JavaObject(Object obj) {
    6.59 +        this.obj = obj;
    6.60 +    }
    6.61 +
    6.62 +
    6.63 +    @Override
    6.64 +    public ForeignAccess getForeignAccess() {
    6.65 +        return JavaObjectForeign.ACCESS;
    6.66 +    }
    6.67 +
    6.68 +    public static boolean isInstance(TruffleObject obj) {
    6.69 +        return obj instanceof JavaObject;
    6.70 +    }
    6.71 +
    6.72 +    @Override
    6.73 +    public Object get() {
    6.74 +        return obj;
    6.75 +    }
    6.76 +
    6.77 +    @Resolve(message = "HAS_SIZE")
    6.78 +    static abstract class NoSizeNode extends Node {
    6.79 +
    6.80 +        protected boolean access(JavaObject obj) {
    6.81 +            return false;
    6.82 +        }
    6.83 +    }
    6.84 +
    6.85 +}
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/boot-truffle/src/main/java/net/java/html/boot/truffle/JavaValue.java	Thu Jul 21 10:26:00 2016 +0200
     7.3 @@ -0,0 +1,91 @@
     7.4 +/**
     7.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     7.6 + *
     7.7 + * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
     7.8 + *
     7.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    7.10 + * Other names may be trademarks of their respective owners.
    7.11 + *
    7.12 + * The contents of this file are subject to the terms of either the GNU
    7.13 + * General Public License Version 2 only ("GPL") or the Common
    7.14 + * Development and Distribution License("CDDL") (collectively, the
    7.15 + * "License"). You may not use this file except in compliance with the
    7.16 + * License. You can obtain a copy of the License at
    7.17 + * http://www.netbeans.org/cddl-gplv2.html
    7.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    7.19 + * specific language governing permissions and limitations under the
    7.20 + * License.  When distributing the software, include this License Header
    7.21 + * Notice in each file and include the License file at
    7.22 + * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    7.23 + * particular file as subject to the "Classpath" exception as provided
    7.24 + * by Oracle in the GPL Version 2 section of the License file that
    7.25 + * accompanied this code. If applicable, add the following below the
    7.26 + * License Header, with the fields enclosed by brackets [] replaced by
    7.27 + * your own identifying information:
    7.28 + * "Portions Copyrighted [year] [name of copyright owner]"
    7.29 + *
    7.30 + * Contributor(s):
    7.31 + *
    7.32 + * The Original Software is NetBeans. The Initial Developer of the Original
    7.33 + * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
    7.34 + *
    7.35 + * If you wish your version of this file to be governed by only the CDDL
    7.36 + * or only the GPL Version 2, indicate your decision by adding
    7.37 + * "[Contributor] elects to include this software in this distribution
    7.38 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
    7.39 + * single choice of license, a recipient has the option to distribute
    7.40 + * your version of this file under either the CDDL, the GPL Version 2 or
    7.41 + * to extend the choice of license to its licensees as provided above.
    7.42 + * However, if you add GPL Version 2 code and therefore, elected the GPL
    7.43 + * Version 2 license, then the option applies only if the new code is
    7.44 + * made subject to such option by the copyright holder.
    7.45 + */
    7.46 +package net.java.html.boot.truffle;
    7.47 +
    7.48 +import com.oracle.truffle.api.interop.TruffleObject;
    7.49 +import com.oracle.truffle.api.interop.java.JavaInterop;
    7.50 +
    7.51 +abstract class JavaValue {
    7.52 +    public abstract Object get();
    7.53 +
    7.54 +    static Object toJavaScript(Object conv, TrufflePresenter.WrapArray wrap) {
    7.55 +        if (conv instanceof TruffleObject) {
    7.56 +            return conv;
    7.57 +        }
    7.58 +        if (JavaArray.isArray(conv)) {
    7.59 +            conv = wrap.copy(new JavaArray(wrap, conv));
    7.60 +        }
    7.61 +        if (conv instanceof Character) {
    7.62 +            conv = (int) (Character) conv;
    7.63 +        }
    7.64 +        if (conv == null || conv.getClass().getSimpleName().equals("$JsCallbacks$")) { // NOI18N
    7.65 +            conv = JavaInterop.asTruffleObject(conv);
    7.66 +        } else if (!isJSReady(conv)) {
    7.67 +            conv = new JavaObject(conv);
    7.68 +        }
    7.69 +        return conv;
    7.70 +    }
    7.71 +
    7.72 +    private static boolean isJSReady(Object obj) {
    7.73 +        if (obj == null) {
    7.74 +            return true;
    7.75 +        }
    7.76 +        if (obj instanceof String) {
    7.77 +            return true;
    7.78 +        }
    7.79 +        if (obj instanceof Number) {
    7.80 +            return true;
    7.81 +        }
    7.82 +        if (obj instanceof Character) {
    7.83 +            return true;
    7.84 +        }
    7.85 +        if (obj instanceof Boolean) {
    7.86 +            return true;
    7.87 +        }
    7.88 +        if (obj instanceof TruffleObject) {
    7.89 +            return true;
    7.90 +        }
    7.91 +        return false;
    7.92 +    }
    7.93 +
    7.94 +}
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/boot-truffle/src/main/java/net/java/html/boot/truffle/TrufflePresenter.java	Thu Jul 21 10:26:00 2016 +0200
     8.3 @@ -0,0 +1,262 @@
     8.4 +/**
     8.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     8.6 + *
     8.7 + * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
     8.8 + *
     8.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    8.10 + * Other names may be trademarks of their respective owners.
    8.11 + *
    8.12 + * The contents of this file are subject to the terms of either the GNU
    8.13 + * General Public License Version 2 only ("GPL") or the Common
    8.14 + * Development and Distribution License("CDDL") (collectively, the
    8.15 + * "License"). You may not use this file except in compliance with the
    8.16 + * License. You can obtain a copy of the License at
    8.17 + * http://www.netbeans.org/cddl-gplv2.html
    8.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    8.19 + * specific language governing permissions and limitations under the
    8.20 + * License.  When distributing the software, include this License Header
    8.21 + * Notice in each file and include the License file at
    8.22 + * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    8.23 + * particular file as subject to the "Classpath" exception as provided
    8.24 + * by Oracle in the GPL Version 2 section of the License file that
    8.25 + * accompanied this code. If applicable, add the following below the
    8.26 + * License Header, with the fields enclosed by brackets [] replaced by
    8.27 + * your own identifying information:
    8.28 + * "Portions Copyrighted [year] [name of copyright owner]"
    8.29 + *
    8.30 + * Contributor(s):
    8.31 + *
    8.32 + * The Original Software is NetBeans. The Initial Developer of the Original
    8.33 + * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
    8.34 + *
    8.35 + * If you wish your version of this file to be governed by only the CDDL
    8.36 + * or only the GPL Version 2, indicate your decision by adding
    8.37 + * "[Contributor] elects to include this software in this distribution
    8.38 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
    8.39 + * single choice of license, a recipient has the option to distribute
    8.40 + * your version of this file under either the CDDL, the GPL Version 2 or
    8.41 + * to extend the choice of license to its licensees as provided above.
    8.42 + * However, if you add GPL Version 2 code and therefore, elected the GPL
    8.43 + * Version 2 license, then the option applies only if the new code is
    8.44 + * made subject to such option by the copyright holder.
    8.45 + */
    8.46 +package net.java.html.boot.truffle;
    8.47 +
    8.48 +import com.oracle.truffle.api.CallTarget;
    8.49 +import com.oracle.truffle.api.Truffle;
    8.50 +import com.oracle.truffle.api.TruffleLanguage;
    8.51 +import com.oracle.truffle.api.interop.TruffleObject;
    8.52 +import com.oracle.truffle.api.interop.java.JavaInterop;
    8.53 +import com.oracle.truffle.api.source.Source;
    8.54 +import com.oracle.truffle.api.vm.PolyglotEngine;
    8.55 +import java.io.Closeable;
    8.56 +import java.io.IOException;
    8.57 +import java.io.Reader;
    8.58 +import java.net.URL;
    8.59 +import java.util.ArrayList;
    8.60 +import java.util.List;
    8.61 +import java.util.concurrent.Executor;
    8.62 +import org.netbeans.html.boot.spi.Fn;
    8.63 +import org.netbeans.html.boot.spi.Fn.Presenter;
    8.64 +
    8.65 +/**
    8.66 + * Implementation of {@link Presenter} that delegates to Truffle.
    8.67 + *
    8.68 + * @author Jaroslav Tulach
    8.69 + */
    8.70 +final class TrufflePresenter implements Fn.KeepAlive,
    8.71 +    Presenter, Fn.FromJavaScript, Fn.ToJavaScript, Executor {
    8.72 +
    8.73 +    private Eval eval;
    8.74 +    private WrapArray copy;
    8.75 +    private final Executor exc;
    8.76 +    private final CallTarget isNull;
    8.77 +    private final CallTarget isArray;
    8.78 +
    8.79 +    TrufflePresenter(Executor exc, TruffleObject eval) {
    8.80 +        this.exc = exc;
    8.81 +        this.eval = eval == null ? null : JavaInterop.asJavaFunction(Eval.class, eval);
    8.82 +        this.isNull = Truffle.getRuntime().createCallTarget(new IsNullNode());
    8.83 +        this.isArray = Truffle.getRuntime().createCallTarget(new IsArrayNode());
    8.84 +    }
    8.85 +
    8.86 +    @Override
    8.87 +    public Fn defineFn(String code, String... names) {
    8.88 +        return defineImpl(code, names, null);
    8.89 +    }
    8.90 +
    8.91 +    @Override
    8.92 +    public Fn defineFn(String code, String[] names, boolean[] keepAlive) {
    8.93 +        return defineImpl(code, names, keepAlive);
    8.94 +    }
    8.95 +
    8.96 +    private FnImpl defineImpl(String code, String[] names, boolean[] keepAlive) {
    8.97 +        StringBuilder sb = new StringBuilder();
    8.98 +        sb.append("(function() {\n");
    8.99 +        sb.append("  return function(");
   8.100 +        String sep = "";
   8.101 +        for (String n : names) {
   8.102 +            sb.append(sep).append(n);
   8.103 +            sep = ",";
   8.104 +        }
   8.105 +        sb.append(") {\n");
   8.106 +        sb.append(code);
   8.107 +        sb.append("\n  };\n");
   8.108 +        sb.append("})()\n");
   8.109 +
   8.110 +        TruffleObject fn = (TruffleObject) getEval().eval(sb.toString());
   8.111 +        return new FnImpl(this, fn, names.length);
   8.112 +    }
   8.113 +
   8.114 +    @Override
   8.115 +    public void displayPage(URL page, Runnable onPageLoad) {
   8.116 +        if (onPageLoad != null) {
   8.117 +            onPageLoad.run();
   8.118 +        }
   8.119 +    }
   8.120 +
   8.121 +    @Override
   8.122 +    public void loadScript(Reader code) throws Exception {
   8.123 +        Source src = Source.newBuilder(code).
   8.124 +            name("unknown.js").
   8.125 +            mimeType("text/javascript").
   8.126 +            build();
   8.127 +        getEval().eval(src.getCode());
   8.128 +    }
   8.129 +
   8.130 +    interface WrapArray {
   8.131 +        public Object copy(Object arr);
   8.132 +    }
   8.133 +
   8.134 +    interface Eval {
   8.135 +        public Object eval(String code);
   8.136 +    }
   8.137 +
   8.138 +    final Object checkArray(Object val) throws Exception {
   8.139 +        if (val instanceof TruffleObject) {
   8.140 +            final TruffleObject truffleObj = (TruffleObject)val;
   8.141 +            boolean hasSize = (boolean) isArray.call(truffleObj);
   8.142 +            if (hasSize) {
   8.143 +                List<?> list = JavaInterop.asJavaObject(List.class, truffleObj);
   8.144 +                Object[] arr = list.toArray();
   8.145 +                for (int i = 0; i < arr.length; i++) {
   8.146 +                    arr[i] = toJava(arr[i]);
   8.147 +                }
   8.148 +                return arr;
   8.149 +            }
   8.150 +        }
   8.151 +        return val;
   8.152 +    }
   8.153 +
   8.154 +    @Override
   8.155 +    public Object toJava(Object jsArray) {
   8.156 +        if (jsArray instanceof JavaValue) {
   8.157 +            jsArray = ((JavaValue) jsArray).get();
   8.158 +        }
   8.159 +        if (jsArray instanceof TruffleObject) {
   8.160 +            boolean checkNull = (boolean) isNull.call(jsArray);
   8.161 +            if (checkNull) {
   8.162 +                return null;
   8.163 +            }
   8.164 +        }
   8.165 +        try {
   8.166 +            return checkArray(jsArray);
   8.167 +        } catch (Exception ex) {
   8.168 +            throw new IllegalStateException(ex);
   8.169 +        }
   8.170 +    }
   8.171 +
   8.172 +    @Override
   8.173 +    public Object toJavaScript(Object conv) {
   8.174 +        return JavaValue.toJavaScript(conv, getWrap());
   8.175 +    }
   8.176 +
   8.177 +    @Override
   8.178 +    public void execute(final Runnable command) {
   8.179 +        if (Fn.activePresenter() == this) {
   8.180 +            command.run();
   8.181 +            return;
   8.182 +        }
   8.183 +
   8.184 +        class Wrap implements Runnable {
   8.185 +
   8.186 +            @Override
   8.187 +            public void run() {
   8.188 +                try (Closeable c = Fn.activate(TrufflePresenter.this)) {
   8.189 +                    command.run();
   8.190 +                } catch (IOException ex) {
   8.191 +                    throw new IllegalStateException(ex);
   8.192 +                }
   8.193 +            }
   8.194 +        }
   8.195 +        final Runnable wrap = new Wrap();
   8.196 +        if (exc == null) {
   8.197 +            wrap.run();
   8.198 +        } else {
   8.199 +            exc.execute(wrap);
   8.200 +        }
   8.201 +    }
   8.202 +
   8.203 +    private Eval getEval() {
   8.204 +        if (eval == null) {
   8.205 +            try {
   8.206 +                PolyglotEngine engine = PolyglotEngine.newBuilder().build();
   8.207 +                TruffleObject fn = (TruffleObject) engine.eval(
   8.208 +                    Source.newBuilder("eval.bind(this)").
   8.209 +                        mimeType("text/javascript").
   8.210 +                        name("eval.js").build()
   8.211 +                ).get();
   8.212 +                eval = JavaInterop.asJavaFunction(Eval.class, fn);
   8.213 +            } catch (IOException ex) {
   8.214 +                throw new IllegalStateException(ex);
   8.215 +            }
   8.216 +        }
   8.217 +        return eval;
   8.218 +    }
   8.219 +
   8.220 +    private WrapArray getWrap() {
   8.221 +        if (copy == null) {
   8.222 +            TruffleObject fn = (TruffleObject) getEval().eval("(function(arr) {\n"
   8.223 +                + "  var n = [];\n"
   8.224 +                + "  for (var i = 0; i < arr.length; i++) {\n"
   8.225 +                + "    n[i] = arr[i];\n"
   8.226 +                + "  }\n"
   8.227 +                + "  return n;\n"
   8.228 +                + "}).bind(this)"
   8.229 +            );
   8.230 +            copy = JavaInterop.asJavaFunction(WrapArray.class, fn);
   8.231 +        }
   8.232 +        return copy;
   8.233 +    }
   8.234 +
   8.235 +    private class FnImpl extends Fn {
   8.236 +
   8.237 +        private final CallTarget fn;
   8.238 +
   8.239 +        public FnImpl(Presenter presenter, TruffleObject fn, int arity) {
   8.240 +            super(presenter);
   8.241 +            this.fn = Truffle.getRuntime().createCallTarget(new FnRootNode(fn, arity));
   8.242 +        }
   8.243 +
   8.244 +        @Override
   8.245 +        public Object invoke(Object thiz, Object... args) throws Exception {
   8.246 +            List<Object> all = new ArrayList<>(args.length + 1);
   8.247 +            all.add(thiz == null ? fn : toJavaScript(thiz));
   8.248 +            for (Object conv : args) {
   8.249 +                conv = toJavaScript(conv);
   8.250 +                all.add(conv);
   8.251 +            }
   8.252 +            Object ret = fn.call(all.toArray());
   8.253 +            if (ret instanceof JavaValue) {
   8.254 +                ret = ((JavaValue)ret).get();
   8.255 +            }
   8.256 +            if (ret == fn) {
   8.257 +                return null;
   8.258 +            }
   8.259 +            return toJava(ret);
   8.260 +        }
   8.261 +    }
   8.262 +
   8.263 +    static abstract class JavaLang extends TruffleLanguage<Object> {
   8.264 +    }
   8.265 +}
     9.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.2 +++ b/boot-truffle/src/main/java/net/java/html/boot/truffle/package.html	Thu Jul 21 10:26:00 2016 +0200
     9.3 @@ -0,0 +1,51 @@
     9.4 +<!--
     9.5 +
     9.6 +    DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     9.7 +
     9.8 +    Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
     9.9 +
    9.10 +    Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    9.11 +    Other names may be trademarks of their respective owners.
    9.12 +
    9.13 +    The contents of this file are subject to the terms of either the GNU
    9.14 +    General Public License Version 2 only ("GPL") or the Common
    9.15 +    Development and Distribution License("CDDL") (collectively, the
    9.16 +    "License"). You may not use this file except in compliance with the
    9.17 +    License. You can obtain a copy of the License at
    9.18 +    http://www.netbeans.org/cddl-gplv2.html
    9.19 +    or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    9.20 +    specific language governing permissions and limitations under the
    9.21 +    License.  When distributing the software, include this License Header
    9.22 +    Notice in each file and include the License file at
    9.23 +    nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    9.24 +    particular file as subject to the "Classpath" exception as provided
    9.25 +    by Oracle in the GPL Version 2 section of the License file that
    9.26 +    accompanied this code. If applicable, add the following below the
    9.27 +    License Header, with the fields enclosed by brackets [] replaced by
    9.28 +    your own identifying information:
    9.29 +    "Portions Copyrighted [year] [name of copyright owner]"
    9.30 +
    9.31 +    Contributor(s):
    9.32 +
    9.33 +    The Original Software is NetBeans. The Initial Developer of the Original
    9.34 +    Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
    9.35 +
    9.36 +    If you wish your version of this file to be governed by only the CDDL
    9.37 +    or only the GPL Version 2, indicate your decision by adding
    9.38 +    "[Contributor] elects to include this software in this distribution
    9.39 +    under the [CDDL or GPL Version 2] license." If you do not indicate a
    9.40 +    single choice of license, a recipient has the option to distribute
    9.41 +    your version of this file under either the CDDL, the GPL Version 2 or
    9.42 +    to extend the choice of license to its licensees as provided above.
    9.43 +    However, if you add GPL Version 2 code and therefore, elected the GPL
    9.44 +    Version 2 license, then the option applies only if the new code is
    9.45 +    made subject to such option by the copyright holder.
    9.46 +
    9.47 +-->
    9.48 +<body>
    9.49 +    <p>
    9.50 +        Integration with <a href="http://github.com/graalvm/truffle">Truffle</a>
    9.51 +        useful to execute against <b>node.js</b> running on top of
    9.52 +        the <a href="http://www.oracle.com/technetwork/oracle-labs/program-languages/overview/">GraalVM</a>.
    9.53 +    </p>
    9.54 +</body>
    10.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    10.2 +++ b/boot-truffle/src/test/java/net/java/html/boot/truffle/SingleCase.java	Thu Jul 21 10:26:00 2016 +0200
    10.3 @@ -0,0 +1,127 @@
    10.4 +/**
    10.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
    10.6 + *
    10.7 + * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
    10.8 + *
    10.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
   10.10 + * Other names may be trademarks of their respective owners.
   10.11 + *
   10.12 + * The contents of this file are subject to the terms of either the GNU
   10.13 + * General Public License Version 2 only ("GPL") or the Common
   10.14 + * Development and Distribution License("CDDL") (collectively, the
   10.15 + * "License"). You may not use this file except in compliance with the
   10.16 + * License. You can obtain a copy of the License at
   10.17 + * http://www.netbeans.org/cddl-gplv2.html
   10.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
   10.19 + * specific language governing permissions and limitations under the
   10.20 + * License.  When distributing the software, include this License Header
   10.21 + * Notice in each file and include the License file at
   10.22 + * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
   10.23 + * particular file as subject to the "Classpath" exception as provided
   10.24 + * by Oracle in the GPL Version 2 section of the License file that
   10.25 + * accompanied this code. If applicable, add the following below the
   10.26 + * License Header, with the fields enclosed by brackets [] replaced by
   10.27 + * your own identifying information:
   10.28 + * "Portions Copyrighted [year] [name of copyright owner]"
   10.29 + *
   10.30 + * Contributor(s):
   10.31 + *
   10.32 + * The Original Software is NetBeans. The Initial Developer of the Original
   10.33 + * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
   10.34 + *
   10.35 + * If you wish your version of this file to be governed by only the CDDL
   10.36 + * or only the GPL Version 2, indicate your decision by adding
   10.37 + * "[Contributor] elects to include this software in this distribution
   10.38 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
   10.39 + * single choice of license, a recipient has the option to distribute
   10.40 + * your version of this file under either the CDDL, the GPL Version 2 or
   10.41 + * to extend the choice of license to its licensees as provided above.
   10.42 + * However, if you add GPL Version 2 code and therefore, elected the GPL
   10.43 + * Version 2 license, then the option applies only if the new code is
   10.44 + * made subject to such option by the copyright holder.
   10.45 + */
   10.46 +package net.java.html.boot.truffle;
   10.47 +
   10.48 +import java.lang.reflect.InvocationTargetException;
   10.49 +import java.lang.reflect.Method;
   10.50 +import java.util.concurrent.Executor;
   10.51 +import java.util.concurrent.Executors;
   10.52 +import org.netbeans.html.boot.spi.Fn;
   10.53 +import org.netbeans.html.boot.impl.FnContext;
   10.54 +import org.testng.IHookCallBack;
   10.55 +import org.testng.IHookable;
   10.56 +import org.testng.ITest;
   10.57 +import org.testng.ITestResult;
   10.58 +import org.testng.annotations.Test;
   10.59 +
   10.60 +/**
   10.61 + *
   10.62 + * @author Jaroslav Tulach
   10.63 + */
   10.64 +public final class SingleCase implements ITest, IHookable, Runnable {
   10.65 +    static final Executor JS = Executors.newSingleThreadExecutor();
   10.66 +    private final Fn.Presenter p;
   10.67 +    private final Method m;
   10.68 +    private Object result;
   10.69 +    private Object inst;
   10.70 +
   10.71 +    SingleCase(Fn.Presenter p, Method m) {
   10.72 +        this.p = p;
   10.73 +        this.m = m;
   10.74 +    }
   10.75 +
   10.76 +    @Override
   10.77 +    public String getTestName() {
   10.78 +        return m.getName();
   10.79 +    }
   10.80 +
   10.81 +    @Test
   10.82 +    public synchronized void executeTest() throws Exception {
   10.83 +        if (result == null) {
   10.84 +            JS.execute(this);
   10.85 +            wait();
   10.86 +        }
   10.87 +        if (result instanceof Exception) {
   10.88 +            throw (Exception)result;
   10.89 +        }
   10.90 +        if (result instanceof Error) {
   10.91 +            throw (Error)result;
   10.92 +        }
   10.93 +    }
   10.94 +
   10.95 +    @Override
   10.96 +    public synchronized void run() {
   10.97 +        boolean notify = true;
   10.98 +        try {
   10.99 +            FnContext.currentPresenter(p);
  10.100 +            if (inst == null) {
  10.101 +                inst = m.getDeclaringClass().newInstance();
  10.102 +            }
  10.103 +            result = m.invoke(inst);
  10.104 +            if (result == null) {
  10.105 +                result = this;
  10.106 +            }
  10.107 +        } catch (InvocationTargetException ex) {
  10.108 +            Throwable r = ex.getTargetException();
  10.109 +            if (r instanceof InterruptedException) {
  10.110 +                notify = false;
  10.111 +                JS.execute(this);
  10.112 +                return;
  10.113 +            }
  10.114 +            result = r;
  10.115 +        } catch (Exception ex) {
  10.116 +            result = ex;
  10.117 +        } finally {
  10.118 +            if (notify) {
  10.119 +                notifyAll();
  10.120 +            }
  10.121 +            FnContext.currentPresenter(null);
  10.122 +        }
  10.123 +    }
  10.124 +
  10.125 +    @Override
  10.126 +    public void run(IHookCallBack ihcb, ITestResult itr) {
  10.127 +        ihcb.runTestMethod(itr);
  10.128 +    }
  10.129 +    
  10.130 +}
    11.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    11.2 +++ b/boot-truffle/src/test/java/net/java/html/boot/truffle/TruffleJavaScriptTest.java	Thu Jul 21 10:26:00 2016 +0200
    11.3 @@ -0,0 +1,156 @@
    11.4 +/**
    11.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
    11.6 + *
    11.7 + * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
    11.8 + *
    11.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
   11.10 + * Other names may be trademarks of their respective owners.
   11.11 + *
   11.12 + * The contents of this file are subject to the terms of either the GNU
   11.13 + * General Public License Version 2 only ("GPL") or the Common
   11.14 + * Development and Distribution License("CDDL") (collectively, the
   11.15 + * "License"). You may not use this file except in compliance with the
   11.16 + * License. You can obtain a copy of the License at
   11.17 + * http://www.netbeans.org/cddl-gplv2.html
   11.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
   11.19 + * specific language governing permissions and limitations under the
   11.20 + * License.  When distributing the software, include this License Header
   11.21 + * Notice in each file and include the License file at
   11.22 + * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
   11.23 + * particular file as subject to the "Classpath" exception as provided
   11.24 + * by Oracle in the GPL Version 2 section of the License file that
   11.25 + * accompanied this code. If applicable, add the following below the
   11.26 + * License Header, with the fields enclosed by brackets [] replaced by
   11.27 + * your own identifying information:
   11.28 + * "Portions Copyrighted [year] [name of copyright owner]"
   11.29 + *
   11.30 + * Contributor(s):
   11.31 + *
   11.32 + * The Original Software is NetBeans. The Initial Developer of the Original
   11.33 + * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
   11.34 + *
   11.35 + * If you wish your version of this file to be governed by only the CDDL
   11.36 + * or only the GPL Version 2, indicate your decision by adding
   11.37 + * "[Contributor] elects to include this software in this distribution
   11.38 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
   11.39 + * single choice of license, a recipient has the option to distribute
   11.40 + * your version of this file under either the CDDL, the GPL Version 2 or
   11.41 + * to extend the choice of license to its licensees as provided above.
   11.42 + * However, if you add GPL Version 2 code and therefore, elected the GPL
   11.43 + * Version 2 license, then the option applies only if the new code is
   11.44 + * made subject to such option by the copyright holder.
   11.45 + */
   11.46 +package net.java.html.boot.truffle;
   11.47 +
   11.48 +import com.oracle.truffle.api.source.Source;
   11.49 +import com.oracle.truffle.api.vm.PolyglotEngine;
   11.50 +import java.lang.annotation.Annotation;
   11.51 +import java.lang.reflect.Method;
   11.52 +import java.util.ArrayList;
   11.53 +import java.util.List;
   11.54 +import java.util.concurrent.Executors;
   11.55 +import net.java.html.boot.BrowserBuilder;
   11.56 +import org.netbeans.html.boot.spi.Fn;
   11.57 +import org.netbeans.html.json.tck.JavaScriptTCK;
   11.58 +import org.netbeans.html.json.tck.KOTest;
   11.59 +import org.testng.Assert;
   11.60 +import static org.testng.Assert.assertEquals;
   11.61 +import org.testng.SkipException;
   11.62 +import org.testng.annotations.Factory;
   11.63 +import org.testng.annotations.Test;
   11.64 +
   11.65 +/**
   11.66 + *
   11.67 + * @author Jaroslav Tulach
   11.68 + */
   11.69 +public class TruffleJavaScriptTest {
   11.70 +    private static Class<?> browserClass;
   11.71 +    private static Fn.Presenter browserPresenter;
   11.72 +    
   11.73 +    public TruffleJavaScriptTest() {
   11.74 +    }
   11.75 +
   11.76 +    @Factory public static Object[] compatibilityTests() throws Exception {
   11.77 +        PolyglotEngine engine = PolyglotEngine.newBuilder().build();
   11.78 +        PolyglotEngine.Value result = null;
   11.79 +        try {
   11.80 +            result = engine.eval(Source.fromText("6 * 7", "test.js").withMimeType("text/javascript"));
   11.81 +        } catch (Exception notSupported) {
   11.82 +            if (notSupported.getMessage().contains("text/javascript")) {
   11.83 +                return new Object[] { new SkipTest(notSupported.getMessage()) };
   11.84 +            }
   11.85 +        }
   11.86 +        assertEquals(42, result.as(Number.class).intValue(), "Executed OK");
   11.87 +
   11.88 +        final BrowserBuilder bb = BrowserBuilder.newBrowser(new TrufflePresenter(SingleCase.JS, null)).
   11.89 +            loadClass(TruffleJavaScriptTest.class).
   11.90 +            loadPage("empty.html").
   11.91 +            invoke("initialized");
   11.92 +
   11.93 +        Executors.newSingleThreadExecutor().submit(new Runnable() {
   11.94 +            @Override
   11.95 +            public void run() {
   11.96 +                bb.showAndWait();
   11.97 +            }
   11.98 +        });
   11.99 +
  11.100 +        List<Object> res = new ArrayList<>();
  11.101 +        Class<? extends Annotation> test = 
  11.102 +            loadClass().getClassLoader().loadClass(KOTest.class.getName()).
  11.103 +            asSubclass(Annotation.class);
  11.104 +
  11.105 +        Class[] arr = (Class[]) loadClass().getDeclaredMethod("tests").invoke(null);
  11.106 +        for (Class c : arr) {
  11.107 +            if (c.getSimpleName().contains("GC")) {
  11.108 +                continue;
  11.109 +            }
  11.110 +            for (Method m : c.getMethods()) {
  11.111 +                if (m.getAnnotation(test) != null) {
  11.112 +                    res.add(new SingleCase(browserPresenter, m));
  11.113 +                }
  11.114 +            }
  11.115 +        }
  11.116 +        return res.toArray();
  11.117 +    }
  11.118 +
  11.119 +    static synchronized Class<?> loadClass() throws InterruptedException {
  11.120 +        while (browserClass == null) {
  11.121 +            TruffleJavaScriptTest.class.wait();
  11.122 +        }
  11.123 +        return browserClass;
  11.124 +    }
  11.125 +    
  11.126 +    public static synchronized void ready(Class<?> browserCls) throws Exception {
  11.127 +        browserClass = browserCls;
  11.128 +        browserPresenter = Fn.activePresenter();
  11.129 +        TruffleJavaScriptTest.class.notifyAll();
  11.130 +    }
  11.131 +    
  11.132 +    public static void initialized() throws Exception {
  11.133 +        Assert.assertSame(TruffleJavaScriptTest.class.getClassLoader(),
  11.134 +            ClassLoader.getSystemClassLoader(),
  11.135 +            "No special classloaders"
  11.136 +        );
  11.137 +        TruffleJavaScriptTest.ready(Tck.class);
  11.138 +    }
  11.139 +
  11.140 +    public static final class Tck extends JavaScriptTCK {
  11.141 +
  11.142 +        public static Class[] tests() {
  11.143 +            return testClasses();
  11.144 +        }
  11.145 +    }
  11.146 +
  11.147 +    public static class SkipTest {
  11.148 +        private final String message;
  11.149 +
  11.150 +        public SkipTest(String message) {
  11.151 +            this.message = message;
  11.152 +        }
  11.153 +
  11.154 +        @Test
  11.155 +        public void needsGraalVMToExecuteTheTests() {
  11.156 +            throw new SkipException(message);
  11.157 +        }
  11.158 +    }
  11.159 +}
    12.1 --- a/pom.xml	Fri Jul 15 19:12:50 2016 +0200
    12.2 +++ b/pom.xml	Thu Jul 21 10:26:00 2016 +0200
    12.3 @@ -35,6 +35,7 @@
    12.4      <module>ko-osgi-test</module>
    12.5      <module>equinox-agentclass-hook</module>
    12.6      <module>boot-script</module>
    12.7 +    <module>boot-truffle</module>
    12.8      <module>boot-agent-test</module>
    12.9      <module>xhr4j</module>
   12.10    </modules>