|
51 | 51 | import org.graalvm.polyglot.Context; |
52 | 52 | import org.graalvm.polyglot.HostAccess; |
53 | 53 | import org.graalvm.polyglot.Value; |
| 54 | +import org.graalvm.polyglot.proxy.ProxyExecutable; |
| 55 | +import org.graalvm.polyglot.proxy.ProxyObject; |
54 | 56 | import org.junit.Test; |
55 | 57 |
|
56 | 58 | import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; |
@@ -85,12 +87,38 @@ public void testForeignExecutable() { |
85 | 87 | } |
86 | 88 | } |
87 | 89 |
|
| 90 | + private static void checkStringification(Object objectToStringify, String expectedResult) { |
| 91 | + try (Context context = JSTest.newContextBuilder().allowHostAccess(HostAccess.ALL).build()) { |
| 92 | + context.getBindings(ID).putMember("objectToStringify", objectToStringify); |
| 93 | + Value result = context.eval(ID, "JSON.stringify(objectToStringify)"); |
| 94 | + assertEquals(expectedResult, result.asString()); |
| 95 | + } |
| 96 | + } |
| 97 | + |
88 | 98 | @Test |
89 | 99 | public void testNonReadableMembers() { |
90 | | - try (Context context = JSTest.newContextBuilder().allowHostAccess(HostAccess.ALL).build()) { |
91 | | - context.getBindings(ID).putMember("myObj", new InvocableMemberObject(Collections.singletonMap("someKey", "someValue"))); |
92 | | - Value result = context.eval(ID, "JSON.stringify(myObj)"); |
93 | | - assertEquals("{}", result.asString()); |
| 100 | + checkStringification(new InvocableMemberObject(Collections.singletonMap("someKey", "someValue")), "{}"); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void testToJSONOfProxyObject() { |
| 105 | + Object object = ProxyObject.fromMap(Collections.singletonMap("toJSON", new ProxyExecutable() { |
| 106 | + @Override |
| 107 | + public Object execute(Value... arguments) { |
| 108 | + return "fromToJSON"; |
| 109 | + } |
| 110 | + })); |
| 111 | + checkStringification(object, "\"fromToJSON\""); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void testToJSONOfHostObject() { |
| 116 | + checkStringification(new HostObjectWithToJSON(), "\"HostObjectWithToJSON\""); |
| 117 | + } |
| 118 | + |
| 119 | + public static class HostObjectWithToJSON { |
| 120 | + public Object toJSON(@SuppressWarnings("unused") String key) { |
| 121 | + return "HostObjectWithToJSON"; |
94 | 122 | } |
95 | 123 | } |
96 | 124 |
|
|
0 commit comments