Skip to content

Commit

Permalink
Added Async Functions to File System Module
Browse files Browse the repository at this point in the history
Added asynchronous function support to js_fn macro
Improved Future to Promise API to accept more types
Modified functions to accept &str where possible
Cleaned up default module exports
Updated to Rust 2021 Edition
Added license to crate manifests
  • Loading branch information
Redfire75369 committed Nov 17, 2021
1 parent f4e4483 commit 7a2faf5
Show file tree
Hide file tree
Showing 22 changed files with 524 additions and 162 deletions.
125 changes: 125 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
[package]
name = "cli"
version = "0.1.0"
edition = "2021"
authors = ["Redfire <redfire75369@hotmail.com>"]
edition = "2018"
license = "MPL-2.0"

[dependencies]
clap = "2.33.3"
Expand Down
2 changes: 1 addition & 1 deletion cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ fn main() {
CONFIG
.set(Config::default().log_level(log_level).script(subcmd.is_present("script")))
.expect("Config Initialisation Failed");
run::run(&String::from(subcmd.value_of("path").unwrap_or("./main.js")));
run::run(subcmd.value_of("path").unwrap_or("./main.js"));
}
_ => (),
}
Expand Down
3 changes: 2 additions & 1 deletion ion-proc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
[package]
name = "ion-proc"
version = "0.1.0"
edition = "2021"
authors = ["Redfire <redfire75369@hotmail.com>"]
edition = "2018"
license = "MPL-2.0"

[lib]
proc-macro = true
Expand Down
3 changes: 2 additions & 1 deletion ion/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
[package]
name = "ion"
version = "0.1.0"
edition = "2021"
authors = ["Redfire <redfire75369@hotmail.com>"]
edition = "2018"
license = "MPL-2.0"

[dependencies]
chrono = "0.4.19"
Expand Down
12 changes: 4 additions & 8 deletions ion/src/exception.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,10 @@ impl Exception {
let exception = IonObject::from(exception.to_object());
Exception::clear(cx);

let message = exception.get_as::<String>(cx, String::from("message"), ()).unwrap();
let filename = exception.get_as::<String>(cx, String::from("fileName"), ()).unwrap();
let lineno = exception
.get_as::<u32>(cx, String::from("lineNumber"), ConversionBehavior::Clamp)
.unwrap();
let column = exception
.get_as::<u32>(cx, String::from("columnNumber"), ConversionBehavior::Clamp)
.unwrap();
let message = exception.get_as::<String>(cx, "message", ()).unwrap();
let filename = exception.get_as::<String>(cx, "fileName", ()).unwrap();
let lineno = exception.get_as::<u32>(cx, "lineNumber", ConversionBehavior::Clamp).unwrap();
let column = exception.get_as::<u32>(cx, "columnNumber", ConversionBehavior::Clamp).unwrap();

Some(Exception {
message,
Expand Down
2 changes: 1 addition & 1 deletion ion/src/functions/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl IonFunction {
}

/// Creates a new [IonFunction] with the given name, native function, number of arguments and flags.
pub unsafe fn new(cx: IonContext, name: String, func: Option<IonNativeFunction>, nargs: u32, flags: u32) -> IonFunction {
pub unsafe fn new(cx: IonContext, name: &str, func: Option<IonNativeFunction>, nargs: u32, flags: u32) -> IonFunction {
let name = format!("{}\0", name);
IonFunction::from(JS_NewFunction(cx, func, nargs, flags, name.as_ptr() as *const i8))
}
Expand Down
22 changes: 19 additions & 3 deletions ion/src/functions/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ macro_rules! js_fn_raw_m {
unsafe extern "C" fn $name(cx: IonContext, argc: u32, vp: *mut Value) -> bool {
use ::mozjs::conversions::ToJSValConvertible;

let args = Arguments::new(argc, vp);
let args = $crate::functions::arguments::Arguments::new(argc, vp);

unsafe fn native_fn($($param : $type),*) -> IonResult<$ret> $body

Expand All @@ -26,7 +26,7 @@ macro_rules! js_fn_raw_m {
}
}
}
}
};
}

#[macro_export]
Expand All @@ -40,7 +40,23 @@ macro_rules! js_fn_m {

$body
});
}
};
(async unsafe fn $name:ident($($args:tt)*) -> Result<$res:ty, $rej:ty> $body:tt) => {
js_fn_raw_m!(unsafe fn $name(cx: IonContext, args: &Arguments) -> IonResult<$crate::objects::promise::IonPromise> {
#[allow(unused_imports)]
use ::mozjs::conversions::FromJSValConvertible;

unpack_args!((stringify!($name), cx, args) ($($args)*));

let future = async $body;

if let Some(promise) = $crate::objects::promise::IonPromise::new_with_future(cx, future) {
Ok(promise)
} else {
Err($crate::error::IonError::None)
}
});
};
}

#[macro_export]
Expand Down
Loading

0 comments on commit 7a2faf5

Please sign in to comment.