Skip to content

Commit

Permalink
return ext_data from rust
Browse files Browse the repository at this point in the history
  • Loading branch information
tantaman committed Nov 14, 2023
1 parent 676f2e0 commit fc43775
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
4 changes: 2 additions & 2 deletions core/rs/bundle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ pub extern "C" fn sqlite3_crsqlrustbundle_init(
db: *mut sqlite::sqlite3,
err_msg: *mut *mut c_char,
api: *mut sqlite::api_routines,
) -> c_int {
) -> *mut ::core::ffi::c_void {
sqlite::EXTENSION_INIT2(api);

let rc = sqlite3_crsqlfractionalindex_init(db, err_msg, api);
if rc != 0 {
return rc;
return core::ptr::null_mut();
}

sqlite3_crsqlcore_init(db, err_msg, api)
Expand Down
39 changes: 24 additions & 15 deletions core/rs/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ mod unpack_columns_vtab;
mod util;

use core::mem;
use core::ptr::null_mut;
use core::{ffi::c_char, slice};
extern crate alloc;
use automigrate::*;
Expand Down Expand Up @@ -96,7 +97,7 @@ pub extern "C" fn sqlite3_crsqlcore_init(
db: *mut sqlite::sqlite3,
err_msg: *mut *mut c_char,
api: *mut sqlite::api_routines,
) -> c_int {
) -> *mut c_void {
sqlite::EXTENSION_INIT2(api);

let rc = db
Expand All @@ -112,7 +113,7 @@ pub extern "C" fn sqlite3_crsqlcore_init(
)
.unwrap_or(sqlite::ResultCode::ERROR);
if rc != ResultCode::OK {
return rc as c_int;
return null_mut();
}

let rc = db
Expand All @@ -128,7 +129,7 @@ pub extern "C" fn sqlite3_crsqlcore_init(
)
.unwrap_or(sqlite::ResultCode::ERROR);
if rc != ResultCode::OK {
return rc as c_int;
return null_mut();
}

let rc = db
Expand All @@ -144,22 +145,22 @@ pub extern "C" fn sqlite3_crsqlcore_init(
)
.unwrap_or(sqlite::ResultCode::ERROR);
if rc != ResultCode::OK {
return rc as c_int;
return null_mut();
}

let rc = unpack_columns_vtab::create_module(db).unwrap_or(sqlite::ResultCode::ERROR);
if rc != ResultCode::OK {
return rc as c_int;
return null_mut();
}

let rc = create_cl_set_vtab::create_module(db).unwrap_or(ResultCode::ERROR);
if rc != ResultCode::OK {
return rc as c_int;
return null_mut();
}

let rc = crate::bootstrap::crsql_init_peer_tracking_table(db);
if rc != ResultCode::OK as c_int {
return rc;
return null_mut();
}

let sync_bit_ptr = sqlite::malloc(mem::size_of::<c_int>()) as *mut c_int;
Expand All @@ -181,21 +182,29 @@ pub extern "C" fn sqlite3_crsqlcore_init(
)
.unwrap_or(sqlite::ResultCode::ERROR);
if rc != ResultCode::OK {
return rc as c_int;
return null_mut();
}

let rc = crate::bootstrap::crsql_maybe_update_db(db, err_msg);
if rc != ResultCode::OK as c_int {
return null_mut();
}

let site_id_buffer = sqlite::malloc(consts::SITE_ID_LEN * mem::size_of::<*const c_char>());
let rc = if rc == ResultCode::OK {
crate::bootstrap::crsql_init_site_id(db, site_id_buffer)
};
let ext_data = unsafe { crsql_newExtData(db, siteIdBuffer) };
let site_id_buffer =
sqlite::malloc((consts::SITE_ID_LEN as usize) * mem::size_of::<*const c_char>());
let rc = crate::bootstrap::crsql_init_site_id(db, site_id_buffer);
if rc != ResultCode::OK as c_int {
sqlite::free(site_id_buffer as *mut c_void);
return null_mut();
}

let ext_data = unsafe { crsql_newExtData(db, site_id_buffer as *mut c_char) };
if ext_data.is_null() {
return ResultCode::ERROR as c_int;
sqlite::free(site_id_buffer as *mut c_void);
return null_mut();
}

return rc as c_int;
return ext_data as *mut c_void;
}

pub unsafe extern "C" fn crsql_sqlite_free(ptr: *mut c_void) {
Expand Down
9 changes: 6 additions & 3 deletions core/src/crsqlite.c
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,8 @@ static void closeHook(void *pUserData, sqlite3 *db) {
}
#endif

int sqlite3_crsqlrustbundle_init(sqlite3 *db, char **pzErrMsg,
const sqlite3_api_routines *pApi);
void *sqlite3_crsqlrustbundle_init(sqlite3 *db, char **pzErrMsg,
const sqlite3_api_routines *pApi);

#ifdef _WIN32
__declspec(dllexport)
Expand All @@ -302,7 +302,10 @@ __declspec(dllexport)
// RN it is safe here since the rust bundle init is largely just reigstering
// function pointers. we need to init the rust bundle otherwise sqlite api
// methods are not isntalled when we start calling rust
rc = sqlite3_crsqlrustbundle_init(db, pzErrMsg, pApi);
crsql_ExtData *pExtData = sqlite3_crsqlrustbundle_init(db, pzErrMsg, pApi);
if (pExtData == 0) {
return SQLITE_ERROR;
}

if (rc == SQLITE_OK) {
rc = sqlite3_create_function(
Expand Down

0 comments on commit fc43775

Please sign in to comment.