From edc53e25c1bddee8b4604687a8e7dfc9a51490d9 Mon Sep 17 00:00:00 2001 From: Adam Ruka Date: Sat, 20 Apr 2024 16:21:39 -0700 Subject: [PATCH] Allow writes to prototypes directly. --- .../part_13/runtime/InteropDynamicObject.java | 18 ++++++++++++++++++ .../part_13/runtime/JavaScriptObject.java | 6 ------ .../truffle/part_13/InheritanceTest.java | 18 ++++++++++++++++++ 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/part-14/src/main/java/com/endoflineblog/truffle/part_13/runtime/InteropDynamicObject.java b/part-14/src/main/java/com/endoflineblog/truffle/part_13/runtime/InteropDynamicObject.java index 60029076..107d56f7 100644 --- a/part-14/src/main/java/com/endoflineblog/truffle/part_13/runtime/InteropDynamicObject.java +++ b/part-14/src/main/java/com/endoflineblog/truffle/part_13/runtime/InteropDynamicObject.java @@ -42,4 +42,22 @@ Object getMembers(@SuppressWarnings("unused") boolean includeInternal, @CachedLibrary("this") DynamicObjectLibrary thisObjectLibrary) { return new MemberNamesObject(thisObjectLibrary.getKeyArray(this)); } + + @ExportMessage + boolean isMemberModifiable(String member, + @CachedLibrary("this") DynamicObjectLibrary thisObjectLibrary) { + return this.isMemberReadable(member, thisObjectLibrary); + } + + @ExportMessage + boolean isMemberInsertable(String member, + @CachedLibrary("this") DynamicObjectLibrary thisObjectLibrary) { + return !this.isMemberModifiable(member, thisObjectLibrary); + } + + @ExportMessage + void writeMember(String member, Object value, + @CachedLibrary("this") DynamicObjectLibrary thisObjectLibrary) { + thisObjectLibrary.put(this, member, value); + } } diff --git a/part-14/src/main/java/com/endoflineblog/truffle/part_13/runtime/JavaScriptObject.java b/part-14/src/main/java/com/endoflineblog/truffle/part_13/runtime/JavaScriptObject.java index 502b9752..1fc2a8c7 100644 --- a/part-14/src/main/java/com/endoflineblog/truffle/part_13/runtime/JavaScriptObject.java +++ b/part-14/src/main/java/com/endoflineblog/truffle/part_13/runtime/JavaScriptObject.java @@ -73,10 +73,4 @@ boolean isMemberInsertable(String member, @CachedLibrary("this.classPrototypeObject") DynamicObjectLibrary prototypeObjectLibrary) { return !this.isMemberModifiable(member, thisObjectLibrary, prototypeObjectLibrary); } - - @ExportMessage - void writeMember(String member, Object value, - @CachedLibrary("this") DynamicObjectLibrary thisObjectLibrary) { - thisObjectLibrary.put(this, member, value); - } } diff --git a/part-14/src/test/java/com/endoflineblog/truffle/part_13/InheritanceTest.java b/part-14/src/test/java/com/endoflineblog/truffle/part_13/InheritanceTest.java index 2dc3bf45..14bed56c 100644 --- a/part-14/src/test/java/com/endoflineblog/truffle/part_13/InheritanceTest.java +++ b/part-14/src/test/java/com/endoflineblog/truffle/part_13/InheritanceTest.java @@ -179,6 +179,24 @@ void hasOwnProperty_returns_false_for_primitives() { assertFalse(result.asBoolean()); } + @Test + void prototypes_can_be_written_to() { + Value result = this.context.eval("ezs", "" + + "class Class { " + + " m() { " + + " return 13; " + + " } " + + "} " + + "function m() { " + + " return 46; " + + "} " + + "const obj = new Class(); " + + "Class.m = m; " + + "obj.m(); " + ); + assertEquals(46, result.asInt()); + } + @Test void extending_non_existent_class_is_an_error() { try {