use crate::{
ecmascript::{
Agent, ArgumentsList, BUILTIN_STRING_MEMORY, Behaviour, Builtin,
BuiltinIntrinsicConstructor, ExceptionType, JsResult, Object, Realm, String, Value,
builders::BuiltinFunctionBuilder, proxy_create,
},
engine::{Bindable, GcScope},
heap::IntrinsicConstructorIndexes,
};
pub(crate) struct ProxyConstructor;
impl Builtin for ProxyConstructor {
const NAME: String<'static> = BUILTIN_STRING_MEMORY.Proxy;
const LENGTH: u8 = 2;
const BEHAVIOUR: Behaviour = Behaviour::Constructor(Self::constructor);
}
impl BuiltinIntrinsicConstructor for ProxyConstructor {
const INDEX: IntrinsicConstructorIndexes = IntrinsicConstructorIndexes::Proxy;
}
struct ProxyRevocable;
impl Builtin for ProxyRevocable {
const NAME: String<'static> = BUILTIN_STRING_MEMORY.revocable;
const LENGTH: u8 = 2;
const BEHAVIOUR: Behaviour = Behaviour::Regular(ProxyConstructor::revocable);
}
impl ProxyConstructor {
fn constructor<'gc>(
agent: &mut Agent,
_this_value: Value,
arguments: ArgumentsList,
new_target: Option<Object>,
gc: GcScope<'gc, '_>,
) -> JsResult<'gc, Value<'gc>> {
let gc = gc.into_nogc();
let target = arguments.get(0).bind(gc);
let handler = arguments.get(1).bind(gc);
if new_target.is_none() {
return Err(agent.throw_exception_with_static_message(
ExceptionType::TypeError,
"calling a builtin Proxy constructor without new is forbidden",
gc,
));
}
proxy_create(agent, target, handler, gc).map(|proxy| proxy.into())
}
fn revocable<'gc>(
agent: &mut Agent,
_this_value: Value,
_arguments: ArgumentsList,
gc: GcScope<'gc, '_>,
) -> JsResult<'gc, Value<'gc>> {
Err(agent.todo("Proxy.revocable", gc.into_nogc()))
}
pub(crate) fn create_intrinsic(agent: &mut Agent, realm: Realm<'static>) {
BuiltinFunctionBuilder::new_intrinsic_constructor::<ProxyConstructor>(agent, realm)
.with_property_capacity(1)
.with_builtin_function_property::<ProxyRevocable>()
.build();
}
}