Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion ext/node/ops/sqlite/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,7 @@ impl StatementSync {
RETURN = "return",
DONE = "done",
VALUE = "value",
__STATEMENT_REF = "__statement_ref",
}

self.reset()?;
Expand Down Expand Up @@ -797,8 +798,19 @@ impl StatementSync {
let names = &[
NEXT.v8_string(scope).unwrap().into(),
RETURN.v8_string(scope).unwrap().into(),
__STATEMENT_REF.v8_string(scope).unwrap().into(),
];
let values = &[next_func.into(), return_func.into()];

// Get the cppgc wrapper object to keep the statement alive
// We store a reference to the statement object on the iterator to prevent
// the GC from collecting it while the iterator is still in use.
let statement_ref = if let Some(args) = params {
args.this().into()
} else {
v8::undefined(scope).into()
};

let values = &[next_func.into(), return_func.into(), statement_ref];
let iterator = v8::Object::with_prototype_and_properties(
scope,
js_iterator_proto,
Expand Down
14 changes: 14 additions & 0 deletions tests/unit_node/sqlite_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1035,3 +1035,17 @@ Deno.test("[node/sqlite] numbered parameters in different order (?2, ?1)", () =>
// SQL puts ?2 in column a, ?1 in column b
assertEquals(row, { a: "second_arg", b: "first_arg", __proto__: null });
});

// https://github.com/denoland/deno/issues/31744
Deno.test("[node/sqlite] StatementSync alive while iterator", () => {
using db = new DatabaseSync(":memory:");
db.exec("create table if not exists t1(id integer primary key)");
const p = db.prepare(`insert into t1(id) values (?)`);
for (let id = 1; id <= 1_000_000; id++) {
p.run(id);
}
const iter = db.prepare(`select id from t1`).iterate();
for (const { id } of iter) {
assertEquals(typeof id, "number");
}
});
Loading