[prev in list] [next in list] [prev in thread] [next in thread] 

List:       htmlunit-develop
Subject:    [HtmlUnit] SVN: [11312] trunk/js/src
From:       asashour () users ! sourceforge ! net
Date:       2015-09-28 9:02:23
Message-ID: E1ZgUKJ-0001kl-6g () sfs-ml-4 ! v29 ! ch3 ! sourceforge ! com
[Download RAW message or body]

Revision: 11312
          http://sourceforge.net/p/htmlunit/code/11312
Author:   asashour
Date:     2015-09-28 09:02:23 +0000 (Mon, 28 Sep 2015)
Log Message:
-----------
[JS] addEventListener() works for EventTarget/Window (Chrome-like)

Modified Paths:
--------------
    trunk/js/src/main/java/com/gargoylesoftware/js/nashorn/internal/objects/Global.java
  trunk/js/src/main/java/com/gargoylesoftware/js/nashorn/internal/tools/nasgen/JavaMain.java


Added Paths:
-----------
    trunk/js/src/test/java/com/gargoylesoftware/js/host/MyEventTarget.java
    trunk/js/src/test/java/com/gargoylesoftware/js/host/MyWindow.java
    trunk/js/src/test/java/com/gargoylesoftware/js/host/MyWindowTest.java

Modified: trunk/js/src/main/java/com/gargoylesoftware/js/nashorn/internal/objects/Global.java
 ===================================================================
--- trunk/js/src/main/java/com/gargoylesoftware/js/nashorn/internal/objects/Global.java	2015-09-27 \
                20:54:30 UTC (rev 11311)
+++ trunk/js/src/main/java/com/gargoylesoftware/js/nashorn/internal/objects/Global.java	2015-09-28 \
09:02:23 UTC (rev 11312) @@ -2774,7 +2774,7 @@
     public Object put(final Object key, final Object value, final boolean strict) {
         if (value instanceof ScriptObject) {
             final Class<?> enclosing = value.getClass().getEnclosingClass();
-            if (ScriptObject.class.isAssignableFrom(enclosing)) {
+            if (enclosing != null && ScriptObject.class.isAssignableFrom(enclosing)) \
                {
                 prototypes.put((Class<ScriptObject>) enclosing, (ScriptObject) \
value);  }
         }

Modified: trunk/js/src/main/java/com/gargoylesoftware/js/nashorn/internal/tools/nasgen/JavaMain.java
 ===================================================================
--- trunk/js/src/main/java/com/gargoylesoftware/js/nashorn/internal/tools/nasgen/JavaMain.java	2015-09-27 \
                20:54:30 UTC (rev 11311)
+++ trunk/js/src/main/java/com/gargoylesoftware/js/nashorn/internal/tools/nasgen/JavaMain.java	2015-09-28 \
09:02:23 UTC (rev 11312) @@ -80,7 +80,7 @@
             }
         }
         else {
-            process(args[0], false);
+            process(args[0], true);
         }
 
         System.out.println("Finished");

Added: trunk/js/src/test/java/com/gargoylesoftware/js/host/MyEventTarget.java
===================================================================
--- trunk/js/src/test/java/com/gargoylesoftware/js/host/MyEventTarget.java	           \
                (rev 0)
