From 787bdf1868850ee27195d0da925d1dfda552cba9 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Mon, 12 Feb 2024 16:53:26 -0800 Subject: [PATCH 1/5] start --- src/wasm-interpreter.h | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/wasm-interpreter.h b/src/wasm-interpreter.h index 1d9c576bc8b..4a918056d4b 100644 --- a/src/wasm-interpreter.h +++ b/src/wasm-interpreter.h @@ -1975,7 +1975,28 @@ class ExpressionRunner : public OverriddenVisitor { Flow visitStringWTF8Advance(StringWTF8Advance* curr) { WASM_UNREACHABLE("unimp"); } - Flow visitStringWTF16Get(StringWTF16Get* curr) { WASM_UNREACHABLE("unimp"); } + Flow visitStringWTF16Get(StringWTF16Get* curr) { + NOTE_ENTER("StringEq"); + Flow ref = visit(curr->ref); + if (ref.breaking()) { + return ref; + } + Flow pos = visit(curr->pos); + if (pos.breaking()) { + return pos; + } + auto refValue = ref.getSingleValue(); + auto data = refValue.getGCData(); + if (!data) { + trap("null ref"); + } + auto& values = data->values; + Index i = pos.getSingleValue().geti32(); + if (i >= values.size()) { + trap("string oob"); + } + return Literal(values[i].geti32()); + } Flow visitStringIterNext(StringIterNext* curr) { WASM_UNREACHABLE("unimp"); } Flow visitStringIterMove(StringIterMove* curr) { WASM_UNREACHABLE("unimp"); } Flow visitStringSliceWTF(StringSliceWTF* curr) { WASM_UNREACHABLE("unimp"); } From 65a278269d898b9f67fc0beffac25fbaa44cbdb4 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Mon, 12 Feb 2024 16:57:44 -0800 Subject: [PATCH 2/5] festive --- src/wasm-interpreter.h | 4 +++- test/lit/exec/strings.wast | 10 ++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/wasm-interpreter.h b/src/wasm-interpreter.h index 4a918056d4b..acfe09335a9 100644 --- a/src/wasm-interpreter.h +++ b/src/wasm-interpreter.h @@ -1971,7 +1971,9 @@ class ExpressionRunner : public OverriddenVisitor { } return Literal(result); } - Flow visitStringAs(StringAs* curr) { WASM_UNREACHABLE("unimp"); } + Flow visitStringAs(StringAs* curr) { + // TODO + } Flow visitStringWTF8Advance(StringWTF8Advance* curr) { WASM_UNREACHABLE("unimp"); } diff --git a/test/lit/exec/strings.wast b/test/lit/exec/strings.wast index e7e31392af5..45419c572b1 100644 --- a/test/lit/exec/strings.wast +++ b/test/lit/exec/strings.wast @@ -162,6 +162,16 @@ (string.const "hf") ) ) + + (func $get_codeunit (export "get_codeunit") (result i32) + ;; Reads 'c' which is code 99 + (stringview_wtf16.get_codeunit + (string.as_wtf16 + (string.const "abcdefg") + ) + (i32.const 2) + ) + ) ) ;; CHECK: [fuzz-exec] calling new_wtf16_array ;; CHECK-NEXT: [fuzz-exec] note result: new_wtf16_array => string("ello") From c3a73ba749d7b86eb13a64e3e16bc39f591a5b8d Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Tue, 13 Feb 2024 14:16:17 -0800 Subject: [PATCH 3/5] work --- src/wasm-interpreter.h | 17 ++++++++++++++++- src/wasm/wasm-type.cpp | 4 +++- test/lit/exec/strings.wast | 6 ++++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/wasm-interpreter.h b/src/wasm-interpreter.h index acfe09335a9..b1ac6217623 100644 --- a/src/wasm-interpreter.h +++ b/src/wasm-interpreter.h @@ -1972,7 +1972,22 @@ class ExpressionRunner : public OverriddenVisitor { return Literal(result); } Flow visitStringAs(StringAs* curr) { - // TODO + // For now we only support JS-style strings. + assert(curr->op == StringAsWTF16); + + Flow flow = visit(curr->ref); + if (flow.breaking()) { + return flow; + } + auto value = flow.getSingleValue(); + auto data = value.getGCData(); + if (!data) { + trap("null ref"); + } + + // A JS-style string can be viewed simply as the underlying data. All we + // need to do is fix up the type. + return Literal(data, curr->type.getHeapType()); } Flow visitStringWTF8Advance(StringWTF8Advance* curr) { WASM_UNREACHABLE("unimp"); diff --git a/src/wasm/wasm-type.cpp b/src/wasm/wasm-type.cpp index 31042835c04..5c239bf9fe4 100644 --- a/src/wasm/wasm-type.cpp +++ b/src/wasm/wasm-type.cpp @@ -1163,7 +1163,9 @@ bool HeapType::isFunction() const { bool HeapType::isData() const { if (isBasic()) { - return id == struct_ || id == array || id == string; + // TODO comment about stringview JS like + return id == struct_ || id == array || id == string || + id == stringview_wtf16; } else { return getHeapTypeInfo(*this)->isData(); } diff --git a/test/lit/exec/strings.wast b/test/lit/exec/strings.wast index 45419c572b1..d84808c285c 100644 --- a/test/lit/exec/strings.wast +++ b/test/lit/exec/strings.wast @@ -163,6 +163,8 @@ ) ) + ;; CHECK: [fuzz-exec] calling get_codeunit + ;; CHECK-NEXT: [fuzz-exec] note result: get_codeunit => 99 (func $get_codeunit (export "get_codeunit") (result i32) ;; Reads 'c' which is code 99 (stringview_wtf16.get_codeunit @@ -223,6 +225,9 @@ ;; CHECK: [fuzz-exec] calling compare.10 ;; CHECK-NEXT: [fuzz-exec] note result: compare.10 => -1 + +;; CHECK: [fuzz-exec] calling get_codeunit +;; CHECK-NEXT: [fuzz-exec] note result: get_codeunit => 99 ;; CHECK-NEXT: [fuzz-exec] comparing compare.1 ;; CHECK-NEXT: [fuzz-exec] comparing compare.10 ;; CHECK-NEXT: [fuzz-exec] comparing compare.2 @@ -239,4 +244,5 @@ ;; CHECK-NEXT: [fuzz-exec] comparing eq.3 ;; CHECK-NEXT: [fuzz-exec] comparing eq.4 ;; CHECK-NEXT: [fuzz-exec] comparing eq.5 +;; CHECK-NEXT: [fuzz-exec] comparing get_codeunit ;; CHECK-NEXT: [fuzz-exec] comparing new_wtf16_array From dec8362f1c8ad8ea0dfea9452b6a48dac6000430 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Tue, 13 Feb 2024 14:26:30 -0800 Subject: [PATCH 4/5] another --- src/wasm-interpreter.h | 16 +++++++++++++++- test/lit/exec/strings.wast | 17 ++++++++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/wasm-interpreter.h b/src/wasm-interpreter.h index b1ac6217623..b65b909c788 100644 --- a/src/wasm-interpreter.h +++ b/src/wasm-interpreter.h @@ -1902,7 +1902,21 @@ class ExpressionRunner : public OverriddenVisitor { Flow visitStringConst(StringConst* curr) { return Literal(curr->string.toString()); } - Flow visitStringMeasure(StringMeasure* curr) { WASM_UNREACHABLE("unimp"); } + Flow visitStringMeasure(StringMeasure* curr) { + // For now we only support JS-style strings. + assert(curr->op == StringMeasureWTF16View); + + Flow flow = visit(curr->ref); + if (flow.breaking()) { + return flow; + } + auto value = flow.getSingleValue(); + auto data = value.getGCData(); + if (!data) { + trap("null ref"); + } + return Literal(int32_t(data->values.size())); + } Flow visitStringEncode(StringEncode* curr) { WASM_UNREACHABLE("unimp"); } Flow visitStringConcat(StringConcat* curr) { WASM_UNREACHABLE("unimp"); } Flow visitStringEq(StringEq* curr) { diff --git a/test/lit/exec/strings.wast b/test/lit/exec/strings.wast index d84808c285c..711d2e36a27 100644 --- a/test/lit/exec/strings.wast +++ b/test/lit/exec/strings.wast @@ -166,7 +166,7 @@ ;; CHECK: [fuzz-exec] calling get_codeunit ;; CHECK-NEXT: [fuzz-exec] note result: get_codeunit => 99 (func $get_codeunit (export "get_codeunit") (result i32) - ;; Reads 'c' which is code 99 + ;; Reads 'c' which is code 99. (stringview_wtf16.get_codeunit (string.as_wtf16 (string.const "abcdefg") @@ -174,6 +174,17 @@ (i32.const 2) ) ) + + ;; CHECK: [fuzz-exec] calling get_length + ;; CHECK-NEXT: [fuzz-exec] note result: get_length => 7 + (func $get_length (export "get_length") (result i32) + ;; This should return 7. + (stringview_wtf16.length + (string.as_wtf16 + (string.const "1234567") + ) + ) + ) ) ;; CHECK: [fuzz-exec] calling new_wtf16_array ;; CHECK-NEXT: [fuzz-exec] note result: new_wtf16_array => string("ello") @@ -228,6 +239,9 @@ ;; CHECK: [fuzz-exec] calling get_codeunit ;; CHECK-NEXT: [fuzz-exec] note result: get_codeunit => 99 + +;; CHECK: [fuzz-exec] calling get_length +;; CHECK-NEXT: [fuzz-exec] note result: get_length => 7 ;; CHECK-NEXT: [fuzz-exec] comparing compare.1 ;; CHECK-NEXT: [fuzz-exec] comparing compare.10 ;; CHECK-NEXT: [fuzz-exec] comparing compare.2 @@ -245,4 +259,5 @@ ;; CHECK-NEXT: [fuzz-exec] comparing eq.4 ;; CHECK-NEXT: [fuzz-exec] comparing eq.5 ;; CHECK-NEXT: [fuzz-exec] comparing get_codeunit +;; CHECK-NEXT: [fuzz-exec] comparing get_length ;; CHECK-NEXT: [fuzz-exec] comparing new_wtf16_array From 752a6931b2b508f7f6b41ee5798037809ebba288 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Tue, 13 Feb 2024 14:29:45 -0800 Subject: [PATCH 5/5] fix --- src/wasm/wasm-type.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/wasm/wasm-type.cpp b/src/wasm/wasm-type.cpp index 5c239bf9fe4..b14e5aa39d2 100644 --- a/src/wasm/wasm-type.cpp +++ b/src/wasm/wasm-type.cpp @@ -1163,7 +1163,6 @@ bool HeapType::isFunction() const { bool HeapType::isData() const { if (isBasic()) { - // TODO comment about stringview JS like return id == struct_ || id == array || id == string || id == stringview_wtf16; } else {