1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-28 02:59:54 +00:00

Rename some fuctions in util.

This commit is contained in:
Calvin Rose 2017-04-18 16:55:03 -04:00
parent ea432242d3
commit f4a6f4073f
7 changed files with 60 additions and 36 deletions

View File

@ -29,12 +29,13 @@
#include <gst/disasm.h> #include <gst/disasm.h>
/* Compile and run an ast */ /* Compile and run an ast */
int debug_compile_and_run(Gst *vm, GstValue ast) { int debug_compile_and_run(Gst *vm, GstValue ast, GstValue env) {
GstCompiler c; GstCompiler c;
GstValue func; GstValue func;
/* Try to compile generated AST */ /* Try to compile generated AST */
gst_compiler(&c, vm); gst_compiler(&c, vm);
gst_compiler_usemodule(&c, "std"); gst_compiler_usemodule(&c, "std");
gst_compiler_globals(&c, env);
func = gst_wrap_function(gst_compiler_compile(&c, ast)); func = gst_wrap_function(gst_compiler_compile(&c, ast));
/* Check for compilation errors */ /* Check for compilation errors */
if (c.error) { if (c.error) {
@ -92,7 +93,7 @@ int debug_run(Gst *vm, FILE *in) {
printf("Unexpected end of source\n"); printf("Unexpected end of source\n");
return 1; return 1;
} }
return debug_compile_and_run(vm, ast); return debug_compile_and_run(vm, ast, gst_wrap_nil());
} }
/* A simple repl */ /* A simple repl */
@ -100,6 +101,7 @@ int debug_repl(Gst *vm) {
char buffer[1024] = {0}; char buffer[1024] = {0};
const char *reader = buffer; const char *reader = buffer;
GstParser p; GstParser p;
GstValue *st;
int reset; int reset;
for (;;) { for (;;) {
/* Init parser */ /* Init parser */
@ -127,7 +129,10 @@ int debug_repl(Gst *vm) {
printf("Unexpected end of source\n"); printf("Unexpected end of source\n");
continue; continue;
} }
if (0 == debug_compile_and_run(vm, gst_parse_consume(&p))) { /* Add _ to environemt */
st = gst_struct_begin(vm, 1);
gst_struct_put(st, gst_string_cv(vm, "_"), vm->ret);
if (0 == debug_compile_and_run(vm, gst_parse_consume(&p), gst_wrap_struct(gst_struct_end(vm, st)))) {
printf("%s\n", gst_to_string(vm, vm->ret)); printf("%s\n", gst_to_string(vm, vm->ret));
} }
} }

View File

@ -1072,7 +1072,7 @@ void gst_compiler_globals(GstCompiler *c, GstValue env) {
/* Use a module that was loaded into the vm */ /* Use a module that was loaded into the vm */
void gst_compiler_usemodule(GstCompiler *c, const char *modulename) { void gst_compiler_usemodule(GstCompiler *c, const char *modulename) {
GstValue mod = gst_object_get(c->vm->rootenv, gst_string_cv(c->vm, modulename)); GstValue mod = gst_object_get(c->vm->modules, gst_string_cv(c->vm, modulename));
gst_compiler_globals(c, mod); gst_compiler_globals(c, mod);
} }

View File

@ -264,19 +264,14 @@ void gst_mem_tag(void *mem, uint32_t tags) {
/* Run garbage collection */ /* Run garbage collection */
void gst_collect(Gst *vm) { void gst_collect(Gst *vm) {
GstValueUnion renv;
/* Thread can be null */ /* Thread can be null */
if (vm->thread) { if (vm->thread)
GstValueUnion t; gst_mark_value(vm, gst_wrap_thread(vm->thread));
t.thread = vm->thread; gst_mark_value(vm, gst_wrap_object(vm->modules));
gst_mark(vm, t, GST_THREAD); gst_mark_value(vm, gst_wrap_object(vm->registry));
}
renv.object = vm->rootenv;
gst_mark(vm, renv, GST_OBJECT);
gst_mark_value(vm, vm->ret); gst_mark_value(vm, vm->ret);
if (vm->scratch) { if (vm->scratch)
gc_header(vm->scratch)->color = vm->black; gc_header(vm->scratch)->color = vm->black;
}
gst_sweep(vm); gst_sweep(vm);
vm->nextCollection = 0; vm->nextCollection = 0;
} }

View File

@ -414,18 +414,19 @@ int gst_stl_serialize(Gst *vm) {
/* TODO - add userdata to allow for manipulation of FILE pointers. */ /* TODO - add userdata to allow for manipulation of FILE pointers. */
int gst_stl_open(Gst *vm) { int gst_stl_open(Gst *vm) {
GstValue ret;
const uint8_t *fname = gst_to_string(vm, gst_arg(vm, 0)); const uint8_t *fname = gst_to_string(vm, gst_arg(vm, 0));
const uint8_t *fmode = gst_to_string(vm, gst_arg(vm, 1)); const uint8_t *fmode = gst_to_string(vm, gst_arg(vm, 1));
FILE *f; FILE *f;
FILE **fp;
GstValue *st;
if (gst_count_args(vm) < 2) if (gst_count_args(vm) < 2)
gst_c_throwc(vm, "expected filename and filemode"); gst_c_throwc(vm, "expected filename and filemode");
f = fopen((const char *)fname, (const char *)fmode); f = fopen((const char *)fname, (const char *)fmode);
if (!f) if (!f)
gst_c_throwc(vm, "could not open file"); gst_c_throwc(vm, "could not open file");
ret.type = GST_USERDATA; st = gst_struct_begin(vm, 0);
ret.data.pointer = f; fp = gst_userdata(vm, sizeof(FILE *), gst_struct_end(vm, st));
gst_c_return(vm, ret); gst_c_return(vm, gst_wrap_userdata(fp));
} }
/****/ /****/
@ -464,5 +465,5 @@ static const GstModuleItem const std_module[] = {
/* Load all libraries */ /* Load all libraries */
void gst_stl_load(Gst *vm) { void gst_stl_load(Gst *vm) {
gst_c_register(vm, "std", gst_c_module_struct(vm, std_module)); gst_module_put(vm, "std", gst_cmodule_struct(vm, std_module));
} }

View File

@ -56,7 +56,7 @@ GST_WRAP_DEFINE(funcdef, GstFuncDef *, GST_FUNCDEF, def)
#undef GST_WRAP_DEFINE #undef GST_WRAP_DEFINE
GstValue gst_c_module_object(Gst *vm, const GstModuleItem *mod) { GstValue gst_cmodule_object(Gst *vm, const GstModuleItem *mod) {
GstObject *module = gst_object(vm, 10); GstObject *module = gst_object(vm, 10);
while (mod->name != NULL) { while (mod->name != NULL) {
GstValue key = gst_string_cv(vm, mod->name); GstValue key = gst_string_cv(vm, mod->name);
@ -66,7 +66,7 @@ GstValue gst_c_module_object(Gst *vm, const GstModuleItem *mod) {
return gst_wrap_object(module); return gst_wrap_object(module);
} }
GstValue gst_c_module_struct(Gst *vm, const GstModuleItem *mod) { GstValue gst_cmodule_struct(Gst *vm, const GstModuleItem *mod) {
uint32_t count = 0; uint32_t count = 0;
const GstModuleItem *m = mod; const GstModuleItem *m = mod;
GstValue *st; GstValue *st;
@ -86,8 +86,26 @@ GstValue gst_c_module_struct(Gst *vm, const GstModuleItem *mod) {
} }
void gst_c_register(Gst *vm, const char *packagename, GstValue mod) { void gst_module_put(Gst *vm, const char *packagename, GstValue mod) {
if (vm->rootenv == NULL) if (vm->modules == NULL)
vm->rootenv = gst_object(vm, 10); vm->modules = gst_object(vm, 10);
gst_object_put(vm, vm->rootenv, gst_string_cv(vm, packagename), mod); gst_object_put(vm, vm->modules, gst_string_cv(vm, packagename), mod);
}
GstValue gst_module_get(Gst *vm, const char *packagename) {
if (!vm->modules)
return gst_wrap_nil();
return gst_object_get(vm->modules, gst_string_cv(vm, packagename));
}
void gst_register_put(Gst *vm, const char *name, GstValue c) {
if (vm->registry == NULL)
vm->registry = gst_object(vm, 10);
gst_object_put(vm, vm->registry, gst_string_cv(vm, name), c);
}
GstValue gst_register_get(Gst *vm, const char *name) {
if (!vm->registry)
return gst_wrap_nil();
return gst_object_get(vm->registry, gst_string_cv(vm, name));
} }

View File

@ -498,16 +498,17 @@ void gst_init(Gst *vm) {
/* Garbage collection */ /* Garbage collection */
vm->blocks = NULL; vm->blocks = NULL;
vm->nextCollection = 0; vm->nextCollection = 0;
/* Setting memoryInterval to zero currently forces /* Setting memoryInterval to zero forces
* a collection pretty much every cycle, which is * a collection pretty much every cycle, which is
* obviously horrible for performance. It helps ensure * obviously horrible for performance, but helps ensure
* there are no memory bugs during dev */ * there are no memory bugs during dev */
vm->memoryInterval = 2000; vm->memoryInterval = 2000;
vm->black = 0; vm->black = 0;
/* Add thread */ /* Add thread */
vm->thread = NULL; vm->thread = NULL;
/* Set up global env */ /* Set up global env */
vm->rootenv = NULL; vm->modules = NULL;
vm->registry = NULL;
/* Set up scratch memory */ /* Set up scratch memory */
vm->scratch = NULL; vm->scratch = NULL;
vm->scratch_len = 0; vm->scratch_len = 0;
@ -522,7 +523,8 @@ void gst_init(Gst *vm) {
void gst_deinit(Gst *vm) { void gst_deinit(Gst *vm) {
gst_clear_memory(vm); gst_clear_memory(vm);
vm->thread = NULL; vm->thread = NULL;
vm->rootenv = NULL; vm->modules = NULL;
vm->registry = NULL;
vm->ret.type = GST_NIL; vm->ret.type = GST_NIL;
vm->scratch = NULL; vm->scratch = NULL;
vm->scratch_len = 0; vm->scratch_len = 0;

View File

@ -283,13 +283,13 @@ struct Gst {
uint32_t cache_capacity; uint32_t cache_capacity;
uint32_t cache_count; uint32_t cache_count;
uint32_t cache_deleted; uint32_t cache_deleted;
/* Scratch memory */ /* Scratch memory (should be marked in gc) */
char *scratch; char *scratch;
uint32_t scratch_len; uint32_t scratch_len;
/* Thread */ /* GC roots */
GstThread *thread; GstThread *thread;
/* A GC root */ GstObject *modules;
GstObject *rootenv; GstObject *registry;
/* Return state */ /* Return state */
const char *crash; const char *crash;
GstValue ret; /* Returned value from gst_start. Also holds errors. */ GstValue ret; /* Returned value from gst_start. Also holds errors. */
@ -511,9 +511,12 @@ uint16_t gst_count_args(Gst *vm);
/* C Api */ /* C Api */
/***/ /***/
GstValue gst_c_module_object(Gst *vm, const GstModuleItem *mod); GstValue gst_cmodule_object(Gst *vm, const GstModuleItem *mod);
GstValue gst_c_module_struct(Gst *vm, const GstModuleItem *mod); GstValue gst_cmodule_struct(Gst *vm, const GstModuleItem *mod);
void gst_c_register(Gst *vm, const char *packagename, GstValue mod); void gst_module_put(Gst *vm, const char *packagename, GstValue mod);
GstValue gst_module_get(Gst *vm, const char *packagename);
void gst_register_put(Gst *vm, const char *packagename, GstValue mod);
GstValue gst_register_get(Gst *vm, const char *name);
/* Wrap data in GstValue */ /* Wrap data in GstValue */
GstValue gst_wrap_nil(); GstValue gst_wrap_nil();