diff options
-rw-r--r-- | eval.c | 20 | ||||
-rw-r--r-- | eval.h | 2 | ||||
-rw-r--r-- | main.c | 23 |
3 files changed, 37 insertions, 8 deletions
@@ -1557,10 +1557,11 @@ ut_register_variable(struct json_object *scope, const char *key, struct json_obj } enum ut_error_type -ut_run(struct ut_state *state, struct json_object *env) +ut_run(struct ut_state *state, struct json_object *env, struct json_object *modules) { struct ut_op *op = ut_get_op(state, state->main); struct json_object *entry, *scope, *args, *rv; + size_t i; if (!op || op->type != T_FUNC) { ut_exception(state, state->main, "Runtime error: Invalid root operation in AST"); @@ -1589,6 +1590,23 @@ ut_run(struct ut_state *state, struct json_object *env) ut_lib_init(state, scope); args = json_object_new_array(); + + if (modules) { + for (i = 0; i < json_object_array_length(modules); i++) { + json_object_array_put_idx(args, 0, json_object_get(json_object_array_get_idx(modules, i))); + + rv = ut_invoke(state, state->main, NULL, + json_object_object_get(scope, "require"), + args); + + ut_register_variable(scope, + json_object_get_string(json_object_array_get_idx(modules, i)), + rv); + } + + json_object_array_del_idx(args, 0, 1); + } + rv = ut_invoke(state, state->main, NULL, entry, args); json_object_put(entry); @@ -43,6 +43,6 @@ struct json_object * ut_invoke(struct ut_state *, uint32_t, struct json_object *, struct json_object *, struct json_object *); enum ut_error_type -ut_run(struct ut_state *state, struct json_object *env); +ut_run(struct ut_state *state, struct json_object *env, struct json_object *modules); #endif @@ -47,7 +47,8 @@ print_usage(char *app) " -r Do not trim trailing block newlines\n" " -S Enable strict mode\n" " -e Set global variables from given JSON object\n" - " -E Set global variables from given JSON file\n", + " -E Set global variables from given JSON file\n" + " -m Preload given module\n", app); } @@ -141,7 +142,8 @@ static void dump(struct ut_state *s, uint32_t off, int level) { #endif /* NDEBUG */ static enum ut_error_type -parse(struct ut_state *state, const char *source, bool dumponly, struct json_object *env) +parse(struct ut_state *state, const char *source, bool dumponly, + struct json_object *env, struct json_object *modules) { enum ut_error_type err; char *msg; @@ -158,7 +160,7 @@ parse(struct ut_state *state, const char *source, bool dumponly, struct json_obj #endif /* NDEBUG */ } else { - err = ut_run(state, env); + err = ut_run(state, env, modules); } } @@ -232,7 +234,7 @@ int main(int argc, char **argv) { char *srcstr = NULL, *srcfile = NULL, *envstr = NULL; - struct json_object *env = NULL, *o; + struct json_object *env = NULL, *modules = NULL, *o; struct ut_state *state; bool dumponly = false; int opt, rv = 0; @@ -253,7 +255,7 @@ main(int argc, char **argv) state->lstrip_blocks = 1; state->trim_blocks = 1; - while ((opt = getopt(argc, argv, "dhlrSe:E:i:s:")) != -1) + while ((opt = getopt(argc, argv, "dhlrSe:E:i:s:m:")) != -1) { switch (opt) { case 'h': @@ -322,6 +324,13 @@ main(int argc, char **argv) json_object_object_add(env, key, val); break; + + case 'm': + modules = modules ? modules : json_object_new_array(); + + json_object_array_add(modules, json_object_new_string(optarg)); + + break; } } @@ -332,9 +341,11 @@ main(int argc, char **argv) goto out; } - rv = parse(state, srcstr ? srcstr : srcfile, dumponly, env); + rv = parse(state, srcstr ? srcstr : srcfile, dumponly, env, modules); out: + json_object_put(modules); + json_object_put(env); free(srcfile); return rv; |