Move sleep, uname to Pythonic module names
This commit is contained in:
parent
a4ba95d5ea
commit
0966a21c7a
2
modules/dummy.krk
Normal file
2
modules/dummy.krk
Normal file
@ -0,0 +1,2 @@
|
||||
# Just in case there are no modules sources available...
|
||||
return object()
|
@ -1,7 +0,0 @@
|
||||
# This is a module
|
||||
let module = object()
|
||||
|
||||
module.sleep = __builtins__.sleep
|
||||
module.uname = __builtins__.uname
|
||||
|
||||
return module
|
53
src/os.c
Normal file
53
src/os.c
Normal file
@ -0,0 +1,53 @@
|
||||
/**
|
||||
* Currently just uname().
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/utsname.h>
|
||||
|
||||
#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);
|
||||
}
|
||||
|
||||
|
48
src/time.c
Normal file
48
src/time.c
Normal file
@ -0,0 +1,48 @@
|
||||
/**
|
||||
* Currently just sleep().
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#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);
|
||||
}
|
||||
|
@ -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
|
||||
|
||||
|
52
vm.c
52
vm.c
@ -3,7 +3,6 @@
|
||||
#include <unistd.h>
|
||||
#include <dlfcn.h>
|
||||
#include <errno.h>
|
||||
#include <sys/utsname.h>
|
||||
#include <sys/stat.h>
|
||||
#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
|
||||
|
2
vm.h
2
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[]);
|
||||
|
Loading…
Reference in New Issue
Block a user