blob: 6d02be6adc98b057d9065bdbfed1f121dce2a9f7 [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;
72 int found_autoload = FALSE;
73 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010074
75 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
76 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +020077 emsg(_(e_vim9script_can_only_be_used_in_script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010078 return;
79 }
Bram Moolenaar2b327002020-12-26 15:39:31 +010080
81 si = SCRIPT_ITEM(sid);
82 if (si->sn_state == SN_STATE_HAD_COMMAND)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010083 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +020084 emsg(_(e_vim9script_must_be_first_command_in_script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010085 return;
86 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +000087
88 for (p = eap->arg; !IS_WHITE_OR_NUL(*p); p = skipwhite(skiptowhite(p)))
Bram Moolenaar2b327002020-12-26 15:39:31 +010089 {
Bram Moolenaardc4451d2022-01-09 21:36:37 +000090 if (STRNCMP(p, "noclear", 7) == 0 && IS_WHITE_OR_NUL(p[7]))
91 {
92 if (found_noclear)
93 {
94 semsg(_(e_duplicate_argument_str), p);
95 return;
96 }
97 found_noclear = TRUE;
98 }
99 else if (STRNCMP(p, "autoload", 8) == 0 && IS_WHITE_OR_NUL(p[8]))
100 {
101 if (found_autoload)
102 {
103 semsg(_(e_duplicate_argument_str), p);
104 return;
105 }
106 found_autoload = TRUE;
107 if (script_name_after_autoload(si) == NULL)
108 {
109 emsg(_(e_using_autoload_in_script_not_under_autoload_directory));
110 return;
111 }
112 }
113 else
114 {
115 semsg(_(e_invalid_argument_str), eap->arg);
116 return;
117 }
Bram Moolenaar2b327002020-12-26 15:39:31 +0100118 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000119
120 if (si->sn_state == SN_STATE_RELOAD && !found_noclear)
Bram Moolenaar2b327002020-12-26 15:39:31 +0100121 {
122 hashtab_T *ht = &SCRIPT_VARS(sid);
123
124 // Reloading a script without the "noclear" argument: clear
125 // script-local variables and functions.
126 hashtab_free_contents(ht);
127 hash_init(ht);
128 delete_script_functions(sid);
129
130 // old imports and script variables are no longer valid
131 free_imports_and_script_vars(sid);
132 }
133 si->sn_state = SN_STATE_HAD_COMMAND;
134
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +0000135 // Store the prefix with the script. It isused to find exported functions.
136 si->sn_autoload_prefix = get_autoload_prefix(si);
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000137
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100138 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
139 si->sn_version = SCRIPT_VERSION_VIM9;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100140
141 if (STRCMP(p_cpo, CPO_VIM) != 0)
142 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200143 si->sn_save_cpo = vim_strsave(p_cpo);
Bram Moolenaar37294bd2021-03-10 13:40:08 +0100144 set_option_value((char_u *)"cpo", 0L, (char_u *)CPO_VIM, OPT_NO_REDRAW);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100145 }
Bram Moolenaar9b8d6222020-12-28 18:26:00 +0100146#else
147 // No check for this being the first command, it doesn't matter.
148 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
149#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100150}
151
Dominique Pelle748b3082022-01-08 12:41:16 +0000152#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100153/*
Bram Moolenaarae616492020-07-28 20:07:27 +0200154 * When in Vim9 script give an error and return FAIL.
155 */
156 int
157not_in_vim9(exarg_T *eap)
158{
Bram Moolenaar68e30442020-07-28 21:15:07 +0200159 if (in_vim9script())
160 switch (eap->cmdidx)
161 {
Bram Moolenaarada1d872021-02-20 08:16:51 +0100162 case CMD_k:
163 if (eap->addr_count > 0)
164 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000165 emsg(_(e_no_range_allowed));
Bram Moolenaarada1d872021-02-20 08:16:51 +0100166 return FAIL;
167 }
168 // FALLTHROUGH
Bram Moolenaar68e30442020-07-28 21:15:07 +0200169 case CMD_append:
170 case CMD_change:
Bram Moolenaarf5a48012020-08-01 17:00:03 +0200171 case CMD_insert:
Bram Moolenaar65088802021-03-13 21:07:21 +0100172 case CMD_open:
Bram Moolenaarf5a48012020-08-01 17:00:03 +0200173 case CMD_t:
Bram Moolenaar68e30442020-07-28 21:15:07 +0200174 case CMD_xit:
Bram Moolenaar9b8d6222020-12-28 18:26:00 +0100175 semsg(_(e_command_not_supported_in_vim9_script_missing_var_str), eap->cmd);
Bram Moolenaar68e30442020-07-28 21:15:07 +0200176 return FAIL;
177 default: break;
178 }
Bram Moolenaarae616492020-07-28 20:07:27 +0200179 return OK;
180}
181
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100182/*
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100183 * Give an error message if "p" points at "#{" and return TRUE.
184 * This avoids that using a legacy style #{} dictionary leads to difficult to
185 * understand errors.
186 */
187 int
188vim9_bad_comment(char_u *p)
189{
Bram Moolenaara0399ef2021-03-20 15:00:01 +0100190 if (p[0] == '#' && p[1] == '{' && p[2] != '{')
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100191 {
192 emsg(_(e_cannot_use_hash_curly_to_start_comment));
193 return TRUE;
194 }
195 return FALSE;
196}
Dominique Pelle748b3082022-01-08 12:41:16 +0000197#endif
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100198
199/*
Bram Moolenaara0399ef2021-03-20 15:00:01 +0100200 * Return TRUE if "p" points at a "#" not followed by one '{'.
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100201 * Does not check for white space.
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100202 */
203 int
204vim9_comment_start(char_u *p)
205{
Bram Moolenaara0399ef2021-03-20 15:00:01 +0100206 return p[0] == '#' && (p[1] != '{' || p[2] == '{');
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100207}
208
Bram Moolenaar9b8d6222020-12-28 18:26:00 +0100209#if defined(FEAT_EVAL) || defined(PROTO)
210
Bram Moolenaarae616492020-07-28 20:07:27 +0200211/*
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200212 * "++nr" and "--nr" commands.
213 */
214 void
215ex_incdec(exarg_T *eap)
216{
217 char_u *cmd = eap->cmd;
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200218 char_u *nextcmd = eap->nextcmd;
219 size_t len = STRLEN(eap->cmd) + 8;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200220
Bram Moolenaar22480d12021-06-25 21:31:09 +0200221 if (VIM_ISWHITE(cmd[2]))
222 {
223 semsg(_(e_no_white_space_allowed_after_str_str),
224 eap->cmdidx == CMD_increment ? "++" : "--", eap->cmd);
225 return;
226 }
227
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200228 // This works like "nr += 1" or "nr -= 1".
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200229 // Add a '|' to avoid looking in the next line.
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200230 eap->cmd = alloc(len);
231 if (eap->cmd == NULL)
232 return;
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200233 vim_snprintf((char *)eap->cmd, len, "%s %c= 1 |", cmd + 2,
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200234 eap->cmdidx == CMD_increment ? '+' : '-');
235 eap->arg = eap->cmd;
236 eap->cmdidx = CMD_var;
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200237 eap->nextcmd = NULL;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200238 ex_let(eap);
239 vim_free(eap->cmd);
240 eap->cmd = cmd;
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200241 eap->nextcmd = nextcmd;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200242}
243
244/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100245 * ":export let Name: type"
246 * ":export const Name: type"
247 * ":export def Name(..."
248 * ":export class Name ..."
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100249 */
250 void
Bram Moolenaar09689a02020-05-09 22:50:08 +0200251ex_export(exarg_T *eap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100252{
Bram Moolenaar17d36cb2022-01-12 11:46:40 +0000253 int prev_did_emsg = did_emsg;
254
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200255 if (!in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100256 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200257 emsg(_(e_export_can_only_be_used_in_vim9script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100258 return;
259 }
260
261 eap->cmd = eap->arg;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +0100262 (void)find_ex_command(eap, NULL, lookup_scriptitem, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100263 switch (eap->cmdidx)
264 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200265 case CMD_var:
266 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100267 case CMD_const:
268 case CMD_def:
Bram Moolenaar160aa862022-01-10 21:29:57 +0000269 case CMD_function:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100270 // case CMD_class:
271 is_export = TRUE;
272 do_cmdline(eap->cmd, eap->getline, eap->cookie,
273 DOCMD_VERBOSE + DOCMD_NOWAIT);
274
275 // The command will reset "is_export" when exporting an item.
276 if (is_export)
277 {
Bram Moolenaar17d36cb2022-01-12 11:46:40 +0000278 if (did_emsg == prev_did_emsg)
279 emsg(_(e_export_with_invalid_argument));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100280 is_export = FALSE;
281 }
282 break;
283 default:
Bram Moolenaar17d36cb2022-01-12 11:46:40 +0000284 if (did_emsg == prev_did_emsg)
285 emsg(_(e_invalid_command_after_export));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100286 break;
287 }
288}
289
290/*
291 * Add a new imported item entry to the current script.
292 */
293 static imported_T *
294new_imported(garray_T *gap)
295{
296 if (ga_grow(gap, 1) == OK)
297 return ((imported_T *)gap->ga_data + gap->ga_len++);
298 return NULL;
299}
300
301/*
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200302 * Free the script variables from "sn_all_vars".
303 */
304 static void
305free_all_script_vars(scriptitem_T *si)
306{
307 int todo;
308 hashtab_T *ht = &si->sn_all_vars.dv_hashtab;
309 hashitem_T *hi;
310 sallvar_T *sav;
311 sallvar_T *sav_next;
Bram Moolenaardd297bc2021-12-10 10:37:38 +0000312 int idx;
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200313
314 hash_lock(ht);
315 todo = (int)ht->ht_used;
316 for (hi = ht->ht_array; todo > 0; ++hi)
317 {
318 if (!HASHITEM_EMPTY(hi))
319 {
320 --todo;
321
322 // Free the variable. Don't remove it from the hashtab, ht_array
323 // might change then. hash_clear() takes care of it later.
324 sav = HI2SAV(hi);
325 while (sav != NULL)
326 {
327 sav_next = sav->sav_next;
328 if (sav->sav_di == NULL)
329 clear_tv(&sav->sav_tv);
330 vim_free(sav);
331 sav = sav_next;
332 }
333 }
334 }
335 hash_clear(ht);
336 hash_init(ht);
337
Bram Moolenaardd297bc2021-12-10 10:37:38 +0000338 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
339 {
340 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
341
342 if (sv->sv_type_allocated)
343 free_type(sv->sv_type);
344 }
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200345 ga_clear(&si->sn_var_vals);
346
347 // existing commands using script variable indexes are no longer valid
348 si->sn_script_seq = current_sctx.sc_seq;
349}
350
351/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100352 * Free all imported items in script "sid".
353 */
354 void
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200355free_imports_and_script_vars(int sid)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100356{
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100357 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100358 int idx;
359
360 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
361 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100362 imported_T *imp = ((imported_T *)si->sn_imports.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100363
364 vim_free(imp->imp_name);
365 }
366 ga_clear(&si->sn_imports);
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200367
368 free_all_script_vars(si);
369
Bram Moolenaar6110e792020-07-08 19:35:21 +0200370 clear_type_list(&si->sn_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100371}
372
373/*
Bram Moolenaara6294952020-12-27 13:39:50 +0100374 * Mark all imports as possible to redefine. Used when a script is loaded
375 * again but not cleared.
376 */
377 void
378mark_imports_for_reload(int sid)
379{
380 scriptitem_T *si = SCRIPT_ITEM(sid);
381 int idx;
382
383 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
384 {
385 imported_T *imp = ((imported_T *)si->sn_imports.ga_data) + idx;
386
387 imp->imp_flags |= IMP_FLAGS_RELOAD;
388 }
389}
390
391/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100392 * Handle an ":import" command and add the resulting imported_T to "gap", when
393 * not NULL, or script "import_sid" sn_imports.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200394 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100395 * Returns a pointer to after the command or NULL in case of failure
396 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200397 static char_u *
Bram Moolenaar1c991142020-07-04 13:15:31 +0200398handle_import(
399 char_u *arg_start,
400 garray_T *gap,
401 int import_sid,
402 evalarg_T *evalarg,
403 void *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100404{
405 char_u *arg = arg_start;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000406 char_u *nextarg;
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000407 int is_autoload = FALSE;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000408 int getnext;
409 char_u *expr_end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100410 int ret = FAIL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000411 char_u *as_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100412 typval_T tv;
413 int sid = -1;
414 int res;
Bram Moolenaar921ba522021-07-29 22:25:05 +0200415 long start_lnum = SOURCING_LNUM;
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000416 garray_T *import_gap;
417 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100418
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000419 if (STRNCMP(arg, "autoload", 8) == 0 && VIM_ISWHITE(arg[8]))
420 {
421 is_autoload = TRUE;
422 arg = skipwhite(arg + 8);
423 }
424
Bram Moolenaar4db572e2021-07-18 18:21:38 +0200425 // The name of the file can be an expression, which must evaluate to a
426 // string.
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000427 ret = eval0_retarg(arg, &tv, NULL, evalarg, &expr_end);
Bram Moolenaar4db572e2021-07-18 18:21:38 +0200428 if (ret == FAIL)
429 goto erret;
430 if (tv.v_type != VAR_STRING
431 || tv.vval.v_string == NULL || *tv.vval.v_string == NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100432 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000433 semsg(_(e_invalid_string_for_import_str), arg);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200434 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100435 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100436
Bram Moolenaar921ba522021-07-29 22:25:05 +0200437 // Give error messages for the start of the line.
438 SOURCING_LNUM = start_lnum;
439
Bram Moolenaar1c991142020-07-04 13:15:31 +0200440 /*
441 * find script file
442 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100443 if (*tv.vval.v_string == '.')
444 {
445 size_t len;
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100446 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100447 char_u *tail = gettail(si->sn_name);
448 char_u *from_name;
449
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000450 if (is_autoload)
451 res = FAIL;
452 else
453 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100454
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000455 // Relative to current script: "./name.vim", "../../name.vim".
456 len = STRLEN(si->sn_name) - STRLEN(tail)
457 + STRLEN(tv.vval.v_string) + 2;
458 from_name = alloc((int)len);
459 if (from_name == NULL)
460 goto erret;
461 vim_strncpy(from_name, si->sn_name, tail - si->sn_name);
462 add_pathsep(from_name);
463 STRCAT(from_name, tv.vval.v_string);
464 simplify_filename(from_name);
465
466 res = do_source(from_name, FALSE, DOSO_NONE, &sid);
467 vim_free(from_name);
468 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100469 }
470 else if (mch_isFullName(tv.vval.v_string))
471 {
472 // Absolute path: "/tmp/name.vim"
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000473 if (is_autoload)
474 res = FAIL;
475 else
476 res = do_source(tv.vval.v_string, FALSE, DOSO_NONE, &sid);
477 }
478 else if (is_autoload)
479 {
480 size_t len = 9 + STRLEN(tv.vval.v_string) + 1;
481 char_u *from_name;
482
483 // Find file in "autoload" subdirs in 'runtimepath'.
484 from_name = alloc((int)len);
485 if (from_name == NULL)
486 goto erret;
487 vim_snprintf((char *)from_name, len, "autoload/%s", tv.vval.v_string);
488 // we need a scriptitem without loading the script
489 sid = find_script_in_rtp(from_name);
490 vim_free(from_name);
Bram Moolenaard041f422022-01-12 19:54:00 +0000491 if (SCRIPT_ID_VALID(sid))
492 {
493 scriptitem_T *si = SCRIPT_ITEM(sid);
494
495 if (si->sn_autoload_prefix == NULL)
496 si->sn_autoload_prefix = get_autoload_prefix(si);
497 res = OK;
498 }
499 else
500 res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100501 }
502 else
503 {
504 size_t len = 7 + STRLEN(tv.vval.v_string) + 1;
505 char_u *from_name;
506
507 // Find file in "import" subdirs in 'runtimepath'.
508 from_name = alloc((int)len);
509 if (from_name == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200510 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100511 vim_snprintf((char *)from_name, len, "import/%s", tv.vval.v_string);
512 res = source_in_path(p_rtp, from_name, DIP_NOAFTER, &sid);
513 vim_free(from_name);
514 }
515
516 if (res == FAIL || sid <= 0)
517 {
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000518 semsg(_(is_autoload && sid <= 0
519 ? e_autoload_import_cannot_use_absolute_or_relative_path
520 : e_could_not_import_str), tv.vval.v_string);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200521 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100522 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100523
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000524 import_gap = gap != NULL ? gap : &SCRIPT_ITEM(import_sid)->sn_imports;
525 for (i = 0; i < import_gap->ga_len; ++i)
526 {
527 imported_T *import = (imported_T *)import_gap->ga_data + i;
528
529 if (import->imp_sid == sid)
530 {
531 if (import->imp_flags & IMP_FLAGS_RELOAD)
532 {
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000533 // encountering same script first time on a reload is OK
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000534 import->imp_flags &= ~IMP_FLAGS_RELOAD;
535 break;
536 }
537 semsg(_(e_cannot_import_same_script_twice_str), tv.vval.v_string);
538 goto erret;
539 }
540 }
541
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000542 // Allow for the "as Name" to be in the next line.
543 nextarg = eval_next_non_blank(expr_end, evalarg, &getnext);
544 if (STRNCMP("as", nextarg, 2) == 0 && IS_WHITE_OR_NUL(nextarg[2]))
545 {
546 char_u *p;
547
548 if (getnext)
549 arg = eval_next_line(evalarg);
550 else
551 arg = nextarg;
552
553 // Skip over "as Name "; no line break allowed after "as".
554 // Do not allow for ':' and '#'.
555 arg = skipwhite(arg + 2);
556 p = arg;
557 if (eval_isnamec1(*arg))
558 while (ASCII_ISALNUM(*arg) || *arg == '_')
559 ++arg;
560 if (p == arg || !IS_WHITE_OR_NUL(*arg))
561 {
562 semsg(_(e_syntax_error_in_import_str), p);
563 goto erret;
564 }
565 as_name = vim_strnsave(p, arg - p);
566 arg = skipwhite(arg);
567 }
568 else
569 {
570 char_u *p = gettail(tv.vval.v_string);
571 char_u *end = (char_u *)strstr((char *)p, ".vim");
572
573 if (!ends_excmd2(arg_start, expr_end))
574 {
575 semsg(_(e_trailing_characters_str), expr_end);
576 goto erret;
577 }
Bram Moolenaar834d4182022-01-07 13:38:24 +0000578 if (end == NULL || end[4] != NUL)
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000579 {
Bram Moolenaar834d4182022-01-07 13:38:24 +0000580 semsg(_(e_imported_script_must_use_as_or_end_in_dot_vim_str), p);
581 goto erret;
582 }
583 if (end == p)
584 {
585 semsg(_(e_cannot_import_dot_vim_without_using_as), p);
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000586 goto erret;
587 }
588 as_name = vim_strnsave(p, end - p);
589 }
590
591 if (as_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100592 {
Bram Moolenaar0a842842021-02-27 22:41:19 +0100593 imported_T *imported;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100594
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000595 imported = find_imported(as_name, FALSE, STRLEN(as_name), cctx);
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000596 if (imported != NULL && imported->imp_sid != sid)
Bram Moolenaara6294952020-12-27 13:39:50 +0100597 {
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000598 semsg(_(e_name_already_defined_str), as_name);
599 goto erret;
Bram Moolenaara6294952020-12-27 13:39:50 +0100600 }
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000601 else if (imported == NULL
602 && check_defined(as_name, STRLEN(as_name), cctx, FALSE) == FAIL)
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000603 goto erret;
Bram Moolenaara6294952020-12-27 13:39:50 +0100604
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100605 if (imported == NULL)
Bram Moolenaar17d36cb2022-01-12 11:46:40 +0000606 {
607 imported = new_imported(import_gap);
608 if (imported == NULL)
609 goto erret;
610 imported->imp_name = as_name;
611 as_name = NULL;
612 imported->imp_sid = sid;
613 if (is_autoload)
614 imported->imp_flags = IMP_FLAGS_AUTOLOAD;
615 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100616 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200617
Bram Moolenaar1c991142020-07-04 13:15:31 +0200618erret:
Bram Moolenaar4db572e2021-07-18 18:21:38 +0200619 clear_tv(&tv);
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000620 vim_free(as_name);
621 return arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100622}
623
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200624/*
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000625 * ":import 'filename'"
626 * ":import 'filename' as Name"
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200627 */
628 void
629ex_import(exarg_T *eap)
630{
631 char_u *cmd_end;
632 evalarg_T evalarg;
633
634 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
635 {
636 emsg(_(e_import_can_only_be_used_in_script));
637 return;
638 }
639 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
640
641 cmd_end = handle_import(eap->arg, NULL, current_sctx.sc_sid,
642 &evalarg, NULL);
643 if (cmd_end != NULL)
644 set_nextcmd(eap, cmd_end);
645 clear_evalarg(&evalarg, eap);
646}
647
648/*
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000649 * Find an exported item in "sid" matching "name".
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200650 * When it is a variable return the index.
651 * When it is a user function return "*ufunc".
652 * When not found returns -1 and "*ufunc" is NULL.
653 */
654 int
655find_exported(
656 int sid,
657 char_u *name,
658 ufunc_T **ufunc,
659 type_T **type,
660 cctx_T *cctx,
661 int verbose)
662{
663 int idx = -1;
664 svar_T *sv;
665 scriptitem_T *script = SCRIPT_ITEM(sid);
666
667 // Find name in "script".
668 idx = get_script_item_idx(sid, name, 0, cctx);
669 if (idx >= 0)
670 {
671 sv = ((svar_T *)script->sn_var_vals.ga_data) + idx;
Bram Moolenaara909c482022-01-06 22:07:57 +0000672 *ufunc = NULL;
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200673 if (!sv->sv_export)
674 {
675 if (verbose)
676 semsg(_(e_item_not_exported_in_script_str), name);
677 return -1;
678 }
679 *type = sv->sv_type;
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200680 }
681 else
682 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +0000683 size_t len = STRLEN(name);
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200684 char_u buffer[200];
685 char_u *funcname;
686
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +0000687 // It could be a user function. Normally this is stored as
688 // "<SNR>99_name". For an autoload script a function is stored with
689 // the autoload prefix: "dir#script#name".
690 if (script->sn_autoload_prefix != NULL)
691 len += STRLEN(script->sn_autoload_prefix) + 2;
692 else
693 len += 15;
694
695 if (len < sizeof(buffer))
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200696 funcname = buffer;
697 else
698 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +0000699 funcname = alloc(len);
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200700 if (funcname == NULL)
701 return -1;
702 }
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +0000703 if (script->sn_autoload_prefix != NULL)
704 {
705 sprintf((char *)funcname, "%s%s", script->sn_autoload_prefix, name);
706 }
707 else
708 {
709 funcname[0] = K_SPECIAL;
710 funcname[1] = KS_EXTRA;
711 funcname[2] = (int)KE_SNR;
712 sprintf((char *)funcname + 3, "%ld_%s", (long)sid, name);
713 }
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200714 *ufunc = find_func(funcname, FALSE, NULL);
715 if (funcname != buffer)
716 vim_free(funcname);
717
718 if (*ufunc == NULL)
719 {
720 if (verbose)
721 semsg(_(e_item_not_found_in_script_str), name);
722 return -1;
723 }
724 else if (((*ufunc)->uf_flags & FC_EXPORT) == 0)
725 {
726 if (verbose)
727 semsg(_(e_item_not_exported_in_script_str), name);
728 *ufunc = NULL;
729 return -1;
730 }
731 }
732
733 return idx;
734}
735
736/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200737 * Declare a script-local variable without init: "let var: type".
738 * "const" is an error since the value is missing.
739 * Returns a pointer to after the type.
740 */
741 char_u *
742vim9_declare_scriptvar(exarg_T *eap, char_u *arg)
743{
744 char_u *p;
745 char_u *name;
746 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
747 type_T *type;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200748 typval_T init_tv;
749
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200750 if (eap->cmdidx == CMD_final || eap->cmdidx == CMD_const)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200751 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200752 if (eap->cmdidx == CMD_final)
753 emsg(_(e_final_requires_a_value));
754 else
755 emsg(_(e_const_requires_a_value));
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200756 return arg + STRLEN(arg);
757 }
758
759 // Check for valid starting character.
760 if (!eval_isnamec1(*arg))
761 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000762 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200763 return arg + STRLEN(arg);
764 }
765
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200766 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
Bram Moolenaar3b74b6b2020-06-19 19:01:43 +0200767 if (*p == ':' && (VIM_ISWHITE(p[1]) || p != arg + 1))
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200768 break;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200769
770 if (*p != ':')
771 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +0200772 emsg(_(e_type_or_initialization_required));
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200773 return arg + STRLEN(arg);
774 }
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200775 if (!VIM_ISWHITE(p[1]))
776 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100777 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200778 return arg + STRLEN(arg);
779 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200780 name = vim_strnsave(arg, p - arg);
781
Bram Moolenaard0edaf92021-05-28 21:06:08 +0200782 // parse type, check for reserved name
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200783 p = skipwhite(p + 1);
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100784 type = parse_type(&p, &si->sn_type_list, TRUE);
Bram Moolenaard0edaf92021-05-28 21:06:08 +0200785 if (type == NULL || check_reserved_name(name) == FAIL)
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200786 {
787 vim_free(name);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200788 return p;
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200789 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200790
791 // Create the variable with 0/NULL value.
792 CLEAR_FIELD(init_tv);
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +0200793 if (type->tt_type == VAR_ANY)
794 // A variable of type "any" is not possible, just use zero instead
795 init_tv.v_type = VAR_NUMBER;
796 else
797 init_tv.v_type = type->tt_type;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000798 set_var_const(name, 0, type, &init_tv, FALSE, 0, 0);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200799
800 vim_free(name);
801 return p;
802}
803
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200804/*
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200805 * Vim9 part of adding a script variable: add it to sn_all_vars (lookup by name
806 * with a hashtable) and sn_var_vals (lookup by index).
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100807 * When "create" is TRUE this is a new variable, otherwise find and update an
808 * existing variable.
Bram Moolenaar08251752021-01-11 21:20:18 +0100809 * "flags" can have ASSIGN_FINAL or ASSIGN_CONST.
Bram Moolenaarf2253962021-04-13 20:53:13 +0200810 * When "*type" is NULL use "tv" for the type and update "*type". If
811 * "do_member" is TRUE also use the member type, otherwise use "any".
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200812 */
813 void
Bram Moolenaar08251752021-01-11 21:20:18 +0100814update_vim9_script_var(
815 int create,
816 dictitem_T *di,
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +0000817 char_u *name,
Bram Moolenaar08251752021-01-11 21:20:18 +0100818 int flags,
819 typval_T *tv,
Bram Moolenaarf2253962021-04-13 20:53:13 +0200820 type_T **type,
821 int do_member)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200822{
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100823 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
824 hashitem_T *hi;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200825 svar_T *sv = NULL;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200826
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100827 if (create)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200828 {
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200829 sallvar_T *newsav;
830 sallvar_T *sav = NULL;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200831
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100832 // Store a pointer to the typval_T, so that it can be found by index
833 // instead of using a hastab lookup.
834 if (ga_grow(&si->sn_var_vals, 1) == FAIL)
835 return;
836
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +0000837 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200838 if (!HASHITEM_EMPTY(hi))
839 {
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200840 // Variable with this name exists, either in this block or in
841 // another block.
842 for (sav = HI2SAV(hi); ; sav = sav->sav_next)
843 {
844 if (sav->sav_block_id == si->sn_current_block_id)
845 {
846 // variable defined in a loop, re-use the entry
847 sv = ((svar_T *)si->sn_var_vals.ga_data)
848 + sav->sav_var_vals_idx;
849 // unhide the variable
850 if (sv->sv_tv == &sav->sav_tv)
851 {
852 clear_tv(&sav->sav_tv);
853 sv->sv_tv = &di->di_tv;
854 sav->sav_di = di;
855 }
856 break;
857 }
858 if (sav->sav_next == NULL)
859 break;
860 }
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200861 }
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200862
863 if (sv == NULL)
864 {
865 // Variable not defined or not defined in current block: Add a
866 // svar_T and create a new sallvar_T.
867 sv = ((svar_T *)si->sn_var_vals.ga_data) + si->sn_var_vals.ga_len;
868 newsav = (sallvar_T *)alloc_clear(
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +0000869 sizeof(sallvar_T) + STRLEN(name));
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200870 if (newsav == NULL)
871 return;
872
873 sv->sv_tv = &di->di_tv;
874 sv->sv_const = (flags & ASSIGN_FINAL) ? ASSIGN_FINAL
875 : (flags & ASSIGN_CONST) ? ASSIGN_CONST : 0;
876 sv->sv_export = is_export;
877 newsav->sav_var_vals_idx = si->sn_var_vals.ga_len;
878 ++si->sn_var_vals.ga_len;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +0000879 STRCPY(&newsav->sav_key, name);
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200880 sv->sv_name = newsav->sav_key;
881 newsav->sav_di = di;
882 newsav->sav_block_id = si->sn_current_block_id;
883
884 if (HASHITEM_EMPTY(hi))
885 // new variable name
886 hash_add(&si->sn_all_vars.dv_hashtab, newsav->sav_key);
887 else if (sav != NULL)
888 // existing name in a new block, append to the list
889 sav->sav_next = newsav;
890 }
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200891 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100892 else
893 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000894 sv = find_typval_in_script(&di->di_tv, 0);
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100895 }
896 if (sv != NULL)
897 {
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100898 if (*type == NULL)
Bram Moolenaar114dbda2022-01-03 12:28:03 +0000899 *type = typval2type(tv, get_copyID(), &si->sn_type_list,
900 do_member ? TVTT_DO_MEMBER : 0);
Bram Moolenaardd297bc2021-12-10 10:37:38 +0000901 if (sv->sv_type_allocated)
902 free_type(sv->sv_type);
903 if (*type != NULL && ((*type)->tt_type == VAR_FUNC
904 || (*type)->tt_type == VAR_PARTIAL))
905 {
906 // The type probably uses uf_type_list, which is cleared when the
907 // function is freed, but the script variable may keep the type.
908 // Make a copy to avoid using freed memory.
909 sv->sv_type = alloc_type(*type);
910 sv->sv_type_allocated = TRUE;
911 }
912 else
913 {
914 sv->sv_type = *type;
915 sv->sv_type_allocated = FALSE;
916 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100917 }
918
919 // let ex_export() know the export worked.
920 is_export = FALSE;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200921}
922
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200923/*
924 * Hide a script variable when leaving a block.
925 * "idx" is de index in sn_var_vals.
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200926 * When "func_defined" is non-zero then a function was defined in this block,
927 * the variable may be accessed by it. Otherwise the variable can be cleared.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200928 */
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200929 void
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200930hide_script_var(scriptitem_T *si, int idx, int func_defined)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200931{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200932 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200933 hashtab_T *script_ht = get_script_local_ht();
934 hashtab_T *all_ht = &si->sn_all_vars.dv_hashtab;
935 hashitem_T *script_hi;
936 hashitem_T *all_hi;
937
938 // Remove a variable declared inside the block, if it still exists.
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200939 // If it was added in a nested block it will already have been removed.
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200940 // The typval is moved into the sallvar_T.
941 script_hi = hash_find(script_ht, sv->sv_name);
942 all_hi = hash_find(all_ht, sv->sv_name);
943 if (!HASHITEM_EMPTY(script_hi) && !HASHITEM_EMPTY(all_hi))
944 {
945 dictitem_T *di = HI2DI(script_hi);
946 sallvar_T *sav = HI2SAV(all_hi);
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200947 sallvar_T *sav_prev = NULL;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200948
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200949 // There can be multiple entries with the same name in different
950 // blocks, find the right one.
951 while (sav != NULL && sav->sav_var_vals_idx != idx)
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200952 {
953 sav_prev = sav;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200954 sav = sav->sav_next;
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200955 }
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200956 if (sav != NULL)
957 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200958 if (func_defined)
959 {
960 // move the typval from the dictitem to the sallvar
961 sav->sav_tv = di->di_tv;
962 di->di_tv.v_type = VAR_UNKNOWN;
963 sav->sav_flags = di->di_flags;
964 sav->sav_di = NULL;
965 sv->sv_tv = &sav->sav_tv;
966 }
967 else
968 {
969 if (sav_prev == NULL)
970 hash_remove(all_ht, all_hi);
971 else
972 sav_prev->sav_next = sav->sav_next;
973 sv->sv_name = NULL;
974 vim_free(sav);
975 }
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200976 delete_var(script_ht, script_hi);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200977 }
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200978 }
979}
980
981/*
Bram Moolenaar10c65862020-10-08 21:16:42 +0200982 * Find the script-local variable that links to "dest".
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000983 * If "sid" is zero use the current script.
Bram Moolenaarc967d572021-07-08 21:38:50 +0200984 * Returns NULL if not found and give an internal error.
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200985 */
Bram Moolenaar10c65862020-10-08 21:16:42 +0200986 svar_T *
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000987find_typval_in_script(typval_T *dest, scid_T sid)
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200988{
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000989 scriptitem_T *si = SCRIPT_ITEM(sid == 0 ? current_sctx.sc_sid : sid);
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200990 int idx;
991
Bram Moolenaar81e17fb2020-08-21 21:55:43 +0200992 if (si->sn_version != SCRIPT_VERSION_VIM9)
993 // legacy script doesn't store variable types
Bram Moolenaar10c65862020-10-08 21:16:42 +0200994 return NULL;
Bram Moolenaar81e17fb2020-08-21 21:55:43 +0200995
Bram Moolenaara06758d2021-10-15 00:18:37 +0100996 // Find the svar_T in sn_var_vals. Start at the end, in a for loop the
997 // variable was added at the end.
998 for (idx = si->sn_var_vals.ga_len - 1; idx >= 0; --idx)
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200999 {
1000 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1001
Bram Moolenaarc0913d02020-12-05 14:44:37 +01001002 // If "sv_name" is NULL the variable was hidden when leaving a block,
1003 // don't check "sv_tv" then, it might be used for another variable now.
1004 if (sv->sv_name != NULL && sv->sv_tv == dest)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001005 return sv;
Bram Moolenaar34db91f2020-06-13 19:00:10 +02001006 }
Bram Moolenaarc967d572021-07-08 21:38:50 +02001007 iemsg("find_typval_in_script(): not found");
Bram Moolenaar10c65862020-10-08 21:16:42 +02001008 return NULL;
1009}
1010
1011/*
1012 * Check if the type of script variable "dest" allows assigning "value".
1013 * If needed convert "value" to a bool.
1014 */
1015 int
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001016check_script_var_type(
Bram Moolenaar7824fc82021-11-26 17:36:51 +00001017 svar_T *sv,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001018 typval_T *value,
1019 char_u *name,
1020 where_T where)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001021{
Bram Moolenaar10c65862020-10-08 21:16:42 +02001022 int ret;
1023
Bram Moolenaar7824fc82021-11-26 17:36:51 +00001024 if (sv->sv_const != 0)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001025 {
Bram Moolenaar7824fc82021-11-26 17:36:51 +00001026 semsg(_(e_cannot_change_readonly_variable_str), name);
1027 return FAIL;
Bram Moolenaar10c65862020-10-08 21:16:42 +02001028 }
Bram Moolenaar7824fc82021-11-26 17:36:51 +00001029 ret = check_typval_type(sv->sv_type, value, where);
1030 if (ret == OK && need_convert_to_bool(sv->sv_type, value))
1031 {
1032 int val = tv2bool(value);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001033
Bram Moolenaar7824fc82021-11-26 17:36:51 +00001034 clear_tv(value);
1035 value->v_type = VAR_BOOL;
1036 value->v_lock = 0;
1037 value->vval.v_number = val ? VVAL_TRUE : VVAL_FALSE;
1038 }
1039 return ret;
Bram Moolenaar34db91f2020-06-13 19:00:10 +02001040}
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001041
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001042// words that cannot be used as a variable
1043static char *reserved[] = {
1044 "true",
1045 "false",
1046 "null",
Bram Moolenaar74235772021-06-12 14:53:05 +02001047 "this",
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001048 NULL
1049};
1050
1051 int
1052check_reserved_name(char_u *name)
1053{
1054 int idx;
1055
1056 for (idx = 0; reserved[idx] != NULL; ++idx)
1057 if (STRCMP(reserved[idx], name) == 0)
1058 {
1059 semsg(_(e_cannot_use_reserved_name), name);
1060 return FAIL;
1061 }
1062 return OK;
1063}
1064
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001065#endif // FEAT_EVAL