-
Notifications
You must be signed in to change notification settings - Fork 2
QuickJS in Wasm in Nix #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| [package] | ||
| name = "nix-wasm-plugin-quickjs" | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
|
|
||
| [lib] | ||
| crate-type = ["cdylib"] | ||
|
|
||
| [dependencies] | ||
| nix-wasm-rust = { path = "../nix-wasm-rust" } | ||
| rquickjs = { version = "0.11.0", features = ["bindgen"] } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| use nix_wasm_rust::{nix_wasm_init_v1, warn, wasi_arg, Value}; | ||
| use rquickjs::{Array, Context, Object, Runtime, Value as JsValue}; | ||
| use std::string::String as StdString; | ||
|
|
||
| fn fail(context: &str, err: impl std::fmt::Display) -> ! { | ||
| warn!("quickjs {context} failed: {err}"); | ||
| panic!("quickjs {context} failed: {err}"); | ||
| } | ||
|
|
||
| fn js_value_to_nix(value: JsValue) -> Value { | ||
| if value.is_null() || value.is_undefined() { | ||
| return Value::make_null(); | ||
| } | ||
| if let Some(b) = value.as_bool() { | ||
| return Value::make_bool(b); | ||
| } | ||
| if let Some(i) = value.as_int() { | ||
| return Value::make_int(i as i64); | ||
| } | ||
| if let Some(f) = value.as_float() { | ||
| return Value::make_float(f); | ||
| } | ||
| if let Some(js_str) = value.as_string() { | ||
| let s = js_str.to_string().unwrap_or_else(|err| fail("string conversion", err)); | ||
| return Value::make_string(&s); | ||
| } | ||
| if value.is_array() { | ||
| let array: Array = value | ||
| .clone() | ||
| .into_array() | ||
| .unwrap_or_else(|| fail("array conversion", "value is not an array")); | ||
| let mut items = Vec::new(); | ||
| for entry in array.into_iter() { | ||
| let entry = entry.unwrap_or_else(|err| fail("array iteration", err)); | ||
| items.push(js_value_to_nix(entry)); | ||
| } | ||
| return Value::make_list(&items); | ||
| } | ||
| if value.is_object() { | ||
| let object: Object = value | ||
| .into_object() | ||
| .unwrap_or_else(|| fail("object conversion", "value is not an object")); | ||
| let mut entries: Vec<(StdString, Value)> = Vec::new(); | ||
| for entry in object.props::<StdString, JsValue>() { | ||
| let (key, value) = entry.unwrap_or_else(|err| fail("object iteration", err)); | ||
| entries.push((key, js_value_to_nix(value))); | ||
| } | ||
| let attrs: Vec<(&str, Value)> = entries | ||
| .iter() | ||
| .map(|(key, value)| (key.as_str(), *value)) | ||
| .collect(); | ||
| return Value::make_attrset(&attrs); | ||
| } | ||
|
Comment on lines
+10
to
+53
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add recursion guard for nested/cyclic JS values. Lines [10-53] recurse without depth/cycle limits; deeply nested or self-referential objects can overflow the stack instead of failing cleanly. Proposed hardening patch-fn js_value_to_nix(value: JsValue) -> Value {
+const MAX_CONVERSION_DEPTH: usize = 256;
+
+fn js_value_to_nix(value: JsValue) -> Value {
+ js_value_to_nix_with_depth(value, 0)
+}
+
+fn js_value_to_nix_with_depth(value: JsValue, depth: usize) -> Value {
+ if depth > MAX_CONVERSION_DEPTH {
+ fail("value conversion", "maximum nesting depth exceeded");
+ }
if value.is_null() || value.is_undefined() {
return Value::make_null();
}
@@
if value.is_array() {
let array: Array = value
.clone()
.into_array()
.unwrap_or_else(|| fail("array conversion", "value is not an array"));
let mut items = Vec::new();
for entry in array.into_iter() {
let entry = entry.unwrap_or_else(|err| fail("array iteration", err));
- items.push(js_value_to_nix(entry));
+ items.push(js_value_to_nix_with_depth(entry, depth + 1));
}
return Value::make_list(&items);
}
@@
let mut entries: Vec<(StdString, Value)> = Vec::new();
for entry in object.props::<StdString, JsValue>() {
let (key, value) = entry.unwrap_or_else(|err| fail("object iteration", err));
- entries.push((key, js_value_to_nix(value)));
+ entries.push((key, js_value_to_nix_with_depth(value, depth + 1)));
}🤖 Prompt for AI Agents |
||
|
|
||
| warn!("quickjs value type not supported: {:?}", value.type_of()); | ||
| panic!("quickjs value type not supported: {:?}", value.type_of()); | ||
| } | ||
|
|
||
| fn eval_impl(arg: Value) -> Value { | ||
| let code = arg.get_string(); | ||
|
|
||
| let runtime = Runtime::new().unwrap_or_else(|err| fail("runtime init", err)); | ||
| let context = Context::full(&runtime).unwrap_or_else(|err| fail("context init", err)); | ||
|
|
||
| context.with(|ctx| { | ||
| let value: JsValue = ctx.eval(code).unwrap_or_else(|err| fail("eval", err)); | ||
| js_value_to_nix(value) | ||
| }) | ||
| } | ||
|
|
||
| #[no_mangle] | ||
| pub extern "C" fn eval(arg: Value) -> Value { | ||
| nix_wasm_init_v1(); | ||
| eval_impl(arg) | ||
| } | ||
|
|
||
| #[no_mangle] | ||
| pub extern "C" fn _start() { | ||
| nix_wasm_init_v1(); | ||
| let result = eval_impl(wasi_arg()); | ||
| result.return_to_nix(); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| {"arr":[1,"two",false],"bool":true,"float":1.5,"nil":null,"num":1,"obj":{"nested":3},"str":"hello","undef":null} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| builtins.wasi <plugins/nix_wasm_plugin_quickjs.wasm> '' | ||
| ({ | ||
| num: 1, | ||
| float: 1.5, | ||
| str: "hello", | ||
| bool: true, | ||
| nil: null, | ||
| undef: undefined, | ||
| arr: [1, "two", false], | ||
| obj: { nested: 3 } | ||
| }) | ||
| '' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
# First, check the flake.nix file content to understand the setup head -50 flake.nixRepository: DeterminateSystems/nix-wasm-rust
Length of output: 2162
🏁 Script executed:
Repository: DeterminateSystems/nix-wasm-rust
Length of output: 1142
🏁 Script executed:
Repository: DeterminateSystems/nix-wasm-rust
Length of output: 731
🏁 Script executed:
Repository: DeterminateSystems/nix-wasm-rust
Length of output: 3237
🏁 Script executed:
Repository: DeterminateSystems/nix-wasm-rust
Length of output: 2644
🏁 Script executed:
Repository: DeterminateSystems/nix-wasm-rust
Length of output: 510
Hard-coded Rust version dependency is rigid; ensure it remains compatible with nixpkgs updates.
The reference to
rustPackages_1_89depends on the pinned nixpkgs providing this attribute. While the current flake.lock (Jan 2025) should include Rust 1.89, this creates a maintenance risk if nixpkgs is updated to an older version or if the attribute is removed in future nixpkgs releases. Consider adding version flexibility or explicit handling for version availability.🤖 Prompt for AI Agents