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_constructor("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 define_method(&constructor, "revocable", 2, revocable, interp);
22 let global = interp.realm.global.clone();
23 global.define(
24 "Proxy",
25 Property::with_attributes(Value::Object(constructor), true, false),
26 );
27}
28
29/// The target and handler arguments, checked.
30///
31/// Both must be objects, and both fail the same way — the reference has one
32/// `JS_ThrowTypeError(ctx, "not an object")` for the whole `create_proxy` path, so the
33/// message does not say *which* argument was wrong. Checking only `null` let a primitive
34/// handler through, which then made every trap read fail later with a message about a
35/// missing method rather than about the constructor's argument.
36fn target_and_handler(interp: &mut Interp<'_>, args: &[Value]) -> JsResult<(Gc<JsObject>, Value)> {
37 let target = arg(args, 0);
38 let Value::Object(target) = target else {
39 return Err(interp.type_error("not an object"));
40 };
41 let handler = arg(args, 1);
42 if !matches!(handler, Value::Object(_)) {
43 return Err(interp.type_error("not an object"));
44 }
45 Ok((target, handler))
46}
47
48/// `new Proxy(target, handler)`.
49fn constructor(interp: &mut Interp<'_>, _this: Value, args: &[Value]) -> JsResult<Value> {
50 let (target, handler) = target_and_handler(interp, args)?;
51 Ok(Value::Object(interp.new_proxy(target, handler)))
52}
53
54/// `Proxy.revocable(target, handler)` → `{ proxy, revoke }`.
55fn revocable(interp: &mut Interp<'_>, _this: Value, args: &[Value]) -> JsResult<Value> {
56 let (target, handler) = target_and_handler(interp, args)?;
57 let proxy = interp.new_proxy(target, handler);
58 // The revoke function holds the *same* slots the proxy reads, so clearing them here is
59 // what makes the proxy inert — and drops the collector's edge to the target and the
60 // handler, which is the point of revoking at all.
61 let slots = proxy.proxy_state().expect("a proxy was just built");
62 let revoke = interp.new_revoker(slots);
63 let result = interp.new_object(None);
64 result.define_data("proxy", Value::Object(proxy));
65 result.define_data("revoke", Value::Object(revoke));
66 Ok(Value::Object(result))
67}