we (web engine): Experimental web browser project to understand the limits of Claude

Implement JS async functions and await #277

Open pierrelf.com
targeting main
Open opened by and assigned to pierrelf.com
targeting main

Implements issue 3mhn3pphkyd2f

Changes #

Bytecode #

  • New Await opcode (0x82) — saves async function state and returns to the driver
  • is_async flag on bytecode Function struct

Compiler #

  • Async function declarations, expressions, and methods set is_async on compiled function
  • await expr compiles to Await opcode
  • Async arrow functions set is_async on inner function
  • for await (let x of iter) parsed and compiled with @@asyncIterator

VM #

  • Async functions create an internal generator + result promise
  • drive_async_step() runs the generator and resolves/rejects the promise
  • setup_async_resume() wires up promise reactions for await resume
  • throw_into_generator() handles rejected await (throws into try/catch)
  • Exception handlers saved/restored across yield/await suspension points
  • Async generator wrapper with next(), return(), @@asyncIterator
  • __async_resume__ detection in both Call opcode and call_function

Tests #

16 unit tests covering:

  • Async function returns promise, resolves with return value
  • await on resolved/rejected promises
  • await on non-promise values
  • Multiple sequential awaits
  • try/catch around rejected await
  • Throw inside async function rejects promise
  • Async arrow functions (expression and concise body)
  • Await on chained promises
  • Async closures
  • Async object methods
  • Async generators
  • for await...of parsing

This pull request has no versions submitted yet.