diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/binaryen-c.cpp | 29 | ||||
-rw-r--r-- | src/binaryen-c.h | 14 | ||||
-rw-r--r-- | src/js/binaryen.js-post.js | 37 |
3 files changed, 58 insertions, 22 deletions
diff --git a/src/binaryen-c.cpp b/src/binaryen-c.cpp index c2ba7fa6b..f3f21bc15 100644 --- a/src/binaryen-c.cpp +++ b/src/binaryen-c.cpp @@ -2063,7 +2063,7 @@ void BinaryenSetDebugInfo(int on) { std::cout << " BinaryenSetDebugInfo(" << on << ");\n"; } - globalPassOptions.debugInfo = bool(on); + globalPassOptions.debugInfo = on != 0; } void BinaryenModuleRunPasses(BinaryenModuleRef module, const char **passes, BinaryenIndex numPasses) { @@ -2139,6 +2139,33 @@ BinaryenBufferSizes BinaryenModuleWriteWithSourceMap(BinaryenModuleRef module, c return writeModule((Module*)module, output, outputSize, url, sourceMap, sourceMapSize); } +BinaryenModuleAllocateAndWriteResult BinaryenModuleAllocateAndWrite(BinaryenModuleRef module, const char* sourceMapUrl) { + if (tracing) { + std::cout << " // BinaryenModuleAllocateAndWrite(the_module, "; + traceNameOrNULL(sourceMapUrl); + std::cout << ");\n"; + } + + Module* wasm = (Module*)module; + BufferWithRandomAccess buffer(false); + WasmBinaryWriter writer(wasm, buffer, false); + writer.setNamesSection(globalPassOptions.debugInfo); + std::ostringstream os; + if (sourceMapUrl) { + writer.setSourceMap(&os, sourceMapUrl); + } + writer.write(); + void* binary = malloc(buffer.size()); + std::copy_n(buffer.begin(), buffer.size(), static_cast<char*>(binary)); + char* sourceMap = nullptr; + if (sourceMapUrl) { + auto str = os.str(); + sourceMap = (char*)malloc(str.length() + 1); + std::copy_n(str.c_str(), str.length() + 1, sourceMap); + } + return { binary, buffer.size(), sourceMap }; +} + BinaryenModuleRef BinaryenModuleRead(char* input, size_t inputSize) { if (tracing) { std::cout << " // BinaryenModuleRead\n"; diff --git a/src/binaryen-c.h b/src/binaryen-c.h index 9033a6950..87c44db60 100644 --- a/src/binaryen-c.h +++ b/src/binaryen-c.h @@ -707,6 +707,20 @@ typedef struct BinaryenBufferSizes { // @returns how many bytes were written. This will be less than or equal to outputSize BinaryenBufferSizes BinaryenModuleWriteWithSourceMap(BinaryenModuleRef module, const char* url, char* output, size_t outputSize, char* sourceMap, size_t sourceMapSize); +// Result structure of BinaryenModuleAllocateAndWrite. Contained buffers have been allocated +// using malloc() and the user is expected to free() them manually once not needed anymore. +typedef struct BinaryenModuleAllocateAndWriteResult { + void* binary; + size_t binaryBytes; + char* sourceMap; +} BinaryenModuleAllocateAndWriteResult; + +// Serializes a module into binary form, optionally including its source map if +// sourceMapUrl has been specified. Uses the currently set global debugInfo option. +// Differs from BinaryenModuleWrite in that it implicitly allocates appropriate buffers +// using malloc(), and expects the user to free() them manually once not needed anymore. +BinaryenModuleAllocateAndWriteResult BinaryenModuleAllocateAndWrite(BinaryenModuleRef module, const char* sourceMapUrl); + // Deserialize a module from binary form. BinaryenModuleRef BinaryenModuleRead(char* input, size_t inputSize); diff --git a/src/js/binaryen.js-post.js b/src/js/binaryen.js-post.js index 5f1ef84fb..e9b4039f7 100644 --- a/src/js/binaryen.js-post.js +++ b/src/js/binaryen.js-post.js @@ -1160,7 +1160,7 @@ Module['Module'] = function(module) { return Module['_BinaryenModuleOptimize'](module); }; this['optimizeFunction'] = function(func) { - if (typeof func === "string") func = this['getFunction'](func); + if (typeof func === 'string') func = this['getFunction'](func); return Module['_BinaryenFunctionOptimize'](func, module); }; this['runPasses'] = function(passes) { @@ -1171,7 +1171,7 @@ Module['Module'] = function(module) { }); }; this['runPassesOnFunction'] = function(func, passes) { - if (typeof func === "string") func = this['getFunction'](func); + if (typeof func === 'string') func = this['getFunction'](func); return preserveStack(function() { return Module['_BinaryenFunctionRunPasses'](func, module, i32sToStack( passes.map(strToStack) @@ -1184,27 +1184,22 @@ Module['Module'] = function(module) { this['dispose'] = function() { Module['_BinaryenModuleDispose'](module); }; - var MAX = 1024*1024; // TODO: fix this hard-wired limit - var outputBuffer = null; - var sourceMapBuffer = null; this['emitBinary'] = function(sourceMapUrl) { - if (!outputBuffer) outputBuffer = _malloc(MAX); - var bytes = Module['_BinaryenModuleWrite'](module, outputBuffer, MAX); - assert(bytes < MAX, 'FIXME: hardcoded limit on module size'); // we should not use the whole buffer - return new Uint8Array(HEAPU8.subarray(outputBuffer, outputBuffer + bytes)); - }; - this['emitBinaryWithSourceMap'] = function(sourceMapUrl) { - if (!outputBuffer) outputBuffer = _malloc(MAX); - if (!sourceMapBuffer) sourceMapBuffer = _malloc(MAX); return preserveStack(function() { - Module['_BinaryenModuleWriteWithSourceMap'](temp, module, strToStack(sourceMapUrl), outputBuffer, MAX, sourceMapBuffer, MAX); - var outputBytes = HEAPU32[temp >>> 2]; - var sourceMapBytes = HEAPU32[(temp + 1) >>> 2]; - assert(outputBytes < MAX && sourceMapBytes < MAX, 'FIXME: hardcoded limit on module size'); // see above - return { - 'binary': new Uint8Array(HEAPU8.subarray(outputBuffer, outputBuffer + outputBytes)), - 'sourceMap': Pointer_stringify(sourceMapBuffer) - }; + Module['_BinaryenModuleAllocateAndWrite'](temp, module, strToStack(sourceMapUrl)); + var binaryPtr = HEAPU32[ temp >>> 2 ]; + var binaryBytes = HEAPU32[(temp >>> 2) + 1]; + var sourceMapPtr = HEAPU32[(temp >>> 2) + 2]; + try { + var buffer = new Uint8Array(binaryBytes); + buffer.set(HEAPU8.subarray(binaryPtr, binaryPtr + binaryBytes)); + return typeof sourceMapUrl === 'undefined' + ? buffer + : { 'binary': buffer, 'sourceMap': Pointer_stringify(sourceMapPtr) }; + } finally { + _free(binaryPtr); + if (sourceMapPtr) _free(sourceMapPtr); + } }); }; this['interpret'] = function() { |