From c567b59459aebe9204796501268bf15c99d814b8 Mon Sep 17 00:00:00 2001 From: William Bell <62452284+Ugric@users.noreply.github.com> Date: Wed, 6 Aug 2025 03:03:06 +0100 Subject: [PATCH] add __call__ support --- src/runtime/call/call.c | 19 +++++++++++++++++++ src/runtime/objects/object.c | 5 +++++ src/runtime/objects/object.h | 3 +++ 3 files changed, 27 insertions(+) diff --git a/src/runtime/call/call.c b/src/runtime/call/call.c index 5c471fa..871373d 100644 --- a/src/runtime/call/call.c +++ b/src/runtime/call/call.c @@ -12,6 +12,8 @@ #include #include #include +#include + #if defined(_WIN32) #include @@ -72,6 +74,23 @@ void run_call(Translated *translated, RuntimeState *state) { uint8_t from_register = pop_byte(translated, state); uint8_t source_location_index = pop_bytecode(translated, state); ArgonObject *object = state->registers[from_register]; + if (object->type != TYPE_FUNCTION) { + ArgonObject *to_call = get_field(object,"__class__"); + while (to_call) { + ArgonObject *call_object = get_field(to_call,"__call__"); + if (call_object) { + ArgonObject** new_call_args = malloc(sizeof(ArgonObject*)*(*state->call_args_length+1)); + memcpy(new_call_args+1, *state->call_args, *state->call_args_length); + free(*state->call_args); + new_call_args[0] = object; + *state->call_args = new_call_args; + (*state->call_args_length)++; + object = call_object; + break; + } + to_call = get_field(to_call,"__base__"); + }; + } if (object->type == TYPE_FUNCTION) { Stack *scope = create_scope(object->value.argon_fn.stack); for (size_t i = 0; i < *state->call_args_length; i++) { diff --git a/src/runtime/objects/object.c b/src/runtime/objects/object.c index 0521299..c93a35c 100644 --- a/src/runtime/objects/object.c +++ b/src/runtime/objects/object.c @@ -24,4 +24,9 @@ void add_field(ArgonObject *target, char *name, ArgonObject *object) { hashmap_insert_GC(target->dict, siphash64_bytes(name, strlen(name), siphash_key), name, object, 0); +} + +ArgonObject *get_field(ArgonObject *target, char *name) { + uint64_t hash = siphash64_bytes(name, strlen(name), siphash_key); + return hashmap_lookup_GC(target->dict, hash); } \ No newline at end of file diff --git a/src/runtime/objects/object.h b/src/runtime/objects/object.h index a53a314..614d568 100644 --- a/src/runtime/objects/object.h +++ b/src/runtime/objects/object.h @@ -16,4 +16,7 @@ typedef struct ArgonObject ArgonObject; ArgonObject *new_object(); void add_field(ArgonObject *target, char *name, ArgonObject *object); + +ArgonObject *get_field(ArgonObject *target, char *name); + #endif // OBJECT_H \ No newline at end of file