Skip to content

Commit

Permalink
Enable wasm to static link extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
PThorpe92 committed Jan 21, 2025
1 parent cc63aac commit f13d035
Show file tree
Hide file tree
Showing 14 changed files with 26 additions and 68 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 3 additions & 5 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,16 @@ name = "limbo_core"
path = "lib.rs"

[features]
default = ["fs", "json", "uuid", "io_uring", "percentile", "static"]
default = ["fs", "json", "uuid", "io_uring", "percentile"]
fs = []
json = [
"dep:jsonb",
"dep:pest",
"dep:pest_derive",
]
uuid = ["dep:uuid", "limbo_uuid"]
uuid = ["limbo_uuid/static"]
io_uring = ["dep:io-uring", "rustix/io_uring"]
percentile = ["limbo_percentile"]
static = ["limbo_uuid/static", "limbo_percentile/static"]
percentile = ["limbo_percentile/static"]

[target.'cfg(target_os = "linux")'.dependencies]
io-uring = { version = "0.6.1", optional = true }
Expand Down Expand Up @@ -60,7 +59,6 @@ pest_derive = { version = "2.0", optional = true }
rand = "0.8.5"
bumpalo = { version = "3.16.0", features = ["collections", "boxed"] }
limbo_macros = { path = "../macros" }
uuid = { version = "1.11.0", features = ["v4", "v7"], optional = true }
limbo_uuid = { path = "../extensions/uuid", optional = true, features = ["static"] }
limbo_percentile = { path = "../extensions/percentile", optional = true, features = ["static"] }
miette = "7.4.0"
Expand Down
5 changes: 2 additions & 3 deletions core/ext/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl Database {
#[cfg(feature = "uuid")]
pub fn register_uuid(&self) -> Result<(), String> {
let ext_api = Box::new(self.build_limbo_ext());
if unsafe { !::limbo_uuid::register_extension_static(&ext_api).is_ok() } {
if unsafe { !limbo_uuid::register_extension_static(&ext_api).is_ok() } {
return Err("Failed to register uuid extension".to_string());
}
Ok(())
Expand All @@ -94,8 +94,7 @@ impl Database {
#[cfg(feature = "percentile")]
pub fn register_percentile(&self) -> Result<(), String> {
let ext_api = self.build_limbo_ext();
let res = unsafe { ::limbo_percentile::register_extension_static(&ext_api) };
if !res.is_ok() {
if unsafe { !limbo_percentile::register_extension_static(&ext_api).is_ok() } {
return Err("Failed to register percentile extension".to_string());
}
Ok(())
Expand Down
4 changes: 0 additions & 4 deletions core/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
mod error;
#[cfg(not(target_family = "wasm"))]
mod ext;
mod function;
mod io;
Expand All @@ -22,7 +21,6 @@ static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
use fallible_iterator::FallibleIterator;
#[cfg(not(target_family = "wasm"))]
use libloading::{Library, Symbol};
#[cfg(not(target_family = "wasm"))]
use limbo_ext::{ExtensionApi, ExtensionEntryPoint};
use log::trace;
use schema::Schema;
Expand Down Expand Up @@ -139,7 +137,6 @@ impl Database {
_shared_wal: shared_wal.clone(),
syms,
};
#[cfg(not(target_family = "wasm"))]
if let Err(e) = db.register_builtins() {
return Err(LimboError::ExtensionError(e));
}
Expand Down Expand Up @@ -565,7 +562,6 @@ impl SymbolTable {
pub fn new() -> Self {
Self {
functions: HashMap::new(),
// TODO: wasm libs will be very different
#[cfg(not(target_family = "wasm"))]
extensions: Vec::new(),
}
Expand Down
9 changes: 1 addition & 8 deletions core/types.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#[cfg(not(target_family = "wasm"))]
use limbo_ext::{AggCtx, FinalizeFunction, StepFunction};

use crate::error::LimboError;
#[cfg(not(target_family = "wasm"))]
use crate::ext::{ExtValue, ExtValueType};
use crate::storage::sqlite3_ondisk::write_varint;
use crate::Result;
Expand Down Expand Up @@ -75,7 +73,7 @@ impl OwnedValue {
Self::Text(LimboText::new(text))
}
}
#[cfg(not(target_family = "wasm"))]

#[derive(Debug, Clone, PartialEq)]
pub struct ExternalAggState {
pub state: *mut AggCtx,
Expand All @@ -85,7 +83,6 @@ pub struct ExternalAggState {
pub finalized_value: Option<OwnedValue>,
}

#[cfg(not(target_family = "wasm"))]
impl ExternalAggState {
pub fn cache_final_value(&mut self, value: OwnedValue) -> &OwnedValue {
self.finalized_value = Some(value);
Expand Down Expand Up @@ -118,7 +115,6 @@ impl Display for OwnedValue {
}

impl OwnedValue {
#[cfg(not(target_family = "wasm"))]
pub fn to_ffi(&self) -> ExtValue {
match self {
Self::Null => ExtValue::null(),
Expand All @@ -131,7 +127,6 @@ impl OwnedValue {
}
}

#[cfg(not(target_family = "wasm"))]
pub fn from_ffi(v: &ExtValue) -> Self {
match v.value_type() {
ExtValueType::Null => OwnedValue::Null,
Expand Down Expand Up @@ -177,14 +172,12 @@ pub enum AggContext {
Max(Option<OwnedValue>),
Min(Option<OwnedValue>),
GroupConcat(OwnedValue),
#[cfg(not(target_family = "wasm"))]
External(ExternalAggState),
}

const NULL: OwnedValue = OwnedValue::Null;

impl AggContext {
#[cfg(not(target_family = "wasm"))]
pub fn compute_external(&mut self) {
if let Self::External(ext_state) = self {
if ext_state.finalized_value.is_none() {
Expand Down
7 changes: 0 additions & 7 deletions core/vdbe/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ use crate::pseudo::PseudoCursor;
use crate::result::LimboResult;
use crate::storage::sqlite3_ondisk::DatabaseHeader;
use crate::storage::{btree::BTreeCursor, pager::Pager};
#[cfg(not(target_family = "wasm"))]
use crate::types::ExternalAggState;
use crate::types::{AggContext, CursorResult, OwnedRecord, OwnedValue, Record, SeekKey, SeekOp};
use crate::util::parse_schema_rows;
Expand Down Expand Up @@ -1312,8 +1311,6 @@ impl Program {
OwnedValue::build_text(Rc::new("".to_string())),
)))
}

#[cfg(not(target_family = "wasm"))]
AggFunc::External(func) => match func.as_ref() {
ExtFunc::Aggregate {
init,
Expand Down Expand Up @@ -1475,8 +1472,6 @@ impl Program {
*acc += col;
}
}

#[cfg(not(target_family = "wasm"))]
AggFunc::External(_) => {
let (step_fn, state_ptr, argc) = {
let OwnedValue::Agg(agg) = &state.registers[*acc_reg] else {
Expand Down Expand Up @@ -1516,7 +1511,6 @@ impl Program {
AggFunc::Max => {}
AggFunc::Min => {}
AggFunc::GroupConcat | AggFunc::StringAgg => {}
#[cfg(not(target_family = "wasm"))]
AggFunc::External(_) => {
agg.compute_external();
}
Expand Down Expand Up @@ -2011,7 +2005,6 @@ impl Program {
}
}
},
#[cfg(not(target_family = "wasm"))]
crate::function::Func::External(f) => match f.func {
ExtFunc::Scalar(f) => {
call_external_function! {f, *dest, state, arg_count, *start_reg };
Expand Down
5 changes: 5 additions & 0 deletions extensions/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ edition.workspace = true
license.workspace = true
repository.workspace = true


[features]
default = []
static = []

[dependencies]
log = "0.4.20"
limbo_macros = { path = "../../macros" }
4 changes: 2 additions & 2 deletions extensions/percentile/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ repository.workspace = true
crate-type = ["cdylib", "lib"]

[features]
static = []
static = ["limbo_ext/static"]

[dependencies]
limbo_ext = { path = "../core" }
limbo_ext = { path = "../core", features = ["static"] }
8 changes: 0 additions & 8 deletions extensions/percentile/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
use limbo_ext::{register_extension, AggFunc, AggregateDerive, ResultCode, Value};

#[cfg(feature = "static")]
register_extension! {
static_build: true,
aggregates: { Median, Percentile, PercentileCont, PercentileDisc }
}

#[cfg(not(feature = "static"))]
register_extension! {
static_build: true,
aggregates: { Median, Percentile, PercentileCont, PercentileDisc }
}

Expand Down
4 changes: 2 additions & 2 deletions extensions/uuid/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ repository.workspace = true
crate-type = ["cdylib", "lib"]

[features]
static= []
static= [ "limbo_ext/static" ]

[dependencies]
limbo_ext = { path = "../core" }
limbo_ext = { path = "../core", features = ["static"] }
uuid = { version = "1.11.0", features = ["v4", "v7"] }
log = "0.4.20"
8 changes: 0 additions & 8 deletions extensions/uuid/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use limbo_ext::{register_extension, scalar, Value, ValueType};

#[cfg(feature = "static")]
register_extension! {
static_build: true,
scalars: {uuid4_str, uuid4_blob, uuid7_str, uuid7, uuid7_ts, uuid_str, uuid_blob },
}

Expand Down Expand Up @@ -135,9 +133,3 @@ fn uuid_to_unix(uuid: &[u8; 16]) -> u64 {
| ((uuid[4] as u64) << 8)
| (uuid[5] as u64)
}

#[cfg(not(feature = "static"))]
register_extension! {
static_build: false,
scalars: { uuid4_str, uuid4_blob, uuid7_str, uuid7, uuid7_ts, uuid_str, uuid_blob }
}
2 changes: 2 additions & 0 deletions macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ description = "The Limbo database library"
[lib]
proc-macro = true

[features]

[dependencies]
quote = "1.0.38"
proc-macro2 = "1.0.93"
Expand Down
12 changes: 1 addition & 11 deletions macros/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,19 @@ use syn::{Ident, LitStr, Token};
pub(crate) struct RegisterExtensionInput {
pub aggregates: Vec<Ident>,
pub scalars: Vec<Ident>,
pub is_static: bool,
}

impl syn::parse::Parse for RegisterExtensionInput {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
let mut aggregates = Vec::new();
let mut scalars = Vec::new();
let mut is_static = false;

while !input.is_empty() {
if input.peek(syn::Ident) && input.peek2(Token![:]) {
let section_name: Ident = input.parse()?;
input.parse::<Token![:]>()?;

if section_name == "static_build" {
let val: syn::LitBool = input.parse()?;
is_static = val.value;

if input.peek(Token![,]) {
input.parse::<Token![,]>()?;
}
} else if section_name == "aggregates" || section_name == "scalars" {
if section_name == "aggregates" || section_name == "scalars" {
let content;
syn::braced!(content in input);

Expand Down Expand Up @@ -55,7 +46,6 @@ impl syn::parse::Parse for RegisterExtensionInput {
Ok(Self {
aggregates,
scalars,
is_static,
})
}
}
Expand Down
17 changes: 8 additions & 9 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,6 @@ pub fn register_extension(input: TokenStream) -> TokenStream {
let RegisterExtensionInput {
aggregates,
scalars,
is_static,
} = input_ast;

let scalar_calls = scalars.iter().map(|scalar_ident| {
Expand All @@ -389,20 +388,21 @@ pub fn register_extension(input: TokenStream) -> TokenStream {
}
}
});
let static_aggregates = aggregate_calls.clone();
let static_scalars = scalar_calls.clone();

let expanded = if is_static {
quote! {
let expanded = quote! {
#[cfg(feature = "static")]
pub unsafe extern "C" fn register_extension_static(api: &::limbo_ext::ExtensionApi) -> ::limbo_ext::ResultCode {
let api = unsafe { &*api };
#(#scalar_calls)*
#(#static_scalars)*

#(#aggregate_calls)*
#(#static_aggregates)*

::limbo_ext::ResultCode::OK
}
}
} else {
quote! {

#[cfg(not(feature = "static"))]
#[no_mangle]
pub unsafe extern "C" fn register_extension(api: &::limbo_ext::ExtensionApi) -> ::limbo_ext::ResultCode {
let api = unsafe { &*api };
Expand All @@ -412,7 +412,6 @@ pub fn register_extension(input: TokenStream) -> TokenStream {

::limbo_ext::ResultCode::OK
}
}
};

TokenStream::from(expanded)
Expand Down

0 comments on commit f13d035

Please sign in to comment.