node-js 0.1.1

JavaScript as a fusevm frontend: a lexer/parser and compiler to fusevm::Chunk on a JsHost object heap, with no bespoke VM or JIT
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
//! Node `assert` module. Failing assertions throw an `AssertionError` (returned
//! as an `Err`, which the host surfaces as a thrown JS exception).

use crate::host::{
    call_method, invoke, is_callable, promise_of, reject_promise_val, resolve_promise_val,
    subscribe_native, take_exc_or_error, type_error, with_host, JsObj, PromiseState,
};
use fusevm::Value;

pub const METHODS: &[&str] = &[
    "ok",
    "equal",
    "notEqual",
    "strictEqual",
    "notStrictEqual",
    "deepEqual",
    "notDeepEqual",
    "deepStrictEqual",
    "notDeepStrictEqual",
    "throws",
    "doesNotThrow",
    "fail",
    "match",
    "doesNotMatch",
    "ifError",
    "partialDeepStrictEqual",
    "rejects",
    "doesNotReject",
];

pub fn call(method: &str, args: &[Value]) -> Option<Result<Value, String>> {
    let a = || args.first().cloned().unwrap_or(Value::Undef);
    let b = || args.get(1).cloned().unwrap_or(Value::Undef);
    Some(match method {
        "ok" => assert_ok(args),
        "equal" => check(loose_eq(&a(), &b()), args, 2, "==", &a(), &b()),
        "notEqual" => check(!loose_eq(&a(), &b()), args, 2, "!=", &a(), &b()),
        "strictEqual" => check(strict(&a(), &b()), args, 2, "strictEqual", &a(), &b()),
        "notStrictEqual" => check(!strict(&a(), &b()), args, 2, "notStrictEqual", &a(), &b()),
        "deepEqual" => check(
            deep_equal(&a(), &b(), false),
            args,
            2,
            "deepEqual",
            &a(),
            &b(),
        ),
        "notDeepEqual" => check(
            !deep_equal(&a(), &b(), false),
            args,
            2,
            "notDeepEqual",
            &a(),
            &b(),
        ),
        "deepStrictEqual" => check(
            deep_equal(&a(), &b(), true),
            args,
            2,
            "deepStrictEqual",
            &a(),
            &b(),
        ),
        "notDeepStrictEqual" => check(
            !deep_equal(&a(), &b(), true),
            args,
            2,
            "notDeepStrictEqual",
            &a(),
            &b(),
        ),
        "throws" => throws(args, true),
        "doesNotThrow" => throws(args, false),
        "fail" => Err(fail_msg(args, 0, "Failed")),
        "match" => assert_match(args, true),
        "doesNotMatch" => assert_match(args, false),
        "ifError" => if_error(&a()),
        "partialDeepStrictEqual" => partial(&a(), &b(), args),
        "rejects" => Ok(rejects_impl(&a(), true)),
        "doesNotReject" => Ok(rejects_impl(&a(), false)),
        _ => return None,
    })
}

/// The strict-mode variants (`assert.strict.equal` === `assert.strictEqual`).
/// Maps the loose method names onto their strict counterparts, then delegates.
pub fn strict_call(method: &str, args: &[Value]) -> Option<Result<Value, String>> {
    let mapped = match method {
        "equal" => "strictEqual",
        "notEqual" => "notStrictEqual",
        "deepEqual" => "deepStrictEqual",
        "notDeepEqual" => "notDeepStrictEqual",
        other => other,
    };
    call(mapped, args)
}