+++ trunk/js/src/test/java/com/gargoylesoftware/js/host/MyEventTarget.java	2015-09-28 \
09:02:23 UTC (rev 11312) @@ -0,0 +1,112 @@
+/*
+ * Copyright (c) 2015 Gargoyle Software Inc.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (http://www.gnu.org/licenses/).
+ */
+package com.gargoylesoftware.js.host;
+
+import java.lang.invoke.MethodHandle;
+import java.lang.invoke.MethodHandles;
+import java.lang.invoke.MethodType;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import com.gargoylesoftware.js.nashorn.internal.objects.annotations.Browser;
+import com.gargoylesoftware.js.nashorn.internal.objects.annotations.Function;
+import com.gargoylesoftware.js.nashorn.internal.objects.annotations.ScriptClass;
+import com.gargoylesoftware.js.nashorn.internal.runtime.AccessorProperty;
+import com.gargoylesoftware.js.nashorn.internal.runtime.Context;
+import com.gargoylesoftware.js.nashorn.internal.runtime.Property;
+import com.gargoylesoftware.js.nashorn.internal.runtime.PropertyMap;
+import com.gargoylesoftware.js.nashorn.internal.runtime.PrototypeObject;
+import com.gargoylesoftware.js.nashorn.internal.runtime.ScriptFunction;
+import com.gargoylesoftware.js.nashorn.internal.runtime.ScriptObject;
+
+@ScriptClass("EventTarget")
+public class MyEventTarget extends ScriptObject {
+
+    @com.gargoylesoftware.js.nashorn.internal.objects.annotations.Constructor
+    public static MyEventTarget constructor(final boolean newObj, final Object self) \
{ +        final MyEventTarget host = new MyEventTarget();
+        host.setProto(Context.getGlobal().getPrototype(host.getClass()));
+        return host;
+    }
+
+    @Function
+    public static String addEventListener(final Object self) {
+        return Browser.getCurrent().getFamily().name();
+    }
+
+    {
+        final List<Property> list = Collections.emptyList();
+        setMap(PropertyMap.newMap(list));
+    }
+
+    private static MethodHandle staticHandle(final String name, final Class<?> \
rtype, final Class<?>... ptypes) { +        try {
+            return MethodHandles.lookup().findStatic(MyEventTarget.class,
+                    name, MethodType.methodType(rtype, ptypes));
+        }
+        catch (final ReflectiveOperationException e) {
+            throw new IllegalStateException(e);
+        }
+    }
+
+    public static final class Constructor extends ScriptFunction {
+        public Constructor() {
+            super("EventTarget", 
+                    staticHandle("constructor", MyEventTarget.class, boolean.class, \
Object.class), +                    null);
+            final Prototype prototype = new Prototype();
+            PrototypeObject.setConstructor(prototype, this);
+            setPrototype(prototype);
+        }
+    }
+
+    static final class Prototype extends PrototypeObject {
+        private ScriptFunction addEventListener;
+
+        public ScriptFunction G$addEventListener() {
+            return this.addEventListener;
+        }
+
+        public void S$addEventListener(final ScriptFunction function) {
+            this.addEventListener = function;
+        }
+
+        {
+            final List<Property> list = new ArrayList<>(1);
+            list.add(AccessorProperty.create("addEventListener", \
Property.WRITABLE_ENUMERABLE_CONFIGURABLE,  +                    \
virtualHandle("G$addEventListener", ScriptFunction.class), +                    \
virtualHandle("S$addEventListener", void.class, ScriptFunction.class))); +            \
setMap(PropertyMap.newMap(list)); +        }
+
+        Prototype() {
+            addEventListener = ScriptFunction.createBuiltin("addEventListener",
+                    staticHandle("addEventListener", String.class, Object.class));
+        }
+
+        public String getClassName() {
+            return "EventTarget";
+        }
+
+        private static MethodHandle virtualHandle(final String name, final Class<?> \
rtype, final Class<?>... ptypes) { +            try {
+                return MethodHandles.lookup().findVirtual(Prototype.class, name,
+                        MethodType.methodType(rtype, ptypes));
+            }
+            catch (final ReflectiveOperationException e) {
+                throw new IllegalStateException(e);
+            }
+        }
+    }
+}


Property changes on: \
trunk/js/src/test/java/com/gargoylesoftware/js/host/MyEventTarget.java \
                ___________________________________________________________________
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: trunk/js/src/test/java/com/gargoylesoftware/js/host/MyWindow.java
===================================================================
--- trunk/js/src/test/java/com/gargoylesoftware/js/host/MyWindow.java	                \
                (rev 0)
+++ trunk/js/src/test/java/com/gargoylesoftware/js/host/MyWindow.java	2015-09-28 \
09:02:23 UTC (rev 11312) @@ -0,0 +1,70 @@
+/*
+ * Copyright (c) 2015 Gargoyle Software Inc.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (http://www.gnu.org/licenses/).
+ */
+package com.gargoylesoftware.js.host;
+
+import java.lang.invoke.MethodHandle;
+import java.lang.invoke.MethodHandles;
+import java.lang.invoke.MethodType;
+import java.util.Collections;
+import java.util.List;
+
+import com.gargoylesoftware.js.nashorn.internal.objects.annotations.ScriptClass;
+import com.gargoylesoftware.js.nashorn.internal.runtime.Context;
+import com.gargoylesoftware.js.nashorn.internal.runtime.Property;
+import com.gargoylesoftware.js.nashorn.internal.runtime.PropertyMap;
+import com.gargoylesoftware.js.nashorn.internal.runtime.PrototypeObject;
+import com.gargoylesoftware.js.nashorn.internal.runtime.ScriptFunction;
+import com.gargoylesoftware.js.nashorn.internal.runtime.ScriptObject;
+
+@ScriptClass("Window")
+public class MyWindow extends ScriptObject {
+
+    @com.gargoylesoftware.js.nashorn.internal.objects.annotations.Constructor
+    public static MyWindow constructor(final boolean newObj, final Object self) {
+        final MyWindow host = new MyWindow();
+        host.setProto(Context.getGlobal().getPrototype(host.getClass()));
+        return host;
+    }
+
+    {
+        final List<Property> list = Collections.emptyList();
+        setMap(PropertyMap.newMap(list));
+    }
+
+    private static MethodHandle staticHandle(final String name, final Class<?> \
rtype, final Class<?>... ptypes) { +        try {
+            return MethodHandles.lookup().findStatic(MyWindow.class,
+                    name, MethodType.methodType(rtype, ptypes));
+        }
+        catch (final ReflectiveOperationException e) {
+            throw new IllegalStateException(e);
+        }
+    }
+
+    public static final class Constructor extends ScriptFunction {
+        public Constructor() {
+            super("Window", 
+                    staticHandle("constructor", MyWindow.class, boolean.class, \
Object.class), +                    null);
+            final Prototype prototype = new Prototype();
+            PrototypeObject.setConstructor(prototype, this);
+            setPrototype(prototype);
+        }
+    }
+
+    static final class Prototype extends PrototypeObject {
+        public String getClassName() {
+            return "Window";
+        }
+    }
+}


