aboutsummaryrefslogtreecommitdiff
path: root/ext/wasm/tester1.c-pp.js
diff options
context:
space:
mode:
authorstephan <stephan@noemail.net>2022-12-03 13:10:58 +0000
committerstephan <stephan@noemail.net>2022-12-03 13:10:58 +0000
commit09c27a59db621e621e32e4e90aa2b564dc84c574 (patch)
treeaa7e165330c25a02ce29a5e108d788ff3c77f8fc /ext/wasm/tester1.c-pp.js
parent54ac04c8315f8ffc66991cea36d6daa580e8ad8b (diff)
downloadsqlite-09c27a59db621e621e32e4e90aa2b564dc84c574.tar.gz
sqlite-09c27a59db621e621e32e4e90aa2b564dc84c574.zip
Rename wasm.xWrap.resultAdapter() X:free entries to X:dealloc for consistency with wasm.dealloc(). Add an undocumented feature to replace wasm.alloc/dealloc/realloc() with the C-standard allocators (after an allocator misuse led down a several-hour rabbit hole trying to discover a mis-free() violation). Related test updates.
FossilOrigin-Name: d9807656f8a7c2a893d3f68ee5592f44826b8e999ae66f7d9000674b5c1b0207
Diffstat (limited to 'ext/wasm/tester1.c-pp.js')
-rw-r--r--ext/wasm/tester1.c-pp.js52
1 files changed, 34 insertions, 18 deletions
diff --git a/ext/wasm/tester1.c-pp.js b/ext/wasm/tester1.c-pp.js
index 949d2365c..2820555ff 100644
--- a/ext/wasm/tester1.c-pp.js
+++ b/ext/wasm/tester1.c-pp.js
@@ -344,9 +344,20 @@ self.sqlite3InitModule = sqlite3InitModule;
////////////////////////////////////////////////////////////////////
T.g('Basic sanity checks')
- .t("JS wasm-side allocator === sqlite3_malloc()", function(sqlite3){
- T.assert(wasm.alloc.impl === wasm.exports.sqlite3_malloc)
- .assert(wasm.dealloc === wasm.exports.sqlite3_free);
+ .t({
+ name: "JS wasm-side allocator",
+ test: function(sqlite3){
+ if(sqlite3.config.useStdAlloc){
+ warn("Using system allocator. This violates the docs.");
+ T.assert(wasm.alloc.impl === wasm.exports.malloc)
+ .assert(wasm.dealloc === wasm.exports.free)
+ .assert(wasm.realloc.impl === wasm.exports.realloc);
+ }else{
+ T.assert(wasm.alloc.impl === wasm.exports.sqlite3_malloc)
+ .assert(wasm.dealloc === wasm.exports.sqlite3_free)
+ .assert(wasm.realloc.impl === wasm.exports.sqlite3_realloc);
+ }
+ }
})
.t('Namespace object checks', function(sqlite3){
const wasmCtypes = wasm.ctype;
@@ -434,12 +445,14 @@ self.sqlite3InitModule = sqlite3InitModule;
// Check allocation limits and allocator's responses...
T.assert('number' === typeof sqlite3.capi.SQLITE_MAX_ALLOCATION_SIZE);
- const tooMuch = sqlite3.capi.SQLITE_MAX_ALLOCATION_SIZE + 1,
- isAllocErr = (e)=>e instanceof sqlite3.WasmAllocError;
- T.mustThrowMatching(()=>w.alloc(tooMuch), isAllocErr)
- .assert(0 === w.alloc.impl(tooMuch))
- .mustThrowMatching(()=>w.realloc(0, tooMuch), isAllocErr)
- .assert(0 === w.realloc.impl(0, tooMuch));
+ if(!sqlite3.config.useStdAlloc){
+ const tooMuch = sqlite3.capi.SQLITE_MAX_ALLOCATION_SIZE + 1,
+ isAllocErr = (e)=>e instanceof sqlite3.WasmAllocError;
+ T.mustThrowMatching(()=>w.alloc(tooMuch), isAllocErr)
+ .assert(0 === w.alloc.impl(tooMuch))
+ .mustThrowMatching(()=>w.realloc(0, tooMuch), isAllocErr)
+ .assert(0 === w.realloc.impl(0, tooMuch));
+ }
// Check allocFromTypedArray()...
const byteList = [11,22,33]
@@ -553,11 +566,12 @@ self.sqlite3InitModule = sqlite3InitModule;
//log("allocCString()...");
{
- const cstr = w.allocCString("hällo, world");
- const n = w.cstrlen(cstr);
- T.assert(13 === n)
+ const jstr = "hällo, world!";
+ const [cstr, n] = w.allocCString(jstr, true);
+ T.assert(14 === n)
.assert(0===w.getMemValue(cstr+n))
- .assert(chr('d')===w.getMemValue(cstr+n-1));
+ .assert(chr('!')===w.getMemValue(cstr+n-1));
+ w.dealloc(cstr);
}
//log("scopedAlloc() and friends...");
@@ -639,11 +653,13 @@ self.sqlite3InitModule = sqlite3InitModule;
rc = w.xCallWrapped('sqlite3_wasm_enum_json','utf8');
T.assert('string'===typeof rc).assert(rc.length>300);
if(haveWasmCTests()){
- fw = w.xWrap('sqlite3_wasm_test_str_hello', 'utf8:free',['i32']);
- rc = fw(0);
- T.assert('hello'===rc);
- rc = fw(1);
- T.assert(null===rc);
+ if(!sqlite3.config.useStdAlloc){
+ fw = w.xWrap('sqlite3_wasm_test_str_hello', 'utf8:dealloc',['i32']);
+ rc = fw(0);
+ T.assert('hello'===rc);
+ rc = fw(1);
+ T.assert(null===rc);
+ }
if(w.bigIntEnabled){
w.xWrap.resultAdapter('thrice', (v)=>3n*BigInt(v));