@@ -26,83 +26,9 @@ exports.WasmInterop = function (wasmExports) {
2626 __pin,
2727 __unpin,
2828 liftString,
29- lowerString,
30- lowerArray,
31- liftArray,
32- __exports
29+ liftArray
3330 } = wasmExports
3431
35- /**
36- * only strings and numbers in the object are supported for the moment.
37- *
38- * @param {object } args input object
39- * @returns {{keys:string[],vals:string[]} } pointer arrays
40- */
41- function parseArgumentsOld ( args ) {
42- const filtered = Object . entries ( args ) . filter ( ( [ , v ] ) =>
43- [ 'string' , 'number' ] . includes ( typeof v )
44- )
45-
46- const keyPtrs = filtered . map ( ( [ k ] ) => __pin ( lowerString ( k ) ) )
47- const valPtrs = filtered . map ( ( [ , v ] ) => __pin ( lowerString ( v . toString ( ) ) ) )
48-
49- return {
50- keys : keyPtrs ,
51- vals : valPtrs
52- }
53- }
54-
55- function parseArguments ( args ) {
56- const filtered = Object . entries ( args ) . filter ( ( [ , v ] ) =>
57- [ 'string' , 'number' ] . includes ( typeof v )
58- )
59-
60- return filtered . map ( ( [ k , v ] ) => [
61- __pin ( lowerString ( k ) ) ,
62- __pin ( lowerString ( v . toString ( ) ) )
63- ] )
64- }
65-
66- /**
67- * Call the exported function {@link wasmFn} with
68- * two string arrays, one for keys {@link keyVal},
69- * the other for values {@link valPtrs} of a
70- * deconstructed object, or a number {@link num}
71- * or neither.
72- *
73- * @param {{
74- * fn:function(number[],number[]),
75- * keys?:string[],
76- * vals?:string[],
77- * num?:number
78- * }} param0
79- * @returns {string[][]|number|void }
80- */
81- function callExportedFunction ( { fn : wasmFn , entries = [ ] , num = null } ) {
82- if ( typeof num === 'number' ) {
83- return __pin ( wasmFn ( num ) )
84- }
85-
86- if ( keys . length > 0 ) {
87- const keyArrayPtr = __pin ( lowerArray ( ArrayOfStrings_ID , keys ) )
88- const valArrayPtr = __pin ( lowerArray ( ArrayOfStrings_ID , vals ) )
89-
90- // The arrays keep values alive from now on
91- keys . forEach ( __unpin )
92- vals . forEach ( __unpin )
93-
94- // Provide input as two arrays of strings, one for keys, other for values
95- const ptr = __pin ( wasmFn ( keyArrayPtr , valArrayPtr ) )
96-
97- // release arrays
98- __unpin ( keyArrayPtr )
99- __unpin ( valArrayPtr )
100-
101- return ptr
102- }
103- return __pin ( wasmFn ( ) )
104- }
105-
10632 /**
10733 * Construct an object from the key-value pairs in the multidimensional array
10834 * @param {number } ptr - pointer to the address of the array of string arrays
@@ -130,80 +56,15 @@ exports.WasmInterop = function (wasmExports) {
13056 }
13157 }
13258
133- /**
134- * Resolve and check argument types, where `resolve` means
135- * invoke {@link args} if it is a function, then check the return value.
136- * @param {function() } fn - the exported function to call
137- * @param {object|number|function } args - object or number
138- * ...or a function that returns an object or number
139- * @returns
140- */
141- function resolveArguments ( fn , args ) {
142- if ( typeof fn !== 'function' ) {
143- console . warn ( this . callWasmFunction . name , 'not a function' , fn )
144- return null
145- }
146-
147- const resolved = typeof args === 'function' ? args ( ) : args
148- if ( resolved ) {
149- if ( typeof resolved === 'string' ) {
150- return { key : resolved }
151- }
152- if ( ! [ 'number' , 'object' ] . includes ( typeof resolved ) ) {
153- console . warn ( resolveArguments . name , 'invalid argument' , args )
154- return null
155- }
156- return resolved
157- }
158-
159- return { }
160- }
161-
16259 return Object . freeze ( {
163- /**
164- * For any function that accepts and returns an object,
165- * we parse the input object into two string arrays, one for keys,
166- * the other for values, and pass them to the exported function as
167- * arguments. The exported function returns a multidimensional array
168- * of key-value pairs, which we convert to an object and return.
169- *
170- * We can handle objects this way or declare a custom class for each
171- * function - or use the same class if it contains two string arrays :)
172- *
173- * Notes:
174- *
175- * - for the moment, we only support strings and numbers in the input
176- * and output objects. Otherwise, a custom parser is required.
177- *
178- * - {@link args} can also be a number, in which case, so is the return value.
179- *
180- * @param {function() } fn exported wasm function
181- * @param {object|number } [args] object or number, see above
182- * @returns {object|number } object or number, see above
183- */
184- callWasmFunction ( fn , args = { } ) {
185- const resolvedArgs = resolveArguments ( fn , args )
186- if ( ! resolvedArgs ) return
187- // handle numeric arg
188- if ( typeof resolvedArgs === 'number' )
189- return callExportedFunction ( { fn, num : resolvedArgs } )
190- // Parse the object into a couple string arrays, one for keys, the other values
191- const { keys, vals } = parseArguments ( resolvedArgs )
192- // Call the exported function with the key and val arrays
193- const ptr = callExportedFunction ( { fn, keys, vals } )
194- // Construct an object from the key-value pairs array pointer
195- return constructObject ( ptr )
196- } ,
197-
19860 importWasmCommands ( ) {
19961 const commandNames = getCommands ( )
20062 return Object . keys ( commandNames )
20163 . map ( command => {
202- console . log ( { command } )
20364 return {
20465 [ command ] : {
20566 command : model =>
206- __callWasmFunction ( __exports [ command ] , {
67+ __callWasmFunction ( command , {
20768 ...model ,
20869 modelId : model . getId ( ) ,
20970 modelName : model . getName ( )
@@ -233,17 +94,15 @@ exports.WasmInterop = function (wasmExports) {
23394 undo ,
23495 forward
23596 ] = ports [ port ] . split ( ',' )
236- const cb = __exports [ callback ]
237- const undoCb = __exports [ undo ]
23897 return {
23998 /**@type {import("../../domain").ports[x] } */
24099 [ port ] : {
241100 service,
242101 type,
243102 consumesEvent,
244103 producesEvent,
245- callback : data => __callWasmFunction ( cb , data ) ,
246- undo : data => __callWasmFunction ( undoCb , data ) ,
104+ callback : data => __callWasmFunction ( callback , data ) ,
105+ undo : data => __callWasmFunction ( undo , data ) ,
247106 forward
248107 }
249108 }
0 commit comments