javascript 0.1.13

A JavaScript engine implementation 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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
use crate::{
    core::{
        DestructuringElement, Expr, JSObjectDataPtr, PropertyKey, Statement, StatementKind, Value, evaluate_expr, prepare_function_call_env,
    },
    error::JSError,
};

use std::cell::RefCell;
use std::rc::Rc;

/// Handle generator function constructor (when called as `new GeneratorFunction(...)`)
pub fn _handle_generator_function_constructor(_args: &[Expr], _env: &JSObjectDataPtr) -> Result<Value, JSError> {
    // Generator functions cannot be constructed with `new`
    Err(raise_eval_error!("GeneratorFunction is not a constructor"))
}

/// Handle generator function calls (creating generator objects)
pub fn handle_generator_function_call(
    params: &[DestructuringElement],
    body: &[Statement],
    _args: &[Expr],
    env: &JSObjectDataPtr,
) -> Result<Value, JSError> {
    // Create a new generator object
    let generator = Rc::new(RefCell::new(crate::core::JSGenerator {
        params: params.to_vec(),
        body: body.to_vec(),
        env: env.clone(),
        state: crate::core::GeneratorState::NotStarted,
    }));

    // Create a wrapper object for the generator
    let gen_obj = Rc::new(RefCell::new(crate::core::JSObjectData::new()));
    // Store the actual generator data
    gen_obj.borrow_mut().insert(
        crate::core::PropertyKey::String("__generator__".to_string()),
        Rc::new(RefCell::new(Value::Generator(generator))),
    );

    Ok(Value::Object(gen_obj))
}

/// Handle generator instance method calls (like `gen.next()`, `gen.return()`, etc.)
pub fn handle_generator_instance_method(
    generator: &Rc<RefCell<crate::core::JSGenerator>>,
    method: &str,
    args: &[Expr],
    env: &JSObjectDataPtr,
) -> Result<Value, JSError> {
    match method {
        "next" => {
            // Get optional value to send to the generator
            let send_value = if args.is_empty() {
                Value::Undefined
            } else {
                evaluate_expr(env, &args[0])?
            };

            generator_next(generator, send_value)
        }
        "return" => {
            // Return a value and close the generator
            let return_value = if args.is_empty() {
                Value::Undefined
            } else {
                evaluate_expr(env, &args[0])?
            };

            generator_return(generator, return_value)
        }
        "throw" => {
            // Throw an exception into the generator
            let throw_value = if args.is_empty() {
                Value::Undefined
            } else {
                evaluate_expr(env, &args[0])?
            };

            generator_throw(generator, throw_value)
        }
        _ => Err(raise_eval_error!(format!("Generator.prototype.{} is not implemented", method))),
    }
}

