quickjs-oxide 0.4.1

A safe Rust JavaScript engine targeting semantic parity with QuickJS
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
use super::*;
use crate::engine::test_support::test_context;

#[test]
fn ordinary_function_body_lexicals_execute_local_capture_and_constructor_paths() {
    assert_eq!(
        evaluate_in_context(
            "(function(){let x=1,y=x+1,z;return x*10+y+(typeof z==='undefined'?0:100)})()"
        ),
        Value::Int(12)
    );
    assert_eq!(
        evaluate_in_context("(function(){let arguments=3;let eval=4;return arguments+eval})()"),
        Value::Int(7)
    );
    assert_eq!(
        evaluate_in_context("(function(){var read=function(){return x};let x=7;return read()})()"),
        Value::Int(7)
    );
    assert_eq!(
        evaluate_in_context(
            "(function(){let x=function(){return x};return x()===x&&x.name==='x'})()"
        ),
        Value::Bool(true)
    );
    assert_eq!(
        evaluate_in_context(
            "(function(){let x=1;var next=function(){x+=1;return x};return next()*10+next()})()"
        ),
        Value::Int(23)
    );
    assert_eq!(
        evaluate_in_context("(function(){const x=4;return function(){return x}()})()"),
        Value::Int(4)
    );
    assert_eq!(
        evaluate_in_context("Function('let x=1;const y=2;return x+y')()"),
        Value::Int(3)
    );
    assert_eq!(
        evaluate_in_context("(function(){var result=delete x;let x=1;return result})()"),
        Value::Bool(false)
    );
    assert_eq!(
        evaluate_in_context("(function(){const x=0;return x&&=missing})()"),
        Value::Int(0)
    );
}

#[test]
fn lexical_lowering_publishes_tdz_vardefs_and_checked_capture_relays() {
    let script =
        compile_unlinked_script("(function(){let x=1;const y=2;return function(){x+=y;return x}})")
            .unwrap();
    let outer = script
        .constants()
        .iter()
        .find_map(|constant| constant.as_child())
        .expect("script lost its outer function");
    assert!(matches!(
        outer.code(),
        [
            Instruction::SetLocalUninitialized(1),
            Instruction::SetLocalUninitialized(0),
            Instruction::PushI32(1),
            Instruction::InitializeLocal(0),
            Instruction::PushI32(2),
            Instruction::InitializeLocal(1),
            ..
        ]
    ));
    assert_eq!(outer.local_definitions().len(), 2);
    assert_eq!(
        outer.local_definitions()[0].name.as_ref(),
        Some(&JsString::from_static("x"))
    );
    assert!(outer.local_definitions()[0].is_lexical);
    assert!(!outer.local_definitions()[0].is_const);
    assert_eq!(
        outer.local_definitions()[1].name.as_ref(),
        Some(&JsString::from_static("y"))
    );
    assert!(outer.local_definitions()[1].is_lexical);
    assert!(outer.local_definitions()[1].is_const);

    let inner = outer
        .constants()
        .iter()
        .find_map(|constant| constant.as_child())
        .expect("outer function lost its captured child");
    assert_eq!(inner.closure_variables().len(), 2);
    for (index, expected_name, is_const) in [(0, "x", false), (1, "y", true)] {
        let descriptor = inner.closure_variables()[index];
        assert_eq!(
            descriptor.source,
            ClosureSource::ParentLocal(u16::try_from(index).unwrap())
        );
        assert!(descriptor.is_lexical);
        assert_eq!(descriptor.is_const, is_const);
        assert_eq!(descriptor.kind, ClosureVariableKind::Normal);
        let ClosureVariableName::Constant(name) = descriptor.name else {
            panic!("lexical descriptor lost its source name");
        };
        assert_eq!(
            inner.constants()[usize::try_from(name).unwrap()].as_primitive(),
            Some(&crate::engine::value::PrimitiveValue::String(
                JsString::from_static(expected_name)
            ))
        );
    }
    assert!(inner.code().windows(5).any(|window| matches!(
        window,
        [
            Instruction::GetVarRefCheck(0),
            Instruction::GetVarRefCheck(1),
            Instruction::Add,
            Instruction::Dup,
            Instruction::PutVarRefCheck(0),
        ]
    )));
    assert!(inner.code().windows(2).any(|window| matches!(
        window,
        [Instruction::GetVarRefCheck(0), Instruction::Return]
    )));
}

