aboutsummaryrefslogtreecommitdiff
path: root/ext/wasm/common/whwasmutil.js
diff options
context:
space:
mode:
Diffstat (limited to 'ext/wasm/common/whwasmutil.js')
-rw-r--r--ext/wasm/common/whwasmutil.js30
1 files changed, 27 insertions, 3 deletions
diff --git a/ext/wasm/common/whwasmutil.js b/ext/wasm/common/whwasmutil.js
index 38adc76c2..f72fba72c 100644
--- a/ext/wasm/common/whwasmutil.js
+++ b/ext/wasm/common/whwasmutil.js
@@ -212,9 +212,10 @@ self.WhWasmUtilInstaller = function(target){
that will certainly change.
*/
const ptrIR = target.pointerIR || 'i32';
- const ptrSizeof = ('i32'===ptrIR ? 4
- : ('i64'===ptrIR
- ? 8 : toss("Unhandled ptrSizeof:",ptrIR)));
+ const ptrSizeof = target.ptrSizeof =
+ ('i32'===ptrIR ? 4
+ : ('i64'===ptrIR
+ ? 8 : toss("Unhandled ptrSizeof:",ptrIR)));
/** Stores various cached state. */
const cache = Object.create(null);
/** Previously-recorded size of cache.memory.buffer, noted so that
@@ -1022,6 +1023,29 @@ self.WhWasmUtilInstaller = function(target){
target.scopedAlloc, 'scopedAllocCString()');
/**
+ Creates an array, using scopedAlloc(), suitable for passing to a
+ C-level main() routine. The input is a collection with a length
+ property and a forEach() method. A block of memory list.length
+ entries long is allocated and each pointer-sized block of that
+ memory is populated with a scopedAllocCString() conversion of the
+ (''+value) of each element. Returns a pointer to the start of the
+ list, suitable for passing as the 2nd argument to a C-style
+ main() function.
+
+ Throws if list.length is falsy or scopedAllocPush() is not active.
+ */
+ target.scopedAllocMainArgv = function(list){
+ if(!list.length) toss("Cannot allocate empty array.");
+ const pList = target.scopedAlloc(list.length * target.ptrSizeof);
+ let i = 0;
+ list.forEach((e)=>{
+ target.setPtrValue(pList + (target.ptrSizeof * i++),
+ target.scopedAllocCString(""+e));
+ });
+ return pList;
+ };
+
+ /**
Wraps function call func() in a scopedAllocPush() and
scopedAllocPop() block, such that all calls to scopedAlloc() and
friends from within that call will have their memory freed