quickjs-pure 0.8.0

Embeddable JavaScript engine written in pure Rust (QuickJS-compatible)
Documentation
//! The `Proxy` constructor and `Proxy.revocable`.
//!
//! Almost nothing lives here: `Proxy` is a constructor whose only job is to build a
//! [`crate::object::ObjectKind::Proxy`], and every trap is dispatched from
//! `Interp`'s internal-method layer. The two things this module does own are the checks
//! that happen *at construction* — a target must be an object and a handler must not be
//! `null` — and `revocable`, which is the only way a script can take a proxy apart.

use crate::builtins::{arg, define_method};
use crate::interp::{Interp, JsResult};
use crate::object::{JsObject, Property};
use crate::value::Value;
use js_gc::Gc;

/// Install `Proxy` on the realm's global object.
pub fn install(interp: &mut Interp<'_>) {
    let constructor = interp.new_native_constructor("Proxy", 2, constructor);
    // `Proxy` is a function you call with `new`, but it has no `prototype` property:
    // proxies inherit from their *target's* prototype, so there is nothing for one to
    // sit on.
    define_method(&constructor, "revocable", 2, revocable, interp);
    let global = interp.realm.global.clone();
    global.define(
        "Proxy",
        Property::with_attributes(Value::Object(constructor), true, false),
    );
}

/// The target and handler arguments, checked.
///
/// Both must be objects, and both fail the same way — the reference has one
/// `JS_ThrowTypeError(ctx, "not an object")` for the whole `create_proxy` path, so the
/// message does not say *which* argument was wrong. Checking only `null` let a primitive
/// handler through, which then made every trap read fail later with a message about a
/// missing method rather than about the constructor's argument.
fn target_and_handler(interp: &mut Interp<'_>, args: &[Value]) -> JsResult<(Gc<JsObject>, Value)> {
    let target = arg(args, 0);
    let Value::Object(target) = target else {
        return Err(interp.type_error("not an object"));
    };
    let handler = arg(args, 1);
    if !matches!(handler, Value::Object(_)) {
        return Err(interp.type_error("not an object"));
    }
    Ok((target, handler))
}

/// `new Proxy(target, handler)`.
fn constructor(interp: &mut Interp<'_>, _this: Value, args: &[Value]) -> JsResult<Value> {
    let (target, handler) = target_and_handler(interp, args)?;
    Ok(Value::Object(interp.new_proxy(target, handler)))
}

/// `Proxy.revocable(target, handler)` → `{ proxy, revoke }`.
fn revocable(interp: &mut Interp<'_>, _this: Value, args: &[Value]) -> JsResult<Value> {
    let (target, handler) = target_and_handler(interp, args)?;
    let proxy = interp.new_proxy(target, handler);
    // The revoke function holds the *same* slots the proxy reads, so clearing them here is
    // what makes the proxy inert — and drops the collector's edge to the target and the
    // handler, which is the point of revoking at all.
    let slots = proxy.proxy_state().expect("a proxy was just built");
    let revoke = interp.new_revoker(slots);
    let result = interp.new_object(None);
    result.define_data("proxy", Value::Object(proxy));
    result.define_data("revoke", Value::Object(revoke));
    Ok(Value::Object(result))
}