#[test]
fn nested_block_and_switch_lexicals_lower_scope_lifetimes() {
    let script = compile_unlinked_script(
        "(function(){var read;{read=function(){return ++value};let value=40;}return read()*100+read();})()",
    )
    .unwrap();
    let outer = script
        .constants()
        .iter()
        .find_map(|constant| constant.as_child())
        .expect("script lost its block function");
    assert!(
        outer
            .code()
            .iter()
            .any(|instruction| matches!(instruction, Instruction::SetLocalUninitialized(1)))
    );
    assert!(
        outer
            .code()
            .iter()
            .any(|instruction| matches!(instruction, Instruction::CloseLocal(1)))
    );
    let child = outer
        .constants()
        .iter()
        .find_map(|constant| constant.as_child())
        .expect("block function lost its captured child");
    assert_eq!(
        child.closure_variables()[0].source,
        ClosureSource::ParentLocal(1)
    );
    assert_eq!(
        evaluate_in_context(
            "(function(){var read;{read=function(){return ++value};let value=40;}return read()*100+read();})()"
        ),
        Value::Int(4142)
    );

    let switch_script = compile_unlinked_script(
        "(function(){var read;switch(0){case 0:let value=40;read=function(){return ++value};break;}return read()*100+read();})()",
    )
    .unwrap();
    let switch_function = switch_script
        .constants()
        .iter()
        .find_map(|constant| constant.as_child())
        .expect("script lost its switch function");
    assert!(switch_function.code().windows(2).any(|window| matches!(
        window,
        [
            Instruction::PushI32(0),
            Instruction::SetLocalUninitialized(1)
        ]
    )));
    assert!(
        switch_function
            .code()
            .windows(2)
            .any(|window| matches!(window, [Instruction::Drop, Instruction::CloseLocal(1)]))
    );
    assert_eq!(
        evaluate_in_context(
            "(function(){var read;switch(0){case 0:let value=40;read=function(){return ++value};break;}return read()*100+read();})()"
        ),
        Value::Int(4142)
    );
    assert_eq!(evaluate_in_context("{let value=42;value;}"), Value::Int(42));
    assert_eq!(
        evaluate_in_context("switch(0){case 0:const value=42;value;}"),
        Value::Int(42)
    );

    let (runtime, context) = test_context();
    let Err(RuntimeError::Exception(Value::Object(thrown))) =
        context.eval("(function(){{let value=40;throw function(){return ++value};}})()")
    else {
        panic!("nested lexical throw did not preserve the escaped closure");
    };
    let callable = runtime.as_callable(&thrown).unwrap().unwrap();
    assert_eq!(
        context.call(&callable, Value::Undefined, &[]).unwrap(),
        Value::Int(41)
    );
    assert_eq!(
        context.call(&callable, Value::Undefined, &[]).unwrap(),
        Value::Int(42)
    );
}

#[test]
fn nested_lexical_cleanup_shadowing_and_quickjs_var_quirks_execute() {
    assert_eq!(
        evaluate_in_context(
            "(function(){var first,second,index=0;while(index<2){{let value=index++;if(index===1){first=function(){return ++value};continue;}second=function(){return ++value};}}return first()*100+second()*10+first()+second();})()"
        ),
        Value::Int(125)
    );
    assert_eq!(
        evaluate_in_context(
            "(function(){var first,second,index=0;outer:while(index<2){switch(0){case 0:let value=index++;if(index===1){first=function(){return ++value};continue outer;}second=function(){return ++value};break outer;}}return first()*100+second()*10+first()+second();})()"
        ),
        Value::Int(125)
    );
    assert_eq!(
        evaluate_in_context(
            "(function self(parameter){var outer='O',result;{let parameter='P',outer='B',self='S';result=parameter+outer+self;}return result+'|'+parameter+'|'+outer+'|'+typeof self;})('p')"
        ),
        Value::String(JsString::from_static("PBS|p|O|function"))
    );
    for source in [
        "(function(){var value;{var value;let value;}return 1})()",
        "(function(value){{var value;let value;}return 1})(0)",
    ] {
        assert_eq!(evaluate_in_context(source), Value::Int(1), "{source}");
    }
    for (source, message) in [
        (
            "(function(){var value;{let value;var value;}})",
            "invalid redefinition of lexical identifier",
        ),
        (
            "(function(){{var value;}let value;})",
            "invalid redefinition of a variable",
        ),
        (
            "(function(){let value;{var value;}})",
            "invalid redefinition of lexical identifier",
        ),
        (
            "(function(){switch(0){case 0:let value;case 1:const value=1;}})",
            "invalid redefinition of lexical identifier",
        ),
    ] {
        assert_eq!(
            compile_unlinked_script(source).unwrap_err().message(),
            message,
            "{source}"
        );
    }
}

