Skip to content

Commit 5f767b7

Browse files
authored
Move StringSliceWTF16 avoiding non UTF slices fix to precompute.cpp. (#7322)
1 parent 1d7757c commit 5f767b7

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

src/passes/Precompute.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,38 @@ class PrecomputingExpressionRunner
240240
// string.encode_wtf16_array anyhow.)
241241
return Flow(NONCONSTANT_FLOW);
242242
}
243+
244+
Flow visitStringSliceWTF(StringSliceWTF* curr) {
245+
auto flow = Super::visitStringSliceWTF(curr);
246+
if (flow.breaking()) {
247+
return flow;
248+
}
249+
250+
auto refData = flow.getSingleValue().getGCData();
251+
if (!refData) {
252+
return Flow(NONCONSTANT_FLOW);
253+
}
254+
255+
auto& refValues = refData->values;
256+
if (refValues.size() == 0) {
257+
return flow;
258+
}
259+
260+
// Check that the slice is valid; since we can assume that we have a valid
261+
// UTF-16, we only need to check that it did not split surrogate pairs.
262+
auto firstChar = refValues[0].getInteger();
263+
if (firstChar >= 0xDC00 && firstChar <= 0xDFFF) {
264+
// The first char cannot be a low surrogate.
265+
return Flow(NONCONSTANT_FLOW);
266+
}
267+
268+
auto lastChar = refValues[refValues.size() - 1].getInteger();
269+
if (lastChar >= 0xD800 && lastChar <= 0xDBFF) {
270+
// The last char cannot be a high surrogate.
271+
return Flow(NONCONSTANT_FLOW);
272+
}
273+
return flow;
274+
}
243275
};
244276

245277
struct Precompute

src/wasm-interpreter.h

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2273,20 +2273,6 @@ class ExpressionRunner : public OverriddenVisitor<SubType, Flow> {
22732273

22742274
Literals contents;
22752275
if (endVal > startVal) {
2276-
// Check that the slice is valid; since we can assume that we have a valid
2277-
// UTF-16, we only need to check that it did not split surrgate pairs.
2278-
auto firstChar = refValues[startVal].getInteger();
2279-
if (firstChar >= 0xDC00 && firstChar <= 0xDFFF) {
2280-
// The first char cannot be a low surrogate.
2281-
return Flow(NONCONSTANT_FLOW);
2282-
}
2283-
2284-
auto lastChar = refValues[endVal - 1].getInteger();
2285-
if (lastChar >= 0xD800 && lastChar <= 0xDBFF) {
2286-
// The last char cannot be a high surrogate.
2287-
return Flow(NONCONSTANT_FLOW);
2288-
}
2289-
22902276
contents.reserve(endVal - startVal);
22912277
for (size_t i = startVal; i < endVal; i++) {
22922278
if (i < refValues.size()) {

0 commit comments

Comments
 (0)