diff --git a/lang/bytecode.c b/lang/bytecode.c index 19bb6f5..e56826f 100644 --- a/lang/bytecode.c +++ b/lang/bytecode.c @@ -11,6 +11,8 @@ #include "rational.h" #include "string.h" #include "vm.h" +#define MODULE_INTERNAL_API +#include "module.h" // Opcode definitions, see bytecode_run for further documentation enum opcodes { @@ -32,8 +34,8 @@ OP_GET_ARG = 0x0F, }; -void bytecode_run( - VmState state, Object obj, const unsigned char *bytecode, Object args) { +void bytecode_run(VmState state, Object obj, const unsigned char *bytecode, + Object module) { const unsigned char *pos_code = &bytecode[0]; unsigned char op = OP_END; while ((op = *pos_code++) != OP_END) { @@ -170,10 +172,7 @@ case OP_GET_ARG: { debug_msg("OP_GET_ARG %i\n", *pos_code); int index = *pos_code++; - vm_abort_if(state, - object_list_length(state, args) <= index, - "argument out of range"); - Object obj = object_list_get(state, args, index); + Object obj = module_runtime_use(state, module, index); vm_stack_push(state, obj); break; } diff --git a/lang/bytecode.h b/lang/bytecode.h index bba4371..a73a1d7 100644 --- a/lang/bytecode.h +++ b/lang/bytecode.h @@ -9,8 +9,8 @@ // Runs bytecode // obj is used for OP_SELF -// args is used for OP_GET_ARG -void bytecode_run( - VmState state, Object obj, const unsigned char *bytecode, Object args); +// module is used for OP_GET_ARG +void bytecode_run(VmState state, Object obj, const unsigned char *bytecode, + Object module); #endif diff --git a/lang/module.c b/lang/module.c index 54d7b98..9d805d8 100644 --- a/lang/module.c +++ b/lang/module.c @@ -57,12 +57,11 @@ object_drop(state, &data->uses); } -Object module_runtime_uses(VmState state, Object obj) { +Object module_runtime_use(VmState state, Object obj, int index) { struct module_runtime_data *data = (struct module_runtime_data *)object_priv( state, obj, &module_runtime_class); - object_hold(state, data->uses); - return data->uses; + return object_list_get(state, data->uses, index); } static struct object_call module_runtime_calls[] = {{.name = NULL, /* end */}}; @@ -102,16 +101,13 @@ struct module_class_data *data = (struct module_class_data *)object_priv( state, obj, &module_class_class); - struct module_runtime_data *runtime = - (struct module_runtime_data *)object_priv( - state, data->module_runtime, &module_runtime_class); ModuleClassCall *call = data->class->calls; - Object args = runtime->uses; while (call->name != NULL) { if (strcmp(call->name, name) == 0) { const unsigned char *bytecode = data->class->bytecodes[call->bytecode_index]; - bytecode_run(state, obj, bytecode, args); + bytecode_run( + state, obj, bytecode, data->module_runtime); return; } ++call; diff --git a/lang/module.h b/lang/module.h index a8f1beb..12896ea 100644 --- a/lang/module.h +++ b/lang/module.h @@ -47,6 +47,9 @@ typedef struct module_info ModuleInfo; +// Gets another module from a module's runtime data +Object module_runtime_use(VmState state, Object module, int index); + #endif #endif