From 8e2c9367c0c411a8dff932408ac56561e68b657b Mon Sep 17 00:00:00 2001 From: PThorpe92 Date: Thu, 6 Mar 2025 20:45:08 -0500 Subject: [PATCH] add missing method to add builtin vfs to ext api --- Cargo.lock | 4 +--- extensions/core/Cargo.toml | 2 +- extensions/core/src/lib.rs | 20 ++++++++++++++++++++ extensions/core/src/vfs_modules.rs | 2 +- 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 174e37be4..34adaaf5d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1043,10 +1043,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "43a49c392881ce6d5c3b8cb70f98717b7c07aabbdff06687b9030dbfbe2725f8" dependencies = [ "cfg-if", - "js-sys", "libc", "wasi 0.13.3+wasi-0.2.2", - "wasm-bindgen", "windows-targets 0.52.6", ] @@ -1713,7 +1711,7 @@ name = "limbo_ext" version = "0.0.16" dependencies = [ "chrono", - "getrandom 0.3.1", + "getrandom 0.2.15", "limbo_macros", ] diff --git a/extensions/core/Cargo.toml b/extensions/core/Cargo.toml index 25369e133..da1552c8f 100644 --- a/extensions/core/Cargo.toml +++ b/extensions/core/Cargo.toml @@ -13,5 +13,5 @@ static = [] [dependencies] chrono = "0.4.40" -getrandom = { version = "0.3.1", features = ["wasm_js"] } +getrandom = { version = "0.2.15", features = ["js"] } limbo_macros = { workspace = true } diff --git a/extensions/core/src/lib.rs b/extensions/core/src/lib.rs index 6cdaa8a05..03a4bf43a 100644 --- a/extensions/core/src/lib.rs +++ b/extensions/core/src/lib.rs @@ -29,6 +29,26 @@ pub struct ExtensionApiRef { pub api: *const ExtensionApi, } +impl ExtensionApi { + /// Since we want the option to build in extensions at compile time as well, + /// we add a slice of VfsImpls to the extension API, and this is called with any + /// libraries that we load staticly that will add their VFS implementations to the list. + pub fn add_builtin_vfs(&mut self, vfs: *const VfsImpl) -> ResultCode { + if vfs.is_null() || self.builtin_vfs.is_null() { + return ResultCode::Error; + } + let mut new = unsafe { + let slice = + std::slice::from_raw_parts_mut(self.builtin_vfs, self.builtin_vfs_count as usize); + Vec::from(slice) + }; + new.push(vfs); + self.builtin_vfs = Box::into_raw(new.into_boxed_slice()) as *mut *const VfsImpl; + self.builtin_vfs_count += 1; + ResultCode::OK + } +} + pub type ExtensionEntryPoint = unsafe extern "C" fn(api: *const ExtensionApi) -> ResultCode; pub type ScalarFunction = unsafe extern "C" fn(argc: i32, *const Value) -> Value; diff --git a/extensions/core/src/vfs_modules.rs b/extensions/core/src/vfs_modules.rs index 3b2ab3a28..abadee180 100644 --- a/extensions/core/src/vfs_modules.rs +++ b/extensions/core/src/vfs_modules.rs @@ -14,7 +14,7 @@ pub trait VfsExtension: Default + Send + Sync { } fn generate_random_number(&self) -> i64 { let mut buf = [0u8; 8]; - getrandom::fill(&mut buf).unwrap(); + getrandom::getrandom(&mut buf).unwrap(); i64::from_ne_bytes(buf) } fn get_current_time(&self) -> String {