|
1 | 1 | /* |
2 | | - * Copyright (c) 2019, 2021, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2019, 2022, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | 4 | * |
5 | 5 | * The Universal Permissive License (UPL), Version 1.0 |
|
46 | 46 | import static org.junit.Assert.assertFalse; |
47 | 47 | import static org.junit.Assert.assertTrue; |
48 | 48 |
|
| 49 | +import java.time.Instant; |
49 | 50 | import java.util.Arrays; |
50 | 51 | import java.util.List; |
51 | 52 |
|
|
54 | 55 | import org.graalvm.polyglot.Value; |
55 | 56 | import org.graalvm.polyglot.proxy.ProxyArray; |
56 | 57 | import org.graalvm.polyglot.proxy.ProxyExecutable; |
| 58 | +import org.junit.Assert; |
57 | 59 | import org.junit.Test; |
58 | 60 |
|
| 61 | +import com.oracle.truffle.api.interop.InteropLibrary; |
| 62 | +import com.oracle.truffle.api.interop.TruffleObject; |
| 63 | +import com.oracle.truffle.api.library.ExportLibrary; |
| 64 | +import com.oracle.truffle.api.library.ExportMessage; |
59 | 65 | import com.oracle.truffle.js.test.JSTest; |
60 | 66 |
|
61 | 67 | public class ForeignObjectPrototypeTest { |
@@ -120,4 +126,235 @@ public void testHostMethodStatic() { |
120 | 126 | } |
121 | 127 | } |
122 | 128 |
|
| 129 | + @Test |
| 130 | + public void testPrototype() { |
| 131 | + testPrototypeIntl("Array", ProxyArray.fromArray("fun", "with", "proxy", "array")); |
| 132 | + testPrototypeIntl("Date", Instant.now()); |
| 133 | + testPrototypeIntl("Map", new TestTruffleHash()); |
| 134 | + testPrototypeIntl("String", new TestTruffleString()); |
| 135 | + testPrototypeIntl("Boolean", new TestTruffleBoolean()); |
| 136 | + testPrototypeIntl("Number", new TestTruffleNumber()); |
| 137 | + testPrototypeIntl("Function", (ProxyExecutable) v -> true); |
| 138 | + testPrototypeIntl("Object", new Object()); |
| 139 | + } |
| 140 | + |
| 141 | + private static void testPrototypeIntl(String prototype, Object obj) { |
| 142 | + String code = "(obj) => { var proto = Object.getPrototypeOf(obj); \n" + |
| 143 | + " var protoProto = Object.getPrototypeOf(proto); \n" + |
| 144 | + " return protoProto === " + prototype + ".prototype; \n" + |
| 145 | + "}"; |
| 146 | + try (Context context = JSTest.newContextBuilder(ID).build()) { |
| 147 | + Value result = context.eval(ID, code).execute(obj); |
| 148 | + Assert.assertTrue(result.asBoolean()); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + @Test |
| 153 | + public void testPrototypeDisabled() { |
| 154 | + testDisabled(ProxyArray.fromArray("fun", "with", "proxy", "array")); |
| 155 | + testDisabled((ProxyExecutable) args -> "hi"); |
| 156 | + testDisabled(Instant.now()); |
| 157 | + } |
| 158 | + |
| 159 | + private static void testDisabled(Object array) { |
| 160 | + String code = "(obj) => { return Object.getPrototypeOf(obj) === null; }"; |
| 161 | + try (Context context = JSTest.newContextBuilder(ID).option(FOREIGN_OBJECT_PROTOTYPE_NAME, "false").build()) { |
| 162 | + Value result = context.eval(ID, code).execute(array); |
| 163 | + Assert.assertTrue(result.asBoolean()); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + @Test |
| 168 | + public void testHostMethodHasPrecedence() { |
| 169 | + String codeGetEpochSecond = "(obj) => { return obj.getEpochSecond(); }"; |
| 170 | + try (Context context = JSTest.newContextBuilder(ID).allowHostAccess(HostAccess.ALL).build()) { |
| 171 | + Instant inst = Instant.now(); |
| 172 | + |
| 173 | + // check getter from Java can be called |
| 174 | + long second = context.eval(ID, codeGetEpochSecond).execute(inst).asLong(); |
| 175 | + Assert.assertEquals(inst.getEpochSecond(), second); |
| 176 | + |
| 177 | + // provide your own getter |
| 178 | + context.eval(ID, "(obj) => { Object.getPrototypeOf(obj).getEpochSecond = () => { return 666; }; };").execute(inst); |
| 179 | + |
| 180 | + // verify that still the host getter is called |
| 181 | + Instant inst2 = Instant.now(); |
| 182 | + Value result = context.eval(ID, codeGetEpochSecond).execute(inst2); |
| 183 | + Assert.assertEquals(inst2.getEpochSecond(), result.asLong()); |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + @Test |
| 188 | + public void testJSBuiltinCanBeOverwritten() { |
| 189 | + String codeGetUTCString = "(obj) => { return obj.toUTCString(); }"; |
| 190 | + try (Context context = JSTest.newContextBuilder(ID).allowHostAccess(HostAccess.ALL).build()) { |
| 191 | + Instant inst = Instant.now(); |
| 192 | + |
| 193 | + // check JS Prototype method can be called |
| 194 | + String utcString = context.eval(ID, codeGetUTCString).execute(inst).asString(); |
| 195 | + Assert.assertTrue(utcString.length() > 10); |
| 196 | + |
| 197 | + // provide your own getter |
| 198 | + context.eval(ID, "(obj) => { Object.getPrototypeOf(obj).toUTCString = () => { return 'special'; }; };").execute(inst); |
| 199 | + |
| 200 | + // verify that on another object this overwritten method is called |
| 201 | + Instant inst2 = Instant.now(); |
| 202 | + Value result = context.eval(ID, codeGetUTCString).execute(inst2); |
| 203 | + Assert.assertEquals("special", result.asString()); |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + @Test |
| 208 | + 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()); |
| 217 | + } |
| 218 | + |
| 219 | + private static void testInstanceofIntl(String prototype, Object obj) { |
| 220 | + String code = "(obj) => { return (obj instanceof " + prototype + "); }"; |
| 221 | + try (Context context = JSTest.newContextBuilder(ID).build()) { |
| 222 | + Value result = context.eval(ID, code).execute(obj); |
| 223 | + Assert.assertTrue(result.asBoolean()); |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + @ExportLibrary(InteropLibrary.class) |
| 228 | + public static class TestTruffleHash implements TruffleObject { |
| 229 | + |
| 230 | + @ExportMessage |
| 231 | + @SuppressWarnings("static-method") |
| 232 | + boolean hasHashEntries() { |
| 233 | + return true; |
| 234 | + } |
| 235 | + |
| 236 | + @ExportMessage |
| 237 | + @SuppressWarnings("static-method") |
| 238 | + long getHashSize() { |
| 239 | + return 0; |
| 240 | + } |
| 241 | + |
| 242 | + @ExportMessage |
| 243 | + @SuppressWarnings("static-method") |
| 244 | + Object getHashEntriesIterator() { |
| 245 | + return null; |
| 246 | + } |
| 247 | + } |
| 248 | + |
| 249 | + @ExportLibrary(InteropLibrary.class) |
| 250 | + public static class TestTruffleString implements TruffleObject { |
| 251 | + @ExportMessage |
| 252 | + @SuppressWarnings("static-method") |
| 253 | + boolean isString() { |
| 254 | + return true; |
| 255 | + } |
| 256 | + |
| 257 | + @ExportMessage |
| 258 | + @SuppressWarnings("static-method") |
| 259 | + String asString() { |
| 260 | + return ""; |
| 261 | + } |
| 262 | + } |
| 263 | + |
| 264 | + @ExportLibrary(InteropLibrary.class) |
| 265 | + public static class TestTruffleBoolean implements TruffleObject { |
| 266 | + @ExportMessage |
| 267 | + @SuppressWarnings("static-method") |
| 268 | + boolean isBoolean() { |
| 269 | + return true; |
| 270 | + } |
| 271 | + |
| 272 | + @ExportMessage |
| 273 | + @SuppressWarnings("static-method") |
| 274 | + boolean asBoolean() { |
| 275 | + return true; |
| 276 | + } |
| 277 | + } |
| 278 | + |
| 279 | + @ExportLibrary(InteropLibrary.class) |
| 280 | + public static class TestTruffleNumber implements TruffleObject { |
| 281 | + @ExportMessage |
| 282 | + @SuppressWarnings("static-method") |
| 283 | + boolean isNumber() { |
| 284 | + return true; |
| 285 | + } |
| 286 | + |
| 287 | + @ExportMessage |
| 288 | + @SuppressWarnings("static-method") |
| 289 | + final boolean fitsInByte() { |
| 290 | + return true; |
| 291 | + } |
| 292 | + |
| 293 | + @ExportMessage |
| 294 | + @SuppressWarnings("static-method") |
| 295 | + final boolean fitsInShort() { |
| 296 | + return true; |
| 297 | + } |
| 298 | + |
| 299 | + @ExportMessage |
| 300 | + @SuppressWarnings("static-method") |
| 301 | + final boolean fitsInInt() { |
| 302 | + return true; |
| 303 | + } |
| 304 | + |
| 305 | + @ExportMessage |
| 306 | + @SuppressWarnings("static-method") |
| 307 | + final boolean fitsInLong() { |
| 308 | + return true; |
| 309 | + } |
| 310 | + |
| 311 | + @ExportMessage |
| 312 | + @SuppressWarnings("static-method") |
| 313 | + final boolean fitsInFloat() { |
| 314 | + return true; |
| 315 | + } |
| 316 | + |
| 317 | + @ExportMessage |
| 318 | + @SuppressWarnings("static-method") |
| 319 | + final boolean fitsInDouble() { |
| 320 | + return true; |
| 321 | + } |
| 322 | + |
| 323 | + @ExportMessage |
| 324 | + @SuppressWarnings("static-method") |
| 325 | + final byte asByte() { |
| 326 | + return (byte) 0; |
| 327 | + } |
| 328 | + |
| 329 | + @ExportMessage |
| 330 | + @SuppressWarnings("static-method") |
| 331 | + final short asShort() { |
| 332 | + return (short) 0; |
| 333 | + } |
| 334 | + |
| 335 | + @ExportMessage |
| 336 | + @SuppressWarnings("static-method") |
| 337 | + final int asInt() { |
| 338 | + return 0; |
| 339 | + } |
| 340 | + |
| 341 | + @ExportMessage |
| 342 | + @SuppressWarnings("static-method") |
| 343 | + final long asLong() { |
| 344 | + return 0L; |
| 345 | + } |
| 346 | + |
| 347 | + @ExportMessage |
| 348 | + @SuppressWarnings("static-method") |
| 349 | + final float asFloat() { |
| 350 | + return 0.0F; |
| 351 | + } |
| 352 | + |
| 353 | + @ExportMessage |
| 354 | + @SuppressWarnings("static-method") |
| 355 | + final double asDouble() { |
| 356 | + return 0.0D; |
| 357 | + } |
| 358 | + |
| 359 | + } |
123 | 360 | } |
0 commit comments