8148924: Inconsistent "this" context in JSAdapter adaptee function calls
authorhannesw
Fri, 28 Oct 2016 16:52:20 +0200
changeset 41841 7e66ae6baf2d
parent 41840 ed63ad1bb2cc
child 41842 50202a344d28
8148924: Inconsistent "this" context in JSAdapter adaptee function calls Reviewed-by: jlaskey
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeJSAdapter.java
nashorn/test/script/basic/jsadapter-ids.js
nashorn/test/script/basic/jsadapter.js
nashorn/test/script/basic/jsadapter.js.EXPECTED
nashorn/test/script/basic/jsadapterlink.js
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeJSAdapter.java	Thu Oct 27 16:29:02 2016 +0000
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeJSAdapter.java	Fri Oct 28 16:52:20 2016 +0200
@@ -365,7 +365,7 @@
 
         Object obj;
         if (func instanceof ScriptFunction) {
-            obj = ScriptRuntime.apply((ScriptFunction)func, adaptee);
+            obj = ScriptRuntime.apply((ScriptFunction)func, this);
         } else {
             obj = new NativeArray(0);
         }
@@ -544,7 +544,7 @@
     private Object callAdaptee(final Object retValue, final String name, final Object... args) {
         final Object func = adaptee.get(name);
         if (func instanceof ScriptFunction) {
-            return ScriptRuntime.apply((ScriptFunction)func, adaptee, args);
+            return ScriptRuntime.apply((ScriptFunction)func, this, args);
         }
         return retValue;
     }
--- a/nashorn/test/script/basic/jsadapter-ids.js	Thu Oct 27 16:29:02 2016 +0000
+++ b/nashorn/test/script/basic/jsadapter-ids.js	Fri Oct 28 16:52:20 2016 +0200
@@ -30,6 +30,7 @@
 
 var obj = new JSAdapter() {
     __getIds__: function() {
+        Assert.assertTrue(this === obj);
         print("__getIds__ called");
         return [ "foo", "bar" ];
     }
--- a/nashorn/test/script/basic/jsadapter.js	Thu Oct 27 16:29:02 2016 +0000
+++ b/nashorn/test/script/basic/jsadapter.js	Fri Oct 28 16:52:20 2016 +0200
@@ -30,39 +30,81 @@
 
 var obj = new JSAdapter() {
     __get__: function(name) {
+        Assert.assertTrue(this === obj);
         print("getter called for '" + name + "'"); return name;
     },
 
     __put__: function(name, value) {
+        Assert.assertTrue(this === obj);
         print("setter called for '" + name + "' with " + value);
     },
 
     __call__: function(name, arg1, arg2) {
+        Assert.assertTrue(this === obj);
         print("method '" + name + "' called with " + arg1 + ", " + arg2);
     },
 
     __new__: function(arg1, arg2) {
+        Assert.assertTrue(this === obj);
         print("new with " + arg1 + ", " + arg2);
     },
 
     __getKeys__: function() {
+        Assert.assertTrue(this === obj);
         print("__getKeys__ called");
         return [ "foo", "bar" ];
     },
 
     __getValues__: function() {
+        Assert.assertTrue(this === obj);
         print("__getValues__ called");
         return [ "fooval", "barval" ];
     },
 
     __has__: function(name) {
+        Assert.assertTrue(this === obj);
         print("__has__ called with '" + name + "'");
         return name == "js";
     },
 
     __delete__: function(name) {
+        Assert.assertTrue(this === obj);
         print("__delete__ called with '" + name + "'");
         return true;
+    },
+
+    __preventExtensions__ : function() {
+        Assert.assertTrue(this === obj);
+        print("__preventExtensions__ called");
+    },
+
+    __freeze__ : function() {
+        Assert.assertTrue(this === obj);
+        print("__freeze__ called");
+
+    },
+
+    __isFrozen__ : function() {
+        Assert.assertTrue(this === obj);
+        print("__isFrozen__ called");
+        return false;
+    },
+
+    __seal__ : function() {
+        Assert.assertTrue(this === obj);
+        print("__seal__ called");
+    },
+
+    __isSealed__ : function() {
+        Assert.assertTrue(this === obj);
+        print("__isSealed__ called");
+        return false;
+    },
+
+    __isExtensible__ : function() {
+        Assert.assertTrue(this === obj);
+        print("__isExtensible__ called");
+        return true;
     }
 };
 
@@ -103,3 +145,13 @@
 print(obj["js"]);
 obj["js"] = "javascript";
 print(obj["javascript"]);
+
+// call __isExtensible__, __isSealed__, __isFrozen__
+print(Object.isExtensible(obj));
+print(Object.isSealed(obj));
+print(Object.isFrozen(obj));
+
+// call __freeze__, __seal__, __preventExtensions__
+Object.freeze(obj);
+Object.seal(obj);
+Object.preventExtensions(obj);
--- a/nashorn/test/script/basic/jsadapter.js.EXPECTED	Thu Oct 27 16:29:02 2016 +0000
+++ b/nashorn/test/script/basic/jsadapter.js.EXPECTED	Fri Oct 28 16:52:20 2016 +0200
@@ -20,3 +20,12 @@
 setter called for 'js' with javascript
 getter called for 'javascript'
 javascript
+__isExtensible__ called
+true
+__isSealed__ called
+false
+__isFrozen__ called
+false
+__freeze__ called
+__seal__ called
+__preventExtensions__ called
--- a/nashorn/test/script/basic/jsadapterlink.js	Thu Oct 27 16:29:02 2016 +0000
+++ b/nashorn/test/script/basic/jsadapterlink.js	Fri Oct 28 16:52:20 2016 +0200
@@ -31,18 +31,21 @@
 
 var js1 = new JSAdapter() {
     __get__: function(name) {
+        Assert.assertTrue(this === js1);
         return "js1->" + name;
     }
 };
 
 var js2 = new JSAdapter() {
     __get__: function(name) {
+        Assert.assertTrue(this === js2);
         return "js2->" + name;
     }
 };
 
 var js3 = new JSAdapter() {
     __get__: function(name) {
+        Assert.assertTrue(this === js3);
         return "js3->" + name;
     }
 };