diff --git a/modules/dummy.krk b/modules/dummy.krk new file mode 100644 index 0000000..9fbb1f1 --- /dev/null +++ b/modules/dummy.krk @@ -0,0 +1,2 @@ +# Just in case there are no modules sources available... +return object() diff --git a/modules/system.krk b/modules/system.krk deleted file mode 100644 index 00ddeb4..0000000 --- a/modules/system.krk +++ /dev/null @@ -1,7 +0,0 @@ -# This is a module -let module = object() - -module.sleep = __builtins__.sleep -module.uname = __builtins__.uname - -return module diff --git a/src/os.c b/src/os.c new file mode 100644 index 0000000..7a36e6a --- /dev/null +++ b/src/os.c @@ -0,0 +1,53 @@ +/** + * Currently just uname(). + */ +#include +#include +#include +#include +#include +#include + +#include "../vm.h" +#include "../value.h" +#include "../object.h" + +#define S(c) (krk_copyString(c,sizeof(c)-1)) + +/** + * system.uname() + */ +static KrkValue krk_uname(int argc, KrkValue argv[]) { + struct utsname buf; + if (uname(&buf) < 0) return NONE_VAL(); + + KRK_PAUSE_GC(); + + KrkValue result = krk_dict_of(5 * 2, (KrkValue[]) { + OBJECT_VAL(S("sysname")), OBJECT_VAL(krk_copyString(buf.sysname,strlen(buf.sysname))), + OBJECT_VAL(S("nodename")), OBJECT_VAL(krk_copyString(buf.nodename,strlen(buf.nodename))), + OBJECT_VAL(S("release")), OBJECT_VAL(krk_copyString(buf.release,strlen(buf.release))), + OBJECT_VAL(S("version")), OBJECT_VAL(krk_copyString(buf.version,strlen(buf.version))), + OBJECT_VAL(S("machine")), OBJECT_VAL(krk_copyString(buf.machine,strlen(buf.machine))) + }); + + KRK_RESUME_GC(); + + return result; +} + +KrkValue krk_module_onload_os(void) { + KrkInstance * module = krk_newInstance(vm.objectClass); + /* Store it on the stack for now so we can do stuff that may trip GC + * and not lose it to garbage colletion... */ + krk_push(OBJECT_VAL(module)); + + krk_defineNative(&module->fields, "uname", krk_uname); + + /* Pop the module object before returning; it'll get pushed again + * by the VM before the GC has a chance to run, so it's safe. */ + assert(AS_INSTANCE(krk_pop()) == module); + return OBJECT_VAL(module); +} + + diff --git a/src/time.c b/src/time.c new file mode 100644 index 0000000..0d2db7e --- /dev/null +++ b/src/time.c @@ -0,0 +1,48 @@ +/** + * Currently just sleep(). + */ +#include +#include +#include +#include +#include + +#include "../vm.h" +#include "../value.h" +#include "../object.h" + +#define S(c) (krk_copyString(c,sizeof(c)-1)) + +/** + * system.sleep(seconds) + */ +static KrkValue krk_sleep(int argc, KrkValue argv[]) { + if (argc < 1) { + krk_runtimeError(vm.exceptions.argumentError, "sleep: expect at least one argument."); + return BOOLEAN_VAL(0); + } + + /* Accept an integer or a floating point. Anything else, just ignore. */ + unsigned int usecs = (IS_INTEGER(argv[0]) ? AS_INTEGER(argv[0]) : + (IS_FLOATING(argv[0]) ? AS_FLOATING(argv[0]) : 0)) * + 1000000; + + usleep(usecs); + + return BOOLEAN_VAL(1); +} + +KrkValue krk_module_onload_time(void) { + KrkInstance * module = krk_newInstance(vm.objectClass); + /* Store it on the stack for now so we can do stuff that may trip GC + * and not lose it to garbage colletion... */ + krk_push(OBJECT_VAL(module)); + + krk_defineNative(&module->fields, "sleep", krk_sleep); + + /* Pop the module object before returning; it'll get pushed again + * by the VM before the GC has a chance to run, so it's safe. */ + assert(AS_INSTANCE(krk_pop()) == module); + return OBJECT_VAL(module); +} + diff --git a/test/test.krk b/test/test.krk index df6b60f..d477e3a 100755 --- a/test/test.krk +++ b/test/test.krk @@ -1,5 +1,5 @@ #!/home/klange/Projects/kuroko/kuroko -import system +import time # You may want to look at this in an editor with the syntax highlighting # set to Python. Not even bim has a highlighter for Kuroko yet. @@ -61,7 +61,7 @@ print "The function call returned: " + result # figured something with arguments would be more useful? The purpose of this # language is to be used for writing syntax highlighters, configs, and also # plugins for bim, so native bindings are going to be very important. -result = system.sleep(0.1) +result = time.sleep(0.1) print "Call to sleep returned: " + result diff --git a/vm.c b/vm.c index 054cabc..be1d22c 100644 --- a/vm.c +++ b/vm.c @@ -3,7 +3,6 @@ #include #include #include -#include #include #include "vm.h" #include "debug.h" @@ -443,7 +442,7 @@ static KrkValue krk_list_of(int argc, KrkValue argv[]) { * Exposed method called to produce dictionaries from {expr: expr, ...} sequences in managed code. * Presented in the global namespace as dictOf(...). Expects arguments as key,value,key,value... */ -static KrkValue krk_dict_of(int argc, KrkValue argv[]) { +KrkValue krk_dict_of(int argc, KrkValue argv[]) { if (argc % 2 != 0) { krk_runtimeError(vm.exceptions.argumentError, "Expected even number of arguments to dictOf"); return NONE_VAL(); @@ -465,49 +464,6 @@ static KrkValue krk_dict_of(int argc, KrkValue argv[]) { return out; } -#ifndef NO_SYSTEM_BINDS -/** - * system.uname() - */ -static KrkValue krk_uname(int argc, KrkValue argv[]) { - struct utsname buf; - if (uname(&buf) < 0) return NONE_VAL(); - - KRK_PAUSE_GC(); - - KrkValue result = krk_dict_of(5 * 2, (KrkValue[]) { - OBJECT_VAL(S("sysname")), OBJECT_VAL(krk_copyString(buf.sysname,strlen(buf.sysname))), - OBJECT_VAL(S("nodename")), OBJECT_VAL(krk_copyString(buf.nodename,strlen(buf.nodename))), - OBJECT_VAL(S("release")), OBJECT_VAL(krk_copyString(buf.release,strlen(buf.release))), - OBJECT_VAL(S("version")), OBJECT_VAL(krk_copyString(buf.version,strlen(buf.version))), - OBJECT_VAL(S("machine")), OBJECT_VAL(krk_copyString(buf.machine,strlen(buf.machine))) - }); - - KRK_RESUME_GC(); - - return result; -} - -/** - * system.sleep(seconds) - */ -static KrkValue krk_sleep(int argc, KrkValue argv[]) { - if (argc < 1) { - krk_runtimeError(vm.exceptions.argumentError, "sleep: expect at least one argument."); - return BOOLEAN_VAL(0); - } - - /* Accept an integer or a floating point. Anything else, just ignore. */ - unsigned int usecs = (IS_INTEGER(argv[0]) ? AS_INTEGER(argv[0]) : - (IS_FLOATING(argv[0]) ? AS_FLOATING(argv[0]) : 0)) * - 1000000; - - usleep(usecs); - - return BOOLEAN_VAL(1); -} -#endif - /** * __builtins__.set_tracing(mode) */ @@ -1331,12 +1287,6 @@ void krk_initVM(int flags) { /* __builtins__.set_tracing is namespaced */ krk_defineNative(&vm.builtins->fields, "set_tracing", krk_set_tracing); -#ifndef NO_SYSTEM_BINDS - /* Set some other built-ins for the system module */ - krk_defineNative(&vm.builtins->fields, "sleep", krk_sleep); - krk_defineNative(&vm.builtins->fields, "uname", krk_uname); -#endif - /** * Read the managed code builtins module, which contains the base * definitions for collections so we can pull them into the global diff --git a/vm.h b/vm.h index 923ba30..83c4d8c 100644 --- a/vm.h +++ b/vm.h @@ -120,3 +120,5 @@ extern KrkValue krk_runNext(void); extern KrkValue krk_typeOf(int argc, KrkValue argv[]); extern int krk_bindMethod(KrkClass * _class, KrkString * name); extern int krk_callValue(KrkValue callee, int argCount, int extra); + +extern KrkValue krk_dict_of(int argc, KrkValue argv[]);