Publish native arity and validate callback contracts before registration
Verify Tessera GUI / verify (push) Failing after 7m44s
Verify Tessera GUI / verify (push) Failing after 7m44s
This commit is contained in:
+38
-2
@@ -11,6 +11,24 @@ fn invalid(message: &'static str) -> VmError {
|
||||
VmError::InvalidArgument(message)
|
||||
}
|
||||
|
||||
pub fn validate_handler(
|
||||
store: &endor_core::ObjectStore,
|
||||
function: ObjectRef,
|
||||
) -> Result<(), VmError> {
|
||||
if !store.object(function)?.payload().is_function() {
|
||||
return Err(invalid("event handler must be a Function"));
|
||||
}
|
||||
let arity = store
|
||||
.call_arity(function)?
|
||||
.ok_or(invalid("event handler requires positional metadata"))?;
|
||||
if !arity.accepts(0) && !arity.accepts(1) {
|
||||
return Err(invalid(
|
||||
"event handler must accept zero arguments or one Event",
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct Event {
|
||||
dispatch: Option<WeakObjectRef>,
|
||||
@@ -84,6 +102,10 @@ enum Method {
|
||||
Connected,
|
||||
}
|
||||
impl NativeCallable for Method {
|
||||
fn arity(&self) -> Option<endor_core::CallArity> {
|
||||
Some(endor_core::CallArity::exact(1))
|
||||
}
|
||||
|
||||
fn invoke(&self, cx: &mut NativeContext<'_>, args: &[Value]) -> Result<NativeResult, VmError> {
|
||||
let [Value::Object(owner)] = args else {
|
||||
return Err(invalid("event method receiver"));
|
||||
@@ -159,6 +181,10 @@ impl NativeCallable for Method {
|
||||
#[derive(Debug)]
|
||||
pub struct Constructor;
|
||||
impl NativeCallable for Constructor {
|
||||
fn arity(&self) -> Option<endor_core::CallArity> {
|
||||
Some(endor_core::CallArity::range(2, usize::MAX))
|
||||
}
|
||||
|
||||
fn invoke(&self, cx: &mut NativeContext<'_>, args: &[Value]) -> Result<NativeResult, VmError> {
|
||||
let [Value::Object(definition), Value::Text(kind), values @ ..] = args else {
|
||||
return Err(invalid("Event requires an event kind"));
|
||||
@@ -171,6 +197,8 @@ impl NativeCallable for Constructor {
|
||||
if fields.len() != values.len() {
|
||||
return Err(invalid("incorrect Event field arguments"));
|
||||
}
|
||||
cx.store()
|
||||
.check_class_field_arguments(*definition, values)?;
|
||||
let event = new_event(cx, *definition, kind)?;
|
||||
for (field, value) in fields.iter().zip(values) {
|
||||
cx.set_attr(event, &field.name, value.clone())?;
|
||||
@@ -240,6 +268,10 @@ struct Delivery {
|
||||
handler: Handler,
|
||||
}
|
||||
impl NativeCallable for Delivery {
|
||||
fn arity(&self) -> Option<endor_core::CallArity> {
|
||||
Some(endor_core::CallArity::exact(0))
|
||||
}
|
||||
|
||||
fn invoke(&self, cx: &mut NativeContext<'_>, _: &[Value]) -> Result<NativeResult, VmError> {
|
||||
let mut event = cx.store().payload::<Event>(self.event, "Event")?.clone();
|
||||
if event.immediate || event.stopped_at.is_some_and(|group| group != self.group) {
|
||||
@@ -280,8 +312,8 @@ impl NativeCallable for Delivery {
|
||||
if matches!(self.handler, Handler::ObjectMethod)
|
||||
|| cx
|
||||
.store()
|
||||
.positional_parameter_count(function)?
|
||||
.is_some_and(|count| count > 0)
|
||||
.call_arity(function)?
|
||||
.is_some_and(|arity| arity.accepts(1))
|
||||
{
|
||||
arguments.push(Value::Object(self.event));
|
||||
}
|
||||
@@ -294,6 +326,10 @@ impl NativeCallable for Delivery {
|
||||
#[derive(Debug)]
|
||||
struct Finish(ObjectRef);
|
||||
impl NativeCallable for Finish {
|
||||
fn arity(&self) -> Option<endor_core::CallArity> {
|
||||
Some(endor_core::CallArity::exact(0))
|
||||
}
|
||||
|
||||
fn invoke(&self, cx: &mut NativeContext<'_>, _: &[Value]) -> Result<NativeResult, VmError> {
|
||||
let mut event = cx.store().payload::<Event>(self.0, "Event")?.clone();
|
||||
event.dispatch = None;
|
||||
|
||||
Reference in New Issue
Block a user