77
88/**
99 * WASM interop functions
10- * - find exported functions
11- * - call exported functions
12- * - import command configuration
10+ * - call any exported function
11+ * (no js glue code required)
1312 * - import port configuration
14- * - decode memory addresses
13+ * - import inbound port functions
14+ * - import commands
1515 * @param {WebAssembly.Exports } wasmExports
1616 * @returns adapter functions
1717 */
@@ -29,11 +29,23 @@ 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+
3244 /**
3345 *
3446 * @param {string } fn function name
3547 * @param {string[][] } kv key-value pairs
36- * @returns
48+ * @returns { object }
3749 */
3850 function lift ( fn , kv ) {
3951 return liftArray (
@@ -46,14 +58,14 @@ exports.WasmInterop = function (wasmExports) {
4658 2 ,
4759 _exports [ fn ] ( kv ) >>> 0
4860 )
49- . map ( ( [ k , v ] ) => ( { [ k ] : v } ) )
61+ . map ( ( [ k , v ] ) => ( { [ k ] : parse ( v ) } ) )
5062 . reduce ( ( a , b ) => ( { ...a , ...b } ) )
5163 }
5264
5365 /**
5466 *
5567 * @param {string[][] } kv key-value pairs in a 2 dimensional string array
56- * @returns
68+ * @returns { string[][] }
5769 */
5870 function lower ( kv ) {
5971 return (
@@ -63,7 +75,7 @@ exports.WasmInterop = function (wasmExports) {
6375 pointer ,
6476 lowerArray (
6577 ( pointer , value ) => {
66- store_ref ( pointer , lowerString ( value ?. toString ( ) ) || notnull ( ) )
78+ store_ref ( pointer , lowerString ( value ) || notnull ( ) )
6779 } ,
6880 4 ,
6981 2 ,
@@ -78,10 +90,15 @@ exports.WasmInterop = function (wasmExports) {
7890 )
7991 }
8092
81- function cleanse ( obj ) {
82- return Object . entries ( obj )
83- . filter ( ( [ k , v ] ) => [ 'string' , 'number' , 'boolean' ] . includes ( typeof v ) )
84- . map ( ( [ k , v ] ) => [ k , v . toString ( ) ] )
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 ( ) ] )
98+
99+ // handle custom port format
100+ if ( obj . port && obj . args ) return convert ( obj . args )
101+ return convert ( obj )
85102 }
86103
87104 /**
@@ -98,9 +115,7 @@ exports.WasmInterop = function (wasmExports) {
98115 * @returns {object|number } object
99116 */
100117 function callWasmFunction ( fn , obj ) {
101- const props = cleanse ( obj )
102- const kv = lower ( props )
103- return lift ( fn , kv )
118+ return lift ( fn , lower ( clean ( obj ) ) )
104119 }
105120
106121 return Object . freeze ( {
@@ -135,7 +150,6 @@ exports.WasmInterop = function (wasmExports) {
135150 * wasm function.
136151 */
137152 importWasmPorts ( ) {
138- /** @type {import("../../domain").ports } */
139153 const ports = getPorts ( )
140154 return Object . keys ( ports )
141155 . map ( port => {
@@ -149,32 +163,34 @@ exports.WasmInterop = function (wasmExports) {
149163 inbound
150164 ] = ports [ port ] . split ( ',' )
151165 return {
152- /** @type {import("../../domain").ports[x] } */
153166 [ port ] : {
154167 service,
155168 type,
156169 consumesEvent,
157170 producesEvent,
158171 callback : data => callWasmFunction ( callback , data ) ,
159172 undo : data => callWasmFunction ( undo , data ) ,
160- inbound : function inbound ( port , args , id ) {
173+ inbound ( port , args , id ) {
161174 callWasmFunction ( inbound , { port, ...args , id } )
162175 }
163176 }
164177 }
165178 } )
166- . reduce ( ( p , c ) => ( { ...p , ...c } ) )
179+ . reduce ( ( p , c ) => ( { ...p , ...c } ) , { } )
167180 } ,
168181
182+ /**
183+ *
184+ * @returns {{[x: string]:(x) => any} }
185+ */
169186 importWasmPortFunctions ( ) {
170- const ports = getPorts ( )
171- return Object . values ( ports )
172- . filter ( v => v . inbound )
173- . reduce ( ( a , b ) => [ ...a , ...b ] , [ ] )
187+ return Object . entries ( getPorts ( ) )
188+ . map ( ( [ k , v ] ) => [ k , v . split ( ',' ) [ 1 ] ] )
189+ . filter ( ( [ k , v ] ) => v === 'inbound' )
190+ . map ( ( [ k , v ] ) => ( { [ k ] : x => callWasmFunction ( k , x ) } ) )
191+ . reduce ( ( a , b ) => ( { ...a , ...b } ) , { } )
174192 } ,
175193
176- constructObject ( ptr ) {
177- return constructObject ( ptr , false )
178- }
194+ callWasmFunction
179195 } )
180196}
0 commit comments