]> Nishi Git Mirror - gwion.git/commitdiff
:bug: Fix global func
authorfennecdjay <astor.jeremie@wanadoo.fr>
Thu, 21 Feb 2019 10:38:31 +0000 (11:38 +0100)
committerfennecdjay <astor.jeremie@wanadoo.fr>
Thu, 21 Feb 2019 10:38:31 +0000 (11:38 +0100)
src/emit/emit.c
src/oo/env.c
src/oo/nspc.c
src/oo/value.c
src/parse/check.c
src/parse/scan2.c
src/vm/vm.c

index 08a75f5a0ef4f24efead0060723df076c200155e..d0bb4d58f59ed71d39a5d8b9977c3cc0960270fd 100644 (file)
@@ -515,9 +515,8 @@ ANN static m_bool emit_exp_decl_global(const Emitter emit, const Var_Decl var_de
     CHECK_BB(emit_instantiate_object(emit, type, array, is_ref))
   f_instr *exec = (f_instr*)dotstatic;
   const Instr instr = emit_kind(emit, v->type->size, emit_addr, exec);
-v->d.ptr = _mp_alloc(v->type->size);
-// here union flag is more like 'treat as builtin'
-SET_FLAG(v, builtin | ae_flag_union);
+  v->d.ptr = _mp_alloc(v->type->size);
+  SET_FLAG(v, union);
   instr->m_val = (m_uint)v->d.ptr;
   instr->m_val2 = v->type->size;
   if(is_obj && (is_array || !is_ref)) {
@@ -525,6 +524,8 @@ SET_FLAG(v, builtin | ae_flag_union);
     assign->m_val = (m_uint)emit_var;
     if(is_array && !emit->env->scope)
       ADD_REF(type)
+    const Instr instr = emit_add_instr(emit, RegAddRef);
+    instr->m_val = emit_var;
   }
   return GW_OK;
 }
@@ -1573,8 +1574,7 @@ ANN /*static */m_bool emit_func_def(const Emitter emit, const Func_Def func_def)
   emit_func_def_code(emit, func);
   emit->env->func = former;
   emit_pop_code(emit);
-//  if(!emit->env->class_def && !GET_FLAG(func_def, template))
-  if(!emit->env->class_def)
+  if(!emit->env->class_def && !GET_FLAG(func_def, global))
     emit_func_def_global(emit, func->value_ref);
   MEMOIZE_INI
   return GW_OK;