// Helper to replace the first `yield` occurrence inside an Expr with a
// provided `send_value`. `replaced` becomes true once a replacement is made.
fn replace_first_yield_in_expr(expr: &Expr, send_value: &Value, replaced: &mut bool) -> Expr {
    use crate::core::Expr;
    match expr {
        Expr::Yield(_) => {
            if !*replaced {
                *replaced = true;
                Expr::Value(send_value.clone())
            } else {
                expr.clone()
            }
        }
        Expr::YieldStar(_) => {
            if !*replaced {
                *replaced = true;
                Expr::Value(send_value.clone())
            } else {
                expr.clone()
            }
        }
        Expr::Binary(a, op, b) => Expr::Binary(
            Box::new(replace_first_yield_in_expr(a, send_value, replaced)),
            op.clone(),
            Box::new(replace_first_yield_in_expr(b, send_value, replaced)),
        ),
        Expr::Assign(a, b) => Expr::Assign(
            Box::new(replace_first_yield_in_expr(a, send_value, replaced)),
            Box::new(replace_first_yield_in_expr(b, send_value, replaced)),
        ),
        Expr::Index(a, b) => Expr::Index(
            Box::new(replace_first_yield_in_expr(a, send_value, replaced)),
            Box::new(replace_first_yield_in_expr(b, send_value, replaced)),
        ),
        Expr::Property(a, s) => Expr::Property(Box::new(replace_first_yield_in_expr(a, send_value, replaced)), s.clone()),
        Expr::Call(a, args) => Expr::Call(
            Box::new(replace_first_yield_in_expr(a, send_value, replaced)),
            args.iter()
                .map(|arg| replace_first_yield_in_expr(arg, send_value, replaced))
                .collect(),
        ),
        Expr::Object(pairs) => Expr::Object(
            pairs
                .iter()
                .map(|(k, v, is_method)| {
                    (
                        replace_first_yield_in_expr(k, send_value, replaced),
                        replace_first_yield_in_expr(v, send_value, replaced),
                        *is_method,
                    )
                })
                .collect(),
        ),
        Expr::Array(items) => Expr::Array(
            items
                .iter()
                .map(|it| it.as_ref().map(|e| replace_first_yield_in_expr(e, send_value, replaced)))
                .collect(),
        ),
        Expr::LogicalNot(a) => Expr::LogicalNot(Box::new(replace_first_yield_in_expr(a, send_value, replaced))),
        Expr::TypeOf(a) => Expr::TypeOf(Box::new(replace_first_yield_in_expr(a, send_value, replaced))),
        Expr::Delete(a) => Expr::Delete(Box::new(replace_first_yield_in_expr(a, send_value, replaced))),
        Expr::Void(a) => Expr::Void(Box::new(replace_first_yield_in_expr(a, send_value, replaced))),
        Expr::Increment(a) => Expr::Increment(Box::new(replace_first_yield_in_expr(a, send_value, replaced))),
        Expr::Decrement(a) => Expr::Decrement(Box::new(replace_first_yield_in_expr(a, send_value, replaced))),
        Expr::PostIncrement(a) => Expr::PostIncrement(Box::new(replace_first_yield_in_expr(a, send_value, replaced))),
        Expr::PostDecrement(a) => Expr::PostDecrement(Box::new(replace_first_yield_in_expr(a, send_value, replaced))),
        Expr::LogicalAnd(a, b) => Expr::LogicalAnd(
            Box::new(replace_first_yield_in_expr(a, send_value, replaced)),
            Box::new(replace_first_yield_in_expr(b, send_value, replaced)),
        ),
        Expr::LogicalOr(a, b) => Expr::LogicalOr(
            Box::new(replace_first_yield_in_expr(a, send_value, replaced)),
            Box::new(replace_first_yield_in_expr(b, send_value, replaced)),
        ),
        Expr::Comma(a, b) => Expr::Comma(
            Box::new(replace_first_yield_in_expr(a, send_value, replaced)),
            Box::new(replace_first_yield_in_expr(b, send_value, replaced)),
        ),
        Expr::Spread(a) => Expr::Spread(Box::new(replace_first_yield_in_expr(a, send_value, replaced))),
        Expr::OptionalCall(a, args) => Expr::OptionalCall(
            Box::new(replace_first_yield_in_expr(a, send_value, replaced)),
            args.iter()
                .map(|arg| replace_first_yield_in_expr(arg, send_value, replaced))
                .collect(),
        ),
        Expr::OptionalIndex(a, b) => Expr::OptionalIndex(
            Box::new(replace_first_yield_in_expr(a, send_value, replaced)),
            Box::new(replace_first_yield_in_expr(b, send_value, replaced)),
        ),
        Expr::Conditional(a, b, c) => Expr::Conditional(
            Box::new(replace_first_yield_in_expr(a, send_value, replaced)),
            Box::new(replace_first_yield_in_expr(b, send_value, replaced)),
            Box::new(replace_first_yield_in_expr(c, send_value, replaced)),
        ),
        _ => expr.clone(),
    }
}

fn replace_first_yield_in_statement(stmt: &mut Statement, send_value: &Value, replaced: &mut bool) {
    match &mut stmt.kind {
        StatementKind::Expr(e) => {
            *e = replace_first_yield_in_expr(e, send_value, replaced);
        }
        StatementKind::Let(decls) | StatementKind::Var(decls) => {
            for (_, expr_opt) in decls.iter_mut() {
                if let Some(expr) = expr_opt {
                    *expr = replace_first_yield_in_expr(expr, send_value, replaced);
                }
            }
        }
        StatementKind::Const(decls) => {
            for (_, expr) in decls.iter_mut() {
                *expr = replace_first_yield_in_expr(expr, send_value, replaced);
            }
        }
        StatementKind::Return(Some(expr)) => {
            *expr = replace_first_yield_in_expr(expr, send_value, replaced);
        }
        StatementKind::If(cond, then_body, else_body_opt) => {
            *cond = replace_first_yield_in_expr(cond, send_value, replaced);
            for s in then_body.iter_mut() {
                replace_first_yield_in_statement(s, send_value, replaced);
                if *replaced {
                    return;
                }
            }
            if let Some(else_body) = else_body_opt {
                for s in else_body.iter_mut() {
                    replace_first_yield_in_statement(s, send_value, replaced);
                    if *replaced {
                        return;
                    }
                }
            }
        }
        StatementKind::For(_, cond_opt, _, body) => {
            if let Some(cond) = cond_opt {
                *cond = replace_first_yield_in_expr(cond, send_value, replaced);
            }
            for s in body.iter_mut() {
                replace_first_yield_in_statement(s, send_value, replaced);
                if *replaced {
                    return;
                }
            }
        }
        StatementKind::While(cond, body) => {
            *cond = replace_first_yield_in_expr(cond, send_value, replaced);
            for s in body.iter_mut() {
                replace_first_yield_in_statement(s, send_value, replaced);
                if *replaced {
                    return;
                }
            }
        }
        StatementKind::DoWhile(body, cond) => {
            for s in body.iter_mut() {
                replace_first_yield_in_statement(s, send_value, replaced);
                if *replaced {
                    return;
                }
            }
            *cond = replace_first_yield_in_expr(cond, send_value, replaced);
        }
        StatementKind::ForOf(_, _, body)
        | StatementKind::ForIn(_, _, body)
        | StatementKind::ForOfDestructuringObject(_, _, body)
        | StatementKind::ForOfDestructuringArray(_, _, body) => {
            for s in body.iter_mut() {
                replace_first_yield_in_statement(s, send_value, replaced);
                if *replaced {
                    return;
                }
            }
        }
        StatementKind::Block(stmts) => {
            for s in stmts.iter_mut() {
                replace_first_yield_in_statement(s, send_value, replaced);
                if *replaced {
                    return;
                }
            }
        }
        _ => {}
    }
}

