Skip to content

Commit bc0e990

Browse files
kripkenradekdoulik
authored andcommitted
wasm-ctor-eval: Properly eval strings (WebAssembly#6276)
WebAssembly#6244 tried to do this but was not quite right. It treated a string like an array or a struct, which means create a global for it. But just creating a global isn't enough, as it needs to also be sorted in the right place etc. which requires changes in other places. But there is a much simpler solution here: string constants are just constants, which we can emit in-line, so do that.
1 parent 7d77c56 commit bc0e990

File tree

2 files changed

+6
-12
lines changed

2 files changed

+6
-12
lines changed

src/tools/wasm-ctor-eval.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -838,8 +838,9 @@ struct CtorEvalExternalInterface : EvallingModuleRunner::ExternalInterface {
838838
// GC data (structs and arrays) must be handled with the special global-
839839
// creating logic later down. But MVP types as well as i31s (even
840840
// externalized i31s) can be handled by the general makeConstantExpression
841-
// logic (which knows how to handle externalization, for i31s).
842-
if (!value.isData()) {
841+
// logic (which knows how to handle externalization, for i31s; and it also
842+
// can handle string constants).
843+
if (!value.isData() || value.type.getHeapType().isString()) {
843844
return builder.makeConstantExpression(original);
844845
}
845846

@@ -889,12 +890,6 @@ struct CtorEvalExternalInterface : EvallingModuleRunner::ExternalInterface {
889890
} else if (heapType.isArray()) {
890891
// TODO: for repeated identical values, can use ArrayNew
891892
init = builder.makeArrayNewFixed(heapType, args);
892-
} else if (heapType.isString()) {
893-
std::string s;
894-
for (auto c : values) {
895-
s += char(c.getInteger());
896-
}
897-
init = builder.makeStringConst(s);
898893
} else {
899894
WASM_UNREACHABLE("bad gc type");
900895
}

test/lit/ctor-eval/string.wast

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
;; the output, as precomputing a string results in an identical string.
66

77
(module
8-
;; CHECK: (type $0 (func (result anyref)))
9-
10-
;; CHECK: (global $global (ref string) (string.const "one"))
118
(global $global (ref string) (string.const "one"))
129

1310
(export "test" (func $test))
@@ -16,8 +13,10 @@
1613
(global.get $global)
1714
)
1815
)
16+
;; CHECK: (type $0 (func (result anyref)))
17+
1918
;; CHECK: (export "test" (func $test_1))
2019

2120
;; CHECK: (func $test_1 (type $0) (result anyref)
22-
;; CHECK-NEXT: (global.get $global)
21+
;; CHECK-NEXT: (string.const "one")
2322
;; CHECK-NEXT: )

0 commit comments

Comments
 (0)