summaryrefslogtreecommitdiff
path: root/src/asm2wasm.h
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2016-11-17 14:20:43 -0800
committerAlon Zakai <alonzakai@gmail.com>2016-12-07 16:50:04 -1000
commit7bd25106baf71882badec1e03f45f6cae5d31560 (patch)
tree2b0c72dd8ff670d26ab502c4cf89b205aa228552 /src/asm2wasm.h
parent98e9e604c7e2e4f928abe8f05691df90cddf09e4 (diff)
downloadbinaryen-7bd25106baf71882badec1e03f45f6cae5d31560.tar.gz
binaryen-7bd25106baf71882badec1e03f45f6cae5d31560.tar.bz2
binaryen-7bd25106baf71882badec1e03f45f6cae5d31560.zip
support asm.js numeric exports by creating a global and exporting that
Diffstat (limited to 'src/asm2wasm.h')
-rw-r--r--src/asm2wasm.h51
1 files changed, 35 insertions, 16 deletions
diff --git a/src/asm2wasm.h b/src/asm2wasm.h
index 951a0e019..586adaa99 100644
--- a/src/asm2wasm.h
+++ b/src/asm2wasm.h
@@ -831,25 +831,44 @@ void Asm2WasmBuilder::processAsm(Ref ast) {
for (unsigned k = 0; k < contents->size(); k++) {
Ref pair = contents[k];
IString key = pair[0]->getIString();
- assert(pair[1][0] == NAME);
- IString value = pair[1][1]->getIString();
- if (key == Name("_emscripten_replace_memory")) {
- // asm.js memory growth provides this special non-asm function, which we don't need (we use grow_memory)
- assert(!wasm.checkFunction(value));
- continue;
- } else if (key == UDIVMODDI4) {
- udivmoddi4 = value;
- } else if (key == GET_TEMP_RET0) {
- getTempRet0 = value;
- }
- if (exported.count(key) > 0) {
- // asm.js allows duplicate exports, but not wasm. use the last, like asm.js
- exported[key]->value = value;
+ if (pair[1][0] == NAME) {
+ // exporting a function
+ IString value = pair[1][1]->getIString();
+ if (key == Name("_emscripten_replace_memory")) {
+ // asm.js memory growth provides this special non-asm function, which we don't need (we use grow_memory)
+ assert(!wasm.checkFunction(value));
+ continue;
+ } else if (key == UDIVMODDI4) {
+ udivmoddi4 = value;
+ } else if (key == GET_TEMP_RET0) {
+ getTempRet0 = value;
+ }
+ if (exported.count(key) > 0) {
+ // asm.js allows duplicate exports, but not wasm. use the last, like asm.js
+ exported[key]->value = value;
+ } else {
+ auto* export_ = new Export;
+ export_->name = key;
+ export_->value = value;
+ export_->kind = ExternalKind::Function;
+ wasm.addExport(export_);
+ exported[key] = export_;
+ }
} else {
+ // export a number. create a global and export it
+ assert(pair[1][0] == NUM);
+ assert(exported.count(key) == 0);
+ auto value = pair[1][1]->getInteger();
+ auto global = new Global();
+ global->name = key;
+ global->type = i32;
+ global->init = builder.makeConst(Literal(int32_t(value)));
+ global->mutable_ = false;
+ wasm.addGlobal(global);
auto* export_ = new Export;
export_->name = key;
- export_->value = value;
- export_->kind = ExternalKind::Function;
+ export_->value = global->name;
+ export_->kind = ExternalKind::Global;
wasm.addExport(export_);
exported[key] = export_;
}