fn expr_contains_yield(e: &Expr) -> bool {
    match e {
        Expr::Yield(_) | Expr::YieldStar(_) => true,
        Expr::Binary(a, _, b) => expr_contains_yield(a) || expr_contains_yield(b),
        Expr::Assign(a, b) => expr_contains_yield(a) || expr_contains_yield(b),
        Expr::Index(a, b) => expr_contains_yield(a) || expr_contains_yield(b),
        Expr::Property(a, _) => expr_contains_yield(a),
        Expr::Call(a, args) => expr_contains_yield(a) || args.iter().any(expr_contains_yield),
        Expr::Object(pairs) => pairs.iter().any(|(k, v, _)| expr_contains_yield(k) || expr_contains_yield(v)),
        Expr::Array(items) => items.iter().any(|it| it.as_ref().is_some_and(expr_contains_yield)),
        Expr::UnaryNeg(a)
        | Expr::LogicalNot(a)
        | Expr::TypeOf(a)
        | Expr::Delete(a)
        | Expr::Void(a)
        | Expr::PostIncrement(a)
        | Expr::PostDecrement(a)
        | Expr::Increment(a)
        | Expr::Decrement(a) => expr_contains_yield(a),
        Expr::LogicalAnd(a, b) | Expr::LogicalOr(a, b) | Expr::Comma(a, b) | Expr::Conditional(a, b, _) => {
            expr_contains_yield(a) || expr_contains_yield(b)
        }
        Expr::OptionalCall(a, args) => expr_contains_yield(a) || args.iter().any(expr_contains_yield),
        Expr::OptionalIndex(a, b) => expr_contains_yield(a) || expr_contains_yield(b),
        _ => false,
    }
}

// Replace the first nested statement containing a yield with a Throw statement
// holding `throw_value`. Returns true if a replacement was performed.
fn replace_first_yield_statement_with_throw(stmt: &mut Statement, throw_value: &Value) -> bool {
    match &mut stmt.kind {
        StatementKind::Expr(e) => {
            if expr_contains_yield(e) {
                stmt.kind = StatementKind::Throw(Expr::Value(throw_value.clone()));
                return true;
            }
            false
        }
        StatementKind::Let(decls) | StatementKind::Var(decls) => {
            for (_, expr_opt) in decls {
                if let Some(expr) = expr_opt
                    && expr_contains_yield(expr)
                {
                    stmt.kind = StatementKind::Throw(Expr::Value(throw_value.clone()));
                    return true;
                }
            }
            false
        }
        StatementKind::Const(decls) => {
            for (_, expr) in decls {
                if expr_contains_yield(expr) {
                    stmt.kind = StatementKind::Throw(Expr::Value(throw_value.clone()));
                    return true;
                }
            }
            false
        }
        StatementKind::If(_, then_body, else_body_opt) => {
            for s in then_body.iter_mut() {
                if replace_first_yield_statement_with_throw(s, throw_value) {
                    return true;
                }
            }
            if let Some(else_body) = else_body_opt {
                for s in else_body.iter_mut() {
                    if replace_first_yield_statement_with_throw(s, throw_value) {
                        return true;
                    }
                }
            }
            false
        }
        StatementKind::Block(stmts) => {
            for s in stmts.iter_mut() {
                if replace_first_yield_statement_with_throw(s, throw_value) {
                    return true;
                }
            }
            false
        }
        StatementKind::For(_, _, _, body)
        | StatementKind::ForOf(_, _, body)
        | StatementKind::ForIn(_, _, body)
        | StatementKind::ForOfDestructuringObject(_, _, body)
        | StatementKind::ForOfDestructuringArray(_, _, body)
        | StatementKind::While(_, body) => {
            for s in body.iter_mut() {
                if replace_first_yield_statement_with_throw(s, throw_value) {
                    return true;
                }
            }
            false
        }
        StatementKind::DoWhile(body, _) => {
            for s in body.iter_mut() {
                if replace_first_yield_statement_with_throw(s, throw_value) {
                    return true;
                }
            }
            false
        }
        StatementKind::TryCatch(try_body, _, catch_body, finally_body_opt) => {
            for s in try_body.iter_mut() {
                if replace_first_yield_statement_with_throw(s, throw_value) {
                    return true;
                }
            }
            for s in catch_body.iter_mut() {
                if replace_first_yield_statement_with_throw(s, throw_value) {
                    return true;
                }
            }
            if let Some(finally) = finally_body_opt {
                for s in finally.iter_mut() {
                    if replace_first_yield_statement_with_throw(s, throw_value) {
                        return true;
                    }
                }
            }
            false
        }
        _ => false,
    }
}

