diff src/vim9cmds.c @ 30083:a542dfb1c1a2 v9.0.0379

patch 9.0.0379: cleaning up after writefile() is a hassle Commit: https://github.com/vim/vim/commit/806a273f3c84ecd475913d901890bb1929be9a0a Author: Bram Moolenaar <Bram@vim.org> Date: Sun Sep 4 15:40:36 2022 +0100 patch 9.0.0379: cleaning up after writefile() is a hassle Problem: Cleaning up after writefile() is a hassle. Solution: Add the 'D' flag to defer deleting the written file. Very useful in tests.
author Bram Moolenaar <Bram@vim.org>
date Sun, 04 Sep 2022 17:00:03 +0200
parents 6cf788ab844c
children a5417ca098af
line wrap: on
line diff
--- a/src/vim9cmds.c
+++ b/src/vim9cmds.c
@@ -1685,6 +1685,27 @@ compile_eval(char_u *arg, cctx_T *cctx)
 }
 
 /*
+ * Get the local variable index for deferred function calls.
+ * Reserve it when not done already.
+ * Returns zero for failure.
+ */
+    int
+get_defer_var_idx(cctx_T *cctx)
+{
+    dfunc_T	*dfunc = ((dfunc_T *)def_functions.ga_data)
+					       + cctx->ctx_ufunc->uf_dfunc_idx;
+    if (dfunc->df_defer_var_idx == 0)
+    {
+	lvar_T *lvar = reserve_local(cctx, (char_u *)"@defer@", 7,
+							    TRUE, &t_list_any);
+	if (lvar == NULL)
+	    return 0;
+	dfunc->df_defer_var_idx = lvar->lv_idx + 1;
+    }
+    return dfunc->df_defer_var_idx;
+}
+
+/*
  * Compile "defer func(arg)".
  */
     char_u *
@@ -1693,7 +1714,7 @@ compile_defer(char_u *arg_start, cctx_T 
     char_u	*p;
     char_u	*arg = arg_start;
     int		argcount = 0;
-    dfunc_T	*dfunc;
+    int		defer_var_idx;
     type_T	*type;
     int		func_idx;
 
@@ -1730,16 +1751,10 @@ compile_defer(char_u *arg_start, cctx_T 
 
     // TODO: check argument count with "type"
 
-    dfunc = ((dfunc_T *)def_functions.ga_data) + cctx->ctx_ufunc->uf_dfunc_idx;
-    if (dfunc->df_defer_var_idx == 0)
-    {
-	lvar_T *lvar = reserve_local(cctx, (char_u *)"@defer@", 7,
-							    TRUE, &t_list_any);
-	if (lvar == NULL)
-	    return NULL;
-	dfunc->df_defer_var_idx = lvar->lv_idx + 1;
-    }
-    if (generate_DEFER(cctx, dfunc->df_defer_var_idx - 1, argcount) == FAIL)
+    defer_var_idx = get_defer_var_idx(cctx);
+    if (defer_var_idx == 0)
+	return NULL;
+    if (generate_DEFER(cctx, defer_var_idx - 1, argcount) == FAIL)
 	return NULL;
 
     return skipwhite(arg);