You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@netbeans.apache.org by jt...@apache.org on 2019/04/02 07:30:31 UTC

[incubator-netbeans-html4j] branch master updated (7b354fd -> 6f6d808)

This is an automated email from the ASF dual-hosted git repository.

jtulach pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-netbeans-html4j.git.


    from 7b354fd  Using NetBeans Parent POM version 1 seems to work now
     new 57b8616  Execute the test on all JavaScript engines
     new 6f6d808  Graal.js RC15 needs to explicitly request access to Java classes

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../net/java/html/boot/script/ScriptPresenter.java |  25 ++++-
 .../html/boot/script/Jsr223JavaScriptTest.java     | 112 ++++++++++-----------
 .../net/java/html/boot/script/ScriptsTest.java     |  47 +++++++--
 .../java/net/java/html/boot/script/SingleCase.java |   6 +-
 4 files changed, 118 insertions(+), 72 deletions(-)


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@netbeans.apache.org
For additional commands, e-mail: commits-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[incubator-netbeans-html4j] 01/02: Execute the test on all JavaScript engines

Posted by jt...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jtulach pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-netbeans-html4j.git

commit 57b861667e3de8567edab38a58ec65ad385e99bf
Author: Jaroslav Tulach <ja...@apidesign.org>
AuthorDate: Tue Apr 2 08:28:28 2019 +0200

    Execute the test on all JavaScript engines
---
 .../html/boot/script/Jsr223JavaScriptTest.java     | 112 ++++++++++-----------
 .../net/java/html/boot/script/ScriptsTest.java     |  47 +++++++--
 .../java/net/java/html/boot/script/SingleCase.java |   6 +-
 3 files changed, 98 insertions(+), 67 deletions(-)

diff --git a/boot-script/src/test/java/net/java/html/boot/script/Jsr223JavaScriptTest.java b/boot-script/src/test/java/net/java/html/boot/script/Jsr223JavaScriptTest.java
index bfa5f21..d21cbbd 100644
--- a/boot-script/src/test/java/net/java/html/boot/script/Jsr223JavaScriptTest.java
+++ b/boot-script/src/test/java/net/java/html/boot/script/Jsr223JavaScriptTest.java
@@ -22,14 +22,16 @@ import java.lang.annotation.Annotation;
 import java.lang.reflect.Method;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.Executor;
 import java.util.concurrent.Executors;
 import javax.script.ScriptEngine;
+import javax.script.ScriptEngineFactory;
 import javax.script.ScriptEngineManager;
+import javax.script.ScriptException;
 import net.java.html.boot.BrowserBuilder;
 import org.netbeans.html.boot.spi.Fn;
 import org.netbeans.html.json.tck.KOTest;
-import org.testng.Assert;
 import static org.testng.Assert.assertEquals;
 import org.testng.annotations.Factory;
 
