Skip to content

Commit

Permalink
Add minor improvements to rs_port.
Browse files Browse the repository at this point in the history
  • Loading branch information
viferga committed Dec 12, 2024
1 parent 182b646 commit d901405
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 60 deletions.
4 changes: 4 additions & 0 deletions source/ports/rs_port/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
{
"[rust]": {
"editor.defaultFormatter": "rust-lang.rust-analyzer",
"editor.formatOnSave": true
},
"rust-analyzer.checkOnSave": true,
"rust-analyzer.check.command": "clippy"
}
152 changes: 114 additions & 38 deletions source/ports/rs_port/src/parsers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,50 +56,126 @@ pub fn raw_to_metacallobj_leak<T: MetaCallValue>(
pub fn raw_to_metacallobj_untyped(ret: *mut c_void) -> Box<dyn MetaCallValue> {
match (ret.is_null(), unsafe { metacall_value_id(ret) }) {
(true, _) => metacallobj_result_wrap(MetaCallNull::from_metacall_raw(ret)),
(_, metacall_value_id::METACALL_BOOL) => metacallobj_result_wrap(bool::from_metacall_raw(ret)),
(_, metacall_value_id::METACALL_CHAR) => metacallobj_result_wrap(char::from_metacall_raw(ret)),
(_, metacall_value_id::METACALL_SHORT) => metacallobj_result_wrap(i16::from_metacall_raw(ret)),
(_, metacall_value_id::METACALL_INT) => metacallobj_result_wrap(i32::from_metacall_raw(ret)),
(_, metacall_value_id::METACALL_LONG) => metacallobj_result_wrap(i64::from_metacall_raw(ret)),
(_, metacall_value_id::METACALL_FLOAT) => metacallobj_result_wrap(f32::from_metacall_raw(ret)),
(_, metacall_value_id::METACALL_DOUBLE) => metacallobj_result_wrap(f64::from_metacall_raw(ret)),
(_, metacall_value_id::METACALL_STRING) => metacallobj_result_wrap(String::from_metacall_raw(ret)),
(_, metacall_value_id::METACALL_BUFFER) => metacallobj_result_wrap(<Vec<i8>>::from_metacall_raw(ret)),
(_, metacall_value_id::METACALL_ARRAY) => metacallobj_result_wrap(<Vec<MetaCallNull>>::from_metacall_raw(ret)),
(_, metacall_value_id::METACALL_MAP) => metacallobj_result_wrap(<HashMap<String, MetaCallNull>>::from_metacall_raw(ret)),
(_, metacall_value_id::METACALL_PTR) => metacallobj_result_wrap(<MetaCallPointer>::from_metacall_raw(ret)),
(_, metacall_value_id::METACALL_FUTURE) => metacallobj_result_wrap(MetaCallFuture::from_metacall_raw(ret)),
(_, metacall_value_id::METACALL_FUNCTION) => metacallobj_result_wrap(MetaCallFunction::from_metacall_raw(ret)),
(_, metacall_value_id::METACALL_NULL) => metacallobj_result_wrap(MetaCallNull::from_metacall_raw(ret)),
(_, metacall_value_id::METACALL_CLASS) => metacallobj_result_wrap(MetaCallClass::from_metacall_raw(ret)),
(_, metacall_value_id::METACALL_OBJECT) => metacallobj_result_wrap(MetaCallObject::from_metacall_raw(ret)),
(_, metacall_value_id::METACALL_EXCEPTION) => metacallobj_result_wrap(MetaCallException::from_metacall_raw(ret)),
(_, metacall_value_id::METACALL_THROWABLE) => metacallobj_result_wrap(MetaCallThrowable::from_metacall_raw(ret)),
(_, metacall_value_id::METACALL_BOOL) => {
metacallobj_result_wrap(bool::from_metacall_raw(ret))
}
(_, metacall_value_id::METACALL_CHAR) => {
metacallobj_result_wrap(char::from_metacall_raw(ret))
}
(_, metacall_value_id::METACALL_SHORT) => {
metacallobj_result_wrap(i16::from_metacall_raw(ret))
}
(_, metacall_value_id::METACALL_INT) => {
metacallobj_result_wrap(i32::from_metacall_raw(ret))
}
(_, metacall_value_id::METACALL_LONG) => {
metacallobj_result_wrap(i64::from_metacall_raw(ret))
}
(_, metacall_value_id::METACALL_FLOAT) => {
metacallobj_result_wrap(f32::from_metacall_raw(ret))
}
(_, metacall_value_id::METACALL_DOUBLE) => {
metacallobj_result_wrap(f64::from_metacall_raw(ret))
}
(_, metacall_value_id::METACALL_STRING) => {
metacallobj_result_wrap(String::from_metacall_raw(ret))
}
(_, metacall_value_id::METACALL_BUFFER) => {
metacallobj_result_wrap(<Vec<i8>>::from_metacall_raw(ret))
}
(_, metacall_value_id::METACALL_ARRAY) => {
metacallobj_result_wrap(<Vec<MetaCallNull>>::from_metacall_raw(ret))
}
(_, metacall_value_id::METACALL_MAP) => {
metacallobj_result_wrap(<HashMap<String, MetaCallNull>>::from_metacall_raw(ret))
}
(_, metacall_value_id::METACALL_PTR) => {
metacallobj_result_wrap(<MetaCallPointer>::from_metacall_raw(ret))
}
(_, metacall_value_id::METACALL_FUTURE) => {
metacallobj_result_wrap(MetaCallFuture::from_metacall_raw(ret))
}
(_, metacall_value_id::METACALL_FUNCTION) => {
metacallobj_result_wrap(MetaCallFunction::from_metacall_raw(ret))
}
(_, metacall_value_id::METACALL_NULL) => {
metacallobj_result_wrap(MetaCallNull::from_metacall_raw(ret))
}
(_, metacall_value_id::METACALL_CLASS) => {
metacallobj_result_wrap(MetaCallClass::from_metacall_raw(ret))
}
(_, metacall_value_id::METACALL_OBJECT) => {
metacallobj_result_wrap(MetaCallObject::from_metacall_raw(ret))
}
(_, metacall_value_id::METACALL_EXCEPTION) => {
metacallobj_result_wrap(MetaCallException::from_metacall_raw(ret))
}
(_, metacall_value_id::METACALL_THROWABLE) => {
metacallobj_result_wrap(MetaCallThrowable::from_metacall_raw(ret))
}
_ => metacallobj_result_wrap(MetaCallNull::from_metacall_raw(ret)),
}
}
pub fn raw_to_metacallobj_untyped_leak(ret: *mut c_void) -> Box<dyn MetaCallValue> {
match (ret.is_null(), unsafe { metacall_value_id(ret) }) {
(true, _) => metacallobj_result_wrap(MetaCallNull::from_metacall_raw_leak(ret)),
(_, metacall_value_id::METACALL_BOOL) => metacallobj_result_wrap(bool::from_metacall_raw_leak(ret)),
(_, metacall_value_id::METACALL_CHAR) => metacallobj_result_wrap(char::from_metacall_raw_leak(ret)),
(_, metacall_value_id::METACALL_SHORT) => metacallobj_result_wrap(i16::from_metacall_raw_leak(ret)),
(_, metacall_value_id::METACALL_INT) => metacallobj_result_wrap(i32::from_metacall_raw_leak(ret)),
(_, metacall_value_id::METACALL_LONG) => metacallobj_result_wrap(i64::from_metacall_raw_leak(ret)),
(_, metacall_value_id::METACALL_FLOAT) => metacallobj_result_wrap(f32::from_metacall_raw_leak(ret)),
(_, metacall_value_id::METACALL_DOUBLE) => metacallobj_result_wrap(f64::from_metacall_raw_leak(ret)),
(_, metacall_value_id::METACALL_STRING) => metacallobj_result_wrap(String::from_metacall_raw_leak(ret)),
(_, metacall_value_id::METACALL_BUFFER) => metacallobj_result_wrap(<Vec<i8>>::from_metacall_raw_leak(ret)),
(_, metacall_value_id::METACALL_ARRAY) => metacallobj_result_wrap(<Vec<MetaCallNull>>::from_metacall_raw_leak(ret)),
(_, metacall_value_id::METACALL_MAP) => { metacallobj_result_wrap(<HashMap<String, MetaCallNull>>::from_metacall_raw_leak(ret)) }
(_, metacall_value_id::METACALL_PTR) => metacallobj_result_wrap(<MetaCallPointer>::from_metacall_raw_leak(ret)),
(_, metacall_value_id::METACALL_FUTURE) => metacallobj_result_wrap(MetaCallFuture::from_metacall_raw_leak(ret)),
(_, metacall_value_id::METACALL_FUNCTION) => metacallobj_result_wrap(MetaCallFunction::from_metacall_raw_leak(ret)),
(_, metacall_value_id::METACALL_NULL) => metacallobj_result_wrap(MetaCallNull::from_metacall_raw_leak(ret)),
(_, metacall_value_id::METACALL_CLASS) => metacallobj_result_wrap(MetaCallClass::from_metacall_raw_leak(ret)),
(_, metacall_value_id::METACALL_OBJECT) => metacallobj_result_wrap(MetaCallObject::from_metacall_raw_leak(ret)),
(_, metacall_value_id::METACALL_EXCEPTION) => metacallobj_result_wrap(MetaCallException::from_metacall_raw_leak(ret)),
(_, metacall_value_id::METACALL_THROWABLE) => metacallobj_result_wrap(MetaCallThrowable::from_metacall_raw_leak(ret)),
(_, metacall_value_id::METACALL_BOOL) => {
metacallobj_result_wrap(bool::from_metacall_raw_leak(ret))
}
(_, metacall_value_id::METACALL_CHAR) => {
metacallobj_result_wrap(char::from_metacall_raw_leak(ret))
}
(_, metacall_value_id::METACALL_SHORT) => {
metacallobj_result_wrap(i16::from_metacall_raw_leak(ret))
}
(_, metacall_value_id::METACALL_INT) => {
metacallobj_result_wrap(i32::from_metacall_raw_leak(ret))
}
(_, metacall_value_id::METACALL_LONG) => {
metacallobj_result_wrap(i64::from_metacall_raw_leak(ret))
}
(_, metacall_value_id::METACALL_FLOAT) => {
metacallobj_result_wrap(f32::from_metacall_raw_leak(ret))
}
(_, metacall_value_id::METACALL_DOUBLE) => {
metacallobj_result_wrap(f64::from_metacall_raw_leak(ret))
}
(_, metacall_value_id::METACALL_STRING) => {
metacallobj_result_wrap(String::from_metacall_raw_leak(ret))
}
(_, metacall_value_id::METACALL_BUFFER) => {
metacallobj_result_wrap(<Vec<i8>>::from_metacall_raw_leak(ret))
}
(_, metacall_value_id::METACALL_ARRAY) => {
metacallobj_result_wrap(<Vec<MetaCallNull>>::from_metacall_raw_leak(ret))
}
(_, metacall_value_id::METACALL_MAP) => {
metacallobj_result_wrap(<HashMap<String, MetaCallNull>>::from_metacall_raw_leak(ret))
}
(_, metacall_value_id::METACALL_PTR) => {
metacallobj_result_wrap(<MetaCallPointer>::from_metacall_raw_leak(ret))
}
(_, metacall_value_id::METACALL_FUTURE) => {
metacallobj_result_wrap(MetaCallFuture::from_metacall_raw_leak(ret))
}
(_, metacall_value_id::METACALL_FUNCTION) => {
metacallobj_result_wrap(MetaCallFunction::from_metacall_raw_leak(ret))
}
(_, metacall_value_id::METACALL_NULL) => {
metacallobj_result_wrap(MetaCallNull::from_metacall_raw_leak(ret))
}
(_, metacall_value_id::METACALL_CLASS) => {
metacallobj_result_wrap(MetaCallClass::from_metacall_raw_leak(ret))
}
(_, metacall_value_id::METACALL_OBJECT) => {
metacallobj_result_wrap(MetaCallObject::from_metacall_raw_leak(ret))
}
(_, metacall_value_id::METACALL_EXCEPTION) => {
metacallobj_result_wrap(MetaCallException::from_metacall_raw_leak(ret))
}
(_, metacall_value_id::METACALL_THROWABLE) => {
metacallobj_result_wrap(MetaCallThrowable::from_metacall_raw_leak(ret))
}
_ => metacallobj_result_wrap(MetaCallNull::from_metacall_raw_leak(ret)),
}
}
Expand Down
44 changes: 22 additions & 22 deletions source/ports/rs_port/src/types/metacall_future.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ use std::{
pub type MetaCallFutureHandler = fn(Box<dyn MetaCallValue>, Box<dyn MetaCallValue>);

/// Represents MetaCallFuture. Keep in mind that it's not supported to pass a future as an argument.
///
///
/// ## **Usage example:**
///
///
/// **Javascript Code:**
/// ```javascript
/// function doubleValueAfterTime(value, delay) {
Expand All @@ -33,20 +33,20 @@ pub type MetaCallFutureHandler = fn(Box<dyn MetaCallValue>, Box<dyn MetaCallValu
/// });
/// }
/// ```
///
///
/// **Calling Example:**
/// ```rust
/// use metacall::{MetaCallValue, MetaCallFuture, metacall};
/// fn runner(x: i32) {
///
///
/// fn resolve(result: impl MetaCallValue, data: impl MetaCallValue) {
/// println!("Resolve:: result: {:#?}, data: {:#?}", result, data); //
/// println!("Resolve:: result: {:#?}, data: {:#?}", result, data); //
/// }
///
///
/// fn reject(error: impl MetaCallValue, data: impl MetaCallValue) {
/// println!("Reject:: error: {:#?}, data: {:#?}", error, data);
/// }
///
///
/// let future = metacall::<MetaCallFuture>("doubleValueAfterTime", [1, 2000]).unwrap();
/// future.then(resolve).catch(reject).await_fut();
/// }
Expand Down Expand Up @@ -161,47 +161,47 @@ impl MetaCallFuture {
}

/// Adds a resolve callback.
///
///
/// ## **Usage example:**
///
///
///
///
/// ```javascript
/// // Javascript script
///
///
/// function func_always_rejects(value, delay) {
/// return new Promise((resolve) => {
/// resolve('Resolve message.');
/// resolve('Resolve message.');
/// });
/// }
/// ```
/// **Calling Example:**
///
///
/// ```rust
/// use metacall::{MetaCallValue, MetaCallFuture, metacall_no_args};
/// fn calling() {
/// fn reject(result: impl MetaCallValue, _: impl MetaCallValue) {
/// println!("Resolve:: {:#?}", result); // Resolve:: "Resolve message"
/// }
///
///
/// let future = metacall_no_args::<MetaCallFuture>("func_always_resolve").unwrap();
/// future.then(resolve).catch(reject).await_fut();
/// }
/// ```
pub fn then(mut self, resolve: MetaCallFutureHandler) -> Self {
self.resolve = Some(resolve);

self
}

/// Adds a reject callback.
///
///
/// ## **Usage example:**
///
///
/// ```javascript
/// // Javascript script
/// function func_always_rejects(value, delay) {
/// return new Promise((_, reject) => {
/// reject('Error: Reject message.');
/// reject('Error: Reject message.');
/// });
/// }
/// ```
Expand All @@ -212,7 +212,7 @@ impl MetaCallFuture {
/// fn reject(error: impl MetaCallValue, _: impl MetaCallValue) {
/// println!("Reject:: error: {:#?}", error); // Reject:: error: "Error: Reject message"
/// }
///
///
/// let future = metacall_no_args::<MetaCallFuture>("func_always_rejects").unwrap();
/// future.then(resolve).catch(reject).await_fut();
/// }
Expand All @@ -224,11 +224,11 @@ impl MetaCallFuture {
}

/// Adds data to use it inside the `resolver` and `reject`.
///
///
/// Example:
/// ```rust
/// use metacall::{MetaCallValue, MetaCallFuture, metacall};
///
///
/// fn run() {
/// let x = 10;
/// fn resolve(result: impl MetaCallValue, data: impl MetaCallValue) {
Expand Down

0 comments on commit d901405

Please sign in to comment.