diff --git a/lang/object.c b/lang/object.c index 2237a9b..4889406 100644 --- a/lang/object.c +++ b/lang/object.c @@ -60,9 +60,7 @@ *objptr = object_none(); } -void dispatch_call(VmState state, Object obj, const char *name) { - if (obj == object_none()) - vm_abort_msg(state, "dispatch_call: cannot dispatch on none"); +void dispatch_default(VmState state, Object obj, const char *name) { struct object_call *call = obj->class_data->calls; while (call->name != NULL) { if (strcmp(call->name, name) == 0) { @@ -71,5 +69,14 @@ } ++call; } - vm_abort_msg(state, "dispatch_call: no call found to dispatch"); + vm_abort_msg(state, "dispatch_default: no call found to dispatch"); +} + +void dispatch_call(VmState state, Object obj, const char *name) { + if (obj == object_none()) + vm_abort_msg(state, "dispatch_call: cannot dispatch on none"); + object_caller caller = (obj->class_data->caller); + if (!caller) + caller = dispatch_default; + caller(state, obj, name); } diff --git a/lang/object.h b/lang/object.h index 1939426..2031806 100644 --- a/lang/object.h +++ b/lang/object.h @@ -13,11 +13,15 @@ int priv; }; +// Function for dispatching an object call +typedef void (*object_caller)(VmState, Object, const char *); + // Object class shared between objects struct object_class { void (*cleanup)(VmState state, Object obj); struct object_call *calls; // Points to array terminated by call with NULL name + object_caller caller; // Optional custom dispatcher }; // Creates an object with a class and allocated private data