about summary refs log tree commit diff
path: root/pkg/lang/vm/exec.go
diff options
context:
space:
mode:
authorMel <einebeere@gmail.com>2022-05-30 02:02:53 +0000
committerGitHub <noreply@github.com>2022-05-30 02:02:53 +0000
commit78a29c41098db5e5f8291e0345a3cd443c52b329 (patch)
treebe8b3f1bba2491531e5be30165a6fc9b2a423fdc /pkg/lang/vm/exec.go
parentd2f69dccb3643834a79da79be4ece189a7178c9e (diff)
downloadjinx-78a29c41098db5e5f8291e0345a3cd443c52b329.tar.zst
jinx-78a29c41098db5e5f8291e0345a3cd443c52b329.zip
Specify arg count on VM Functions
Diffstat (limited to 'pkg/lang/vm/exec.go')
-rw-r--r--pkg/lang/vm/exec.go29
1 files changed, 24 insertions, 5 deletions
diff --git a/pkg/lang/vm/exec.go b/pkg/lang/vm/exec.go
index 79016fa..41b7019 100644
--- a/pkg/lang/vm/exec.go
+++ b/pkg/lang/vm/exec.go
@@ -43,7 +43,8 @@ func (vm *VM) execPushArray() error {
 }
 
 func (vm *VM) execPushFunction(pc int) {
-	vm.stack.Push(value.NewFunction(pc))
+	// TODO: Make push ops into functions, where the argCount can be passed.
+	vm.stack.Push(value.NewFunction(pc, 0))
 }
 
 func (vm *VM) execGetLocal(offset int) error {
@@ -126,7 +127,7 @@ func (vm *VM) execGetMember(name string) error {
 		member = member.WithEnv(envPtr)
 	}
 
-	val := value.NewFunction(0).WithData(member)
+	val := value.NewFunction(0, 0).WithData(member)
 	vm.stack.Push(val)
 
 	parent.Drop(vm.memory)
@@ -504,7 +505,7 @@ func (vm *VM) execLte() error {
 	return nil
 }
 
-func (vm *VM) execCall() error {
+func (vm *VM) execCall(argCount uint) error {
 	f, err := vm.stack.Pop()
 	if err != nil {
 		return err
@@ -519,13 +520,31 @@ func (vm *VM) execCall() error {
 
 	fn := f.Data().(value.FunctionData)
 
+	if argCount != fn.Args() {
+		return ErrNotEnoughArguments{
+			Got:   argCount,
+			Needed: fn.Args(),
+		}
+	}
+
+	if err = vm.stack.ShiftTopCallBase(int(argCount)); err != nil {
+		return err
+	}
+
 	if err = vm.stack.PushCall(fn.Pc(), vm.pc, fn.Env()); err != nil {
 		return err
 	}
 
 	if fn.Native() != nil {
-		// TODO: Arguments
-		val, err := fn.Native()([]value.Value{})
+		args := make([]value.Value, argCount)
+		for i := 0; i < int(argCount); i++ {
+			args[i], err = vm.stack.Pop()
+			if err != nil {
+				return err
+			}
+		}
+
+		val, err := fn.Native()(args)
 		if err != nil {
 			return err
 		}