static int compile_expr1(char_u **arg, cctx_T *cctx);
static int compile_expr2(char_u **arg, cctx_T *cctx);
static int compile_expr3(char_u **arg, cctx_T *cctx);
+static void delete_def_function_contents(dfunc_T *dfunc);
/*
* Lookup variable "name" in the local scope and return the index.
return idx;
}
+/*
+ * Remove local variables above "new_top".
+ */
+ static void
+unwind_locals(cctx_T *cctx, int new_top)
+{
+ if (cctx->ctx_locals.ga_len > new_top)
+ {
+ int idx;
+ lvar_T *lvar;
+
+ for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
+ {
+ lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
+ vim_free(lvar->lv_name);
+ }
+ }
+ cctx->ctx_locals.ga_len = new_top;
+}
+
+/*
+ * Free all local variables.
+ */
+ static void
+free_local(cctx_T *cctx)
+{
+ unwind_locals(cctx, 0);
+ ga_clear(&cctx->ctx_locals);
+}
+
/*
* Skip over a type definition and return a pointer to just after it.
*/
return NULL;
}
+/*
+ * Free all imported variables.
+ */
+ static void
+free_imported(cctx_T *cctx)
+{
+ int idx;
+
+ for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
+ {
+ imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
+
+ vim_free(import->imp_name);
+ }
+ ga_clear(&cctx->ctx_imports);
+}
+
/*
* Generate an instruction to load script-local variable "name".
*/
// Get the funcref in "rettv".
if (get_lambda_tv(arg, &rettv, TRUE) == FAIL)
return FAIL;
+
ufunc = rettv.vval.v_partial->pt_func;
+ ++ufunc->uf_refcount;
+ clear_tv(&rettv);
// The function will have one line: "return {expr}".
// Compile it into instructions.
return FAIL;
}
- // The function will have one line: "return {expr}".
- // Compile it into instructions.
ufunc = rettv.vval.v_partial->pt_func;
++ufunc->uf_refcount;
+ clear_tv(&rettv);
+
+ // The function will have one line: "return {expr}".
+ // Compile it into instructions.
compile_def_function(ufunc, TRUE);
// compile the arguments
// call the compiled function
ret = generate_CALL(cctx, ufunc, argcount);
- clear_tv(&rettv);
return ret;
}
{
int cc;
long numval;
- char_u *stringval = NULL;
dest = dest_option;
if (cmdidx == CMD_const)
}
cc = *p;
*p = NUL;
- opt_type = get_option_value(arg + 1, &numval, &stringval, opt_flags);
+ opt_type = get_option_value(arg + 1, &numval, NULL, opt_flags);
*p = cc;
if (opt_type == -3)
{
emsg(_(e_elseif_without_if));
return NULL;
}
- cctx->ctx_locals.ga_len = scope->se_local_count;
+ unwind_locals(cctx, scope->se_local_count);
if (cctx->ctx_skip == MAYBE)
{
emsg(_(e_else_without_if));
return NULL;
}
- cctx->ctx_locals.ga_len = scope->se_local_count;
+ unwind_locals(cctx, scope->se_local_count);
// jump from previous block to the end, unless the else block is empty
if (cctx->ctx_skip == MAYBE)
}
ifscope = &scope->se_u.se_if;
cctx->ctx_scope = scope->se_outer;
- cctx->ctx_locals.ga_len = scope->se_local_count;
+ unwind_locals(cctx, scope->se_local_count);
if (scope->se_u.se_if.is_if_label >= 0)
{
}
forscope = &scope->se_u.se_for;
cctx->ctx_scope = scope->se_outer;
- cctx->ctx_locals.ga_len = scope->se_local_count;
+ unwind_locals(cctx, scope->se_local_count);
// At end of ":for" scope jump back to the FOR instruction.
generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
return NULL;
}
cctx->ctx_scope = scope->se_outer;
- cctx->ctx_locals.ga_len = scope->se_local_count;
+ unwind_locals(cctx, scope->se_local_count);
// At end of ":for" scope jump back to the FOR instruction.
generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
scope_T *scope = cctx->ctx_scope;
cctx->ctx_scope = scope->se_outer;
- cctx->ctx_locals.ga_len = scope->se_local_count;
+ unwind_locals(cctx, scope->se_local_count);
vim_free(scope);
}
if (ufunc->uf_dfunc_idx >= 0)
{
- // redefining a function that was compiled before
+ // Redefining a function that was compiled before.
dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
- dfunc->df_deleted = FALSE;
+
+ // Free old instructions.
+ delete_def_function_contents(dfunc);
}
else
{
generate_instr(&cctx, ISN_RETURN);
}
+ dfunc->df_deleted = FALSE;
dfunc->df_instr = instr->ga_data;
dfunc->df_instr_count = instr->ga_len;
dfunc->df_varcount = cctx.ctx_max_local;
erret:
if (ret == FAIL)
{
+ int idx;
+
+ for (idx = 0; idx < instr->ga_len; ++idx)
+ delete_instr(((isn_T *)instr->ga_data) + idx);
ga_clear(instr);
+
ufunc->uf_dfunc_idx = -1;
- --def_functions.ga_len;
+ if (!dfunc->df_deleted)
+ --def_functions.ga_len;
+
+ // Don't execute this function body.
+ ga_clear_strings(&ufunc->uf_lines);
+
if (errormsg != NULL)
emsg(errormsg);
else if (called_emsg == called_emsg_before)
emsg(_("E1028: compile_def_function failed"));
-
- // don't execute this function body
- ufunc->uf_lines.ga_len = 0;
}
current_sctx = save_current_sctx;
+ free_imported(&cctx);
+ free_local(&cctx);
ga_clear(&cctx.ctx_type_stack);
- ga_clear(&cctx.ctx_locals);
}
/*
* Delete an instruction, free what it contains.
*/
- static void
+ void
delete_instr(isn_T *isn)
{
switch (isn->isn_type)
}
}
+/*
+ * Free all instructions for "dfunc".
+ */
+ static void
+delete_def_function_contents(dfunc_T *dfunc)
+{
+ int idx;
+
+ ga_clear(&dfunc->df_def_args_isn);
+
+ if (dfunc->df_instr != NULL)
+ {
+ for (idx = 0; idx < dfunc->df_instr_count; ++idx)
+ delete_instr(dfunc->df_instr + idx);
+ VIM_CLEAR(dfunc->df_instr);
+ }
+
+ dfunc->df_deleted = TRUE;
+}
+
/*
* When a user function is deleted, delete any associated def function.
*/
void
delete_def_function(ufunc_T *ufunc)
{
- int idx;
-
if (ufunc->uf_dfunc_idx >= 0)
{
dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
+ ufunc->uf_dfunc_idx;
- ga_clear(&dfunc->df_def_args_isn);
- for (idx = 0; idx < dfunc->df_instr_count; ++idx)
- delete_instr(dfunc->df_instr + idx);
- VIM_CLEAR(dfunc->df_instr);
-
- dfunc->df_deleted = TRUE;
+ delete_def_function_contents(dfunc);
}
}
#if defined(EXITFREE) || defined(PROTO)
+/*
+ * Free all functions defined with ":def".
+ */
void
free_def_functions(void)
{
- vim_free(def_functions.ga_data);
+ int idx;
+
+ for (idx = 0; idx < def_functions.ga_len; ++idx)
+ {
+ dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
+
+ delete_def_function_contents(dfunc);
+ }
+
+ ga_clear(&def_functions);
}
#endif
// that was defined later: we can call it directly next time.
if (iptr != NULL)
{
+ delete_instr(iptr);
iptr->isn_type = ISN_DCALL;
iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
iptr->isn_arg.dfunc.cdf_argcount = argcount;
for (;;)
{
isn_T *iptr;
- trycmd_T *trycmd = NULL;
+
+ veryfast_breakcheck();
+ if (got_int)
+ {
+ // Turn CTRL-C into an exception.
+ got_int = FALSE;
+ if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) != FAIL)
+ goto failed;
+ did_throw = TRUE;
+ }
if (did_throw && !ectx.ec_in_catch)
{
garray_T *trystack = &ectx.ec_trystack;
+ trycmd_T *trycmd = NULL;
// An exception jumps to the first catch, finally, or returns from
// the current function.
// store $ENV
case ISN_STOREENV:
--ectx.ec_stack.ga_len;
- vim_setenv_ext(iptr->isn_arg.string,
- tv_get_string(STACK_TV_BOT(0)));
+ tv = STACK_TV_BOT(0);
+ vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
+ clear_tv(tv);
break;
// store @r
case ISN_RETURN:
{
garray_T *trystack = &ectx.ec_trystack;
+ trycmd_T *trycmd = NULL;
if (trystack->ga_len > 0)
trycmd = ((trycmd_T *)trystack->ga_data)
// start of ":try" block
case ISN_TRY:
{
+ trycmd_T *trycmd = NULL;
+
if (ga_grow(&ectx.ec_trystack, 1) == FAIL)
goto failed;
trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
if (trystack->ga_len > 0)
{
- trycmd = ((trycmd_T *)trystack->ga_data)
+ trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
+ trystack->ga_len - 1;
trycmd->tcd_caught = TRUE;
}
if (trystack->ga_len > 0)
{
+ trycmd_T *trycmd = NULL;
+
--trystack->ga_len;
--trylevel;
trycmd = ((trycmd_T *)trystack->ga_data)
for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
clear_tv(STACK_TV(idx));
vim_free(ectx.ec_stack.ga_data);
+ vim_free(ectx.ec_trystack.ga_data);
return ret;
}