Skip to content

Commit

Permalink
Convert non-string properties to strings during access.
Browse files Browse the repository at this point in the history
  • Loading branch information
skinny85 committed Feb 11, 2024
1 parent cf6e496 commit c920a14
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.endoflineblog.truffle.part_13.exceptions.EasyScriptException;
import com.endoflineblog.truffle.part_13.nodes.exprs.strings.ReadTruffleStringPropertyNode;
import com.endoflineblog.truffle.part_13.runtime.EasyScriptTruffleStrings;
import com.endoflineblog.truffle.part_13.runtime.Undefined;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.Fallback;
Expand Down Expand Up @@ -60,6 +61,13 @@ protected Object readProperty(Object target, String propertyName,
}
}

@Specialization(guards = "interopLibrary.hasMembers(target)", limit = "2")
protected Object readNonStringProperty(Object target, Object property,
@CachedLibrary("target") InteropLibrary interopLibrary) {
return this.readProperty(target, EasyScriptTruffleStrings.toString(property),
interopLibrary);
}

/**
* Reading any property of {@code undefined}
* results in an error in JavaScript.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.endoflineblog.truffle.part_13.nodes.exprs.properties;

import com.endoflineblog.truffle.part_13.exceptions.EasyScriptException;
import com.endoflineblog.truffle.part_13.runtime.Undefined;
import com.endoflineblog.truffle.part_13.runtime.EasyScriptTruffleStrings;
import com.oracle.truffle.api.dsl.Fallback;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.interop.InteropLibrary;
Expand Down Expand Up @@ -30,6 +30,13 @@ protected Object writeProperty(Object target, String propertyName, Object rvalue
return rvalue;
}

@Specialization(guards = "interopLibrary.hasMembers(target)", limit = "2")
protected Object writeNonStringProperty(Object target, Object property, Object rvalue,
@CachedLibrary("target") InteropLibrary interopLibrary) {
return this.writeProperty(target, EasyScriptTruffleStrings.toString(property),
rvalue, interopLibrary);
}

/**
* Attempting to write any property of {@code undefined}
* results in an error in JavaScript.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,9 @@ public static boolean same(Object o1, Object o2) {
public static String concatToStrings(Object o1, Object o2) {
return o1.toString() + o2.toString();
}

@TruffleBoundary
public static String toString(Object object) {
return object.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,6 @@ public void non_int_indexes_are_ignored_on_write() {
assertEquals(45, result.asInt());
}

@Test
public void negative_indexes_are_ignored_on_write() {
Value array = this.context.eval("ezs", "" +
"let a = [9]; " +
"a[-1] = 45; " +
"a"
);

assertEquals(1, array.getArraySize());
assertEquals(9, array.getArrayElement(0).asInt());
assertFalse(array.hasMember("-1"));
}

@Test
public void non_stable_array_reads_work_correctly() {
Value result = this.context.eval("ezs", "" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,4 +245,25 @@ public void arrays_have_indexed_writeable_properties() {
assertEquals(5, result.getArrayElement(0).asInt());
assertEquals(5, result.getArrayElement(1).asInt());
}

@Test
public void non_string_properties_are_converted_to_strings() {
Value result = this.context.eval("ezs", "" +
"class A { } " +
"const a = new A(); " +
"a[true] = 35; " +
"a[1 + 2 === 3]"
);
assertEquals(35, result.asInt());
}

@Test
public void negative_array_indexes_are_converted_to_strings() {
Value result = this.context.eval("ezs", "" +
"let a = [9]; " +
"a[-1] = 45; " +
"a[-1]; "
);
assertEquals(45, result.asInt());
}
}

0 comments on commit c920a14

Please sign in to comment.