// Helper to find a yield expression within statements. Returns the
// index of the containing top-level statement and the inner yield
// expression if found.
fn find_first_yield_in_statements(stmts: &[Statement]) -> Option<(usize, Option<Box<Expr>>)> {
    for (i, s) in stmts.iter().enumerate() {
        match &s.kind {
            StatementKind::Expr(e) => match e {
                Expr::Yield(inner) => return Some((i, inner.clone())),
                Expr::YieldStar(inner) => return Some((i, Some(inner.clone()))),
                _ => {}
            },
            StatementKind::Block(inner_stmts) => {
                if let Some((_inner_idx, found)) = find_first_yield_in_statements(inner_stmts) {
                    return Some((i, found));
                }
            }
            StatementKind::If(_, then_body, else_body_opt) => {
                if let Some((_inner_idx, found)) = find_first_yield_in_statements(then_body) {
                    return Some((i, found));
                }
                if let Some(else_body) = else_body_opt
                    && let Some((_inner_idx, found)) = find_first_yield_in_statements(else_body)
                {
                    return Some((i, found));
                }
            }
            StatementKind::For(_, _, _, body) | StatementKind::While(_, body) | StatementKind::DoWhile(body, _) => {
                if let Some((_inner_idx, found)) = find_first_yield_in_statements(body) {
                    return Some((i, found));
                }
            }
            StatementKind::ForOf(_, _, body)
            | StatementKind::ForIn(_, _, body)
            | StatementKind::ForOfDestructuringObject(_, _, body)
            | StatementKind::ForOfDestructuringArray(_, _, body) => {
                if let Some((_inner_idx, found)) = find_first_yield_in_statements(body) {
                    return Some((i, found));
                }
            }
            StatementKind::FunctionDeclaration(_, _, _, _) => {
                // don't search nested function declarations
            }
            _ => {}
        }
    }
    None
}

