Skip to main content

quickjs/builtins/
proxy.rs

1//! The `Proxy` constructor and `Proxy.revocable`.
2//!
3//! Almost nothing lives here: `Proxy` is a constructor whose only job is to build a
4//! [`crate::object::ObjectKind::Proxy`], and every trap is dispatched from
5//! `Interp`'s internal-method layer. The two things this module does own are the checks
6//! that happen *at construction* — a target must be an object and a handler must not be
7//! `null` — and `revocable`, which is the only way a script can take a proxy apart.
8
9use crate::builtins::{arg, define_method};
10use crate::interp::{Interp, JsResult};
11use crate::object::{JsObject, Property};
12use crate::value::Value;
13use js_gc::Gc;
14
15/// Install `Proxy` on the realm's global object.
16pub fn install(interp: &mut Interp<'_>) {
17    let constructor = interp.new_native("Proxy", 2, constructor);
18    // `Proxy` is a function you call with `new`, but it has no `prototype` property:
19    // proxies inherit from their *target's* prototype, so there is nothing for one to
20    // sit on.
21    constructor.define(
22        "_not_a_constructor",
23        Property::with_attributes(Value::Bool(false), false, false),
24    );
25    define_method(&constructor, "revocable", 2, revocable, interp);
26
27    let global = interp.realm.global.clone();
28    global.define(
29        "Proxy",
30        Property::with_attributes(Value::Object(constructor), true, false),
31    );
32}
33
34/// The target and handler arguments, checked.
35fn target_and_handler(interp: &mut Interp<'_>, args: &[Value]) -> JsResult<(Gc<JsObject>, Value)> {
36    let target = arg(args, 0);
37    let Value::Object(target) = target else {
38        return Err(interp.type_error("proxy target must be an object"));
39    };
40    let handler = arg(args, 1);
41    // A primitive handler would make every trap read fail later with a confusing message;
42    // the spec refuses it here, where the script can still tell what it did wrong.
43    if matches!(handler, Value::Null) {
44        return Err(interp.type_error("proxy handler must be an object"));
45    }
46    Ok((target, handler))
47}
48
49/// `new Proxy(target, handler)`.
50fn constructor(interp: &mut Interp<'_>, _this: Value, args: &[Value]) -> JsResult<Value> {
51    let (target, handler) = target_and_handler(interp, args)?;
52    Ok(Value::Object(interp.new_proxy(target, handler)))
53}
54
55/// `Proxy.revocable(target, handler)` → `{ proxy, revoke }`.
56fn revocable(interp: &mut Interp<'_>, _this: Value, args: &[Value]) -> JsResult<Value> {
57    let (target, handler) = target_and_handler(interp, args)?;
58    let proxy = interp.new_proxy(target, handler);
59    // The revoke function holds the *same* slots the proxy reads, so clearing them here is
60    // what makes the proxy inert — and drops the collector's edge to the target and the
61    // handler, which is the point of revoking at all.
62    let slots = proxy.proxy_state().expect("a proxy was just built");
63    let revoke = interp.new_revoker(slots);
64    let result = interp.new_object(None);
65    result.define_data("proxy", Value::Object(proxy));
66    result.define_data("revoke", Value::Object(revoke));
67    Ok(Value::Object(result))
68}