pipa-js 0.1.1

A fast, minimal ES2023 JavaScript runtime built in Rust.
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
use pipa::{JSRuntime, eval};

fn assert_js_ok(ctx: &mut pipa::JSContext, code: &str, msg: &str) {
    let r = eval(ctx, code);
    assert!(r.is_ok(), "{}: {:?}", msg, r);
}

#[test]
fn test_object_creation() {
    let mut runtime = JSRuntime::new();
    let mut ctx = runtime.new_context();

    assert!(eval(&mut ctx, "var obj = {a: 1, b: 2}").is_ok());
    assert!(eval(&mut ctx, "var empty = {}").is_ok());
}

#[test]
fn test_regular_for_loop() {
    let mut runtime = JSRuntime::new();
    let mut ctx = runtime.new_context();

    assert_js_ok(
        &mut ctx,
        "var sum = 0; for (var i = 0; i < 3; i++) { sum = sum + 1; } if (sum !== 3) throw new Error('for loop sum mismatch');",
        "regular for loop failed",
    );
}

#[test]
fn test_object_property_access() {
    let mut runtime = JSRuntime::new();
    let mut ctx = runtime.new_context();

    assert!(eval(&mut ctx, "var obj = {a: 1}; obj.a").is_ok());
    assert_js_ok(
        &mut ctx,
        "var obj = {a: 1}; if (obj.a !== 1) throw new Error('obj.a mismatch'); var obj2 = {a: 1, b: 2}; if (obj2.a + obj2.b !== 3) throw new Error('obj sum mismatch');",
        "object property access failed",
    );
}

#[test]
fn test_object_nested() {
    let mut runtime = JSRuntime::new();
    let mut ctx = runtime.new_context();

    assert!(eval(&mut ctx, "var obj = {a: {b: 1}}; obj.a.b").is_ok());
}

#[test]
fn test_object_keys() {
    let mut runtime = JSRuntime::new();
    let mut ctx = runtime.new_context();

    let r = eval(&mut ctx, "var obj = {a: 1, b: 2}; Object.keys(obj).length");
    assert!(r.is_ok(), "Object.keys failed");

    let r2 = eval(&mut ctx, "Object.keys({}).length");
    assert!(r2.is_ok());
}

#[test]
fn test_object_values() {
    let mut runtime = JSRuntime::new();
    let mut ctx = runtime.new_context();

    let r = eval(
        &mut ctx,
        "var obj = {a: 1, b: 2}; Object.values(obj).length",
    );
    assert!(r.is_ok(), "Object.values failed");
}

#[test]
fn test_object_entries() {
    let mut runtime = JSRuntime::new();
    let mut ctx = runtime.new_context();

    let r = eval(&mut ctx, "var obj = {a: 1}; Object.entries(obj).length");
    assert!(r.is_ok());
}

#[test]
fn test_object_assign() {
    let mut runtime = JSRuntime::new();
    let mut ctx = runtime.new_context();

    assert_js_ok(
        &mut ctx,
        "var target = {a: 1}; var src = {b: 2}; var result = Object.assign(target, src); if (result.b !== 2) throw new Error('Object.assign mismatch');",
        "object assign failed",
    );
}

#[test]
fn test_object_create() {
    let mut runtime = JSRuntime::new();
    let mut ctx = runtime.new_context();

    assert!(
        eval(
            &mut ctx,
            "var proto = {greet: 'hello'}; var obj = Object.create(proto); obj.greet"
        )
        .is_ok()
    );

    assert!(eval(&mut ctx, "var obj = Object.create(null)").is_ok());
}

#[test]
fn test_object_get_prototype_of() {
    let mut runtime = JSRuntime::new();
    let mut ctx = runtime.new_context();

    assert!(
        eval(
            &mut ctx,
            "var proto = {}; var obj = Object.create(proto); Object.getPrototypeOf(obj)"
        )
        .is_ok()
    );

    assert!(eval(&mut ctx, "Object.getPrototypeOf(null)").is_ok());
}

#[test]
fn test_object_set_prototype_of() {
    let mut runtime = JSRuntime::new();
    let mut ctx = runtime.new_context();

    assert!(eval(&mut ctx, "var proto1 = {a: 1}").is_ok());
    assert!(eval(&mut ctx, "var proto2 = {b: 2}").is_ok());
    assert!(eval(&mut ctx, "var obj = Object.create(proto1)").is_ok());
    let r = eval(&mut ctx, "Object.setPrototypeOf(obj, proto2)");
    assert!(r.is_ok());
    let r2 = eval(&mut ctx, "obj.b");
    assert!(r2.is_ok());
}

#[test]
fn test_object_prototype_has_own_property() {
    let mut runtime = JSRuntime::new();
    let mut ctx = runtime.new_context();

    assert!(eval(&mut ctx, "var obj = {a: 1}; obj.hasOwnProperty('a')").is_ok());
    assert_js_ok(
        &mut ctx,
        "var x1 = {a: 1}; if (x1.hasOwnProperty('a') !== true) throw new Error('hasOwnProperty a'); var x2 = {a: 1}; if (x2.hasOwnProperty('b') !== false) throw new Error('hasOwnProperty b'); var x3 = Object.create({a: 1}); if (x3.hasOwnProperty('a') !== false) throw new Error('proto own a'); var x4 = Object.create({a: 1}); if (x4.hasOwnProperty('b') !== false) throw new Error('proto own b');",
        "object prototype hasOwnProperty failed",
    );
}