index ffa8de24b4dcaa27de8d0c1a70d6ba82ba8106b6..88a0da91db0ba6e9faaeabe807be1e2a23f3462b 100644 (file)
@@ -23,8 +23,6 @@ Env new_env() {
   vector_init(&env->nspc_stack);
   vector_init(&env->known_ctx);
   env->type_xid = 0;
-//  scope_init(&env->swi);
-//  vector_pop((Vector)&env->swi);
   vector_init((Vector)&env->swi);
   map_init(&env->swi.map);
 
@@ -48,10 +46,10 @@ ANN void env_reset(const Env env) {
 
 ANN void free_env(const Env a) {
   const m_uint size = vector_size(&a->known_ctx);
-  for(m_uint i = 0; i < size; i++)
-    REM_REF((Context)vector_at(&a->known_ctx, i));
-  REM_REF(a->global_nspc);
+  for(m_uint i = size + 1; --i;)
+    REM_REF((Context)vector_at(&a->known_ctx, i - 1));
   vector_release(&a->known_ctx);
+  REM_REF(a->global_nspc);
   vector_release(&a->nspc_stack);
   vector_release(&a->class_stack);
   vector_release(&a->breaks);
index 3ff54f1b19e4c77d2fb4c4923da5345a48ed8801..7d15ef3739bed87741c5cac41114ee261296c8db 100644 (file)
@@ -58,9 +58,9 @@ describe_nspc_free(Func, func)
 describe_nspc_free(Type, type)
 
 ANN static void free_nspc(Nspc a) {
-  free_nspc_value(a);
   nspc_free_func(a);
   nspc_free_type(a);
+  free_nspc_value(a);
 
   if(a->class_data)
     free(a->class_data);
index ae39b9e69d05db65381ad73c1d42226a8c930611..879b4d1975b86ca7f4064ae22e102af4566ca31f 100644 (file)
@@ -8,12 +8,10 @@
 
 ANN static void free_value(Value a) {
   if(!GET_FLAG(a, func) && a->d.ptr &&
-!(GET_FLAG(a, enum) && GET_FLAG(a, builtin) && a->owner_class)
-&& isa(a->type, t_object) < 0)
-    _mp_free(a->type->size, a->d.ptr);
-  if(isa(a->type, t_class) > 0 || isa(a->type, t_function) > 0)
-    REM_REF(a->type)
-  if(GET_FLAG(a->type, op))
+      !(GET_FLAG(a, enum) && GET_FLAG(a, builtin) && a->owner_class)
+      && isa(a->type, t_object) < 0)
+   _mp_free(a->type->size, a->d.ptr);
+  if(isa(a->type, t_class) > 0 || isa(a->type, t_function) > 0 || GET_FLAG(a->type, op))
     REM_REF(a->type)
   mp_free(Value, a);
 }
index b6ef1b57be0c6bb03f612606d8447f06d4e4ae1b..818db81b886111d0ed8937b31217583c34111074 100644 (file)
@@ -103,6 +103,8 @@ ANN Type check_exp_decl(const Env env, const Exp_Decl* decl) { GWDEBUG_EXE
       decl_member(env->curr, v);
     else if(GET_FLAG(decl->td, static))
       decl_static(env->curr, v);
+    else if(global || (env->func && GET_FLAG(env->func->def, global)))
+      SET_FLAG(v, abstract);
     if(isa(decl->type, t_fptr) > 0)
       CHECK_BO(check_fptr_decl(env, var))
   SET_FLAG(v, checked | ae_flag_used);
@@ -148,6 +150,10 @@ ANN static Value check_non_res_value(const Env env, const Exp_Primary* primary)
       ERR_O(primary->self->pos,
             "non-static member '%s' used from static function.", s_name(primary->d.var))
     return v;
+  } else if(env->func && GET_FLAG(env->func->def, global)) {
+    if(!SAFE_FLAG(value, abstract) && !SAFE_FLAG(value, arg))
+      ERR_O(primary->self->pos,
+            "non-global variable '%s' used from global function.", s_name(primary->d.var))
   }
   return value;
 }
@@ -1087,6 +1093,8 @@ ANN m_bool check_func_def(const Env env, const Func_Def f) { GWDEBUG_EXE
   CHECK_BB(check_func_def_override(env, f))
   if(env->class_def)
     CHECK_BB(check_parent_match(env, f))
+  else if(GET_FLAG(f, global))
+    env_push_global(env);
   const Func former = env->func;
   env->func = func;
   ++env->scope;
@@ -1105,6 +1113,8 @@ ANN m_bool check_func_def(const Env env, const Func_Def f) { GWDEBUG_EXE
   nspc_pop_value(env->curr);
   --env->scope;
   env->func = former;
+  if(GET_FLAG(f, global))
+    env_push_global(env);
   return ret;
 }
 
index 7ab64c34fd8ab2639132811db57d309ec15055b8..0246c64c3430066b5ef4d7a76dd11dbb8014ae36 100644 (file)
@@ -385,7 +385,6 @@ ANN2(1, 2) static m_bool scan2_func_def_template(const Env env, const Func_Def f
   --i;
   const Symbol sym = func_symbol(env->curr->name, func_name, "template", i);
   nspc_add_value(env->curr, sym, value);
-   nspc_add_value(env->curr, sym, value);
   if(!overload) {
     func->vt_index = i;
     ADD_REF(value)
@@ -484,6 +483,9 @@ ANN2(1,2,4) static Value func_create(const Env env, const Func_Def f,
 ANN m_bool scan2_func_def(const Env env, const Func_Def f) { GWDEBUG_EXE
   Value value    = NULL;
   f->stack_depth = 0;
+  m_uint scope = env->scope;
+  if(GET_FLAG(f, global))
+    scope = env_push_global(env);
   const Value overload = nspc_lookup_value0(env->curr, f->name);
   m_str func_name = s_name(f->name);
   if(overload)
@@ -506,12 +508,7 @@ ANN m_bool scan2_func_def(const Env env, const Func_Def f) { GWDEBUG_EXE
   }
   const Func base = get_func(env, f);
   if(!base) {
-    m_uint scope = env->scope;
-    if(GET_FLAG(f, global))
-      scope = env_push_global(env);
     CHECK_OB((value = func_create(env, f, overload, func_name)))
-    if(GET_FLAG(f, global))
-      env_pop(env, scope);
   } else {
     f->func = base;
 }
@@ -524,6 +521,8 @@ ANN m_bool scan2_func_def(const Env env, const Func_Def f) { GWDEBUG_EXE
       CHECK_BB(scan2_func_def_op(env, f))
     SET_FLAG(value, checked);
   }
+  if(GET_FLAG(f, global))
+    env_pop(env, scope);
   return GW_OK;
 }
 
index 93686cd5d971a17f13bde80e6bc08f2df783b317..5aa5f42546ec32109489e90576fe056a14965fc8 100644 (file)
@@ -563,8 +563,6 @@ funcusr:
   *(m_uint*)  mem = pc; mem += SZ_INT;
   pc = 0;
   code = *(VM_Code*)reg;
-//puts(code->name);
-//  assert(code);
   ip = code->instr->ptr + OFFSET;
   m_uint stack_depth = code->stack_depth;
   if(stack_depth) {
@@ -581,7 +579,6 @@ funcusr:
   if(overflow_(mem, shred))
     Except(shred, "StackOverflow");
 }
-//puts(code->name);
 DISPATCH();
 funcmember:
 {