summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2020-09-12 00:12:09 +0200
committerJo-Philipp Wich <jo@mein.io>2020-09-12 00:12:39 +0200
commit0329d7d1708f37396d63eb1801b671f16999390f (patch)
treeb6bf314c4f93d944b160962b673dadddbbc3b818
parent79579e819c289ef1b41b6ba0a643c2fc4d1858cf (diff)
eval: keep references to function contexts
Otherwise the "this" context might be gc'ed before the function can access it. This bug manifested itself with long chained expressions such as: require("fs").open("file.txt").read(10) The file handle produced by open() was gc'ed before invoking read() on it due to the evaluation not increasing its refcount. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
-rw-r--r--eval.c12
1 files changed, 9 insertions, 3 deletions
diff --git a/eval.c b/eval.c
index 1e8d9a3..0f1d7b2 100644
--- a/eval.c
+++ b/eval.c
@@ -1326,7 +1326,9 @@ ut_execute_op(struct ut_state *state, uint32_t off)
case T_LABEL:
scope = ut_getref(state, off, &key);
- state->ctx = scope;
+
+ ut_putval(state->ctx);
+ state->ctx = json_object_get(scope);
val = ut_getval(scope, key);
ut_putval(scope);
@@ -1335,7 +1337,9 @@ ut_execute_op(struct ut_state *state, uint32_t off)
case T_DOT:
scope = ut_getref_required(state, off, &key);
- state->ctx = scope;
+
+ ut_putval(state->ctx);
+ state->ctx = json_object_get(scope);
if (!key)
return scope;
@@ -1349,7 +1353,9 @@ ut_execute_op(struct ut_state *state, uint32_t off)
/* postfix access */
if (op->is_postfix) {
scope = ut_getref_required(state, off, &key);
- state->ctx = scope;
+
+ ut_putval(state->ctx);
+ state->ctx = json_object_get(scope);
if (!key)
return scope;