Skip to content

Commit

Permalink
Merge branch 'main' into developments
Browse files Browse the repository at this point in the history
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
adrien-zinger committed May 1, 2023
2 parents d94cf39 + 975f0ff commit da65e44
Show file tree
Hide file tree
Showing 13 changed files with 693 additions and 252 deletions.
13 changes: 3 additions & 10 deletions Cargo.lock

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

11 changes: 5 additions & 6 deletions Cargo.toml
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" }
62 changes: 53 additions & 9 deletions examples/callback.rs
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())
}
}
}
24 changes: 24 additions & 0 deletions examples/callback_closure.rs
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)
}
11 changes: 6 additions & 5 deletions examples/hello.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ use rusty_jsc::JSContext;

fn main() {
let mut context = JSContext::default();
match context.evaluate_script("'hello, world'", 1) {
Ok(value) => println!("{}", value.to_string(&context)),
Err(err) => {
println!("Uncaught: {}", err.to_string(&context));
}
let value = context.evaluate_script("'hello, world'", 1);
if let Ok(value) = value {
println!("{}", value.to_js_string(&context).unwrap());
} else {
let ex = value.unwrap_err().to_js_string(&context).unwrap();
println!("Uncaught: {}", ex);
}
}
12 changes: 7 additions & 5 deletions examples/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ use rusty_jsc::{JSContext, JSValue};
fn main() {
let mut context = JSContext::default();
let mut global = context.get_global_object();
let hello = JSValue::string(&context, "hello, world!".to_string()).unwrap();
global.set_property(&context, "hello".to_string(), hello);
let hello = JSValue::string(&context, "hello, world!");
global.set_property(&context, "hello", hello).unwrap();
match context.evaluate_script("hello", 1) {
Ok(value) => println!("{}", value.to_string(&context)),
Err(err) => {
println!("Uncaught: {}", err.to_string(&context));
Ok(value) => {
println!("{}", value.to_js_string(&context).unwrap());
}
Err(e) => {
println!("Uncaught: {}", e.to_js_string(&context).unwrap())
}
}
}
2 changes: 1 addition & 1 deletion macros/Cargo.lock

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

6 changes: 4 additions & 2 deletions macros/Cargo.toml
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"

Expand Down
Loading

0 comments on commit da65e44

Please sign in to comment.