Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revise: the way of accessing the optVm's appData #3868

Merged
merged 2 commits into from
Dec 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions dsl/optscript.c
Original file line number Diff line number Diff line change
Expand Up @@ -647,12 +647,13 @@
}

void
opt_vm_clear (OptVM *vm)
opt_vm_clear (OptVM *vm, bool clear_app_data)
{
ptrArrayClear (vm->estack);
ptrArrayClear (vm->ostack);
vm_dstack_clear (vm);
vm->app_data = NULL;
if (clear_app_data)
vm->app_data = NULL;

Check warning on line 656 in dsl/optscript.c

View check run for this annotation

Codecov / codecov/patch

dsl/optscript.c#L656

Added line #L656 was not covered by tests
dict_op_clear (vm->error);
}

Expand Down
2 changes: 1 addition & 1 deletion dsl/optscript.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void opt_vm_print_prompt (OptVM *vm);
int opt_vm_help (OptVM *vm, MIO *out,
struct OptHelpExtender *extop, void *data);

void opt_vm_clear (OptVM *vm);
void opt_vm_clear (OptVM *vm, bool clear_app_data);
void opt_vm_dstack_push (OptVM *vm, EsObject *dict);
void opt_vm_dstack_pop (OptVM *vm);

Expand Down
2 changes: 1 addition & 1 deletion extra-cmds/optscript-repl.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@
r = optscript_run (vm, file? NULL: "OPT", &renew);
if (renew)
{
opt_vm_clear (vm);
opt_vm_clear (vm, true);

Check warning on line 214 in extra-cmds/optscript-repl.c

View check run for this annotation

Codecov / codecov/patch

extra-cmds/optscript-repl.c#L214

Added line #L214 was not covered by tests
opt_vm_dstack_push (vm, dict);
renew = false;
goto renew;
Expand Down
112 changes: 75 additions & 37 deletions main/lregex.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,12 @@
ptrArray *hook_code[SCRIPT_HOOK_MAX];

langType owner;
};

typedef struct {
struct lregexControlBlock *lcb;
scriptWindow *window;
};
} appData;

