summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--ast.c13
-rw-r--r--ast.h2
-rw-r--r--eval.c9
3 files changed, 20 insertions, 4 deletions
diff --git a/ast.c b/ast.c
index 21c2b65..390c4d2 100644
--- a/ast.c
+++ b/ast.c
@@ -323,6 +323,7 @@ ut_new_func(struct ut_state *s, struct ut_op *decl, struct ut_scope *scope)
struct ut_op *op, *name, *args, *arg;
struct ut_function *fn;
size_t sz;
+ char *p;
sz = ALIGN(sizeof(*op)) + ALIGN(sizeof(*fn));
@@ -332,16 +333,24 @@ ut_new_func(struct ut_state *s, struct ut_op *decl, struct ut_scope *scope)
if (name)
sz += ALIGN(json_object_get_string_len(name->val) + 1);
+ if (s->filename)
+ sz += ALIGN(strlen(s->filename) + 1);
+
op = xalloc(sz);
fn = (void *)op + ALIGN(sizeof(*op));
fn->entry = decl->tree.operand[2];
+ p = (char *)fn + ALIGN(sizeof(*fn));
+
if (name) {
- fn->name = (char *)fn + ALIGN(sizeof(*fn));
- strcpy(fn->name, json_object_get_string(name->val));
+ fn->name = strcpy(p, json_object_get_string(name->val));
+ p += ALIGN(json_object_get_string_len(name->val) + 1);
}
+ if (s->filename)
+ fn->filename = strcpy(p, s->filename);
+
if (args) {
fn->args = xjs_new_array();
diff --git a/ast.h b/ast.h
index 365d4c0..8d9a8eb 100644
--- a/ast.h
+++ b/ast.h
@@ -88,7 +88,7 @@ struct ut_scope {
};
struct ut_function {
- char *name;
+ char *name, *filename;
struct json_object *args;
struct ut_scope *scope, *parent_scope;
uint32_t entry;
diff --git a/eval.c b/eval.c
index e9c797b..881766b 100644
--- a/eval.c
+++ b/eval.c
@@ -1004,6 +1004,7 @@ ut_invoke(struct ut_state *state, uint32_t off, struct json_object *this,
struct json_object *rv = NULL;
struct ut_function *fn;
struct ut_scope *sc;
+ char *filename;
size_t arridx;
ut_c_fn *cfn;
@@ -1022,7 +1023,10 @@ ut_invoke(struct ut_state *state, uint32_t off, struct json_object *this,
fn->scope->ctx = json_object_get(this ? this : state->ctx);
sc = state->scope;
+ filename = state->filename;
+
state->scope = ut_acquire_scope(fn->scope);
+ state->filename = fn->filename;
if (fn->args)
for (arridx = 0; arridx < json_object_array_length(fn->args); arridx++)
@@ -1055,10 +1059,13 @@ ut_invoke(struct ut_state *state, uint32_t off, struct json_object *this,
json_object_put(fn->scope->ctx);
fn->scope->ctx = NULL;
- /* ... and reset the function scope */
+ /* ... and reset the function scope... */
ut_release_scope(fn->scope);
fn->scope = NULL;
+ /* ... and the file name context */
+ state->filename = filename;
+
return rv;
}