Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,7 @@ public Object unpack(MessageUnpacker u) throws IOException {
* @return The object
*/
Object unpack(byte[] obj) {
MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(obj);
try {
try (MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(obj)) {
return this.unpack(unpacker);
} catch (IOException e) {
throw new RuntimeException("Unable to unpack object", e);
Expand Down
2 changes: 1 addition & 1 deletion src/main/rust/quickjslib/wasm_lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ crate-type = ["cdylib"]

[dependencies]
wasm_macros = { path = "../wasm_macros" }
rmp-serde = "1.1"
rmp-serde = "1.3"
log = { version = "0.4", features = ["std"] }
serde = { version = "1.0", features = ["derive"] }
rquickjs = { version = "0.11.0", features = [
Expand Down
12 changes: 6 additions & 6 deletions src/main/rust/quickjslib/wasm_lib/src/completable_future.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ pub fn promise_create(

promise.set(JAVA_COMPLETABLE_FUTURE_PTR_FIELD, cf_ptr)?;
let cf = Box::new(PromiseContainer::new(
&ctx,
ctx,
promise,
Some(resolve),
Some(reject),
Expand Down Expand Up @@ -275,12 +275,12 @@ pub(crate) fn convert_promise<'js>(promise: Promise<'js>) -> rquickjs::Result<JS

let completable_future_complete = JavaPromise::new(
ctx_pointer.ptr,
completable_future_ptr.try_into().unwrap(),
completable_future_ptr,
false,
);
let completable_future_reject = JavaPromise::new(
ctx_pointer.ptr,
completable_future_ptr.try_into().unwrap(),
completable_future_ptr,
true,
);

Expand All @@ -307,8 +307,8 @@ pub(crate) fn convert_promise<'js>(promise: Promise<'js>) -> rquickjs::Result<JS
))?;
debug!("Called .then() and .catch() on the promise");

return Ok(JSJavaProxy::CompletableFuture(
completable_future_ptr.try_into().unwrap(),
Ok(JSJavaProxy::CompletableFuture(
completable_future_ptr,
promise_ptr,
));
))
}
4 changes: 2 additions & 2 deletions src/main/rust/quickjslib/wasm_lib/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub fn eval_script(ctx: &Ctx<'_>, script: String) -> rquickjs::Result<JSJavaProx
pub fn eval_script_async(ctx: &Ctx<'_>, script: String) -> rquickjs::Result<JSJavaProxy> {
debug!("Evaluating async script: {}", script);
let promise = ctx.eval_promise(script)?;
let result = JSJavaProxy::from_js(&ctx, promise.into_value())?;
let result = JSJavaProxy::from_js(ctx, promise.into_value())?;
Ok(result)
}

Expand Down Expand Up @@ -93,7 +93,7 @@ pub fn get_global(ctx: &Ctx<'_>, name: String) -> rquickjs::Result<JSJavaProxy>
}

thread_local! {
static CONTEXT_STACK: RefCell<Vec<(u64, Ctx<'static>)>> = RefCell::new(Vec::new());
static CONTEXT_STACK: RefCell<Vec<(u64, Ctx<'static>)>> = const { RefCell::new(Vec::new()) };
}

/// Helper function to get a Ctx from a Context, handling re-entrancy.
Expand Down
12 changes: 6 additions & 6 deletions src/main/rust/quickjslib/wasm_lib/src/from_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ impl<'js, T> FromError<'js> for Option<Box<T>> {
error!("Failed to call js {}: {}", message, stacktrace);
None
} else {
error!("Failed to call js {}", err.to_string());
error!("Failed to call js {}", err);
None
}
}
_ => {
error!("Failed to call js {}", err.to_string());
error!("Failed to call js {}", err);
None
}
}
Expand All @@ -65,12 +65,12 @@ impl<'js> FromError<'js> for bool {
error!("Failed to call js {}: {}", message, stacktrace);
false
} else {
error!("Failed to call js {}", err.to_string());
error!("Failed to call js {}", err);
false
}
}
_ => {
error!("Failed to call js {}", err.to_string());
error!("Failed to call js {}", err);
false
}
}
Expand All @@ -90,12 +90,12 @@ impl<'js> FromError<'js> for i32 {
error!("Failed to call js {}: {}", message, stacktrace);
-1
} else {
error!("Failed to call js {}", err.to_string());
error!("Failed to call js {}", err);
-1
}
}
_ => {
error!("Failed to call js {}", err.to_string());
error!("Failed to call js {}", err);
-1
}
}
Expand Down
35 changes: 30 additions & 5 deletions src/main/rust/quickjslib/wasm_lib/src/into_wasm_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,21 @@ impl IntoWasmResult for JSJavaProxy {

/// Converts a Box<T> into a u64 that can be returned to Java (by returning the pointer to the object)
impl<T> IntoWasmResult for Box<T> {
#[inline]
fn into_wasm(self) -> u64 {
// return the pointer to the object
let ptr = Box::into_raw(self);
ptr as u64
}
}

/// Converts a Option<Box<T>> into a u64 that can be returned to Java (by returning the pointer to the object).
/// For None 0 is returned
impl<T> IntoWasmResult for Option<Box<T>> {
/// Converts an Option<IntoWasmResult> into a u64 that can be returned to Java (by returning the pointer to the object).
/// For None 0 is returned
impl<T> IntoWasmResult for Option<T>
where
T: IntoWasmResult,
{
#[inline]
fn into_wasm(self) -> u64 {
match self {
Some(v) => v.into_wasm(),
Expand All @@ -49,6 +54,7 @@ impl IntoWasmResult for String {

/// Converts a bool into a u64 that can be returned to Java (by returning 1 for true and 0 for false)
impl IntoWasmResult for bool {
#[inline]
fn into_wasm(self) -> u64 {
if self {
1
Expand All @@ -60,29 +66,48 @@ impl IntoWasmResult for bool {

/// Converts an i32 into a u64 that can be returned to Java (by returning the value as is)
impl IntoWasmResult for i32 {
#[inline]
fn into_wasm(self) -> u64 {
self as u64
}
}

/// Converts a u32 into a u64 that can be returned to Java (by returning the value as is)
impl IntoWasmResult for u32 {
#[inline]
fn into_wasm(self) -> u64 {
self as u64
}
}

/// Converts an i64 into a u64 that can be returned to Java (by returning the value as is)
///
impl IntoWasmResult for i64 {
#[inline]
fn into_wasm(self) -> u64 {
self as u64
}
}

/// Converts a u64 into a u64 that can be returned to Java (by returning the value as is)
impl IntoWasmResult for u64 {
#[inline]
fn into_wasm(self) -> u64 {
self as u64
self
}
}

/// Converts a f32 into a u64 (by bit representation not by value)
impl IntoWasmResult for f32 {
#[inline]
fn into_wasm(self) -> u64 {
self.to_bits().into_wasm()
}
}

/// Converts a f64 into a u64 (by bit representation not by value)
impl IntoWasmResult for f64 {
#[inline]
fn into_wasm(self) -> u64 {
self.to_bits().into_wasm()
}
}
19 changes: 15 additions & 4 deletions src/main/rust/quickjslib/wasm_lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,28 @@ mod native_object;
mod quickjs_function;
mod runtime;

/// Give the host a way to free memory to prevent leaks
/// Give the wasm host a way to free memory to prevent leaks
///
/// # Safety
///
/// * `ptr` must have been allocated by `alloc`.
/// * `size` must match the exact same size used during allocation.
/// * The memory must not be accessed or used after being deallocated.
#[no_mangle]
pub extern "C" fn dealloc(ptr: *mut u8, size: usize) {
pub unsafe extern "C" fn dealloc(ptr: *mut u8, size: usize) {
unsafe {
let _ = Vec::from_raw_parts(ptr, 0, size);
}
}

/// Give the host a way to allocate memory inside the Wasm module
/// Give the wasm host a way to allocate memory inside the Wasm module
///
/// # Safety
///
/// * The caller must ensure that the returned pointer is properly deallocated using `dealloc` with the exact same size to prevent memory leaks.
/// * The allocated memory is uninitialized and must be initialized before being read.
#[no_mangle]
pub extern "C" fn alloc(size: usize) -> *mut u8 {
pub unsafe extern "C" fn alloc(size: usize) -> *mut u8 {
let mut buf = Vec::with_capacity(size);
let ptr = buf.as_mut_ptr();
mem::forget(buf); // Prevent Rust from freeing the memory
Expand Down
12 changes: 6 additions & 6 deletions src/main/rust/quickjslib/wasm_lib/src/native_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::js_to_java_proxy::JSJavaProxy;
#[wasm_export]
pub fn array_create(ctx: &Ctx<'_>) -> rquickjs::Result<Option<Box<Persistent<Array<'static>>>>> {
let js_array = rquickjs::Array::new(ctx.clone()).unwrap();
let persistent = Persistent::save(&ctx, js_array);
let persistent = Persistent::save(ctx, js_array);
let result = Box::new(persistent);
Ok(Some(result))
}
Expand All @@ -22,7 +22,7 @@ pub fn array_size(
ctx: &Ctx<'_>,
persistent_array: &Persistent<Array<'static>>,
) -> rquickjs::Result<i32> {
let v = persistent_array.clone().restore(&ctx)?;
let v = persistent_array.clone().restore(ctx)?;
Ok(v.len() as i32)
}

Expand All @@ -33,7 +33,7 @@ pub fn array_add(
index: i32,
value: JSJavaProxy,
) -> rquickjs::Result<bool> {
let array = persistent_array.clone().restore(&ctx)?;
let array = persistent_array.clone().restore(ctx)?;
splice_array(array, index, 0, Some(value))?;
Ok(true)
}
Expand All @@ -45,7 +45,7 @@ pub fn array_set(
index: i32,
value: JSJavaProxy,
) -> rquickjs::Result<bool> {
let array = persistent_array.clone().restore(&ctx)?;
let array = persistent_array.clone().restore(ctx)?;

array.set(index as usize, value)?;
Ok(true)
Expand All @@ -57,7 +57,7 @@ pub fn array_get(
persistent_array: &Persistent<Array<'static>>,
index: i32,
) -> rquickjs::Result<JSJavaProxy> {
let array = persistent_array.clone().restore(&ctx)?;
let array = persistent_array.clone().restore(ctx)?;

array.get(index as usize)?
}
Expand All @@ -68,7 +68,7 @@ pub fn array_remove(
persistent_array: &Persistent<Array<'static>>,
index: i32,
) -> rquickjs::Result<bool> {
let array = persistent_array.clone().restore(&ctx)?;
let array = persistent_array.clone().restore(ctx)?;

splice_array(array, index, 1, None)?;

Expand Down
12 changes: 6 additions & 6 deletions src/main/rust/quickjslib/wasm_lib/src/native_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::js_to_java_proxy::JSJavaProxy;
#[wasm_export]
pub fn object_create(ctx: &Ctx<'_>) -> rquickjs::Result<Option<Box<Persistent<Object<'static>>>>> {
let js_object = rquickjs::Object::new(ctx.clone())?;
let persistent = Persistent::save(&ctx, js_object);
let persistent = Persistent::save(ctx, js_object);
let result = Box::new(persistent);
Ok(Some(result))
}
Expand All @@ -23,7 +23,7 @@ pub fn object_size(
ctx: &Ctx<'_>,
persistent_object: &Persistent<Object<'static>>,
) -> rquickjs::Result<i32> {
let v = persistent_object.clone().restore(&ctx)?;
let v = persistent_object.clone().restore(ctx)?;
Ok(v.len() as i32)
}

Expand All @@ -33,7 +33,7 @@ pub fn object_contains_key(
persistent_object: &Persistent<Object<'static>>,
key: JSJavaProxy,
) -> rquickjs::Result<bool> {
let v = persistent_object.clone().restore(&ctx)?;
let v = persistent_object.clone().restore(ctx)?;
v.contains_key(key)
}

Expand All @@ -60,7 +60,7 @@ pub fn object_remove_value(
persistent_object: &Persistent<Object<'static>>,
key: JSJavaProxy,
) -> rquickjs::Result<bool> {
let v = persistent_object.clone().restore(&ctx)?;
let v = persistent_object.clone().restore(ctx)?;
v.remove(key)?;
Ok(true)
}
Expand All @@ -72,7 +72,7 @@ pub fn object_set_value(
key: JSJavaProxy,
value: JSJavaProxy,
) -> rquickjs::Result<bool> {
let v = persistent_object.clone().restore(&ctx)?;
let v = persistent_object.clone().restore(ctx)?;
v.set(key, value)?;
Ok(true)
}
Expand All @@ -82,7 +82,7 @@ pub fn object_key_set(
ctx: &Ctx<'_>,
persistent_object: &Persistent<Object<'static>>,
) -> rquickjs::Result<JSJavaProxy> {
let v = persistent_object.clone().restore(&ctx)?;
let v = persistent_object.clone().restore(ctx)?;

let object_keys: ObjectKeysIter<'_, JSJavaProxy> = v.keys();

Expand Down
8 changes: 4 additions & 4 deletions src/main/rust/quickjslib/wasm_lib/src/quickjs_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ use wasm_macros::wasm_export;
use crate::js_to_java_proxy::JSJavaProxy;

#[wasm_export]
pub fn call_function<'js>(
pub fn call_function(
ctx: &Ctx<'_>,
persistent_function: &Persistent<Function<'static>>,
args: JSJavaProxy,
) -> rquickjs::Result<JSJavaProxy> {
let function = persistent_function.clone().restore(&ctx)?;
let function = persistent_function.clone().restore(ctx)?;
debug!("Calling function with args: {:?}", args);
function.call(args)?
}
Expand Down Expand Up @@ -75,7 +75,7 @@ impl JavaFunction {
JSJavaProxy::Undefined
}
};
crate::dealloc(result_ptr as *mut u8, result_len);
unsafe { crate::dealloc(result_ptr as *mut u8, result_len) };

debug!(
"Calling Java function: {} on context {} with arg: {:?} -> {:?}",
Expand Down Expand Up @@ -113,7 +113,7 @@ impl<'js, P> IntoJsFunc<'js, P> for JavaFunction {

// If the result is an exception, throw it
if let JSJavaProxy::Exception(message, _stacktrace) = &result {
let exception = rquickjs::Exception::from_message(params.ctx().clone(), &message)?;
let exception = rquickjs::Exception::from_message(params.ctx().clone(), message)?;
Err(params.ctx().throw(exception.into_value()))
} else {
result.into_js(params.ctx())
Expand Down
2 changes: 1 addition & 1 deletion src/main/rust/quickjslib/wasm_macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ proc-macro = true
syn = { version = "2.0", features = ["full"] }
quote = "1.0"
proc-macro2 = "1.0"
regex = "1.12.3"
regex = "1.12"
Loading