1
0
mirror of https://github.com/janet-lang/janet synced 2024-06-25 06:33:16 +00:00

Refactor native module declarations. marshal can now

serialize entire environment.
This commit is contained in:
Calvin Rose 2018-08-26 14:35:01 -04:00
parent 45d0597294
commit 75c66ea6dd
20 changed files with 97 additions and 78 deletions

View File

@ -590,7 +590,7 @@ static const DstReg cfuns[] = {
};
DST_MODULE_ENTRY(DstArgs args) {
DstTable *env = dst_env_arg(args);
dst_env_cfuns(env, cfuns);
DstTable *env = dst_env(args);
dst_cfuns(env, "json", cfuns);
return 0;
}

View File

@ -394,7 +394,7 @@ static const DstReg cfuns[] = {
};
DST_MODULE_ENTRY(DstArgs args) {
DstTable *env = dst_env_arg(args);
dst_env_cfuns(env, cfuns);
DstTable *env = dst_env(args);
dst_cfuns(env, "sqlite3", cfuns);
return 0;
}

View File

@ -255,7 +255,7 @@ static const DstReg cfuns[] = {
/* Load the array module */
int dst_lib_array(DstArgs args) {
DstTable *env = dst_env_arg(args);
dst_env_cfuns(env, cfuns);
DstTable *env = dst_env(args);
dst_cfuns(env, NULL, cfuns);
return 0;
}

View File

@ -930,7 +930,7 @@ static const DstReg cfuns[] = {
/* Load the library */
int dst_lib_asm(DstArgs args) {
DstTable *env = dst_env_arg(args);
dst_env_cfuns(env, cfuns);
DstTable *env = dst_env(args);
dst_cfuns(env, NULL, cfuns);
return 0;
}

View File

@ -277,7 +277,7 @@ static const DstReg cfuns[] = {
};
int dst_lib_buffer(DstArgs args) {
DstTable *env = dst_env_arg(args);
dst_env_cfuns(env, cfuns);
DstTable *env = dst_env(args);
dst_cfuns(env, NULL, cfuns);
return 0;
}

View File

@ -194,7 +194,7 @@ DstSlot dstc_resolve(
/* Symbol not found - check for global */
{
Dst check;
DstBindingType btype = dst_env_resolve(c->env, sym, &check);
DstBindingType btype = dst_resolve(c->env, sym, &check);
switch (btype) {
default:
case DST_BINDING_NONE:
@ -455,7 +455,7 @@ static int macroexpand1(
return 0;
}
Dst macroval;
DstBindingType btype = dst_env_resolve(c->env, name, &macroval);
DstBindingType btype = dst_resolve(c->env, name, &macroval);
if (btype != DST_BINDING_MACRO ||
!dst_checktype(macroval, DST_FUNCTION))
return 0;
@ -711,7 +711,7 @@ static const DstReg cfuns[] = {
};
int dst_lib_compile(DstArgs args) {
DstTable *env = dst_env_arg(args);
dst_env_cfuns(env, cfuns);
DstTable *env = dst_env(args);
dst_cfuns(env, NULL, cfuns);
return 0;
}

View File

@ -341,7 +341,8 @@ static void dst_quick_asm(
DST_OUT_OF_MEMORY;
}
memcpy(def->bytecode, bytecode, bytecode_size);
dst_env_def(env, name, dst_wrap_function(dst_thunk(def)));
dst_def(env, name, dst_wrap_function(dst_thunk(def)));
dst_register(name, dst_wrap_function(dst_thunk(def)));
}
/* Macros for easier inline dst assembly */
@ -533,7 +534,7 @@ DstTable *dst_core_env(void) {
Dst ret = dst_wrap_table(env);
/* Load main functions */
dst_env_cfuns(env, cfuns);
dst_cfuns(env, NULL, cfuns);
dst_quick_asm(env, DST_FUN_YIELD, "debug", 0, 1, debug_asm, sizeof(debug_asm));
dst_quick_asm(env, DST_FUN_ERROR, "error", 1, 1, error_asm, sizeof(error_asm));
@ -572,7 +573,7 @@ DstTable *dst_core_env(void) {
templatize_comparator(env, DST_FUN_NEQ, "not==", 1, DOP_NUMERIC_EQUAL);
/* Platform detection */
dst_env_def(env, "dst.version", dst_cstringv(DST_VERSION));
dst_def(env, "dst.version", dst_cstringv(DST_VERSION));
/* Set as gc root */
dst_gcroot(dst_wrap_table(env));
@ -599,7 +600,7 @@ DstTable *dst_core_env(void) {
}
/* Allow references to the environment */
dst_env_def(env, "_env", ret);
dst_def(env, "_env", ret);
/* Run bootstrap source */
dst_dobytes(env, dst_gen_core, sizeof(dst_gen_core), "core.dst");

View File

@ -467,7 +467,7 @@ static const DstReg cfuns[] = {
/* Module entry point */
int dst_lib_fiber(DstArgs args) {
DstTable *env = dst_env_arg(args);
dst_env_cfuns(env, cfuns);
DstTable *env = dst_env(args);
dst_cfuns(env, NULL, cfuns);
return 0;
}

View File

@ -371,14 +371,14 @@ static const DstReg cfuns[] = {
};
static void addf(DstTable *env, const char *name, Dst val) {
dst_env_def(env, name, val);
dst_def(env, name, val);
dst_register(name, val);
}
/* Module entry point */
int dst_lib_io(DstArgs args) {
DstTable *env = dst_env_arg(args);
dst_env_cfuns(env, cfuns);
DstTable *env = dst_env(args);
dst_cfuns(env, NULL, cfuns);
/* stdout */
addf(env, "stdout",

View File

@ -376,10 +376,9 @@ static void marshal_one(MarshalState *st, Dst x, int flags) {
{
MARK_SEEN();
Dst regval = dst_table_get(dst_vm_registry, x);
if (dst_checktype(regval, DST_NIL)) {
if (!dst_checktype(regval, DST_SYMBOL))
goto noregval;
}
const uint8_t *regname = dst_to_string(regval);
const uint8_t *regname = dst_unwrap_symbol(regval);
pushbyte(st, LB_REGISTRY);
pushint(st, dst_string_length(regname));
pushbytes(st, regname, dst_string_length(regname));
@ -1060,7 +1059,7 @@ static const DstReg cfuns[] = {
/* Module entry point */
int dst_lib_marsh(DstArgs args) {
DstTable *env = dst_env_arg(args);
dst_env_cfuns(env, cfuns);
DstTable *env = dst_env(args);
dst_cfuns(env, NULL, cfuns);
return 0;
}

View File

@ -154,11 +154,11 @@ static const DstReg cfuns[] = {
/* Module entry point */
int dst_lib_math(DstArgs args) {
DstTable *env = dst_env_arg(args);
dst_env_cfuns(env, cfuns);
DstTable *env = dst_env(args);
dst_cfuns(env, NULL, cfuns);
dst_env_def(env, "math.pi", dst_wrap_real(3.1415926535897931));
dst_env_def(env, "math.e", dst_wrap_real(2.7182818284590451));
dst_env_def(env, "math.inf", dst_wrap_real(INFINITY));
dst_def(env, "math.pi", dst_wrap_real(3.1415926535897931));
dst_def(env, "math.e", dst_wrap_real(2.7182818284590451));
dst_def(env, "math.inf", dst_wrap_real(INFINITY));
return 0;
}

View File

@ -283,7 +283,7 @@ static const DstReg cfuns[] = {
/* Module entry point */
int dst_lib_os(DstArgs args) {
DstTable *env = dst_env_arg(args);
dst_env_cfuns(env, cfuns);
DstTable *env = dst_env(args);
dst_cfuns(env, NULL, cfuns);
return 0;
}

View File

@ -787,7 +787,7 @@ static const DstReg cfuns[] = {
/* Load the library */
int dst_lib_parse(DstArgs args) {
DstTable *env = dst_env_arg(args);
dst_env_cfuns(env, cfuns);
DstTable *env = dst_env(args);
dst_cfuns(env, NULL, cfuns);
return 0;
}

View File

@ -1002,7 +1002,7 @@ static const DstReg cfuns[] = {
/* Module entry point */
int dst_lib_string(DstArgs args) {
DstTable *env = dst_env_arg(args);
dst_env_cfuns(env, cfuns);
DstTable *env = dst_env(args);
dst_cfuns(env, NULL, cfuns);
return 0;
}

View File

@ -293,8 +293,8 @@ static const DstReg cfuns[] = {
/* Load the table module */
int dst_lib_table(DstArgs args) {
DstTable *env = dst_env_arg(args);
dst_env_cfuns(env, cfuns);
DstTable *env = dst_env(args);
dst_cfuns(env, NULL, cfuns);
return 0;
}

View File

@ -152,11 +152,16 @@ static int cfun_append(DstArgs args) {
DST_RETURN_TUPLE(args, dst_tuple_end(n));
}
static const DstReg cfuns[] = {
{"tuple.slice", cfun_slice},
{"tuple.append", cfun_append},
{"tuple.prepend", cfun_prepend},
{NULL, NULL}
};
/* Load the tuple module */
int dst_lib_tuple(DstArgs args) {
DstTable *env = dst_env_arg(args);
dst_env_def(env, "tuple.slice", dst_wrap_cfunction(cfun_slice));
dst_env_def(env, "tuple.append", dst_wrap_cfunction(cfun_append));
dst_env_def(env, "tuple.prepend", dst_wrap_cfunction(cfun_prepend));
DstTable *env = dst_env(args);
dst_cfuns(env, NULL, cfuns);
return 0;
}

View File

@ -137,21 +137,22 @@ const void *dst_strbinsearch(
return NULL;
}
/* Register a value in the global registry */
void dst_register(const char *name, Dst value) {
Dst regkey = dst_csymbolv(name);
dst_table_put(dst_vm_registry, regkey, value);
dst_table_put(dst_vm_registry, value, regkey);
}
/* Add a module definition */
void dst_env_def(DstTable *env, const char *name, Dst val) {
/* Add a def to an environment */
void dst_def(DstTable *env, const char *name, Dst val) {
DstTable *subt = dst_table(1);
dst_table_put(subt, dst_csymbolv(":value"), val);
dst_table_put(env, dst_csymbolv(name), dst_wrap_table(subt));
}
/* Add a var to the environment */
void dst_env_var(DstTable *env, const char *name, Dst val) {
void dst_var(DstTable *env, const char *name, Dst val) {
DstArray *array = dst_array(1);
DstTable *subt = dst_table(1);
dst_array_push(array, val);
@ -160,19 +161,32 @@ void dst_env_var(DstTable *env, const char *name, Dst val) {
}
/* Load many cfunctions at once */
void dst_env_cfuns(DstTable *env, const DstReg *cfuns) {
void dst_cfuns(DstTable *env, const char *regprefix, const DstReg *cfuns) {
while (cfuns->name) {
Dst name = dst_csymbolv(cfuns->name);
Dst longname = name;
if (regprefix) {
int32_t reglen = 0;
int32_t nmlen = 0;
while (regprefix[reglen]) reglen++;
while (cfuns->name[nmlen]) nmlen++;
uint8_t *longname_buffer =
dst_string_begin(reglen + 1 + nmlen);
memcpy(longname_buffer, regprefix, reglen);
longname_buffer[reglen] = '.';
memcpy(longname_buffer + reglen + 1, cfuns->name, nmlen);
longname = dst_wrap_symbol(dst_string_end(longname_buffer));
}
Dst fun = dst_wrap_cfunction(cfuns->cfun);
dst_env_def(env, cfuns->name, fun);
dst_table_put(dst_vm_registry, name, fun);
dst_table_put(dst_vm_registry, fun, name);
dst_def(env, cfuns->name, fun);
dst_table_put(dst_vm_registry, longname, fun);
dst_table_put(dst_vm_registry, fun, longname);
cfuns++;
}
}
/* Resolve a symbol in the environment */
DstBindingType dst_env_resolve(DstTable *env, const uint8_t *sym, Dst *out) {
DstBindingType dst_resolve(DstTable *env, const uint8_t *sym, Dst *out) {
Dst ref;
DstTable *entry_table;
Dst entry = dst_table_get(env, dst_wrap_symbol(sym));
@ -195,7 +209,7 @@ DstBindingType dst_env_resolve(DstTable *env, const uint8_t *sym, Dst *out) {
}
/* Get module from the arguments passed to library */
DstTable *dst_env_arg(DstArgs args) {
DstTable *dst_env(DstArgs args) {
DstTable *module;
if (args.n >= 1 && dst_checktype(args.v[0], DST_TABLE)) {
module = dst_unwrap_table(args.v[0]);

View File

@ -38,4 +38,19 @@ const void *dst_strbinsearch(
size_t itemsize,
const uint8_t *key);
/* Initialize builtin libraries */
int dst_lib_io(DstArgs args);
int dst_lib_math(DstArgs args);
int dst_lib_array(DstArgs args);
int dst_lib_tuple(DstArgs args);
int dst_lib_buffer(DstArgs args);
int dst_lib_table(DstArgs args);
int dst_lib_fiber(DstArgs args);
int dst_lib_os(DstArgs args);
int dst_lib_string(DstArgs args);
int dst_lib_marsh(DstArgs args);
int dst_lib_parse(DstArgs args);
int dst_lib_asm(DstArgs args);
int dst_lib_compile(DstArgs args);
#endif

View File

@ -1031,7 +1031,6 @@ DST_API int dst_equals(Dst x, Dst y);
DST_API int32_t dst_hash(Dst x);
DST_API int dst_compare(Dst x, Dst y);
DST_API int dst_cstrcmp(const uint8_t *str, const char *other);
DST_API void dst_register(const char *name, Dst value);
/* VM functions */
DST_API int dst_init(void);
@ -1040,18 +1039,19 @@ DST_API DstSignal dst_continue(DstFiber *fiber, Dst in, Dst *out);
#define dst_run(F,O) dst_continue(F, dst_wrap_nil(), O)
DST_API DstSignal dst_call(DstFunction *fun, int32_t argn, const Dst *argv, Dst *out, DstFiber **f);
/* Env helpers */
/* C Library helpers */
typedef enum {
DST_BINDING_NONE,
DST_BINDING_DEF,
DST_BINDING_VAR,
DST_BINDING_MACRO
} DstBindingType;
DST_API void dst_env_def(DstTable *env, const char *name, Dst val);
DST_API void dst_env_var(DstTable *env, const char *name, Dst val);
DST_API void dst_env_cfuns(DstTable *env, const DstReg *cfuns);
DST_API DstBindingType dst_env_resolve(DstTable *env, const uint8_t *sym, Dst *out);
DST_API DstTable *dst_env_arg(DstArgs args);
DST_API void dst_def(DstTable *env, const char *name, Dst val);
DST_API void dst_var(DstTable *env, const char *name, Dst val);
DST_API void dst_cfuns(DstTable *env, const char *regprefix, const DstReg *cfuns);
DST_API DstBindingType dst_resolve(DstTable *env, const uint8_t *sym, Dst *out);
DST_API DstTable *dst_env(DstArgs args);
DST_API void dst_register(const char *name, Dst value);
/* C Function helpers */
DST_API int dst_arity_err(DstArgs args, int32_t n, const char *prefix);
@ -1059,21 +1059,6 @@ DST_API int dst_type_err(DstArgs args, int32_t n, DstType expected);
DST_API int dst_typemany_err(DstArgs args, int32_t n, int expected);
DST_API int dst_typeabstract_err(DstArgs args, int32_t n, const DstAbstractType *at);
/* Initialize builtin libraries */
DST_API int dst_lib_io(DstArgs args);
DST_API int dst_lib_math(DstArgs args);
DST_API int dst_lib_array(DstArgs args);
DST_API int dst_lib_tuple(DstArgs args);
DST_API int dst_lib_buffer(DstArgs args);
DST_API int dst_lib_table(DstArgs args);
DST_API int dst_lib_fiber(DstArgs args);
DST_API int dst_lib_os(DstArgs args);
DST_API int dst_lib_string(DstArgs args);
DST_API int dst_lib_marsh(DstArgs args);
DST_API int dst_lib_parse(DstArgs args);
DST_API int dst_lib_asm(DstArgs args);
DST_API int dst_lib_compile(DstArgs args);
/* Helpers for writing modules */
#define DST_MODULE_ENTRY DST_API int _dst_init

View File

@ -38,10 +38,10 @@ int main(int argc, char **argv) {
args = dst_array(argc);
for (i = 0; i < argc; i++)
dst_array_push(args, dst_cstringv(argv[i]));
dst_env_def(env, "process.args", dst_wrap_array(args));
dst_def(env, "process.args", dst_wrap_array(args));
/* Expose line getter */
dst_env_def(env, "getline", dst_wrap_cfunction(dst_line_getter));
dst_def(env, "getline", dst_wrap_cfunction(dst_line_getter));
dst_register("getline", dst_wrap_cfunction(dst_line_getter));
dst_line_init();