1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-08-08 14:02:16 +03:00

Cherry-pick [c4dab53b8ea3401abd] for sqlite3.wasm.xWrap() optimizations.

FossilOrigin-Name: 9b97412d3aa791870016ab3c6f565b6a6afa1764f98e969833aec093b9b29919
This commit is contained in:
stephan
2022-12-23 18:19:28 +00:00
parent 3547e4997f
commit a9e1d96cd8
4 changed files with 184 additions and 104 deletions

View File

@@ -539,7 +539,7 @@ self.sqlite3InitModule = sqlite3InitModule;
}
w.dealloc(m);
}
// isPtr32()
{
const ip = w.isPtr32;
@@ -743,6 +743,65 @@ self.sqlite3InitModule = sqlite3InitModule;
.assert('HI' === cj(new Uint8Array([72, 73])));
});
// jsFuncToWasm()
{
const fsum3 = (x,y,z)=>x+y+z;
fw = w.jsFuncToWasm('i(iii)', fsum3);
T.assert(fw instanceof Function)
.assert( fsum3 !== fw )
.assert( 3 === fw.length )
.assert( 6 === fw(1,2,3) );
T.mustThrowMatching( ()=>w.jsFuncToWasm('x()', function(){}),
'Invalid signature letter: x');
}
// xWrap(Function,...)
{
let fp;
try {
const fmy = function fmy(i,s,d){
if(fmy.debug) log("fmy(",...arguments,")");
T.assert( 3 === i )
.assert( w.isPtr(s) )
.assert( w.cstrToJs(s) === 'a string' )
.assert( T.eqApprox(1.2, d) );
return w.allocCString("hi");
};
fmy.debug = false;
const xwArgs = ['string:dealloc', ['i32', 'string', 'f64']];
fw = w.xWrap(fmy, ...xwArgs);
const fmyArgs = [3, 'a string', 1.2];
let rc = fw(...fmyArgs);
T.assert( 'hi' === rc );
if(0){
/* Retain this as a "reminder to self"...
This extra level of indirection does not work: the
string argument is ending up as a null in fmy() but
the numeric arguments are making their ways through
What's happening is: installFunction() is creating a
WASM-compatible function instance. When we pass a JS string
into there it's getting coerced into `null` before being passed
on to the lower-level wrapper.
*/
fmy.debug = true;
fp = wasm.installFunction('i(isd)', fw);
fw = w.functionEntry(fp);
rc = fw(...fmyArgs);
log("rc =",rc);
T.assert( 'hi' === rc );
// Similarly, this does not work:
//let fpw = w.xWrap(fp, null, [null,null,null]);
//rc = fpw(...fmyArgs);
//log("rc =",rc);
//T.assert( 'hi' === rc );
}
}finally{
wasm.uninstallFunction(fp);
}
}
if(haveWasmCTests()){
if(!sqlite3.config.useStdAlloc){
fw = w.xWrap('sqlite3_wasm_test_str_hello', 'utf8:dealloc',['i32']);
@@ -768,7 +827,7 @@ self.sqlite3InitModule = sqlite3InitModule;
});
}
}
}
}/*xWrap()*/
}/*WhWasmUtil*/)
////////////////////////////////////////////////////////////////////
@@ -997,7 +1056,6 @@ self.sqlite3InitModule = sqlite3InitModule;
P.restore(stack);
}
}/*pstack tests*/)
////////////////////////////////////////////////////////////////////
;/*end of C/WASM utils checks*/