diff options
Diffstat (limited to 'pkg/lang/vm/exec.go')
| -rw-r--r-- | pkg/lang/vm/exec.go | 53 |
1 files changed, 38 insertions, 15 deletions
diff --git a/pkg/lang/vm/exec.go b/pkg/lang/vm/exec.go index eeb8710..ade6cc0 100644 --- a/pkg/lang/vm/exec.go +++ b/pkg/lang/vm/exec.go @@ -44,7 +44,7 @@ func (vm *VM) execPushArray() error { func (vm *VM) execPushFunction(pc int) { // TODO: Make push ops into functions, where the argCount can be passed. - vm.stack.Push(value.NewFunction(code.NewPos(vm.module(), pc), 0)) + vm.stack.Push(value.NewFunction(code.NewPos(vm.moduleID(), pc), 0)) } func (vm *VM) execPushObject() error { @@ -71,35 +71,56 @@ func (vm *VM) execAddGlobal(name string) error { return err } - globalPtr, err := vm.memory.Allocate(mem.CellKindGlobal) + module := vm.module() + qualifiedName, err := module.GetGlobal(name) if err != nil { return err } - globalCell := value.GlobalCell(v) - if err := vm.memory.Set(globalPtr, globalCell); err != nil { - return err + // Fill the core ptrs if we got a core global. + if module.IsCore() { + if v.Type() == value.TypeRefType { + ptr := v.Data().(value.TypeRefData).TypeRef() + + switch qualifiedName { + case ":core:Type": + vm.corePtrs.SetTypeRef(ptr) + case ":core:Int": + vm.corePtrs.SetInt(ptr) + case ":core:Float": + vm.corePtrs.SetFloat(ptr) + case ":core:Bool": + vm.corePtrs.SetBool(ptr) + case ":core:Null": + vm.corePtrs.SetNull(ptr) + case ":core:Array": + vm.corePtrs.SetArray(ptr) + case ":core:Function": + vm.corePtrs.SetFunction(ptr) + case ":core:String": + vm.corePtrs.SetString(ptr) + } + } } - vm.globals[name] = globalPtr + if err := vm.AddGlobal(qualifiedName, v); err != nil { + return err + } return nil } func (vm *VM) execGetGlobal(name string) error { - ptr, ok := vm.globals[name] - if !ok { - return ErrNoSuchGlobal{GlobalName: name} - } - - cell, err := vm.getMemCell(ptr, mem.CellKindGlobal, false) + v, ok, err := vm.GetGlobal(name) if err != nil { return err } - v := cell.(value.GlobalCell).Get() - v = v.Clone(vm.memory) + if !ok { + return ErrNoSuchGlobal{GlobalName: name} + } + v = v.Clone(vm.memory) vm.stack.Push(v) return nil } @@ -234,7 +255,7 @@ func (vm *VM) execGetMember(name string) error { } } - typeCell, err := vm.getMemCell(parent.TypePtr(), mem.CellKindType, false) + typeCell, err := vm.getMemCell(parent.TypePtr(&vm.corePtrs), mem.CellKindType, false) if err != nil { return err } @@ -889,6 +910,8 @@ func (vm *VM) execCall(argCount uint) error { if !val.IsEmpty() { vm.stack.Push(val) + } else { + vm.stack.Push(value.NewNull()) } return nil |
