From: Jérémie Astor Date: Sun, 15 Dec 2019 13:07:09 +0000 (+0100) Subject: :art: Add global_nspc layers X-Git-Tag: nightly~1982 X-Git-Url: http://10.10.0.4:5575/?a=commitdiff_plain;h=806709e462db5f180498fba57cb791e5e1d8ba5a;p=gwion.git :art: Add global_nspc layers --- diff --git a/include/env/env.h b/include/env/env.h index 21e78232..7b05267c 100644 --- a/include/env/env.h +++ b/include/env/env.h @@ -52,4 +52,5 @@ ANN m_bool type_engine_check_prog(const Env, const Ast); ANN m_bool traverse_func_template(const Env, const Func_Def); ANN2(1,3) void env_err(const Env, const loc_t pos, const m_str fmt, ...); ANN Value global_string(const Env env, const m_str str); +ANN void release_ctx(struct Env_Scope_ *a, struct Gwion_* gwion); #endif diff --git a/include/gwion.h b/include/gwion.h index 6ddba52e..e95cf71a 100644 --- a/include/gwion.h +++ b/include/gwion.h @@ -24,4 +24,6 @@ ANN void gwion_run(const Gwion gwion); ANN void gwion_end(const Gwion gwion); void free_code_instr(const Vector v, const Gwion gwion); ANN void gwion_end_child(const VM_Shred shred, const Gwion gwion); +ANN void push_global(const Gwion gwion, const m_str name); +ANN Nspc pop_global(const Gwion gwion); #endif diff --git a/src/env/env.c b/src/env/env.c index cbcefeb7..ca70b0d5 100644 --- a/src/env/env.c +++ b/src/env/env.c @@ -40,10 +40,14 @@ ANN void env_reset(const Env env) { env->scope->depth = 0; } -ANN static void free_env_scope(struct Env_Scope_ *a, Gwion gwion) { +ANN void release_ctx(struct Env_Scope_ *a, struct Gwion_ *gwion) { const m_uint size = vector_size(&a->known_ctx); for(m_uint i = size + 1; --i;) REM_REF((Context)vector_at(&a->known_ctx, i - 1), gwion); +} + +ANN static void free_env_scope(struct Env_Scope_ *a, Gwion gwion) { + release_ctx(a, gwion); vector_release(&a->known_ctx); vector_release(&a->nspc_stack); vector_release(&a->class_stack); @@ -54,8 +58,8 @@ ANN static void free_env_scope(struct Env_Scope_ *a, Gwion gwion) { ANN void free_env(const Env a) { free_env_scope(a->scope, a->gwion); - REM_REF(a->global_nspc, a->gwion); - free(a); + while(pop_global(a->gwion)); + xfree(a); } ANN2(1,3) m_uint env_push(const Env env, const Type type, const Nspc nspc) { diff --git a/src/gwion.c b/src/gwion.c index 84986050..cb88c413 100644 --- a/src/gwion.c +++ b/src/gwion.c @@ -170,3 +170,15 @@ ANN struct SpecialId_* specialid_get(const Gwion gwion, const Symbol sym) { } return NULL; } + +ANN void push_global(struct Gwion_ *gwion, const m_str name) { + const Nspc nspc = new_nspc(gwion->mp, name); + nspc->parent = gwion->env->global_nspc; + gwion->env->global_nspc = nspc; +} + +ANN Nspc pop_global(struct Gwion_ *gwion) { + const Nspc nspc = gwion->env->global_nspc->parent; + REM_REF(gwion->env->global_nspc, gwion) + return gwion->env->global_nspc = nspc; +} diff --git a/src/lib/engine.c b/src/lib/engine.c index 338aee73..6d6dcf85 100644 --- a/src/lib/engine.c +++ b/src/lib/engine.c @@ -17,7 +17,8 @@ #include "specialid.h" static GACK(gack_class) { - gw_out("class(%s)", actual_type(shred->info->vm->gwion, t)->name); + const Type type = actual_type(shred->info->vm->gwion, t) ?: t; + gw_out("class(%s)", type->name); } static GACK(gack_function) { @@ -151,11 +152,13 @@ ANN m_bool type_engine_init(VM* vm, const Vector plug_dirs) { OperCK oper = {}; struct Gwi_ gwi = { .gwion=vm->gwion, .loc=&loc, .oper=&oper }; CHECK_BB(import_core_libs(&gwi)) + push_global(vm->gwion, "[plugins]"); vm->gwion->env->name = "[imported]"; for(m_uint i = 0; i < vector_size(plug_dirs); ++i) { m_bool (*import)(Gwi) = (m_bool(*)(Gwi))vector_at(plug_dirs, i); if(import && import(&gwi) < 0) gwi_reset(&gwi); } + push_global(vm->gwion, "[user]"); return GW_OK; }