@@ -29,18 +29,6 @@ exports.WasmInterop = function (wasmExports) {
2929 _exports
3030 } = wasmExports
3131
32- function parse ( v ) {
33- return ! isNaN ( parseInt ( v ) )
34- ? parseInt ( v )
35- : ! isNaN ( parseFloat ( v ) )
36- ? parseFloat ( v )
37- : / t r u e / i. test ( v )
38- ? true
39- : / f a l s e / i. test ( v )
40- ? false
41- : v
42- }
43-
4432 /**
4533 *
4634 * @param {string } fn function name
@@ -58,8 +46,6 @@ exports.WasmInterop = function (wasmExports) {
5846 2 ,
5947 _exports [ fn ] ( kv ) >>> 0
6048 )
61- . map ( ( [ k , v ] ) => ( { [ k ] : parse ( v ) } ) )
62- . reduce ( ( a , b ) => ( { ...a , ...b } ) )
6349 }
6450
6551 /**
@@ -90,39 +76,81 @@ exports.WasmInterop = function (wasmExports) {
9076 )
9177 }
9278
93- function clean ( obj ) {
94- const convert = obj =>
95- Object . entries ( obj )
96- . filter ( ( [ k , v ] ) => [ 'string' , 'number' , 'boolean' ] . includes ( typeof v ) )
97- . map ( ( [ k , v ] ) => [ k , v . toString ( ) ] )
79+ /**
80+ *
81+ * @param {string } v - value
82+ * @returns {string|number|boolean }
83+ */
84+ function parseString ( v ) {
85+ return ! isNaN ( parseFloat ( v ) )
86+ ? parseFloat ( v )
87+ : / ^ t r u e $ / i. test ( v )
88+ ? true
89+ : / ^ f a l s e $ / i. test ( v )
90+ ? false
91+ : v
92+ }
93+
94+ /**
95+ *
96+ * @param {object } o
97+ * @returns {string[][] }
98+ */
99+ function parseObject ( o ) {
100+ return Object . entries ( o )
101+ . filter ( ( [ k , v ] ) => [ 'string' , 'number' , 'boolean' ] . includes ( typeof v ) )
102+ . map ( ( [ k , v ] ) => [ k , v . toString ( ) ] )
103+ }
98104
105+ /**
106+ *
107+ * @param {object } obj
108+ * @returns {string[][] }
109+ */
110+ function toKeyValueArray ( obj ) {
99111 // handle custom port format
100- if ( obj . port && obj . args ) return convert ( obj . args )
101- return convert ( obj )
112+ if ( obj . port && obj . args ) return parseObject ( obj . args )
113+ return parseObject ( obj )
102114 }
103115
104116 /**
105- * Parse the input object into a multidimensional array of key-value pairs
106- * and pass it as an argument to the exported wasm function. Do the reverse for
107- * the response. Consequently, any wasm port or command function must accept a
108- * multidemensional array of strings (numbers are converted to strings) and return
109- * a multidimensional array of strings. Before they can be called, they must be
110- * registered in a modelspec, i.e. getPorts() and getCommands() must return
111- * appropria metadata.
117+ *
118+ * @param {string[][] } kv
119+ * @returns {object }
120+ */
121+ function fromKeyValueArray ( kv ) {
122+ return kv
123+ . map ( ( [ k , v ] ) => ( { [ k ] : parseString ( v ) } ) )
124+ . reduce ( ( a , b ) => ( { ...a , ...b } ) )
125+ }
112126
113- * @param {string } fn exported wasm function name
114- * @param {object|number } [obj] object, see above
115- * @returns {object|number } object
127+ /**
128+ * Parse the input object into a multidimensional array of key-value string pairs
129+ * and pass it as an argument to the exported wasm function. Do the reverse for
130+ * the return value. The interface requires that any wasm port or command function
131+ * accept a multidemensional array of strings (numbers and booleans are converted
132+ * to strings) and return a multidimensional array of strings. These functions must
133+ * be defined in the ModelSpec, i.e. `getPorts()` and `getCommands()` list the names
134+ * of functions to be exported from the wasm module that implement the interface,
135+ * ```js
136+ * (string[][]) => string[][]
137+ * ```
138+ * or
139+ * ```js
140+ * () => string[][]
141+ *```
142+ * @param {string } fn name of exported function
143+ * @param {object } [obj] object; see above
144+ * @returns {object } object
116145 */
117146 function callWasmFunction ( fn , obj ) {
118- return lift ( fn , lower ( clean ( obj ) ) )
147+ return fromKeyValueArray ( lift ( fn , lower ( toKeyValueArray ( obj ) ) ) )
119148 }
120149
121150 return Object . freeze ( {
122151 /**
123152 * For every command in {@link getCommands} create a
124- * an entry that will invoke one or more of the exported
125- * wasm functions.
153+ * an entry that will invoke the exported wasm function.
126154 */
127155 importWasmCommands ( ) {
128156 const commandNames = getCommands ( )
@@ -144,10 +172,9 @@ exports.WasmInterop = function (wasmExports) {
144172 } ,
145173
146174 /**
147- * For every port in {@link getPorts} create a
148- * an entry that in the {@link ModelSpecification.ports}
149- * that will invoke one or more of the exported
150- * wasm functions.
175+ * For every `port` in {@link getPorts} create a
176+ * an entry in {@link ModelSpecification.ports}
177+ * that will invoke the exported wasm function.
151178 */
152179 importWasmPorts ( ) {
153180 const ports = getPorts ( )
0 commit comments