Mercurial > vim
comparison src/eval.c @ 9562:86af4a48c00a v7.4.2058
commit https://github.com/vim/vim/commit/a9b579f3d7463720a316e11e77a7a9fbb9267986
Author: Bram Moolenaar <Bram@vim.org>
Date: Sun Jul 17 18:29:19 2016 +0200
patch 7.4.2058
Problem: eval.c is too big.
Solution: Move user functions to userfunc.c
author | Christian Brabandt <cb@256bit.org> |
---|---|
date | Sun, 17 Jul 2016 18:30:05 +0200 |
parents | 1e68dfd7931b |
children | 5eaa708ab50d |
comparison
equal
deleted
inserted
replaced
9561:8b20e3f947c7 | 9562:86af4a48c00a |
---|---|
27 #ifdef MACOS | 27 #ifdef MACOS |
28 # include <time.h> /* for time_t */ | 28 # include <time.h> /* for time_t */ |
29 #endif | 29 #endif |
30 | 30 |
31 #define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */ | 31 #define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */ |
32 | |
33 #define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not | |
34 be freed. */ | |
35 | |
36 /* | |
37 * Structure returned by get_lval() and used by set_var_lval(). | |
38 * For a plain name: | |
39 * "name" points to the variable name. | |
40 * "exp_name" is NULL. | |
41 * "tv" is NULL | |
42 * For a magic braces name: | |
43 * "name" points to the expanded variable name. | |
44 * "exp_name" is non-NULL, to be freed later. | |
45 * "tv" is NULL | |
46 * For an index in a list: | |
47 * "name" points to the (expanded) variable name. | |
48 * "exp_name" NULL or non-NULL, to be freed later. | |
49 * "tv" points to the (first) list item value | |
50 * "li" points to the (first) list item | |
51 * "range", "n1", "n2" and "empty2" indicate what items are used. | |
52 * For an existing Dict item: | |
53 * "name" points to the (expanded) variable name. | |
54 * "exp_name" NULL or non-NULL, to be freed later. | |
55 * "tv" points to the dict item value | |
56 * "newkey" is NULL | |
57 * For a non-existing Dict item: | |
58 * "name" points to the (expanded) variable name. | |
59 * "exp_name" NULL or non-NULL, to be freed later. | |
60 * "tv" points to the Dictionary typval_T | |
61 * "newkey" is the key for the new item. | |
62 */ | |
63 typedef struct lval_S | |
64 { | |
65 char_u *ll_name; /* start of variable name (can be NULL) */ | |
66 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */ | |
67 typval_T *ll_tv; /* Typeval of item being used. If "newkey" | |
68 isn't NULL it's the Dict to which to add | |
69 the item. */ | |
70 listitem_T *ll_li; /* The list item or NULL. */ | |
71 list_T *ll_list; /* The list or NULL. */ | |
72 int ll_range; /* TRUE when a [i:j] range was used */ | |
73 long ll_n1; /* First index for list */ | |
74 long ll_n2; /* Second index for list range */ | |
75 int ll_empty2; /* Second index is empty: [i:] */ | |
76 dict_T *ll_dict; /* The Dictionary or NULL */ | |
77 dictitem_T *ll_di; /* The dictitem or NULL */ | |
78 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */ | |
79 } lval_T; | |
80 | 32 |
81 static char *e_letunexp = N_("E18: Unexpected characters in :let"); | 33 static char *e_letunexp = N_("E18: Unexpected characters in :let"); |
82 static char *e_undefvar = N_("E121: Undefined variable: %s"); | 34 static char *e_undefvar = N_("E121: Undefined variable: %s"); |
83 static char *e_missbrac = N_("E111: Missing ']'"); | 35 static char *e_missbrac = N_("E111: Missing ']'"); |
84 static char *e_listarg = N_("E686: Argument of %s must be a List"); | 36 static char *e_listarg = N_("E686: Argument of %s must be a List"); |
85 static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary"); | 37 static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary"); |
86 static char *e_listreq = N_("E714: List required"); | 38 static char *e_listreq = N_("E714: List required"); |
87 #ifdef FEAT_QUICKFIX | 39 #ifdef FEAT_QUICKFIX |
88 static char *e_stringreq = N_("E928: String required"); | 40 static char *e_stringreq = N_("E928: String required"); |
89 #endif | 41 #endif |
90 static char *e_toomanyarg = N_("E118: Too many arguments for function: %s"); | |
91 static char *e_dictkey = N_("E716: Key not present in Dictionary: %s"); | |
92 static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it"); | |
93 static char *e_funcdict = N_("E717: Dictionary entry already exists"); | |
94 static char *e_funcref = N_("E718: Funcref required"); | |
95 static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary"); | 42 static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary"); |
96 static char *e_letwrong = N_("E734: Wrong variable type for %s="); | 43 static char *e_letwrong = N_("E734: Wrong variable type for %s="); |
97 static char *e_nofunc = N_("E130: Unknown function: %s"); | |
98 static char *e_illvar = N_("E461: Illegal variable name: %s"); | 44 static char *e_illvar = N_("E461: Illegal variable name: %s"); |
99 #ifdef FEAT_FLOAT | 45 #ifdef FEAT_FLOAT |
100 static char *e_float_as_string = N_("E806: using Float as a String"); | 46 static char *e_float_as_string = N_("E806: using Float as a String"); |
101 #endif | 47 #endif |
102 | 48 |
132 #define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1]) | 78 #define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1]) |
133 #define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab) | 79 #define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab) |
134 | 80 |
135 static int echo_attr = 0; /* attributes used for ":echo" */ | 81 static int echo_attr = 0; /* attributes used for ":echo" */ |
136 | 82 |
137 /* Values for trans_function_name() argument: */ | |
138 #define TFN_INT 1 /* internal function name OK */ | |
139 #define TFN_QUIET 2 /* no error messages */ | |
140 #define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */ | |
141 | |
142 /* Values for get_lval() flags argument: */ | |
143 #define GLV_QUIET TFN_QUIET /* no error messages */ | |
144 #define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */ | |
145 | |
146 /* | |
147 * Structure to hold info for a user function. | |
148 */ | |
149 typedef struct ufunc ufunc_T; | |
150 | |
151 struct ufunc | |
152 { | |
153 int uf_varargs; /* variable nr of arguments */ | |
154 int uf_flags; | |
155 int uf_calls; /* nr of active calls */ | |
156 garray_T uf_args; /* arguments */ | |
157 garray_T uf_lines; /* function lines */ | |
158 #ifdef FEAT_PROFILE | |
159 int uf_profiling; /* TRUE when func is being profiled */ | |
160 /* profiling the function as a whole */ | |
161 int uf_tm_count; /* nr of calls */ | |
162 proftime_T uf_tm_total; /* time spent in function + children */ | |
163 proftime_T uf_tm_self; /* time spent in function itself */ | |
164 proftime_T uf_tm_children; /* time spent in children this call */ | |
165 /* profiling the function per line */ | |
166 int *uf_tml_count; /* nr of times line was executed */ | |
167 proftime_T *uf_tml_total; /* time spent in a line + children */ | |
168 proftime_T *uf_tml_self; /* time spent in a line itself */ | |
169 proftime_T uf_tml_start; /* start time for current line */ | |
170 proftime_T uf_tml_children; /* time spent in children for this line */ | |
171 proftime_T uf_tml_wait; /* start wait time for current line */ | |
172 int uf_tml_idx; /* index of line being timed; -1 if none */ | |
173 int uf_tml_execed; /* line being timed was executed */ | |
174 #endif | |
175 scid_T uf_script_ID; /* ID of script where function was defined, | |
176 used for s: variables */ | |
177 int uf_refcount; /* for numbered function: reference count */ | |
178 char_u uf_name[1]; /* name of function (actually longer); can | |
179 start with <SNR>123_ (<SNR> is K_SPECIAL | |
180 KS_EXTRA KE_SNR) */ | |
181 }; | |
182 | |
183 /* function flags */ | |
184 #define FC_ABORT 1 /* abort function on error */ | |
185 #define FC_RANGE 2 /* function accepts range */ | |
186 #define FC_DICT 4 /* Dict function, uses "self" */ | |
187 | |
188 /* | |
189 * All user-defined functions are found in this hashtable. | |
190 */ | |
191 static hashtab_T func_hashtab; | |
192 | |
193 /* The names of packages that once were loaded are remembered. */ | 83 /* The names of packages that once were loaded are remembered. */ |
194 static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL}; | 84 static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL}; |
195 | |
196 /* From user function to hashitem and back. */ | |
197 static ufunc_T dumuf; | |
198 #define UF2HIKEY(fp) ((fp)->uf_name) | |
199 #define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf))) | |
200 #define HI2UF(hi) HIKEY2UF((hi)->hi_key) | |
201 | |
202 #define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j] | |
203 #define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j] | |
204 | |
205 #define MAX_FUNC_ARGS 20 /* maximum number of function arguments */ | |
206 #define VAR_SHORT_LEN 20 /* short variable name length */ | |
207 #define FIXVAR_CNT 12 /* number of fixed variables */ | |
208 | |
209 /* structure to hold info for a function that is currently being executed. */ | |
210 typedef struct funccall_S funccall_T; | |
211 | |
212 struct funccall_S | |
213 { | |
214 ufunc_T *func; /* function being called */ | |
215 int linenr; /* next line to be executed */ | |
216 int returned; /* ":return" used */ | |
217 struct /* fixed variables for arguments */ | |
218 { | |
219 dictitem_T var; /* variable (without room for name) */ | |
220 char_u room[VAR_SHORT_LEN]; /* room for the name */ | |
221 } fixvar[FIXVAR_CNT]; | |
222 dict_T l_vars; /* l: local function variables */ | |
223 dictitem_T l_vars_var; /* variable for l: scope */ | |
224 dict_T l_avars; /* a: argument variables */ | |
225 dictitem_T l_avars_var; /* variable for a: scope */ | |
226 list_T l_varlist; /* list for a:000 */ | |
227 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */ | |
228 typval_T *rettv; /* return value */ | |
229 linenr_T breakpoint; /* next line with breakpoint or zero */ | |
230 int dbg_tick; /* debug_tick when breakpoint was set */ | |
231 int level; /* top nesting level of executed function */ | |
232 #ifdef FEAT_PROFILE | |
233 proftime_T prof_child; /* time spent in a child */ | |
234 #endif | |
235 funccall_T *caller; /* calling function or NULL */ | |
236 }; | |
237 | 85 |
238 /* | 86 /* |
239 * Info used by a ":for" loop. | 87 * Info used by a ":for" loop. |
240 */ | 88 */ |
241 typedef struct | 89 typedef struct |
243 int fi_semicolon; /* TRUE if ending in '; var]' */ | 91 int fi_semicolon; /* TRUE if ending in '; var]' */ |
244 int fi_varcount; /* nr of variables in the list */ | 92 int fi_varcount; /* nr of variables in the list */ |
245 listwatch_T fi_lw; /* keep an eye on the item used. */ | 93 listwatch_T fi_lw; /* keep an eye on the item used. */ |
246 list_T *fi_list; /* list being used */ | 94 list_T *fi_list; /* list being used */ |
247 } forinfo_T; | 95 } forinfo_T; |
248 | |
249 /* | |
250 * Struct used by trans_function_name() | |
251 */ | |
252 typedef struct | |
253 { | |
254 dict_T *fd_dict; /* Dictionary used */ | |
255 char_u *fd_newkey; /* new key in "dict" in allocated memory */ | |
256 dictitem_T *fd_di; /* Dictionary item used */ | |
257 } funcdict_T; | |
258 | 96 |
259 | 97 |
260 /* | 98 /* |
261 * Array to hold the value of v: variables. | 99 * Array to hold the value of v: variables. |
262 * The value is in a dictitem, so that it can also be used in the v: scope. | 100 * The value is in a dictitem, so that it can also be used in the v: scope. |
371 static void prepare_vimvar(int idx, typval_T *save_tv); | 209 static void prepare_vimvar(int idx, typval_T *save_tv); |
372 static void restore_vimvar(int idx, typval_T *save_tv); | 210 static void restore_vimvar(int idx, typval_T *save_tv); |
373 static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars); | 211 static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars); |
374 static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon); | 212 static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon); |
375 static char_u *skip_var_one(char_u *arg); | 213 static char_u *skip_var_one(char_u *arg); |
376 static void list_hashtable_vars(hashtab_T *ht, char_u *prefix, int empty, int *first); | |
377 static void list_glob_vars(int *first); | 214 static void list_glob_vars(int *first); |
378 static void list_buf_vars(int *first); | 215 static void list_buf_vars(int *first); |
379 static void list_win_vars(int *first); | 216 static void list_win_vars(int *first); |
380 #ifdef FEAT_WINDOWS | 217 #ifdef FEAT_WINDOWS |
381 static void list_tab_vars(int *first); | 218 static void list_tab_vars(int *first); |
382 #endif | 219 #endif |
383 static void list_vim_vars(int *first); | 220 static void list_vim_vars(int *first); |
384 static void list_script_vars(int *first); | 221 static void list_script_vars(int *first); |
385 static void list_func_vars(int *first); | |
386 static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first); | 222 static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first); |
387 static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op); | 223 static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op); |
388 static int check_changedtick(char_u *arg); | 224 static int check_changedtick(char_u *arg); |
389 static char_u *get_lval(char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags); | |
390 static void clear_lval(lval_T *lp); | |
391 static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op); | 225 static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op); |
392 static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op); | 226 static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op); |
393 static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep); | 227 static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep); |
394 static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit); | 228 static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit); |
395 static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock); | 229 static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock); |
396 static void item_lock(typval_T *tv, int deep, int lock); | 230 static void item_lock(typval_T *tv, int deep, int lock); |
397 static int tv_islocked(typval_T *tv); | 231 static int tv_islocked(typval_T *tv); |
398 | 232 |
399 static int eval0(char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate); | |
400 static int eval2(char_u **arg, typval_T *rettv, int evaluate); | 233 static int eval2(char_u **arg, typval_T *rettv, int evaluate); |
401 static int eval3(char_u **arg, typval_T *rettv, int evaluate); | 234 static int eval3(char_u **arg, typval_T *rettv, int evaluate); |
402 static int eval4(char_u **arg, typval_T *rettv, int evaluate); | 235 static int eval4(char_u **arg, typval_T *rettv, int evaluate); |
403 static int eval5(char_u **arg, typval_T *rettv, int evaluate); | 236 static int eval5(char_u **arg, typval_T *rettv, int evaluate); |
404 static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string); | 237 static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string); |
407 static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose); | 240 static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose); |
408 static int get_option_tv(char_u **arg, typval_T *rettv, int evaluate); | 241 static int get_option_tv(char_u **arg, typval_T *rettv, int evaluate); |
409 static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate); | 242 static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate); |
410 static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate); | 243 static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate); |
411 static int free_unref_items(int copyID); | 244 static int free_unref_items(int copyID); |
412 static int get_function_args(char_u **argp, char_u endchar, garray_T *newargs, int *varargs, int skip); | |
413 static int get_lambda_tv(char_u **arg, typval_T *rettv, int evaluate); | |
414 static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID); | |
415 static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate); | 245 static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate); |
416 static int find_internal_func(char_u *name); | |
417 static char_u *deref_func_name(char_u *name, int *lenp, partial_T **partial, int no_autoload); | |
418 static int get_func_tv(char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, partial_T *partial, dict_T *selfdict); | |
419 static void emsg_funcname(char *ermsg, char_u *name); | |
420 static int non_zero_arg(typval_T *argvars); | 246 static int non_zero_arg(typval_T *argvars); |
421 | 247 |
422 #ifdef FEAT_FLOAT | 248 #ifdef FEAT_FLOAT |
423 static void f_abs(typval_T *argvars, typval_T *rettv); | 249 static void f_abs(typval_T *argvars, typval_T *rettv); |
424 static void f_acos(typval_T *argvars, typval_T *rettv); | 250 static void f_acos(typval_T *argvars, typval_T *rettv); |
816 static void f_xor(typval_T *argvars, typval_T *rettv); | 642 static void f_xor(typval_T *argvars, typval_T *rettv); |
817 | 643 |
818 static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp); | 644 static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp); |
819 static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum); | 645 static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum); |
820 static int get_env_len(char_u **arg); | 646 static int get_env_len(char_u **arg); |
821 static int get_id_len(char_u **arg); | |
822 static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose); | 647 static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose); |
823 static char_u *find_name_end(char_u *arg, char_u **expr_start, char_u **expr_end, int flags); | |
824 #define FNE_INCL_BR 1 /* find_name_end(): include [] in name */ | |
825 #define FNE_CHECK_START 2 /* find_name_end(): check name starts with | |
826 valid character */ | |
827 static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end); | 648 static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end); |
828 static int eval_isnamec(int c); | |
829 static int eval_isnamec1(int c); | |
830 static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload); | 649 static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload); |
831 static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose); | |
832 static typval_T *alloc_string_tv(char_u *string); | 650 static typval_T *alloc_string_tv(char_u *string); |
833 static void init_tv(typval_T *varp); | 651 static void init_tv(typval_T *varp); |
834 #ifdef FEAT_FLOAT | 652 #ifdef FEAT_FLOAT |
835 static float_T get_tv_float(typval_T *varp); | 653 static float_T get_tv_float(typval_T *varp); |
836 #endif | 654 #endif |
837 static linenr_T get_tv_lnum(typval_T *argvars); | 655 static linenr_T get_tv_lnum(typval_T *argvars); |
838 static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf); | 656 static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf); |
839 static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload); | |
840 static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload); | 657 static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload); |
841 static hashtab_T *find_var_ht(char_u *name, char_u **varname); | 658 static hashtab_T *find_var_ht(char_u *name, char_u **varname); |
842 static funccall_T *get_funccal(void); | |
843 static void vars_clear_ext(hashtab_T *ht, int free_val); | |
844 static void delete_var(hashtab_T *ht, hashitem_T *hi); | 659 static void delete_var(hashtab_T *ht, hashitem_T *hi); |
845 static void list_one_var(dictitem_T *v, char_u *prefix, int *first); | 660 static void list_one_var(dictitem_T *v, char_u *prefix, int *first); |
846 static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first); | 661 static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first); |
847 static void set_var(char_u *name, typval_T *varp, int copy); | 662 static void set_var(char_u *name, typval_T *varp, int copy); |
848 static int var_check_fixed(int flags, char_u *name, int use_gettext); | 663 static int var_check_fixed(int flags, char_u *name, int use_gettext); |
849 static char_u *find_option_end(char_u **arg, int *opt_flags); | 664 static char_u *find_option_end(char_u **arg, int *opt_flags); |
850 static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd, partial_T **partial); | |
851 static int eval_fname_script(char_u *p); | |
852 static int eval_fname_sid(char_u *p); | |
853 static void list_func_head(ufunc_T *fp, int indent); | |
854 static ufunc_T *find_func(char_u *name); | |
855 static int function_exists(char_u *name); | |
856 static int builtin_function(char_u *name, int len); | |
857 #ifdef FEAT_PROFILE | |
858 static void func_do_profile(ufunc_T *fp); | |
859 static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self); | |
860 static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self); | |
861 static int | |
862 # ifdef __BORLANDC__ | |
863 _RTLENTRYF | |
864 # endif | |
865 prof_total_cmp(const void *s1, const void *s2); | |
866 static int | |
867 # ifdef __BORLANDC__ | |
868 _RTLENTRYF | |
869 # endif | |
870 prof_self_cmp(const void *s1, const void *s2); | |
871 #endif | |
872 static int script_autoload(char_u *name, int reload); | |
873 static char_u *autoload_name(char_u *name); | |
874 static void cat_func_name(char_u *buf, ufunc_T *fp); | |
875 static void func_free(ufunc_T *fp); | |
876 static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict); | |
877 static int can_free_funccal(funccall_T *fc, int copyID) ; | |
878 static void free_funccal(funccall_T *fc, int free_val); | |
879 static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr); | |
880 static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp); | 665 static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp); |
881 static win_T *find_tabwin(typval_T *wvp, typval_T *tvp); | 666 static win_T *find_tabwin(typval_T *wvp, typval_T *tvp); |
882 static void getwinvar(typval_T *argvars, typval_T *rettv, int off); | 667 static void getwinvar(typval_T *argvars, typval_T *rettv, int off); |
883 static int searchpair_cmn(typval_T *argvars, pos_T *match_pos); | 668 static int searchpair_cmn(typval_T *argvars, pos_T *match_pos); |
884 static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp); | 669 static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp); |
903 | 688 |
904 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE); | 689 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE); |
905 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE); | 690 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE); |
906 vimvardict.dv_lock = VAR_FIXED; | 691 vimvardict.dv_lock = VAR_FIXED; |
907 hash_init(&compat_hashtab); | 692 hash_init(&compat_hashtab); |
908 hash_init(&func_hashtab); | 693 func_init(); |
909 | 694 |
910 for (i = 0; i < VV_LEN; ++i) | 695 for (i = 0; i < VV_LEN; ++i) |
911 { | 696 { |
912 p = &vimvars[i]; | 697 p = &vimvars[i]; |
913 if (STRLEN(p->vv_name) > 16) | 698 if (STRLEN(p->vv_name) > 16) |
1000 /* unreferenced lists and dicts */ | 785 /* unreferenced lists and dicts */ |
1001 (void)garbage_collect(FALSE); | 786 (void)garbage_collect(FALSE); |
1002 | 787 |
1003 /* functions */ | 788 /* functions */ |
1004 free_all_functions(); | 789 free_all_functions(); |
1005 hash_clear(&func_hashtab); | 790 } |
1006 } | 791 #endif |
1007 #endif | |
1008 | |
1009 /* | |
1010 * Return the name of the executed function. | |
1011 */ | |
1012 char_u * | |
1013 func_name(void *cookie) | |
1014 { | |
1015 return ((funccall_T *)cookie)->func->uf_name; | |
1016 } | |
1017 | |
1018 /* | |
1019 * Return the address holding the next breakpoint line for a funccall cookie. | |
1020 */ | |
1021 linenr_T * | |
1022 func_breakpoint(void *cookie) | |
1023 { | |
1024 return &((funccall_T *)cookie)->breakpoint; | |
1025 } | |
1026 | |
1027 /* | |
1028 * Return the address holding the debug tick for a funccall cookie. | |
1029 */ | |
1030 int * | |
1031 func_dbg_tick(void *cookie) | |
1032 { | |
1033 return &((funccall_T *)cookie)->dbg_tick; | |
1034 } | |
1035 | |
1036 /* | |
1037 * Return the nesting level for a funccall cookie. | |
1038 */ | |
1039 int | |
1040 func_level(void *cookie) | |
1041 { | |
1042 return ((funccall_T *)cookie)->level; | |
1043 } | |
1044 | |
1045 /* pointer to funccal for currently active function */ | |
1046 funccall_T *current_funccal = NULL; | |
1047 | |
1048 /* pointer to list of previously used funccal, still around because some | |
1049 * item in it is still being used. */ | |
1050 funccall_T *previous_funccal = NULL; | |
1051 | |
1052 /* | |
1053 * Return TRUE when a function was ended by a ":return" command. | |
1054 */ | |
1055 int | |
1056 current_func_returned(void) | |
1057 { | |
1058 return current_funccal->returned; | |
1059 } | |
1060 | 792 |
1061 | 793 |
1062 /* | 794 /* |
1063 * Set an internal variable to a string value. Creates the variable if it does | 795 * Set an internal variable to a string value. Creates the variable if it does |
1064 * not already exist. | 796 * not already exist. |
1765 | 1497 |
1766 return rettv.vval.v_list; | 1498 return rettv.vval.v_list; |
1767 } | 1499 } |
1768 #endif | 1500 #endif |
1769 | 1501 |
1770 /* | |
1771 * Save the current function call pointer, and set it to NULL. | |
1772 * Used when executing autocommands and for ":source". | |
1773 */ | |
1774 void * | |
1775 save_funccal(void) | |
1776 { | |
1777 funccall_T *fc = current_funccal; | |
1778 | |
1779 current_funccal = NULL; | |
1780 return (void *)fc; | |
1781 } | |
1782 | |
1783 void | |
1784 restore_funccal(void *vfc) | |
1785 { | |
1786 funccall_T *fc = (funccall_T *)vfc; | |
1787 | |
1788 current_funccal = fc; | |
1789 } | |
1790 | |
1791 #if defined(FEAT_PROFILE) || defined(PROTO) | |
1792 /* | |
1793 * Prepare profiling for entering a child or something else that is not | |
1794 * counted for the script/function itself. | |
1795 * Should always be called in pair with prof_child_exit(). | |
1796 */ | |
1797 void | |
1798 prof_child_enter( | |
1799 proftime_T *tm) /* place to store waittime */ | |
1800 { | |
1801 funccall_T *fc = current_funccal; | |
1802 | |
1803 if (fc != NULL && fc->func->uf_profiling) | |
1804 profile_start(&fc->prof_child); | |
1805 script_prof_save(tm); | |
1806 } | |
1807 | |
1808 /* | |
1809 * Take care of time spent in a child. | |
1810 * Should always be called after prof_child_enter(). | |
1811 */ | |
1812 void | |
1813 prof_child_exit( | |
1814 proftime_T *tm) /* where waittime was stored */ | |
1815 { | |
1816 funccall_T *fc = current_funccal; | |
1817 | |
1818 if (fc != NULL && fc->func->uf_profiling) | |
1819 { | |
1820 profile_end(&fc->prof_child); | |
1821 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */ | |
1822 profile_add(&fc->func->uf_tm_children, &fc->prof_child); | |
1823 profile_add(&fc->func->uf_tml_children, &fc->prof_child); | |
1824 } | |
1825 script_prof_restore(tm); | |
1826 } | |
1827 #endif | |
1828 | |
1829 | 1502 |
1830 #ifdef FEAT_FOLDING | 1503 #ifdef FEAT_FOLDING |
1831 /* | 1504 /* |
1832 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding | 1505 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding |
1833 * it in "*cp". Doesn't give error messages. | 1506 * it in "*cp". Doesn't give error messages. |
2128 | 1801 |
2129 /* | 1802 /* |
2130 * List variables for hashtab "ht" with prefix "prefix". | 1803 * List variables for hashtab "ht" with prefix "prefix". |
2131 * If "empty" is TRUE also list NULL strings as empty strings. | 1804 * If "empty" is TRUE also list NULL strings as empty strings. |
2132 */ | 1805 */ |
2133 static void | 1806 void |
2134 list_hashtable_vars( | 1807 list_hashtable_vars( |
2135 hashtab_T *ht, | 1808 hashtab_T *ht, |
2136 char_u *prefix, | 1809 char_u *prefix, |
2137 int empty, | 1810 int empty, |
2138 int *first) | 1811 int *first) |
2218 list_script_vars(int *first) | 1891 list_script_vars(int *first) |
2219 { | 1892 { |
2220 if (current_SID > 0 && current_SID <= ga_scripts.ga_len) | 1893 if (current_SID > 0 && current_SID <= ga_scripts.ga_len) |
2221 list_hashtable_vars(&SCRIPT_VARS(current_SID), | 1894 list_hashtable_vars(&SCRIPT_VARS(current_SID), |
2222 (char_u *)"s:", FALSE, first); | 1895 (char_u *)"s:", FALSE, first); |
2223 } | |
2224 | |
2225 /* | |
2226 * List function variables, if there is a function. | |
2227 */ | |
2228 static void | |
2229 list_func_vars(int *first) | |
2230 { | |
2231 if (current_funccal != NULL) | |
2232 list_hashtable_vars(¤t_funccal->l_vars.dv_hashtab, | |
2233 (char_u *)"l:", FALSE, first); | |
2234 } | 1896 } |
2235 | 1897 |
2236 /* | 1898 /* |
2237 * List variables in "arg". | 1899 * List variables in "arg". |
2238 */ | 1900 */ |
2565 * | 2227 * |
2566 * Returns a pointer to just after the name, including indexes. | 2228 * Returns a pointer to just after the name, including indexes. |
2567 * When an evaluation error occurs "lp->ll_name" is NULL; | 2229 * When an evaluation error occurs "lp->ll_name" is NULL; |
2568 * Returns NULL for a parsing error. Still need to free items in "lp"! | 2230 * Returns NULL for a parsing error. Still need to free items in "lp"! |
2569 */ | 2231 */ |
2570 static char_u * | 2232 char_u * |
2571 get_lval( | 2233 get_lval( |
2572 char_u *name, | 2234 char_u *name, |
2573 typval_T *rettv, | 2235 typval_T *rettv, |
2574 lval_T *lp, | 2236 lval_T *lp, |
2575 int unlet, | 2237 int unlet, |
2910 } | 2572 } |
2911 | 2573 |
2912 /* | 2574 /* |
2913 * Clear lval "lp" that was filled by get_lval(). | 2575 * Clear lval "lp" that was filled by get_lval(). |
2914 */ | 2576 */ |
2915 static void | 2577 void |
2916 clear_lval(lval_T *lp) | 2578 clear_lval(lval_T *lp) |
2917 { | 2579 { |
2918 vim_free(lp->ll_exp_name); | 2580 vim_free(lp->ll_exp_name); |
2919 vim_free(lp->ll_newkey); | 2581 vim_free(lp->ll_newkey); |
2920 } | 2582 } |
3403 } | 3065 } |
3404 | 3066 |
3405 #endif /* FEAT_CMDL_COMPL */ | 3067 #endif /* FEAT_CMDL_COMPL */ |
3406 | 3068 |
3407 /* | 3069 /* |
3408 * ":1,25call func(arg1, arg2)" function call. | |
3409 */ | |
3410 void | |
3411 ex_call(exarg_T *eap) | |
3412 { | |
3413 char_u *arg = eap->arg; | |
3414 char_u *startarg; | |
3415 char_u *name; | |
3416 char_u *tofree; | |
3417 int len; | |
3418 typval_T rettv; | |
3419 linenr_T lnum; | |
3420 int doesrange; | |
3421 int failed = FALSE; | |
3422 funcdict_T fudi; | |
3423 partial_T *partial = NULL; | |
3424 | |
3425 if (eap->skip) | |
3426 { | |
3427 /* trans_function_name() doesn't work well when skipping, use eval0() | |
3428 * instead to skip to any following command, e.g. for: | |
3429 * :if 0 | call dict.foo().bar() | endif */ | |
3430 ++emsg_skip; | |
3431 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL) | |
3432 clear_tv(&rettv); | |
3433 --emsg_skip; | |
3434 return; | |
3435 } | |
3436 | |
3437 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial); | |
3438 if (fudi.fd_newkey != NULL) | |
3439 { | |
3440 /* Still need to give an error message for missing key. */ | |
3441 EMSG2(_(e_dictkey), fudi.fd_newkey); | |
3442 vim_free(fudi.fd_newkey); | |
3443 } | |
3444 if (tofree == NULL) | |
3445 return; | |
3446 | |
3447 /* Increase refcount on dictionary, it could get deleted when evaluating | |
3448 * the arguments. */ | |
3449 if (fudi.fd_dict != NULL) | |
3450 ++fudi.fd_dict->dv_refcount; | |
3451 | |
3452 /* If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its | |
3453 * contents. For VAR_PARTIAL get its partial, unless we already have one | |
3454 * from trans_function_name(). */ | |
3455 len = (int)STRLEN(tofree); | |
3456 name = deref_func_name(tofree, &len, | |
3457 partial != NULL ? NULL : &partial, FALSE); | |
3458 | |
3459 /* Skip white space to allow ":call func ()". Not good, but required for | |
3460 * backward compatibility. */ | |
3461 startarg = skipwhite(arg); | |
3462 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */ | |
3463 | |
3464 if (*startarg != '(') | |
3465 { | |
3466 EMSG2(_("E107: Missing parentheses: %s"), eap->arg); | |
3467 goto end; | |
3468 } | |
3469 | |
3470 /* | |
3471 * When skipping, evaluate the function once, to find the end of the | |
3472 * arguments. | |
3473 * When the function takes a range, this is discovered after the first | |
3474 * call, and the loop is broken. | |
3475 */ | |
3476 if (eap->skip) | |
3477 { | |
3478 ++emsg_skip; | |
3479 lnum = eap->line2; /* do it once, also with an invalid range */ | |
3480 } | |
3481 else | |
3482 lnum = eap->line1; | |
3483 for ( ; lnum <= eap->line2; ++lnum) | |
3484 { | |
3485 if (!eap->skip && eap->addr_count > 0) | |
3486 { | |
3487 curwin->w_cursor.lnum = lnum; | |
3488 curwin->w_cursor.col = 0; | |
3489 #ifdef FEAT_VIRTUALEDIT | |
3490 curwin->w_cursor.coladd = 0; | |
3491 #endif | |
3492 } | |
3493 arg = startarg; | |
3494 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg, | |
3495 eap->line1, eap->line2, &doesrange, | |
3496 !eap->skip, partial, fudi.fd_dict) == FAIL) | |
3497 { | |
3498 failed = TRUE; | |
3499 break; | |
3500 } | |
3501 | |
3502 /* Handle a function returning a Funcref, Dictionary or List. */ | |
3503 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL) | |
3504 { | |
3505 failed = TRUE; | |
3506 break; | |
3507 } | |
3508 | |
3509 clear_tv(&rettv); | |
3510 if (doesrange || eap->skip) | |
3511 break; | |
3512 | |
3513 /* Stop when immediately aborting on error, or when an interrupt | |
3514 * occurred or an exception was thrown but not caught. | |
3515 * get_func_tv() returned OK, so that the check for trailing | |
3516 * characters below is executed. */ | |
3517 if (aborting()) | |
3518 break; | |
3519 } | |
3520 if (eap->skip) | |
3521 --emsg_skip; | |
3522 | |
3523 if (!failed) | |
3524 { | |
3525 /* Check for trailing illegal characters and a following command. */ | |
3526 if (!ends_excmd(*arg)) | |
3527 { | |
3528 emsg_severe = TRUE; | |
3529 EMSG(_(e_trailing)); | |
3530 } | |
3531 else | |
3532 eap->nextcmd = check_nextcmd(arg); | |
3533 } | |
3534 | |
3535 end: | |
3536 dict_unref(fudi.fd_dict); | |
3537 vim_free(tofree); | |
3538 } | |
3539 | |
3540 /* | |
3541 * ":unlet[!] var1 ... " command. | 3070 * ":unlet[!] var1 ... " command. |
3542 */ | 3071 */ |
3543 void | 3072 void |
3544 ex_unlet(exarg_T *eap) | 3073 ex_unlet(exarg_T *eap) |
3545 { | 3074 { |
3701 dictitem_T *di; | 3230 dictitem_T *di; |
3702 | 3231 |
3703 ht = find_var_ht(name, &varname); | 3232 ht = find_var_ht(name, &varname); |
3704 if (ht != NULL && *varname != NUL) | 3233 if (ht != NULL && *varname != NUL) |
3705 { | 3234 { |
3706 if (ht == &globvarht) | 3235 d = get_current_funccal_dict(ht); |
3707 d = &globvardict; | |
3708 else if (current_funccal != NULL | |
3709 && ht == ¤t_funccal->l_vars.dv_hashtab) | |
3710 d = ¤t_funccal->l_vars; | |
3711 else if (ht == &compat_hashtab) | |
3712 d = &vimvardict; | |
3713 else | |
3714 { | |
3715 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE); | |
3716 d = di == NULL ? NULL : di->di_tv.vval.v_dict; | |
3717 } | |
3718 if (d == NULL) | 3236 if (d == NULL) |
3719 { | 3237 { |
3720 EMSG2(_(e_intern2), "do_unlet()"); | 3238 if (ht == &globvarht) |
3721 return FAIL; | 3239 d = &globvardict; |
3240 else if (ht == &compat_hashtab) | |
3241 d = &vimvardict; | |
3242 else | |
3243 { | |
3244 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE); | |
3245 d = di == NULL ? NULL : di->di_tv.vval.v_dict; | |
3246 } | |
3247 if (d == NULL) | |
3248 { | |
3249 EMSG2(_(e_intern2), "do_unlet()"); | |
3250 return FAIL; | |
3251 } | |
3722 } | 3252 } |
3723 hi = hash_find(ht, varname); | 3253 hi = hash_find(ht, varname); |
3724 if (!HASHITEM_EMPTY(hi)) | 3254 if (!HASHITEM_EMPTY(hi)) |
3725 { | 3255 { |
3726 di = HI2DI(hi); | 3256 di = HI2DI(hi); |
4113 * This calls eval1() and handles error message and nextcmd. | 3643 * This calls eval1() and handles error message and nextcmd. |
4114 * Put the result in "rettv" when returning OK and "evaluate" is TRUE. | 3644 * Put the result in "rettv" when returning OK and "evaluate" is TRUE. |
4115 * Note: "rettv.v_lock" is not set. | 3645 * Note: "rettv.v_lock" is not set. |
4116 * Return OK or FAIL. | 3646 * Return OK or FAIL. |
4117 */ | 3647 */ |
4118 static int | 3648 int |
4119 eval0( | 3649 eval0( |
4120 char_u *arg, | 3650 char_u *arg, |
4121 typval_T *rettv, | 3651 typval_T *rettv, |
4122 char_u **nextcmd, | 3652 char_u **nextcmd, |
4123 int evaluate) | 3653 int evaluate) |
6086 { | 5616 { |
6087 current_copyID += COPYID_INC; | 5617 current_copyID += COPYID_INC; |
6088 return current_copyID; | 5618 return current_copyID; |
6089 } | 5619 } |
6090 | 5620 |
6091 /* Used by get_func_tv() */ | |
6092 static garray_T funcargs = GA_EMPTY; | |
6093 | |
6094 /* | 5621 /* |
6095 * Garbage collection for lists and dictionaries. | 5622 * Garbage collection for lists and dictionaries. |
6096 * | 5623 * |
6097 * We use reference counts to be able to free most items right away when they | 5624 * We use reference counts to be able to free most items right away when they |
6098 * are no longer used. But for composite items it's possible that it becomes | 5625 * are no longer used. But for composite items it's possible that it becomes |
6122 int copyID; | 5649 int copyID; |
6123 int abort = FALSE; | 5650 int abort = FALSE; |
6124 buf_T *buf; | 5651 buf_T *buf; |
6125 win_T *wp; | 5652 win_T *wp; |
6126 int i; | 5653 int i; |
6127 funccall_T *fc, **pfc; | |
6128 int did_free = FALSE; | 5654 int did_free = FALSE; |
6129 int did_free_funccal = FALSE; | |
6130 #ifdef FEAT_WINDOWS | 5655 #ifdef FEAT_WINDOWS |
6131 tabpage_T *tp; | 5656 tabpage_T *tp; |
6132 #endif | 5657 #endif |
6133 | 5658 |
6134 if (!testing) | 5659 if (!testing) |
6149 */ | 5674 */ |
6150 | 5675 |
6151 /* Don't free variables in the previous_funccal list unless they are only | 5676 /* Don't free variables in the previous_funccal list unless they are only |
6152 * referenced through previous_funccal. This must be first, because if | 5677 * referenced through previous_funccal. This must be first, because if |
6153 * the item is referenced elsewhere the funccal must not be freed. */ | 5678 * the item is referenced elsewhere the funccal must not be freed. */ |
6154 for (fc = previous_funccal; fc != NULL; fc = fc->caller) | 5679 abort = abort || set_ref_in_previous_funccal(copyID); |
6155 { | |
6156 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1, | |
6157 NULL); | |
6158 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1, | |
6159 NULL); | |
6160 } | |
6161 | 5680 |
6162 /* script-local variables */ | 5681 /* script-local variables */ |
6163 for (i = 1; i <= ga_scripts.ga_len; ++i) | 5682 for (i = 1; i <= ga_scripts.ga_len; ++i) |
6164 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL); | 5683 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL); |
6165 | 5684 |
6187 | 5706 |
6188 /* global variables */ | 5707 /* global variables */ |
6189 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL); | 5708 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL); |
6190 | 5709 |
6191 /* function-local variables */ | 5710 /* function-local variables */ |
6192 for (fc = current_funccal; fc != NULL; fc = fc->caller) | 5711 abort = abort || set_ref_in_call_stack(copyID); |
6193 { | |
6194 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL); | |
6195 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL); | |
6196 } | |
6197 | 5712 |
6198 /* function call arguments, if v:testing is set. */ | 5713 /* function call arguments, if v:testing is set. */ |
6199 for (i = 0; i < funcargs.ga_len; ++i) | 5714 abort = abort || set_ref_in_func_args(copyID); |
6200 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i], | |
6201 copyID, NULL, NULL); | |
6202 | 5715 |
6203 /* v: vars */ | 5716 /* v: vars */ |
6204 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL); | 5717 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL); |
6205 | 5718 |
6206 #ifdef FEAT_LUA | 5719 #ifdef FEAT_LUA |
6234 */ | 5747 */ |
6235 did_free = free_unref_items(copyID); | 5748 did_free = free_unref_items(copyID); |
6236 | 5749 |
6237 /* | 5750 /* |
6238 * 3. Check if any funccal can be freed now. | 5751 * 3. Check if any funccal can be freed now. |
5752 * This may call us back recursively. | |
6239 */ | 5753 */ |
6240 for (pfc = &previous_funccal; *pfc != NULL; ) | 5754 free_unref_funccal(copyID, testing); |
6241 { | |
6242 if (can_free_funccal(*pfc, copyID)) | |
6243 { | |
6244 fc = *pfc; | |
6245 *pfc = fc->caller; | |
6246 free_funccal(fc, TRUE); | |
6247 did_free = TRUE; | |
6248 did_free_funccal = TRUE; | |
6249 } | |
6250 else | |
6251 pfc = &(*pfc)->caller; | |
6252 } | |
6253 if (did_free_funccal) | |
6254 /* When a funccal was freed some more items might be garbage | |
6255 * collected, so run again. */ | |
6256 (void)garbage_collect(testing); | |
6257 } | 5755 } |
6258 else if (p_verbose > 0) | 5756 else if (p_verbose > 0) |
6259 { | 5757 { |
6260 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!")); | 5758 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!")); |
6261 } | 5759 } |
6563 } | 6061 } |
6564 } | 6062 } |
6565 } | 6063 } |
6566 #endif | 6064 #endif |
6567 return abort; | 6065 return abort; |
6568 } | |
6569 | |
6570 /* Get function arguments. */ | |
6571 static int | |
6572 get_function_args( | |
6573 char_u **argp, | |
6574 char_u endchar, | |
6575 garray_T *newargs, | |
6576 int *varargs, | |
6577 int skip) | |
6578 { | |
6579 int mustend = FALSE; | |
6580 char_u *arg = *argp; | |
6581 char_u *p = arg; | |
6582 int c; | |
6583 int i; | |
6584 | |
6585 if (newargs != NULL) | |
6586 ga_init2(newargs, (int)sizeof(char_u *), 3); | |
6587 | |
6588 if (varargs != NULL) | |
6589 *varargs = FALSE; | |
6590 | |
6591 /* | |
6592 * Isolate the arguments: "arg1, arg2, ...)" | |
6593 */ | |
6594 while (*p != endchar) | |
6595 { | |
6596 if (p[0] == '.' && p[1] == '.' && p[2] == '.') | |
6597 { | |
6598 if (varargs != NULL) | |
6599 *varargs = TRUE; | |
6600 p += 3; | |
6601 mustend = TRUE; | |
6602 } | |
6603 else | |
6604 { | |
6605 arg = p; | |
6606 while (ASCII_ISALNUM(*p) || *p == '_') | |
6607 ++p; | |
6608 if (arg == p || isdigit(*arg) | |
6609 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0) | |
6610 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0)) | |
6611 { | |
6612 if (!skip) | |
6613 EMSG2(_("E125: Illegal argument: %s"), arg); | |
6614 break; | |
6615 } | |
6616 if (newargs != NULL && ga_grow(newargs, 1) == FAIL) | |
6617 return FAIL; | |
6618 if (newargs != NULL) | |
6619 { | |
6620 c = *p; | |
6621 *p = NUL; | |
6622 arg = vim_strsave(arg); | |
6623 if (arg == NULL) | |
6624 goto err_ret; | |
6625 | |
6626 /* Check for duplicate argument name. */ | |
6627 for (i = 0; i < newargs->ga_len; ++i) | |
6628 if (STRCMP(((char_u **)(newargs->ga_data))[i], arg) == 0) | |
6629 { | |
6630 EMSG2(_("E853: Duplicate argument name: %s"), arg); | |
6631 vim_free(arg); | |
6632 goto err_ret; | |
6633 } | |
6634 ((char_u **)(newargs->ga_data))[newargs->ga_len] = arg; | |
6635 newargs->ga_len++; | |
6636 | |
6637 *p = c; | |
6638 } | |
6639 if (*p == ',') | |
6640 ++p; | |
6641 else | |
6642 mustend = TRUE; | |
6643 } | |
6644 p = skipwhite(p); | |
6645 if (mustend && *p != endchar) | |
6646 { | |
6647 if (!skip) | |
6648 EMSG2(_(e_invarg2), *argp); | |
6649 break; | |
6650 } | |
6651 } | |
6652 ++p; /* skip the ')' */ | |
6653 | |
6654 *argp = p; | |
6655 return OK; | |
6656 | |
6657 err_ret: | |
6658 if (newargs != NULL) | |
6659 ga_clear_strings(newargs); | |
6660 return FAIL; | |
6661 } | |
6662 | |
6663 /* | |
6664 * Parse a lambda expression and get a Funcref from "*arg". | |
6665 * Return OK or FAIL. Returns NOTDONE for dict or {expr}. | |
6666 */ | |
6667 static int | |
6668 get_lambda_tv(char_u **arg, typval_T *rettv, int evaluate) | |
6669 { | |
6670 garray_T newargs; | |
6671 garray_T newlines; | |
6672 ufunc_T *fp = NULL; | |
6673 int varargs; | |
6674 int ret; | |
6675 char_u name[20]; | |
6676 char_u *start = skipwhite(*arg + 1); | |
6677 char_u *s, *e; | |
6678 static int lambda_no = 0; | |
6679 | |
6680 ga_init(&newargs); | |
6681 ga_init(&newlines); | |
6682 | |
6683 /* First, check if this is a lambda expression. "->" must exist. */ | |
6684 ret = get_function_args(&start, '-', NULL, NULL, TRUE); | |
6685 if (ret == FAIL || *start != '>') | |
6686 return NOTDONE; | |
6687 | |
6688 /* Parse the arguments again. */ | |
6689 *arg = skipwhite(*arg + 1); | |
6690 ret = get_function_args(arg, '-', &newargs, &varargs, FALSE); | |
6691 if (ret == FAIL || **arg != '>') | |
6692 goto errret; | |
6693 | |
6694 /* Get the start and the end of the expression. */ | |
6695 *arg = skipwhite(*arg + 1); | |
6696 s = *arg; | |
6697 ret = skip_expr(arg); | |
6698 if (ret == FAIL) | |
6699 goto errret; | |
6700 e = *arg; | |
6701 *arg = skipwhite(*arg); | |
6702 if (**arg != '}') | |
6703 goto errret; | |
6704 ++*arg; | |
6705 | |
6706 if (evaluate) | |
6707 { | |
6708 int len; | |
6709 char_u *p; | |
6710 | |
6711 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + 20)); | |
6712 if (fp == NULL) | |
6713 goto errret; | |
6714 | |
6715 sprintf((char*)name, "<lambda>%d", ++lambda_no); | |
6716 | |
6717 ga_init2(&newlines, (int)sizeof(char_u *), 1); | |
6718 if (ga_grow(&newlines, 1) == FAIL) | |
6719 goto errret; | |
6720 | |
6721 /* Add "return " before the expression. | |
6722 * TODO: Support multiple expressions. */ | |
6723 len = 7 + e - s + 1; | |
6724 p = (char_u *)alloc(len); | |
6725 if (p == NULL) | |
6726 goto errret; | |
6727 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p; | |
6728 STRCPY(p, "return "); | |
6729 STRNCPY(p + 7, s, e - s); | |
6730 p[7 + e - s] = NUL; | |
6731 | |
6732 fp->uf_refcount = 1; | |
6733 STRCPY(fp->uf_name, name); | |
6734 hash_add(&func_hashtab, UF2HIKEY(fp)); | |
6735 fp->uf_args = newargs; | |
6736 fp->uf_lines = newlines; | |
6737 | |
6738 #ifdef FEAT_PROFILE | |
6739 fp->uf_tml_count = NULL; | |
6740 fp->uf_tml_total = NULL; | |
6741 fp->uf_tml_self = NULL; | |
6742 fp->uf_profiling = FALSE; | |
6743 if (prof_def_func()) | |
6744 func_do_profile(fp); | |
6745 #endif | |
6746 fp->uf_varargs = TRUE; | |
6747 fp->uf_flags = 0; | |
6748 fp->uf_calls = 0; | |
6749 fp->uf_script_ID = current_SID; | |
6750 | |
6751 rettv->vval.v_string = vim_strsave(name); | |
6752 rettv->v_type = VAR_FUNC; | |
6753 } | |
6754 else | |
6755 ga_clear_strings(&newargs); | |
6756 | |
6757 return OK; | |
6758 | |
6759 errret: | |
6760 ga_clear_strings(&newargs); | |
6761 ga_clear_strings(&newlines); | |
6762 vim_free(fp); | |
6763 return FAIL; | |
6764 } | 6066 } |
6765 | 6067 |
6766 static char * | 6068 static char * |
6767 get_var_special_name(int nr) | 6069 get_var_special_name(int nr) |
6768 { | 6070 { |
6971 * "numbuf" is used for a number. | 6273 * "numbuf" is used for a number. |
6972 * Does not put quotes around strings, as ":echo" displays values. | 6274 * Does not put quotes around strings, as ":echo" displays values. |
6973 * When "copyID" is not NULL replace recursive lists and dicts with "...". | 6275 * When "copyID" is not NULL replace recursive lists and dicts with "...". |
6974 * May return NULL. | 6276 * May return NULL. |
6975 */ | 6277 */ |
6976 static char_u * | 6278 char_u * |
6977 echo_string( | 6279 echo_string( |
6978 typval_T *tv, | 6280 typval_T *tv, |
6979 char_u **tofree, | 6281 char_u **tofree, |
6980 char_u *numbuf, | 6282 char_u *numbuf, |
6981 int copyID) | 6283 int copyID) |
7622 | 6924 |
7623 /* | 6925 /* |
7624 * Find internal function in table above. | 6926 * Find internal function in table above. |
7625 * Return index, or -1 if not found | 6927 * Return index, or -1 if not found |
7626 */ | 6928 */ |
7627 static int | 6929 int |
7628 find_internal_func( | 6930 find_internal_func( |
7629 char_u *name) /* name of the function */ | 6931 char_u *name) /* name of the function */ |
7630 { | 6932 { |
7631 int first = 0; | 6933 int first = 0; |
7632 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1; | 6934 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1; |
7648 return x; | 6950 return x; |
7649 } | 6951 } |
7650 return -1; | 6952 return -1; |
7651 } | 6953 } |
7652 | 6954 |
7653 /* | |
7654 * Check if "name" is a variable of type VAR_FUNC. If so, return the function | |
7655 * name it contains, otherwise return "name". | |
7656 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set | |
7657 * "partialp". | |
7658 */ | |
7659 static char_u * | |
7660 deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload) | |
7661 { | |
7662 dictitem_T *v; | |
7663 int cc; | |
7664 | |
7665 if (partialp != NULL) | |
7666 *partialp = NULL; | |
7667 | |
7668 cc = name[*lenp]; | |
7669 name[*lenp] = NUL; | |
7670 v = find_var(name, NULL, no_autoload); | |
7671 name[*lenp] = cc; | |
7672 if (v != NULL && v->di_tv.v_type == VAR_FUNC) | |
7673 { | |
7674 if (v->di_tv.vval.v_string == NULL) | |
7675 { | |
7676 *lenp = 0; | |
7677 return (char_u *)""; /* just in case */ | |
7678 } | |
7679 *lenp = (int)STRLEN(v->di_tv.vval.v_string); | |
7680 return v->di_tv.vval.v_string; | |
7681 } | |
7682 | |
7683 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL) | |
7684 { | |
7685 partial_T *pt = v->di_tv.vval.v_partial; | |
7686 | |
7687 if (pt == NULL) | |
7688 { | |
7689 *lenp = 0; | |
7690 return (char_u *)""; /* just in case */ | |
7691 } | |
7692 if (partialp != NULL) | |
7693 *partialp = pt; | |
7694 *lenp = (int)STRLEN(pt->pt_name); | |
7695 return pt->pt_name; | |
7696 } | |
7697 | |
7698 return name; | |
7699 } | |
7700 | |
7701 /* | |
7702 * Allocate a variable for the result of a function. | |
7703 * Return OK or FAIL. | |
7704 */ | |
7705 static int | |
7706 get_func_tv( | |
7707 char_u *name, /* name of the function */ | |
7708 int len, /* length of "name" */ | |
7709 typval_T *rettv, | |
7710 char_u **arg, /* argument, pointing to the '(' */ | |
7711 linenr_T firstline, /* first line of range */ | |
7712 linenr_T lastline, /* last line of range */ | |
7713 int *doesrange, /* return: function handled range */ | |
7714 int evaluate, | |
7715 partial_T *partial, /* for extra arguments */ | |
7716 dict_T *selfdict) /* Dictionary for "self" */ | |
7717 { | |
7718 char_u *argp; | |
7719 int ret = OK; | |
7720 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */ | |
7721 int argcount = 0; /* number of arguments found */ | |
7722 | |
7723 /* | |
7724 * Get the arguments. | |
7725 */ | |
7726 argp = *arg; | |
7727 while (argcount < MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc)) | |
7728 { | |
7729 argp = skipwhite(argp + 1); /* skip the '(' or ',' */ | |
7730 if (*argp == ')' || *argp == ',' || *argp == NUL) | |
7731 break; | |
7732 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL) | |
7733 { | |
7734 ret = FAIL; | |
7735 break; | |
7736 } | |
7737 ++argcount; | |
7738 if (*argp != ',') | |
7739 break; | |
7740 } | |
7741 if (*argp == ')') | |
7742 ++argp; | |
7743 else | |
7744 ret = FAIL; | |
7745 | |
7746 if (ret == OK) | |
7747 { | |
7748 int i = 0; | |
7749 | |
7750 if (get_vim_var_nr(VV_TESTING)) | |
7751 { | |
7752 /* Prepare for calling test_garbagecollect_now(), need to know | |
7753 * what variables are used on the call stack. */ | |
7754 if (funcargs.ga_itemsize == 0) | |
7755 ga_init2(&funcargs, (int)sizeof(typval_T *), 50); | |
7756 for (i = 0; i < argcount; ++i) | |
7757 if (ga_grow(&funcargs, 1) == OK) | |
7758 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] = | |
7759 &argvars[i]; | |
7760 } | |
7761 | |
7762 ret = call_func(name, len, rettv, argcount, argvars, | |
7763 firstline, lastline, doesrange, evaluate, partial, selfdict); | |
7764 | |
7765 funcargs.ga_len -= i; | |
7766 } | |
7767 else if (!aborting()) | |
7768 { | |
7769 if (argcount == MAX_FUNC_ARGS) | |
7770 emsg_funcname(N_("E740: Too many arguments for function %s"), name); | |
7771 else | |
7772 emsg_funcname(N_("E116: Invalid arguments for function %s"), name); | |
7773 } | |
7774 | |
7775 while (--argcount >= 0) | |
7776 clear_tv(&argvars[argcount]); | |
7777 | |
7778 *arg = skipwhite(argp); | |
7779 return ret; | |
7780 } | |
7781 | |
7782 #define ERROR_UNKNOWN 0 | |
7783 #define ERROR_TOOMANY 1 | |
7784 #define ERROR_TOOFEW 2 | |
7785 #define ERROR_SCRIPT 3 | |
7786 #define ERROR_DICT 4 | |
7787 #define ERROR_NONE 5 | |
7788 #define ERROR_OTHER 6 | |
7789 #define FLEN_FIXED 40 | |
7790 | |
7791 /* | |
7792 * In a script change <SID>name() and s:name() to K_SNR 123_name(). | |
7793 * Change <SNR>123_name() to K_SNR 123_name(). | |
7794 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory | |
7795 * (slow). | |
7796 */ | |
7797 static char_u * | |
7798 fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error) | |
7799 { | |
7800 int llen; | |
7801 char_u *fname; | |
7802 int i; | |
7803 | |
7804 llen = eval_fname_script(name); | |
7805 if (llen > 0) | |
7806 { | |
7807 fname_buf[0] = K_SPECIAL; | |
7808 fname_buf[1] = KS_EXTRA; | |
7809 fname_buf[2] = (int)KE_SNR; | |
7810 i = 3; | |
7811 if (eval_fname_sid(name)) /* "<SID>" or "s:" */ | |
7812 { | |
7813 if (current_SID <= 0) | |
7814 *error = ERROR_SCRIPT; | |
7815 else | |
7816 { | |
7817 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID); | |
7818 i = (int)STRLEN(fname_buf); | |
7819 } | |
7820 } | |
7821 if (i + STRLEN(name + llen) < FLEN_FIXED) | |
7822 { | |
7823 STRCPY(fname_buf + i, name + llen); | |
7824 fname = fname_buf; | |
7825 } | |
7826 else | |
7827 { | |
7828 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1)); | |
7829 if (fname == NULL) | |
7830 *error = ERROR_OTHER; | |
7831 else | |
7832 { | |
7833 *tofree = fname; | |
7834 mch_memmove(fname, fname_buf, (size_t)i); | |
7835 STRCPY(fname + i, name + llen); | |
7836 } | |
7837 } | |
7838 } | |
7839 else | |
7840 fname = name; | |
7841 return fname; | |
7842 } | |
7843 | |
7844 /* | |
7845 * Call a function with its resolved parameters | |
7846 * Return FAIL when the function can't be called, OK otherwise. | |
7847 * Also returns OK when an error was encountered while executing the function. | |
7848 */ | |
7849 int | 6955 int |
7850 call_func( | 6956 call_internal_func( |
7851 char_u *funcname, /* name of the function */ | 6957 char_u *name, |
7852 int len, /* length of "name" */ | 6958 int argcount, |
7853 typval_T *rettv, /* return value goes here */ | 6959 typval_T *argvars, |
7854 int argcount_in, /* number of "argvars" */ | 6960 typval_T *rettv) |
7855 typval_T *argvars_in, /* vars for arguments, must have "argcount" | 6961 { |
7856 PLUS ONE elements! */ | 6962 int i; |
7857 linenr_T firstline, /* first line of range */ | 6963 |
7858 linenr_T lastline, /* last line of range */ | 6964 i = find_internal_func(name); |
7859 int *doesrange, /* return: function handled range */ | 6965 if (i < 0) |
7860 int evaluate, | 6966 return ERROR_UNKNOWN; |
7861 partial_T *partial, /* optional, can be NULL */ | 6967 if (argcount < functions[i].f_min_argc) |
7862 dict_T *selfdict_in) /* Dictionary for "self" */ | 6968 return ERROR_TOOFEW; |
7863 { | 6969 if (argcount > functions[i].f_max_argc) |
7864 int ret = FAIL; | 6970 return ERROR_TOOMANY; |
7865 int error = ERROR_NONE; | 6971 argvars[argcount].v_type = VAR_UNKNOWN; |
7866 int i; | 6972 functions[i].f_func(argvars, rettv); |
7867 ufunc_T *fp; | 6973 return ERROR_NONE; |
7868 char_u fname_buf[FLEN_FIXED + 1]; | |
7869 char_u *tofree = NULL; | |
7870 char_u *fname; | |
7871 char_u *name; | |
7872 int argcount = argcount_in; | |
7873 typval_T *argvars = argvars_in; | |
7874 dict_T *selfdict = selfdict_in; | |
7875 typval_T argv[MAX_FUNC_ARGS + 1]; /* used when "partial" is not NULL */ | |
7876 int argv_clear = 0; | |
7877 | |
7878 /* Make a copy of the name, if it comes from a funcref variable it could | |
7879 * be changed or deleted in the called function. */ | |
7880 name = vim_strnsave(funcname, len); | |
7881 if (name == NULL) | |
7882 return ret; | |
7883 | |
7884 fname = fname_trans_sid(name, fname_buf, &tofree, &error); | |
7885 | |
7886 *doesrange = FALSE; | |
7887 | |
7888 if (partial != NULL) | |
7889 { | |
7890 /* When the function has a partial with a dict and there is a dict | |
7891 * argument, use the dict argument. That is backwards compatible. | |
7892 * When the dict was bound explicitly use the one from the partial. */ | |
7893 if (partial->pt_dict != NULL | |
7894 && (selfdict_in == NULL || !partial->pt_auto)) | |
7895 selfdict = partial->pt_dict; | |
7896 if (error == ERROR_NONE && partial->pt_argc > 0) | |
7897 { | |
7898 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear) | |
7899 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]); | |
7900 for (i = 0; i < argcount_in; ++i) | |
7901 argv[i + argv_clear] = argvars_in[i]; | |
7902 argvars = argv; | |
7903 argcount = partial->pt_argc + argcount_in; | |
7904 } | |
7905 } | |
7906 | |
7907 | |
7908 /* execute the function if no errors detected and executing */ | |
7909 if (evaluate && error == ERROR_NONE) | |
7910 { | |
7911 char_u *rfname = fname; | |
7912 | |
7913 /* Ignore "g:" before a function name. */ | |
7914 if (fname[0] == 'g' && fname[1] == ':') | |
7915 rfname = fname + 2; | |
7916 | |
7917 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */ | |
7918 rettv->vval.v_number = 0; | |
7919 error = ERROR_UNKNOWN; | |
7920 | |
7921 if (!builtin_function(rfname, -1)) | |
7922 { | |
7923 /* | |
7924 * User defined function. | |
7925 */ | |
7926 fp = find_func(rfname); | |
7927 | |
7928 #ifdef FEAT_AUTOCMD | |
7929 /* Trigger FuncUndefined event, may load the function. */ | |
7930 if (fp == NULL | |
7931 && apply_autocmds(EVENT_FUNCUNDEFINED, | |
7932 rfname, rfname, TRUE, NULL) | |
7933 && !aborting()) | |
7934 { | |
7935 /* executed an autocommand, search for the function again */ | |
7936 fp = find_func(rfname); | |
7937 } | |
7938 #endif | |
7939 /* Try loading a package. */ | |
7940 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting()) | |
7941 { | |
7942 /* loaded a package, search for the function again */ | |
7943 fp = find_func(rfname); | |
7944 } | |
7945 | |
7946 if (fp != NULL) | |
7947 { | |
7948 if (fp->uf_flags & FC_RANGE) | |
7949 *doesrange = TRUE; | |
7950 if (argcount < fp->uf_args.ga_len) | |
7951 error = ERROR_TOOFEW; | |
7952 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len) | |
7953 error = ERROR_TOOMANY; | |
7954 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL) | |
7955 error = ERROR_DICT; | |
7956 else | |
7957 { | |
7958 int did_save_redo = FALSE; | |
7959 | |
7960 /* | |
7961 * Call the user function. | |
7962 * Save and restore search patterns, script variables and | |
7963 * redo buffer. | |
7964 */ | |
7965 save_search_patterns(); | |
7966 #ifdef FEAT_INS_EXPAND | |
7967 if (!ins_compl_active()) | |
7968 #endif | |
7969 { | |
7970 saveRedobuff(); | |
7971 did_save_redo = TRUE; | |
7972 } | |
7973 ++fp->uf_calls; | |
7974 call_user_func(fp, argcount, argvars, rettv, | |
7975 firstline, lastline, | |
7976 (fp->uf_flags & FC_DICT) ? selfdict : NULL); | |
7977 if (--fp->uf_calls <= 0 && (isdigit(*fp->uf_name) | |
7978 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0) | |
7979 && fp->uf_refcount <= 0) | |
7980 /* Function was unreferenced while being used, free it | |
7981 * now. */ | |
7982 func_free(fp); | |
7983 if (did_save_redo) | |
7984 restoreRedobuff(); | |
7985 restore_search_patterns(); | |
7986 error = ERROR_NONE; | |
7987 } | |
7988 } | |
7989 } | |
7990 else | |
7991 { | |
7992 /* | |
7993 * Find the function name in the table, call its implementation. | |
7994 */ | |
7995 i = find_internal_func(fname); | |
7996 if (i >= 0) | |
7997 { | |
7998 if (argcount < functions[i].f_min_argc) | |
7999 error = ERROR_TOOFEW; | |
8000 else if (argcount > functions[i].f_max_argc) | |
8001 error = ERROR_TOOMANY; | |
8002 else | |
8003 { | |
8004 argvars[argcount].v_type = VAR_UNKNOWN; | |
8005 functions[i].f_func(argvars, rettv); | |
8006 error = ERROR_NONE; | |
8007 } | |
8008 } | |
8009 } | |
8010 /* | |
8011 * The function call (or "FuncUndefined" autocommand sequence) might | |
8012 * have been aborted by an error, an interrupt, or an explicitly thrown | |
8013 * exception that has not been caught so far. This situation can be | |
8014 * tested for by calling aborting(). For an error in an internal | |
8015 * function or for the "E132" error in call_user_func(), however, the | |
8016 * throw point at which the "force_abort" flag (temporarily reset by | |
8017 * emsg()) is normally updated has not been reached yet. We need to | |
8018 * update that flag first to make aborting() reliable. | |
8019 */ | |
8020 update_force_abort(); | |
8021 } | |
8022 if (error == ERROR_NONE) | |
8023 ret = OK; | |
8024 | |
8025 /* | |
8026 * Report an error unless the argument evaluation or function call has been | |
8027 * cancelled due to an aborting error, an interrupt, or an exception. | |
8028 */ | |
8029 if (!aborting()) | |
8030 { | |
8031 switch (error) | |
8032 { | |
8033 case ERROR_UNKNOWN: | |
8034 emsg_funcname(N_("E117: Unknown function: %s"), name); | |
8035 break; | |
8036 case ERROR_TOOMANY: | |
8037 emsg_funcname(e_toomanyarg, name); | |
8038 break; | |
8039 case ERROR_TOOFEW: | |
8040 emsg_funcname(N_("E119: Not enough arguments for function: %s"), | |
8041 name); | |
8042 break; | |
8043 case ERROR_SCRIPT: | |
8044 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"), | |
8045 name); | |
8046 break; | |
8047 case ERROR_DICT: | |
8048 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"), | |
8049 name); | |
8050 break; | |
8051 } | |
8052 } | |
8053 | |
8054 while (argv_clear > 0) | |
8055 clear_tv(&argv[--argv_clear]); | |
8056 vim_free(tofree); | |
8057 vim_free(name); | |
8058 | |
8059 return ret; | |
8060 } | |
8061 | |
8062 /* | |
8063 * Give an error message with a function name. Handle <SNR> things. | |
8064 * "ermsg" is to be passed without translation, use N_() instead of _(). | |
8065 */ | |
8066 static void | |
8067 emsg_funcname(char *ermsg, char_u *name) | |
8068 { | |
8069 char_u *p; | |
8070 | |
8071 if (*name == K_SPECIAL) | |
8072 p = concat_str((char_u *)"<SNR>", name + 3); | |
8073 else | |
8074 p = name; | |
8075 EMSG2(_(ermsg), p); | |
8076 if (p != name) | |
8077 vim_free(p); | |
8078 } | 6974 } |
8079 | 6975 |
8080 /* | 6976 /* |
8081 * Return TRUE for a non-zero Number and a non-empty String. | 6977 * Return TRUE for a non-zero Number and a non-empty String. |
8082 */ | 6978 */ |
9015 */ | 7911 */ |
9016 static void | 7912 static void |
9017 f_byteidxcomp(typval_T *argvars, typval_T *rettv) | 7913 f_byteidxcomp(typval_T *argvars, typval_T *rettv) |
9018 { | 7914 { |
9019 byteidx(argvars, rettv, TRUE); | 7915 byteidx(argvars, rettv, TRUE); |
9020 } | |
9021 | |
9022 int | |
9023 func_call( | |
9024 char_u *name, | |
9025 typval_T *args, | |
9026 partial_T *partial, | |
9027 dict_T *selfdict, | |
9028 typval_T *rettv) | |
9029 { | |
9030 listitem_T *item; | |
9031 typval_T argv[MAX_FUNC_ARGS + 1]; | |
9032 int argc = 0; | |
9033 int dummy; | |
9034 int r = 0; | |
9035 | |
9036 for (item = args->vval.v_list->lv_first; item != NULL; | |
9037 item = item->li_next) | |
9038 { | |
9039 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc)) | |
9040 { | |
9041 EMSG(_("E699: Too many arguments")); | |
9042 break; | |
9043 } | |
9044 /* Make a copy of each argument. This is needed to be able to set | |
9045 * v_lock to VAR_FIXED in the copy without changing the original list. | |
9046 */ | |
9047 copy_tv(&item->li_tv, &argv[argc++]); | |
9048 } | |
9049 | |
9050 if (item == NULL) | |
9051 r = call_func(name, (int)STRLEN(name), rettv, argc, argv, | |
9052 curwin->w_cursor.lnum, curwin->w_cursor.lnum, | |
9053 &dummy, TRUE, partial, selfdict); | |
9054 | |
9055 /* Free the arguments. */ | |
9056 while (argc > 0) | |
9057 clear_tv(&argv[--argc]); | |
9058 | |
9059 return r; | |
9060 } | 7916 } |
9061 | 7917 |
9062 /* | 7918 /* |
9063 * "call(func, arglist [, dict])" function | 7919 * "call(func, arglist [, dict])" function |
9064 */ | 7920 */ |
20636 /* | 19492 /* |
20637 * Get the length of the name of a function or internal variable. | 19493 * Get the length of the name of a function or internal variable. |
20638 * "arg" is advanced to the first non-white character after the name. | 19494 * "arg" is advanced to the first non-white character after the name. |
20639 * Return 0 if something is wrong. | 19495 * Return 0 if something is wrong. |
20640 */ | 19496 */ |
20641 static int | 19497 int |
20642 get_id_len(char_u **arg) | 19498 get_id_len(char_u **arg) |
20643 { | 19499 { |
20644 char_u *p; | 19500 char_u *p; |
20645 int len; | 19501 int len; |
20646 | 19502 |
20744 * start and end of the first magic braces item. | 19600 * start and end of the first magic braces item. |
20745 * "flags" can have FNE_INCL_BR and FNE_CHECK_START. | 19601 * "flags" can have FNE_INCL_BR and FNE_CHECK_START. |
20746 * Return a pointer to just after the name. Equal to "arg" if there is no | 19602 * Return a pointer to just after the name. Equal to "arg" if there is no |
20747 * valid name. | 19603 * valid name. |
20748 */ | 19604 */ |
20749 static char_u * | 19605 char_u * |
20750 find_name_end( | 19606 find_name_end( |
20751 char_u *arg, | 19607 char_u *arg, |
20752 char_u **expr_start, | 19608 char_u **expr_start, |
20753 char_u **expr_end, | 19609 char_u **expr_end, |
20754 int flags) | 19610 int flags) |
20898 | 19754 |
20899 /* | 19755 /* |
20900 * Return TRUE if character "c" can be used in a variable or function name. | 19756 * Return TRUE if character "c" can be used in a variable or function name. |
20901 * Does not include '{' or '}' for magic braces. | 19757 * Does not include '{' or '}' for magic braces. |
20902 */ | 19758 */ |
20903 static int | 19759 int |
20904 eval_isnamec(int c) | 19760 eval_isnamec(int c) |
20905 { | 19761 { |
20906 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR); | 19762 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR); |
20907 } | 19763 } |
20908 | 19764 |
20909 /* | 19765 /* |
20910 * Return TRUE if character "c" can be used as the first character in a | 19766 * Return TRUE if character "c" can be used as the first character in a |
20911 * variable or function name (excluding '{' and '}'). | 19767 * variable or function name (excluding '{' and '}'). |
20912 */ | 19768 */ |
20913 static int | 19769 int |
20914 eval_isnamec1(int c) | 19770 eval_isnamec1(int c) |
20915 { | 19771 { |
20916 return (ASCII_ISALPHA(c) || c == '_'); | 19772 return (ASCII_ISALPHA(c) || c == '_'); |
20917 } | 19773 } |
20918 | 19774 |
21236 /* | 20092 /* |
21237 * Handle expr[expr], expr[expr:expr] subscript and .name lookup. | 20093 * Handle expr[expr], expr[expr:expr] subscript and .name lookup. |
21238 * Also handle function call with Funcref variable: func(expr) | 20094 * Also handle function call with Funcref variable: func(expr) |
21239 * Can all be combined: dict.func(expr)[idx]['func'](expr) | 20095 * Can all be combined: dict.func(expr)[idx]['func'](expr) |
21240 */ | 20096 */ |
21241 static int | 20097 int |
21242 handle_subscript( | 20098 handle_subscript( |
21243 char_u **arg, | 20099 char_u **arg, |
21244 typval_T *rettv, | 20100 typval_T *rettv, |
21245 int evaluate, /* do more than finding the end */ | 20101 int evaluate, /* do more than finding the end */ |
21246 int verbose) /* give error messages */ | 20102 int verbose) /* give error messages */ |
21325 if (selfdict != NULL | 20181 if (selfdict != NULL |
21326 && (rettv->v_type == VAR_FUNC | 20182 && (rettv->v_type == VAR_FUNC |
21327 || (rettv->v_type == VAR_PARTIAL | 20183 || (rettv->v_type == VAR_PARTIAL |
21328 && (rettv->vval.v_partial->pt_auto | 20184 && (rettv->vval.v_partial->pt_auto |
21329 || rettv->vval.v_partial->pt_dict == NULL)))) | 20185 || rettv->vval.v_partial->pt_dict == NULL)))) |
21330 { | 20186 selfdict = make_partial(selfdict, rettv); |
21331 char_u *fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string | |
21332 : rettv->vval.v_partial->pt_name; | |
21333 char_u *tofree = NULL; | |
21334 ufunc_T *fp; | |
21335 char_u fname_buf[FLEN_FIXED + 1]; | |
21336 int error; | |
21337 | |
21338 /* Translate "s:func" to the stored function name. */ | |
21339 fname = fname_trans_sid(fname, fname_buf, &tofree, &error); | |
21340 fp = find_func(fname); | |
21341 vim_free(tofree); | |
21342 | |
21343 if (fp != NULL && (fp->uf_flags & FC_DICT)) | |
21344 { | |
21345 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T)); | |
21346 | |
21347 if (pt != NULL) | |
21348 { | |
21349 pt->pt_refcount = 1; | |
21350 pt->pt_dict = selfdict; | |
21351 pt->pt_auto = TRUE; | |
21352 selfdict = NULL; | |
21353 if (rettv->v_type == VAR_FUNC) | |
21354 { | |
21355 /* Just a function: Take over the function name and use | |
21356 * selfdict. */ | |
21357 pt->pt_name = rettv->vval.v_string; | |
21358 } | |
21359 else | |
21360 { | |
21361 partial_T *ret_pt = rettv->vval.v_partial; | |
21362 int i; | |
21363 | |
21364 /* Partial: copy the function name, use selfdict and copy | |
21365 * args. Can't take over name or args, the partial might | |
21366 * be referenced elsewhere. */ | |
21367 pt->pt_name = vim_strsave(ret_pt->pt_name); | |
21368 func_ref(pt->pt_name); | |
21369 if (ret_pt->pt_argc > 0) | |
21370 { | |
21371 pt->pt_argv = (typval_T *)alloc( | |
21372 sizeof(typval_T) * ret_pt->pt_argc); | |
21373 if (pt->pt_argv == NULL) | |
21374 /* out of memory: drop the arguments */ | |
21375 pt->pt_argc = 0; | |
21376 else | |
21377 { | |
21378 pt->pt_argc = ret_pt->pt_argc; | |
21379 for (i = 0; i < pt->pt_argc; i++) | |
21380 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]); | |
21381 } | |
21382 } | |
21383 partial_unref(ret_pt); | |
21384 } | |
21385 rettv->v_type = VAR_PARTIAL; | |
21386 rettv->vval.v_partial = pt; | |
21387 } | |
21388 } | |
21389 } | |
21390 | 20187 |
21391 dict_unref(selfdict); | 20188 dict_unref(selfdict); |
21392 return ret; | 20189 return ret; |
21393 } | 20190 } |
21394 | 20191 |
21807 * Return a pointer to it if found, NULL if not found. | 20604 * Return a pointer to it if found, NULL if not found. |
21808 * Careful: "a:0" variables don't have a name. | 20605 * Careful: "a:0" variables don't have a name. |
21809 * When "htp" is not NULL we are writing to the variable, set "htp" to the | 20606 * When "htp" is not NULL we are writing to the variable, set "htp" to the |
21810 * hashtab_T used. | 20607 * hashtab_T used. |
21811 */ | 20608 */ |
21812 static dictitem_T * | 20609 dictitem_T * |
21813 find_var(char_u *name, hashtab_T **htp, int no_autoload) | 20610 find_var(char_u *name, hashtab_T **htp, int no_autoload) |
21814 { | 20611 { |
21815 char_u *varname; | 20612 char_u *varname; |
21816 hashtab_T *ht; | 20613 hashtab_T *ht; |
21817 | 20614 |
21847 case 'b': return &curbuf->b_bufvar; | 20644 case 'b': return &curbuf->b_bufvar; |
21848 case 'w': return &curwin->w_winvar; | 20645 case 'w': return &curwin->w_winvar; |
21849 #ifdef FEAT_WINDOWS | 20646 #ifdef FEAT_WINDOWS |
21850 case 't': return &curtab->tp_winvar; | 20647 case 't': return &curtab->tp_winvar; |
21851 #endif | 20648 #endif |
21852 case 'l': return current_funccal == NULL | 20649 case 'l': return get_funccal_local_var(); |
21853 ? NULL : ¤t_funccal->l_vars_var; | 20650 case 'a': return get_funccal_args_var(); |
21854 case 'a': return current_funccal == NULL | |
21855 ? NULL : ¤t_funccal->l_avars_var; | |
21856 } | 20651 } |
21857 return NULL; | 20652 return NULL; |
21858 } | 20653 } |
21859 | 20654 |
21860 hi = hash_find(ht, varname); | 20655 hi = hash_find(ht, varname); |
21885 */ | 20680 */ |
21886 static hashtab_T * | 20681 static hashtab_T * |
21887 find_var_ht(char_u *name, char_u **varname) | 20682 find_var_ht(char_u *name, char_u **varname) |
21888 { | 20683 { |
21889 hashitem_T *hi; | 20684 hashitem_T *hi; |
20685 hashtab_T *ht; | |
21890 | 20686 |
21891 if (name[0] == NUL) | 20687 if (name[0] == NUL) |
21892 return NULL; | 20688 return NULL; |
21893 if (name[1] != ':') | 20689 if (name[1] != ':') |
21894 { | 20690 { |
21900 /* "version" is "v:version" in all scopes */ | 20696 /* "version" is "v:version" in all scopes */ |
21901 hi = hash_find(&compat_hashtab, name); | 20697 hi = hash_find(&compat_hashtab, name); |
21902 if (!HASHITEM_EMPTY(hi)) | 20698 if (!HASHITEM_EMPTY(hi)) |
21903 return &compat_hashtab; | 20699 return &compat_hashtab; |
21904 | 20700 |
21905 if (current_funccal == NULL) | 20701 ht = get_funccal_local_ht(); |
20702 if (ht == NULL) | |
21906 return &globvarht; /* global variable */ | 20703 return &globvarht; /* global variable */ |
21907 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */ | 20704 return ht; /* local variable */ |
21908 } | 20705 } |
21909 *varname = name + 2; | 20706 *varname = name + 2; |
21910 if (*name == 'g') /* global variable */ | 20707 if (*name == 'g') /* global variable */ |
21911 return &globvarht; | 20708 return &globvarht; |
21912 /* There must be no ':' or '#' in the rest of the name, unless g: is used | 20709 /* There must be no ':' or '#' in the rest of the name, unless g: is used |
21922 if (*name == 't') /* tab page variable */ | 20719 if (*name == 't') /* tab page variable */ |
21923 return &curtab->tp_vars->dv_hashtab; | 20720 return &curtab->tp_vars->dv_hashtab; |
21924 #endif | 20721 #endif |
21925 if (*name == 'v') /* v: variable */ | 20722 if (*name == 'v') /* v: variable */ |
21926 return &vimvarht; | 20723 return &vimvarht; |
21927 if (*name == 'a' && current_funccal != NULL) /* function argument */ | 20724 if (*name == 'a') /* a: function argument */ |
21928 return &get_funccal()->l_avars.dv_hashtab; | 20725 return get_funccal_args_ht(); |
21929 if (*name == 'l' && current_funccal != NULL) /* local function variable */ | 20726 if (*name == 'l') /* l: local function variable */ |
21930 return &get_funccal()->l_vars.dv_hashtab; | 20727 return get_funccal_local_ht(); |
21931 if (*name == 's' /* script variable */ | 20728 if (*name == 's' /* script variable */ |
21932 && current_SID > 0 && current_SID <= ga_scripts.ga_len) | 20729 && current_SID > 0 && current_SID <= ga_scripts.ga_len) |
21933 return &SCRIPT_VARS(current_SID); | 20730 return &SCRIPT_VARS(current_SID); |
21934 return NULL; | 20731 return NULL; |
21935 } | |
21936 | |
21937 /* | |
21938 * Get function call environment based on bactrace debug level | |
21939 */ | |
21940 static funccall_T * | |
21941 get_funccal(void) | |
21942 { | |
21943 int i; | |
21944 funccall_T *funccal; | |
21945 funccall_T *temp_funccal; | |
21946 | |
21947 funccal = current_funccal; | |
21948 if (debug_backtrace_level > 0) | |
21949 { | |
21950 for (i = 0; i < debug_backtrace_level; i++) | |
21951 { | |
21952 temp_funccal = funccal->caller; | |
21953 if (temp_funccal) | |
21954 funccal = temp_funccal; | |
21955 else | |
21956 /* backtrace level overflow. reset to max */ | |
21957 debug_backtrace_level = i; | |
21958 } | |
21959 } | |
21960 return funccal; | |
21961 } | 20732 } |
21962 | 20733 |
21963 /* | 20734 /* |
21964 * Get the string value of a (global/local) variable. | 20735 * Get the string value of a (global/local) variable. |
21965 * Note: see get_tv_string() for how long the pointer remains valid. | 20736 * Note: see get_tv_string() for how long the pointer remains valid. |
22054 } | 20825 } |
22055 | 20826 |
22056 /* | 20827 /* |
22057 * Like vars_clear(), but only free the value if "free_val" is TRUE. | 20828 * Like vars_clear(), but only free the value if "free_val" is TRUE. |
22058 */ | 20829 */ |
22059 static void | 20830 void |
22060 vars_clear_ext(hashtab_T *ht, int free_val) | 20831 vars_clear_ext(hashtab_T *ht, int free_val) |
22061 { | 20832 { |
22062 int todo; | 20833 int todo; |
22063 hashitem_T *hi; | 20834 hashitem_T *hi; |
22064 dictitem_T *v; | 20835 dictitem_T *v; |
22780 ++p; | 21551 ++p; |
22781 return p; | 21552 return p; |
22782 } | 21553 } |
22783 | 21554 |
22784 /* | 21555 /* |
22785 * ":function" | |
22786 */ | |
22787 void | |
22788 ex_function(exarg_T *eap) | |
22789 { | |
22790 char_u *theline; | |
22791 int j; | |
22792 int c; | |
22793 int saved_did_emsg; | |
22794 int saved_wait_return = need_wait_return; | |
22795 char_u *name = NULL; | |
22796 char_u *p; | |
22797 char_u *arg; | |
22798 char_u *line_arg = NULL; | |
22799 garray_T newargs; | |
22800 garray_T newlines; | |
22801 int varargs = FALSE; | |
22802 int flags = 0; | |
22803 ufunc_T *fp; | |
22804 int indent; | |
22805 int nesting; | |
22806 char_u *skip_until = NULL; | |
22807 dictitem_T *v; | |
22808 funcdict_T fudi; | |
22809 static int func_nr = 0; /* number for nameless function */ | |
22810 int paren; | |
22811 hashtab_T *ht; | |
22812 int todo; | |
22813 hashitem_T *hi; | |
22814 int sourcing_lnum_off; | |
22815 | |
22816 /* | |
22817 * ":function" without argument: list functions. | |
22818 */ | |
22819 if (ends_excmd(*eap->arg)) | |
22820 { | |
22821 if (!eap->skip) | |
22822 { | |
22823 todo = (int)func_hashtab.ht_used; | |
22824 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi) | |
22825 { | |
22826 if (!HASHITEM_EMPTY(hi)) | |
22827 { | |
22828 --todo; | |
22829 fp = HI2UF(hi); | |
22830 if (!isdigit(*fp->uf_name)) | |
22831 list_func_head(fp, FALSE); | |
22832 } | |
22833 } | |
22834 } | |
22835 eap->nextcmd = check_nextcmd(eap->arg); | |
22836 return; | |
22837 } | |
22838 | |
22839 /* | |
22840 * ":function /pat": list functions matching pattern. | |
22841 */ | |
22842 if (*eap->arg == '/') | |
22843 { | |
22844 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL); | |
22845 if (!eap->skip) | |
22846 { | |
22847 regmatch_T regmatch; | |
22848 | |
22849 c = *p; | |
22850 *p = NUL; | |
22851 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC); | |
22852 *p = c; | |
22853 if (regmatch.regprog != NULL) | |
22854 { | |
22855 regmatch.rm_ic = p_ic; | |
22856 | |
22857 todo = (int)func_hashtab.ht_used; | |
22858 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi) | |
22859 { | |
22860 if (!HASHITEM_EMPTY(hi)) | |
22861 { | |
22862 --todo; | |
22863 fp = HI2UF(hi); | |
22864 if (!isdigit(*fp->uf_name) | |
22865 && vim_regexec(®match, fp->uf_name, 0)) | |
22866 list_func_head(fp, FALSE); | |
22867 } | |
22868 } | |
22869 vim_regfree(regmatch.regprog); | |
22870 } | |
22871 } | |
22872 if (*p == '/') | |
22873 ++p; | |
22874 eap->nextcmd = check_nextcmd(p); | |
22875 return; | |
22876 } | |
22877 | |
22878 /* | |
22879 * Get the function name. There are these situations: | |
22880 * func normal function name | |
22881 * "name" == func, "fudi.fd_dict" == NULL | |
22882 * dict.func new dictionary entry | |
22883 * "name" == NULL, "fudi.fd_dict" set, | |
22884 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func | |
22885 * dict.func existing dict entry with a Funcref | |
22886 * "name" == func, "fudi.fd_dict" set, | |
22887 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL | |
22888 * dict.func existing dict entry that's not a Funcref | |
22889 * "name" == NULL, "fudi.fd_dict" set, | |
22890 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL | |
22891 * s:func script-local function name | |
22892 * g:func global function name, same as "func" | |
22893 */ | |
22894 p = eap->arg; | |
22895 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL); | |
22896 paren = (vim_strchr(p, '(') != NULL); | |
22897 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip) | |
22898 { | |
22899 /* | |
22900 * Return on an invalid expression in braces, unless the expression | |
22901 * evaluation has been cancelled due to an aborting error, an | |
22902 * interrupt, or an exception. | |
22903 */ | |
22904 if (!aborting()) | |
22905 { | |
22906 if (!eap->skip && fudi.fd_newkey != NULL) | |
22907 EMSG2(_(e_dictkey), fudi.fd_newkey); | |
22908 vim_free(fudi.fd_newkey); | |
22909 return; | |
22910 } | |
22911 else | |
22912 eap->skip = TRUE; | |
22913 } | |
22914 | |
22915 /* An error in a function call during evaluation of an expression in magic | |
22916 * braces should not cause the function not to be defined. */ | |
22917 saved_did_emsg = did_emsg; | |
22918 did_emsg = FALSE; | |
22919 | |
22920 /* | |
22921 * ":function func" with only function name: list function. | |
22922 */ | |
22923 if (!paren) | |
22924 { | |
22925 if (!ends_excmd(*skipwhite(p))) | |
22926 { | |
22927 EMSG(_(e_trailing)); | |
22928 goto ret_free; | |
22929 } | |
22930 eap->nextcmd = check_nextcmd(p); | |
22931 if (eap->nextcmd != NULL) | |
22932 *p = NUL; | |
22933 if (!eap->skip && !got_int) | |
22934 { | |
22935 fp = find_func(name); | |
22936 if (fp != NULL) | |
22937 { | |
22938 list_func_head(fp, TRUE); | |
22939 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j) | |
22940 { | |
22941 if (FUNCLINE(fp, j) == NULL) | |
22942 continue; | |
22943 msg_putchar('\n'); | |
22944 msg_outnum((long)(j + 1)); | |
22945 if (j < 9) | |
22946 msg_putchar(' '); | |
22947 if (j < 99) | |
22948 msg_putchar(' '); | |
22949 msg_prt_line(FUNCLINE(fp, j), FALSE); | |
22950 out_flush(); /* show a line at a time */ | |
22951 ui_breakcheck(); | |
22952 } | |
22953 if (!got_int) | |
22954 { | |
22955 msg_putchar('\n'); | |
22956 msg_puts((char_u *)" endfunction"); | |
22957 } | |
22958 } | |
22959 else | |
22960 emsg_funcname(N_("E123: Undefined function: %s"), name); | |
22961 } | |
22962 goto ret_free; | |
22963 } | |
22964 | |
22965 /* | |
22966 * ":function name(arg1, arg2)" Define function. | |
22967 */ | |
22968 p = skipwhite(p); | |
22969 if (*p != '(') | |
22970 { | |
22971 if (!eap->skip) | |
22972 { | |
22973 EMSG2(_("E124: Missing '(': %s"), eap->arg); | |
22974 goto ret_free; | |
22975 } | |
22976 /* attempt to continue by skipping some text */ | |
22977 if (vim_strchr(p, '(') != NULL) | |
22978 p = vim_strchr(p, '('); | |
22979 } | |
22980 p = skipwhite(p + 1); | |
22981 | |
22982 ga_init2(&newlines, (int)sizeof(char_u *), 3); | |
22983 | |
22984 if (!eap->skip) | |
22985 { | |
22986 /* Check the name of the function. Unless it's a dictionary function | |
22987 * (that we are overwriting). */ | |
22988 if (name != NULL) | |
22989 arg = name; | |
22990 else | |
22991 arg = fudi.fd_newkey; | |
22992 if (arg != NULL && (fudi.fd_di == NULL | |
22993 || (fudi.fd_di->di_tv.v_type != VAR_FUNC | |
22994 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL))) | |
22995 { | |
22996 if (*arg == K_SPECIAL) | |
22997 j = 3; | |
22998 else | |
22999 j = 0; | |
23000 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j]) | |
23001 : eval_isnamec(arg[j]))) | |
23002 ++j; | |
23003 if (arg[j] != NUL) | |
23004 emsg_funcname((char *)e_invarg2, arg); | |
23005 } | |
23006 /* Disallow using the g: dict. */ | |
23007 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE) | |
23008 EMSG(_("E862: Cannot use g: here")); | |
23009 } | |
23010 | |
23011 if (get_function_args(&p, ')', &newargs, &varargs, eap->skip) == FAIL) | |
23012 goto errret_2; | |
23013 | |
23014 /* find extra arguments "range", "dict" and "abort" */ | |
23015 for (;;) | |
23016 { | |
23017 p = skipwhite(p); | |
23018 if (STRNCMP(p, "range", 5) == 0) | |
23019 { | |
23020 flags |= FC_RANGE; | |
23021 p += 5; | |
23022 } | |
23023 else if (STRNCMP(p, "dict", 4) == 0) | |
23024 { | |
23025 flags |= FC_DICT; | |
23026 p += 4; | |
23027 } | |
23028 else if (STRNCMP(p, "abort", 5) == 0) | |
23029 { | |
23030 flags |= FC_ABORT; | |
23031 p += 5; | |
23032 } | |
23033 else | |
23034 break; | |
23035 } | |
23036 | |
23037 /* When there is a line break use what follows for the function body. | |
23038 * Makes 'exe "func Test()\n...\nendfunc"' work. */ | |
23039 if (*p == '\n') | |
23040 line_arg = p + 1; | |
23041 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg) | |
23042 EMSG(_(e_trailing)); | |
23043 | |
23044 /* | |
23045 * Read the body of the function, until ":endfunction" is found. | |
23046 */ | |
23047 if (KeyTyped) | |
23048 { | |
23049 /* Check if the function already exists, don't let the user type the | |
23050 * whole function before telling him it doesn't work! For a script we | |
23051 * need to skip the body to be able to find what follows. */ | |
23052 if (!eap->skip && !eap->forceit) | |
23053 { | |
23054 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL) | |
23055 EMSG(_(e_funcdict)); | |
23056 else if (name != NULL && find_func(name) != NULL) | |
23057 emsg_funcname(e_funcexts, name); | |
23058 } | |
23059 | |
23060 if (!eap->skip && did_emsg) | |
23061 goto erret; | |
23062 | |
23063 msg_putchar('\n'); /* don't overwrite the function name */ | |
23064 cmdline_row = msg_row; | |
23065 } | |
23066 | |
23067 indent = 2; | |
23068 nesting = 0; | |
23069 for (;;) | |
23070 { | |
23071 if (KeyTyped) | |
23072 { | |
23073 msg_scroll = TRUE; | |
23074 saved_wait_return = FALSE; | |
23075 } | |
23076 need_wait_return = FALSE; | |
23077 sourcing_lnum_off = sourcing_lnum; | |
23078 | |
23079 if (line_arg != NULL) | |
23080 { | |
23081 /* Use eap->arg, split up in parts by line breaks. */ | |
23082 theline = line_arg; | |
23083 p = vim_strchr(theline, '\n'); | |
23084 if (p == NULL) | |
23085 line_arg += STRLEN(line_arg); | |
23086 else | |
23087 { | |
23088 *p = NUL; | |
23089 line_arg = p + 1; | |
23090 } | |
23091 } | |
23092 else if (eap->getline == NULL) | |
23093 theline = getcmdline(':', 0L, indent); | |
23094 else | |
23095 theline = eap->getline(':', eap->cookie, indent); | |
23096 if (KeyTyped) | |
23097 lines_left = Rows - 1; | |
23098 if (theline == NULL) | |
23099 { | |
23100 EMSG(_("E126: Missing :endfunction")); | |
23101 goto erret; | |
23102 } | |
23103 | |
23104 /* Detect line continuation: sourcing_lnum increased more than one. */ | |
23105 if (sourcing_lnum > sourcing_lnum_off + 1) | |
23106 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1; | |
23107 else | |
23108 sourcing_lnum_off = 0; | |
23109 | |
23110 if (skip_until != NULL) | |
23111 { | |
23112 /* between ":append" and "." and between ":python <<EOF" and "EOF" | |
23113 * don't check for ":endfunc". */ | |
23114 if (STRCMP(theline, skip_until) == 0) | |
23115 { | |
23116 vim_free(skip_until); | |
23117 skip_until = NULL; | |
23118 } | |
23119 } | |
23120 else | |
23121 { | |
23122 /* skip ':' and blanks*/ | |
23123 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p) | |
23124 ; | |
23125 | |
23126 /* Check for "endfunction". */ | |
23127 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0) | |
23128 { | |
23129 if (line_arg == NULL) | |
23130 vim_free(theline); | |
23131 break; | |
23132 } | |
23133 | |
23134 /* Increase indent inside "if", "while", "for" and "try", decrease | |
23135 * at "end". */ | |
23136 if (indent > 2 && STRNCMP(p, "end", 3) == 0) | |
23137 indent -= 2; | |
23138 else if (STRNCMP(p, "if", 2) == 0 | |
23139 || STRNCMP(p, "wh", 2) == 0 | |
23140 || STRNCMP(p, "for", 3) == 0 | |
23141 || STRNCMP(p, "try", 3) == 0) | |
23142 indent += 2; | |
23143 | |
23144 /* Check for defining a function inside this function. */ | |
23145 if (checkforcmd(&p, "function", 2)) | |
23146 { | |
23147 if (*p == '!') | |
23148 p = skipwhite(p + 1); | |
23149 p += eval_fname_script(p); | |
23150 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL)); | |
23151 if (*skipwhite(p) == '(') | |
23152 { | |
23153 ++nesting; | |
23154 indent += 2; | |
23155 } | |
23156 } | |
23157 | |
23158 /* Check for ":append" or ":insert". */ | |
23159 p = skip_range(p, NULL); | |
23160 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p')) | |
23161 || (p[0] == 'i' | |
23162 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n' | |
23163 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's')))))) | |
23164 skip_until = vim_strsave((char_u *)"."); | |
23165 | |
23166 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */ | |
23167 arg = skipwhite(skiptowhite(p)); | |
23168 if (arg[0] == '<' && arg[1] =='<' | |
23169 && ((p[0] == 'p' && p[1] == 'y' | |
23170 && (!ASCII_ISALPHA(p[2]) || p[2] == 't')) | |
23171 || (p[0] == 'p' && p[1] == 'e' | |
23172 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r')) | |
23173 || (p[0] == 't' && p[1] == 'c' | |
23174 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l')) | |
23175 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a' | |
23176 && !ASCII_ISALPHA(p[3])) | |
23177 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b' | |
23178 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y')) | |
23179 || (p[0] == 'm' && p[1] == 'z' | |
23180 && (!ASCII_ISALPHA(p[2]) || p[2] == 's')) | |
23181 )) | |
23182 { | |
23183 /* ":python <<" continues until a dot, like ":append" */ | |
23184 p = skipwhite(arg + 2); | |
23185 if (*p == NUL) | |
23186 skip_until = vim_strsave((char_u *)"."); | |
23187 else | |
23188 skip_until = vim_strsave(p); | |
23189 } | |
23190 } | |
23191 | |
23192 /* Add the line to the function. */ | |
23193 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL) | |
23194 { | |
23195 if (line_arg == NULL) | |
23196 vim_free(theline); | |
23197 goto erret; | |
23198 } | |
23199 | |
23200 /* Copy the line to newly allocated memory. get_one_sourceline() | |
23201 * allocates 250 bytes per line, this saves 80% on average. The cost | |
23202 * is an extra alloc/free. */ | |
23203 p = vim_strsave(theline); | |
23204 if (p != NULL) | |
23205 { | |
23206 if (line_arg == NULL) | |
23207 vim_free(theline); | |
23208 theline = p; | |
23209 } | |
23210 | |
23211 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline; | |
23212 | |
23213 /* Add NULL lines for continuation lines, so that the line count is | |
23214 * equal to the index in the growarray. */ | |
23215 while (sourcing_lnum_off-- > 0) | |
23216 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL; | |
23217 | |
23218 /* Check for end of eap->arg. */ | |
23219 if (line_arg != NULL && *line_arg == NUL) | |
23220 line_arg = NULL; | |
23221 } | |
23222 | |
23223 /* Don't define the function when skipping commands or when an error was | |
23224 * detected. */ | |
23225 if (eap->skip || did_emsg) | |
23226 goto erret; | |
23227 | |
23228 /* | |
23229 * If there are no errors, add the function | |
23230 */ | |
23231 if (fudi.fd_dict == NULL) | |
23232 { | |
23233 v = find_var(name, &ht, FALSE); | |
23234 if (v != NULL && v->di_tv.v_type == VAR_FUNC) | |
23235 { | |
23236 emsg_funcname(N_("E707: Function name conflicts with variable: %s"), | |
23237 name); | |
23238 goto erret; | |
23239 } | |
23240 | |
23241 fp = find_func(name); | |
23242 if (fp != NULL) | |
23243 { | |
23244 if (!eap->forceit) | |
23245 { | |
23246 emsg_funcname(e_funcexts, name); | |
23247 goto erret; | |
23248 } | |
23249 if (fp->uf_calls > 0) | |
23250 { | |
23251 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"), | |
23252 name); | |
23253 goto erret; | |
23254 } | |
23255 /* redefine existing function */ | |
23256 ga_clear_strings(&(fp->uf_args)); | |
23257 ga_clear_strings(&(fp->uf_lines)); | |
23258 vim_free(name); | |
23259 name = NULL; | |
23260 } | |
23261 } | |
23262 else | |
23263 { | |
23264 char numbuf[20]; | |
23265 | |
23266 fp = NULL; | |
23267 if (fudi.fd_newkey == NULL && !eap->forceit) | |
23268 { | |
23269 EMSG(_(e_funcdict)); | |
23270 goto erret; | |
23271 } | |
23272 if (fudi.fd_di == NULL) | |
23273 { | |
23274 /* Can't add a function to a locked dictionary */ | |
23275 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE)) | |
23276 goto erret; | |
23277 } | |
23278 /* Can't change an existing function if it is locked */ | |
23279 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE)) | |
23280 goto erret; | |
23281 | |
23282 /* Give the function a sequential number. Can only be used with a | |
23283 * Funcref! */ | |
23284 vim_free(name); | |
23285 sprintf(numbuf, "%d", ++func_nr); | |
23286 name = vim_strsave((char_u *)numbuf); | |
23287 if (name == NULL) | |
23288 goto erret; | |
23289 } | |
23290 | |
23291 if (fp == NULL) | |
23292 { | |
23293 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL) | |
23294 { | |
23295 int slen, plen; | |
23296 char_u *scriptname; | |
23297 | |
23298 /* Check that the autoload name matches the script name. */ | |
23299 j = FAIL; | |
23300 if (sourcing_name != NULL) | |
23301 { | |
23302 scriptname = autoload_name(name); | |
23303 if (scriptname != NULL) | |
23304 { | |
23305 p = vim_strchr(scriptname, '/'); | |
23306 plen = (int)STRLEN(p); | |
23307 slen = (int)STRLEN(sourcing_name); | |
23308 if (slen > plen && fnamecmp(p, | |
23309 sourcing_name + slen - plen) == 0) | |
23310 j = OK; | |
23311 vim_free(scriptname); | |
23312 } | |
23313 } | |
23314 if (j == FAIL) | |
23315 { | |
23316 EMSG2(_("E746: Function name does not match script file name: %s"), name); | |
23317 goto erret; | |
23318 } | |
23319 } | |
23320 | |
23321 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name))); | |
23322 if (fp == NULL) | |
23323 goto erret; | |
23324 | |
23325 if (fudi.fd_dict != NULL) | |
23326 { | |
23327 if (fudi.fd_di == NULL) | |
23328 { | |
23329 /* add new dict entry */ | |
23330 fudi.fd_di = dictitem_alloc(fudi.fd_newkey); | |
23331 if (fudi.fd_di == NULL) | |
23332 { | |
23333 vim_free(fp); | |
23334 goto erret; | |
23335 } | |
23336 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL) | |
23337 { | |
23338 vim_free(fudi.fd_di); | |
23339 vim_free(fp); | |
23340 goto erret; | |
23341 } | |
23342 } | |
23343 else | |
23344 /* overwrite existing dict entry */ | |
23345 clear_tv(&fudi.fd_di->di_tv); | |
23346 fudi.fd_di->di_tv.v_type = VAR_FUNC; | |
23347 fudi.fd_di->di_tv.v_lock = 0; | |
23348 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name); | |
23349 fp->uf_refcount = 1; | |
23350 | |
23351 /* behave like "dict" was used */ | |
23352 flags |= FC_DICT; | |
23353 } | |
23354 | |
23355 /* insert the new function in the function list */ | |
23356 STRCPY(fp->uf_name, name); | |
23357 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL) | |
23358 { | |
23359 vim_free(fp); | |
23360 goto erret; | |
23361 } | |
23362 } | |
23363 fp->uf_args = newargs; | |
23364 fp->uf_lines = newlines; | |
23365 #ifdef FEAT_PROFILE | |
23366 fp->uf_tml_count = NULL; | |
23367 fp->uf_tml_total = NULL; | |
23368 fp->uf_tml_self = NULL; | |
23369 fp->uf_profiling = FALSE; | |
23370 if (prof_def_func()) | |
23371 func_do_profile(fp); | |
23372 #endif | |
23373 fp->uf_varargs = varargs; | |
23374 fp->uf_flags = flags; | |
23375 fp->uf_calls = 0; | |
23376 fp->uf_script_ID = current_SID; | |
23377 goto ret_free; | |
23378 | |
23379 erret: | |
23380 ga_clear_strings(&newargs); | |
23381 errret_2: | |
23382 ga_clear_strings(&newlines); | |
23383 ret_free: | |
23384 vim_free(skip_until); | |
23385 vim_free(fudi.fd_newkey); | |
23386 vim_free(name); | |
23387 did_emsg |= saved_did_emsg; | |
23388 need_wait_return |= saved_wait_return; | |
23389 } | |
23390 | |
23391 /* | |
23392 * Get a function name, translating "<SID>" and "<SNR>". | |
23393 * Also handles a Funcref in a List or Dictionary. | |
23394 * Returns the function name in allocated memory, or NULL for failure. | |
23395 * flags: | |
23396 * TFN_INT: internal function name OK | |
23397 * TFN_QUIET: be quiet | |
23398 * TFN_NO_AUTOLOAD: do not use script autoloading | |
23399 * Advances "pp" to just after the function name (if no error). | |
23400 */ | |
23401 static char_u * | |
23402 trans_function_name( | |
23403 char_u **pp, | |
23404 int skip, /* only find the end, don't evaluate */ | |
23405 int flags, | |
23406 funcdict_T *fdp, /* return: info about dictionary used */ | |
23407 partial_T **partial) /* return: partial of a FuncRef */ | |
23408 { | |
23409 char_u *name = NULL; | |
23410 char_u *start; | |
23411 char_u *end; | |
23412 int lead; | |
23413 char_u sid_buf[20]; | |
23414 int len; | |
23415 lval_T lv; | |
23416 | |
23417 if (fdp != NULL) | |
23418 vim_memset(fdp, 0, sizeof(funcdict_T)); | |
23419 start = *pp; | |
23420 | |
23421 /* Check for hard coded <SNR>: already translated function ID (from a user | |
23422 * command). */ | |
23423 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA | |
23424 && (*pp)[2] == (int)KE_SNR) | |
23425 { | |
23426 *pp += 3; | |
23427 len = get_id_len(pp) + 3; | |
23428 return vim_strnsave(start, len); | |
23429 } | |
23430 | |
23431 /* A name starting with "<SID>" or "<SNR>" is local to a script. But | |
23432 * don't skip over "s:", get_lval() needs it for "s:dict.func". */ | |
23433 lead = eval_fname_script(start); | |
23434 if (lead > 2) | |
23435 start += lead; | |
23436 | |
23437 /* Note that TFN_ flags use the same values as GLV_ flags. */ | |
23438 end = get_lval(start, NULL, &lv, FALSE, skip, flags, | |
23439 lead > 2 ? 0 : FNE_CHECK_START); | |
23440 if (end == start) | |
23441 { | |
23442 if (!skip) | |
23443 EMSG(_("E129: Function name required")); | |
23444 goto theend; | |
23445 } | |
23446 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range))) | |
23447 { | |
23448 /* | |
23449 * Report an invalid expression in braces, unless the expression | |
23450 * evaluation has been cancelled due to an aborting error, an | |
23451 * interrupt, or an exception. | |
23452 */ | |
23453 if (!aborting()) | |
23454 { | |
23455 if (end != NULL) | |
23456 EMSG2(_(e_invarg2), start); | |
23457 } | |
23458 else | |
23459 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR); | |
23460 goto theend; | |
23461 } | |
23462 | |
23463 if (lv.ll_tv != NULL) | |
23464 { | |
23465 if (fdp != NULL) | |
23466 { | |
23467 fdp->fd_dict = lv.ll_dict; | |
23468 fdp->fd_newkey = lv.ll_newkey; | |
23469 lv.ll_newkey = NULL; | |
23470 fdp->fd_di = lv.ll_di; | |
23471 } | |
23472 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL) | |
23473 { | |
23474 name = vim_strsave(lv.ll_tv->vval.v_string); | |
23475 *pp = end; | |
23476 } | |
23477 else if (lv.ll_tv->v_type == VAR_PARTIAL | |
23478 && lv.ll_tv->vval.v_partial != NULL) | |
23479 { | |
23480 name = vim_strsave(lv.ll_tv->vval.v_partial->pt_name); | |
23481 *pp = end; | |
23482 if (partial != NULL) | |
23483 *partial = lv.ll_tv->vval.v_partial; | |
23484 } | |
23485 else | |
23486 { | |
23487 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL | |
23488 || lv.ll_dict == NULL || fdp->fd_newkey == NULL)) | |
23489 EMSG(_(e_funcref)); | |
23490 else | |
23491 *pp = end; | |
23492 name = NULL; | |
23493 } | |
23494 goto theend; | |
23495 } | |
23496 | |
23497 if (lv.ll_name == NULL) | |
23498 { | |
23499 /* Error found, but continue after the function name. */ | |
23500 *pp = end; | |
23501 goto theend; | |
23502 } | |
23503 | |
23504 /* Check if the name is a Funcref. If so, use the value. */ | |
23505 if (lv.ll_exp_name != NULL) | |
23506 { | |
23507 len = (int)STRLEN(lv.ll_exp_name); | |
23508 name = deref_func_name(lv.ll_exp_name, &len, partial, | |
23509 flags & TFN_NO_AUTOLOAD); | |
23510 if (name == lv.ll_exp_name) | |
23511 name = NULL; | |
23512 } | |
23513 else | |
23514 { | |
23515 len = (int)(end - *pp); | |
23516 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD); | |
23517 if (name == *pp) | |
23518 name = NULL; | |
23519 } | |
23520 if (name != NULL) | |
23521 { | |
23522 name = vim_strsave(name); | |
23523 *pp = end; | |
23524 if (STRNCMP(name, "<SNR>", 5) == 0) | |
23525 { | |
23526 /* Change "<SNR>" to the byte sequence. */ | |
23527 name[0] = K_SPECIAL; | |
23528 name[1] = KS_EXTRA; | |
23529 name[2] = (int)KE_SNR; | |
23530 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1); | |
23531 } | |
23532 goto theend; | |
23533 } | |
23534 | |
23535 if (lv.ll_exp_name != NULL) | |
23536 { | |
23537 len = (int)STRLEN(lv.ll_exp_name); | |
23538 if (lead <= 2 && lv.ll_name == lv.ll_exp_name | |
23539 && STRNCMP(lv.ll_name, "s:", 2) == 0) | |
23540 { | |
23541 /* When there was "s:" already or the name expanded to get a | |
23542 * leading "s:" then remove it. */ | |
23543 lv.ll_name += 2; | |
23544 len -= 2; | |
23545 lead = 2; | |
23546 } | |
23547 } | |
23548 else | |
23549 { | |
23550 /* skip over "s:" and "g:" */ | |
23551 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':')) | |
23552 lv.ll_name += 2; | |
23553 len = (int)(end - lv.ll_name); | |
23554 } | |
23555 | |
23556 /* | |
23557 * Copy the function name to allocated memory. | |
23558 * Accept <SID>name() inside a script, translate into <SNR>123_name(). | |
23559 * Accept <SNR>123_name() outside a script. | |
23560 */ | |
23561 if (skip) | |
23562 lead = 0; /* do nothing */ | |
23563 else if (lead > 0) | |
23564 { | |
23565 lead = 3; | |
23566 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name)) | |
23567 || eval_fname_sid(*pp)) | |
23568 { | |
23569 /* It's "s:" or "<SID>" */ | |
23570 if (current_SID <= 0) | |
23571 { | |
23572 EMSG(_(e_usingsid)); | |
23573 goto theend; | |
23574 } | |
23575 sprintf((char *)sid_buf, "%ld_", (long)current_SID); | |
23576 lead += (int)STRLEN(sid_buf); | |
23577 } | |
23578 } | |
23579 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len)) | |
23580 { | |
23581 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"), | |
23582 start); | |
23583 goto theend; | |
23584 } | |
23585 if (!skip && !(flags & TFN_QUIET)) | |
23586 { | |
23587 char_u *cp = vim_strchr(lv.ll_name, ':'); | |
23588 | |
23589 if (cp != NULL && cp < end) | |
23590 { | |
23591 EMSG2(_("E884: Function name cannot contain a colon: %s"), start); | |
23592 goto theend; | |
23593 } | |
23594 } | |
23595 | |
23596 name = alloc((unsigned)(len + lead + 1)); | |
23597 if (name != NULL) | |
23598 { | |
23599 if (lead > 0) | |
23600 { | |
23601 name[0] = K_SPECIAL; | |
23602 name[1] = KS_EXTRA; | |
23603 name[2] = (int)KE_SNR; | |
23604 if (lead > 3) /* If it's "<SID>" */ | |
23605 STRCPY(name + 3, sid_buf); | |
23606 } | |
23607 mch_memmove(name + lead, lv.ll_name, (size_t)len); | |
23608 name[lead + len] = NUL; | |
23609 } | |
23610 *pp = end; | |
23611 | |
23612 theend: | |
23613 clear_lval(&lv); | |
23614 return name; | |
23615 } | |
23616 | |
23617 /* | |
23618 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case). | |
23619 * Return 2 if "p" starts with "s:". | |
23620 * Return 0 otherwise. | |
23621 */ | |
23622 static int | |
23623 eval_fname_script(char_u *p) | |
23624 { | |
23625 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with | |
23626 * the standard library function. */ | |
23627 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0 | |
23628 || MB_STRNICMP(p + 1, "SNR>", 4) == 0)) | |
23629 return 5; | |
23630 if (p[0] == 's' && p[1] == ':') | |
23631 return 2; | |
23632 return 0; | |
23633 } | |
23634 | |
23635 /* | |
23636 * Return TRUE if "p" starts with "<SID>" or "s:". | |
23637 * Only works if eval_fname_script() returned non-zero for "p"! | |
23638 */ | |
23639 static int | |
23640 eval_fname_sid(char_u *p) | |
23641 { | |
23642 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I'); | |
23643 } | |
23644 | |
23645 /* | |
23646 * List the head of the function: "name(arg1, arg2)". | |
23647 */ | |
23648 static void | |
23649 list_func_head(ufunc_T *fp, int indent) | |
23650 { | |
23651 int j; | |
23652 | |
23653 msg_start(); | |
23654 if (indent) | |
23655 MSG_PUTS(" "); | |
23656 MSG_PUTS("function "); | |
23657 if (fp->uf_name[0] == K_SPECIAL) | |
23658 { | |
23659 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8)); | |
23660 msg_puts(fp->uf_name + 3); | |
23661 } | |
23662 else | |
23663 msg_puts(fp->uf_name); | |
23664 msg_putchar('('); | |
23665 for (j = 0; j < fp->uf_args.ga_len; ++j) | |
23666 { | |
23667 if (j) | |
23668 MSG_PUTS(", "); | |
23669 msg_puts(FUNCARG(fp, j)); | |
23670 } | |
23671 if (fp->uf_varargs) | |
23672 { | |
23673 if (j) | |
23674 MSG_PUTS(", "); | |
23675 MSG_PUTS("..."); | |
23676 } | |
23677 msg_putchar(')'); | |
23678 if (fp->uf_flags & FC_ABORT) | |
23679 MSG_PUTS(" abort"); | |
23680 if (fp->uf_flags & FC_RANGE) | |
23681 MSG_PUTS(" range"); | |
23682 if (fp->uf_flags & FC_DICT) | |
23683 MSG_PUTS(" dict"); | |
23684 msg_clr_eos(); | |
23685 if (p_verbose > 0) | |
23686 last_set_msg(fp->uf_script_ID); | |
23687 } | |
23688 | |
23689 /* | |
23690 * Find a function by name, return pointer to it in ufuncs. | |
23691 * Return NULL for unknown function. | |
23692 */ | |
23693 static ufunc_T * | |
23694 find_func(char_u *name) | |
23695 { | |
23696 hashitem_T *hi; | |
23697 | |
23698 hi = hash_find(&func_hashtab, name); | |
23699 if (!HASHITEM_EMPTY(hi)) | |
23700 return HI2UF(hi); | |
23701 return NULL; | |
23702 } | |
23703 | |
23704 #if defined(EXITFREE) || defined(PROTO) | |
23705 void | |
23706 free_all_functions(void) | |
23707 { | |
23708 hashitem_T *hi; | |
23709 | |
23710 /* Need to start all over every time, because func_free() may change the | |
23711 * hash table. */ | |
23712 while (func_hashtab.ht_used > 0) | |
23713 for (hi = func_hashtab.ht_array; ; ++hi) | |
23714 if (!HASHITEM_EMPTY(hi)) | |
23715 { | |
23716 func_free(HI2UF(hi)); | |
23717 break; | |
23718 } | |
23719 } | |
23720 #endif | |
23721 | |
23722 int | |
23723 translated_function_exists(char_u *name) | |
23724 { | |
23725 if (builtin_function(name, -1)) | |
23726 return find_internal_func(name) >= 0; | |
23727 return find_func(name) != NULL; | |
23728 } | |
23729 | |
23730 /* | |
23731 * Return TRUE if a function "name" exists. | |
23732 */ | |
23733 static int | |
23734 function_exists(char_u *name) | |
23735 { | |
23736 char_u *nm = name; | |
23737 char_u *p; | |
23738 int n = FALSE; | |
23739 | |
23740 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD, | |
23741 NULL, NULL); | |
23742 nm = skipwhite(nm); | |
23743 | |
23744 /* Only accept "funcname", "funcname ", "funcname (..." and | |
23745 * "funcname(...", not "funcname!...". */ | |
23746 if (p != NULL && (*nm == NUL || *nm == '(')) | |
23747 n = translated_function_exists(p); | |
23748 vim_free(p); | |
23749 return n; | |
23750 } | |
23751 | |
23752 char_u * | |
23753 get_expanded_name(char_u *name, int check) | |
23754 { | |
23755 char_u *nm = name; | |
23756 char_u *p; | |
23757 | |
23758 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL); | |
23759 | |
23760 if (p != NULL && *nm == NUL) | |
23761 if (!check || translated_function_exists(p)) | |
23762 return p; | |
23763 | |
23764 vim_free(p); | |
23765 return NULL; | |
23766 } | |
23767 | |
23768 /* | |
23769 * Return TRUE if "name" looks like a builtin function name: starts with a | |
23770 * lower case letter and doesn't contain AUTOLOAD_CHAR. | |
23771 * "len" is the length of "name", or -1 for NUL terminated. | |
23772 */ | |
23773 static int | |
23774 builtin_function(char_u *name, int len) | |
23775 { | |
23776 char_u *p; | |
23777 | |
23778 if (!ASCII_ISLOWER(name[0])) | |
23779 return FALSE; | |
23780 p = vim_strchr(name, AUTOLOAD_CHAR); | |
23781 return p == NULL || (len > 0 && p > name + len); | |
23782 } | |
23783 | |
23784 #if defined(FEAT_PROFILE) || defined(PROTO) | |
23785 /* | |
23786 * Start profiling function "fp". | |
23787 */ | |
23788 static void | |
23789 func_do_profile(ufunc_T *fp) | |
23790 { | |
23791 int len = fp->uf_lines.ga_len; | |
23792 | |
23793 if (len == 0) | |
23794 len = 1; /* avoid getting error for allocating zero bytes */ | |
23795 fp->uf_tm_count = 0; | |
23796 profile_zero(&fp->uf_tm_self); | |
23797 profile_zero(&fp->uf_tm_total); | |
23798 if (fp->uf_tml_count == NULL) | |
23799 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len)); | |
23800 if (fp->uf_tml_total == NULL) | |
23801 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned) | |
23802 (sizeof(proftime_T) * len)); | |
23803 if (fp->uf_tml_self == NULL) | |
23804 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned) | |
23805 (sizeof(proftime_T) * len)); | |
23806 fp->uf_tml_idx = -1; | |
23807 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL | |
23808 || fp->uf_tml_self == NULL) | |
23809 return; /* out of memory */ | |
23810 | |
23811 fp->uf_profiling = TRUE; | |
23812 } | |
23813 | |
23814 /* | |
23815 * Dump the profiling results for all functions in file "fd". | |
23816 */ | |
23817 void | |
23818 func_dump_profile(FILE *fd) | |
23819 { | |
23820 hashitem_T *hi; | |
23821 int todo; | |
23822 ufunc_T *fp; | |
23823 int i; | |
23824 ufunc_T **sorttab; | |
23825 int st_len = 0; | |
23826 | |
23827 todo = (int)func_hashtab.ht_used; | |
23828 if (todo == 0) | |
23829 return; /* nothing to dump */ | |
23830 | |
23831 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo)); | |
23832 | |
23833 for (hi = func_hashtab.ht_array; todo > 0; ++hi) | |
23834 { | |
23835 if (!HASHITEM_EMPTY(hi)) | |
23836 { | |
23837 --todo; | |
23838 fp = HI2UF(hi); | |
23839 if (fp->uf_profiling) | |
23840 { | |
23841 if (sorttab != NULL) | |
23842 sorttab[st_len++] = fp; | |
23843 | |
23844 if (fp->uf_name[0] == K_SPECIAL) | |
23845 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3); | |
23846 else | |
23847 fprintf(fd, "FUNCTION %s()\n", fp->uf_name); | |
23848 if (fp->uf_tm_count == 1) | |
23849 fprintf(fd, "Called 1 time\n"); | |
23850 else | |
23851 fprintf(fd, "Called %d times\n", fp->uf_tm_count); | |
23852 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total)); | |
23853 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self)); | |
23854 fprintf(fd, "\n"); | |
23855 fprintf(fd, "count total (s) self (s)\n"); | |
23856 | |
23857 for (i = 0; i < fp->uf_lines.ga_len; ++i) | |
23858 { | |
23859 if (FUNCLINE(fp, i) == NULL) | |
23860 continue; | |
23861 prof_func_line(fd, fp->uf_tml_count[i], | |
23862 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE); | |
23863 fprintf(fd, "%s\n", FUNCLINE(fp, i)); | |
23864 } | |
23865 fprintf(fd, "\n"); | |
23866 } | |
23867 } | |
23868 } | |
23869 | |
23870 if (sorttab != NULL && st_len > 0) | |
23871 { | |
23872 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *), | |
23873 prof_total_cmp); | |
23874 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE); | |
23875 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *), | |
23876 prof_self_cmp); | |
23877 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE); | |
23878 } | |
23879 | |
23880 vim_free(sorttab); | |
23881 } | |
23882 | |
23883 static void | |
23884 prof_sort_list( | |
23885 FILE *fd, | |
23886 ufunc_T **sorttab, | |
23887 int st_len, | |
23888 char *title, | |
23889 int prefer_self) /* when equal print only self time */ | |
23890 { | |
23891 int i; | |
23892 ufunc_T *fp; | |
23893 | |
23894 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title); | |
23895 fprintf(fd, "count total (s) self (s) function\n"); | |
23896 for (i = 0; i < 20 && i < st_len; ++i) | |
23897 { | |
23898 fp = sorttab[i]; | |
23899 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self, | |
23900 prefer_self); | |
23901 if (fp->uf_name[0] == K_SPECIAL) | |
23902 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3); | |
23903 else | |
23904 fprintf(fd, " %s()\n", fp->uf_name); | |
23905 } | |
23906 fprintf(fd, "\n"); | |
23907 } | |
23908 | |
23909 /* | |
23910 * Print the count and times for one function or function line. | |
23911 */ | |
23912 static void | |
23913 prof_func_line( | |
23914 FILE *fd, | |
23915 int count, | |
23916 proftime_T *total, | |
23917 proftime_T *self, | |
23918 int prefer_self) /* when equal print only self time */ | |
23919 { | |
23920 if (count > 0) | |
23921 { | |
23922 fprintf(fd, "%5d ", count); | |
23923 if (prefer_self && profile_equal(total, self)) | |
23924 fprintf(fd, " "); | |
23925 else | |
23926 fprintf(fd, "%s ", profile_msg(total)); | |
23927 if (!prefer_self && profile_equal(total, self)) | |
23928 fprintf(fd, " "); | |
23929 else | |
23930 fprintf(fd, "%s ", profile_msg(self)); | |
23931 } | |
23932 else | |
23933 fprintf(fd, " "); | |
23934 } | |
23935 | |
23936 /* | |
23937 * Compare function for total time sorting. | |
23938 */ | |
23939 static int | |
23940 #ifdef __BORLANDC__ | |
23941 _RTLENTRYF | |
23942 #endif | |
23943 prof_total_cmp(const void *s1, const void *s2) | |
23944 { | |
23945 ufunc_T *p1, *p2; | |
23946 | |
23947 p1 = *(ufunc_T **)s1; | |
23948 p2 = *(ufunc_T **)s2; | |
23949 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total); | |
23950 } | |
23951 | |
23952 /* | |
23953 * Compare function for self time sorting. | |
23954 */ | |
23955 static int | |
23956 #ifdef __BORLANDC__ | |
23957 _RTLENTRYF | |
23958 #endif | |
23959 prof_self_cmp(const void *s1, const void *s2) | |
23960 { | |
23961 ufunc_T *p1, *p2; | |
23962 | |
23963 p1 = *(ufunc_T **)s1; | |
23964 p2 = *(ufunc_T **)s2; | |
23965 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self); | |
23966 } | |
23967 | |
23968 #endif | |
23969 | |
23970 /* | |
23971 * If "name" has a package name try autoloading the script for it. | |
23972 * Return TRUE if a package was loaded. | |
23973 */ | |
23974 static int | |
23975 script_autoload( | |
23976 char_u *name, | |
23977 int reload) /* load script again when already loaded */ | |
23978 { | |
23979 char_u *p; | |
23980 char_u *scriptname, *tofree; | |
23981 int ret = FALSE; | |
23982 int i; | |
23983 | |
23984 /* If there is no '#' after name[0] there is no package name. */ | |
23985 p = vim_strchr(name, AUTOLOAD_CHAR); | |
23986 if (p == NULL || p == name) | |
23987 return FALSE; | |
23988 | |
23989 tofree = scriptname = autoload_name(name); | |
23990 | |
23991 /* Find the name in the list of previously loaded package names. Skip | |
23992 * "autoload/", it's always the same. */ | |
23993 for (i = 0; i < ga_loaded.ga_len; ++i) | |
23994 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0) | |
23995 break; | |
23996 if (!reload && i < ga_loaded.ga_len) | |
23997 ret = FALSE; /* was loaded already */ | |
23998 else | |
23999 { | |
24000 /* Remember the name if it wasn't loaded already. */ | |
24001 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK) | |
24002 { | |
24003 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname; | |
24004 tofree = NULL; | |
24005 } | |
24006 | |
24007 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */ | |
24008 if (source_runtime(scriptname, 0) == OK) | |
24009 ret = TRUE; | |
24010 } | |
24011 | |
24012 vim_free(tofree); | |
24013 return ret; | |
24014 } | |
24015 | |
24016 /* | |
24017 * Return the autoload script name for a function or variable name. | 21556 * Return the autoload script name for a function or variable name. |
24018 * Returns NULL when out of memory. | 21557 * Returns NULL when out of memory. |
24019 */ | 21558 */ |
24020 static char_u * | 21559 char_u * |
24021 autoload_name(char_u *name) | 21560 autoload_name(char_u *name) |
24022 { | 21561 { |
24023 char_u *p; | 21562 char_u *p; |
24024 char_u *scriptname; | 21563 char_u *scriptname; |
24025 | 21564 |
24034 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL) | 21573 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL) |
24035 *p = '/'; | 21574 *p = '/'; |
24036 return scriptname; | 21575 return scriptname; |
24037 } | 21576 } |
24038 | 21577 |
24039 #if defined(FEAT_CMDL_COMPL) || defined(PROTO) | 21578 /* |
24040 | 21579 * If "name" has a package name try autoloading the script for it. |
24041 /* | 21580 * Return TRUE if a package was loaded. |
24042 * Function given to ExpandGeneric() to obtain the list of user defined | 21581 */ |
24043 * function names. | 21582 int |
24044 */ | 21583 script_autoload( |
24045 char_u * | 21584 char_u *name, |
24046 get_user_func_name(expand_T *xp, int idx) | 21585 int reload) /* load script again when already loaded */ |
24047 { | 21586 { |
24048 static long_u done; | 21587 char_u *p; |
24049 static hashitem_T *hi; | 21588 char_u *scriptname, *tofree; |
24050 ufunc_T *fp; | 21589 int ret = FALSE; |
24051 | 21590 int i; |
24052 if (idx == 0) | 21591 |
24053 { | 21592 /* If there is no '#' after name[0] there is no package name. */ |
24054 done = 0; | 21593 p = vim_strchr(name, AUTOLOAD_CHAR); |
24055 hi = func_hashtab.ht_array; | 21594 if (p == NULL || p == name) |
24056 } | 21595 return FALSE; |
24057 if (done < func_hashtab.ht_used) | 21596 |
24058 { | 21597 tofree = scriptname = autoload_name(name); |
24059 if (done++ > 0) | 21598 |
24060 ++hi; | 21599 /* Find the name in the list of previously loaded package names. Skip |
24061 while (HASHITEM_EMPTY(hi)) | 21600 * "autoload/", it's always the same. */ |
24062 ++hi; | 21601 for (i = 0; i < ga_loaded.ga_len; ++i) |
24063 fp = HI2UF(hi); | 21602 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0) |
24064 | 21603 break; |
24065 if (fp->uf_flags & FC_DICT) | 21604 if (!reload && i < ga_loaded.ga_len) |
24066 return (char_u *)""; /* don't show dict functions */ | 21605 ret = FALSE; /* was loaded already */ |
24067 | |
24068 if (STRLEN(fp->uf_name) + 4 >= IOSIZE) | |
24069 return fp->uf_name; /* prevents overflow */ | |
24070 | |
24071 cat_func_name(IObuff, fp); | |
24072 if (xp->xp_context != EXPAND_USER_FUNC) | |
24073 { | |
24074 STRCAT(IObuff, "("); | |
24075 if (!fp->uf_varargs && fp->uf_args.ga_len == 0) | |
24076 STRCAT(IObuff, ")"); | |
24077 } | |
24078 return IObuff; | |
24079 } | |
24080 return NULL; | |
24081 } | |
24082 | |
24083 #endif /* FEAT_CMDL_COMPL */ | |
24084 | |
24085 /* | |
24086 * Copy the function name of "fp" to buffer "buf". | |
24087 * "buf" must be able to hold the function name plus three bytes. | |
24088 * Takes care of script-local function names. | |
24089 */ | |
24090 static void | |
24091 cat_func_name(char_u *buf, ufunc_T *fp) | |
24092 { | |
24093 if (fp->uf_name[0] == K_SPECIAL) | |
24094 { | |
24095 STRCPY(buf, "<SNR>"); | |
24096 STRCAT(buf, fp->uf_name + 3); | |
24097 } | |
24098 else | 21606 else |
24099 STRCPY(buf, fp->uf_name); | 21607 { |
24100 } | 21608 /* Remember the name if it wasn't loaded already. */ |
24101 | 21609 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK) |
24102 /* | 21610 { |
24103 * ":delfunction {name}" | 21611 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname; |
24104 */ | 21612 tofree = NULL; |
24105 void | 21613 } |
24106 ex_delfunction(exarg_T *eap) | 21614 |
24107 { | 21615 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */ |
24108 ufunc_T *fp = NULL; | 21616 if (source_runtime(scriptname, 0) == OK) |
24109 char_u *p; | 21617 ret = TRUE; |
24110 char_u *name; | 21618 } |
24111 funcdict_T fudi; | 21619 |
24112 | |
24113 p = eap->arg; | |
24114 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL); | |
24115 vim_free(fudi.fd_newkey); | |
24116 if (name == NULL) | |
24117 { | |
24118 if (fudi.fd_dict != NULL && !eap->skip) | |
24119 EMSG(_(e_funcref)); | |
24120 return; | |
24121 } | |
24122 if (!ends_excmd(*skipwhite(p))) | |
24123 { | |
24124 vim_free(name); | |
24125 EMSG(_(e_trailing)); | |
24126 return; | |
24127 } | |
24128 eap->nextcmd = check_nextcmd(p); | |
24129 if (eap->nextcmd != NULL) | |
24130 *p = NUL; | |
24131 | |
24132 if (!eap->skip) | |
24133 fp = find_func(name); | |
24134 vim_free(name); | |
24135 | |
24136 if (!eap->skip) | |
24137 { | |
24138 if (fp == NULL) | |
24139 { | |
24140 EMSG2(_(e_nofunc), eap->arg); | |
24141 return; | |
24142 } | |
24143 if (fp->uf_calls > 0) | |
24144 { | |
24145 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg); | |
24146 return; | |
24147 } | |
24148 | |
24149 if (fudi.fd_dict != NULL) | |
24150 { | |
24151 /* Delete the dict item that refers to the function, it will | |
24152 * invoke func_unref() and possibly delete the function. */ | |
24153 dictitem_remove(fudi.fd_dict, fudi.fd_di); | |
24154 } | |
24155 else | |
24156 func_free(fp); | |
24157 } | |
24158 } | |
24159 | |
24160 /* | |
24161 * Free a function and remove it from the list of functions. | |
24162 */ | |
24163 static void | |
24164 func_free(ufunc_T *fp) | |
24165 { | |
24166 hashitem_T *hi; | |
24167 | |
24168 /* clear this function */ | |
24169 ga_clear_strings(&(fp->uf_args)); | |
24170 ga_clear_strings(&(fp->uf_lines)); | |
24171 #ifdef FEAT_PROFILE | |
24172 vim_free(fp->uf_tml_count); | |
24173 vim_free(fp->uf_tml_total); | |
24174 vim_free(fp->uf_tml_self); | |
24175 #endif | |
24176 | |
24177 /* remove the function from the function hashtable */ | |
24178 hi = hash_find(&func_hashtab, UF2HIKEY(fp)); | |
24179 if (HASHITEM_EMPTY(hi)) | |
24180 EMSG2(_(e_intern2), "func_free()"); | |
24181 else | |
24182 hash_remove(&func_hashtab, hi); | |
24183 | |
24184 vim_free(fp); | |
24185 } | |
24186 | |
24187 /* | |
24188 * Unreference a Function: decrement the reference count and free it when it | |
24189 * becomes zero. Only for numbered functions. | |
24190 */ | |
24191 void | |
24192 func_unref(char_u *name) | |
24193 { | |
24194 ufunc_T *fp; | |
24195 | |
24196 if (name == NULL) | |
24197 return; | |
24198 else if (isdigit(*name)) | |
24199 { | |
24200 fp = find_func(name); | |
24201 if (fp == NULL) | |
24202 { | |
24203 #ifdef EXITFREE | |
24204 if (!entered_free_all_mem) | |
24205 #endif | |
24206 EMSG2(_(e_intern2), "func_unref()"); | |
24207 } | |
24208 else if (--fp->uf_refcount <= 0) | |
24209 { | |
24210 /* Only delete it when it's not being used. Otherwise it's done | |
24211 * when "uf_calls" becomes zero. */ | |
24212 if (fp->uf_calls == 0) | |
24213 func_free(fp); | |
24214 } | |
24215 } | |
24216 else if (STRNCMP(name, "<lambda>", 8) == 0) | |
24217 { | |
24218 /* fail silently, when lambda function isn't found. */ | |
24219 fp = find_func(name); | |
24220 if (fp != NULL && --fp->uf_refcount <= 0) | |
24221 { | |
24222 /* Only delete it when it's not being used. Otherwise it's done | |
24223 * when "uf_calls" becomes zero. */ | |
24224 if (fp->uf_calls == 0) | |
24225 func_free(fp); | |
24226 } | |
24227 } | |
24228 } | |
24229 | |
24230 /* | |
24231 * Count a reference to a Function. | |
24232 */ | |
24233 void | |
24234 func_ref(char_u *name) | |
24235 { | |
24236 ufunc_T *fp; | |
24237 | |
24238 if (name == NULL) | |
24239 return; | |
24240 else if (isdigit(*name)) | |
24241 { | |
24242 fp = find_func(name); | |
24243 if (fp == NULL) | |
24244 EMSG2(_(e_intern2), "func_ref()"); | |
24245 else | |
24246 ++fp->uf_refcount; | |
24247 } | |
24248 else if (STRNCMP(name, "<lambda>", 8) == 0) | |
24249 { | |
24250 /* fail silently, when lambda function isn't found. */ | |
24251 fp = find_func(name); | |
24252 if (fp != NULL) | |
24253 ++fp->uf_refcount; | |
24254 } | |
24255 } | |
24256 | |
24257 /* | |
24258 * Call a user function. | |
24259 */ | |
24260 static void | |
24261 call_user_func( | |
24262 ufunc_T *fp, /* pointer to function */ | |
24263 int argcount, /* nr of args */ | |
24264 typval_T *argvars, /* arguments */ | |
24265 typval_T *rettv, /* return value */ | |
24266 linenr_T firstline, /* first line of range */ | |
24267 linenr_T lastline, /* last line of range */ | |
24268 dict_T *selfdict) /* Dictionary for "self" */ | |
24269 { | |
24270 char_u *save_sourcing_name; | |
24271 linenr_T save_sourcing_lnum; | |
24272 scid_T save_current_SID; | |
24273 funccall_T *fc; | |
24274 int save_did_emsg; | |
24275 static int depth = 0; | |
24276 dictitem_T *v; | |
24277 int fixvar_idx = 0; /* index in fixvar[] */ | |
24278 int i; | |
24279 int ai; | |
24280 int islambda = FALSE; | |
24281 char_u numbuf[NUMBUFLEN]; | |
24282 char_u *name; | |
24283 size_t len; | |
24284 #ifdef FEAT_PROFILE | |
24285 proftime_T wait_start; | |
24286 proftime_T call_start; | |
24287 #endif | |
24288 | |
24289 /* If depth of calling is getting too high, don't execute the function */ | |
24290 if (depth >= p_mfd) | |
24291 { | |
24292 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'")); | |
24293 rettv->v_type = VAR_NUMBER; | |
24294 rettv->vval.v_number = -1; | |
24295 return; | |
24296 } | |
24297 ++depth; | |
24298 | |
24299 line_breakcheck(); /* check for CTRL-C hit */ | |
24300 | |
24301 fc = (funccall_T *)alloc(sizeof(funccall_T)); | |
24302 fc->caller = current_funccal; | |
24303 current_funccal = fc; | |
24304 fc->func = fp; | |
24305 fc->rettv = rettv; | |
24306 rettv->vval.v_number = 0; | |
24307 fc->linenr = 0; | |
24308 fc->returned = FALSE; | |
24309 fc->level = ex_nesting_level; | |
24310 /* Check if this function has a breakpoint. */ | |
24311 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0); | |
24312 fc->dbg_tick = debug_tick; | |
24313 | |
24314 if (STRNCMP(fp->uf_name, "<lambda>", 8) == 0) | |
24315 islambda = TRUE; | |
24316 | |
24317 /* | |
24318 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables | |
24319 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free | |
24320 * each argument variable and saves a lot of time. | |
24321 */ | |
24322 /* | |
24323 * Init l: variables. | |
24324 */ | |
24325 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE); | |
24326 if (selfdict != NULL) | |
24327 { | |
24328 /* Set l:self to "selfdict". Use "name" to avoid a warning from | |
24329 * some compiler that checks the destination size. */ | |
24330 v = &fc->fixvar[fixvar_idx++].var; | |
24331 name = v->di_key; | |
24332 STRCPY(name, "self"); | |
24333 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX; | |
24334 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v)); | |
24335 v->di_tv.v_type = VAR_DICT; | |
24336 v->di_tv.v_lock = 0; | |
24337 v->di_tv.vval.v_dict = selfdict; | |
24338 ++selfdict->dv_refcount; | |
24339 } | |
24340 | |
24341 /* | |
24342 * Init a: variables. | |
24343 * Set a:0 to "argcount". | |
24344 * Set a:000 to a list with room for the "..." arguments. | |
24345 */ | |
24346 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE); | |
24347 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0", | |
24348 (varnumber_T)(argcount - fp->uf_args.ga_len)); | |
24349 /* Use "name" to avoid a warning from some compiler that checks the | |
24350 * destination size. */ | |
24351 v = &fc->fixvar[fixvar_idx++].var; | |
24352 name = v->di_key; | |
24353 STRCPY(name, "000"); | |
24354 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX; | |
24355 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v)); | |
24356 v->di_tv.v_type = VAR_LIST; | |
24357 v->di_tv.v_lock = VAR_FIXED; | |
24358 v->di_tv.vval.v_list = &fc->l_varlist; | |
24359 vim_memset(&fc->l_varlist, 0, sizeof(list_T)); | |
24360 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT; | |
24361 fc->l_varlist.lv_lock = VAR_FIXED; | |
24362 | |
24363 /* | |
24364 * Set a:firstline to "firstline" and a:lastline to "lastline". | |
24365 * Set a:name to named arguments. | |
24366 * Set a:N to the "..." arguments. | |
24367 */ | |
24368 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline", | |
24369 (varnumber_T)firstline); | |
24370 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline", | |
24371 (varnumber_T)lastline); | |
24372 for (i = 0; i < argcount; ++i) | |
24373 { | |
24374 int addlocal = FALSE; | |
24375 dictitem_T *v2; | |
24376 | |
24377 ai = i - fp->uf_args.ga_len; | |
24378 if (ai < 0) | |
24379 { | |
24380 /* named argument a:name */ | |
24381 name = FUNCARG(fp, i); | |
24382 if (islambda) | |
24383 addlocal = TRUE; | |
24384 } | |
24385 else | |
24386 { | |
24387 /* "..." argument a:1, a:2, etc. */ | |
24388 sprintf((char *)numbuf, "%d", ai + 1); | |
24389 name = numbuf; | |
24390 } | |
24391 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN) | |
24392 { | |
24393 v = &fc->fixvar[fixvar_idx++].var; | |
24394 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX; | |
24395 | |
24396 if (addlocal) | |
24397 v2 = v; | |
24398 } | |
24399 else | |
24400 { | |
24401 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) | |
24402 + STRLEN(name))); | |
24403 if (v == NULL) | |
24404 break; | |
24405 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC; | |
24406 | |
24407 if (addlocal) | |
24408 { | |
24409 v2 = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) | |
24410 + STRLEN(name))); | |
24411 if (v2 == NULL) | |
24412 { | |
24413 vim_free(v); | |
24414 break; | |
24415 } | |
24416 v2->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC; | |
24417 } | |
24418 } | |
24419 STRCPY(v->di_key, name); | |
24420 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v)); | |
24421 | |
24422 /* Note: the values are copied directly to avoid alloc/free. | |
24423 * "argvars" must have VAR_FIXED for v_lock. */ | |
24424 v->di_tv = argvars[i]; | |
24425 v->di_tv.v_lock = VAR_FIXED; | |
24426 | |
24427 /* Named arguments can be accessed without the "a:" prefix in lambda | |
24428 * expressions. Add to the l: dict. */ | |
24429 if (addlocal) | |
24430 { | |
24431 STRCPY(v2->di_key, name); | |
24432 copy_tv(&v->di_tv, &v2->di_tv); | |
24433 v2->di_tv.v_lock = VAR_FIXED; | |
24434 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v2)); | |
24435 } | |
24436 | |
24437 if (ai >= 0 && ai < MAX_FUNC_ARGS) | |
24438 { | |
24439 list_append(&fc->l_varlist, &fc->l_listitems[ai]); | |
24440 fc->l_listitems[ai].li_tv = argvars[i]; | |
24441 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED; | |
24442 } | |
24443 } | |
24444 | |
24445 /* Don't redraw while executing the function. */ | |
24446 ++RedrawingDisabled; | |
24447 save_sourcing_name = sourcing_name; | |
24448 save_sourcing_lnum = sourcing_lnum; | |
24449 sourcing_lnum = 1; | |
24450 /* need space for function name + ("function " + 3) or "[number]" */ | |
24451 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name)) | |
24452 + STRLEN(fp->uf_name) + 20; | |
24453 sourcing_name = alloc((unsigned)len); | |
24454 if (sourcing_name != NULL) | |
24455 { | |
24456 if (save_sourcing_name != NULL | |
24457 && STRNCMP(save_sourcing_name, "function ", 9) == 0) | |
24458 sprintf((char *)sourcing_name, "%s[%d]..", | |
24459 save_sourcing_name, (int)save_sourcing_lnum); | |
24460 else | |
24461 STRCPY(sourcing_name, "function "); | |
24462 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp); | |
24463 | |
24464 if (p_verbose >= 12) | |
24465 { | |
24466 ++no_wait_return; | |
24467 verbose_enter_scroll(); | |
24468 | |
24469 smsg((char_u *)_("calling %s"), sourcing_name); | |
24470 if (p_verbose >= 14) | |
24471 { | |
24472 char_u buf[MSG_BUF_LEN]; | |
24473 char_u numbuf2[NUMBUFLEN]; | |
24474 char_u *tofree; | |
24475 char_u *s; | |
24476 | |
24477 msg_puts((char_u *)"("); | |
24478 for (i = 0; i < argcount; ++i) | |
24479 { | |
24480 if (i > 0) | |
24481 msg_puts((char_u *)", "); | |
24482 if (argvars[i].v_type == VAR_NUMBER) | |
24483 msg_outnum((long)argvars[i].vval.v_number); | |
24484 else | |
24485 { | |
24486 /* Do not want errors such as E724 here. */ | |
24487 ++emsg_off; | |
24488 s = tv2string(&argvars[i], &tofree, numbuf2, 0); | |
24489 --emsg_off; | |
24490 if (s != NULL) | |
24491 { | |
24492 if (vim_strsize(s) > MSG_BUF_CLEN) | |
24493 { | |
24494 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN); | |
24495 s = buf; | |
24496 } | |
24497 msg_puts(s); | |
24498 vim_free(tofree); | |
24499 } | |
24500 } | |
24501 } | |
24502 msg_puts((char_u *)")"); | |
24503 } | |
24504 msg_puts((char_u *)"\n"); /* don't overwrite this either */ | |
24505 | |
24506 verbose_leave_scroll(); | |
24507 --no_wait_return; | |
24508 } | |
24509 } | |
24510 #ifdef FEAT_PROFILE | |
24511 if (do_profiling == PROF_YES) | |
24512 { | |
24513 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL)) | |
24514 func_do_profile(fp); | |
24515 if (fp->uf_profiling | |
24516 || (fc->caller != NULL && fc->caller->func->uf_profiling)) | |
24517 { | |
24518 ++fp->uf_tm_count; | |
24519 profile_start(&call_start); | |
24520 profile_zero(&fp->uf_tm_children); | |
24521 } | |
24522 script_prof_save(&wait_start); | |
24523 } | |
24524 #endif | |
24525 | |
24526 save_current_SID = current_SID; | |
24527 current_SID = fp->uf_script_ID; | |
24528 save_did_emsg = did_emsg; | |
24529 did_emsg = FALSE; | |
24530 | |
24531 /* call do_cmdline() to execute the lines */ | |
24532 do_cmdline(NULL, get_func_line, (void *)fc, | |
24533 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT); | |
24534 | |
24535 --RedrawingDisabled; | |
24536 | |
24537 /* when the function was aborted because of an error, return -1 */ | |
24538 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN) | |
24539 { | |
24540 clear_tv(rettv); | |
24541 rettv->v_type = VAR_NUMBER; | |
24542 rettv->vval.v_number = -1; | |
24543 } | |
24544 | |
24545 #ifdef FEAT_PROFILE | |
24546 if (do_profiling == PROF_YES && (fp->uf_profiling | |
24547 || (fc->caller != NULL && fc->caller->func->uf_profiling))) | |
24548 { | |
24549 profile_end(&call_start); | |
24550 profile_sub_wait(&wait_start, &call_start); | |
24551 profile_add(&fp->uf_tm_total, &call_start); | |
24552 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children); | |
24553 if (fc->caller != NULL && fc->caller->func->uf_profiling) | |
24554 { | |
24555 profile_add(&fc->caller->func->uf_tm_children, &call_start); | |
24556 profile_add(&fc->caller->func->uf_tml_children, &call_start); | |
24557 } | |
24558 } | |
24559 #endif | |
24560 | |
24561 /* when being verbose, mention the return value */ | |
24562 if (p_verbose >= 12) | |
24563 { | |
24564 ++no_wait_return; | |
24565 verbose_enter_scroll(); | |
24566 | |
24567 if (aborting()) | |
24568 smsg((char_u *)_("%s aborted"), sourcing_name); | |
24569 else if (fc->rettv->v_type == VAR_NUMBER) | |
24570 smsg((char_u *)_("%s returning #%ld"), sourcing_name, | |
24571 (long)fc->rettv->vval.v_number); | |
24572 else | |
24573 { | |
24574 char_u buf[MSG_BUF_LEN]; | |
24575 char_u numbuf2[NUMBUFLEN]; | |
24576 char_u *tofree; | |
24577 char_u *s; | |
24578 | |
24579 /* The value may be very long. Skip the middle part, so that we | |
24580 * have some idea how it starts and ends. smsg() would always | |
24581 * truncate it at the end. Don't want errors such as E724 here. */ | |
24582 ++emsg_off; | |
24583 s = tv2string(fc->rettv, &tofree, numbuf2, 0); | |
24584 --emsg_off; | |
24585 if (s != NULL) | |
24586 { | |
24587 if (vim_strsize(s) > MSG_BUF_CLEN) | |
24588 { | |
24589 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN); | |
24590 s = buf; | |
24591 } | |
24592 smsg((char_u *)_("%s returning %s"), sourcing_name, s); | |
24593 vim_free(tofree); | |
24594 } | |
24595 } | |
24596 msg_puts((char_u *)"\n"); /* don't overwrite this either */ | |
24597 | |
24598 verbose_leave_scroll(); | |
24599 --no_wait_return; | |
24600 } | |
24601 | |
24602 vim_free(sourcing_name); | |
24603 sourcing_name = save_sourcing_name; | |
24604 sourcing_lnum = save_sourcing_lnum; | |
24605 current_SID = save_current_SID; | |
24606 #ifdef FEAT_PROFILE | |
24607 if (do_profiling == PROF_YES) | |
24608 script_prof_restore(&wait_start); | |
24609 #endif | |
24610 | |
24611 if (p_verbose >= 12 && sourcing_name != NULL) | |
24612 { | |
24613 ++no_wait_return; | |
24614 verbose_enter_scroll(); | |
24615 | |
24616 smsg((char_u *)_("continuing in %s"), sourcing_name); | |
24617 msg_puts((char_u *)"\n"); /* don't overwrite this either */ | |
24618 | |
24619 verbose_leave_scroll(); | |
24620 --no_wait_return; | |
24621 } | |
24622 | |
24623 did_emsg |= save_did_emsg; | |
24624 current_funccal = fc->caller; | |
24625 --depth; | |
24626 | |
24627 /* If the a:000 list and the l: and a: dicts are not referenced we can | |
24628 * free the funccall_T and what's in it. */ | |
24629 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT | |
24630 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT | |
24631 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT) | |
24632 { | |
24633 free_funccal(fc, FALSE); | |
24634 } | |
24635 else | |
24636 { | |
24637 hashitem_T *hi; | |
24638 listitem_T *li; | |
24639 int todo; | |
24640 | |
24641 /* "fc" is still in use. This can happen when returning "a:000" or | |
24642 * assigning "l:" to a global variable. | |
24643 * Link "fc" in the list for garbage collection later. */ | |
24644 fc->caller = previous_funccal; | |
24645 previous_funccal = fc; | |
24646 | |
24647 /* Make a copy of the a: variables, since we didn't do that above. */ | |
24648 todo = (int)fc->l_avars.dv_hashtab.ht_used; | |
24649 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi) | |
24650 { | |
24651 if (!HASHITEM_EMPTY(hi)) | |
24652 { | |
24653 --todo; | |
24654 v = HI2DI(hi); | |
24655 copy_tv(&v->di_tv, &v->di_tv); | |
24656 } | |
24657 } | |
24658 | |
24659 /* Make a copy of the a:000 items, since we didn't do that above. */ | |
24660 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next) | |
24661 copy_tv(&li->li_tv, &li->li_tv); | |
24662 } | |
24663 } | |
24664 | |
24665 /* | |
24666 * Return TRUE if items in "fc" do not have "copyID". That means they are not | |
24667 * referenced from anywhere that is in use. | |
24668 */ | |
24669 static int | |
24670 can_free_funccal(funccall_T *fc, int copyID) | |
24671 { | |
24672 return (fc->l_varlist.lv_copyID != copyID | |
24673 && fc->l_vars.dv_copyID != copyID | |
24674 && fc->l_avars.dv_copyID != copyID); | |
24675 } | |
24676 | |
24677 /* | |
24678 * Free "fc" and what it contains. | |
24679 */ | |
24680 static void | |
24681 free_funccal( | |
24682 funccall_T *fc, | |
24683 int free_val) /* a: vars were allocated */ | |
24684 { | |
24685 listitem_T *li; | |
24686 | |
24687 /* The a: variables typevals may not have been allocated, only free the | |
24688 * allocated variables. */ | |
24689 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val); | |
24690 | |
24691 /* free all l: variables */ | |
24692 vars_clear(&fc->l_vars.dv_hashtab); | |
24693 | |
24694 /* Free the a:000 variables if they were allocated. */ | |
24695 if (free_val) | |
24696 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next) | |
24697 clear_tv(&li->li_tv); | |
24698 | |
24699 vim_free(fc); | |
24700 } | |
24701 | |
24702 /* | |
24703 * Add a number variable "name" to dict "dp" with value "nr". | |
24704 */ | |
24705 static void | |
24706 add_nr_var( | |
24707 dict_T *dp, | |
24708 dictitem_T *v, | |
24709 char *name, | |
24710 varnumber_T nr) | |
24711 { | |
24712 STRCPY(v->di_key, name); | |
24713 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX; | |
24714 hash_add(&dp->dv_hashtab, DI2HIKEY(v)); | |
24715 v->di_tv.v_type = VAR_NUMBER; | |
24716 v->di_tv.v_lock = VAR_FIXED; | |
24717 v->di_tv.vval.v_number = nr; | |
24718 } | |
24719 | |
24720 /* | |
24721 * ":return [expr]" | |
24722 */ | |
24723 void | |
24724 ex_return(exarg_T *eap) | |
24725 { | |
24726 char_u *arg = eap->arg; | |
24727 typval_T rettv; | |
24728 int returning = FALSE; | |
24729 | |
24730 if (current_funccal == NULL) | |
24731 { | |
24732 EMSG(_("E133: :return not inside a function")); | |
24733 return; | |
24734 } | |
24735 | |
24736 if (eap->skip) | |
24737 ++emsg_skip; | |
24738 | |
24739 eap->nextcmd = NULL; | |
24740 if ((*arg != NUL && *arg != '|' && *arg != '\n') | |
24741 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL) | |
24742 { | |
24743 if (!eap->skip) | |
24744 returning = do_return(eap, FALSE, TRUE, &rettv); | |
24745 else | |
24746 clear_tv(&rettv); | |
24747 } | |
24748 /* It's safer to return also on error. */ | |
24749 else if (!eap->skip) | |
24750 { | |
24751 /* | |
24752 * Return unless the expression evaluation has been cancelled due to an | |
24753 * aborting error, an interrupt, or an exception. | |
24754 */ | |
24755 if (!aborting()) | |
24756 returning = do_return(eap, FALSE, TRUE, NULL); | |
24757 } | |
24758 | |
24759 /* When skipping or the return gets pending, advance to the next command | |
24760 * in this line (!returning). Otherwise, ignore the rest of the line. | |
24761 * Following lines will be ignored by get_func_line(). */ | |
24762 if (returning) | |
24763 eap->nextcmd = NULL; | |
24764 else if (eap->nextcmd == NULL) /* no argument */ | |
24765 eap->nextcmd = check_nextcmd(arg); | |
24766 | |
24767 if (eap->skip) | |
24768 --emsg_skip; | |
24769 } | |
24770 | |
24771 /* | |
24772 * Return from a function. Possibly makes the return pending. Also called | |
24773 * for a pending return at the ":endtry" or after returning from an extra | |
24774 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set | |
24775 * when called due to a ":return" command. "rettv" may point to a typval_T | |
24776 * with the return rettv. Returns TRUE when the return can be carried out, | |
24777 * FALSE when the return gets pending. | |
24778 */ | |
24779 int | |
24780 do_return( | |
24781 exarg_T *eap, | |
24782 int reanimate, | |
24783 int is_cmd, | |
24784 void *rettv) | |
24785 { | |
24786 int idx; | |
24787 struct condstack *cstack = eap->cstack; | |
24788 | |
24789 if (reanimate) | |
24790 /* Undo the return. */ | |
24791 current_funccal->returned = FALSE; | |
24792 | |
24793 /* | |
24794 * Cleanup (and inactivate) conditionals, but stop when a try conditional | |
24795 * not in its finally clause (which then is to be executed next) is found. | |
24796 * In this case, make the ":return" pending for execution at the ":endtry". | |
24797 * Otherwise, return normally. | |
24798 */ | |
24799 idx = cleanup_conditionals(eap->cstack, 0, TRUE); | |
24800 if (idx >= 0) | |
24801 { | |
24802 cstack->cs_pending[idx] = CSTP_RETURN; | |
24803 | |
24804 if (!is_cmd && !reanimate) | |
24805 /* A pending return again gets pending. "rettv" points to an | |
24806 * allocated variable with the rettv of the original ":return"'s | |
24807 * argument if present or is NULL else. */ | |
24808 cstack->cs_rettv[idx] = rettv; | |
24809 else | |
24810 { | |
24811 /* When undoing a return in order to make it pending, get the stored | |
24812 * return rettv. */ | |
24813 if (reanimate) | |
24814 rettv = current_funccal->rettv; | |
24815 | |
24816 if (rettv != NULL) | |
24817 { | |
24818 /* Store the value of the pending return. */ | |
24819 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL) | |
24820 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv; | |
24821 else | |
24822 EMSG(_(e_outofmem)); | |
24823 } | |
24824 else | |
24825 cstack->cs_rettv[idx] = NULL; | |
24826 | |
24827 if (reanimate) | |
24828 { | |
24829 /* The pending return value could be overwritten by a ":return" | |
24830 * without argument in a finally clause; reset the default | |
24831 * return value. */ | |
24832 current_funccal->rettv->v_type = VAR_NUMBER; | |
24833 current_funccal->rettv->vval.v_number = 0; | |
24834 } | |
24835 } | |
24836 report_make_pending(CSTP_RETURN, rettv); | |
24837 } | |
24838 else | |
24839 { | |
24840 current_funccal->returned = TRUE; | |
24841 | |
24842 /* If the return is carried out now, store the return value. For | |
24843 * a return immediately after reanimation, the value is already | |
24844 * there. */ | |
24845 if (!reanimate && rettv != NULL) | |
24846 { | |
24847 clear_tv(current_funccal->rettv); | |
24848 *current_funccal->rettv = *(typval_T *)rettv; | |
24849 if (!is_cmd) | |
24850 vim_free(rettv); | |
24851 } | |
24852 } | |
24853 | |
24854 return idx < 0; | |
24855 } | |
24856 | |
24857 /* | |
24858 * Free the variable with a pending return value. | |
24859 */ | |
24860 void | |
24861 discard_pending_return(void *rettv) | |
24862 { | |
24863 free_tv((typval_T *)rettv); | |
24864 } | |
24865 | |
24866 /* | |
24867 * Generate a return command for producing the value of "rettv". The result | |
24868 * is an allocated string. Used by report_pending() for verbose messages. | |
24869 */ | |
24870 char_u * | |
24871 get_return_cmd(void *rettv) | |
24872 { | |
24873 char_u *s = NULL; | |
24874 char_u *tofree = NULL; | |
24875 char_u numbuf[NUMBUFLEN]; | |
24876 | |
24877 if (rettv != NULL) | |
24878 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0); | |
24879 if (s == NULL) | |
24880 s = (char_u *)""; | |
24881 | |
24882 STRCPY(IObuff, ":return "); | |
24883 STRNCPY(IObuff + 8, s, IOSIZE - 8); | |
24884 if (STRLEN(s) + 8 >= IOSIZE) | |
24885 STRCPY(IObuff + IOSIZE - 4, "..."); | |
24886 vim_free(tofree); | 21620 vim_free(tofree); |
24887 return vim_strsave(IObuff); | 21621 return ret; |
24888 } | |
24889 | |
24890 /* | |
24891 * Get next function line. | |
24892 * Called by do_cmdline() to get the next line. | |
24893 * Returns allocated string, or NULL for end of function. | |
24894 */ | |
24895 char_u * | |
24896 get_func_line( | |
24897 int c UNUSED, | |
24898 void *cookie, | |
24899 int indent UNUSED) | |
24900 { | |
24901 funccall_T *fcp = (funccall_T *)cookie; | |
24902 ufunc_T *fp = fcp->func; | |
24903 char_u *retval; | |
24904 garray_T *gap; /* growarray with function lines */ | |
24905 | |
24906 /* If breakpoints have been added/deleted need to check for it. */ | |
24907 if (fcp->dbg_tick != debug_tick) | |
24908 { | |
24909 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, | |
24910 sourcing_lnum); | |
24911 fcp->dbg_tick = debug_tick; | |
24912 } | |
24913 #ifdef FEAT_PROFILE | |
24914 if (do_profiling == PROF_YES) | |
24915 func_line_end(cookie); | |
24916 #endif | |
24917 | |
24918 gap = &fp->uf_lines; | |
24919 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try()) | |
24920 || fcp->returned) | |
24921 retval = NULL; | |
24922 else | |
24923 { | |
24924 /* Skip NULL lines (continuation lines). */ | |
24925 while (fcp->linenr < gap->ga_len | |
24926 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL) | |
24927 ++fcp->linenr; | |
24928 if (fcp->linenr >= gap->ga_len) | |
24929 retval = NULL; | |
24930 else | |
24931 { | |
24932 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]); | |
24933 sourcing_lnum = fcp->linenr; | |
24934 #ifdef FEAT_PROFILE | |
24935 if (do_profiling == PROF_YES) | |
24936 func_line_start(cookie); | |
24937 #endif | |
24938 } | |
24939 } | |
24940 | |
24941 /* Did we encounter a breakpoint? */ | |
24942 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum) | |
24943 { | |
24944 dbg_breakpoint(fp->uf_name, sourcing_lnum); | |
24945 /* Find next breakpoint. */ | |
24946 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, | |
24947 sourcing_lnum); | |
24948 fcp->dbg_tick = debug_tick; | |
24949 } | |
24950 | |
24951 return retval; | |
24952 } | |
24953 | |
24954 #if defined(FEAT_PROFILE) || defined(PROTO) | |
24955 /* | |
24956 * Called when starting to read a function line. | |
24957 * "sourcing_lnum" must be correct! | |
24958 * When skipping lines it may not actually be executed, but we won't find out | |
24959 * until later and we need to store the time now. | |
24960 */ | |
24961 void | |
24962 func_line_start(void *cookie) | |
24963 { | |
24964 funccall_T *fcp = (funccall_T *)cookie; | |
24965 ufunc_T *fp = fcp->func; | |
24966 | |
24967 if (fp->uf_profiling && sourcing_lnum >= 1 | |
24968 && sourcing_lnum <= fp->uf_lines.ga_len) | |
24969 { | |
24970 fp->uf_tml_idx = sourcing_lnum - 1; | |
24971 /* Skip continuation lines. */ | |
24972 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL) | |
24973 --fp->uf_tml_idx; | |
24974 fp->uf_tml_execed = FALSE; | |
24975 profile_start(&fp->uf_tml_start); | |
24976 profile_zero(&fp->uf_tml_children); | |
24977 profile_get_wait(&fp->uf_tml_wait); | |
24978 } | |
24979 } | |
24980 | |
24981 /* | |
24982 * Called when actually executing a function line. | |
24983 */ | |
24984 void | |
24985 func_line_exec(void *cookie) | |
24986 { | |
24987 funccall_T *fcp = (funccall_T *)cookie; | |
24988 ufunc_T *fp = fcp->func; | |
24989 | |
24990 if (fp->uf_profiling && fp->uf_tml_idx >= 0) | |
24991 fp->uf_tml_execed = TRUE; | |
24992 } | |
24993 | |
24994 /* | |
24995 * Called when done with a function line. | |
24996 */ | |
24997 void | |
24998 func_line_end(void *cookie) | |
24999 { | |
25000 funccall_T *fcp = (funccall_T *)cookie; | |
25001 ufunc_T *fp = fcp->func; | |
25002 | |
25003 if (fp->uf_profiling && fp->uf_tml_idx >= 0) | |
25004 { | |
25005 if (fp->uf_tml_execed) | |
25006 { | |
25007 ++fp->uf_tml_count[fp->uf_tml_idx]; | |
25008 profile_end(&fp->uf_tml_start); | |
25009 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start); | |
25010 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start); | |
25011 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start, | |
25012 &fp->uf_tml_children); | |
25013 } | |
25014 fp->uf_tml_idx = -1; | |
25015 } | |
25016 } | |
25017 #endif | |
25018 | |
25019 /* | |
25020 * Return TRUE if the currently active function should be ended, because a | |
25021 * return was encountered or an error occurred. Used inside a ":while". | |
25022 */ | |
25023 int | |
25024 func_has_ended(void *cookie) | |
25025 { | |
25026 funccall_T *fcp = (funccall_T *)cookie; | |
25027 | |
25028 /* Ignore the "abort" flag if the abortion behavior has been changed due to | |
25029 * an error inside a try conditional. */ | |
25030 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try()) | |
25031 || fcp->returned); | |
25032 } | |
25033 | |
25034 /* | |
25035 * return TRUE if cookie indicates a function which "abort"s on errors. | |
25036 */ | |
25037 int | |
25038 func_has_abort( | |
25039 void *cookie) | |
25040 { | |
25041 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT; | |
25042 } | 21622 } |
25043 | 21623 |
25044 #if defined(FEAT_VIMINFO) || defined(FEAT_SESSION) | 21624 #if defined(FEAT_VIMINFO) || defined(FEAT_SESSION) |
25045 typedef enum | 21625 typedef enum |
25046 { | 21626 { |
25076 read_viminfo_varlist(vir_T *virp, int writing) | 21656 read_viminfo_varlist(vir_T *virp, int writing) |
25077 { | 21657 { |
25078 char_u *tab; | 21658 char_u *tab; |
25079 int type = VAR_NUMBER; | 21659 int type = VAR_NUMBER; |
25080 typval_T tv; | 21660 typval_T tv; |
25081 funccall_T *save_funccal; | 21661 void *save_funccal; |
25082 | 21662 |
25083 if (!writing && (find_viminfo_parameter('!') != NULL)) | 21663 if (!writing && (find_viminfo_parameter('!') != NULL)) |
25084 { | 21664 { |
25085 tab = vim_strchr(virp->vir_line + 1, '\t'); | 21665 tab = vim_strchr(virp->vir_line + 1, '\t'); |
25086 if (tab != NULL) | 21666 if (tab != NULL) |
25125 vim_free(etv); | 21705 vim_free(etv); |
25126 } | 21706 } |
25127 } | 21707 } |
25128 | 21708 |
25129 /* when in a function use global variables */ | 21709 /* when in a function use global variables */ |
25130 save_funccal = current_funccal; | 21710 save_funccal = clear_current_funccal(); |
25131 current_funccal = NULL; | |
25132 set_var(virp->vir_line + 1, &tv, FALSE); | 21711 set_var(virp->vir_line + 1, &tv, FALSE); |
25133 current_funccal = save_funccal; | 21712 restore_current_funccal(save_funccal); |
25134 | 21713 |
25135 if (tv.v_type == VAR_STRING) | 21714 if (tv.v_type == VAR_STRING) |
25136 vim_free(tv.vval.v_string); | 21715 vim_free(tv.vval.v_string); |
25137 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST) | 21716 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST) |
25138 clear_tv(&tv); | 21717 clear_tv(&tv); |