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/wasm2js.h | |
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/wasm2js.h')
-rw-r--r-- | src/wasm2js.h | 37 |
1 files changed, 34 insertions, 3 deletions
diff --git a/src/wasm2js.h b/src/wasm2js.h index 43f4d520f..b74572cfe 100644 --- a/src/wasm2js.h +++ b/src/wasm2js.h @@ -764,10 +764,40 @@ void Wasm2JSBuilder::addExports(Ref ast, Module* wasm) { break; } case ExternalKind::Global: { + Ref object = ValueBuilder::makeObject(); + + IString identName = fromName(export_->value, NameScope::Top); + + // getter + { + Ref block = ValueBuilder::makeBlock(); + + block[1]->push_back( + ValueBuilder::makeReturn(ValueBuilder::makeName(identName))); + + ValueBuilder::appendToObjectAsGetter(object, IString("value"), block); + } + + // setter + { + std::ostringstream buffer; + buffer << '_' << identName.c_str(); + auto setterParam = stringToIString(buffer.str()); + + auto block = ValueBuilder::makeBlock(); + + block[1]->push_back( + ValueBuilder::makeBinary(ValueBuilder::makeName(identName), + SET, + ValueBuilder::makeName(setterParam))); + + ValueBuilder::appendToObjectAsSetter( + object, IString("value"), setterParam, block); + } + ValueBuilder::appendToObjectWithQuotes( - exports, - fromName(export_->name, NameScope::Export), - ValueBuilder::makeName(fromName(export_->value, NameScope::Top))); + exports, fromName(export_->name, NameScope::Export), object); + break; } case ExternalKind::Tag: @@ -2600,6 +2630,7 @@ void Wasm2JSGlue::emitPostES6() { for (auto& exp : wasm.exports) { switch (exp->kind) { case ExternalKind::Function: + case ExternalKind::Global: case ExternalKind::Memory: break; |