From 94894cbbce599ebb2943ab9fda6c26e20d01d73d Mon Sep 17 00:00:00 2001 From: Pierre Le Fevre Date: Sun, 22 Mar 2026 15:36:01 +0100 Subject: [PATCH] Implement JS Promise and microtask queue Add Promise constructor, prototype methods, and static methods to the JavaScript engine, with a microtask queue for asynchronous execution: - Promise constructor (via JS preamble so executor runs as bytecode) - Promise.prototype.then/catch/finally with chaining - Promise.resolve/reject static methods - Promise.all/allSettled/race/any static methods - Microtask queue: thread-local queue drained after each vm.execute() - VM.call_function: invoke GC-managed functions (native or bytecode) from outside the execution loop, used by microtask drain - SameValueZero key equality preserved, insertion order maintained Internal storage uses hidden properties (__promise_state__, __promise_result__, __promise_reactions__) on GC-heap objects. Promise.prototype stored as a GC root on the VM to survive collection. 29 new integration tests covering resolve/reject, constructor, executor synchronous execution, then chaining, catch/finally, error propagation, Promise.all/race/any/allSettled, identity passthrough, and double-resolve prevention. Removes Promise from test262 unsupported features list. Co-Authored-By: Claude Opus 4.6 (1M context) --- crates/js/src/builtins.rs | 876 ++++++++++++++++++++++++++++++++++++- crates/js/src/vm.rs | 609 +++++++++++++++++++++++++- crates/js/tests/test262.rs | 1 - 3 files changed, 1483 insertions(+), 3 deletions(-) diff --git a/crates/js/src/builtins.rs b/crates/js/src/builtins.rs index 268d700..4834f52 100644 --- a/crates/js/src/builtins.rs +++ b/crates/js/src/builtins.rs @@ -1,5 +1,5 @@ //! Built-in JavaScript objects and functions: Object, Array, Function, Error, -//! String, Number, Boolean, Symbol, Math, Date, JSON. +//! String, Number, Boolean, Symbol, Math, Date, JSON, Promise. //! //! Registers constructors, static methods, and prototype methods as globals //! in the VM. Callback-based array methods (map, filter, etc.) are defined @@ -7,6 +7,7 @@ use crate::gc::{Gc, GcRef}; use crate::vm::*; +use std::cell::RefCell; use std::collections::HashMap; use std::time::{SystemTime, UNIX_EPOCH}; @@ -246,6 +247,9 @@ pub fn init_builtins(vm: &mut Vm) { // Create and register Map, Set, WeakMap, WeakSet constructors. init_map_set_builtins(vm); + // Create and register Promise (prototype methods + native helpers). + init_promise_builtins(vm); + // Create and register JSON object (static methods only). init_json_object(vm); @@ -4659,6 +4663,834 @@ fn weakset_proto_delete(args: &[Value], ctx: &mut NativeContext) -> Result