Skip to content

Commit

Permalink
class: Class instantiation callback
Browse files Browse the repository at this point in the history
  • Loading branch information
obiwac committed Oct 29, 2024
1 parent 6111bda commit b3d10af
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 6 deletions.
7 changes: 7 additions & 0 deletions flamingo/call.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,13 @@ static int call(
(*rv)->inst.data = NULL;
(*rv)->inst.free_data = NULL;

// XXX A small quirk to note regarding this: an instance won't be created if it isn't assigned to anything.
// That means that the class instantiation callback will never be called either, even if the constructor code itself is.

if (flamingo->class_inst_cb != NULL && flamingo->class_inst_cb(flamingo, *rv, flamingo->class_inst_cb_data, args) < 0) {
return -1;
}

goto done;
}

Expand Down
7 changes: 6 additions & 1 deletion flamingo/flamingo.c
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,16 @@ void flamingo_register_external_fn_cb(flamingo_t* flamingo, flamingo_external_fn
flamingo->external_fn_cb_data = data;
}

void flamingo_register_class_decl_cb(flamingo_t* flamingo, flamingo_class_declaration_cb_t cb, void* data) {
void flamingo_register_class_decl_cb(flamingo_t* flamingo, flamingo_class_decl_cb_t cb, void* data) {
flamingo->class_decl_cb = cb;
flamingo->class_decl_cb_data = data;
}

void flamingo_register_class_inst_cb(flamingo_t* flamingo, flamingo_class_inst_cb_t cb, void* data) {
flamingo->class_inst_cb = cb;
flamingo->class_inst_cb_data = data;
}

void flamingo_add_import_path(flamingo_t* flamingo, char* path) {
char* const duped = strdup(path);
assert(duped != NULL);
Expand Down
40 changes: 35 additions & 5 deletions flamingo/flamingo.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,33 @@ typedef struct flamingo_scope_t flamingo_scope_t;
typedef struct flamingo_env_t flamingo_env_t;
typedef struct flamingo_arg_list_t flamingo_arg_list_t;

typedef int (*flamingo_external_fn_cb_t)(flamingo_t* flamingo, flamingo_val_t* callable, void* data, flamingo_arg_list_t* args, flamingo_val_t** rv);
typedef int (*flamingo_class_declaration_cb_t)(flamingo_t* flamingo, flamingo_val_t* class, void* data);
typedef int (*flamingo_ptm_cb_t)(flamingo_t* flamingo, flamingo_val_t* self, flamingo_arg_list_t* args, flamingo_val_t** rv);
typedef int (*flamingo_external_fn_cb_t)(
flamingo_t* flamingo,
flamingo_val_t* callable,
void* data,
flamingo_arg_list_t* args,
flamingo_val_t** rv
);

typedef int (*flamingo_class_decl_cb_t)(
flamingo_t* flamingo,
flamingo_val_t* class,
void* data
);

typedef int (*flamingo_class_inst_cb_t)(
flamingo_t* flamingo,
flamingo_val_t* instance,
void* data,
flamingo_arg_list_t* args
);

typedef int (*flamingo_ptm_cb_t)(
flamingo_t* flamingo,
flamingo_val_t* self,
flamingo_arg_list_t* args,
flamingo_val_t** rv
);

typedef enum {
FLAMINGO_VAL_KIND_NONE,
Expand Down Expand Up @@ -160,9 +184,12 @@ struct flamingo_t {
flamingo_external_fn_cb_t external_fn_cb;
void* external_fn_cb_data;

flamingo_class_declaration_cb_t class_decl_cb;
flamingo_class_decl_cb_t class_decl_cb;
void* class_decl_cb_data;

flamingo_class_inst_cb_t class_inst_cb;
void* class_inst_cb_data;

// Runtime stuff.

bool inherited_env;
Expand Down Expand Up @@ -204,8 +231,11 @@ int flamingo_create(flamingo_t* flamingo, char const* progname, char* src, size_
void flamingo_destroy(flamingo_t* flamingo);

char* flamingo_err(flamingo_t* flamingo);

void flamingo_register_external_fn_cb(flamingo_t* flamingo, flamingo_external_fn_cb_t cb, void* data);
void flamingo_register_class_decl_cb(flamingo_t* flamingo, flamingo_class_declaration_cb_t cb, void* data);
void flamingo_register_class_decl_cb(flamingo_t* flamingo, flamingo_class_decl_cb_t cb, void* data);
void flamingo_register_class_inst_cb(flamingo_t* flamingo, flamingo_class_inst_cb_t cb, void* data);

void flamingo_add_import_path(flamingo_t* flamingo, char* path);
int flamingo_inherit_env(flamingo_t* flamingo, flamingo_env_t* env);
int flamingo_run(flamingo_t* flamingo);
Expand Down
1 change: 1 addition & 0 deletions flamingo/grammar/import.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ static int import(flamingo_t* flamingo, char* path) {

flamingo_register_external_fn_cb(imported_flamingo, flamingo->external_fn_cb, flamingo->external_fn_cb_data);
flamingo_register_class_decl_cb(imported_flamingo, flamingo->class_decl_cb, flamingo->class_decl_cb_data);
flamingo_register_class_inst_cb(imported_flamingo, flamingo->class_inst_cb, flamingo->class_inst_cb_data);

// Set the scope stack for the imported flamingo instance to be the same as ours.

Expand Down

0 comments on commit b3d10af

Please sign in to comment.