diff options
author | magic-akari <akari.ccino@gmail.com> | 2022-03-18 01:27:28 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-17 17:27:28 +0000 |
commit | b7e1989553a645968399ad379a86b809e04c0ae2 (patch) | |
tree | 714472a3d156ad55f1a8ba1618426426b7295433 /src/emscripten-optimizer | |
parent | 86acb00c7095017f8bded15def3c928909099731 (diff) | |
download | binaryen-b7e1989553a645968399ad379a86b809e04c0ae2.tar.gz binaryen-b7e1989553a645968399ad379a86b809e04c0ae2.tar.bz2 binaryen-b7e1989553a645968399ad379a86b809e04c0ae2.zip |
[wasm2js] Support exports of Globals (#4523)
Export an object with a `.value` property like the wasm JS API does
in browsers, and implement them with a getter and setter.
Fixes #4522
Diffstat (limited to 'src/emscripten-optimizer')
-rw-r--r-- | src/emscripten-optimizer/parser.cpp | 2 | ||||
-rw-r--r-- | src/emscripten-optimizer/parser.h | 6 | ||||
-rw-r--r-- | src/emscripten-optimizer/simple_ast.h | 54 |
3 files changed, 55 insertions, 7 deletions
diff --git a/src/emscripten-optimizer/parser.cpp b/src/emscripten-optimizer/parser.cpp index de749817f..016966299 100644 --- a/src/emscripten-optimizer/parser.cpp +++ b/src/emscripten-optimizer/parser.cpp @@ -107,6 +107,8 @@ IString ATOMICS("Atomics"); IString COMPARE_EXCHANGE("compareExchange"); IString LOAD("load"); IString STORE("store"); +IString GETTER("get"); +IString SETTER("set"); IStringSet keywords("var const function if else do while for break continue return " diff --git a/src/emscripten-optimizer/parser.h b/src/emscripten-optimizer/parser.h index 3e1623c19..c4d596058 100644 --- a/src/emscripten-optimizer/parser.h +++ b/src/emscripten-optimizer/parser.h @@ -124,6 +124,8 @@ extern IString ATOMICS; extern IString COMPARE_EXCHANGE; extern IString LOAD; extern IString STORE; +extern IString GETTER; +extern IString SETTER; extern IStringSet keywords; @@ -207,8 +209,8 @@ template<class NodeRef, class Builder> class Parser { }; struct Frag { - // MSVC does not allow unrestricted unions: - // http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2544.pdf + // MSVC does not allow unrestricted unions: + // http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2544.pdf #ifndef _MSC_VER union { #endif diff --git a/src/emscripten-optimizer/simple_ast.h b/src/emscripten-optimizer/simple_ast.h index 7644daac3..64db04d79 100644 --- a/src/emscripten-optimizer/simple_ast.h +++ b/src/emscripten-optimizer/simple_ast.h @@ -1463,12 +1463,24 @@ struct JSPrinter { newline(); } bool needQuote = false; + const char* getterSetter = nullptr; + const char* setterParam = nullptr; const char* str; if (args[i][0]->isArray()) { - assert(args[i][0][0] == STRING); - // A quoted string. - needQuote = true; - str = args[i][0][1]->getCString(); + if (args[i][0][0] == STRING) { + // A quoted string. + needQuote = true; + str = args[i][0][1]->getCString(); + } else if (args[i][0][0] == GETTER) { + getterSetter = GETTER.c_str(); + str = args[i][0][1]->getCString(); + } else if (args[i][0][0] == SETTER) { + getterSetter = SETTER.c_str(); + str = args[i][0][1]->getCString(); + setterParam = args[i][0][2]->getCString(); + } else { + abort(); + } } else { // Just a raw string, no quotes. str = args[i][0]->getCString(); @@ -1481,6 +1493,10 @@ struct JSPrinter { } check++; } + if (getterSetter != nullptr) { + emit(getterSetter); + space(); + } if (needQuote) { emit('"'); } @@ -1488,7 +1504,15 @@ struct JSPrinter { if (needQuote) { emit('"'); } - emit(":"); + if (getterSetter != nullptr) { + emit('('); + if (setterParam != nullptr) { + emit(setterParam); + } + emit(')'); + } else { + emit(":"); + } space(); print(args[i][1]); } @@ -1828,6 +1852,26 @@ public: &makeRawArray(2)->push_back(makeString(key)).push_back(value)); } + static void appendToObjectAsGetter(Ref array, IString key, Ref value) { + assert(array[0] == OBJECT); + array[1]->push_back(&makeRawArray(2) + ->push_back(&makeRawArray(2) + ->push_back(makeRawString(GETTER)) + .push_back(makeRawString(key))) + .push_back(value)); + } + + static void + appendToObjectAsSetter(Ref array, IString key, IString param, Ref value) { + assert(array[0] == OBJECT); + array[1]->push_back(&makeRawArray(2) + ->push_back(&makeRawArray(3) + ->push_back(makeRawString(SETTER)) + .push_back(makeRawString(key)) + .push_back(makeRawString(param))) + .push_back(value)); + } + static Ref makeSub(Ref obj, Ref index) { return &makeRawArray(2) ->push_back(makeRawString(SUB)) |