Skip to content

Commit 419b65c

Browse files
authored
[Strings] Implement string.encode_wtf16_array (#6402)
1 parent 17823cd commit 419b65c

File tree

2 files changed

+105
-1
lines changed

2 files changed

+105
-1
lines changed

src/wasm-interpreter.h

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1919,7 +1919,43 @@ class ExpressionRunner : public OverriddenVisitor<SubType, Flow> {
19191919
}
19201920
return Literal(int32_t(data->values.size()));
19211921
}
1922-
Flow visitStringEncode(StringEncode* curr) { return Flow(NONCONSTANT_FLOW); }
1922+
Flow visitStringEncode(StringEncode* curr) {
1923+
// For now we only support JS-style strings into arrays.
1924+
if (curr->op != StringEncodeWTF16Array) {
1925+
return Flow(NONCONSTANT_FLOW);
1926+
}
1927+
1928+
Flow ref = visit(curr->ref);
1929+
if (ref.breaking()) {
1930+
return ref;
1931+
}
1932+
Flow ptr = visit(curr->ptr);
1933+
if (ptr.breaking()) {
1934+
return ptr;
1935+
}
1936+
Flow start = visit(curr->start);
1937+
if (start.breaking()) {
1938+
return start;
1939+
}
1940+
1941+
auto refData = ref.getSingleValue().getGCData();
1942+
auto ptrData = ptr.getSingleValue().getGCData();
1943+
if (!refData || !ptrData) {
1944+
trap("null ref");
1945+
}
1946+
auto startVal = start.getSingleValue().getInteger();
1947+
auto& refValues = refData->values;
1948+
auto& ptrValues = ptrData->values;
1949+
if (startVal + refValues.size() > ptrValues.size()) {
1950+
trap("oob");
1951+
}
1952+
1953+
for (Index i = 0; i < refValues.size(); i++) {
1954+
ptrValues[startVal + i] = refValues[i];
1955+
}
1956+
1957+
return Literal(int32_t(refData->values.size()));
1958+
}
19231959
Flow visitStringConcat(StringConcat* curr) { return Flow(NONCONSTANT_FLOW); }
19241960
Flow visitStringEq(StringEq* curr) {
19251961
NOTE_ENTER("StringEq");

test/lit/exec/strings.wast

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44

55
(module
66
(type $array16 (array (mut i16)))
7+
78
(memory 1 1)
89

10+
(import "fuzzing-support" "log" (func $log (param i32)))
11+
912
;; CHECK: [fuzz-exec] calling new_wtf16_array
1013
;; CHECK-NEXT: [fuzz-exec] note result: new_wtf16_array => string("ello")
1114
(func $new_wtf16_array (export "new_wtf16_array") (result stringref)
@@ -185,6 +188,62 @@
185188
)
186189
)
187190
)
191+
192+
;; CHECK: [fuzz-exec] calling encode
193+
;; CHECK-NEXT: [LoggingExternalInterface logging 3]
194+
;; CHECK-NEXT: [LoggingExternalInterface logging 0]
195+
;; CHECK-NEXT: [LoggingExternalInterface logging 97]
196+
;; CHECK-NEXT: [LoggingExternalInterface logging 98]
197+
;; CHECK-NEXT: [LoggingExternalInterface logging 99]
198+
;; CHECK-NEXT: [LoggingExternalInterface logging 0]
199+
(func $encode (export "encode")
200+
(local $array16 (ref $array16))
201+
(local.set $array16
202+
(array.new_default $array16
203+
(i32.const 10)
204+
)
205+
)
206+
;; Log out that we wrote 3 things.
207+
(call $log
208+
(string.encode_wtf16_array
209+
(string.const "abc")
210+
(local.get $array16)
211+
(i32.const 4)
212+
)
213+
)
214+
;; We wrote 3 things at offset 4. Log out the values at 3,4,5,6,7 (the first
215+
;; and last should be 0, and "abc" in between).
216+
(call $log
217+
(array.get $array16
218+
(local.get $array16)
219+
(i32.const 3)
220+
)
221+
)
222+
(call $log
223+
(array.get $array16
224+
(local.get $array16)
225+
(i32.const 4)
226+
)
227+
)
228+
(call $log
229+
(array.get $array16
230+
(local.get $array16)
231+
(i32.const 5)
232+
)
233+
)
234+
(call $log
235+
(array.get $array16
236+
(local.get $array16)
237+
(i32.const 6)
238+
)
239+
)
240+
(call $log
241+
(array.get $array16
242+
(local.get $array16)
243+
(i32.const 7)
244+
)
245+
)
246+
)
188247
)
189248
;; CHECK: [fuzz-exec] calling new_wtf16_array
190249
;; CHECK-NEXT: [fuzz-exec] note result: new_wtf16_array => string("ello")
@@ -242,6 +301,14 @@
242301

243302
;; CHECK: [fuzz-exec] calling get_length
244303
;; CHECK-NEXT: [fuzz-exec] note result: get_length => 7
304+
305+
;; CHECK: [fuzz-exec] calling encode
306+
;; CHECK-NEXT: [LoggingExternalInterface logging 3]
307+
;; CHECK-NEXT: [LoggingExternalInterface logging 0]
308+
;; CHECK-NEXT: [LoggingExternalInterface logging 97]
309+
;; CHECK-NEXT: [LoggingExternalInterface logging 98]
310+
;; CHECK-NEXT: [LoggingExternalInterface logging 99]
311+
;; CHECK-NEXT: [LoggingExternalInterface logging 0]
245312
;; CHECK-NEXT: [fuzz-exec] comparing compare.1
246313
;; CHECK-NEXT: [fuzz-exec] comparing compare.10
247314
;; CHECK-NEXT: [fuzz-exec] comparing compare.2
@@ -253,6 +320,7 @@
253320
;; CHECK-NEXT: [fuzz-exec] comparing compare.8
254321
;; CHECK-NEXT: [fuzz-exec] comparing compare.9
255322
;; CHECK-NEXT: [fuzz-exec] comparing const
323+
;; CHECK-NEXT: [fuzz-exec] comparing encode
256324
;; CHECK-NEXT: [fuzz-exec] comparing eq.1
257325
;; CHECK-NEXT: [fuzz-exec] comparing eq.2
258326
;; CHECK-NEXT: [fuzz-exec] comparing eq.3

0 commit comments

Comments
 (0)