11'use strict'
22
3- const { DefaultDeserializer } = require ( 'v8' )
4-
5- /**@typedef {import("../../domain").ModelSpecification } ModelSpecification */
6- /**@typedef {import("../../domain").Model } Model */
7- /**@typedef {{[x:string]:()=>void} } Service */
8- /**@typedef {function(Service):function(*):Promise } Adapter*/
3+ /** @typedef {import("../../domain").ModelSpecification } ModelSpecification */
4+ /** @typedef {import("../../domain").Model } Model */
5+ /** @typedef {{[x:string]:()=>void} } Service */
6+ /** @typedef {function(Service):function(*):Promise } Adapter */
97
108/**
119 * WASM interop functions
@@ -21,39 +19,79 @@ exports.WasmInterop = function (wasmExports) {
2119 const {
2220 getCommands,
2321 getPorts,
24- __callWasmFunction,
25- ArrayOfStrings_ID,
26- __pin,
27- __unpin,
2822 liftString,
29- liftArray
23+ liftArray,
24+ lowerString,
25+ lowerArray,
26+ store_ref,
27+ notnull,
28+ memory,
29+ callExportedFn
3030 } = wasmExports
3131
32- /**
33- * Construct an object from the key-value pairs in the multidimensional array
34- * @param {number } ptr - pointer to the address of the array of string arrays
35- * @returns {Readonly<{object}> }
36- */
37- function constructObject ( ptr , unpin = true ) {
38- if ( ! ptr ) {
39- console . debug ( constructObject . name , 'null pointer' , ptr )
40- return
41- }
32+ function lift ( fn , kv ) {
33+ return liftArray (
34+ pointer =>
35+ liftArray (
36+ pointer => liftString ( new Uint32Array ( memory . buffer ) [ pointer >>> 2 ] ) ,
37+ 2 ,
38+ new Uint32Array ( memory . buffer ) [ pointer >>> 2 ]
39+ ) ,
40+ 2 ,
41+ callExportedFn ( fn , kv ) >>> 0
42+ )
43+ . map ( ( [ k , v ] ) => ( { [ k ] : v } ) )
44+ . reduce ( ( a , b ) => ( { ...a , ...b } ) )
45+ }
4246
43- try {
44- const obj = liftArray ( ptr )
45- . map ( inner => liftArray ( inner ) )
46- . map ( tuple => ( { [ liftString ( tuple [ 0 ] ) ] : liftString ( tuple [ 1 ] ) } ) )
47- . reduce ( ( obj1 , obj2 ) => ( { ...obj1 , ...obj2 } ) )
47+ function lower ( entries ) {
48+ return (
49+ lowerArray (
50+ ( pointer , value ) => {
51+ store_ref (
52+ pointer ,
53+ lowerArray (
54+ ( pointer , value ) => {
55+ store_ref ( pointer , lowerString ( value . toString ( ) ) || notnull ( ) )
56+ } ,
57+ 4 ,
58+ 2 ,
59+ value
60+ ) || notnull ( )
61+ )
62+ } ,
63+ 5 ,
64+ 2 ,
65+ entries
66+ ) || notnull ( )
67+ )
68+ }
4869
49- const immutableClone = Object . freeze ( { ...obj } )
50- ! unpin || __unpin ( ptr )
51- console . debug ( constructObject . name , ptr )
52- return immutableClone
53- } catch ( e ) {
54- console . error ( constructObject . name , 'error:' , e . message )
55- return { }
56- }
70+ /**
71+ * Parse the input object into a multidimensional array of key-value pairs
72+ * and pass it as an argument to the exported wasm function. Do the reverse for
73+ * the response. Consequently, any wasm port or command function must accept a
74+ * multidemensional array of strings (numbers are converted to strings) and return
75+ * a multidimensional array of strings.
76+ *
77+ * Before they can be called, they must be registered in the wasm modelspec,
78+ * that is getPorts() and getCommands() must return appropria metadata.
79+ *
80+ * Notes:
81+ *
82+ * - for the moment, we only support strings and numbers in the input
83+ * and output objects.
84+ *
85+ * - {@link args} can also be a number, in which case, so is the return value.
86+ *
87+ * @param {string } fn exported wasm function name
88+ * @param {object|number } [obj] object or number, see above
89+ * @returns {object|number } object or number, see above
90+ */
91+ function callWasmFunction ( fn , obj ) {
92+ const entries = Object . entries ( obj )
93+ const kv = lower ( entries )
94+ return lift ( fn , kv )
5795 }
5896
5997 return Object . freeze ( {
@@ -64,7 +102,7 @@ exports.WasmInterop = function (wasmExports) {
64102 return {
65103 [ command ] : {
66104 command : model =>
67- __callWasmFunction ( command , {
105+ callWasmFunction ( command , {
68106 ...model ,
69107 modelId : model . getId ( ) ,
70108 modelName : model . getName ( )
@@ -101,16 +139,44 @@ exports.WasmInterop = function (wasmExports) {
101139 type,
102140 consumesEvent,
103141 producesEvent,
104- callback : data => __callWasmFunction ( callback , data ) ,
105- undo : data => __callWasmFunction ( undo , data ) ,
142+ callback : data => callWasmFunction ( callback , data ) ,
143+ undo : data => callWasmFunction ( undo , data ) ,
106144 forward
107145 }
108146 }
109147 } )
110148 . reduce ( ( p , c ) => ( { ...p , ...c } ) )
111149 } ,
150+
112151 constructObject ( ptr ) {
113152 return constructObject ( ptr , false )
114153 }
115154 } )
116155}
156+
157+ /**
158+ * Construct an object from the key-value pairs in the multidimensional array
159+ * @param {number } ptr - pointer to the address of the array of string arrays
160+ * @returns {Readonly<{object}> }
161+ */
162+ // function constructObject (ptr, unpin = true) {
163+ // if (!ptr) {
164+ // console.debug(constructObject.name, 'null pointer', ptr)
165+ // return
166+ // }
167+
168+ // try {
169+ // const obj = liftArray(ptr)
170+ // .map(inner => liftArray(inner))
171+ // .map(tuple => ({ [liftString(tuple[0])]: liftString(tuple[1]) }))
172+ // .reduce((obj1, obj2) => ({ ...obj1, ...obj2 }))
173+
174+ // const immutableClone = Object.freeze({ ...obj })
175+ // !unpin || __unpin(ptr)
176+ // console.debug(constructObject.name, ptr)
177+ // return immutableClone
178+ // } catch (e) {
179+ // console.error(constructObject.name, 'error:', e.message)
180+ // return {}
181+ // }
182+ // }
0 commit comments