comparison src/userfunc.c @ 9723:80ac9cf77c9b v7.4.2137

commit https://github.com/vim/vim/commit/437bafe4c8a83ed71ee006eda7f54b65a90f0d4c Author: Bram Moolenaar <Bram@vim.org> Date: Mon Aug 1 15:40:54 2016 +0200 patch 7.4.2137 Problem: Using function() with a name will find another function when it is redefined. Solution: Add funcref(). Refer to lambda using a partial. Fix several reference counting issues.
author Christian Brabandt <cb@256bit.org>
date Mon, 01 Aug 2016 15:45:07 +0200
parents 79862f44c647
children 8436bb5134f5
comparison
equal deleted inserted replaced
9722:1557241fd3a7 9723:80ac9cf77c9b
12 */ 12 */
13 13
14 #include "vim.h" 14 #include "vim.h"
15 15
16 #if defined(FEAT_EVAL) || defined(PROTO) 16 #if defined(FEAT_EVAL) || defined(PROTO)
17
18 typedef struct funccall_S funccall_T;
19
20 /*
21 * Structure to hold info for a user function.
22 */
23 typedef struct ufunc ufunc_T;
24
25 struct ufunc
26 {
27 int uf_varargs; /* variable nr of arguments */
28 int uf_flags;
29 int uf_calls; /* nr of active calls */
30 garray_T uf_args; /* arguments */
31 garray_T uf_lines; /* function lines */
32 #ifdef FEAT_PROFILE
33 int uf_profiling; /* TRUE when func is being profiled */
34 /* profiling the function as a whole */
35 int uf_tm_count; /* nr of calls */
36 proftime_T uf_tm_total; /* time spent in function + children */
37 proftime_T uf_tm_self; /* time spent in function itself */
38 proftime_T uf_tm_children; /* time spent in children this call */
39 /* profiling the function per line */
40 int *uf_tml_count; /* nr of times line was executed */
41 proftime_T *uf_tml_total; /* time spent in a line + children */
42 proftime_T *uf_tml_self; /* time spent in a line itself */
43 proftime_T uf_tml_start; /* start time for current line */
44 proftime_T uf_tml_children; /* time spent in children for this line */
45 proftime_T uf_tml_wait; /* start wait time for current line */
46 int uf_tml_idx; /* index of line being timed; -1 if none */
47 int uf_tml_execed; /* line being timed was executed */
48 #endif
49 scid_T uf_script_ID; /* ID of script where function was defined,
50 used for s: variables */
51 int uf_refcount; /* for numbered function: reference count */
52 funccall_T *uf_scoped; /* l: local variables for closure */
53 char_u uf_name[1]; /* name of function (actually longer); can
54 start with <SNR>123_ (<SNR> is K_SPECIAL
55 KS_EXTRA KE_SNR) */
56 };
57
58 /* function flags */ 17 /* function flags */
59 #define FC_ABORT 1 /* abort function on error */ 18 #define FC_ABORT 1 /* abort function on error */
60 #define FC_RANGE 2 /* function accepts range */ 19 #define FC_RANGE 2 /* function accepts range */
61 #define FC_DICT 4 /* Dict function, uses "self" */ 20 #define FC_DICT 4 /* Dict function, uses "self" */
62 #define FC_CLOSURE 8 /* closure, uses outer scope variables */ 21 #define FC_CLOSURE 8 /* closure, uses outer scope variables */
22 #define FC_DELETED 16 /* :delfunction used while uf_refcount > 0 */
63 23
64 /* From user function to hashitem and back. */ 24 /* From user function to hashitem and back. */
65 #define UF2HIKEY(fp) ((fp)->uf_name) 25 #define UF2HIKEY(fp) ((fp)->uf_name)
66 #define HIKEY2UF(p) ((ufunc_T *)(p - offsetof(ufunc_T, uf_name))) 26 #define HIKEY2UF(p) ((ufunc_T *)(p - offsetof(ufunc_T, uf_name)))
67 #define HI2UF(hi) HIKEY2UF((hi)->hi_key) 27 #define HI2UF(hi) HIKEY2UF((hi)->hi_key)
68 28
69 #define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j] 29 #define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
70 #define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j] 30 #define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
71
72 #define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
73 #define VAR_SHORT_LEN 20 /* short variable name length */
74 #define FIXVAR_CNT 12 /* number of fixed variables */
75
76 /* structure to hold info for a function that is currently being executed. */
77 struct funccall_S
78 {
79 ufunc_T *func; /* function being called */
80 int linenr; /* next line to be executed */
81 int returned; /* ":return" used */
82 struct /* fixed variables for arguments */
83 {
84 dictitem_T var; /* variable (without room for name) */
85 char_u room[VAR_SHORT_LEN]; /* room for the name */
86 } fixvar[FIXVAR_CNT];
87 dict_T l_vars; /* l: local function variables */
88 dictitem_T l_vars_var; /* variable for l: scope */
89 dict_T l_avars; /* a: argument variables */
90 dictitem_T l_avars_var; /* variable for a: scope */
91 list_T l_varlist; /* list for a:000 */
92 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
93 typval_T *rettv; /* return value */
94 linenr_T breakpoint; /* next line with breakpoint or zero */
95 int dbg_tick; /* debug_tick when breakpoint was set */
96 int level; /* top nesting level of executed function */
97 #ifdef FEAT_PROFILE
98 proftime_T prof_child; /* time spent in a child */
99 #endif
100 funccall_T *caller; /* calling function or NULL */
101
102 /* for closure */
103 int fc_refcount;
104 int fc_copyID; /* for garbage collection */
105 garray_T fc_funcs; /* list of ufunc_T* which refer this */
106 };
107
108 /*
109 * Struct used by trans_function_name()
110 */
111 typedef struct
112 {
113 dict_T *fd_dict; /* Dictionary used */
114 char_u *fd_newkey; /* new key in "dict" in allocated memory */
115 dictitem_T *fd_di; /* Dictionary item used */
116 } funcdict_T;
117 31
118 /* 32 /*
119 * All user-defined functions are found in this hashtable. 33 * All user-defined functions are found in this hashtable.
120 */ 34 */
121 static hashtab_T func_hashtab; 35 static hashtab_T func_hashtab;
269 current_funccal->fc_refcount++; 183 current_funccal->fc_refcount++;
270 if (ga_grow(&current_funccal->fc_funcs, 1) == FAIL) 184 if (ga_grow(&current_funccal->fc_funcs, 1) == FAIL)
271 return FAIL; 185 return FAIL;
272 ((ufunc_T **)current_funccal->fc_funcs.ga_data) 186 ((ufunc_T **)current_funccal->fc_funcs.ga_data)
273 [current_funccal->fc_funcs.ga_len++] = fp; 187 [current_funccal->fc_funcs.ga_len++] = fp;
274 func_ref(current_funccal->func->uf_name); 188 func_ptr_ref(current_funccal->func);
275 return OK; 189 return OK;
276 } 190 }
277 191
278 /* 192 /*
279 * Parse a lambda expression and get a Funcref from "*arg". 193 * Parse a lambda expression and get a Funcref from "*arg".
286 garray_T newlines; 200 garray_T newlines;
287 garray_T *pnewargs; 201 garray_T *pnewargs;
288 ufunc_T *fp = NULL; 202 ufunc_T *fp = NULL;
289 int varargs; 203 int varargs;
290 int ret; 204 int ret;
291 char_u name[20];
292 char_u *start = skipwhite(*arg + 1); 205 char_u *start = skipwhite(*arg + 1);
293 char_u *s, *e; 206 char_u *s, *e;
294 static int lambda_no = 0; 207 static int lambda_no = 0;
295 int *old_eval_lavars = eval_lavars_used; 208 int *old_eval_lavars = eval_lavars_used;
296 int eval_lavars = FALSE; 209 int eval_lavars = FALSE;
329 goto errret; 242 goto errret;
330 ++*arg; 243 ++*arg;
331 244
332 if (evaluate) 245 if (evaluate)
333 { 246 {
334 int len, flags = 0; 247 int len, flags = 0;
335 char_u *p; 248 char_u *p;
249 char_u name[20];
250 partial_T *pt;
336 251
337 sprintf((char*)name, "<lambda>%d", ++lambda_no); 252 sprintf((char*)name, "<lambda>%d", ++lambda_no);
338 253
339 fp = (ufunc_T *)alloc_clear((unsigned)(sizeof(ufunc_T) + STRLEN(name))); 254 fp = (ufunc_T *)alloc_clear((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
340 if (fp == NULL) 255 if (fp == NULL)
341 goto errret; 256 goto errret;
257 pt = (partial_T *)alloc_clear((unsigned)sizeof(partial_T));
258 if (pt == NULL)
259 {
260 vim_free(fp);
261 goto errret;
262 }
342 263
343 ga_init2(&newlines, (int)sizeof(char_u *), 1); 264 ga_init2(&newlines, (int)sizeof(char_u *), 1);
344 if (ga_grow(&newlines, 1) == FAIL) 265 if (ga_grow(&newlines, 1) == FAIL)
345 goto errret; 266 goto errret;
346 267
378 fp->uf_varargs = TRUE; 299 fp->uf_varargs = TRUE;
379 fp->uf_flags = flags; 300 fp->uf_flags = flags;
380 fp->uf_calls = 0; 301 fp->uf_calls = 0;
381 fp->uf_script_ID = current_SID; 302 fp->uf_script_ID = current_SID;
382 303
383 rettv->vval.v_string = vim_strsave(name); 304 pt->pt_func = fp;
384 rettv->v_type = VAR_FUNC; 305 pt->pt_refcount = 1;
306 rettv->vval.v_partial = pt;
307 rettv->v_type = VAR_PARTIAL;
385 } 308 }
386 309
387 eval_lavars_used = old_eval_lavars; 310 eval_lavars_used = old_eval_lavars;
388 return OK; 311 return OK;
389 312
404 char_u * 327 char_u *
405 deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload) 328 deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
406 { 329 {
407 dictitem_T *v; 330 dictitem_T *v;
408 int cc; 331 int cc;
332 char_u *s;
409 333
410 if (partialp != NULL) 334 if (partialp != NULL)
411 *partialp = NULL; 335 *partialp = NULL;
412 336
413 cc = name[*lenp]; 337 cc = name[*lenp];
419 if (v->di_tv.vval.v_string == NULL) 343 if (v->di_tv.vval.v_string == NULL)
420 { 344 {
421 *lenp = 0; 345 *lenp = 0;
422 return (char_u *)""; /* just in case */ 346 return (char_u *)""; /* just in case */
423 } 347 }
424 *lenp = (int)STRLEN(v->di_tv.vval.v_string); 348 s = v->di_tv.vval.v_string;
425 return v->di_tv.vval.v_string; 349 *lenp = (int)STRLEN(s);
350 return s;
426 } 351 }
427 352
428 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL) 353 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
429 { 354 {
430 partial_T *pt = v->di_tv.vval.v_partial; 355 partial_T *pt = v->di_tv.vval.v_partial;
434 *lenp = 0; 359 *lenp = 0;
435 return (char_u *)""; /* just in case */ 360 return (char_u *)""; /* just in case */
436 } 361 }
437 if (partialp != NULL) 362 if (partialp != NULL)
438 *partialp = pt; 363 *partialp = pt;
439 *lenp = (int)STRLEN(pt->pt_name); 364 s = partial_name(pt);
440 return pt->pt_name; 365 *lenp = (int)STRLEN(s);
366 return s;
441 } 367 }
442 368
443 return name; 369 return name;
444 } 370 }
445 371
609 535
610 /* 536 /*
611 * Find a function by name, return pointer to it in ufuncs. 537 * Find a function by name, return pointer to it in ufuncs.
612 * Return NULL for unknown function. 538 * Return NULL for unknown function.
613 */ 539 */
614 static ufunc_T * 540 ufunc_T *
615 find_func(char_u *name) 541 find_func(char_u *name)
616 { 542 {
617 hashitem_T *hi; 543 hashitem_T *hi;
618 544
619 hi = hash_find(&func_hashtab, name); 545 hi = hash_find(&func_hashtab, name);
676 { 602 {
677 /* Function may have been redefined and point to another 603 /* Function may have been redefined and point to another
678 * funccall_T, don't clear it then. */ 604 * funccall_T, don't clear it then. */
679 if (fp->uf_scoped == fc) 605 if (fp->uf_scoped == fc)
680 fp->uf_scoped = NULL; 606 fp->uf_scoped = NULL;
681 func_unref(fc->func->uf_name); 607 func_ptr_unref(fc->func);
682 } 608 }
683 } 609 }
684 ga_clear(&fc->fc_funcs); 610 ga_clear(&fc->fc_funcs);
685 611
686 /* The a: variables typevals may not have been allocated, only free the 612 /* The a: variables typevals may not have been allocated, only free the
693 /* Free the a:000 variables if they were allocated. */ 619 /* Free the a:000 variables if they were allocated. */
694 if (free_val) 620 if (free_val)
695 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next) 621 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
696 clear_tv(&li->li_tv); 622 clear_tv(&li->li_tv);
697 623
698 func_unref(fc->func->uf_name); 624 func_ptr_unref(fc->func);
699 vim_free(fc); 625 vim_free(fc);
700 } 626 }
701 627
702 /* 628 /*
703 * Call a user function. 629 * Call a user function.
757 fc->dbg_tick = debug_tick; 683 fc->dbg_tick = debug_tick;
758 /* Set up fields for closure. */ 684 /* Set up fields for closure. */
759 fc->fc_refcount = 0; 685 fc->fc_refcount = 0;
760 fc->fc_copyID = 0; 686 fc->fc_copyID = 0;
761 ga_init2(&fc->fc_funcs, sizeof(ufunc_T *), 1); 687 ga_init2(&fc->fc_funcs, sizeof(ufunc_T *), 1);
762 func_ref(fp->uf_name); 688 func_ptr_ref(fp);
763 689
764 if (STRNCMP(fp->uf_name, "<lambda>", 8) == 0) 690 if (STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
765 islambda = TRUE; 691 islambda = TRUE;
766 692
767 /* 693 /*
1110 if (fc == NULL) 1036 if (fc == NULL)
1111 return; 1037 return;
1112 1038
1113 if (--fc->fc_refcount <= 0) 1039 if (--fc->fc_refcount <= 0)
1114 { 1040 {
1115 for (pfc = &previous_funccal; *pfc != NULL; ) 1041 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->caller)
1116 { 1042 {
1117 if (fc == *pfc 1043 if (fc == *pfc)
1118 && fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT 1044 {
1045 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
1119 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT 1046 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
1120 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT) 1047 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
1121 { 1048 {
1122 *pfc = fc->caller; 1049 *pfc = fc->caller;
1123 free_funccal(fc, TRUE); 1050 free_funccal(fc, TRUE);
1124 freed = TRUE; 1051 freed = TRUE;
1052 }
1053 break;
1125 } 1054 }
1126 else
1127 pfc = &(*pfc)->caller;
1128 } 1055 }
1129 } 1056 }
1130 if (!freed) 1057 if (!freed)
1131 { 1058 {
1132 func_unref(fc->func->uf_name); 1059 func_ptr_unref(fc->func);
1133 1060
1134 if (fp != NULL) 1061 if (fp != NULL)
1135 for (i = 0; i < fc->fc_funcs.ga_len; ++i) 1062 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
1136 { 1063 {
1137 if (((ufunc_T **)(fc->fc_funcs.ga_data))[i] == fp) 1064 if (((ufunc_T **)(fc->fc_funcs.ga_data))[i] == fp)
1139 } 1066 }
1140 } 1067 }
1141 } 1068 }
1142 1069
1143 /* 1070 /*
1071 * Remove the function from the function hashtable. If the function was
1072 * deleted while it still has references this was already done.
1073 */
1074 static void
1075 func_remove(ufunc_T *fp)
1076 {
1077 hashitem_T *hi = hash_find(&func_hashtab, UF2HIKEY(fp));
1078
1079 if (!HASHITEM_EMPTY(hi))
1080 hash_remove(&func_hashtab, hi);
1081 }
1082
1083 /*
1144 * Free a function and remove it from the list of functions. 1084 * Free a function and remove it from the list of functions.
1145 */ 1085 */
1146 static void 1086 static void
1147 func_free(ufunc_T *fp) 1087 func_free(ufunc_T *fp)
1148 { 1088 {
1149 hashitem_T *hi;
1150
1151 /* clear this function */ 1089 /* clear this function */
1152 ga_clear_strings(&(fp->uf_args)); 1090 ga_clear_strings(&(fp->uf_args));
1153 ga_clear_strings(&(fp->uf_lines)); 1091 ga_clear_strings(&(fp->uf_lines));
1154 #ifdef FEAT_PROFILE 1092 #ifdef FEAT_PROFILE
1155 vim_free(fp->uf_tml_count); 1093 vim_free(fp->uf_tml_count);
1156 vim_free(fp->uf_tml_total); 1094 vim_free(fp->uf_tml_total);
1157 vim_free(fp->uf_tml_self); 1095 vim_free(fp->uf_tml_self);
1158 #endif 1096 #endif
1159 1097 func_remove(fp);
1160 /* remove the function from the function hashtable */
1161 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
1162 if (HASHITEM_EMPTY(hi))
1163 EMSG2(_(e_intern2), "func_free()");
1164 else
1165 hash_remove(&func_hashtab, hi);
1166 1098
1167 funccal_unref(fp->uf_scoped, fp); 1099 funccal_unref(fp->uf_scoped, fp);
1168 1100
1169 vim_free(fp); 1101 vim_free(fp);
1170 } 1102 }
1331 if (!builtin_function(rfname, -1)) 1263 if (!builtin_function(rfname, -1))
1332 { 1264 {
1333 /* 1265 /*
1334 * User defined function. 1266 * User defined function.
1335 */ 1267 */
1336 fp = find_func(rfname); 1268 if (partial != NULL && partial->pt_func != NULL)
1269 fp = partial->pt_func;
1270 else
1271 fp = find_func(rfname);
1337 1272
1338 #ifdef FEAT_AUTOCMD 1273 #ifdef FEAT_AUTOCMD
1339 /* Trigger FuncUndefined event, may load the function. */ 1274 /* Trigger FuncUndefined event, may load the function. */
1340 if (fp == NULL 1275 if (fp == NULL
1341 && apply_autocmds(EVENT_FUNCUNDEFINED, 1276 && apply_autocmds(EVENT_FUNCUNDEFINED,
1351 { 1286 {
1352 /* loaded a package, search for the function again */ 1287 /* loaded a package, search for the function again */
1353 fp = find_func(rfname); 1288 fp = find_func(rfname);
1354 } 1289 }
1355 1290
1356 if (fp != NULL) 1291 if (fp != NULL && (fp->uf_flags & FC_DELETED))
1292 error = ERROR_DELETED;
1293 else if (fp != NULL)
1357 { 1294 {
1358 if (argv_func != NULL) 1295 if (argv_func != NULL)
1359 argcount = argv_func(argcount, argvars, fp->uf_args.ga_len); 1296 argcount = argv_func(argcount, argvars, fp->uf_args.ga_len);
1360 1297
1361 if (fp->uf_flags & FC_RANGE) 1298 if (fp->uf_flags & FC_RANGE)
1385 } 1322 }
1386 ++fp->uf_calls; 1323 ++fp->uf_calls;
1387 call_user_func(fp, argcount, argvars, rettv, 1324 call_user_func(fp, argcount, argvars, rettv,
1388 firstline, lastline, 1325 firstline, lastline,
1389 (fp->uf_flags & FC_DICT) ? selfdict : NULL); 1326 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
1390 if (--fp->uf_calls <= 0 && (isdigit(*fp->uf_name) 1327 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
1391 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
1392 && fp->uf_refcount <= 0)
1393 /* Function was unreferenced while being used, free it 1328 /* Function was unreferenced while being used, free it
1394 * now. */ 1329 * now. */
1395 func_free(fp); 1330 func_free(fp);
1396 if (did_save_redo) 1331 if (did_save_redo)
1397 restoreRedobuff(); 1332 restoreRedobuff();
1430 { 1365 {
1431 switch (error) 1366 switch (error)
1432 { 1367 {
1433 case ERROR_UNKNOWN: 1368 case ERROR_UNKNOWN:
1434 emsg_funcname(N_("E117: Unknown function: %s"), name); 1369 emsg_funcname(N_("E117: Unknown function: %s"), name);
1370 break;
1371 case ERROR_DELETED:
1372 emsg_funcname(N_("E933: Function was deleted: %s"), name);
1435 break; 1373 break;
1436 case ERROR_TOOMANY: 1374 case ERROR_TOOMANY:
1437 emsg_funcname((char *)e_toomanyarg, name); 1375 emsg_funcname((char *)e_toomanyarg, name);
1438 break; 1376 break;
1439 case ERROR_TOOFEW: 1377 case ERROR_TOOFEW:
1514 * TFN_QUIET: be quiet 1452 * TFN_QUIET: be quiet
1515 * TFN_NO_AUTOLOAD: do not use script autoloading 1453 * TFN_NO_AUTOLOAD: do not use script autoloading
1516 * TFN_NO_DEREF: do not dereference a Funcref 1454 * TFN_NO_DEREF: do not dereference a Funcref
1517 * Advances "pp" to just after the function name (if no error). 1455 * Advances "pp" to just after the function name (if no error).
1518 */ 1456 */
1519 static char_u * 1457 char_u *
1520 trans_function_name( 1458 trans_function_name(
1521 char_u **pp, 1459 char_u **pp,
1522 int skip, /* only find the end, don't evaluate */ 1460 int skip, /* only find the end, don't evaluate */
1523 int flags, 1461 int flags,
1524 funcdict_T *fdp, /* return: info about dictionary used */ 1462 funcdict_T *fdp, /* return: info about dictionary used */
1593 *pp = end; 1531 *pp = end;
1594 } 1532 }
1595 else if (lv.ll_tv->v_type == VAR_PARTIAL 1533 else if (lv.ll_tv->v_type == VAR_PARTIAL
1596 && lv.ll_tv->vval.v_partial != NULL) 1534 && lv.ll_tv->vval.v_partial != NULL)
1597 { 1535 {
1598 name = vim_strsave(lv.ll_tv->vval.v_partial->pt_name); 1536 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
1599 *pp = end; 1537 *pp = end;
1600 if (partial != NULL) 1538 if (partial != NULL)
1601 *partial = lv.ll_tv->vval.v_partial; 1539 *partial = lv.ll_tv->vval.v_partial;
1602 } 1540 }
1603 else 1541 else
1750 garray_T newargs; 1688 garray_T newargs;
1751 garray_T newlines; 1689 garray_T newlines;
1752 int varargs = FALSE; 1690 int varargs = FALSE;
1753 int flags = 0; 1691 int flags = 0;
1754 ufunc_T *fp; 1692 ufunc_T *fp;
1693 int overwrite = FALSE;
1755 int indent; 1694 int indent;
1756 int nesting; 1695 int nesting;
1757 char_u *skip_until = NULL; 1696 char_u *skip_until = NULL;
1758 dictitem_T *v; 1697 dictitem_T *v;
1759 funcdict_T fudi; 1698 funcdict_T fudi;
2212 { 2151 {
2213 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"), 2152 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
2214 name); 2153 name);
2215 goto erret; 2154 goto erret;
2216 } 2155 }
2217 /* redefine existing function */ 2156 if (fp->uf_refcount > 1)
2218 ga_clear_strings(&(fp->uf_args)); 2157 {
2219 ga_clear_strings(&(fp->uf_lines)); 2158 /* This function is referenced somewhere, don't redefine it but
2220 vim_free(name); 2159 * create a new one. */
2221 name = NULL; 2160 --fp->uf_refcount;
2161 fp = NULL;
2162 overwrite = TRUE;
2163 }
2164 else
2165 {
2166 /* redefine existing function */
2167 ga_clear_strings(&(fp->uf_args));
2168 ga_clear_strings(&(fp->uf_lines));
2169 vim_free(name);
2170 name = NULL;
2171 }
2222 } 2172 }
2223 } 2173 }
2224 else 2174 else
2225 { 2175 {
2226 char numbuf[20]; 2176 char numbuf[20];
2306 /* overwrite existing dict entry */ 2256 /* overwrite existing dict entry */
2307 clear_tv(&fudi.fd_di->di_tv); 2257 clear_tv(&fudi.fd_di->di_tv);
2308 fudi.fd_di->di_tv.v_type = VAR_FUNC; 2258 fudi.fd_di->di_tv.v_type = VAR_FUNC;
2309 fudi.fd_di->di_tv.v_lock = 0; 2259 fudi.fd_di->di_tv.v_lock = 0;
2310 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name); 2260 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
2311 fp->uf_refcount = 1;
2312 2261
2313 /* behave like "dict" was used */ 2262 /* behave like "dict" was used */
2314 flags |= FC_DICT; 2263 flags |= FC_DICT;
2315 } 2264 }
2316 2265
2317 /* insert the new function in the function list */ 2266 /* insert the new function in the function list */
2318 STRCPY(fp->uf_name, name); 2267 STRCPY(fp->uf_name, name);
2319 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL) 2268 if (overwrite)
2269 {
2270 hi = hash_find(&func_hashtab, name);
2271 hi->hi_key = UF2HIKEY(fp);
2272 }
2273 else if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
2320 { 2274 {
2321 vim_free(fp); 2275 vim_free(fp);
2322 goto erret; 2276 goto erret;
2323 } 2277 }
2278 fp->uf_refcount = 1;
2324 } 2279 }
2325 fp->uf_args = newargs; 2280 fp->uf_args = newargs;
2326 fp->uf_lines = newlines; 2281 fp->uf_lines = newlines;
2327 if ((flags & FC_CLOSURE) != 0) 2282 if ((flags & FC_CLOSURE) != 0)
2328 { 2283 {
2329 ++fp->uf_refcount;
2330 if (register_closure(fp) == FAIL) 2284 if (register_closure(fp) == FAIL)
2331 goto erret; 2285 goto erret;
2332 } 2286 }
2333 else 2287 else
2334 fp->uf_scoped = NULL; 2288 fp->uf_scoped = NULL;
2748 /* Delete the dict item that refers to the function, it will 2702 /* Delete the dict item that refers to the function, it will
2749 * invoke func_unref() and possibly delete the function. */ 2703 * invoke func_unref() and possibly delete the function. */
2750 dictitem_remove(fudi.fd_dict, fudi.fd_di); 2704 dictitem_remove(fudi.fd_dict, fudi.fd_di);
2751 } 2705 }
2752 else 2706 else
2753 func_free(fp); 2707 {
2708 /* Normal functions (not numbered functions and lambdas) have a
2709 * refcount of 1 for the entry in the hashtable. When deleting
2710 * them and the refcount is more than one, it should be kept.
2711 * Numbered functions and lambdas snould be kept if the refcount is
2712 * one or more. */
2713 if (fp->uf_refcount > (isdigit(fp->uf_name[0])
2714 || fp->uf_name[0] == '<') ? 0 : 1)
2715 {
2716 /* Function is still referenced somewhere. Don't free it but
2717 * do remove it from the hashtable. */
2718 func_remove(fp);
2719 fp->uf_flags |= FC_DELETED;
2720 fp->uf_refcount--;
2721 }
2722 else
2723 func_free(fp);
2724 }
2754 } 2725 }
2755 } 2726 }
2756 2727
2757 /* 2728 /*
2758 * Unreference a Function: decrement the reference count and free it when it 2729 * Unreference a Function: decrement the reference count and free it when it
2759 * becomes zero. Only for numbered functions. 2730 * becomes zero.
2760 */ 2731 */
2761 void 2732 void
2762 func_unref(char_u *name) 2733 func_unref(char_u *name)
2763 { 2734 {
2764 ufunc_T *fp = NULL; 2735 ufunc_T *fp = NULL;
2765 2736
2766 if (name == NULL) 2737 if (name == NULL)
2767 return; 2738 return;
2768 if (isdigit(*name)) 2739 fp = find_func(name);
2769 { 2740 if (fp == NULL && isdigit(*name))
2770 fp = find_func(name); 2741 {
2771 if (fp == NULL)
2772 {
2773 #ifdef EXITFREE 2742 #ifdef EXITFREE
2774 if (!entered_free_all_mem) 2743 if (!entered_free_all_mem)
2775 #endif 2744 #endif
2776 EMSG2(_(e_intern2), "func_unref()"); 2745 EMSG2(_(e_intern2), "func_unref()");
2777 }
2778 }
2779 else if (STRNCMP(name, "<lambda>", 8) == 0)
2780 {
2781 /* fail silently, when lambda function isn't found. */
2782 fp = find_func(name);
2783 } 2746 }
2784 if (fp != NULL && --fp->uf_refcount <= 0) 2747 if (fp != NULL && --fp->uf_refcount <= 0)
2785 { 2748 {
2786 /* Only delete it when it's not being used. Otherwise it's done 2749 /* Only delete it when it's not being used. Otherwise it's done
2787 * when "uf_calls" becomes zero. */ 2750 * when "uf_calls" becomes zero. */
2789 func_free(fp); 2752 func_free(fp);
2790 } 2753 }
2791 } 2754 }
2792 2755
2793 /* 2756 /*
2757 * Unreference a Function: decrement the reference count and free it when it
2758 * becomes zero.
2759 */
2760 void
2761 func_ptr_unref(ufunc_T *fp)
2762 {
2763 if (fp != NULL && --fp->uf_refcount <= 0)
2764 {
2765 /* Only delete it when it's not being used. Otherwise it's done
2766 * when "uf_calls" becomes zero. */
2767 if (fp->uf_calls == 0)
2768 func_free(fp);
2769 }
2770 }
2771
2772 /*
2794 * Count a reference to a Function. 2773 * Count a reference to a Function.
2795 */ 2774 */
2796 void 2775 void
2797 func_ref(char_u *name) 2776 func_ref(char_u *name)
2798 { 2777 {
2799 ufunc_T *fp; 2778 ufunc_T *fp;
2800 2779
2801 if (name == NULL) 2780 if (name == NULL)
2802 return; 2781 return;
2782 fp = find_func(name);
2783 if (fp != NULL)
2784 ++fp->uf_refcount;
2803 else if (isdigit(*name)) 2785 else if (isdigit(*name))
2804 { 2786 /* Only give an error for a numbered function.
2805 fp = find_func(name); 2787 * Fail silently, when named or lambda function isn't found. */
2806 if (fp == NULL) 2788 EMSG2(_(e_intern2), "func_ref()");
2807 EMSG2(_(e_intern2), "func_ref()"); 2789 }
2808 else 2790
2809 ++fp->uf_refcount; 2791 /*
2810 } 2792 * Count a reference to a Function.
2811 else if (STRNCMP(name, "<lambda>", 8) == 0) 2793 */
2812 { 2794 void
2813 /* fail silently, when lambda function isn't found. */ 2795 func_ptr_ref(ufunc_T *fp)
2814 fp = find_func(name); 2796 {
2815 if (fp != NULL) 2797 if (fp != NULL)
2816 ++fp->uf_refcount; 2798 ++fp->uf_refcount;
2817 }
2818 } 2799 }
2819 2800
2820 /* 2801 /*
2821 * Return TRUE if items in "fc" do not have "copyID". That means they are not 2802 * Return TRUE if items in "fc" do not have "copyID". That means they are not
2822 * referenced from anywhere that is in use. 2803 * referenced from anywhere that is in use.
3296 * Returns the updated "selfdict_in". 3277 * Returns the updated "selfdict_in".
3297 */ 3278 */
3298 dict_T * 3279 dict_T *
3299 make_partial(dict_T *selfdict_in, typval_T *rettv) 3280 make_partial(dict_T *selfdict_in, typval_T *rettv)
3300 { 3281 {
3301 char_u *fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string 3282 char_u *fname;
3302 : rettv->vval.v_partial->pt_name;
3303 char_u *tofree = NULL; 3283 char_u *tofree = NULL;
3304 ufunc_T *fp; 3284 ufunc_T *fp;
3305 char_u fname_buf[FLEN_FIXED + 1]; 3285 char_u fname_buf[FLEN_FIXED + 1];
3306 int error; 3286 int error;
3307 dict_T *selfdict = selfdict_in; 3287 dict_T *selfdict = selfdict_in;
3308 3288
3309 /* Translate "s:func" to the stored function name. */ 3289 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial->pt_func != NULL)
3310 fname = fname_trans_sid(fname, fname_buf, &tofree, &error); 3290 fp = rettv->vval.v_partial->pt_func;
3311 fp = find_func(fname); 3291 else
3312 vim_free(tofree); 3292 {
3293 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
3294 : rettv->vval.v_partial->pt_name;
3295 /* Translate "s:func" to the stored function name. */
3296 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
3297 fp = find_func(fname);
3298 vim_free(tofree);
3299 }
3313 3300
3314 if (fp != NULL && (fp->uf_flags & FC_DICT)) 3301 if (fp != NULL && (fp->uf_flags & FC_DICT))
3315 { 3302 {
3316 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T)); 3303 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
3317 3304
3333 int i; 3320 int i;
3334 3321
3335 /* Partial: copy the function name, use selfdict and copy 3322 /* Partial: copy the function name, use selfdict and copy
3336 * args. Can't take over name or args, the partial might 3323 * args. Can't take over name or args, the partial might
3337 * be referenced elsewhere. */ 3324 * be referenced elsewhere. */
3338 pt->pt_name = vim_strsave(ret_pt->pt_name); 3325 if (ret_pt->pt_name != NULL)
3339 func_ref(pt->pt_name); 3326 {
3327 pt->pt_name = vim_strsave(ret_pt->pt_name);
3328 func_ref(pt->pt_name);
3329 }
3330 else
3331 {
3332 pt->pt_func = ret_pt->pt_func;
3333 func_ptr_ref(pt->pt_func);
3334 }
3340 if (ret_pt->pt_argc > 0) 3335 if (ret_pt->pt_argc > 0)
3341 { 3336 {
3342 pt->pt_argv = (typval_T *)alloc( 3337 pt->pt_argv = (typval_T *)alloc(
3343 sizeof(typval_T) * ret_pt->pt_argc); 3338 sizeof(typval_T) * ret_pt->pt_argc);
3344 if (pt->pt_argv == NULL) 3339 if (pt->pt_argv == NULL)
3701 * "ht_stack" is used to add hashtabs to be marked. Can be NULL. 3696 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
3702 * 3697 *
3703 * Returns TRUE if setting references failed somehow. 3698 * Returns TRUE if setting references failed somehow.
3704 */ 3699 */
3705 int 3700 int
3706 set_ref_in_func(char_u *name, int copyID) 3701 set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
3707 { 3702 {
3708 ufunc_T *fp; 3703 ufunc_T *fp = fp_in;
3709 funccall_T *fc; 3704 funccall_T *fc;
3710 int error = ERROR_NONE; 3705 int error = ERROR_NONE;
3711 char_u fname_buf[FLEN_FIXED + 1]; 3706 char_u fname_buf[FLEN_FIXED + 1];
3712 char_u *tofree = NULL; 3707 char_u *tofree = NULL;
3713 char_u *fname; 3708 char_u *fname;
3714 3709
3715 if (name == NULL) 3710 if (name == NULL && fp_in == NULL)
3716 return FALSE; 3711 return FALSE;
3717 3712
3718 fname = fname_trans_sid(name, fname_buf, &tofree, &error); 3713 if (fp_in == NULL)
3719 fp = find_func(fname); 3714 {
3715 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3716 fp = find_func(fname);
3717 }
3720 if (fp != NULL) 3718 if (fp != NULL)
3721 { 3719 {
3722 for (fc = fp->uf_scoped; fc != NULL; fc = fc->func->uf_scoped) 3720 for (fc = fp->uf_scoped; fc != NULL; fc = fc->func->uf_scoped)
3723 { 3721 {
3724 if (fc->fc_copyID != copyID) 3722 if (fc->fc_copyID != copyID)