Skip to content

Commit 4b7e31d

Browse files
simongdaviesCopilot
andcommitted
Apply suggestions from code review
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Signed-off-by: Simon Davies <simongdavies@users.noreply.github.com>
1 parent 4ac2aed commit 4b7e31d

File tree

5 files changed

+18
-7
lines changed

5 files changed

+18
-7
lines changed

src/hyperlight-js-runtime/src/host_fn.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,13 @@ fn value_to_json_with_binaries<'js>(
169169

170170
// Handle objects
171171
if let Some(obj) = value.as_object() {
172+
// Check for toJSON() method — matches JSON.stringify behaviour
173+
// (e.g., Date.toJSON() returns an ISO string, not {}).
174+
if let Ok(to_json) = obj.get::<_, Function>("toJSON") {
175+
let result: Value = to_json.call((rquickjs::function::This(obj.clone()),))?;
176+
return value_to_json_with_binaries(ctx, result, binaries, depth + 1);
177+
}
178+
172179
let mut json_obj = serde_json::Map::new();
173180
for entry in obj.props::<String, Value>() {
174181
let (key, val) = entry?;

src/hyperlight-js-runtime/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ impl JsRuntime {
125125

126126
/// Register a binary-capable host function in the specified module.
127127
///
128-
/// This variant supports `Uint8Array`/`ArrayBuffer` arguments and returns.
128+
/// This variant supports `Uint8Array` arguments and returns.
129129
/// Binary data is passed via a sidecar channel instead of JSON encoding,
130130
/// avoiding base64 overhead.
131131
///

src/hyperlight-js/src/sandbox/host_fn.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,10 @@ impl HostModule {
202202
// data is not supported — reject if blobs are present.
203203
if !blobs.is_empty() {
204204
return Err(crate::HyperlightError::Error(format!(
205-
"Function '{name}' received {} binary argument(s) but was registered \
206-
with `register` (typed JSON-only). Use `register_js` for functions \
207-
that accept Uint8Array/Buffer arguments.",
205+
"Function '{}' received {} binary argument(s) but was registered \
206+
with `register` (typed JSON-only). Use `register_js` for functions \
207+
that accept Uint8Array/Buffer arguments.",
208+
name,
208209
blobs.len()
209210
)));
210211
}

src/js-host-api/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -465,8 +465,9 @@ Node.js code.
465465
Host functions natively support `Uint8Array`/`Buffer` arguments and returns.
466466
Binary data travels through a dedicated sidecar channel, keeping overhead
467467
minimal. Top-level `Buffer` arguments and returns are passed as raw bytes
468-
with no encoding. Nested Buffers inside returned objects/arrays are
469-
serialized by napi-rs's default conversion.
468+
with no encoding. Nested `Buffer`/`Uint8Array` values inside returned objects
469+
and arrays are also extracted into the binary sidecar and restored on the
470+
guest side.
470471
471472
```javascript
472473
const { SandboxBuilder } = require('@hyperlight/js-host-api');

src/js-host-api/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1200,7 +1200,9 @@ unsafe fn napi_to_json_with_buffer_extraction(
12001200
}
12011201
Ok(serde_json::Value::Object(obj))
12021202
}
1203-
// Symbols, functions, bigints, external — produce null like JSON.stringify.
1203+
// Symbols, functions, bigints, external — currently coerced to JSON null for host
1204+
// return values. Note: this intentionally differs from JSON.stringify, which e.g.
1205+
// throws on BigInt and omits certain properties instead of serializing them to null.
12041206
_ => Ok(serde_json::Value::Null),
12051207
}
12061208
}

0 commit comments

Comments
 (0)