// SPDX-License-Identifier: MIT // Copyright (c) 2023 John Watts and the LuminaSensum contributors #include "vm.h" #include "object.h" #include <stddef.h> void vm_stack_set(struct vm_state *state, int index, struct object *obj) { struct object **stack_pos = &state->stack_base[index]; struct object *old = *stack_pos; if (old != NULL) { object_drop(stack_pos); } *stack_pos = obj; } struct object *vm_stack_get(struct vm_state *state, int index) { struct object *obj = state->stack_base[index]; if (obj != NULL) { object_hold(obj); } return obj; } void vm_stack_push(struct vm_state *state, struct object *obj) { *state->stack_next++ = obj; } struct object *vm_stack_pop(struct vm_state *state) { struct object *obj = *--state->stack_next; *state->stack_next = NULL; return obj; } void vm_stack_drop(struct vm_state *state, int count) { while (count--) { struct object *obj = vm_stack_pop(state); if (obj != NULL) { object_drop(&obj); } } } void vm_call(struct object *obj, const char *name, int arg_count, struct vm_state *state) { struct object **old_base = state->stack_base; state->stack_base = state->stack_next - arg_count; dispatch_call(obj, name, arg_count, state); state->stack_base = old_base; }