diff --git a/crates/wasm-rquickjs/skeleton/src/builtin/sqlite.js b/crates/wasm-rquickjs/skeleton/src/builtin/sqlite.js index aea0b653f..90f7b76fd 100644 --- a/crates/wasm-rquickjs/skeleton/src/builtin/sqlite.js +++ b/crates/wasm-rquickjs/skeleton/src/builtin/sqlite.js @@ -124,12 +124,23 @@ class _DatabaseSyncImpl { err.code = 'ERR_INVALID_ARG_TYPE'; throw err; } - const stmtOptions = { - readBigInts: options.readBigInts ?? this.#options.readBigInts, - returnArrays: options.returnArrays ?? this.#options.returnArrays, - allowBareNamedParameters: options.allowBareNamedParameters ?? this.#options.allowBareNamedParameters, - allowUnknownNamedParameters: options.allowUnknownNamedParameters ?? this.#options.allowUnknownNamedParameters, - }; + if (options === null || (typeof options !== 'object' && typeof options !== 'function')) { + const err = new TypeError('The "options" argument must be an object.'); + err.code = 'ERR_INVALID_ARG_TYPE'; + throw err; + } + const stmtOptions = {}; + for (const key of ['returnArrays', 'readBigInts', 'allowBareNamedParameters', 'allowUnknownNamedParameters']) { + const value = options[key]; + if (value !== undefined && typeof value !== 'boolean') { + const err = new TypeError(`The "options.${key}" argument must be a boolean.`); + err.code = 'ERR_INVALID_ARG_TYPE'; + throw err; + } + stmtOptions[key] = value === undefined ? this.#options[key] : value; + } + // An option getter can close the database while validation runs. + if (this.#connId === null) throwDbNotOpen('ERR_INVALID_STATE'); return new Statement(_stmtSecret, this.#connId, sql, stmtOptions); } diff --git a/examples/runtime/sqlite/src/sqlite.js b/examples/runtime/sqlite/src/sqlite.js index f42c589d7..cfb10b13b 100644 --- a/examples/runtime/sqlite/src/sqlite.js +++ b/examples/runtime/sqlite/src/sqlite.js @@ -1,6 +1,7 @@ import { DatabaseSync, serializeDatabaseSync, restoreDatabaseSync, isAutocommitDatabaseSync } from 'node:sqlite'; import * as fs from 'node:fs'; import * as path from 'node:path'; +import assert from 'node:assert/strict'; let tmpCounter = 0; @@ -219,3 +220,100 @@ export function testTypeValidation() { return true; } + +export function testPrepareOptions() { + const db = new DatabaseSync(':memory:', { + readBigInts: true, + returnArrays: true, + allowBareNamedParameters: false, + allowUnknownNamedParameters: true, + }); + try { + const keys = ['returnArrays', 'readBigInts', 'allowBareNamedParameters', 'allowUnknownNamedParameters']; + for (const key of keys) { + for (const value of [null, 'true', 1, {}]) { + assert.throws(() => db.prepare('SELECT 7', { [key]: value }), { + name: 'TypeError', code: 'ERR_INVALID_ARG_TYPE', + message: `The "options.${key}" argument must be a boolean.`, + }); + } + } + for (const options of [null, 0, 'options', false]) { + assert.throws(() => db.prepare('SELECT 7', options), { + name: 'TypeError', code: 'ERR_INVALID_ARG_TYPE', + message: 'The "options" argument must be an object.', + }); + } + + const calls = []; + const options = {}; + for (const key of keys) { + Object.defineProperty(options, key, { + get() { calls.push(key); return undefined; }, + }); + } + assert.deepEqual(db.prepare("SELECT 7 AS n, 'pear' AS label", options).get(), [7n, 'pear']); + assert.deepEqual(calls, keys); + for (let i = 0; i < keys.length - 1; i++) { + const options = { + [keys[i]]: null, + get [keys[i + 1]]() { throw new Error('later option must not be read'); }, + }; + assert.throws(() => db.prepare('SELECT 7', options), { + code: 'ERR_INVALID_ARG_TYPE', + message: `The "options.${keys[i]}" argument must be a boolean.`, + }); + } + const sentinel = new Error('option getter'); + assert.throws(() => db.prepare('SELECT 7', { + get readBigInts() { throw sentinel; }, + }), error => error === sentinel); + const unread = { get returnArrays() { throw sentinel; } }; + assert.throws(() => db.prepare(null, unread), { + code: 'ERR_INVALID_ARG_TYPE', message: /The "sql" argument must be a string/, + }); + + for (const options of [undefined, {}, [], () => {}, { readBigInts: undefined, returnArrays: undefined }]) { + assert.deepEqual(db.prepare("SELECT 7 AS n, 'pear' AS label", options).get(), [7n, 'pear']); + } + const query = db.prepare("SELECT 7 AS n, 'pear' AS label", Object.create({ + readBigInts: false, returnArrays: false, + })); + assert.deepEqual(query.get(), { __proto__: null, n: 7, label: 'pear' }); + query.setReadBigInts(true); + query.setReturnArrays(true); + assert.deepEqual(query.get(), [7n, 'pear']); + + const sql = 'SELECT $left AS x, $right AS y'; + assert.deepEqual(db.prepare(sql).get({ left: 13n, right: 29n }), [null, null]); + const named = db.prepare(sql, Object.create({ allowBareNamedParameters: true })); + assert.deepEqual(named.get({ left: 13n, right: 29n, extra: 41n }), [13n, 29n]); + const strict = db.prepare(sql, { + allowBareNamedParameters: true, allowUnknownNamedParameters: false, + }); + assert.throws(() => strict.get({ left: 13n, right: 29n, extra: 41n }), { code: 'ERR_INVALID_STATE' }); + strict.setAllowUnknownNamedParameters(true); + assert.deepEqual(strict.get({ left: 13n, right: 29n, extra: 41n }), [13n, 29n]); + named.setAllowBareNamedParameters(false); + assert.deepEqual(named.get({ left: 13n, right: 29n }), [null, null]); + assert.deepEqual(named.get({ $left: 13n, $right: 29n }), [13n, 29n]); + for (const value of [undefined, null]) { + const closing = new DatabaseSync(':memory:'); + assert.throws(() => closing.prepare('SELECT 7', { + get returnArrays() { closing.close(); return value; }, + }), value === undefined ? { + code: 'ERR_INVALID_STATE', message: 'database is not open', + } : { + code: 'ERR_INVALID_ARG_TYPE', + message: 'The "options.returnArrays" argument must be a boolean.', + }); + } + db.close(); + assert.throws(() => db.prepare(null, unread), { + code: 'ERR_INVALID_STATE', message: 'database is not open', + }); + } finally { + if (db.isOpen) db.close(); + } + return true; +} diff --git a/examples/runtime/sqlite/wit/sqlite.wit b/examples/runtime/sqlite/wit/sqlite.wit index 3a214dd95..32d1e071b 100644 --- a/examples/runtime/sqlite/wit/sqlite.wit +++ b/examples/runtime/sqlite/wit/sqlite.wit @@ -11,4 +11,5 @@ world sqlite { export test-wal-mode-snapshot: func() -> bool; export test-autocommit-tracking: func() -> bool; export test-type-validation: func() -> bool; + export test-prepare-options: func() -> bool; } diff --git a/tests/goldenfiles/generated_types_sqlite_exports.d.ts b/tests/goldenfiles/generated_types_sqlite_exports.d.ts index 8b6465fff..a1077e8c3 100644 --- a/tests/goldenfiles/generated_types_sqlite_exports.d.ts +++ b/tests/goldenfiles/generated_types_sqlite_exports.d.ts @@ -9,4 +9,5 @@ declare module 'sqlite' { export function testWalModeSnapshot(): boolean; export function testAutocommitTracking(): boolean; export function testTypeValidation(): boolean; + export function testPrepareOptions(): boolean; } diff --git a/tests/node_compat/config.jsonc b/tests/node_compat/config.jsonc index 3d9de62df..5acc160b9 100644 --- a/tests/node_compat/config.jsonc +++ b/tests/node_compat/config.jsonc @@ -8590,7 +8590,7 @@ "subtests": { "test_00_named_parameters": {}, "test_01_statement_prototype_setallowunknownnamedparameters": { "category": "known-gap", "reason": "Node 26.13.1 node:sqlite API additions or changed expectations are not implemented yet" }, - "test_02_options_allowunknownnamedparameters": { "category": "known-gap", "reason": "Node 26.13.1 node:sqlite API additions or changed expectations are not implemented yet" } + "test_02_options_allowunknownnamedparameters": { "category": "runnable" } } }, "parallel/test-sqlite-session.js": { @@ -15035,9 +15035,9 @@ "test_12_statement_prototype_all_with_array_output": {}, "test_13_statement_prototype_iterate_with_array_output": { "category": "known-gap", "reason": "Node 26.13.1 node:sqlite API additions or changed expectations are not implemented yet" }, "test_14_statement_prototype_setallowbarenamedparameters": { "category": "known-gap", "reason": "Node 26.13.1 node:sqlite API additions or changed expectations are not implemented yet" }, - "test_15_options_readbigints": { "category": "known-gap", "reason": "Node 26.13.1 node:sqlite API additions or changed expectations are not implemented yet" }, - "test_16_options_returnarrays": { "category": "known-gap", "reason": "Node 26.13.1 node:sqlite API additions or changed expectations are not implemented yet" }, - "test_17_options_allowbarenamedparameters": { "category": "known-gap", "reason": "Node 26.13.1 node:sqlite API additions or changed expectations are not implemented yet" }, + "test_15_options_readbigints": { "category": "runnable" }, + "test_16_options_returnarrays": { "category": "runnable" }, + "test_17_options_allowbarenamedparameters": { "category": "runnable" }, "test_18_statement_prototype_close": { "category": "known-gap", "reason": "Node 26.13.1 node:sqlite API additions or changed expectations are not implemented yet" }, "test_19_statement_prototype_symbol_dispose": { "category": "known-gap", "reason": "Node 26.13.1 node:sqlite API additions or changed expectations are not implemented yet" }, "test_20_options_persistent": { "category": "known-gap", "reason": "Node 26.13.1 node:sqlite API additions or changed expectations are not implemented yet" }, diff --git a/tests/node_compat/report.md b/tests/node_compat/report.md index 5f70741e9..0dc825dc7 100644 --- a/tests/node_compat/report.md +++ b/tests/node_compat/report.md @@ -8,21 +8,21 @@ This report is generated from `config.jsonc` and the pinned vendored Node.js sou Primary compatibility is measured over the public API surface we can provide: CI-enforced passing (`runnable`) plus `known-gap`. WASI-impossible tests, engine differences, unevaluated tests, and Node.js-internals tests are acknowledged separately and excluded from the primary percentage. -**Primary compatibility (CI-enforced):** 3158/4461 (70.8%) +**Primary compatibility (CI-enforced):** 3162/4461 (70.9%) When comparing revisions, read the runnable count and secondary full-public percentage alongside the primary percentage. Reclassifying a test into an excluded category can increase the primary percentage without increasing runnable coverage. | Classification | Count | Primary % | Public inventory % | All listed % | |----------------|-------|-----------|--------------------|--------------| -| ✅ passing (runnable) | 3158 | 70.8% | 32.8% | 29.3% | -| 🧩 known gap | 1303 | 29.2% | 13.5% | 12.1% | +| ✅ passing (runnable) | 3162 | 70.9% | 32.9% | 29.3% | +| 🧩 known gap | 1299 | 29.1% | 13.5% | 12.0% | | 🚫 WASI-impossible (excluded) | 1148 | — | 11.9% | 10.6% | | ⚙️ engine difference (excluded) | 167 | — | 1.7% | 1.5% | | ❔ unevaluated (excluded) | 3841 | — | 39.9% | 35.6% | | 🔒 Node.js internals (excluded) | 1177 | — | — | 10.9% | | **Total** | **10794** | | | **100.0%** | -Secondary full-public compatibility, including public tests that are currently excluded from primary: **3158/9617 (32.8%)**. +Secondary full-public compatibility, including public tests that are currently excluded from primary: **3162/9617 (32.9%)**. ## Inventory by Module @@ -71,7 +71,7 @@ Secondary full-public compatibility, including public tests that are currently e | shadow_realm | 11 | 0 | 0 | 0 | 11 | 0 | 0 | 0.0% | 0.0% | | signal | 5 | 1 | 0 | 3 | 0 | 0 | 1 | 100.0% | 25.0% | | snapshot | 61 | 0 | 0 | 0 | 57 | 4 | 0 | 0.0% | 0.0% | -| sqlite | 117 | 27 | 32 | 0 | 0 | 58 | 0 | 45.8% | 23.1% | +| sqlite | 117 | 31 | 28 | 0 | 0 | 58 | 0 | 52.5% | 26.5% | | stdio | 23 | 12 | 7 | 3 | 0 | 0 | 1 | 63.2% | 54.5% | | stream | 872 | 686 | 30 | 2 | 0 | 113 | 41 | 95.8% | 82.6% | | string_decoder | 5 | 3 | 0 | 0 | 0 | 2 | 0 | 100.0% | 60.0% | @@ -731,11 +731,11 @@ Secondary full-public compatibility, including public tests that are currently e | `test-sqlite-backup.mjs` | 15 | 0 | 0 | 0 | 0 | 15 | 0 | | `test-sqlite-config.js` | 9 | 0 | 0 | 0 | 0 | 9 | 0 | | `test-sqlite-database.js` | 8 | 3 | 5 | 0 | 0 | 0 | 0 | -| `test-sqlite-named-parameters.js` | 3 | 1 | 2 | 0 | 0 | 0 | 0 | +| `test-sqlite-named-parameters.js` | 3 | 2 | 1 | 0 | 0 | 0 | 0 | | `test-sqlite-options-getter-reentry.js` | 3 | 0 | 0 | 0 | 0 | 3 | 0 | | `test-sqlite-serialize.js` | 2 | 0 | 0 | 0 | 0 | 2 | 0 | | `test-sqlite-session.js` | 28 | 13 | 1 | 0 | 0 | 14 | 0 | -| `test-sqlite-statement.js` | 22 | 4 | 18 | 0 | 0 | 0 | 0 | +| `test-sqlite-statement.js` | 22 | 7 | 15 | 0 | 0 | 0 | 0 | | `test-sqlite-template-tag.js` | 18 | 0 | 0 | 0 | 0 | 18 | 0 | | `test-sqlite-timeout.js` | 2 | 0 | 0 | 0 | 0 | 2 | 0 | | `test-sqlite-typed-array-and-data-view.js` | 2 | 1 | 1 | 0 | 0 | 0 | 0 | @@ -1090,14 +1090,14 @@ Secondary full-public compatibility, including public tests that are currently e ## Classified Non-Runnable Tests -### known gap (1303) +### known gap (1299) | Reason | Count | Example entries | |--------|-------|-----------------| | node:http2 public API is a stub in WebAssembly runtime | 106 | `parallel/test-http2-head-request.js`, `parallel/test-http2-info-headers.js`, `parallel/test-http2-invalidargtypes-errors.js`, ... (+103) | -| Node 26.13.1 node:sqlite API additions or changed expectations are not implemented yet | 30 | `parallel/test-sqlite-custom-functions.js`, `parallel/test-sqlite-data-types.js`, `parallel/test-sqlite-database.js#test_00_database_constructor`, ... (+27) | | requires isolated process.execPath TypeScript entry execution and Node CLI flags; the direct process.features check requires a separate feature-disabled component | 30 | `es-module/test-typescript.mjs#test_00_expect_process_features_typescript_to_be_false_when_no_exper`, `es-module/test-typescript.mjs#test_01_execute_a_typescript_file`, `es-module/test-typescript.mjs#test_02_execute_a_typescript_file_with_imports`, ... (+27) | | requires simulated process.execPath / Node CLI mode support deferred to follow-up PR | 29 | `es-module/test-esm-cjs-load-error-note.mjs`, `es-module/test-esm-detect-ambiguous.mjs#test_00_module_syntax_detection`, `es-module/test-esm-detect-ambiguous.mjs#test_01_wrapping_a_require_of_an_es_module_while_using_abort_on_unca`, ... (+26) | +| Node 26.13.1 node:sqlite API additions or changed expectations are not implemented yet | 26 | `parallel/test-sqlite-custom-functions.js`, `parallel/test-sqlite-data-types.js`, `parallel/test-sqlite-database.js#test_00_database_constructor`, ... (+23) | | requires isolated process.execPath --eval execution, TypeScript input-type flags, and child warning/output/exit capture | 24 | `es-module/test-typescript-eval.mjs#test_00_eval_typescript_esm_syntax`, `es-module/test-typescript-eval.mjs#test_01_eval_typescript_esm_syntax_with_input_type_module`, `es-module/test-typescript-eval.mjs#test_02_eval_typescript_commonjs_syntax`, ... (+21) | | requires isolated process.execPath TypeScript entry execution and Node CLI default-module / require-module flag handling | 24 | `es-module/test-typescript-commonjs.mjs#test_00_require_a_ts_file_with_explicit_extension_succeeds`, `es-module/test-typescript-commonjs.mjs#test_01_eval_require_a_ts_file_with_implicit_extension_fails`, `es-module/test-typescript-commonjs.mjs#test_02_eval_require_a_cts_file_with_implicit_extension_fails`, ... (+21) | | stream edge case not yet handled | 22 | `parallel/test-stream-compose.js#block_17_block_17`, `parallel/test-stream-drop-take.js#block_01_don_t_wait_for_next_item_in_the_original_stream_when_already`, `parallel/test-stream-duplex-from.js#block_17_block_17`, ... (+19) | diff --git a/tests/runtime/sqlite.rs b/tests/runtime/sqlite.rs index 07be7d438..38cd3d341 100644 --- a/tests/runtime/sqlite.rs +++ b/tests/runtime/sqlite.rs @@ -196,3 +196,15 @@ async fn sqlite_test_memory_roundtrip_unoptimized( assert_eq!(r, Some(Val::Bool(true))); Ok(()) } + +#[test] +async fn sqlite_test_prepare_options( + #[tagged_as("sqlite")] compiled_test: &CompiledTest, +) -> anyhow::Result<()> { + let (r, output) = + invoke_and_capture_output(compiled_test.wasm_path(), None, "test-prepare-options", &[]) + .await; + println!("Output:\n{}", output); + assert_eq!(r?, Some(Val::Bool(true))); + Ok(()) +}