work on method wrapper and native function support
This commit is contained in:
@@ -9,6 +9,7 @@
|
||||
#include "../../memory.h"
|
||||
#include "type/type.h"
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
ArgonObject *BASE_CLASS = NULL;
|
||||
@@ -26,13 +27,32 @@ void add_field(ArgonObject *target, char *name, ArgonObject *object) {
|
||||
object, 0);
|
||||
}
|
||||
|
||||
ArgonObject *get_field_for_class(ArgonObject *target, char *name) {
|
||||
ArgonObject *bind_object_to_function(ArgonObject *object,
|
||||
ArgonObject *function) {
|
||||
ArgonObject *bound_method_wrapper = new_object();
|
||||
bound_method_wrapper->type = TYPE_METHOD;
|
||||
add_field(bound_method_wrapper, "__class__", ARGON_METHOD_TYPE);
|
||||
add_field(bound_method_wrapper, "__binding__", object);
|
||||
add_field(bound_method_wrapper, "__function__", function);
|
||||
ArgonObject *function_name = get_field(function, "__name__", false);
|
||||
if (function_name)
|
||||
add_field(bound_method_wrapper, "__name__", function_name);
|
||||
return bound_method_wrapper;
|
||||
}
|
||||
|
||||
ArgonObject *get_field_for_class(ArgonObject *target, char *name, ArgonObject *binding_object) {
|
||||
char *field = "__base__";
|
||||
while (target) {
|
||||
uint64_t hash = siphash64_bytes(name, strlen(name), siphash_key);
|
||||
ArgonObject *object = hashmap_lookup_GC(target->dict, hash);
|
||||
if (object)
|
||||
|
||||
if (object) {
|
||||
if (object->type == TYPE_FUNCTION ||
|
||||
object->type == TYPE_NATIVE_FUNCTION) {
|
||||
object = bind_object_to_function(binding_object, object);
|
||||
}
|
||||
return object;
|
||||
}
|
||||
hash = siphash64_bytes(field, strlen(field), siphash_key);
|
||||
target = hashmap_lookup_GC(target->dict, hash);
|
||||
}
|
||||
@@ -41,14 +61,8 @@ ArgonObject *get_field_for_class(ArgonObject *target, char *name) {
|
||||
|
||||
ArgonObject *get_field(ArgonObject *target, char *name, bool recursive) {
|
||||
char *field = "__class__";
|
||||
while (target) {
|
||||
uint64_t hash = siphash64_bytes(name, strlen(name), siphash_key);
|
||||
ArgonObject *object = hashmap_lookup_GC(target->dict, hash);
|
||||
if (!recursive || object)
|
||||
return object;
|
||||
hash = siphash64_bytes(field, strlen(field), siphash_key);
|
||||
target = hashmap_lookup_GC(target->dict, hash);
|
||||
field = "__base__";
|
||||
}
|
||||
return NULL;
|
||||
ArgonObject *object = hashmap_lookup_GC(target->dict, siphash64_bytes(name, strlen(name), siphash_key));
|
||||
if (!recursive || object)
|
||||
return object;
|
||||
return get_field_for_class(hashmap_lookup_GC(target->dict, siphash64_bytes(field, strlen(field), siphash_key)), name, target);
|
||||
}
|
||||
Reference in New Issue
Block a user