Rework the internals of how argument/result types are converted to/from wasm in prep for attempting to support automated conversions of function pointer argument types.

FossilOrigin-Name: 58052d8285cbc2aa8c4f8a9f5c2d33ac12014f6a70afbc93bc4ce5b9fd1f9ee5
This commit is contained in:
stephan 2022-12-12 08:16:47 +00:00
parent 81aca2574a
commit b2eb8a5352
3 changed files with 56 additions and 44 deletions

View File

@ -1305,30 +1305,37 @@ self.WhWasmUtilInstaller = function(target){
cache.xWrap = Object.create(null);
const xcv = cache.xWrap.convert = Object.create(null);
/** Map of type names to argument conversion functions. */
cache.xWrap.convert.arg = Object.create(null);
cache.xWrap.convert.arg = new Map;
/** Map of type names to return result conversion functions. */
cache.xWrap.convert.result = Object.create(null);
cache.xWrap.convert.result = new Map;
const xArg = xcv.arg, xResult = xcv.result;
if(target.bigIntEnabled){
xcv.arg.i64 = (i)=>BigInt(i);
xArg.set('i64', (i)=>BigInt(i));
}
xcv.arg.i32 = (i)=>(i | 0);
xcv.arg.i16 = (i)=>((i | 0) & 0xFFFF);
xcv.arg.i8 = (i)=>((i | 0) & 0xFF);
xcv.arg.f32 = xcv.arg.float = (i)=>Number(i).valueOf();
xcv.arg.f64 = xcv.arg.double = xcv.arg.f32;
xcv.arg.int = xcv.arg.i32;
xcv.result['*'] = xcv.result['pointer'] = xcv.arg['**'] = xcv.arg[ptrIR];
xcv.result['number'] = (v)=>Number(v);
xArg.set('i32', (i)=>(i | 0));
xArg.set('i16', (i)=>((i | 0) & 0xFFFF));
xArg.set('i8', (i)=>((i | 0) & 0xFF));
xArg.set('f32', (i)=>Number(i).valueOf());
xArg.set('float', xArg.get('f32'));
xArg.set('f64', xArg.get('f32'));
xArg.set('double', xArg.get('f64'));
xArg.set('int', xArg.get('i32'));
xResult.set('*', xArg.get(ptrIR));
xResult.set('pointer', xArg.get(ptrIR));
xArg.set('**', xArg.get(ptrIR));
xResult.set('number', (v)=>Number(v));
{ /* Copy certain xcv.arg[...] handlers to xcv.result[...] and
{ /* Copy certain xArg[...] handlers to xResult[...] and
add pointer-style variants of them. */
const copyToResult = ['i8', 'i16', 'i32', 'int',
'f32', 'float', 'f64', 'double'];
if(target.bigIntEnabled) copyToResult.push('i64');
const adaptPtr = xArg.get(ptrIR);
for(const t of copyToResult){
xcv.arg[t+'*'] = xcv.result[t+'*'] = xcv.arg[ptrIR];
xcv.result[t] = xcv.arg[t] || toss("Missing arg converter:",t);
xArg.set(t+'*', adaptPtr);
xResult.set(t+'*', adaptPtr);
xResult.set(t, (xArg.get(t) || toss("Missing arg converter:",t)));
}
}
@ -1348,23 +1355,28 @@ self.WhWasmUtilInstaller = function(target){
Would that be too much magic concentrated in one place, ready to
backfire?
*/
xcv.arg.string = xcv.arg.utf8 = xcv.arg['pointer'] = xcv.arg['*']
= function(v){
if('string'===typeof v) return target.scopedAllocCString(v);
return v ? xcv.arg[ptrIR](v) : null;
};
xcv.result.string = xcv.result.utf8 = (i)=>target.cstrToJs(i);
xcv.result['string:dealloc'] = xcv.result['utf8:dealloc'] = (i)=>{
xArg.set('string', function(v){
if('string'===typeof v) return target.scopedAllocCString(v);
return v ? xArg.get(ptrIR)(v) : null;
});
xArg.set('utf8', xArg.get('string'));
xArg.set('pointer', xArg.get('string'));
xArg.set('*', xArg.get('string'));
xResult.set('string', (i)=>target.cstrToJs(i));
xResult.set('utf8', xResult.get('string'));
xResult.set('string:dealloc', (i)=>{
try { return i ? target.cstrToJs(i) : null }
finally{ target.dealloc(i) }
};
xcv.result.json = (i)=>JSON.parse(target.cstrToJs(i));
xcv.result['json:dealloc'] = (i)=>{
});
xResult.set('utf8:dealloc', xResult.get('string:dealloc'));
xResult.set('json', (i)=>JSON.parse(target.cstrToJs(i)));
xResult.set('json:dealloc', (i)=>{
try{ return i ? JSON.parse(target.cstrToJs(i)) : null }
finally{ target.dealloc(i) }
}
xcv.result['void'] = (v)=>undefined;
xcv.result['null'] = (v)=>v;
});
xResult.set('void', (v)=>undefined);
xResult.set('null', (v)=>v);
if(0){
/***
@ -1377,17 +1389,17 @@ self.WhWasmUtilInstaller = function(target){
the value will always be treated like -1 (which has a useful
case in the sqlite3 bindings).
*/
xcv.arg['func-ptr'] = function(v){
if(!(v instanceof Function)) return xcv.arg[ptrIR];
xArg.set('func-ptr', function(v){
if(!(v instanceof Function)) return xArg.get(ptrIR);
const f = target.jsFuncToWasm(v, WHAT_SIGNATURE);
};
});
}
const __xArgAdapterCheck =
(t)=>xcv.arg[t] || toss("Argument adapter not found:",t);
(t)=>xArg.get(t) || toss("Argument adapter not found:",t);
const __xResultAdapterCheck =
(t)=>xcv.result[t] || toss("Result adapter not found:",t);
(t)=>xResult.get(t) || toss("Result adapter not found:",t);
cache.xWrap.convertArg = (t,v)=>__xArgAdapterCheck(t)(v);
cache.xWrap.convertResult =
@ -1576,15 +1588,15 @@ self.WhWasmUtilInstaller = function(target){
/** Internal impl for xWrap.resultAdapter() and argAdapter(). */
const __xAdapter = function(func, argc, typeName, adapter, modeName, xcvPart){
if('string'===typeof typeName){
if(1===argc) return xcvPart[typeName];
if(1===argc) return xcvPart.get(typeName);
else if(2===argc){
if(!adapter){
delete xcvPart[typeName];
delete xcvPart.get(typeName);
return func;
}else if(!(adapter instanceof Function)){
toss(modeName,"requires a function argument.");
}
xcvPart[typeName] = adapter;
xcvPart.set(typeName, adapter);
return func;
}
}
@ -1621,7 +1633,7 @@ self.WhWasmUtilInstaller = function(target){
*/
target.xWrap.resultAdapter = function f(typeName, adapter){
return __xAdapter(f, arguments.length, typeName, adapter,
'resultAdapter()', xcv.result);
'resultAdapter()', xResult);
};
/**
@ -1651,7 +1663,7 @@ self.WhWasmUtilInstaller = function(target){
*/
target.xWrap.argAdapter = function f(typeName, adapter){
return __xAdapter(f, arguments.length, typeName, adapter,
'argAdapter()', xcv.arg);
'argAdapter()', xArg);
};
/**

View File

@ -1,5 +1,5 @@
C Add\ssqlite3.mjs\sto\sthe\snew\s'quick'\swasm\sbuild\sfor\sthe\ssake\sof\sthe\ssnapshot\sbuild.
D 2022-12-12T07:52:22.223
C Rework\sthe\sinternals\sof\show\sargument/result\stypes\sare\sconverted\sto/from\swasm\sin\sprep\sfor\sattempting\sto\ssupport\sautomated\sconversions\sof\sfunction\spointer\sargument\stypes.
D 2022-12-12T08:16:47.837
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@ -521,7 +521,7 @@ F ext/wasm/c-pp.c 92285f7bce67ed7b7020b40fde8ed0982c442b63dc33df9dfd4b658d4a6c07
F ext/wasm/common/SqliteTestUtil.js d8bf97ecb0705a2299765c8fc9e11b1a5ac7f10988bbf375a6558b7ca287067b
F ext/wasm/common/emscripten.css 11bd104b6c0d597c67d40cc8ecc0a60dae2b965151e3b6a37fa5708bac3acd15
F ext/wasm/common/testing.css 0ff15602a3ab2bad8aef2c3bd120c7ee3fd1c2054ad2ace7e214187ae68d926f
F ext/wasm/common/whwasmutil.js 11090ae276ed66150095717ab41c5229b679a66d6fd1b95d40653177d3945cf1
F ext/wasm/common/whwasmutil.js 399918a67a22cab80f3a3f4f1c39eb2f83226953ed19579f2761157532a5c036
F ext/wasm/demo-123-worker.html a0b58d9caef098a626a1a1db567076fca4245e8d60ba94557ede8684350a81ed
F ext/wasm/demo-123.html 8c70a412ce386bd3796534257935eb1e3ea5c581e5d5aea0490b8232e570a508
F ext/wasm/demo-123.js ebae30756585bca655b4ab2553ec9236a87c23ad24fc8652115dcedb06d28df6
@ -2067,8 +2067,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
P f6f3397a4667b15a96bdf4189c54789d622fd90351405e963d7f56ac93a9344c
R d627f5a67dc0aecc2508e72943a45de5
P 5a3f2224c37c7e28ce627bd98a9907a16635bffeea36e8ead707586aad37ccfe
R f46a7095aa975a79509232fc3fdad048
U stephan
Z 0f415e47f3a1eb8e5ab16a6cd254eefd
Z f352fee6fe16045a62c502e094ae567d
# Remove this line to create a well-formed Fossil manifest.

View File

@ -1 +1 @@
5a3f2224c37c7e28ce627bd98a9907a16635bffeea36e8ead707586aad37ccfe
58052d8285cbc2aa8c4f8a9f5c2d33ac12014f6a70afbc93bc4ce5b9fd1f9ee5