Skip to content

Commit

Permalink
tests: Class instantiation callback tests
Browse files Browse the repository at this point in the history
  • Loading branch information
obiwac committed Oct 29, 2024
1 parent b3d10af commit d277342
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
23 changes: 23 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <assert.h>
#include <errno.h>
#include <inttypes.h>
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
Expand Down Expand Up @@ -101,6 +102,26 @@ static int class_decl_cb(flamingo_t* flamingo, flamingo_val_t* class, void* data
return 0;
}

static int class_inst_cb(flamingo_t* flamingo, flamingo_val_t* inst, void* data, flamingo_arg_list_t* args) {
flamingo_val_t* const class = inst->inst.class;

if (flamingo_cstrcmp(class->name, "ExternalClass", class->name_size) == 0) {
if (args->count != 1) {
return flamingo_raise_error(flamingo, "ExternalClass: expected 1 argument, got %zu", args->count);
}

if (args->args[0]->kind != FLAMINGO_VAL_KIND_INT) {
return flamingo_raise_error(flamingo, "ExternalClass: expected argument to be an integer");
}

if (args->args[0]->integer.integer != 420) {
return flamingo_raise_error(flamingo, "ExternalClass: expected argument to be 420, got %" PRId64, args->args[0]->integer.integer);
}
}

return 0;
}

int main(int argc, char* argv[]) {
init_name = *argv;

Expand Down Expand Up @@ -157,6 +178,8 @@ int main(int argc, char* argv[]) {

flamingo_register_external_fn_cb(&flamingo, external_fn_cb, NULL);
flamingo_register_class_decl_cb(&flamingo, class_decl_cb, NULL);
flamingo_register_class_inst_cb(&flamingo, class_inst_cb, NULL);

flamingo_add_import_path(&flamingo, "tests/import_path");

// run program
Expand Down
4 changes: 3 additions & 1 deletion tests/class_extern.fl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
class ExternalClass {
class ExternalClass(x: int) {
static let will_be_modified = 69
}

assert ExternalClass.will_be_modified == 420

let _ = ExternalClass(420) # XXX See comment above 'class_inst_cb' call in 'flamingo/call.h'.

0 comments on commit d277342

Please sign in to comment.