1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-28 19:19:53 +00:00

Update representation of an environment.

This commit is contained in:
bakpakin 2017-07-09 16:10:47 -04:00
parent 1878ece2af
commit de9d7bcfdc
3 changed files with 47 additions and 36 deletions

View File

@ -1088,22 +1088,29 @@ static const GstModuleItem std_module[] = {
{NULL, NULL}
};
/* Load all libraries */
/* Load stl library into the current environment. Create stl module object
* only if it is not yet created. */
void gst_stl_load(Gst *vm) {
GstValue maybeEnv;
/* Load the normal c functions */
gst_module_mutable(vm, "std", std_module);
/* Wrap stdin and stdout */
FILE **inp = gst_userdata(vm, sizeof(FILE *), &gst_stl_filetype);
FILE **outp = gst_userdata(vm, sizeof(FILE *), &gst_stl_filetype);
FILE **errp = gst_userdata(vm, sizeof(FILE *), &gst_stl_filetype);
*inp = stdin;
*outp = stdout;
*errp = stderr;
gst_module_put(vm, "std", "stdin", gst_wrap_userdata(inp));
gst_module_put(vm, "std", "stdout", gst_wrap_userdata(outp));
gst_module_put(vm, "std", "stderr", gst_wrap_userdata(outp));
maybeEnv = gst_table_get(vm->modules, gst_string_cvs(vm, "std"));
if (maybeEnv.type == GST_TABLE)
GstValue maybeEnv = gst_table_get(vm->modules, gst_string_cvs(vm, "std"));
if (maybeEnv.type == GST_TABLE) {
/* Module already created, so merge into main vm. */
gst_env_merge(vm, vm->env, maybeEnv.data.table);
} else {
/* Module not yet created */
/* Load the normal c functions */
gst_module_mutable(vm, "std", std_module);
/* Wrap stdin and stdout */
FILE **inp = gst_userdata(vm, sizeof(FILE *), &gst_stl_filetype);
FILE **outp = gst_userdata(vm, sizeof(FILE *), &gst_stl_filetype);
FILE **errp = gst_userdata(vm, sizeof(FILE *), &gst_stl_filetype);
*inp = stdin;
*outp = stdout;
*errp = stderr;
gst_module_put(vm, "std", "stdin", gst_wrap_userdata(inp));
gst_module_put(vm, "std", "stdout", gst_wrap_userdata(outp));
gst_module_put(vm, "std", "stderr", gst_wrap_userdata(outp));
// Now merge
maybeEnv = gst_table_get(vm->modules, gst_string_cvs(vm, "std"));
gst_env_merge(vm, vm->env, maybeEnv.data.table);
}
}

View File

@ -343,9 +343,9 @@ int gst_callc(Gst *vm, GstCFunction fn, int numargs, ...) {
return result;
}
static GstTable *gst_env_inttab(Gst *vm, GstTable *env, GstInteger i) {
static GstTable *gst_env_keytab(Gst *vm, GstTable *env, const char *keyword) {
GstTable *tab;
GstValue key = gst_wrap_integer(i);
GstValue key = gst_string_cv(vm, keyword);
GstValue maybeTab = gst_table_get(env, key);
if (maybeTab.type != GST_TABLE) {
tab = gst_table(vm, 10);
@ -357,11 +357,11 @@ static GstTable *gst_env_inttab(Gst *vm, GstTable *env, GstInteger i) {
}
GstTable *gst_env_nils(Gst *vm, GstTable *env) {
return gst_env_inttab(vm, env, GST_ENV_NILS);
return gst_env_keytab(vm, env, "nils");
}
GstTable *gst_env_meta(Gst *vm, GstTable *env) {
return gst_env_inttab(vm, env, GST_ENV_METADATA);
return gst_env_keytab(vm, env, "meta");
}
/* Add many global variables and bind to nil */
@ -391,6 +391,18 @@ static void mergemeta(Gst *vm, GstTable *destEnv, GstTable *meta) {
}
}
/* Simple strequal between gst string ans c string, no 0s in b allowed */
static int streq(const char *str, const uint8_t *b) {
uint32_t len = gst_string_length(b);
uint32_t i;
const uint8_t *ustr = (const uint8_t *)str;
for (i = 0; i < len; ++i) {
if (ustr[i] != b[i])
return 0;
}
return 1;
}
/* Add many global variables */
void gst_env_merge(Gst *vm, GstTable *destEnv, GstTable *srcEnv) {
const GstValue *data = srcEnv->data;
@ -399,18 +411,14 @@ void gst_env_merge(Gst *vm, GstTable *destEnv, GstTable *srcEnv) {
for (i = 0; i < len; i += 2) {
if (data[i].type == GST_SYMBOL) {
gst_table_put(vm, destEnv, data[i], data[i + 1]);
} else if (data[i].type == GST_INTEGER) {
switch (data[i].data.integer) {
case GST_ENV_NILS:
if (data[i + 1].type == GST_TABLE)
mergenils(vm, destEnv, data[i + 1].data.table);
break;
case GST_ENV_METADATA:
if (data[i + 1].type == GST_TABLE)
mergemeta(vm, destEnv, data[i + 1].data.table);
break;
default:
break;
} else if (data[i].type == GST_STRING) {
const uint8_t *k = data[i].data.string;
if (streq("nils", k)) {
if (data[i + 1].type == GST_TABLE)
mergenils(vm, destEnv, data[i + 1].data.table);
} else if (streq("meta", k)) {
if (data[i + 1].type == GST_TABLE)
mergemeta(vm, destEnv, data[i + 1].data.table);
}
}
}

View File

@ -634,10 +634,6 @@ int gst_hashtable_view(GstValue tab, const GstValue **data, uint32_t *cap);
/* Misc */
/****/
#define GST_ENV_NILS 0
#define GST_ENV_METADATA 1
#define GST_ENV_VARS 2
int gst_read_real(const uint8_t *string, const uint8_t *end, double *ret, int forceInt);
int gst_read_integer(const uint8_t *string, const uint8_t *end, int64_t *ret);
GstReal gst_integer_to_real(GstInteger x);