Skip to content

Commit 91b0cf3

Browse files
fix(ext/node): move sqlite_extension into separate module from sqlite_extension_test to permit loadable_extension feature
This allows the rusqlite `loadable_extension` feature to be used -- without it, we were building an extension that triggered a segfault on load (because it wasn't using the pointer provided during extension load to access the previously-initialized sqlite library instance). Because that feature conflicts with other features used _outside_ the `sqlite_extension_test` module, we split off to a separate workspace. The test harness stays in `sqlite_extension_test`, as it depends on other parts of the workspace.
1 parent 9badb5d commit 91b0cf3

File tree

10 files changed

+412
-85
lines changed

10 files changed

+412
-85
lines changed

Cargo.lock

Lines changed: 7 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ members = [
5151
"tests/sqlite_extension_test",
5252
"tests/util/server",
5353
]
54-
exclude = ["tests/util/std/hash/_wasm"]
54+
# tests/sqlite_extension is excluded because it requires rusqlite's "loadable_extension"
55+
# feature, which is incompatible with the "session" feature used by the rest of the workspace.
56+
exclude = ["tests/util/std/hash/_wasm", "tests/sqlite_extension"]
5557

5658
[workspace.package]
5759
authors = ["the Deno authors"]

tests/sqlite_extension/Cargo.lock

Lines changed: 142 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/sqlite_extension/Cargo.toml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright 2018-2025 the Deno authors. MIT license.
2+
3+
# This crate is excluded from the workspace because it requires rusqlite's
4+
# "loadable_extension" feature, which is incompatible with the "session" feature
5+
# used by the rest of the workspace. Build with:
6+
# cargo build --manifest-path tests/sqlite_extension/Cargo.toml
7+
8+
[package]
9+
name = "test_sqlite_extension"
10+
version = "0.1.0"
11+
authors = ["the Deno authors"]
12+
edition = "2024"
13+
license = "MIT"
14+
publish = false
15+
repository = "https://github.com/denoland/deno"
16+
17+
[lib]
18+
crate-type = ["cdylib"]
19+
20+
[workspace]
21+
22+
[dependencies]
23+
rusqlite = { version = "0.37.0", default-features = false, features = ["loadable_extension", "functions"] }

tests/sqlite_extension/src/lib.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2018-2025 the Deno authors. MIT license.
2+
3+
//! A simple SQLite loadable extension for testing.
4+
//!
5+
//! This extension provides a `test_func(x)` function that returns its argument unchanged.
6+
7+
use std::os::raw::c_char;
8+
use std::os::raw::c_int;
9+
10+
use rusqlite::Connection;
11+
use rusqlite::Result;
12+
use rusqlite::ffi;
13+
use rusqlite::functions::FunctionFlags;
14+
use rusqlite::types::ToSqlOutput;
15+
use rusqlite::types::Value;
16+
17+
/// Entry point for SQLite to load the extension.
18+
#[expect(clippy::not_unsafe_ptr_arg_deref)]
19+
#[unsafe(no_mangle)]
20+
pub unsafe extern "C" fn sqlite3_extension_init(
21+
db: *mut ffi::sqlite3,
22+
pz_err_msg: *mut *mut c_char,
23+
p_api: *mut ffi::sqlite3_api_routines,
24+
) -> c_int {
25+
// SAFETY: This function is called by SQLite with valid pointers.
26+
// extension_init2 handles the API initialization.
27+
unsafe { Connection::extension_init2(db, pz_err_msg, p_api, extension_init) }
28+
}
29+
30+
fn extension_init(db: Connection) -> Result<bool> {
31+
db.create_scalar_function(
32+
"test_func",
33+
1,
34+
FunctionFlags::SQLITE_DETERMINISTIC,
35+
|ctx| {
36+
// Return the argument value unchanged
37+
let value: Value = ctx.get(0)?;
38+
Ok(ToSqlOutput::Owned(value))
39+
},
40+
)?;
41+
Ok(false)
42+
}

0 commit comments

Comments
 (0)