444cb28d06
Replace it with two functions that won't run into issues with trying to call bound objects, instances, etc. that weren't on the stack. In general, if you have a callable value of unknown type you should call `krk_callStack` with it on the stack followed by all of its args; to make this abundantly clear, `krk_callStack` only takes the argument count. For calling things we know to be safe (closures and natives), there is `krk_callDirect`, which should be used, for example, when calling core methods like __repr__.
56 lines
1.8 KiB
C
56 lines
1.8 KiB
C
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <kuroko/kuroko.h>
|
|
#include <kuroko/vm.h>
|
|
#include <kuroko/util.h>
|
|
|
|
#include "simple-repl.h"
|
|
|
|
int main(int argc, char * argv[]) {
|
|
/* Initialize VM with traceback printing disabled (we'll print them ourselves) */
|
|
krk_initVM(KRK_GLOBAL_CLEAN_OUTPUT);
|
|
|
|
/* Disable imports, ensure the system module is inaccessible, disable print */
|
|
krk_tableDelete(&vm.system->fields, OBJECT_VAL(S("module_paths")));
|
|
krk_tableDelete(&vm.modules, OBJECT_VAL(S("kuroko")));
|
|
krk_tableDelete(&vm.modules, OBJECT_VAL(S("os"))); /* Leaks sensitive information */
|
|
krk_tableDelete(&vm.modules, OBJECT_VAL(S("fileio"))); /* File access is a big no */
|
|
krk_tableDelete(&vm.modules, OBJECT_VAL(S("dis"))); /* Can be used to mess with bytecode and break the VM */
|
|
krk_tableDelete(&vm.modules, OBJECT_VAL(S("threading"))); /* Let's just turn that off for now */
|
|
krk_tableDelete(&vm.modules, OBJECT_VAL(S("gc"))); /* Lets users stop the garbage collector, so let's turn that off */
|
|
|
|
/* Set up our module context. */
|
|
krk_startModule("__main__");
|
|
|
|
/* Attach a docstring so that we can interpret strings */
|
|
krk_attachNamedValue(&krk_currentThread.module->fields,"__doc__", NONE_VAL());
|
|
|
|
int retval = 0;
|
|
|
|
if (argc > 1) {
|
|
KrkValue result = krk_interpret(argv[1], "<stdin>");
|
|
if (!IS_NONE(result)) {
|
|
if (IS_INTEGER(result)) {
|
|
retval = AS_INTEGER(result);
|
|
}
|
|
KrkClass * type = krk_getType(result);
|
|
if (type->_reprer) {
|
|
krk_push(result);
|
|
result = krk_callDirect(type->_reprer, 1);
|
|
}
|
|
if (IS_STRING(result)) {
|
|
fprintf(stdout, " => %s\n", AS_CSTRING(result));
|
|
}
|
|
} else if (krk_currentThread.flags & KRK_THREAD_HAS_EXCEPTION) {
|
|
krk_dumpTraceback();
|
|
retval = 1;
|
|
}
|
|
} else {
|
|
runSimpleRepl();
|
|
}
|
|
|
|
krk_freeVM();
|
|
return retval;
|
|
}
|
|
|