Skip to content

Commit

Permalink
Further unifying module's loading and setup routines related to globa…
Browse files Browse the repository at this point in the history
…l vars
  • Loading branch information
pachanga committed Nov 19, 2024
1 parent 2fbf4a0 commit 640f241
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 25 deletions.
13 changes: 3 additions & 10 deletions src/vm/module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,19 +117,12 @@ public void InitWithCompiled(CompiledModule compiled)
gvar_vals.Resize(compiled.total_gvars_num);
}

public void InitGlobalVars(VM vm)
{
//let's init all our own global variables
for(int g=0;g<local_gvars_num;++g)
gvar_vals[g] = Val.New(vm);
}

public void ClearGlobalVars()
internal void ClearGlobalVars()
{
for(int i=0;i<gvar_vals.Count;++i)
{
var val = gvar_vals[i];
val.Release();
val?.Release();
}
gvar_vals.Clear();
}
Expand Down Expand Up @@ -180,7 +173,7 @@ public void Setup(Func<string, Module> import2module, SetupFlags flags = SetupFl
);
}

public void InitRuntimeGlobalVars()
public void ImportGlobalVars()
{
int gvars_offset = local_gvars_num;

Expand Down
2 changes: 2 additions & 0 deletions src/vm/util/stack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class FixedStack<T>

public int Count
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return head; }
}

Expand All @@ -21,6 +22,7 @@ public FixedStack(int max_capacity)

public T this[int index]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get {
ValidateIndex(index);
return storage[index];
Expand Down
2 changes: 1 addition & 1 deletion src/vm/vm.exec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,7 @@ BHS ExecuteOnce(ExecState exec)
return BHS.SUCCESS;
}

void ExecuteInitCode(Module module)
void ExecInitCode(Module module)
{
var bytecode = module.compiled.initcode;
if(bytecode == null || bytecode.Length == 0)
Expand Down
31 changes: 17 additions & 14 deletions src/vm/vm.module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public enum LoadModuleSymbolError

public bool LoadModule(string module_name)
{
//Console.WriteLine("==START LOAD " + module_name);
if(loading_modules.Count > 0)
throw new Exception("Already loading modules");

Expand All @@ -46,9 +45,12 @@ public bool LoadModule(string module_name)
if(loading_modules.Count == 0)
return false;

//NOTE: registering modules in reverse order
//NOTE: initing modules in reverse order
for(int i=loading_modules.Count;i-- > 0;)
FinishRegistration(loading_modules[i].loaded);
Init_Phase2(loading_modules[i].loaded);
for(int i=loading_modules.Count;i-- > 0;)
Init_Phase3(loading_modules[i].loaded);

loading_modules.Clear();

return true;
Expand All @@ -57,8 +59,9 @@ public bool LoadModule(string module_name)
//NOTE: this method is public only for testing convenience
public void LoadModule(Module module)
{
BeginRegistration(module);
FinishRegistration(module);
Init_Phase1(module);
Init_Phase2(module);
Init_Phase3(module);
}

public Module FindModule(string module_name)
Expand Down Expand Up @@ -89,7 +92,6 @@ bool TryAddToLoadingList(string module_name)
lm.name = module_name;
loading_modules.Add(lm);

//NOTE: passing self as a type proxies 'resolver'
var loaded = loader.Load(module_name, this);

//if no such a module let's remove it from the loading list
Expand All @@ -99,33 +101,34 @@ bool TryAddToLoadingList(string module_name)
}
else
{
//let's add all imported modules as well
foreach(var imported in loaded.compiled.imports)
TryAddToLoadingList(imported);
lm.loaded = loaded;

BeginRegistration(loaded);
Init_Phase1(loaded);
}

return true;
}

void BeginRegistration(Module module)
void Init_Phase1(Module module)
{
//NOTE: for simplicity we add it to the modules at once,
// this is probably a bit 'smelly' but makes further
// symbols setup logic easier
registered_modules[module.name] = module;

module.InitGlobalVars(this);
}

void FinishRegistration(Module module)
void Init_Phase2(Module module)
{
module.Setup(name => FindModule(name));
ExecInitCode(module);
}

module.InitRuntimeGlobalVars();

ExecuteInitCode(module);
void Init_Phase3(Module module)
{
module.ImportGlobalVars();
ExecModuleInitFunc(module);
}

Expand Down

0 comments on commit 640f241

Please sign in to comment.