/*
* DATA DEFINITIONS
Expand Down Expand Up @@ -272,6 +275,34 @@
static char* make_match_string (scriptWindow *window, int group);
static matchLoc *make_mloc (scriptWindow *window, int group, bool start);

static struct lregexControlBlock *get_current_lcb(OptVM *vm)
{
appData * d = opt_vm_get_app_data (vm);
return d->lcb;
}

static scriptWindow *get_current_window(OptVM *vm)
{
appData * d = opt_vm_get_app_data (vm);
return d->window;
}

static struct lregexControlBlock *set_current_lcb(OptVM *vm, struct lregexControlBlock *new_lcb)
{
appData * d = opt_vm_get_app_data (vm);
struct lregexControlBlock *old_lcb = d->lcb;
d->lcb = new_lcb;
return old_lcb;
}

static scriptWindow *set_current_window(OptVM *vm, scriptWindow *new_windown)
{
appData * d = opt_vm_get_app_data (vm);
scriptWindow *old_window = d->window;
d->window = new_windown;
return old_window;
}

static void deleteTable (void *ptrn)
{
struct regexTable *t = ptrn;
Expand Down Expand Up @@ -2139,15 +2170,15 @@
if (es_null (lcb->local_dict))
lcb->local_dict = opt_dict_new (23);
opt_vm_dstack_push (optvm, lcb->local_dict);
opt_vm_set_app_data (optvm, lcb);
set_current_lcb (optvm, lcb);
scriptEvalHook (optvm, lcb, SCRIPT_HOOK_PRELUDE);
}

extern void notifyRegexInputEnd (struct lregexControlBlock *lcb)
{
scriptEvalHook (optvm, lcb, SCRIPT_HOOK_SEQUEL);
opt_vm_set_app_data (optvm, NULL);
opt_vm_clear (optvm);
set_current_lcb (optvm, NULL);
opt_vm_clear (optvm, false);
opt_dict_clear (lcb->local_dict);
unsigned long endline = getInputLineNumber ();
fillEndLineFieldOfUpperScopes (lcb, endline);
Expand Down Expand Up @@ -2601,6 +2632,9 @@
extern void freeRegexResources (void)
{
es_object_unref (lregex_dict);
void *d = opt_vm_set_app_data(optvm, NULL);
if (d)
eFree (d);
opt_vm_delete (optvm);
}

Expand Down Expand Up @@ -3219,14 +3253,16 @@

static void scriptSetup (OptVM *vm, struct lregexControlBlock *lcb, int corkIndex, scriptWindow *window)
{
lcb->window = window;
set_current_lcb (vm, lcb);
set_current_window (vm, window);
optscriptSetup (vm, lcb->local_dict, corkIndex);
}

static void scriptTeardown (OptVM *vm, struct lregexControlBlock *lcb)
{
optscriptTeardown (vm, lcb->local_dict);
lcb->window = NULL;
set_current_lcb (vm, NULL);
set_current_window (vm, NULL);
}

extern void addOptscriptToHook (struct lregexControlBlock *lcb, enum scriptHook hook, const char *code)
Expand Down Expand Up @@ -3283,8 +3319,8 @@
}
else
{
struct lregexControlBlock *lcb = opt_vm_get_app_data (vm);
if (lcb->window->patbuf->regptype != REG_PARSER_SINGLE_LINE)
scriptWindow *window = get_current_window(vm);
if (window->patbuf->regptype != REG_PARSER_SINGLE_LINE)
return OPT_ERR_TYPECHECK;
if (opt_vm_ostack_count (vm) < 4)
return OPT_ERR_UNDERFLOW;
Expand Down Expand Up @@ -3478,8 +3514,7 @@
if (g < 1)
return OPT_ERR_RANGECHECK;

struct lregexControlBlock *lcb = opt_vm_get_app_data (vm);
scriptWindow *window = lcb->window;
scriptWindow *window = get_current_window (vm);

matchLoc *mloc = make_mloc (window, g, start);
if (mloc == NULL)
Expand Down Expand Up @@ -3561,8 +3596,7 @@

static EsObject* lrop_get_match_string_common (OptVM *vm, int i, int npop)
{
struct lregexControlBlock *lcb = opt_vm_get_app_data (vm);
scriptWindow *window = lcb->window;
scriptWindow *window = get_current_window (vm);
const char *cstr = make_match_string (window, i);
if (!cstr)
{
Expand Down Expand Up @@ -3666,7 +3700,7 @@
if (n >= countEntryInCorkQueue())
return OPT_ERR_RANGECHECK;

struct lregexControlBlock *lcb = opt_vm_get_app_data (vm);
struct lregexControlBlock *lcb = get_current_lcb (vm);
lcb->currentScope = n0;

opt_vm_ostack_pop (vm);
Expand All @@ -3676,7 +3710,7 @@

static EsObject* lrop_pop_scope (OptVM *vm, EsObject *name)
{
struct lregexControlBlock *lcb = opt_vm_get_app_data (vm);
struct lregexControlBlock *lcb = get_current_lcb (vm);
if (lcb->currentScope != CORK_NIL)
{
tagEntryInfo *e = getEntryInCorkQueue (lcb->currentScope);
Expand All @@ -3688,14 +3722,14 @@

static EsObject* lrop_clear_scope (OptVM *vm, EsObject *name)
{
struct lregexControlBlock *lcb = opt_vm_get_app_data (vm);
struct lregexControlBlock *lcb = get_current_lcb (vm);

Check warning on line 3725 in main/lregex.c

View check run for this annotation

Codecov / codecov/patch

main/lregex.c#L3725

Added line #L3725 was not covered by tests
lcb->currentScope = CORK_NIL;
return es_false;
}

static EsObject* lrop_ref0_scope (OptVM *vm, EsObject *name)
{
struct lregexControlBlock *lcb = opt_vm_get_app_data (vm);
struct lregexControlBlock *lcb = get_current_lcb (vm);

if (lcb->currentScope == 0)
{
Expand All @@ -3722,7 +3756,7 @@

int n = es_integer_get(nobj);

struct lregexControlBlock *lcb = opt_vm_get_app_data (vm);
struct lregexControlBlock *lcb = get_current_lcb (vm);

Check warning on line 3759 in main/lregex.c

View check run for this annotation

Codecov / codecov/patch

main/lregex.c#L3759

Added line #L3759 was not covered by tests
int scope = lcb->currentScope;

while (n--)
Expand All @@ -3749,7 +3783,7 @@

static EsObject* lrop_get_scope_depth (OptVM *vm, EsObject *name)
{
struct lregexControlBlock *lcb = opt_vm_get_app_data (vm);
struct lregexControlBlock *lcb = get_current_lcb (vm);

Check warning on line 3786 in main/lregex.c

View check run for this annotation

Codecov / codecov/patch

main/lregex.c#L3786

Added line #L3786 was not covered by tests
int scope = lcb->currentScope;

while (scope != CORK_NIL)
Expand Down Expand Up @@ -3816,8 +3850,8 @@

static EsObject* lrop_tenter_common (OptVM *vm, EsObject *name, enum tableAction action)
{
struct lregexControlBlock *lcb = opt_vm_get_app_data (vm);
if (lcb->window->patbuf->regptype != REG_PARSER_MULTI_TABLE)
scriptWindow *window = get_current_window (vm);
if (window->patbuf->regptype != REG_PARSER_MULTI_TABLE)
{
error (WARNING, "Use table related operators only with mtable regular expression");
return OPTSCRIPT_ERR_NOTMTABLEPTRN;
Expand All @@ -3827,11 +3861,12 @@
if (es_object_get_type (table) != OPT_TYPE_NAME)
return OPT_ERR_TYPECHECK;

struct lregexControlBlock *lcb = get_current_lcb(vm);
struct regexTable *t = getRegexTableForOptscriptName (lcb, table);
if (t == NULL)
return OPTSCRIPT_ERR_UNKNOWNTABLE;

lcb->window->taction = (struct mTableActionSpec){
window->taction = (struct mTableActionSpec){
.action = action,
.table = t,
.continuation_table = NULL,
Expand All @@ -3848,8 +3883,8 @@

static EsObject* lrop_tenter_with_continuation (OptVM *vm, EsObject *name)
{
struct lregexControlBlock *lcb = opt_vm_get_app_data (vm);
if (lcb->window->patbuf->regptype != REG_PARSER_MULTI_TABLE)
scriptWindow *window = get_current_window (vm);
if (window->patbuf->regptype != REG_PARSER_MULTI_TABLE)
{
error (WARNING, "Use table related operators only with mtable regular expression");
return OPTSCRIPT_ERR_NOTMTABLEPTRN;
Expand All @@ -3863,14 +3898,15 @@
if (es_object_get_type (cont) != OPT_TYPE_NAME)
return OPT_ERR_TYPECHECK;

struct lregexControlBlock *lcb = get_current_lcb (vm);
struct regexTable *t = getRegexTableForOptscriptName (lcb, table);
if (t == NULL)
return OPTSCRIPT_ERR_UNKNOWNTABLE;
struct regexTable *c = getRegexTableForOptscriptName (lcb, cont);
if (c == NULL)
return OPTSCRIPT_ERR_UNKNOWNTABLE;

lcb->window->taction = (struct mTableActionSpec){
window->taction = (struct mTableActionSpec){
.action = TACTION_ENTER,
.table = t,
.continuation_table = c,
Expand All @@ -3883,14 +3919,14 @@

static EsObject* lrop_tleave (OptVM *vm, EsObject *name)
{
struct lregexControlBlock *lcb = opt_vm_get_app_data (vm);
if (lcb->window->patbuf->regptype != REG_PARSER_MULTI_TABLE)
scriptWindow *window = get_current_window (vm);
if (window->patbuf->regptype != REG_PARSER_MULTI_TABLE)
{
error (WARNING, "Use table related operators only with mtable regular expression");
return OPTSCRIPT_ERR_NOTMTABLEPTRN;
}

lcb->window->taction.action = TACTION_LEAVE;
window->taction.action = TACTION_LEAVE;
return es_false;
}

Expand All @@ -3906,14 +3942,14 @@

static EsObject* lrop_tquit (OptVM *vm, EsObject *name)
{
struct lregexControlBlock *lcb = opt_vm_get_app_data (vm);
if (lcb->window->patbuf->regptype != REG_PARSER_MULTI_TABLE)
scriptWindow *window = get_current_window (vm);
if (window->patbuf->regptype != REG_PARSER_MULTI_TABLE)
{
error (WARNING, "Use table related operators only with mtable regular expression");
return OPTSCRIPT_ERR_NOTMTABLEPTRN;
}

lcb->window->taction.action = TACTION_QUIT;
window->taction.action = TACTION_QUIT;
return es_false;
}

Expand Down Expand Up @@ -3997,8 +4033,8 @@

static EsObject *lrop_advanceto (OptVM *vm, EsObject *name)
{
struct lregexControlBlock *lcb = opt_vm_get_app_data (vm);
if (lcb->window->patbuf->regptype == REG_PARSER_SINGLE_LINE)
scriptWindow *window = get_current_window (vm);
if (window->patbuf->regptype == REG_PARSER_SINGLE_LINE)
{
error (WARNING, "don't use `%s' operator in --regex-<LANG> option",
es_symbol_get (name));
Expand All @@ -4010,8 +4046,8 @@
return OPT_ERR_TYPECHECK;

matchLoc *loc = es_pointer_get (mlocobj);
lcb->window->advanceto = true;
lcb->window->advanceto_delta = loc->delta;
window->advanceto = true;
window->advanceto_delta = loc->delta;

return es_true;
}
Expand Down Expand Up @@ -4043,8 +4079,8 @@

static EsObject *lrop_makepromise (OptVM *vm, EsObject *name)
{
struct lregexControlBlock *lcb = opt_vm_get_app_data (vm);
if (lcb->window->patbuf->regptype == REG_PARSER_SINGLE_LINE)
scriptWindow *window = get_current_window (vm);
if (window->patbuf->regptype == REG_PARSER_SINGLE_LINE)
{
error (WARNING, "don't use `%s' operator in --regex-<LANG> option",
es_symbol_get (name));
Expand Down Expand Up @@ -4103,7 +4139,7 @@

static EsObject *lrop_param (OptVM *vm, EsObject *name)
{
struct lregexControlBlock *lcb = opt_vm_get_app_data (vm);
struct lregexControlBlock *lcb = get_current_lcb (vm);
EsObject *key = opt_vm_ostack_top (vm);
if (es_object_get_type (key) != OPT_TYPE_NAME)
return OPT_ERR_TYPECHECK;
Expand Down Expand Up @@ -4315,6 +4351,8 @@
return;

optvm = optscriptInit ();
appData *d = xCalloc (1, appData);
opt_vm_set_app_data (optvm, d);
lregex_dict = opt_dict_new (17);

OPTSCRIPT_ERR_UNKNOWNTABLE = es_error_intern ("unknowntable");
Expand Down
Loading