summaryrefslogtreecommitdiff
path: root/src/binaryen-c.cpp
diff options
context:
space:
mode:
authorThomas Lively <tlively@google.com>2024-03-22 16:56:33 -0700
committerGitHub <noreply@github.com>2024-03-22 23:56:33 +0000
commitb3fea30f84fef3ff7aa77775e00b83ba62d997cc (patch)
tree53494a466d8e56d34d849d14927817a22f843748 /src/binaryen-c.cpp
parentd3414c3deaebe7ba35731a8c20d7fa5f5a833ca3 (diff)
downloadbinaryen-b3fea30f84fef3ff7aa77775e00b83ba62d997cc.tar.gz
binaryen-b3fea30f84fef3ff7aa77775e00b83ba62d997cc.tar.bz2
binaryen-b3fea30f84fef3ff7aa77775e00b83ba62d997cc.zip
[Strings] Represent string values as WTF-16 internally (#6418)
WTF-16, i.e. arbitrary sequences of 16-bit values, is the encoding of Java and JavaScript strings, and using the same encoding makes the interpretation of string operations trivial, even when accounting for non-ascii characters. Specifically, use little-endian WTF-16. Re-encode string constants from WTF-8 to WTF-16 in the parsers, then back to WTF-8 in the writers. Update the constructor for string `Literal`s to interpret the string as WTF-16 and store a sequence of WTF-16 code units, i.e. 16-bit integers. Update `Builder::makeConstantExpression` accordingly to convert from the new `Literal` string representation back to a WTF-16 string. Update the interpreter to remove the logic for detecting non-ascii characters and bailing out. The naive implementations of all the string operations are correct now that our string encoding matches the JS string encoding.
Diffstat (limited to 'src/binaryen-c.cpp')
-rw-r--r--src/binaryen-c.cpp8
1 files changed, 7 insertions, 1 deletions
diff --git a/src/binaryen-c.cpp b/src/binaryen-c.cpp
index 29e0597d7..402dce553 100644
--- a/src/binaryen-c.cpp
+++ b/src/binaryen-c.cpp
@@ -26,6 +26,7 @@
#include "pass.h"
#include "shell-interface.h"
#include "support/colors.h"
+#include "support/string.h"
#include "wasm-binary.h"
#include "wasm-builder.h"
#include "wasm-interpreter.h"
@@ -1895,8 +1896,13 @@ BinaryenExpressionRef BinaryenStringNew(BinaryenModuleRef module,
}
BinaryenExpressionRef BinaryenStringConst(BinaryenModuleRef module,
const char* name) {
+ // Re-encode from WTF-8 to WTF-16.
+ std::stringstream wtf16;
+ [[maybe_unused]] bool valid = String::convertWTF8ToWTF16(wtf16, name);
+ assert(valid);
+ // TODO: Use wtf16.view() once we have C++20.
return static_cast<Expression*>(
- Builder(*(Module*)module).makeStringConst(name));
+ Builder(*(Module*)module).makeStringConst(wtf16.str()));
}
BinaryenExpressionRef BinaryenStringMeasure(BinaryenModuleRef module,
BinaryenOp op,