#[test]
fn test_object_prototype_value_of() {
    let mut runtime = JSRuntime::new();
    let mut ctx = runtime.new_context();

    assert!(eval(&mut ctx, "var obj = {a: 1}; obj.valueOf()").is_ok());
}

#[test]
fn test_object_prototype_to_string() {
    let mut runtime = JSRuntime::new();
    let mut ctx = runtime.new_context();

    assert!(eval(&mut ctx, "var obj = {a: 1}; obj.toString()").is_ok());
}

#[test]
fn test_object_prototype_is_prototype_of() {
    let mut runtime = JSRuntime::new();
    let mut ctx = runtime.new_context();

    let proto = eval(&mut ctx, "var proto = {}");
    assert!(proto.is_ok());
    let obj = eval(&mut ctx, "var obj = Object.create(proto)");
    assert!(obj.is_ok());
    assert_js_ok(
        &mut ctx,
        "if (proto.isPrototypeOf(obj) !== true) throw new Error('isPrototypeOf true mismatch'); if (obj.isPrototypeOf(proto) !== false) throw new Error('isPrototypeOf false mismatch');",
        "object prototype isPrototypeOf failed",
    );
}

#[test]
fn test_object_prototype_chain() {
    let mut runtime = JSRuntime::new();
    let mut ctx = runtime.new_context();

    assert!(eval(&mut ctx, "var obj = {}; Object.getPrototypeOf(obj)").is_ok());
    assert!(
        eval(
            &mut ctx,
            "var proto = {}; var obj = Object.create(proto); Object.getPrototypeOf(obj) === proto"
        )
        .is_ok()
    );
}

#[test]
fn test_function_bind() {
    let mut runtime = JSRuntime::new();
    let mut ctx = runtime.new_context();

    assert!(eval(&mut ctx, "function add(a, b) { return a + b; }").is_ok());
    assert!(eval(&mut ctx, "var add5 = add.bind(null, 5)").is_ok());
    assert!(eval(&mut ctx, "add5(3)").is_ok());
    assert_js_ok(
        &mut ctx,
        "if (add5(3) !== 8) throw new Error('bind mismatch');",
        "function bind failed",
    );
}

#[test]
fn test_function_call() {
    let mut runtime = JSRuntime::new();
    let mut ctx = runtime.new_context();

    assert!(
        eval(
            &mut ctx,
            "function greet(greeting) { return greeting + ' ' + this.name; }"
        )
        .is_ok()
    );
    assert!(eval(&mut ctx, "var obj = {name: 'Alice'}").is_ok());
    assert!(eval(&mut ctx, "greet.call(obj, 'Hello')").is_ok());
}

#[test]
fn test_function_apply() {
    let mut runtime = JSRuntime::new();
    let mut ctx = runtime.new_context();

    assert!(eval(&mut ctx, "function sum(a, b, c) { return a + b + c; }").is_ok());
    assert!(eval(&mut ctx, "sum.apply(null, [1, 2, 3])").is_ok());
    assert_js_ok(
        &mut ctx,
        "if (sum.apply(null, [1, 2, 3]) !== 6) throw new Error('apply mismatch');",
        "function apply failed",
    );
}

#[test]
fn test_for_in_loop() {
    let mut runtime = JSRuntime::new();
    let mut ctx = runtime.new_context();

    let r = eval(
        &mut ctx,
        "var keys = []; var obj = {a: 1, b: 2}; for (var k in obj) { keys.push(k); } keys.length",
    );
    assert!(r.is_ok());
    assert!(eval(
        &mut ctx,
        "var sum = 0; var obj = {a: 1, b: 2, c: 3}; for (var k in obj) { sum = sum + obj[k]; } sum"
    )
    .is_ok());
    assert_js_ok(
        &mut ctx,
        "var sum2 = 0; var obj2 = {a: 1, b: 2, c: 3}; for (var k2 in obj2) { sum2 = sum2 + obj2[k2]; } if (sum2 !== 6) throw new Error('for-in sum mismatch');",
        "for in loop failed",
    );
}

#[test]
fn test_for_in_debug() {
    let mut runtime = JSRuntime::new();
    let mut ctx = runtime.new_context();

    let r_outside = eval(&mut ctx, "var sum = 0; { sum = 5; } sum");
    println!("outside block sum = {:?}", r_outside.map(|v| v.get_int()));

    let r_block = eval(&mut ctx, "var sum = 0; { sum = 5; }");
    println!("inside block sum = {:?}", r_block.map(|v| v.get_int()));

    let r1 = eval(
        &mut ctx,
        "var sum = 0; for (var i = 0; i < 3; i++) { sum = 1; }",
    );
    println!("for with simple body = {:?}", r1.map(|v| v.get_int()));
}