/// `assert.match(string, regexp)` / `assert.doesNotMatch(...)`. `regexp` must be
/// a `RegExp`; matching runs through the JS `RegExp.prototype.test`.
fn assert_match(args: &[Value], want_match: bool) -> Result<Value, String> {
    let s = args.first().cloned().unwrap_or(Value::Undef);
    let re = args.get(1).cloned().unwrap_or(Value::Undef);
    if !with_host(|h| matches!(h.get(&re), Some(JsObj::RegExp(_)))) {
        return Err(type_error(
            "The \"regexp\" argument must be an instance of RegExp.",
        ));
    }
    let matched = call_method(&re, "test", vec![s.clone()])?;
    let matched = with_host(|h| h.truthy(&matched));
    if matched == want_match {
        return Ok(Value::Undef);
    }
    if let Some(m) = message(args, 2) {
        return Err(assertion_error(&m));
    }
    let (sre, sstr) = with_host(|h| (h.inspect(&re), h.str_of(&s)));
    let verb = if want_match {
        "The input did not match the regular expression"
    } else {
        "The input was expected to not match the regular expression"
    };
    Err(assertion_error(&format!("{verb} {sre}. Input: '{sstr}'")))
}

/// `assert.ifError(value)` — throws unless `value` is `null`/`undefined`.
fn if_error(v: &Value) -> Result<Value, String> {
    if with_host(|h| h.is_nullish(v)) {
        return Ok(Value::Undef);
    }
    let desc = with_host(|h| match h.get(v) {
        Some(JsObj::Object(p)) => p
            .get("message")
            .map(|m| h.str_of(m))
            .unwrap_or_else(|| h.inspect(v)),
        _ => h.inspect(v),
    });
    Err(assertion_error(&format!(
        "ifError got unwanted exception: {desc}"
    )))
}

/// `assert.partialDeepStrictEqual(actual, expected)` — passes when every leaf of
/// `expected` strict-deep-matches the corresponding part of `actual` (extra
/// props/elements in `actual` are ignored).
fn partial(actual: &Value, expected: &Value, args: &[Value]) -> Result<Value, String> {
    if partial_deep(actual, expected) {
        return Ok(Value::Undef);
    }
    if let Some(m) = message(args, 2) {
        return Err(assertion_error(&m));
    }
    let (sa, sb) = with_host(|h| (h.inspect(actual), h.inspect(expected)));
    Err(assertion_error(&format!(
        "Expected values to be strictly deep-equal (partial):\n{sb} should be a subset of {sa}"
    )))
}

fn partial_deep(actual: &Value, expected: &Value) -> bool {
    let ekind = with_host(|h| h.get(expected).map(kind));
    match ekind {
        Some(Kind::Object) => {
            if !matches!(with_host(|h| h.get(actual).map(kind)), Some(Kind::Object)) {
                return false;
            }
            let (ea, ee) = with_host(|h| (object_of(h, actual), object_of(h, expected)));
            ee.iter().all(|(k, ve)| {
                ea.iter()
                    .find(|(k2, _)| k2 == k)
                    .is_some_and(|(_, va)| partial_deep(va, ve))
            })
        }
        Some(Kind::Array) => {
            if !matches!(with_host(|h| h.get(actual).map(kind)), Some(Kind::Array)) {
                return false;
            }
            let (ia, ie) = with_host(|h| (array_of(h, actual), array_of(h, expected)));
            ie.len() <= ia.len() && ie.iter().zip(ia.iter()).all(|(e, a)| partial_deep(a, e))
        }
        _ => strict(actual, expected),
    }
}

/// `assert.rejects(fn|promise)` / `assert.doesNotReject(...)` — returns a Promise
/// that fulfills when the operand settles the expected way, else rejects with an
/// `AssertionError`.
fn rejects_impl(input: &Value, want_reject: bool) -> Value {
    let result = with_host(|h| h.new_promise());
    let rid = with_host(|h| h.promise_id(&result).unwrap());
    // Reduce the operand to a promise: call it if it is a function.
    let operand = if with_host(|h| is_callable(h, input)) {
        match invoke(input, Vec::new(), None) {
            Ok(v) => promise_of(&v),
            Err(e) => {
                let ev = take_exc_or_error(&e);
                let p = with_host(|h| h.new_promise());
                let pid = with_host(|h| h.promise_id(&p).unwrap());
                reject_promise_val(pid, ev);
                p
            }
        }
    } else {
        promise_of(input)
    };
    let Some(oid) = with_host(|h| h.promise_id(&operand)) else {
        // Not thenable: treat as an immediate non-rejection.
        settle_rejects(rid, false, want_reject);
        return result;
    };
    subscribe_native(
        oid,
        Box::new(move |state, _val| {
            settle_rejects(rid, state == PromiseState::Rejected, want_reject);
            Ok(())
        }),
    );
    result
}

/// `new assert.AssertionError(options)` — a real `Error`-prototype-linked object
/// carrying `name`/`message`/`code`/`actual`/`expected`/`operator`. Parent wires
/// this to `construct("AssertionError")` and `constant("assert","AssertionError")`.
pub fn construct_assertion_error(args: &[Value]) -> Value {
    let opts = args.first().cloned().unwrap_or(Value::Undef);
    let (message, actual, expected, operator) = with_host(|h| match h.get(&opts) {
        Some(JsObj::Object(p)) => (
            p.get("message").map(|v| h.str_of(v)),
            p.get("actual").cloned(),
            p.get("expected").cloned(),
            p.get("operator").map(|v| h.str_of(v)),
        ),
        _ => (None, None, None, None),
    });
    let generated = message.is_none();
    let msg = message.unwrap_or_else(|| {
        let (sa, se) = with_host(|h| {
            (
                actual.as_ref().map(|v| h.inspect(v)).unwrap_or_default(),
                expected.as_ref().map(|v| h.inspect(v)).unwrap_or_default(),
            )
        });
        let op = operator.clone().unwrap_or_else(|| "==".to_string());
        format!("{sa} {op} {se}")
    });
    let stack = format!("AssertionError [ERR_ASSERTION]: {msg}\n    at <anonymous>");
    let op_val = operator
        .map(|o| with_host(|h| h.new_str(o)))
        .unwrap_or(Value::Undef);
    let name_v = with_host(|h| h.new_str("AssertionError"));
    let msg_v = with_host(|h| h.new_str(msg));
    let code_v = with_host(|h| h.new_str("ERR_ASSERTION"));
    let stack_v = with_host(|h| h.new_str(stack));
    let mut props: indexmap::IndexMap<String, Value> = indexmap::IndexMap::new();
    props.insert("name".into(), name_v);
    props.insert("message".into(), msg_v);
    props.insert("code".into(), code_v);
    props.insert("actual".into(), actual.unwrap_or(Value::Undef));
    props.insert("expected".into(), expected.unwrap_or(Value::Undef));
    props.insert("operator".into(), op_val);
    props.insert("generatedMessage".into(), Value::Bool(generated));
    props.insert("stack".into(), stack_v);
    let obj = with_host(|h| h.new_object(props));
    with_host(|h| {
        h.ensure_error_protos();
        if let Some(p) = crate::host::error_proto_of(h, "Error") {
            h.set_proto(&obj, p);
        }
    });
    obj
}

fn settle_rejects(rid: u32, rejected: bool, want_reject: bool) {
    if rejected == want_reject {
        resolve_promise_val(rid, Value::Undef);
    } else {
        let msg = if want_reject {
            "AssertionError [ERR_ASSERTION]: Missing expected rejection."
        } else {
            "AssertionError [ERR_ASSERTION]: Got unwanted rejection."
        };
        let ev = with_host(|h| crate::builtins::synth_error(h, msg));
        reject_promise_val(rid, ev);
    }
}

/// `assert(value[, message])` — throws unless `value` is truthy.
pub fn assert_ok(args: &[Value]) -> Result<Value, String> {
    let v = args.first().cloned().unwrap_or(Value::Undef);
    if with_host(|h| h.truthy(&v)) {
        Ok(Value::Undef)
    } else {
        Err(fail_msg(
            args,
            1,
            "The expression evaluated to a falsy value",
        ))
    }
}

fn check(
    pass: bool,
    args: &[Value],
    msg_idx: usize,
    op: &str,
    a: &Value,
    b: &Value,
) -> Result<Value, String> {
    if pass {
        return Ok(Value::Undef);
    }
    if let Some(m) = message(args, msg_idx) {
        return Err(assertion_error(&m));
    }
    let (sa, sb) = with_host(|h| (h.inspect(a), h.inspect(b)));
    Err(assertion_error(&format!("{sa} {op} {sb}")))
}

fn throws(args: &[Value], want_throw: bool) -> Result<Value, String> {
    let f = args.first().cloned().unwrap_or(Value::Undef);
    let threw = invoke(&f, Vec::new(), None).is_err();
    match (threw, want_throw) {
        (true, true) | (false, false) => Ok(Value::Undef),
        (false, true) => Err(assertion_error("Missing expected exception.")),
        (true, false) => Err(assertion_error("Got unwanted exception.")),
    }
}

fn message(args: &[Value], idx: usize) -> Option<String> {
    match args.get(idx) {
        Some(Value::Undef) | None => None,
        Some(v) => Some(with_host(|h| h.str_of(v))),
    }
}

fn fail_msg(args: &[Value], idx: usize, default: &str) -> String {
    assertion_error(&message(args, idx).unwrap_or_else(|| default.to_string()))
}

fn assertion_error(msg: &str) -> String {
    format!("AssertionError [ERR_ASSERTION]: {msg}")
}

fn strict(a: &Value, b: &Value) -> bool {
    with_host(|h| h.strict_eq(a, b))
}

fn loose_eq(a: &Value, b: &Value) -> bool {
    if strict(a, b) {
        return true;
    }
    with_host(|h| {
        let (na, nb) = (h.to_number(a), h.to_number(b));
        if !na.is_nan() && !nb.is_nan() && (na == nb) {
            return true;
        }
        h.str_of(a) == h.str_of(b)
    })
}

/// Structural equality. `strict` compares leaves with `===`, otherwise `==`.
pub fn deep_equal(a: &Value, b: &Value, strict_mode: bool) -> bool {
    let kinds = with_host(|h| {
        let av = h.get(a).map(kind);
        let bv = h.get(b).map(kind);
        (av, bv)
    });
    match kinds {
        (Some(Kind::Array), Some(Kind::Array)) => {
            let (ia, ib) = with_host(|h| (array_of(h, a), array_of(h, b)));
            ia.len() == ib.len()
                && ia
                    .iter()
                    .zip(ib.iter())
                    .all(|(x, y)| deep_equal(x, y, strict_mode))
        }
        (Some(Kind::Object), Some(Kind::Object)) => {
            let (ea, eb) = with_host(|h| (object_of(h, a), object_of(h, b)));
            if ea.len() != eb.len() {
                return false;
            }
            ea.iter().all(|(k, va)| {
                eb.iter()
                    .find(|(k2, _)| k2 == k)
                    .is_some_and(|(_, vb)| deep_equal(va, vb, strict_mode))
            })
        }
        _ => {
            if strict_mode {
                strict(a, b)
            } else {
                loose_eq(a, b)
            }
        }
    }
}

enum Kind {
    Array,
    Object,
}
fn kind(o: &JsObj) -> Kind {
    match o {
        JsObj::Array(_) => Kind::Array,
        _ => Kind::Object,
    }
}
fn array_of(h: &crate::host::JsHost, v: &Value) -> Vec<Value> {
    match h.get(v) {
        Some(JsObj::Array(items)) => items.clone(),
        _ => Vec::new(),
    }
}
fn object_of(h: &crate::host::JsHost, v: &Value) -> Vec<(String, Value)> {
    match h.get(v) {
        Some(JsObj::Object(p)) => p
            .iter()
            .filter(|(k, _)| !k.starts_with("@@") && !k.starts_with('#'))
            .map(|(k, v)| (k.clone(), v.clone()))
            .collect(),
        _ => Vec::new(),
    }
}