diff options
author | Alon Zakai <alonzakai@gmail.com> | 2016-10-11 16:15:15 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-10-11 16:15:15 -0700 |
commit | 85900965a12a3f07c9cca8ef620d4bee039f16fc (patch) | |
tree | 184203f372600d547705ce3d11b616d52ad522aa /src | |
parent | 1dbdfff8e997f74154dfebce124756e415aa431a (diff) | |
parent | 943fd287247f9d23d463a24e8eb4b0f666900c43 (diff) | |
download | binaryen-85900965a12a3f07c9cca8ef620d4bee039f16fc.tar.gz binaryen-85900965a12a3f07c9cca8ef620d4bee039f16fc.tar.bz2 binaryen-85900965a12a3f07c9cca8ef620d4bee039f16fc.zip |
Merge pull request #757 from WebAssembly/js-api
Tiny fixes for native wasm support
Diffstat (limited to 'src')
-rw-r--r-- | src/asm2wasm.h | 5 | ||||
-rw-r--r-- | src/js/wasm.js-post.js | 17 | ||||
-rw-r--r-- | src/passes/Print.cpp | 2 | ||||
-rw-r--r-- | src/wasm-binary.h | 36 |
4 files changed, 32 insertions, 28 deletions
diff --git a/src/asm2wasm.h b/src/asm2wasm.h index b2e7f5512..9b503c84d 100644 --- a/src/asm2wasm.h +++ b/src/asm2wasm.h @@ -664,6 +664,10 @@ void Asm2WasmBuilder::processAsm(Ref ast) { }, debug, false /* do not validate globally yet */); } + // if we see no function tables in the processing below, then the table still exists and has size 0 + + wasm.table.initial = wasm.table.max = 0; + // first pass - do almost everything, but function imports and indirect calls for (unsigned i = 1; i < body->size(); i++) { @@ -784,7 +788,6 @@ void Asm2WasmBuilder::processAsm(Ref ast) { // TODO: when not using aliasing function pointers, we could merge them by noticing that // index 0 in each table is the null func, and each other index should only have one // non-null func. However, that breaks down when function pointer casts are emulated. - wasm.table.exists = true; if (wasm.table.segments.size() == 0) { wasm.table.segments.emplace_back(wasm.allocator.alloc<Const>()->set(Literal(uint32_t(0)))); } diff --git a/src/js/wasm.js-post.js b/src/js/wasm.js-post.js index e7d461642..f093ff64d 100644 --- a/src/js/wasm.js-post.js +++ b/src/js/wasm.js-post.js @@ -223,13 +223,6 @@ function integrateWasmJS(Module) { env['memory'] = providedBuffer; assert(env['memory'] instanceof ArrayBuffer); - if (!('memoryBase' in env)) { - env['memoryBase'] = STATIC_BASE; // tell the memory segments where to place themselves - } - if (!('tableBase' in env)) { - env['tableBase'] = 0; // tell the memory segments where to place themselves - } - wasmJS['providedTotalMemory'] = Module['buffer'].byteLength; // Prepare to generate wasm, using either asm2wasm or s-exprs @@ -290,13 +283,21 @@ function integrateWasmJS(Module) { // import table if (!env['table']) { - var TABLE_SIZE = 1024; // TODO + var TABLE_SIZE = Module['wasmTableSize']; + if (TABLE_SIZE === undefined) TABLE_SIZE = 1024; // works in binaryen interpreter at least if (typeof WebAssembly === 'object' && typeof WebAssembly.Table === 'function') { env['table'] = new WebAssembly.Table({ initial: TABLE_SIZE, maximum: TABLE_SIZE, element: 'anyfunc' }); } else { env['table'] = new Array(TABLE_SIZE); // works in binaryen interpreter at least } } + + if (!env['memoryBase']) { + env['memoryBase'] = STATIC_BASE; // tell the memory segments where to place themselves + } + if (!env['tableBase']) { + env['tableBase'] = 0; // table starts at 0 by default, in dynamic linking this will change + } // try the methods. each should return the exports if it succeeded diff --git a/src/passes/Print.cpp b/src/passes/Print.cpp index 33b5f5d16..075d013e5 100644 --- a/src/passes/Print.cpp +++ b/src/passes/Print.cpp @@ -615,7 +615,7 @@ struct PrintSExpression : public Visitor<PrintSExpression> { void printTableHeader(Table* curr) { printOpening(o, "table") << ' '; o << curr->initial; - if (curr->max && curr->max != Table::kMaxSize) o << ' ' << curr->max; + if (curr->max != Table::kMaxSize) o << ' ' << curr->max; o << " anyfunc)"; } void visitTable(Table *curr) { diff --git a/src/wasm-binary.h b/src/wasm-binary.h index eef315fda..43a43b5c4 100644 --- a/src/wasm-binary.h +++ b/src/wasm-binary.h @@ -558,11 +558,11 @@ public: return ret; } - void writeResizableLimits(Address initial, Address maximum) { - uint32_t flags = maximum ? 1 : 0; + void writeResizableLimits(Address initial, Address maximum, bool hasMaximum) { + uint32_t flags = hasMaximum ? 1 : 0; o << U32LEB(flags); o << U32LEB(initial); - if (flags) { + if (hasMaximum) { o << U32LEB(maximum); } } @@ -590,8 +590,7 @@ public: if (debug) std::cerr << "== writeMemory" << std::endl; auto start = startSection(BinaryConsts::Section::Memory); o << U32LEB(1); // Define 1 memory - Address max = wasm->memory.max == Memory::kMaxSize ? Address(0) : wasm->memory.max; - writeResizableLimits(wasm->memory.initial, max); + writeResizableLimits(wasm->memory.initial, wasm->memory.max, wasm->memory.max != Memory::kMaxSize); finishSection(start); } @@ -639,13 +638,12 @@ public: case ExternalKind::Function: o << U32LEB(getFunctionTypeIndex(import->functionType->name)); break; case ExternalKind::Table: { o << U32LEB(BinaryConsts::ElementType::AnyFunc); - auto max = wasm->table.max == Table::kMaxSize ? Address(0) : wasm->table.max; - writeResizableLimits(wasm->table.initial, max); + writeResizableLimits(wasm->table.initial, wasm->table.max, wasm->table.max != Table::kMaxSize); break; } case ExternalKind::Memory: { - auto max = wasm->memory.max == Memory::kMaxSize ? Address(0) : wasm->memory.max; - writeResizableLimits(wasm->memory.initial, max); break; + writeResizableLimits(wasm->memory.initial, wasm->memory.max, wasm->memory.max != Memory::kMaxSize); + break; } case ExternalKind::Global: o << binaryWasmType(import->globalType); @@ -850,8 +848,7 @@ public: auto start = startSection(BinaryConsts::Section::Table); o << U32LEB(1); // Declare 1 table. o << U32LEB(BinaryConsts::ElementType::AnyFunc); - Address max = wasm->table.max == Table::kMaxSize ? Address(0) : wasm->table.max; - writeResizableLimits(wasm->table.initial, max); + writeResizableLimits(wasm->table.initial, wasm->table.max, wasm->table.max != Table::kMaxSize); finishSection(start); } @@ -1562,7 +1559,7 @@ public: auto numMemories = getU32LEB(); if (!numMemories) return; assert(numMemories == 1); - getResizableLimits(wasm.memory.initial, &wasm.memory.max); + getResizableLimits(wasm.memory.initial, wasm.memory.max, Memory::kMaxSize); } void readSignatures() { @@ -1606,12 +1603,12 @@ public: } } - void getResizableLimits(Address& initial, Address* max) { + void getResizableLimits(Address& initial, Address& max, Address defaultIfNoMax) { auto flags = getU32LEB(); initial = getU32LEB(); bool hasMax = flags & 0x1; - assert(max || !hasMax); - if (hasMax) *max = getU32LEB(); + if (hasMax) max = getU32LEB(); + else max = defaultIfNoMax; } void readImports() { @@ -1640,10 +1637,13 @@ public: if (elementType != BinaryConsts::ElementType::AnyFunc) throw ParseException("Imported table type is not AnyFunc"); wasm.table.exists = true; wasm.table.imported = true; - getResizableLimits(wasm.table.initial, &wasm.table.max); + getResizableLimits(wasm.table.initial, wasm.table.max, Table::kMaxSize); + break; + } + case ExternalKind::Memory: { + getResizableLimits(wasm.memory.initial, wasm.memory.max, Memory::kMaxSize); break; } - case ExternalKind::Memory: getResizableLimits(wasm.memory.initial, &wasm.memory.max); break; case ExternalKind::Global: { curr->globalType = getWasmType(); auto globalMutable = getU32LEB(); @@ -1897,7 +1897,7 @@ public: wasm.table.exists = true; auto elemType = getU32LEB(); if (elemType != BinaryConsts::ElementType::AnyFunc) throw ParseException("ElementType must be AnyFunc in MVP"); - getResizableLimits(wasm.table.initial, &wasm.table.max); + getResizableLimits(wasm.table.initial, wasm.table.max, Table::kMaxSize); } void readTableElements() { |