#[test]
fn lexical_tdz_and_readonly_errors_follow_checked_local_and_capture_order() {
    let (runtime, mut context) = test_context();
    for source in [
        "(function(){return x;let x=1})()",
        "(function(){return typeof x;let x=1})()",
        "(function(){x=1;let x})()",
        "(function(){return function(){return x};let x=1})()()",
        "(function(){var set=function(){x=1};set();let x})()",
        "(function(){var add=function(){x+=missing};add();const x=1})()",
    ] {
        assert_eq!(
            evaluate_error(&runtime, &mut context, source),
            (
                JsString::from_static("ReferenceError"),
                JsString::from_static("x is not initialized")
            ),
            "{source}"
        );
    }
    for source in [
        "(function(){const x=1;x=2})()",
        "(function(){const x=1;return function(){x=2}})()()",
        "(function(){var set=function(){x=1};set();const x=2})()",
    ] {
        assert_eq!(
            evaluate_error(&runtime, &mut context, source),
            (
                JsString::from_static("TypeError"),
                JsString::from_static("'x' is read-only")
            ),
            "{source}"
        );
    }
}

#[test]
fn lexical_parser_matches_redefinition_priority_contextual_let_and_boundaries() {
    let syntax_cases = [
        (
            "(function(){\nlet x;\nlet x;\n})",
            "invalid redefinition of lexical identifier",
            3,
            6,
        ),
        (
            "(function(){\nvar x;\nlet x;\n})",
            "invalid redefinition of a variable",
            3,
            6,
        ),
        (
            "(function(){\nlet x;\nvar x;\n})",
            "invalid redefinition of lexical identifier",
            3,
            6,
        ),
        (
            "(function(x){\nlet x;\n})",
            "invalid redefinition of parameter name",
            2,
            6,
        ),
        (
            "(function(){\nconst x;\n})",
            "missing initializer for const variable",
            2,
            8,
        ),
    ];
    for (source, message, line, column) in syntax_cases {
        let error = compile_unlinked_script(source).unwrap_err();
        assert_eq!(error.kind(), ErrorKind::Syntax, "{source}");
        assert_eq!(error.message(), message, "{source}");
        let span = error.span().expect("syntax error lost its source span");
        assert_eq!(
            (span.start.line, span.start.column),
            (line, column),
            "{source}"
        );
    }
    for (source, message) in [
        (
            "(function(){let let=1})",
            "'let' is not a valid lexical identifier",
        ),
        (
            "(function(){'use strict';let eval=1})",
            "invalid variable name in strict mode",
        ),
        (
            "(function(){if(true) let x=1})",
            "lexical declarations can't appear in single-statement context",
        ),
    ] {
        assert_eq!(
            compile_unlinked_script(source).unwrap_err().message(),
            message
        );
    }

    assert_eq!(
        evaluate_in_context("(function(){var let=0;let=2;return let})()"),
        Value::Int(2)
    );
    assert_eq!(
        evaluate_in_context("(function(){var x=0;if(false) let\nx=1;return x})()"),
        Value::Int(1)
    );
    assert_eq!(
        evaluate_in_context("(function named(){let named=3;return named})()"),
        Value::Int(3)
    );

    assert_eq!(
        evaluate_in_context("(function(){let [[item]]=[[1]];return item})()"),
        Value::Int(1)
    );
    assert_eq!(
        evaluate_in_context("(function(){{let [[item]=[2]]=[];return item}})()"),
        Value::Int(2)
    );
    assert_eq!(
        evaluate_in_context(
            "(function(){switch(0){case 0:let [...[first,second]]=[3,4];return first+second}})()"
        ),
        Value::Int(7)
    );
    assert_eq!(
        evaluate_in_context("(function(){{let nested=1;return nested}})()"),
        Value::Int(1)
    );
    assert_eq!(
        evaluate_in_context("(function(){switch(0){case 0:let inCase=1;return inCase}})()"),
        Value::Int(1)
    );
}

#[test]
fn strip_debug_removes_lexical_tdz_names_but_not_readonly_atoms() {
    let runtime = Runtime::new();
    runtime
        .set_debug_info_mode(DebugInfoMode::StripDebug)
        .expect("set runtime configuration");
    let mut context = runtime.new_context().expect("create context");
    for source in [
        "(function(){return localName;let localName=1})()",
        "(function(){return function probe(){return capturedName};let capturedName=1})()()",
        "(function(){var read;outer:{read=function(){return blockName};break outer;let blockName=1;}return read();})()",
        "(function(){var read;switch(1){case 0:let switchName=1;case 1:read=function(){return switchName};break;}return read();})()",
    ] {
        assert_eq!(
            evaluate_error(&runtime, &mut context, source),
            (
                JsString::from_static("ReferenceError"),
                JsString::from_static("lexical variable is not initialized")
            )
        );
    }
    assert_eq!(
        evaluate_error(
            &runtime,
            &mut context,
            "(function(){var write;{const retainedName=1;write=function(){retainedName=2};}write();})()"
        ),
        (
            JsString::from_static("TypeError"),
            JsString::from_static("'retainedName' is read-only")
        )
    );
}