@@ -38,57 +40,74 @@ import org.testng.annotations.Factory;
  * @author Jaroslav Tulach
  */
 public class Jsr223JavaScriptTest {
-    private static Class<?> browserClass;
-    private static Fn.Presenter browserPresenter;
-    
     public Jsr223JavaScriptTest() {
     }
 
     @Factory public static Object[] compatibilityTests() throws Exception {
-        ScriptEngine engine = new ScriptEngineManager().getEngineByName("javascript");
-        Object left = engine.eval(
-            "(function() {\n" +
-            "  var names = Object.getOwnPropertyNames(this);\n" +
-            "  for (var i = 0; i < names.length; i++) {\n" +
-            "    var n = names[i];\n" +
-            "    if (n === 'Object') continue;\n" +
-            "    if (n === 'Number') continue;\n" +
-            "    if (n === 'Boolean') continue;\n" +
-            "    if (n === 'Array') continue;\n" +
-            "    delete this[n];\n" +
-            "  }\n" +
-            "  return Object.getOwnPropertyNames(this).toString();\n" +
-            "})()\n" +
-            ""
-        );
-        assertEquals(left.toString().toLowerCase().indexOf("java"), -1, "No Java symbols " + left);
-        
+        List<Object> res = new ArrayList<>();
+        final ScriptEngineManager manager = new ScriptEngineManager();
+        for (ScriptEngineFactory f : manager.getEngineFactories()) {
+            if (!isJavaScriptEngineFactory(f)) {
+                continue;
+            }
+            collectTestsForEngine(f.getScriptEngine(), res);
+        }
+        return res.toArray();
+    }
+
+    static boolean isJavaScriptEngineFactory(ScriptEngineFactory f) {
+        if (f.getNames().contains("nashorn")) {
+            return true;
+        }
+        return f.getMimeTypes().contains("text/javascript");
+    }
+
+    private static void collectTestsForEngine(ScriptEngine engine, List<Object> res) throws Exception {
+        Fn.Presenter browserPresenter[] = { null };
+        CountDownLatch cdl = new CountDownLatch(1);
         Fn.Presenter presenter = createPresenter(engine);
         final BrowserBuilder bb = BrowserBuilder.newBrowser(presenter).
             loadPage("empty.html").
-            loadFinished(Jsr223JavaScriptTest::initialized);
+            loadFinished(() -> {
+                browserPresenter[0] = Fn.activePresenter();
+                cdl.countDown();
+            });
 
-        Executors.newSingleThreadExecutor().submit(new Runnable() {
-            @Override
-            public void run() {
-                bb.showAndWait();
-            }
-        });
+        Executors.newSingleThreadExecutor().submit(bb::showAndWait);
+        cdl.await();
 
-        List<Object> res = new ArrayList<>();
-        Class<? extends Annotation> test = 
-            loadClass().getClassLoader().loadClass(KOTest.class.getName()).
-            asSubclass(Annotation.class);
+        assertNoGlobalSymbolsLeft(engine);
+        final String prefix = "[" + engine.getFactory().getEngineName() + "] ";
 
-        Class[] arr = (Class[]) loadClass().getDeclaredMethod("tests").invoke(null);
+        Class<? extends Annotation> test = KOTest.class;
+        Class[] arr = Jsr223JavaScriptTst.tests();
         for (Class c : arr) {
             for (Method m : c.getMethods()) {
                 if (m.getAnnotation(test) != null) {
-                    res.add(new SingleCase(browserPresenter, m));
+                    res.add(new SingleCase(prefix, browserPresenter[0], m));
                 }
             }
         }
-        return res.toArray();
+
+    }
+
+    private static void assertNoGlobalSymbolsLeft(ScriptEngine engine) throws ScriptException {
+        Object left = engine.eval(
+                "(function() {\n" +
+                        "  var names = Object.getOwnPropertyNames(this);\n" +
+                        "  for (var i = 0; i < names.length; i++) {\n" +
+                        "    var n = names[i];\n" +
+                        "    if (n === 'Object') continue;\n" +
+                        "    if (n === 'Number') continue;\n" +
+                        "    if (n === 'Boolean') continue;\n" +
+                        "    if (n === 'Array') continue;\n" +
+                        "    delete this[n];\n" +
+                        "  }\n" +
+                        "  return Object.getOwnPropertyNames(this).toString();\n" +
+                        "})()\n" +
+                        ""
+        );
+        assertEquals(left.toString().toLowerCase().indexOf("java"), -1, "No Java symbols " + left);
     }
 
     private static Fn.Presenter createPresenter(ScriptEngine engine) {
@@ -101,25 +120,4 @@ public class Jsr223JavaScriptTest {
         // END: Jsr223JavaScriptTest#createPresenter
     }
 
-    static synchronized Class<?> loadClass() throws InterruptedException {
-        while (browserClass == null) {
-            Jsr223JavaScriptTest.class.wait();
-        }
-        return browserClass;
-    }
-    
-    private static synchronized void ready(Class<?> browserCls) {
-        browserClass = browserCls;
-        browserPresenter = Fn.activePresenter();
-        Jsr223JavaScriptTest.class.notifyAll();
-    }
-    
-    private static void initialized() {
-        Assert.assertSame(
-            Jsr223JavaScriptTest.class.getClassLoader(),
-            ClassLoader.getSystemClassLoader(),
-            "No special classloaders"
-        );
-        Jsr223JavaScriptTest.ready(Jsr223JavaScriptTst.class);
-    }
 }
diff --git a/boot-script/src/test/java/net/java/html/boot/script/ScriptsTest.java b/boot-script/src/test/java/net/java/html/boot/script/ScriptsTest.java
index 9760f65..9b4775a 100644
--- a/boot-script/src/test/java/net/java/html/boot/script/ScriptsTest.java
+++ b/boot-script/src/test/java/net/java/html/boot/script/ScriptsTest.java
@@ -19,17 +19,33 @@
 package net.java.html.boot.script;
 
 import java.io.Closeable;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.Executor;
-import java.util.logging.Level;
-import java.util.logging.Logger;
+import javax.script.ScriptEngine;
+import javax.script.ScriptEngineFactory;
+import javax.script.ScriptEngineManager;
 import net.java.html.boot.BrowserBuilder;
 import net.java.html.js.JavaScriptBody;
 import org.netbeans.html.boot.spi.Fn;
 import static org.testng.Assert.*;
+import org.testng.annotations.DataProvider;
 import org.testng.annotations.Test;
 
 public class ScriptsTest {
+    @DataProvider(name = "engines")
+    public static Object[][] primeNumbers() {
+        List<Object[]> res = new ArrayList<>();
+        final ScriptEngineManager manager = new ScriptEngineManager();
+        for (ScriptEngineFactory f : manager.getEngineFactories()) {
+            if (!Jsr223JavaScriptTest.isJavaScriptEngineFactory(f)) {
+                continue;
+            }
+            res.add(new Object[] { f.getScriptEngine() });
+        }
+        return res.toArray(new Object[0][]);
+    }
 
     public ScriptsTest() {
     }
@@ -92,9 +108,14 @@ public class ScriptsTest {
         assertSanitized(Scripts.newPresenter().sanitize(true));
     }
 
-    @Test
-    public void noSanitization() throws Exception {
-        assertNotSanitized(Scripts.newPresenter().sanitize(false));
+    @Test(dataProvider = "engines")
+    public void noSanitization(ScriptEngine eng) throws Exception {
+        boolean relaxed = "Graal.js".equals(eng.getFactory().getEngineName());
+
+        assertNotSanitized(
+            Scripts.newPresenter().engine(eng).sanitize(false),
+            relaxed
+        );
     }
 
     private void assertSanitized(Scripts newPresenter) throws Exception {
@@ -112,18 +133,28 @@ public class ScriptsTest {
         }
     }
 
-    private void assertNotSanitized(Scripts builder) throws Exception {
+    private void assertNotSanitized(Scripts builder, boolean relaxed) throws Exception {
         Fn.Presenter p = builder.build();
         try (Closeable c = Fn.activate(p)) {
             Object Java = p.defineFn("return typeof Java;").invoke(null);
             Object Packages = p.defineFn("return typeof Packages;").invoke(null);
             Object alert = p.defineFn("return typeof alert;").invoke(null);
-            assertEquals(Java, "object", "Java symbol found");
-            assertEquals(Packages, "object", "Packages symbol found");
+            assertObjectRelaxed(relaxed, Java, "Java symbol found");
+            assertObjectRelaxed(relaxed, Packages, "Packages symbol found");
             assertEquals(alert, "function", "alert is defined symbol");
         }
     }
 
+    private static void assertObjectRelaxed(boolean relaxed, Object real, String msg) {
+        if ("object".equals(real)) {
+            return;
+        }
+        if (relaxed && "undefined".equals(real)) {
+            return;
+        }
+        assertNull(real, msg);
+    }
+
     private static void awaitPresenter(Fn.Presenter p) {
         Executor e = (Executor) p;
         CountDownLatch cdl = new CountDownLatch(1);
diff --git a/boot-script/src/test/java/net/java/html/boot/script/SingleCase.java b/boot-script/src/test/java/net/java/html/boot/script/SingleCase.java
index 717b3a9..872673e 100644
--- a/boot-script/src/test/java/net/java/html/boot/script/SingleCase.java
+++ b/boot-script/src/test/java/net/java/html/boot/script/SingleCase.java
@@ -40,15 +40,17 @@ public final class SingleCase implements ITest, IHookable, Runnable {
     private final Method m;
     private Object result;
     private Object inst;
+    private final String prefix;
 
-    SingleCase(Fn.Presenter p, Method m) {
+    SingleCase(String prefix, Fn.Presenter p, Method m) {
+        this.prefix = prefix;
         this.p = p;
         this.m = m;
     }
 
     @Override
     public String getTestName() {
-        return m.getName();
+        return prefix + m.getName();
     }
 
     @Test


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@netbeans.apache.org
For additional commands, e-mail: commits-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[incubator-netbeans-html4j] 02/02: Graal.js RC15 needs to explicitly request access to Java classes

Posted by jt...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jtulach pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-netbeans-html4j.git

commit 6f6d808da4e502dea2ce3470507fc04afef288a7
Author: Jaroslav Tulach <ja...@apidesign.org>
AuthorDate: Tue Apr 2 09:29:57 2019 +0200

    Graal.js RC15 needs to explicitly request access to Java classes
---
 .../net/java/html/boot/script/ScriptPresenter.java | 25 +++++++++++++++++-----
 1 file changed, 20 insertions(+), 5 deletions(-)

diff --git a/boot-script/src/main/java/net/java/html/boot/script/ScriptPresenter.java b/boot-script/src/main/java/net/java/html/boot/script/ScriptPresenter.java
index dee6415..63af0e7 100644
--- a/boot-script/src/main/java/net/java/html/boot/script/ScriptPresenter.java
+++ b/boot-script/src/main/java/net/java/html/boot/script/ScriptPresenter.java
@@ -31,7 +31,9 @@ import java.util.Set;
 import java.util.concurrent.Executor;
 import java.util.logging.Level;
 import java.util.logging.Logger;
+import javax.script.Bindings;
 import javax.script.Invocable;
+import javax.script.ScriptContext;
 import javax.script.ScriptEngine;
 import javax.script.ScriptEngineManager;
 import javax.script.ScriptException;
@@ -75,18 +77,31 @@ Presenter, Fn.FromJavaScript, Fn.ToJavaScript, Executor {
         if (eng == null) {
             eng = new ScriptEngineManager().getEngineByName("javascript");
         }
+        IllegalStateException[] makingPolyglot = { null };
+        if (eng.getFactory().getNames().contains("Graal.js")) { // NOI18N
+            try {
+                Bindings bindings = eng.getBindings(ScriptContext.ENGINE_SCOPE);
+                bindings.put("polyglot.js.allowHostAccess", true); // NOI18N
+            } catch (IllegalStateException ex) {
+                makingPolyglot[0] = ex;
+            }
+        }
         this.eng = eng;
         this.exc = exc;
+        Object undef;
         try {
-            Object undef = new UndefinedCallback().undefined(eng);
-            this.undefined = undef;
+            undef = new UndefinedCallback().undefined(eng);
             if (sanitize) {
                 Sanitizer.clean(eng);
             }
             Sanitizer.defineAlert(eng);
         } catch (ScriptException ex) {
+            if (makingPolyglot[0] != null) {
+                throw makingPolyglot[0];
+            }
             throw new IllegalStateException(ex);
         }
+        this.undefined = undef;
         this.jsReady = new HashSet<>();
         this.callback = new CallbackImpl();
     }
@@ -420,14 +435,14 @@ Presenter, Fn.FromJavaScript, Fn.ToJavaScript, Executor {
             undefined = obj;
         }
 
-        public Object undefined(ScriptEngine eng) {
+        public Object undefined(ScriptEngine eng) throws ScriptException {
             undefined = this;
             try {
                 Object fn = eng.eval("(function(js) { js.callback(undefined); })");
                 Invocable inv = (Invocable) eng;
                 inv.invokeMethod(fn, "call", null, this);
-            } catch (NoSuchMethodException | ScriptException ex) {
-                throw new IllegalStateException(ex);
+            } catch (NoSuchMethodException ex) {
+                throw new ScriptException(ex);
             }
             if (undefined == this) {
                 throw new IllegalStateException();


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@netbeans.apache.org
For additional commands, e-mail: commits-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists