mirror of
https://github.com/allankoechke/turso.git
synced 2026-11-30 11:12:58 +00:00
remove scalar_context macro, defer to future work
This commit is contained in:
@@ -11,9 +11,7 @@ use functions::{RegisterAggFn, RegisterScalarFn, UnregisterFunctionFn};
|
||||
use std::os::raw::c_void;
|
||||
#[cfg(feature = "vfs")]
|
||||
pub use turso_macros::VfsDerive;
|
||||
pub use turso_macros::{
|
||||
register_extension, scalar, scalar_context, AggregateDerive, VTabModuleDerive,
|
||||
};
|
||||
pub use turso_macros::{register_extension, scalar, AggregateDerive, VTabModuleDerive};
|
||||
pub use types::{ResultCode, StepResult, Value, ValueType};
|
||||
#[cfg(feature = "vfs")]
|
||||
pub use vfs_modules::{
|
||||
|
||||
@@ -8,16 +8,16 @@ use std::num::NonZeroUsize;
|
||||
use std::rc::Rc;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use turso_ext::{
|
||||
register_extension, scalar, scalar_context, Connection, ConstraintInfo, ConstraintOp,
|
||||
ConstraintUsage, ExtResult, IndexInfo, OrderByInfo, ResultCode, StepResult, VTabCursor,
|
||||
VTabKind, VTabModule, VTabModuleDerive, VTable, Value,
|
||||
register_extension, scalar, Connection, ConstraintInfo, ConstraintOp, ConstraintUsage,
|
||||
ExtResult, IndexInfo, OrderByInfo, ResultCode, StepResult, VTabCursor, VTabKind, VTabModule,
|
||||
VTabModuleDerive, VTable, Value,
|
||||
};
|
||||
#[cfg(not(target_family = "wasm"))]
|
||||
use turso_ext::{BufferRef, Callback, VfsDerive, VfsExtension, VfsFile};
|
||||
|
||||
register_extension! {
|
||||
vtabs: { KVStoreVTabModule, TableStatsVtabModule },
|
||||
scalars: { test_scalar, test_scalar_context },
|
||||
scalars: { test_scalar },
|
||||
vfs: { TestFS },
|
||||
}
|
||||
|
||||
@@ -342,20 +342,6 @@ fn test_scalar(_args: turso_ext::Value) -> turso_ext::Value {
|
||||
turso_ext::Value::from_integer(42)
|
||||
}
|
||||
|
||||
#[scalar_context(name = "test_scalar_context")]
|
||||
fn test_scalar_context(
|
||||
context: usize,
|
||||
args: &[Value],
|
||||
context_destructor: Option<turso_ext::ContextDestructor>,
|
||||
value_destructor: Option<turso_ext::ValueDestructor>,
|
||||
) -> Value {
|
||||
let metadata_score = context as i64
|
||||
+ if context_destructor.is_some() { 100 } else { 0 }
|
||||
+ if value_destructor.is_some() { 10 } else { 0 };
|
||||
let arg_score = args.iter().filter_map(Value::to_integer).sum::<i64>();
|
||||
Value::from_integer(metadata_score + arg_score)
|
||||
}
|
||||
|
||||
#[cfg(not(target_family = "wasm"))]
|
||||
impl VfsExtension for TestFS {
|
||||
const NAME: &'static str = "testvfs";
|
||||
|
||||
@@ -11,7 +11,7 @@ mod vfs_derive;
|
||||
mod vtab_derive;
|
||||
pub use agg_derive::derive_agg_func;
|
||||
pub use match_ignore_ascii_case::match_ignore_ascci_case;
|
||||
pub use scalars::{scalar, scalar_context};
|
||||
pub use scalars::scalar;
|
||||
pub use vfs_derive::derive_vfs_module;
|
||||
pub use vtab_derive::derive_vtab_module;
|
||||
|
||||
|
||||
@@ -95,83 +95,3 @@ pub fn scalar(attr: TokenStream, input: TokenStream) -> TokenStream {
|
||||
TokenStream::from(expanded)
|
||||
}
|
||||
|
||||
pub fn scalar_context(attr: TokenStream, input: TokenStream) -> TokenStream {
|
||||
let ast = parse_macro_input!(input as ItemFn);
|
||||
let fn_name = &ast.sig.ident;
|
||||
let scalar_info = parse_macro_input!(attr as ScalarInfo);
|
||||
let name = &scalar_info.name;
|
||||
let register_fn_name = format_ident!("register_{}", fn_name);
|
||||
let context_variable_name = argument_name(&ast, 0, "context");
|
||||
let args_variable_name = argument_name(&ast, 1, "args");
|
||||
let context_destructor_variable_name = argument_name(&ast, 2, "context_destructor");
|
||||
let value_destructor_variable_name = argument_name(&ast, 3, "value_destructor");
|
||||
let fn_body = &ast.block;
|
||||
let alias_check = if let Some(alias) = &scalar_info.alias {
|
||||
quote! {
|
||||
let Ok(alias_c_name) = ::std::ffi::CString::new(#alias) else {
|
||||
return ::turso_ext::ResultCode::Error;
|
||||
};
|
||||
(api.register_scalar_function)(
|
||||
api.ctx,
|
||||
alias_c_name.as_ptr(),
|
||||
-1,
|
||||
false,
|
||||
0,
|
||||
#fn_name,
|
||||
None,
|
||||
None,
|
||||
);
|
||||
}
|
||||
} else {
|
||||
quote! {}
|
||||
};
|
||||
|
||||
let expanded = quote! {
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn #register_fn_name(
|
||||
api: *const ::turso_ext::ExtensionApi
|
||||
) -> ::turso_ext::ResultCode {
|
||||
if api.is_null() {
|
||||
return ::turso_ext::ResultCode::Error;
|
||||
}
|
||||
let api = unsafe { &*api };
|
||||
let Ok(c_name) = ::std::ffi::CString::new(#name) else {
|
||||
return ::turso_ext::ResultCode::Error;
|
||||
};
|
||||
(api.register_scalar_function)(
|
||||
api.ctx,
|
||||
c_name.as_ptr(),
|
||||
-1,
|
||||
false,
|
||||
0,
|
||||
#fn_name,
|
||||
None,
|
||||
None,
|
||||
);
|
||||
#alias_check
|
||||
::turso_ext::ResultCode::OK
|
||||
}
|
||||
|
||||
#[allow(unused_variables)]
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn #fn_name(
|
||||
context: usize,
|
||||
argc: i32,
|
||||
argv: *const ::turso_ext::Value,
|
||||
context_destructor: Option<::turso_ext::ContextDestructor>,
|
||||
value_destructor: Option<::turso_ext::ValueDestructor>
|
||||
) -> ::turso_ext::Value {
|
||||
let #context_variable_name = context;
|
||||
let #args_variable_name = if argv.is_null() || argc <= 0 {
|
||||
&[]
|
||||
} else {
|
||||
unsafe { std::slice::from_raw_parts(argv, argc as usize) }
|
||||
};
|
||||
let #context_destructor_variable_name = context_destructor;
|
||||
let #value_destructor_variable_name = value_destructor;
|
||||
#fn_body
|
||||
}
|
||||
};
|
||||
|
||||
TokenStream::from(expanded)
|
||||
}
|
||||
|
||||
@@ -320,24 +320,6 @@ pub fn scalar(attr: TokenStream, input: TokenStream) -> TokenStream {
|
||||
ext::scalar(attr, input)
|
||||
}
|
||||
|
||||
/// Declare a context-aware scalar function for your extension.
|
||||
/// ```ignore
|
||||
/// use turso_ext::{scalar_context, ContextDestructor, Value, ValueDestructor};
|
||||
/// #[scalar_context(name = "context_double")]
|
||||
/// fn context_double(
|
||||
/// ctx: usize,
|
||||
/// args: &[Value],
|
||||
/// context_destructor: Option<ContextDestructor>,
|
||||
/// value_destructor: Option<ValueDestructor>,
|
||||
/// ) -> Value {
|
||||
/// Value::from_integer(ctx as i64 + args[0].to_integer().unwrap_or_default())
|
||||
/// }
|
||||
/// ```
|
||||
#[proc_macro_attribute]
|
||||
pub fn scalar_context(attr: TokenStream, input: TokenStream) -> TokenStream {
|
||||
ext::scalar_context(attr, input)
|
||||
}
|
||||
|
||||
/// Define an aggregate function for your extension by deriving
|
||||
/// AggregateDerive on a struct that implements the AggFunc trait.
|
||||
/// ```ignore
|
||||
|
||||
@@ -430,12 +430,6 @@ def _test_kv(exec_name, ext_path):
|
||||
turso.execute_dot(
|
||||
"create virtual table t using kv_store;",
|
||||
)
|
||||
if exec_name is None:
|
||||
turso.run_test_fn(
|
||||
"SELECT test_scalar_context(40, 2);",
|
||||
lambda res: "42" == res,
|
||||
"can call scalar_context macro extension function",
|
||||
)
|
||||
turso.run_test_fn(".schema", lambda res: "CREATE VIRTUAL TABLE t" in res)
|
||||
turso.run_test_fn(
|
||||
"insert into t values ('hello', 'world');",
|
||||
|
||||
Reference in New Issue
Block a user