#[test]
fn test_switch_statement() {
    let mut runtime = JSRuntime::new();
    let mut ctx = runtime.new_context();

    assert_js_ok(
        &mut ctx,
        "var x = 2; switch(x) { case 1: x = 10; break; case 2: x = 20; break; case 3: x = 30; break; } if (x !== 20) throw new Error('switch case2 mismatch'); var y = 1; switch(y) { case 1: y = 10; break; default: y = 99; } if (y !== 10) throw new Error('switch case1 mismatch'); var z = 5; switch(z) { case 1: z = 10; break; default: z = 99; } if (z !== 99) throw new Error('switch default mismatch');",
        "switch statement failed",
    );
    assert!(eval(&mut ctx, "var x = 1; switch(x) { case 1: x = 10; } x").is_ok());
}

#[test]
fn test_switch_simple() {
    let mut runtime = JSRuntime::new();
    let mut ctx = runtime.new_context();
    let r = eval(
        &mut ctx,
        "var x = 2; switch(x) { case 1: x = 10; break; case 2: x = 20; break; } x",
    );
    match r {
        Ok(v) => println!("SIMPLE switch(2) => raw: {:?} int: {}", v, v.get_int()),
        Err(e) => println!("SIMPLE switch(2) error: {}", e),
    }
}

#[test]
fn test_switch_case1() {
    let mut runtime = JSRuntime::new();
    let mut ctx = runtime.new_context();
    let r = eval(
        &mut ctx,
        "var x = 1; switch(x) { case 1: x = 10; break; case 2: x = 20; break; } x",
    );
    match r {
        Ok(v) => println!("switch(1) => raw: {:?} int: {}", v, v.get_int()),
        Err(e) => println!("switch(1) error: {}", e),
    }
}

#[test]
fn test_switch_just_x() {
    let mut runtime = JSRuntime::new();
    let mut ctx = runtime.new_context();
    let r = eval(&mut ctx, "var x = 2; x");
    match r {
        Ok(v) => println!("just x => get_int: {}", v.get_int()),
        Err(e) => println!("error: {}", e),
    }
}

#[test]
fn test_switch_just_20() {
    let mut runtime = JSRuntime::new();
    let mut ctx = runtime.new_context();
    let r = eval(&mut ctx, "20");
    match r {
        Ok(v) => println!("just 20 => get_int: {}", v.get_int()),
        Err(e) => println!("error: {}", e),
    }
}

#[test]
fn test_switch_one_case() {
    let mut runtime = JSRuntime::new();
    let mut ctx = runtime.new_context();
    let r = eval(&mut ctx, "var x = 1; switch(x) { case 1: x = 100; } x");
    match r {
        Ok(v) => println!("one case x=1 => raw: {:?} int: {}", v, v.get_int()),
        Err(e) => println!("error: {}", e),
    }
}

#[test]
fn test_switch_zero() {
    let mut runtime = JSRuntime::new();
    let mut ctx = runtime.new_context();
    let r = eval(&mut ctx, "var x = 0; switch(x) { case 0: x = 5; } x");
    match r {
        Ok(v) => println!("zero case x=0 => raw: {:?} int: {}", v, v.get_int()),
        Err(e) => println!("error: {}", e),
    }
}

#[test]
fn test_switch_fallthrough() {
    let mut runtime = JSRuntime::new();
    let mut ctx = runtime.new_context();

    assert_js_ok(
        &mut ctx,
        "var x = 1; switch(x) { case 1: case 2: x = 100; break; } if (x !== 100) throw new Error('switch fallthrough mismatch');",
        "switch fallthrough failed",
    );
}

#[test]
fn test_switch_debug() {
    let mut runtime = JSRuntime::new();
    let mut ctx = runtime.new_context();
    let result = eval(
        &mut ctx,
        "var x = 2; switch(x) { case 1: x = 10; break; case 2: x = 20; break; } x",
    );
    match result {
        Ok(v) => println!("OK: {:?}", v),
        Err(e) => println!("ERR: {}", e),
    }
}

#[test]
fn test_switch_debug2() {
    let mut runtime = JSRuntime::new();
    let mut ctx = runtime.new_context();
    let result = eval(
        &mut ctx,
        "var x = 2; switch(x) { case 1: x = 10; break; case 2: x = 20; break; case 3: x = 30; break; } x",
    );
    match result {
        Ok(v) => {
            println!("OK: {:?}", v);
            println!("int: {}", v.get_int());
        }
        Err(e) => println!("ERR: {}", e),
    }
}

#[test]
fn test_switch_debug3() {
    let mut runtime = JSRuntime::new();
    let mut ctx = runtime.new_context();
    let result = eval(
        &mut ctx,
        "var x = 2; switch(x) { case 1: x = 10; break; case 2: x = 20; break; case 3: x = 30; break; } x",
    );
    match result {
        Ok(v) => {
            println!("OK: {:?}", v);
            println!("int: {}", v.get_int());
        }
        Err(e) => println!("ERR: {}", e),
    }
}