blob: e13ac4ac47e9e180c0d2eb4bde2eccc0452b7765 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9script.c: :vim9script, :import, :export and friends
12 */
13
14#include "vim.h"
15
Bram Moolenaardc7c3662021-12-20 15:04:29 +000016// When not generating protos this is included in proto.h
17#ifdef PROTO
Bram Moolenaar9b8d6222020-12-28 18:26:00 +010018# include "vim9.h"
19#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010020
Bram Moolenaare535db82021-03-31 21:07:24 +020021/*
22 * Return TRUE when currently using Vim9 script syntax.
23 * Does not go up the stack, a ":function" inside vim9script uses legacy
24 * syntax.
25 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010026 int
27in_vim9script(void)
28{
Bram Moolenaare535db82021-03-31 21:07:24 +020029 // "sc_version" is also set when compiling a ":def" function in legacy
30 // script.
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +020031 return (current_sctx.sc_version == SCRIPT_VERSION_VIM9
32 || (cmdmod.cmod_flags & CMOD_VIM9CMD))
33 && !(cmdmod.cmod_flags & CMOD_LEGACY);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010034}
35
Bram Moolenaarb91d3f82021-04-01 13:17:50 +020036#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010037/*
Bram Moolenaardd9de502021-08-15 13:49:42 +020038 * Return TRUE when currently in a script with script version smaller than
39 * "max_version" or command modifiers forced it.
40 */
41 int
42in_old_script(int max_version)
43{
Bram Moolenaar5cebca22021-08-15 14:39:13 +020044 return (current_sctx.sc_version < max_version
45 && !(cmdmod.cmod_flags & CMOD_VIM9CMD))
Bram Moolenaardd9de502021-08-15 13:49:42 +020046 || (cmdmod.cmod_flags & CMOD_LEGACY);
47}
48
49/*
Bram Moolenaare535db82021-03-31 21:07:24 +020050 * Return TRUE if the current script is Vim9 script.
51 * This also returns TRUE in a legacy function in a Vim9 script.
52 */
53 int
54current_script_is_vim9(void)
55{
56 return SCRIPT_ID_VALID(current_sctx.sc_sid)
57 && SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
58 == SCRIPT_VERSION_VIM9;
59}
Bram Moolenaarb91d3f82021-04-01 13:17:50 +020060#endif
Bram Moolenaare535db82021-03-31 21:07:24 +020061
62/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010063 * ":vim9script".
64 */
65 void
Bram Moolenaar9b8d6222020-12-28 18:26:00 +010066ex_vim9script(exarg_T *eap UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010067{
Bram Moolenaar9b8d6222020-12-28 18:26:00 +010068#ifdef FEAT_EVAL
Bram Moolenaar2b327002020-12-26 15:39:31 +010069 int sid = current_sctx.sc_sid;
Bram Moolenaar101f4812020-06-16 23:18:51 +020070 scriptitem_T *si;
Bram Moolenaardc4451d2022-01-09 21:36:37 +000071 int found_noclear = FALSE;
Bram Moolenaardc4451d2022-01-09 21:36:37 +000072 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010073
74 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
75 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +020076 emsg(_(e_vim9script_can_only_be_used_in_script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010077 return;
78 }
Bram Moolenaar2b327002020-12-26 15:39:31 +010079
80 si = SCRIPT_ITEM(sid);
81 if (si->sn_state == SN_STATE_HAD_COMMAND)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010082 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +020083 emsg(_(e_vim9script_must_be_first_command_in_script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010084 return;
85 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +000086
87 for (p = eap->arg; !IS_WHITE_OR_NUL(*p); p = skipwhite(skiptowhite(p)))
Bram Moolenaar2b327002020-12-26 15:39:31 +010088 {
Bram Moolenaardc4451d2022-01-09 21:36:37 +000089 if (STRNCMP(p, "noclear", 7) == 0 && IS_WHITE_OR_NUL(p[7]))
90 {
91 if (found_noclear)
92 {
93 semsg(_(e_duplicate_argument_str), p);
94 return;
95 }
96 found_noclear = TRUE;
97 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +000098 else
99 {
100 semsg(_(e_invalid_argument_str), eap->arg);
101 return;
102 }
Bram Moolenaar2b327002020-12-26 15:39:31 +0100103 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000104
105 if (si->sn_state == SN_STATE_RELOAD && !found_noclear)
Bram Moolenaar2b327002020-12-26 15:39:31 +0100106 {
107 hashtab_T *ht = &SCRIPT_VARS(sid);
108
109 // Reloading a script without the "noclear" argument: clear
110 // script-local variables and functions.
111 hashtab_free_contents(ht);
112 hash_init(ht);
113 delete_script_functions(sid);
114
115 // old imports and script variables are no longer valid
116 free_imports_and_script_vars(sid);
117 }
118 si->sn_state = SN_STATE_HAD_COMMAND;
119
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000120 // Store the prefix with the script, it is used to find exported functions.
Bram Moolenaar71930f12022-01-13 13:47:43 +0000121 if (si->sn_autoload_prefix == NULL)
122 si->sn_autoload_prefix = get_autoload_prefix(si);
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000123
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100124 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
125 si->sn_version = SCRIPT_VERSION_VIM9;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100126
127 if (STRCMP(p_cpo, CPO_VIM) != 0)
128 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200129 si->sn_save_cpo = vim_strsave(p_cpo);
Bram Moolenaar37294bd2021-03-10 13:40:08 +0100130 set_option_value((char_u *)"cpo", 0L, (char_u *)CPO_VIM, OPT_NO_REDRAW);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100131 }
Bram Moolenaar9b8d6222020-12-28 18:26:00 +0100132#else
133 // No check for this being the first command, it doesn't matter.
134 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
135#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100136}
137
Dominique Pelle748b3082022-01-08 12:41:16 +0000138#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100139/*
Bram Moolenaarae616492020-07-28 20:07:27 +0200140 * When in Vim9 script give an error and return FAIL.
141 */
142 int
143not_in_vim9(exarg_T *eap)
144{
Bram Moolenaar68e30442020-07-28 21:15:07 +0200145 if (in_vim9script())
146 switch (eap->cmdidx)
147 {
Bram Moolenaarada1d872021-02-20 08:16:51 +0100148 case CMD_k:
149 if (eap->addr_count > 0)
150 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000151 emsg(_(e_no_range_allowed));
Bram Moolenaarada1d872021-02-20 08:16:51 +0100152 return FAIL;
153 }
154 // FALLTHROUGH
Bram Moolenaar68e30442020-07-28 21:15:07 +0200155 case CMD_append:
156 case CMD_change:
Bram Moolenaarf5a48012020-08-01 17:00:03 +0200157 case CMD_insert:
Bram Moolenaar65088802021-03-13 21:07:21 +0100158 case CMD_open:
Bram Moolenaarf5a48012020-08-01 17:00:03 +0200159 case CMD_t:
Bram Moolenaar68e30442020-07-28 21:15:07 +0200160 case CMD_xit:
Bram Moolenaar9b8d6222020-12-28 18:26:00 +0100161 semsg(_(e_command_not_supported_in_vim9_script_missing_var_str), eap->cmd);
Bram Moolenaar68e30442020-07-28 21:15:07 +0200162 return FAIL;
163 default: break;
164 }
Bram Moolenaarae616492020-07-28 20:07:27 +0200165 return OK;
166}
167
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100168/*
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100169 * Give an error message if "p" points at "#{" and return TRUE.
170 * This avoids that using a legacy style #{} dictionary leads to difficult to
171 * understand errors.
172 */
173 int
174vim9_bad_comment(char_u *p)
175{
Bram Moolenaara0399ef2021-03-20 15:00:01 +0100176 if (p[0] == '#' && p[1] == '{' && p[2] != '{')
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100177 {
178 emsg(_(e_cannot_use_hash_curly_to_start_comment));
179 return TRUE;
180 }
181 return FALSE;
182}
Dominique Pelle748b3082022-01-08 12:41:16 +0000183#endif
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100184
185/*
Bram Moolenaara0399ef2021-03-20 15:00:01 +0100186 * Return TRUE if "p" points at a "#" not followed by one '{'.
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100187 * Does not check for white space.
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100188 */
189 int
190vim9_comment_start(char_u *p)
191{
Bram Moolenaara0399ef2021-03-20 15:00:01 +0100192 return p[0] == '#' && (p[1] != '{' || p[2] == '{');
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100193}
194
Bram Moolenaar9b8d6222020-12-28 18:26:00 +0100195#if defined(FEAT_EVAL) || defined(PROTO)
196
Bram Moolenaarae616492020-07-28 20:07:27 +0200197/*
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200198 * "++nr" and "--nr" commands.
199 */
200 void
201ex_incdec(exarg_T *eap)
202{
203 char_u *cmd = eap->cmd;
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200204 char_u *nextcmd = eap->nextcmd;
205 size_t len = STRLEN(eap->cmd) + 8;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200206
Bram Moolenaar22480d12021-06-25 21:31:09 +0200207 if (VIM_ISWHITE(cmd[2]))
208 {
209 semsg(_(e_no_white_space_allowed_after_str_str),
210 eap->cmdidx == CMD_increment ? "++" : "--", eap->cmd);
211 return;
212 }
213
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200214 // This works like "nr += 1" or "nr -= 1".
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200215 // Add a '|' to avoid looking in the next line.
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200216 eap->cmd = alloc(len);
217 if (eap->cmd == NULL)
218 return;
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200219 vim_snprintf((char *)eap->cmd, len, "%s %c= 1 |", cmd + 2,
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200220 eap->cmdidx == CMD_increment ? '+' : '-');
221 eap->arg = eap->cmd;
222 eap->cmdidx = CMD_var;
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200223 eap->nextcmd = NULL;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200224 ex_let(eap);
225 vim_free(eap->cmd);
226 eap->cmd = cmd;
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200227 eap->nextcmd = nextcmd;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200228}
229
230/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100231 * ":export let Name: type"
232 * ":export const Name: type"
233 * ":export def Name(..."
234 * ":export class Name ..."
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100235 */
236 void
Bram Moolenaar09689a02020-05-09 22:50:08 +0200237ex_export(exarg_T *eap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100238{
Bram Moolenaar17d36cb2022-01-12 11:46:40 +0000239 int prev_did_emsg = did_emsg;
240
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200241 if (!in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100242 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200243 emsg(_(e_export_can_only_be_used_in_vim9script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100244 return;
245 }
246
247 eap->cmd = eap->arg;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +0100248 (void)find_ex_command(eap, NULL, lookup_scriptitem, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100249 switch (eap->cmdidx)
250 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200251 case CMD_var:
252 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100253 case CMD_const:
254 case CMD_def:
Bram Moolenaar160aa862022-01-10 21:29:57 +0000255 case CMD_function:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100256 // case CMD_class:
257 is_export = TRUE;
258 do_cmdline(eap->cmd, eap->getline, eap->cookie,
259 DOCMD_VERBOSE + DOCMD_NOWAIT);
260
261 // The command will reset "is_export" when exporting an item.
262 if (is_export)
263 {
Bram Moolenaar17d36cb2022-01-12 11:46:40 +0000264 if (did_emsg == prev_did_emsg)
265 emsg(_(e_export_with_invalid_argument));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100266 is_export = FALSE;
267 }
268 break;
269 default:
Bram Moolenaar17d36cb2022-01-12 11:46:40 +0000270 if (did_emsg == prev_did_emsg)
271 emsg(_(e_invalid_command_after_export));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100272 break;
273 }
274}
275
276/*
277 * Add a new imported item entry to the current script.
278 */
279 static imported_T *
280new_imported(garray_T *gap)
281{
282 if (ga_grow(gap, 1) == OK)
283 return ((imported_T *)gap->ga_data + gap->ga_len++);
284 return NULL;
285}
286
287/*
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200288 * Free the script variables from "sn_all_vars".
289 */
290 static void
291free_all_script_vars(scriptitem_T *si)
292{
293 int todo;
294 hashtab_T *ht = &si->sn_all_vars.dv_hashtab;
295 hashitem_T *hi;
296 sallvar_T *sav;
297 sallvar_T *sav_next;
Bram Moolenaardd297bc2021-12-10 10:37:38 +0000298 int idx;
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200299
300 hash_lock(ht);
301 todo = (int)ht->ht_used;
302 for (hi = ht->ht_array; todo > 0; ++hi)
303 {
304 if (!HASHITEM_EMPTY(hi))
305 {
306 --todo;
307
308 // Free the variable. Don't remove it from the hashtab, ht_array
309 // might change then. hash_clear() takes care of it later.
310 sav = HI2SAV(hi);
311 while (sav != NULL)
312 {
313 sav_next = sav->sav_next;
314 if (sav->sav_di == NULL)
315 clear_tv(&sav->sav_tv);
316 vim_free(sav);
317 sav = sav_next;
318 }
319 }
320 }
321 hash_clear(ht);
322 hash_init(ht);
323
Bram Moolenaardd297bc2021-12-10 10:37:38 +0000324 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
325 {
326 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
327
328 if (sv->sv_type_allocated)
329 free_type(sv->sv_type);
330 }
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200331 ga_clear(&si->sn_var_vals);
332
333 // existing commands using script variable indexes are no longer valid
334 si->sn_script_seq = current_sctx.sc_seq;
335}
336
337/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100338 * Free all imported items in script "sid".
339 */
340 void
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200341free_imports_and_script_vars(int sid)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100342{
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100343 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100344 int idx;
345
346 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
347 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100348 imported_T *imp = ((imported_T *)si->sn_imports.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100349
350 vim_free(imp->imp_name);
351 }
352 ga_clear(&si->sn_imports);
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200353
354 free_all_script_vars(si);
355
Bram Moolenaar6110e792020-07-08 19:35:21 +0200356 clear_type_list(&si->sn_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100357}
358
359/*
Bram Moolenaara6294952020-12-27 13:39:50 +0100360 * Mark all imports as possible to redefine. Used when a script is loaded
361 * again but not cleared.
362 */
363 void
364mark_imports_for_reload(int sid)
365{
366 scriptitem_T *si = SCRIPT_ITEM(sid);
367 int idx;
368
369 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
370 {
371 imported_T *imp = ((imported_T *)si->sn_imports.ga_data) + idx;
372
373 imp->imp_flags |= IMP_FLAGS_RELOAD;
374 }
375}
376
377/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100378 * Handle an ":import" command and add the resulting imported_T to "gap", when
379 * not NULL, or script "import_sid" sn_imports.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200380 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100381 * Returns a pointer to after the command or NULL in case of failure
382 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200383 static char_u *
Bram Moolenaar1c991142020-07-04 13:15:31 +0200384handle_import(
385 char_u *arg_start,
386 garray_T *gap,
387 int import_sid,
388 evalarg_T *evalarg,
389 void *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100390{
391 char_u *arg = arg_start;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000392 char_u *nextarg;
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000393 int is_autoload = FALSE;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000394 int getnext;
395 char_u *expr_end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100396 int ret = FAIL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000397 char_u *as_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100398 typval_T tv;
Bram Moolenaar1836d612022-01-18 13:14:47 +0000399 int sid = -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100400 int res;
Bram Moolenaar921ba522021-07-29 22:25:05 +0200401 long start_lnum = SOURCING_LNUM;
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000402 garray_T *import_gap;
403 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100404
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000405 if (STRNCMP(arg, "autoload", 8) == 0 && VIM_ISWHITE(arg[8]))
406 {
407 is_autoload = TRUE;
408 arg = skipwhite(arg + 8);
409 }
410
Bram Moolenaar4db572e2021-07-18 18:21:38 +0200411 // The name of the file can be an expression, which must evaluate to a
412 // string.
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000413 ret = eval0_retarg(arg, &tv, NULL, evalarg, &expr_end);
Bram Moolenaar4db572e2021-07-18 18:21:38 +0200414 if (ret == FAIL)
415 goto erret;
416 if (tv.v_type != VAR_STRING
417 || tv.vval.v_string == NULL || *tv.vval.v_string == NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100418 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000419 semsg(_(e_invalid_string_for_import_str), arg);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200420 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100421 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100422
Bram Moolenaar921ba522021-07-29 22:25:05 +0200423 // Give error messages for the start of the line.
424 SOURCING_LNUM = start_lnum;
425
Bram Moolenaar1c991142020-07-04 13:15:31 +0200426 /*
427 * find script file
428 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100429 if (*tv.vval.v_string == '.')
430 {
431 size_t len;
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100432 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100433 char_u *tail = gettail(si->sn_name);
434 char_u *from_name;
435
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000436 if (is_autoload)
437 res = FAIL;
438 else
439 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100440
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000441 // Relative to current script: "./name.vim", "../../name.vim".
442 len = STRLEN(si->sn_name) - STRLEN(tail)
443 + STRLEN(tv.vval.v_string) + 2;
444 from_name = alloc((int)len);
445 if (from_name == NULL)
446 goto erret;
447 vim_strncpy(from_name, si->sn_name, tail - si->sn_name);
448 add_pathsep(from_name);
449 STRCAT(from_name, tv.vval.v_string);
450 simplify_filename(from_name);
451
452 res = do_source(from_name, FALSE, DOSO_NONE, &sid);
453 vim_free(from_name);
454 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100455 }
Bram Moolenaar113b8dc2022-01-18 13:43:58 +0000456 else if (mch_isFullName(tv.vval.v_string)
457#ifdef BACKSLASH_IN_FILENAME
458 // On MS-Windows omitting the drive is still handled like an
459 // absolute path, not using 'runtimepath'.
460 || *tv.vval.v_string == '/' || *tv.vval.v_string == '\\'
461#endif
462 )
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100463 {
464 // Absolute path: "/tmp/name.vim"
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000465 if (is_autoload)
466 res = FAIL;
467 else
468 res = do_source(tv.vval.v_string, FALSE, DOSO_NONE, &sid);
469 }
470 else if (is_autoload)
471 {
472 size_t len = 9 + STRLEN(tv.vval.v_string) + 1;
473 char_u *from_name;
474
475 // Find file in "autoload" subdirs in 'runtimepath'.
476 from_name = alloc((int)len);
477 if (from_name == NULL)
478 goto erret;
479 vim_snprintf((char *)from_name, len, "autoload/%s", tv.vval.v_string);
480 // we need a scriptitem without loading the script
481 sid = find_script_in_rtp(from_name);
482 vim_free(from_name);
Bram Moolenaard041f422022-01-12 19:54:00 +0000483 if (SCRIPT_ID_VALID(sid))
484 {
485 scriptitem_T *si = SCRIPT_ITEM(sid);
486
487 if (si->sn_autoload_prefix == NULL)
488 si->sn_autoload_prefix = get_autoload_prefix(si);
489 res = OK;
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +0000490 if (override_autoload && si->sn_state == SN_STATE_NOT_LOADED)
491 // testing override: load autoload script right away
492 (void)do_source(si->sn_name, FALSE, DOSO_NONE, NULL);
Bram Moolenaard041f422022-01-12 19:54:00 +0000493 }
494 else
495 res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100496 }
497 else
498 {
499 size_t len = 7 + STRLEN(tv.vval.v_string) + 1;
500 char_u *from_name;
501
502 // Find file in "import" subdirs in 'runtimepath'.
503 from_name = alloc((int)len);
504 if (from_name == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200505 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100506 vim_snprintf((char *)from_name, len, "import/%s", tv.vval.v_string);
507 res = source_in_path(p_rtp, from_name, DIP_NOAFTER, &sid);
508 vim_free(from_name);
509 }
510
511 if (res == FAIL || sid <= 0)
512 {
Bram Moolenaar1836d612022-01-18 13:14:47 +0000513 semsg(_(is_autoload && sid == -2
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000514 ? e_autoload_import_cannot_use_absolute_or_relative_path
515 : e_could_not_import_str), tv.vval.v_string);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200516 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100517 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100518
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000519 import_gap = gap != NULL ? gap : &SCRIPT_ITEM(import_sid)->sn_imports;
520 for (i = 0; i < import_gap->ga_len; ++i)
521 {
522 imported_T *import = (imported_T *)import_gap->ga_data + i;
523
524 if (import->imp_sid == sid)
525 {
526 if (import->imp_flags & IMP_FLAGS_RELOAD)
527 {
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000528 // encountering same script first time on a reload is OK
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000529 import->imp_flags &= ~IMP_FLAGS_RELOAD;
530 break;
531 }
532 semsg(_(e_cannot_import_same_script_twice_str), tv.vval.v_string);
533 goto erret;
534 }
535 }
536
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000537 // Allow for the "as Name" to be in the next line.
538 nextarg = eval_next_non_blank(expr_end, evalarg, &getnext);
539 if (STRNCMP("as", nextarg, 2) == 0 && IS_WHITE_OR_NUL(nextarg[2]))
540 {
541 char_u *p;
542
543 if (getnext)
544 arg = eval_next_line(evalarg);
545 else
546 arg = nextarg;
547
548 // Skip over "as Name "; no line break allowed after "as".
549 // Do not allow for ':' and '#'.
550 arg = skipwhite(arg + 2);
551 p = arg;
552 if (eval_isnamec1(*arg))
553 while (ASCII_ISALNUM(*arg) || *arg == '_')
554 ++arg;
555 if (p == arg || !IS_WHITE_OR_NUL(*arg))
556 {
557 semsg(_(e_syntax_error_in_import_str), p);
558 goto erret;
559 }
560 as_name = vim_strnsave(p, arg - p);
561 arg = skipwhite(arg);
562 }
563 else
564 {
565 char_u *p = gettail(tv.vval.v_string);
566 char_u *end = (char_u *)strstr((char *)p, ".vim");
567
568 if (!ends_excmd2(arg_start, expr_end))
569 {
570 semsg(_(e_trailing_characters_str), expr_end);
571 goto erret;
572 }
Bram Moolenaar834d4182022-01-07 13:38:24 +0000573 if (end == NULL || end[4] != NUL)
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000574 {
Bram Moolenaar834d4182022-01-07 13:38:24 +0000575 semsg(_(e_imported_script_must_use_as_or_end_in_dot_vim_str), p);
576 goto erret;
577 }
578 if (end == p)
579 {
580 semsg(_(e_cannot_import_dot_vim_without_using_as), p);
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000581 goto erret;
582 }
583 as_name = vim_strnsave(p, end - p);
584 }
585
586 if (as_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100587 {
Bram Moolenaar0a842842021-02-27 22:41:19 +0100588 imported_T *imported;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100589
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000590 imported = find_imported(as_name, FALSE, STRLEN(as_name), cctx);
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000591 if (imported != NULL && imported->imp_sid != sid)
Bram Moolenaara6294952020-12-27 13:39:50 +0100592 {
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000593 semsg(_(e_name_already_defined_str), as_name);
594 goto erret;
Bram Moolenaara6294952020-12-27 13:39:50 +0100595 }
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000596 else if (imported == NULL
597 && check_defined(as_name, STRLEN(as_name), cctx, FALSE) == FAIL)
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000598 goto erret;
Bram Moolenaara6294952020-12-27 13:39:50 +0100599
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100600 if (imported == NULL)
Bram Moolenaar17d36cb2022-01-12 11:46:40 +0000601 {
602 imported = new_imported(import_gap);
603 if (imported == NULL)
604 goto erret;
605 imported->imp_name = as_name;
606 as_name = NULL;
607 imported->imp_sid = sid;
608 if (is_autoload)
609 imported->imp_flags = IMP_FLAGS_AUTOLOAD;
610 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100611 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200612
Bram Moolenaar1c991142020-07-04 13:15:31 +0200613erret:
Bram Moolenaar4db572e2021-07-18 18:21:38 +0200614 clear_tv(&tv);
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000615 vim_free(as_name);
616 return arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100617}
618
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200619/*
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000620 * ":import 'filename'"
621 * ":import 'filename' as Name"
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200622 */
623 void
624ex_import(exarg_T *eap)
625{
626 char_u *cmd_end;
627 evalarg_T evalarg;
628
629 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
630 {
631 emsg(_(e_import_can_only_be_used_in_script));
632 return;
633 }
634 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
635
636 cmd_end = handle_import(eap->arg, NULL, current_sctx.sc_sid,
637 &evalarg, NULL);
638 if (cmd_end != NULL)
639 set_nextcmd(eap, cmd_end);
640 clear_evalarg(&evalarg, eap);
641}
642
643/*
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000644 * Find an exported item in "sid" matching "name".
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200645 * When it is a variable return the index.
646 * When it is a user function return "*ufunc".
647 * When not found returns -1 and "*ufunc" is NULL.
648 */
649 int
650find_exported(
651 int sid,
652 char_u *name,
653 ufunc_T **ufunc,
654 type_T **type,
655 cctx_T *cctx,
656 int verbose)
657{
658 int idx = -1;
659 svar_T *sv;
660 scriptitem_T *script = SCRIPT_ITEM(sid);
661
662 // Find name in "script".
663 idx = get_script_item_idx(sid, name, 0, cctx);
664 if (idx >= 0)
665 {
666 sv = ((svar_T *)script->sn_var_vals.ga_data) + idx;
Bram Moolenaara909c482022-01-06 22:07:57 +0000667 *ufunc = NULL;
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200668 if (!sv->sv_export)
669 {
670 if (verbose)
671 semsg(_(e_item_not_exported_in_script_str), name);
672 return -1;
673 }
674 *type = sv->sv_type;
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200675 }
676 else
677 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +0000678 size_t len = STRLEN(name);
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200679 char_u buffer[200];
680 char_u *funcname;
681
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +0000682 // It could be a user function. Normally this is stored as
683 // "<SNR>99_name". For an autoload script a function is stored with
684 // the autoload prefix: "dir#script#name".
685 if (script->sn_autoload_prefix != NULL)
686 len += STRLEN(script->sn_autoload_prefix) + 2;
687 else
688 len += 15;
689
690 if (len < sizeof(buffer))
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200691 funcname = buffer;
692 else
693 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +0000694 funcname = alloc(len);
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200695 if (funcname == NULL)
696 return -1;
697 }
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +0000698 if (script->sn_autoload_prefix != NULL)
699 {
700 sprintf((char *)funcname, "%s%s", script->sn_autoload_prefix, name);
701 }
702 else
703 {
704 funcname[0] = K_SPECIAL;
705 funcname[1] = KS_EXTRA;
706 funcname[2] = (int)KE_SNR;
707 sprintf((char *)funcname + 3, "%ld_%s", (long)sid, name);
708 }
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000709 *ufunc = find_func(funcname, FALSE);
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200710
711 if (*ufunc == NULL)
712 {
713 if (verbose)
Bram Moolenaard02dce22022-01-18 17:43:04 +0000714 {
715 ufunc_T *alt_ufunc = NULL;
716
717 if (script->sn_autoload_prefix != NULL)
718 {
719 // try find the function by the script-local name
720 funcname[0] = K_SPECIAL;
721 funcname[1] = KS_EXTRA;
722 funcname[2] = (int)KE_SNR;
723 sprintf((char *)funcname + 3, "%ld_%s", (long)sid, name);
724 alt_ufunc = find_func(funcname, FALSE);
725 }
726 if (alt_ufunc != NULL)
727 semsg(_(e_item_not_exported_in_script_str), name);
728 else
729 semsg(_(e_item_not_found_in_script_str), name);
730 }
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200731 }
732 else if (((*ufunc)->uf_flags & FC_EXPORT) == 0)
733 {
734 if (verbose)
735 semsg(_(e_item_not_exported_in_script_str), name);
736 *ufunc = NULL;
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200737 }
Bram Moolenaard02dce22022-01-18 17:43:04 +0000738 if (funcname != buffer)
739 vim_free(funcname);
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200740 }
741
742 return idx;
743}
744
745/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200746 * Declare a script-local variable without init: "let var: type".
747 * "const" is an error since the value is missing.
748 * Returns a pointer to after the type.
749 */
750 char_u *
751vim9_declare_scriptvar(exarg_T *eap, char_u *arg)
752{
753 char_u *p;
754 char_u *name;
755 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
756 type_T *type;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200757 typval_T init_tv;
758
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200759 if (eap->cmdidx == CMD_final || eap->cmdidx == CMD_const)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200760 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200761 if (eap->cmdidx == CMD_final)
762 emsg(_(e_final_requires_a_value));
763 else
764 emsg(_(e_const_requires_a_value));
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200765 return arg + STRLEN(arg);
766 }
767
768 // Check for valid starting character.
769 if (!eval_isnamec1(*arg))
770 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000771 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200772 return arg + STRLEN(arg);
773 }
774
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200775 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
Bram Moolenaar3b74b6b2020-06-19 19:01:43 +0200776 if (*p == ':' && (VIM_ISWHITE(p[1]) || p != arg + 1))
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200777 break;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200778
779 if (*p != ':')
780 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +0200781 emsg(_(e_type_or_initialization_required));
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200782 return arg + STRLEN(arg);
783 }
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200784 if (!VIM_ISWHITE(p[1]))
785 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100786 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200787 return arg + STRLEN(arg);
788 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200789 name = vim_strnsave(arg, p - arg);
790
Bram Moolenaard0edaf92021-05-28 21:06:08 +0200791 // parse type, check for reserved name
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200792 p = skipwhite(p + 1);
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100793 type = parse_type(&p, &si->sn_type_list, TRUE);
Bram Moolenaard0edaf92021-05-28 21:06:08 +0200794 if (type == NULL || check_reserved_name(name) == FAIL)
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200795 {
796 vim_free(name);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200797 return p;
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200798 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200799
800 // Create the variable with 0/NULL value.
801 CLEAR_FIELD(init_tv);
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +0200802 if (type->tt_type == VAR_ANY)
803 // A variable of type "any" is not possible, just use zero instead
804 init_tv.v_type = VAR_NUMBER;
805 else
806 init_tv.v_type = type->tt_type;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000807 set_var_const(name, 0, type, &init_tv, FALSE, 0, 0);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200808
809 vim_free(name);
810 return p;
811}
812
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200813/*
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200814 * Vim9 part of adding a script variable: add it to sn_all_vars (lookup by name
815 * with a hashtable) and sn_var_vals (lookup by index).
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100816 * When "create" is TRUE this is a new variable, otherwise find and update an
817 * existing variable.
Bram Moolenaar08251752021-01-11 21:20:18 +0100818 * "flags" can have ASSIGN_FINAL or ASSIGN_CONST.
Bram Moolenaarf2253962021-04-13 20:53:13 +0200819 * When "*type" is NULL use "tv" for the type and update "*type". If
820 * "do_member" is TRUE also use the member type, otherwise use "any".
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200821 */
822 void
Bram Moolenaar08251752021-01-11 21:20:18 +0100823update_vim9_script_var(
824 int create,
825 dictitem_T *di,
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +0000826 char_u *name,
Bram Moolenaar08251752021-01-11 21:20:18 +0100827 int flags,
828 typval_T *tv,
Bram Moolenaarf2253962021-04-13 20:53:13 +0200829 type_T **type,
830 int do_member)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200831{
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100832 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
833 hashitem_T *hi;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200834 svar_T *sv = NULL;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200835
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100836 if (create)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200837 {
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200838 sallvar_T *newsav;
839 sallvar_T *sav = NULL;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200840
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100841 // Store a pointer to the typval_T, so that it can be found by index
842 // instead of using a hastab lookup.
843 if (ga_grow(&si->sn_var_vals, 1) == FAIL)
844 return;
845
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +0000846 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200847 if (!HASHITEM_EMPTY(hi))
848 {
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200849 // Variable with this name exists, either in this block or in
850 // another block.
851 for (sav = HI2SAV(hi); ; sav = sav->sav_next)
852 {
853 if (sav->sav_block_id == si->sn_current_block_id)
854 {
855 // variable defined in a loop, re-use the entry
856 sv = ((svar_T *)si->sn_var_vals.ga_data)
857 + sav->sav_var_vals_idx;
858 // unhide the variable
859 if (sv->sv_tv == &sav->sav_tv)
860 {
861 clear_tv(&sav->sav_tv);
862 sv->sv_tv = &di->di_tv;
863 sav->sav_di = di;
864 }
865 break;
866 }
867 if (sav->sav_next == NULL)
868 break;
869 }
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200870 }
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200871
872 if (sv == NULL)
873 {
874 // Variable not defined or not defined in current block: Add a
875 // svar_T and create a new sallvar_T.
876 sv = ((svar_T *)si->sn_var_vals.ga_data) + si->sn_var_vals.ga_len;
877 newsav = (sallvar_T *)alloc_clear(
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +0000878 sizeof(sallvar_T) + STRLEN(name));
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200879 if (newsav == NULL)
880 return;
881
882 sv->sv_tv = &di->di_tv;
883 sv->sv_const = (flags & ASSIGN_FINAL) ? ASSIGN_FINAL
884 : (flags & ASSIGN_CONST) ? ASSIGN_CONST : 0;
885 sv->sv_export = is_export;
886 newsav->sav_var_vals_idx = si->sn_var_vals.ga_len;
887 ++si->sn_var_vals.ga_len;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +0000888 STRCPY(&newsav->sav_key, name);
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200889 sv->sv_name = newsav->sav_key;
890 newsav->sav_di = di;
891 newsav->sav_block_id = si->sn_current_block_id;
892
893 if (HASHITEM_EMPTY(hi))
894 // new variable name
895 hash_add(&si->sn_all_vars.dv_hashtab, newsav->sav_key);
896 else if (sav != NULL)
897 // existing name in a new block, append to the list
898 sav->sav_next = newsav;
899 }
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200900 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100901 else
902 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000903 sv = find_typval_in_script(&di->di_tv, 0);
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100904 }
905 if (sv != NULL)
906 {
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100907 if (*type == NULL)
Bram Moolenaar114dbda2022-01-03 12:28:03 +0000908 *type = typval2type(tv, get_copyID(), &si->sn_type_list,
909 do_member ? TVTT_DO_MEMBER : 0);
Bram Moolenaardd297bc2021-12-10 10:37:38 +0000910 if (sv->sv_type_allocated)
911 free_type(sv->sv_type);
912 if (*type != NULL && ((*type)->tt_type == VAR_FUNC
913 || (*type)->tt_type == VAR_PARTIAL))
914 {
915 // The type probably uses uf_type_list, which is cleared when the
916 // function is freed, but the script variable may keep the type.
917 // Make a copy to avoid using freed memory.
918 sv->sv_type = alloc_type(*type);
919 sv->sv_type_allocated = TRUE;
920 }
921 else
922 {
923 sv->sv_type = *type;
924 sv->sv_type_allocated = FALSE;
925 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100926 }
927
928 // let ex_export() know the export worked.
929 is_export = FALSE;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200930}
931
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200932/*
933 * Hide a script variable when leaving a block.
934 * "idx" is de index in sn_var_vals.
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200935 * When "func_defined" is non-zero then a function was defined in this block,
936 * the variable may be accessed by it. Otherwise the variable can be cleared.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200937 */
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200938 void
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200939hide_script_var(scriptitem_T *si, int idx, int func_defined)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200940{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200941 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200942 hashtab_T *script_ht = get_script_local_ht();
943 hashtab_T *all_ht = &si->sn_all_vars.dv_hashtab;
944 hashitem_T *script_hi;
945 hashitem_T *all_hi;
946
947 // Remove a variable declared inside the block, if it still exists.
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200948 // If it was added in a nested block it will already have been removed.
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200949 // The typval is moved into the sallvar_T.
950 script_hi = hash_find(script_ht, sv->sv_name);
951 all_hi = hash_find(all_ht, sv->sv_name);
952 if (!HASHITEM_EMPTY(script_hi) && !HASHITEM_EMPTY(all_hi))
953 {
954 dictitem_T *di = HI2DI(script_hi);
955 sallvar_T *sav = HI2SAV(all_hi);
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200956 sallvar_T *sav_prev = NULL;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200957
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200958 // There can be multiple entries with the same name in different
959 // blocks, find the right one.
960 while (sav != NULL && sav->sav_var_vals_idx != idx)
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200961 {
962 sav_prev = sav;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200963 sav = sav->sav_next;
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200964 }
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200965 if (sav != NULL)
966 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200967 if (func_defined)
968 {
969 // move the typval from the dictitem to the sallvar
970 sav->sav_tv = di->di_tv;
971 di->di_tv.v_type = VAR_UNKNOWN;
972 sav->sav_flags = di->di_flags;
973 sav->sav_di = NULL;
974 sv->sv_tv = &sav->sav_tv;
975 }
976 else
977 {
978 if (sav_prev == NULL)
979 hash_remove(all_ht, all_hi);
980 else
981 sav_prev->sav_next = sav->sav_next;
982 sv->sv_name = NULL;
983 vim_free(sav);
984 }
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200985 delete_var(script_ht, script_hi);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200986 }
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200987 }
988}
989
990/*
Bram Moolenaar10c65862020-10-08 21:16:42 +0200991 * Find the script-local variable that links to "dest".
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000992 * If "sid" is zero use the current script.
Bram Moolenaarc967d572021-07-08 21:38:50 +0200993 * Returns NULL if not found and give an internal error.
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200994 */
Bram Moolenaar10c65862020-10-08 21:16:42 +0200995 svar_T *
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000996find_typval_in_script(typval_T *dest, scid_T sid)
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200997{
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000998 scriptitem_T *si = SCRIPT_ITEM(sid == 0 ? current_sctx.sc_sid : sid);
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200999 int idx;
1000
Bram Moolenaar81e17fb2020-08-21 21:55:43 +02001001 if (si->sn_version != SCRIPT_VERSION_VIM9)
1002 // legacy script doesn't store variable types
Bram Moolenaar10c65862020-10-08 21:16:42 +02001003 return NULL;
Bram Moolenaar81e17fb2020-08-21 21:55:43 +02001004
Bram Moolenaara06758d2021-10-15 00:18:37 +01001005 // Find the svar_T in sn_var_vals. Start at the end, in a for loop the
1006 // variable was added at the end.
1007 for (idx = si->sn_var_vals.ga_len - 1; idx >= 0; --idx)
Bram Moolenaar34db91f2020-06-13 19:00:10 +02001008 {
1009 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1010
Bram Moolenaarc0913d02020-12-05 14:44:37 +01001011 // If "sv_name" is NULL the variable was hidden when leaving a block,
1012 // don't check "sv_tv" then, it might be used for another variable now.
1013 if (sv->sv_name != NULL && sv->sv_tv == dest)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001014 return sv;
Bram Moolenaar34db91f2020-06-13 19:00:10 +02001015 }
Bram Moolenaarc967d572021-07-08 21:38:50 +02001016 iemsg("find_typval_in_script(): not found");
Bram Moolenaar10c65862020-10-08 21:16:42 +02001017 return NULL;
1018}
1019
1020/*
1021 * Check if the type of script variable "dest" allows assigning "value".
1022 * If needed convert "value" to a bool.
1023 */
1024 int
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001025check_script_var_type(
Bram Moolenaar7824fc82021-11-26 17:36:51 +00001026 svar_T *sv,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001027 typval_T *value,
1028 char_u *name,
1029 where_T where)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001030{
Bram Moolenaar10c65862020-10-08 21:16:42 +02001031 int ret;
1032
Bram Moolenaar7824fc82021-11-26 17:36:51 +00001033 if (sv->sv_const != 0)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001034 {
Bram Moolenaar7824fc82021-11-26 17:36:51 +00001035 semsg(_(e_cannot_change_readonly_variable_str), name);
1036 return FAIL;
Bram Moolenaar10c65862020-10-08 21:16:42 +02001037 }
Bram Moolenaar7824fc82021-11-26 17:36:51 +00001038 ret = check_typval_type(sv->sv_type, value, where);
1039 if (ret == OK && need_convert_to_bool(sv->sv_type, value))
1040 {
1041 int val = tv2bool(value);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001042
Bram Moolenaar7824fc82021-11-26 17:36:51 +00001043 clear_tv(value);
1044 value->v_type = VAR_BOOL;
1045 value->v_lock = 0;
1046 value->vval.v_number = val ? VVAL_TRUE : VVAL_FALSE;
1047 }
1048 return ret;
Bram Moolenaar34db91f2020-06-13 19:00:10 +02001049}
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001050
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001051// words that cannot be used as a variable
1052static char *reserved[] = {
1053 "true",
1054 "false",
1055 "null",
Bram Moolenaar74235772021-06-12 14:53:05 +02001056 "this",
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001057 NULL
1058};
1059
1060 int
1061check_reserved_name(char_u *name)
1062{
1063 int idx;
1064
1065 for (idx = 0; reserved[idx] != NULL; ++idx)
1066 if (STRCMP(reserved[idx], name) == 0)
1067 {
1068 semsg(_(e_cannot_use_reserved_name), name);
1069 return FAIL;
1070 }
1071 return OK;
1072}
1073
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001074#endif // FEAT_EVAL