mirror of
https://github.com/janet-lang/janet
synced 2024-11-25 01:37:19 +00:00
Update representation of an environment.
This commit is contained in:
parent
1878ece2af
commit
de9d7bcfdc
39
core/stl.c
39
core/stl.c
@ -1088,22 +1088,29 @@ static const GstModuleItem std_module[] = {
|
|||||||
{NULL, NULL}
|
{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) {
|
void gst_stl_load(Gst *vm) {
|
||||||
GstValue maybeEnv;
|
GstValue maybeEnv = gst_table_get(vm->modules, gst_string_cvs(vm, "std"));
|
||||||
/* Load the normal c functions */
|
if (maybeEnv.type == GST_TABLE) {
|
||||||
gst_module_mutable(vm, "std", std_module);
|
/* Module already created, so merge into main vm. */
|
||||||
/* 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)
|
|
||||||
gst_env_merge(vm, vm->env, maybeEnv.data.table);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
40
core/util.c
40
core/util.c
@ -343,9 +343,9 @@ int gst_callc(Gst *vm, GstCFunction fn, int numargs, ...) {
|
|||||||
return result;
|
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;
|
GstTable *tab;
|
||||||
GstValue key = gst_wrap_integer(i);
|
GstValue key = gst_string_cv(vm, keyword);
|
||||||
GstValue maybeTab = gst_table_get(env, key);
|
GstValue maybeTab = gst_table_get(env, key);
|
||||||
if (maybeTab.type != GST_TABLE) {
|
if (maybeTab.type != GST_TABLE) {
|
||||||
tab = gst_table(vm, 10);
|
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) {
|
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) {
|
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 */
|
/* 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 */
|
/* Add many global variables */
|
||||||
void gst_env_merge(Gst *vm, GstTable *destEnv, GstTable *srcEnv) {
|
void gst_env_merge(Gst *vm, GstTable *destEnv, GstTable *srcEnv) {
|
||||||
const GstValue *data = srcEnv->data;
|
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) {
|
for (i = 0; i < len; i += 2) {
|
||||||
if (data[i].type == GST_SYMBOL) {
|
if (data[i].type == GST_SYMBOL) {
|
||||||
gst_table_put(vm, destEnv, data[i], data[i + 1]);
|
gst_table_put(vm, destEnv, data[i], data[i + 1]);
|
||||||
} else if (data[i].type == GST_INTEGER) {
|
} else if (data[i].type == GST_STRING) {
|
||||||
switch (data[i].data.integer) {
|
const uint8_t *k = data[i].data.string;
|
||||||
case GST_ENV_NILS:
|
if (streq("nils", k)) {
|
||||||
if (data[i + 1].type == GST_TABLE)
|
if (data[i + 1].type == GST_TABLE)
|
||||||
mergenils(vm, destEnv, data[i + 1].data.table);
|
mergenils(vm, destEnv, data[i + 1].data.table);
|
||||||
break;
|
} else if (streq("meta", k)) {
|
||||||
case GST_ENV_METADATA:
|
if (data[i + 1].type == GST_TABLE)
|
||||||
if (data[i + 1].type == GST_TABLE)
|
mergemeta(vm, destEnv, data[i + 1].data.table);
|
||||||
mergemeta(vm, destEnv, data[i + 1].data.table);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -634,10 +634,6 @@ int gst_hashtable_view(GstValue tab, const GstValue **data, uint32_t *cap);
|
|||||||
/* Misc */
|
/* 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_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);
|
int gst_read_integer(const uint8_t *string, const uint8_t *end, int64_t *ret);
|
||||||
GstReal gst_integer_to_real(GstInteger x);
|
GstReal gst_integer_to_real(GstInteger x);
|
||||||
|
Loading…
Reference in New Issue
Block a user