Skip to content

Commit

Permalink
Fix bug with event return values
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimirdabic committed Nov 24, 2023
1 parent d7d950c commit 8c310f2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
18 changes: 11 additions & 7 deletions Blaze Core/Interpreter/Types/EventValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ namespace VD.Blaze.Interpreter.Types
public class EventValue : IValue, IValueCallable, IEvent
{
public List<IValueCallable> Callbacks;
public VM VM;
public VM ParentVM;

public EventValue(VM vm)
{
Callbacks = new List<IValueCallable>();
VM = vm;
ParentVM = vm;
}

public bool AsBoolean()
Expand All @@ -36,12 +36,12 @@ public void Call(VM vm, List<IValue> args)
{
for(int i = Callbacks.Count - 1; i >= 0; i--)
{
Callbacks[i].Call(VM, args);
Callbacks[i].Call(vm, args);

// Only leave the returned value from the callback
vm._inConstructor = i != Callbacks.Count - 1;
}

// If the VM is not running, run it
if (VM.Done)
VM.Execute();
}

public IValue Copy()
Expand All @@ -61,7 +61,11 @@ public string GetName()

public void Raise(List<IValue> args)
{
Call(VM, args);
Call(ParentVM, args);

// If the VM is not running, run it
if (ParentVM.Done)
ParentVM.Execute();
}
}
}
9 changes: 7 additions & 2 deletions Blaze Core/Interpreter/VM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public VM()
public ModuleEnv LoadModule(Module.Module module, ModuleEnv parent = null)
{
ModuleEnv env = new ModuleEnv(module, parent);
FunctionValue staticFunc = new FunctionValue(env.Module.Functions[0], env, env);
FunctionValue staticFunc = new FunctionValue(env.Module.GetStaticFunction(), env, env);

staticFunc.Call(this, null);

Expand All @@ -65,7 +65,7 @@ public static ModuleEnv StaticLoadModule(Module.Module module, ModuleEnv parent
VM vm = new VM();

ModuleEnv env = new ModuleEnv(module, parent);
FunctionValue staticFunc = new FunctionValue(env.Module.Functions[0], env, env);
FunctionValue staticFunc = new FunctionValue(env.Module.GetStaticFunction(), env, env);

vm.RunFunction(staticFunc, null);

Expand Down Expand Up @@ -122,6 +122,11 @@ public void Step()

_current++;

/* Console.WriteLine($"{opcode} {opargi} STACK: ");
foreach(var v in Stack.ToArray())
Console.WriteLine(v.AsString());
Console.Write("\n");*/

switch (opcode)
{
case Opcode.NOP:
Expand Down
Binary file modified Blaze.docx
Binary file not shown.

0 comments on commit 8c310f2

Please sign in to comment.