@@ -206,24 +206,75 @@ public void testJSBuiltinCanBeOverwritten() {
206206
207207 @ Test
208208 public void testForeignInstanceof () {
209- testInstanceofIntl ("Array" , ProxyArray .fromArray ("fun" , "with" , "proxy" , "array" ));
210- testPrototypeIntl ("Date" , Instant .now ());
211- testPrototypeIntl ("Map" , new TestTruffleHash ());
212- testPrototypeIntl ("String" , new TestTruffleString ());
213- testPrototypeIntl ("Boolean" , new TestTruffleBoolean ());
214- testPrototypeIntl ("Number" , new TestTruffleNumber ());
215- testPrototypeIntl ("Function" , (ProxyExecutable ) v -> true );
216- testPrototypeIntl ("Object" , new Object ());
209+ // test expected Instance
210+ Assert .assertTrue (testInstanceofIntl ("Array" , ProxyArray .fromArray ("fun" , "with" , "proxy" , "array" )));
211+ // Assert.assertTrue(testInstanceofIntl("Date", Instant.now())); //see GR-39319
212+ Assert .assertTrue (testInstanceofIntl ("Map" , new TestTruffleHash ()));
213+ Assert .assertTrue (testInstanceofIntl ("String" , new TestTruffleString ()));
214+ Assert .assertTrue (testInstanceofIntl ("Boolean" , new TestTruffleBoolean ()));
215+ Assert .assertTrue (testInstanceofIntl ("Number" , new TestTruffleNumber ()));
216+ Assert .assertTrue (testInstanceofIntl ("Function" , (ProxyExecutable ) v -> true ));
217+ Assert .assertTrue (testInstanceofIntl ("Object" , new Object ()));
218+
219+ // test non-matching instance
220+ Assert .assertFalse (testInstanceofIntl ("RegExp" , ProxyArray .fromArray ("fun" , "with" , "proxy" , "array" )));
221+ Assert .assertFalse (testInstanceofIntl ("RegExp" , Instant .now ()));
222+ Assert .assertFalse (testInstanceofIntl ("RegExp" , new TestTruffleHash ()));
223+ Assert .assertFalse (testInstanceofIntl ("RegExp" , new TestTruffleString ()));
224+ Assert .assertFalse (testInstanceofIntl ("RegExp" , new TestTruffleBoolean ()));
225+ Assert .assertFalse (testInstanceofIntl ("RegExp" , new TestTruffleNumber ()));
226+ Assert .assertFalse (testInstanceofIntl ("RegExp" , (ProxyExecutable ) v -> true ));
227+ Assert .assertFalse (testInstanceofIntl ("RegExp" , new Object ()));
217228 }
218229
219- private static void testInstanceofIntl (String prototype , Object obj ) {
220- String code = "(obj) => { return (obj instanceof " + prototype + "); }" ;
230+ private static boolean testInstanceofIntl (String prototype , Object obj ) {
231+ String code = "(obj) => { return (obj instanceof " + prototype + ") && (obj instanceof Object) ; }" ;
221232 try (Context context = JSTest .newContextBuilder (ID ).build ()) {
222233 Value result = context .eval (ID , code ).execute (obj );
223- Assert . assertTrue ( result .asBoolean () );
234+ return result .asBoolean ();
224235 }
225236 }
226237
238+ @ Test
239+ public void testForeignRightPrototype () {
240+ String code = "ForeignObjectPrototype = Object.getPrototypeOf(new java.lang.Object());\n " +
241+ "function f() {}; \n " +
242+ "f.prototype = ForeignObjectPrototype;\n " +
243+ "new java.lang.Object() instanceof f;" ;
244+ testTrue (code );
245+ }
246+
247+ private static void testTrue (String code ) {
248+ Assert .assertTrue (testIntl (code ));
249+ }
250+
251+ private static void testFalse (String code ) {
252+ Assert .assertFalse (testIntl (code ));
253+ }
254+
255+ private static boolean testIntl (String code ) {
256+ try (Context context = JSTest .newContextBuilder (ID ).allowAllAccess (true ).allowHostAccess (HostAccess .ALL ).build ()) {
257+ Value result = context .eval (ID , code );
258+ return result .asBoolean ();
259+ }
260+ }
261+
262+ @ Test
263+ public void testCallableProxies () {
264+ String code = "new java.lang.Object instanceof new Proxy(Object, {});" ;
265+ testTrue (code );
266+
267+ code = "var handler = { get(target, prop, recv) { return (prop === 'prototype') ? Object.prototype : Reflect.get(target, prop, recv); } };\n " +
268+ "var proxy = new Proxy(function() {}, handler);\n " +
269+ "new java.lang.Object() instanceof proxy" ;
270+ testTrue (code );
271+
272+ code = "var handler = { get(target, prop, recv) { if (prop === 'prototype') { throw new Error() } else { return Reflect.get(target, prop, recv); } } };\n " +
273+ "var proxy = new Proxy(function() {}, handler);\n " +
274+ "42 instanceof proxy" ;
275+ testFalse (code );
276+ }
277+
227278 @ ExportLibrary (InteropLibrary .class )
228279 public static class TestTruffleHash implements TruffleObject {
229280
0 commit comments