Skip to content

Commit 4065284

Browse files
author
Thomas Timmer
committed
feat: add javascriptcore implementation
1 parent c545a78 commit 4065284

File tree

6 files changed

+121
-1
lines changed

6 files changed

+121
-1
lines changed

Cargo.lock

+31
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ boa_gc = "0.16.0"
1212
deno_core = "*"
1313
deno_runtime = "0.115.0"
1414
gc = "0.4.1"
15+
rusty_jsc = "0.1.0"
1516
log = "*"
1617
mlua = { version = "0.8", features = ["luau", "vendored"] }
1718
pretty_env_logger = "*"

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ roblox lua syntax.
1313

1414
Three implementations:
1515

16-
- `Deno`, which uses v8 under the hood
16+
- `Deno`, which uses `v8` under the hood
1717
- `quickjs`, which uses its own engine.
1818
- `boa`, which uses its own engine written in Rust.
19+
- `rusty_jsc` which uses the `javascriptcore` engine
1920

2021
### Python
2122

examples/javascriptcore.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use embedded_languages;
2+
use rusty_jsc::JSValue;
3+
4+
fn main() -> Result<(), JSValue> {
5+
embedded_languages::init_logger();
6+
7+
let script = r#"
8+
console.log(1 + 5)
9+
console.debug(1 + 5)
10+
console.info(1 + 5)
11+
console.warn(1 + 5)
12+
console.error(1 + 5)
13+
14+
"#;
15+
16+
embedded_languages::javascriptcore::run(script)?;
17+
18+
Ok(())
19+
}

src/javascriptcore.rs

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
use rusty_jsc::JSContext;
2+
use rusty_jsc::JSObject;
3+
use rusty_jsc::JSValue;
4+
5+
fn inner_log(
6+
loglevel: log::Level,
7+
context: JSContext,
8+
_function: JSObject,
9+
_this: JSObject,
10+
args: &[JSValue],
11+
) -> Result<JSValue, JSValue> {
12+
let out: Result<Vec<String>, JSValue> = args
13+
.iter()
14+
.map(|x| {
15+
let y = x.to_string(&context)?;
16+
Ok(y.to_string())
17+
})
18+
.collect();
19+
let out = out?.join(" ");
20+
log::log!(loglevel, "{:?}", out);
21+
22+
Ok(JSValue::undefined(&context))
23+
}
24+
25+
#[rusty_jsc::callback]
26+
fn debug(context: JSContext, function: JSObject, this: JSObject, args: &[JSValue]) {
27+
inner_log(log::Level::Debug, context, function, this, args)
28+
}
29+
30+
#[rusty_jsc::callback]
31+
fn info(context: JSContext, function: JSObject, this: JSObject, args: &[JSValue]) {
32+
inner_log(log::Level::Info, context, function, this, args)
33+
}
34+
35+
#[rusty_jsc::callback]
36+
fn warn(context: JSContext, function: JSObject, this: JSObject, args: &[JSValue]) {
37+
inner_log(log::Level::Warn, context, function, this, args)
38+
}
39+
40+
#[rusty_jsc::callback]
41+
fn error(context: JSContext, function: JSObject, this: JSObject, args: &[JSValue]) {
42+
inner_log(log::Level::Error, context, function, this, args)
43+
}
44+
45+
pub fn run(script: &str) -> Result<(), JSValue> {
46+
let mut context = JSContext::default();
47+
48+
let console = JSObject::new(&context);
49+
let callback = JSValue::callback(&context, Some(info));
50+
console.set_property(&context, "log", callback)?;
51+
let callback = JSValue::callback(&context, Some(info));
52+
console.set_property(&context, "info", callback)?;
53+
let callback = JSValue::callback(&context, Some(debug));
54+
console.set_property(&context, "debug", callback)?;
55+
let callback = JSValue::callback(&context, Some(warn));
56+
console.set_property(&context, "warn", callback)?;
57+
let callback = JSValue::callback(&context, Some(error));
58+
console.set_property(&context, "error", callback)?;
59+
60+
let global = context.get_global_object();
61+
global.set_property(&context, "console", console.to_jsvalue())?;
62+
63+
if let Ok(_value) = context.evaluate_script(script, 0) {
64+
()
65+
}
66+
Ok(())
67+
}

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use log::LevelFilter;
22

33
pub mod boa;
44
pub mod deno;
5+
pub mod javascriptcore;
56
pub mod lua;
67
pub mod quickjs;
78
pub mod rhai;

0 commit comments

Comments
 (0)