/// Execute generator.next()
fn generator_next(generator: &Rc<RefCell<crate::core::JSGenerator>>, _send_value: Value) -> Result<Value, JSError> {
    let mut gen_obj = generator.borrow_mut();

    match &mut gen_obj.state {
        crate::core::GeneratorState::NotStarted => {
            // Start executing the generator function. Attempt to find the first
            // `yield` expression in the function body and return its value.
            gen_obj.state = crate::core::GeneratorState::Suspended { pc: 0, stack: vec![] };

            if let Some((idx, yield_inner)) = find_first_yield_in_statements(&gen_obj.body) {
                // Suspend at the containing top-level statement index so
                // that resumed execution re-evaluates the statement with
                // the sent-in value substituted for the `yield`.
                gen_obj.state = crate::core::GeneratorState::Suspended { pc: idx, stack: vec![] };

                // If the yield has an inner expression, evaluate it in a fresh
                // function-like frame whose prototype is the captured env.
                if let Some(inner_expr_box) = yield_inner {
                    let func_env = prepare_function_call_env(Some(&gen_obj.env), None, None, &[], None, None)?;
                    match crate::core::evaluate_expr(&func_env, &inner_expr_box) {
                        Ok(val) => return Ok(create_iterator_result(val, false)),
                        Err(_) => return Ok(create_iterator_result(Value::Undefined, false)),
                    }
                }

                // No inner expression -> yield undefined
                Ok(create_iterator_result(Value::Undefined, false))
            } else {
                // Fallback to previous placeholder behavior
                Ok(create_iterator_result(Value::Number(42.0), false))
            }
        }
        crate::core::GeneratorState::Suspended { pc, stack: _ } => {
            // On resume, execute from the suspended statement index. If a
            // `send_value` was provided to `next(value)`, substitute the
            // first `yield` in that statement with the sent value before
            // executing.
            let pc_val = *pc;
            if pc_val >= gen_obj.body.len() {
                gen_obj.state = crate::core::GeneratorState::Completed;
                return Ok(create_iterator_result(Value::Undefined, true));
            }
            // Clone the tail and replace first yield in the first statement
            let mut tail: Vec<Statement> = gen_obj.body[pc_val..].to_vec();
            let mut replaced = false;
            if let Some(first_stmt) = tail.get_mut(0) {
                replace_first_yield_in_statement(first_stmt, &_send_value, &mut replaced);
            }

            let func_env = prepare_function_call_env(Some(&gen_obj.env), None, None, &[], None, None)?;
            // Execute the (possibly modified) tail
            let result = crate::core::evaluate_statements(&func_env, &tail);
            gen_obj.state = crate::core::GeneratorState::Completed;
            match result {
                Ok(val) => Ok(create_iterator_result(val, true)),
                Err(_) => Ok(create_iterator_result(Value::Undefined, true)),
            }
        }
        crate::core::GeneratorState::Running { .. } => Err(raise_eval_error!("Generator is already running")),
        crate::core::GeneratorState::Completed => Ok(create_iterator_result(Value::Undefined, true)),
    }
}

/// Execute generator.return()
fn generator_return(generator: &Rc<RefCell<crate::core::JSGenerator>>, return_value: Value) -> Result<Value, JSError> {
    let mut gen_obj = generator.borrow_mut();
    gen_obj.state = crate::core::GeneratorState::Completed;
    Ok(create_iterator_result(return_value, true))
}

/// Execute generator.throw()
fn generator_throw(generator: &Rc<RefCell<crate::core::JSGenerator>>, throw_value: Value) -> Result<Value, JSError> {
    let mut gen_obj = generator.borrow_mut();
    match &mut gen_obj.state {
        crate::core::GeneratorState::NotStarted => {
            // Throwing into a not-started generator throws synchronously
            Err(raise_throw_error!(throw_value))
        }
        crate::core::GeneratorState::Suspended { pc, .. } => {
            // Replace the suspended statement with a Throw containing the thrown value
            let pc_val = *pc;
            if pc_val >= gen_obj.body.len() {
                gen_obj.state = crate::core::GeneratorState::Completed;
                return Err(raise_throw_error!(throw_value));
            }
            let mut tail: Vec<Statement> = gen_obj.body[pc_val..].to_vec();
            // Attempt to replace the first nested statement containing a `yield`
            // with a Throw so that surrounding try/catch blocks can catch it.
            let mut replaced = false;
            for s in tail.iter_mut() {
                if replace_first_yield_statement_with_throw(s, &throw_value) {
                    replaced = true;
                    break;
                }
            }
            if !replaced {
                // fallback: replace the top-level statement
                tail[0] = StatementKind::Throw(Expr::Value(throw_value.clone())).into();
            }

            let func_env = prepare_function_call_env(Some(&gen_obj.env), None, None, &[], None, None)?;

            // Execute the modified tail. If the throw is uncaught, evaluate_statements
            // will return Err and we should propagate that to the caller.
            let result = crate::core::evaluate_statements(&func_env, &tail);
            gen_obj.state = crate::core::GeneratorState::Completed;
            match result {
                Ok(val) => Ok(create_iterator_result(val, true)),
                Err(e) => Err(e),
            }
        }
        crate::core::GeneratorState::Running { .. } => Err(raise_eval_error!("Generator is already running")),
        crate::core::GeneratorState::Completed => Err(raise_eval_error!("Generator has already completed")),
    }
}

/// Create an iterator result object {value: value, done: done}
fn create_iterator_result(value: Value, done: bool) -> Value {
    let obj = Rc::new(RefCell::new(crate::core::JSObjectData::default()));

    // Set value property
    obj.borrow_mut()
        .properties
        .insert(PropertyKey::String("value".to_string()), Rc::new(RefCell::new(value)));

    // Set done property
    obj.borrow_mut()
        .properties
        .insert(PropertyKey::String("done".to_string()), Rc::new(RefCell::new(Value::Boolean(done))));

    Value::Object(obj)
}