Skip to content

Commit cd7e273

Browse files
committed
main: make optscript{Setup,Teardown} reentrant
Signed-off-by: Masatake YAMATO <[email protected]>
1 parent 82561b0 commit cd7e273

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

main/script.c

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -289,27 +289,37 @@ extern void optscriptInstallProcs (EsObject *dict,
289289
}
290290

291291
static EsObject *optscript_CorkIndex_sym = es_nil;
292+
static ptrArray *corkIndexStack;
292293
extern void optscriptSetup (OptVM *vm, EsObject *dict, int corkIndex)
293294
{
295+
if (es_null (optscript_CorkIndex_sym))
296+
optscript_CorkIndex_sym = es_symbol_intern (".");
297+
if (corkIndexStack == NULL)
298+
corkIndexStack = ptrArrayNew ((ptrArrayDeleteFunc)es_object_unref);
299+
300+
/* Push the last '.' to corkIndexStack. */
301+
EsObject *last = es_false;
302+
opt_dict_known_and_get (dict, optscript_CorkIndex_sym, &last);
303+
ptrArrayAdd (corkIndexStack, es_object_ref (last));
304+
294305
if (corkIndex != CORK_NIL)
295306
{
296-
static EsObject *corkIndex_sym = es_nil;
297-
if (es_null (corkIndex_sym))
298-
corkIndex_sym = es_symbol_intern (".");
299307
EsObject *corkIndex_val = es_integer_new (corkIndex);
300-
opt_dict_def (dict, corkIndex_sym, corkIndex_val);
308+
opt_dict_def (dict, optscript_CorkIndex_sym, corkIndex_val);
301309
es_object_unref (corkIndex_val);
302-
optscript_CorkIndex_sym = corkIndex_sym;
303310
}
304311
}
305312

306313
extern void optscriptTeardown (OptVM *vm, EsObject *dict)
307314
{
308-
if (!es_null (optscript_CorkIndex_sym))
309-
{
315+
if (opt_dict_known_and_get (dict, optscript_CorkIndex_sym, NULL))
310316
opt_dict_undef (dict, optscript_CorkIndex_sym);
311-
optscript_CorkIndex_sym = es_nil;
312-
}
317+
318+
/* Pop the last '.'. */
319+
EsObject *index = ptrArrayLast (corkIndexStack);
320+
if (!es_object_equal (index, es_false))
321+
opt_dict_def (dict, optscript_CorkIndex_sym, index);
322+
ptrArrayDeleteLast(corkIndexStack);
313323
}
314324

315325
extern EsObject *optscriptRead (OptVM *vm, const char *src, size_t len)

0 commit comments

Comments
 (0)