Skip to content

Commit

Permalink
Allow writes to prototypes directly.
Browse files Browse the repository at this point in the history
  • Loading branch information
skinny85 committed Apr 20, 2024
1 parent a930017 commit edc53e2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit edc53e2

Please sign in to comment.