From cd7e27388a0ff4defb2b68b828e0900eebc6c24b Mon Sep 17 00:00:00 2001 From: Masatake YAMATO Date: Sun, 23 May 2021 15:47:00 +0900 Subject: [PATCH] main: make optscript{Setup,Teardown} reentrant Signed-off-by: Masatake YAMATO --- main/script.c | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/main/script.c b/main/script.c index 5c81da8560..d8accdbea1 100644 --- a/main/script.c +++ b/main/script.c @@ -289,27 +289,37 @@ extern void optscriptInstallProcs (EsObject *dict, } static EsObject *optscript_CorkIndex_sym = es_nil; +static ptrArray *corkIndexStack; extern void optscriptSetup (OptVM *vm, EsObject *dict, int corkIndex) { + if (es_null (optscript_CorkIndex_sym)) + optscript_CorkIndex_sym = es_symbol_intern ("."); + if (corkIndexStack == NULL) + corkIndexStack = ptrArrayNew ((ptrArrayDeleteFunc)es_object_unref); + + /* Push the last '.' to corkIndexStack. */ + EsObject *last = es_false; + opt_dict_known_and_get (dict, optscript_CorkIndex_sym, &last); + ptrArrayAdd (corkIndexStack, es_object_ref (last)); + if (corkIndex != CORK_NIL) { - static EsObject *corkIndex_sym = es_nil; - if (es_null (corkIndex_sym)) - corkIndex_sym = es_symbol_intern ("."); EsObject *corkIndex_val = es_integer_new (corkIndex); - opt_dict_def (dict, corkIndex_sym, corkIndex_val); + opt_dict_def (dict, optscript_CorkIndex_sym, corkIndex_val); es_object_unref (corkIndex_val); - optscript_CorkIndex_sym = corkIndex_sym; } } extern void optscriptTeardown (OptVM *vm, EsObject *dict) { - if (!es_null (optscript_CorkIndex_sym)) - { + if (opt_dict_known_and_get (dict, optscript_CorkIndex_sym, NULL)) opt_dict_undef (dict, optscript_CorkIndex_sym); - optscript_CorkIndex_sym = es_nil; - } + + /* Pop the last '.'. */ + EsObject *index = ptrArrayLast (corkIndexStack); + if (!es_object_equal (index, es_false)) + opt_dict_def (dict, optscript_CorkIndex_sym, index); + ptrArrayDeleteLast(corkIndexStack); } extern EsObject *optscriptRead (OptVM *vm, const char *src, size_t len)