diff options
author | Jo-Philipp Wich <jo@mein.io> | 2020-10-19 17:52:07 +0200 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2020-10-19 23:36:05 +0200 |
commit | 3edf942ac85a90707e57d9e251004459aa6b4cea (patch) | |
tree | c47c2068b22ecbe4683a76807ca6fbf266f34b0f /eval.c | |
parent | 1149ffa23be8ff4059f75068c9e36cfda52a71f8 (diff) |
eval: record correct source contexts in call stack
Also handle calls to C functions.
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'eval.c')
-rw-r--r-- | eval.c | 27 |
1 files changed, 15 insertions, 12 deletions
@@ -975,7 +975,7 @@ ut_invoke(struct ut_state *state, uint32_t off, struct json_object *this, struct json_object *rv = NULL; struct ut_scope *sc; size_t arridx; - ut_c_fn *cfn; + ut_c_fn *fptr; if (!tag) return NULL; @@ -986,32 +986,30 @@ ut_invoke(struct ut_state *state, uint32_t off, struct json_object *this, return ut_new_exception(state, op->off, "Runtime error: Too much recursion"); callstack.next = state->callstack; - callstack.source = state->source; - callstack.funcname = state->function ? state->function->name : NULL; + callstack.function = state->function; callstack.off = op ? op->off : 0; callstack.ctx = json_object_get(this ? this : state->ctx); state->callstack = &callstack; state->calldepth++; + fn = tag->tag.data; + + prev_fn = state->function; + state->function = fn; + /* is native function */ if (tag->type == T_CFUNC) { - cfn = (ut_c_fn *)tag->tag.data; - rv = cfn ? cfn(state, off, argvals) : NULL; + fptr = (ut_c_fn *)fn->cfn; + rv = fptr ? fptr(state, off, argvals) : NULL; } /* is utpl function */ else { - fn = tag->tag.data; - callstack.scope = ut_new_scope(state, fn->parent_scope); sc = state->scope; - state->scope = ut_acquire_scope(callstack.scope); - prev_fn = state->function; - state->function = fn; - if (fn->args) for (arridx = 0; arridx < json_object_array_length(fn->args); arridx++) ut_setval(callstack.scope->scope, json_object_array_get_idx(fn->args, arridx), @@ -1042,9 +1040,10 @@ ut_invoke(struct ut_state *state, uint32_t off, struct json_object *this, /* ... and release it */ ut_release_scope(callstack.scope); - state->function = prev_fn; } + state->function = prev_fn; + json_object_put(callstack.ctx); state->callstack = callstack.next; state->calldepth--; @@ -1679,11 +1678,15 @@ struct json_object * ut_run(struct ut_state *state, struct json_object *env, struct json_object *modules) { struct json_object *args, *rv; + struct ut_function fn = {}; size_t i; state->scope = ut_new_scope(state, NULL); state->ctx = NULL; + fn.source = state->source; + state->function = &fn; + if (env) { json_object_object_foreach(env, key, val) ut_register_variable(state->scope->scope, key, json_object_get(val)); |