Property changes on: \
trunk/js/src/test/java/com/gargoylesoftware/js/host/MyWindow.java \
                ___________________________________________________________________
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: trunk/js/src/test/java/com/gargoylesoftware/js/host/MyWindowTest.java
===================================================================
--- trunk/js/src/test/java/com/gargoylesoftware/js/host/MyWindowTest.java	            \
                (rev 0)
+++ trunk/js/src/test/java/com/gargoylesoftware/js/host/MyWindowTest.java	2015-09-28 \
09:02:23 UTC (rev 11312) @@ -0,0 +1,83 @@
+/*
+ * Copyright (c) 2015 Gargoyle Software Inc.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (http://www.gnu.org/licenses/).
+ */
+package com.gargoylesoftware.js.host;
+
+import static org.junit.Assert.assertEquals;
+
+import javax.script.ScriptContext;
+import javax.script.ScriptEngine;
+import javax.script.ScriptException;
+import javax.script.SimpleScriptContext;
+
+import org.junit.Test;
+
+import com.gargoylesoftware.js.nashorn.api.scripting.NashornScriptEngineFactory;
+import com.gargoylesoftware.js.nashorn.api.scripting.ScriptObjectMirror;
+import com.gargoylesoftware.js.nashorn.internal.objects.Global;
+import com.gargoylesoftware.js.nashorn.internal.objects.annotations.Browser;
+import com.gargoylesoftware.js.nashorn.internal.objects.annotations.BrowserFamily;
+import com.gargoylesoftware.js.nashorn.internal.runtime.Context;
+import com.gargoylesoftware.js.nashorn.internal.runtime.PrototypeObject;
+import com.gargoylesoftware.js.nashorn.internal.runtime.ScriptFunction;
+
+public class MyWindowTest {
+
+    @Test
+    public void addEventListener() throws ScriptException {
+        final Browser chrome = new Browser(BrowserFamily.CHROME, 45);
+        test("[object Object]", "window", chrome);
+        test("function Window() { [native code] }", "Window", chrome);
+        test("function addEventListener() { [native code] }", \
"window.addEventListener", chrome); +//        final Browser ie11 = new \
Browser(BrowserFamily.IE, 11); +//        test("[object Object]", "window", ie11);
+//        test("[object Object]", "Window", ie11);
+//        test("function addEventListener() { [native code] }", \
"window.addEventListener", ie11); +    }
+
+    private void test(final String expected, final String script, final Browser \
browser) throws ScriptException { +        final ScriptEngine engine = new \
NashornScriptEngineFactory().getScriptEngine(); +        initGlobal(engine, browser);
+        final Object object = engine.eval(script);
+        assertEquals(expected, object == null ? "null" : object.toString());
+    }
+
+    private void initGlobal(final ScriptEngine engine, final Browser browser) {
+        Browser.setCurrent(browser);
+        final SimpleScriptContext context = (SimpleScriptContext) \
engine.getContext(); +        final Global global = (Global) ((ScriptObjectMirror) \
context.getBindings(ScriptContext.ENGINE_SCOPE)).getScriptObject(); +        final \
Global oldGlobal = Context.getGlobal(); +        try {
+            Context.setGlobal(global);
+            global.put("EventTarget", new MyEventTarget.Constructor(), true);
+            global.put("Window", new MyWindow.Constructor(), true);
+            setProto(global, "Window", "EventTarget");
+
+            final MyWindow window = new MyWindow();
+            window.setProto(Context.getGlobal().getPrototype(window.getClass()));
+            global.put("window", window, true);
+        }
+        finally {
+            Context.setGlobal(oldGlobal);
+        }
+    }
+
+    private void setProto(final Global global, final String childName, final String \
parentName) { +        final ScriptFunction childFunction = (ScriptFunction) \
global.get(childName); +        final PrototypeObject childPrototype = \
(PrototypeObject) childFunction.getPrototype(); +        final ScriptFunction \
parentFunction = (ScriptFunction) global.get(parentName); +        final \
PrototypeObject parentPrototype = (PrototypeObject) parentFunction.getPrototype(); +  \
childPrototype.setProto(parentPrototype); +        \
childFunction.setProto(parentFunction); +    }
+
+}


Property changes on: \
trunk/js/src/test/java/com/gargoylesoftware/js/host/MyWindowTest.java \
                ___________________________________________________________________
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property

------------------------------------------------------------------------------
_______________________________________________
HtmlUnit-develop mailing list
HtmlUnit-develop@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/htmlunit-develop


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic