Skip to content

Commit 24197fb

Browse files
authored
feat: support undefined (#61)
1 parent 057e270 commit 24197fb

File tree

3 files changed

+36
-13
lines changed

3 files changed

+36
-13
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@ for your platform. If you wish to skip this action you can set the
3838

3939
```sh
4040
env SANDBOX_SKIP_DOWNLOAD=1 npm install --save sandbox
41-
```
41+
`****
42+
43+
## Usage
44+
45+
**NOTE**: As it stands, only values that can be serialized to JSON may be
46+
returned.
4247
4348
## About
4449

src/lib.rs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,42 @@ use std::{
55
thread,
66
};
77

8-
/// Recursively convert a `JSONValue` to a `JsValue`
9-
fn to_value<'a, C: Context<'a>>(cx: &mut C, value: JSONValue) -> Handle<'a, JsValue> {
8+
enum ToValue {
9+
Undefined,
10+
Json(JSONValue),
11+
}
12+
13+
/// Recursively convert a `ToValue` to a `JsValue`
14+
fn to_value<'a, C: Context<'a>>(cx: &mut C, value: ToValue) -> Handle<'a, JsValue> {
1015
match value {
11-
JSONValue::Null => cx.null().upcast::<JsValue>(),
12-
JSONValue::Bool(v) => cx.boolean(v).upcast::<JsValue>(),
13-
JSONValue::Number(v) => cx.number(v.as_f64().unwrap()).upcast::<JsValue>(),
14-
JSONValue::String(v) => cx.string(v).upcast::<JsValue>(),
15-
JSONValue::Array(v) => {
16+
ToValue::Json(JSONValue::Null) => cx.null().upcast::<JsValue>(),
17+
ToValue::Json(JSONValue::Bool(v)) => cx.boolean(v).upcast::<JsValue>(),
18+
ToValue::Json(JSONValue::Number(v)) => cx.number(v.as_f64().unwrap()).upcast::<JsValue>(),
19+
ToValue::Json(JSONValue::String(v)) => cx.string(v).upcast::<JsValue>(),
20+
ToValue::Json(JSONValue::Array(v)) => {
1621
let a = JsArray::new(cx, v.len() as u32);
1722

1823
for (i, s) in v.iter().enumerate() {
19-
let val = to_value(cx, s.to_owned());
24+
let val = to_value(cx, ToValue::Json(s.to_owned()));
2025
a.set(cx, i as u32, val).unwrap();
2126
}
2227

2328
a.upcast::<JsValue>()
2429
}
25-
JSONValue::Object(v) => {
30+
ToValue::Json(JSONValue::Object(v)) => {
2631
let o = cx.empty_object();
2732

2833
for (_, (k, s)) in v.iter().enumerate() {
2934
let key = cx.string(k);
30-
let val = to_value(cx, s.to_owned());
35+
let val = to_value(cx, ToValue::Json(s.to_owned()));
3136

3237
o.set(cx, key, val).unwrap();
3338
}
3439

3540
o.upcast::<JsValue>()
3641
}
42+
43+
ToValue::Undefined => cx.undefined().upcast::<JsValue>(),
3744
}
3845
}
3946

@@ -104,8 +111,9 @@ impl Shovel {
104111
shovel
105112
.send(move |context, channel| {
106113
let val = match context.eval(&source) {
107-
Ok(v) => Ok(v.to_json(context).unwrap()),
108-
Err(v) => Err(v.to_json(context).unwrap()),
114+
Ok(boa::Value::Undefined) => Ok(ToValue::Undefined),
115+
Ok(v) => Ok(ToValue::Json(v.to_json(context).unwrap())),
116+
Err(v) => Err(ToValue::Json(v.to_json(context).unwrap())),
109117
};
110118

111119
channel.send(move |mut cx| {

test/sandbox.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,16 @@ describe("Sandbox", function () {
9090
);
9191
});
9292

93+
it("should be able to bridge `undefined` values", function (done) {
94+
const s = new Sandbox();
95+
96+
s.eval("const a = 1;", function (err, res) {
97+
assert.equal(err, undefined);
98+
assert.equal(res, undefined);
99+
done();
100+
});
101+
});
102+
93103
it("should timeout on long computations");
94104
});
95105
});

0 commit comments

Comments
 (0)