Files
vectorcraft/crates/js/src/builtins/async_function.rs
T
dotCooCoo c0b4413a5a JS: fix async function review points
- AsyncFunction.prototype has Symbol.toStringTag "AsyncFunction",
  configurable only.
- await checks the room for the awaited promise's reaction or job and
  the suspended frame's bytes before it attaches the reaction, so a
  suspension stays within the heap limit.
- An async function's return pops its frame before resolving its
  promise, so a then getter on the returned value runs without it.
  Returning the function's own promise rejects it at the return.
- Block functions whose variable was not declared are kept in a hash
  set, so each block function's lookup takes constant time.
- A shorthand await in an object pattern of an async arrow function's
  head is a syntax error, and the head accepts patterns with defaults
  such as async ({a = 1}) => a. async({a = 1}) without an arrow is
  still a syntax error.
- docs: .jsx files and source strings run with module: true have
  Promise and the async syntax; await is an identifier only in script
  code.
2026-12-02 08:30:09 -08:00

41 lines
2.1 KiB
Rust

//! `AsyncFunction` and its prototype (ECMA-262 2017 section 25.5,
//! <https://262.ecma-international.org/8.0/#sec-async-function-objects>), in realms with
//! [`crate::Options::es2015`]: the prototype of every async function, and the constructor a script
//! reaches through it. `AsyncFunction` is not a global.
use super::{create_dynamic_function, data, native};
use crate::heap::{CONFIGURABLE, Class, NativeKind, Object, Prop};
use crate::interp::{Interp, JsResult};
use crate::value::Value;
/// Creates `AsyncFunction` and `AsyncFunction.prototype`, keeping the prototype for the async
/// functions the realm makes.
pub(crate) fn install(interp: &mut Interp) -> JsResult<()> {
let function_proto = interp.intrinsics.function_proto;
let proto = interp.heap.alloc_object(Object::new(Class::Ordinary, Some(function_proto)));
interp.promises.async_function_proto = Some(proto);
let ctor = native(interp, "AsyncFunction", async_function_ctor, 1, NativeKind::Plain, true)?;
interp.promises.async_function_ctor = Some(ctor);
// The built-in `Function` is the constructor's prototype (section 25.5.2), even after a
// script has replaced `Function.prototype.constructor`.
let function_ctor = interp.intrinsics.function_ctor;
if let Some(o) = interp.heap.object_mut(ctor) {
o.proto = Some(function_ctor);
}
data(interp, ctor, "prototype", Value::Object(proto), 0)?;
// Configurable and not writable (section 25.5.3.1), unlike the `constructor` of the ordinary
// built-in prototypes.
data(interp, proto, "constructor", Value::Object(ctor), CONFIGURABLE)?;
// `AsyncFunction.prototype[@@toStringTag]` is "AsyncFunction", configurable only (section
// 25.5.3.2).
if let Some(tag) = interp.well_known(|w| &w.to_string_tag) {
interp.define_sym(proto, tag, Prop::Data { value: Value::str("AsyncFunction"), flags: CONFIGURABLE })?;
}
Ok(())
}
/// `AsyncFunction(p1, …, body)` and `new AsyncFunction(…)` (section 25.5.1.1).
fn async_function_ctor(interp: &mut Interp, _: Value, args: &[Value], _: bool) -> JsResult<Value> {
create_dynamic_function(interp, args, false, true)
}