From 616caa83b578158a4b6efba10d861dc8b587f010 Mon Sep 17 00:00:00 2001 From: Aymeric Wibo Date: Wed, 30 Oct 2024 11:27:41 +0100 Subject: [PATCH] scope: Set owner for instance or class' static scope --- flamingo/call.h | 2 ++ flamingo/flamingo.h | 4 ++++ flamingo/grammar/function_declaration.h | 2 ++ flamingo/scope.h | 3 +++ 4 files changed, 11 insertions(+) diff --git a/flamingo/call.h b/flamingo/call.h index 2fc10e1..606aaf1 100644 --- a/flamingo/call.h +++ b/flamingo/call.h @@ -225,6 +225,8 @@ static int call( (*rv)->inst.data = NULL; (*rv)->inst.free_data = NULL; + inner_scope->owner = *rv; + // 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. diff --git a/flamingo/flamingo.h b/flamingo/flamingo.h index 031fe35..dcb6d05 100644 --- a/flamingo/flamingo.h +++ b/flamingo/flamingo.h @@ -154,6 +154,10 @@ struct flamingo_scope_t { size_t vars_size; flamingo_var_t* vars; + // If scope is an instance's scope or a class' static scope, this will be set to that instance or class. + + flamingo_val_t* owner; + // Used for return to know what it can and can't return. bool class_scope; diff --git a/flamingo/grammar/function_declaration.h b/flamingo/grammar/function_declaration.h index 2d012ef..874173a 100644 --- a/flamingo/grammar/function_declaration.h +++ b/flamingo/grammar/function_declaration.h @@ -133,7 +133,9 @@ static int parse_function_declaration(flamingo_t* flamingo, TSNode node, flaming if (kind == FLAMINGO_FN_KIND_CLASS) { flamingo_scope_t* const scope = scope_alloc(); + var->val->fn.scope = scope; + scope->owner = var->val; if (find_static_members_in_class(flamingo, scope, body) < 0) { return -1; diff --git a/flamingo/scope.h b/flamingo/scope.h index a378720..3625b73 100644 --- a/flamingo/scope.h +++ b/flamingo/scope.h @@ -17,6 +17,9 @@ static flamingo_scope_t* scope_alloc(void) { scope->vars_size = 0; scope->vars = NULL; + scope->owner = NULL; + scope->class_scope = false; + return scope; }