-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into developments
Update parrallel merged development from rusty_jsc and fix some details. * early return in callbacks issue fixed * remove useless content and duplicated development
- Loading branch information
Showing
13 changed files
with
693 additions
and
252 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,15 @@ | ||
[package] | ||
name = "rusty_jsc" | ||
version = "0.0.3" | ||
version = "0.1.0" | ||
description = "Rust bindings for the JavaScriptCore engine" | ||
keywords = [ "javascriptcore", "javascript" ] | ||
authors = [ "Pekka Enberg" ] | ||
authors = [ "Wasmer Engineering Team <[email protected]>", "Pekka Enberg" ] | ||
license = "MIT" | ||
repository = "https://github.com/penberg/rusty_jsc" | ||
repository = "https://github.com/wasmerio/rusty_jsc" | ||
edition = "2021" | ||
|
||
[lib] | ||
|
||
[dependencies] | ||
anyhow = "1.0.62" | ||
rusty_jsc_macros = { path = "./macros", version = "0.0.3" } | ||
rusty_jsc_sys = { path = "./sys", version = "0.0.3" } | ||
rusty_jsc_macros = { path = "./macros", version = "0.1.0" } | ||
rusty_jsc_sys = { path = "./sys", version = "0.1.0" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,65 @@ | ||
use rusty_jsc::{JSContext, JSValue}; | ||
use rusty_jsc::{JSContext, JSObject, JSValue}; | ||
use rusty_jsc_macros::callback; | ||
|
||
// The JavaScript code calls this Rust function. | ||
#[callback] | ||
fn foo(_context: JSContext) { | ||
fn example( | ||
ctx: JSContext, | ||
_function: JSObject, | ||
_this: JSObject, | ||
args: &[JSValue], | ||
) -> Result<JSValue, JSValue> { | ||
println!( | ||
"hello from Rust land! len: {}, value[0]: {}", | ||
args.len(), | ||
args[0].to_js_string(&ctx).unwrap() | ||
); | ||
Ok(JSValue::string(&ctx, "Returning a string to JS!")) | ||
} | ||
|
||
#[callback] | ||
#[allow(unused)] // Just for the example | ||
fn example2<T>( | ||
ctx: JSContext, | ||
_function: JSObject, | ||
_this: JSObject, | ||
_args: &[JSValue], | ||
) -> Result<JSValue, JSValue> | ||
where | ||
T: Clone, | ||
{ | ||
println!("hello from Rust land!"); | ||
Ok(JSValue::string(&ctx, "Hey")) | ||
} | ||
|
||
fn main() { | ||
let mut context = JSContext::default(); | ||
let callback = JSValue::callback(&context, Some(foo)); | ||
let callback = JSValue::callback(&context, Some(example)); | ||
|
||
let mut global = context.get_global_object(); | ||
global.set_property(&context, "foo".to_string(), callback); | ||
match context.evaluate_script("foo()", 1) { | ||
Ok(value) => println!("{}", value.to_string(&context)), | ||
Err(err) => { | ||
println!("Uncaught: {}", err.to_string(&context)); | ||
global.set_property(&context, "example", callback).unwrap(); | ||
let example = global | ||
.get_property(&context, "example") | ||
.unwrap() | ||
.to_object(&context) | ||
.unwrap(); | ||
let result = example.call_as_function( | ||
&context, | ||
None, | ||
&[ | ||
JSValue::number(&context, 5f64), | ||
JSValue::number(&context, 6f64), | ||
], | ||
); | ||
println!( | ||
"direct call: {}", | ||
result.unwrap().to_js_string(&context).unwrap() | ||
); | ||
match context.evaluate_script("example(1, 2, 3)", 1) { | ||
Ok(value) => { | ||
println!("{}", value.to_js_string(&context).unwrap()); | ||
} | ||
Err(e) => { | ||
println!("Uncaught: {}", e.to_js_string(&context).unwrap()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
use rusty_jsc::{callback_closure, JSContext, JSValue}; | ||
|
||
fn main() { | ||
let context = JSContext::default(); | ||
|
||
let multiplier = 10f64; | ||
let callback = callback_closure!( | ||
&context, | ||
move |ctx: JSContext, _func: JSObject, _this: JSObject, args: &[JSValue]| { | ||
let num = args[0].to_number(&ctx).unwrap(); | ||
Ok(JSValue::number(&ctx, num * multiplier)) | ||
} | ||
); | ||
|
||
let result = callback | ||
.call_as_function( | ||
&context, | ||
Some(&callback), | ||
&[JSValue::number(&context, 5f64)], | ||
) | ||
.unwrap(); | ||
|
||
assert_eq!(result.to_number(&context).unwrap(), 50f64) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
[package] | ||
name = "rusty_jsc_macros" | ||
version = "0.0.3" | ||
version = "0.1.0" | ||
description = "Macros for rusty_jsc" | ||
authors = [ "Pekka Enberg" ] | ||
authors = [ "Wasmer Engineering Team <[email protected]>", "Pekka Enberg", "Adrien Zinger" ] | ||
repository = "https://github.com/wasmerio/rusty_jsc" | ||
license = "MIT" | ||
edition = "2021" | ||
|
||
[lib] | ||
proc-macro = true | ||
|
||
[target.'cfg(target_os = "linux")'.build-dependencies] | ||
pkg-config = "0.3.9" | ||
|
||
|
Oops, something went wrong.