blob: a63f2f7dd2d6b3a5e30ac805792a10017e46742b [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
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +000062#ifdef FEAT_EVAL
63/*
64 * Clear Vim9 script-local variables and functions.
65 */
66 void
67clear_vim9_scriptlocal_vars(int sid)
68{
69 hashtab_T *ht = &SCRIPT_VARS(sid);
70
71 hashtab_free_contents(ht);
72 hash_init(ht);
73 delete_script_functions(sid);
74
75 // old imports and script variables are no longer valid
76 free_imports_and_script_vars(sid);
77}
78#endif
79
Bram Moolenaare535db82021-03-31 21:07:24 +020080/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010081 * ":vim9script".
82 */
83 void
Bram Moolenaar9b8d6222020-12-28 18:26:00 +010084ex_vim9script(exarg_T *eap UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010085{
Bram Moolenaar9b8d6222020-12-28 18:26:00 +010086#ifdef FEAT_EVAL
Bram Moolenaar2b327002020-12-26 15:39:31 +010087 int sid = current_sctx.sc_sid;
Bram Moolenaar101f4812020-06-16 23:18:51 +020088 scriptitem_T *si;
Bram Moolenaardc4451d2022-01-09 21:36:37 +000089 int found_noclear = FALSE;
Bram Moolenaardc4451d2022-01-09 21:36:37 +000090 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010091
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +000092 if (!sourcing_a_script(eap))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010093 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +020094 emsg(_(e_vim9script_can_only_be_used_in_script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010095 return;
96 }
Bram Moolenaar2b327002020-12-26 15:39:31 +010097
98 si = SCRIPT_ITEM(sid);
99 if (si->sn_state == SN_STATE_HAD_COMMAND)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100100 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200101 emsg(_(e_vim9script_must_be_first_command_in_script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100102 return;
103 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000104
105 for (p = eap->arg; !IS_WHITE_OR_NUL(*p); p = skipwhite(skiptowhite(p)))
Bram Moolenaar2b327002020-12-26 15:39:31 +0100106 {
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000107 if (STRNCMP(p, "noclear", 7) == 0 && IS_WHITE_OR_NUL(p[7]))
108 {
109 if (found_noclear)
110 {
111 semsg(_(e_duplicate_argument_str), p);
112 return;
113 }
114 found_noclear = TRUE;
115 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000116 else
117 {
118 semsg(_(e_invalid_argument_str), eap->arg);
119 return;
120 }
Bram Moolenaar2b327002020-12-26 15:39:31 +0100121 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000122
123 if (si->sn_state == SN_STATE_RELOAD && !found_noclear)
Bram Moolenaar2b327002020-12-26 15:39:31 +0100124 // Reloading a script without the "noclear" argument: clear
125 // script-local variables and functions.
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +0000126 clear_vim9_scriptlocal_vars(sid);
Bram Moolenaar2b327002020-12-26 15:39:31 +0100127 si->sn_state = SN_STATE_HAD_COMMAND;
128
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000129 // Store the prefix with the script, it is used to find exported functions.
Bram Moolenaar71930f12022-01-13 13:47:43 +0000130 if (si->sn_autoload_prefix == NULL)
131 si->sn_autoload_prefix = get_autoload_prefix(si);
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000132
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100133 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
134 si->sn_version = SCRIPT_VERSION_VIM9;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100135
136 if (STRCMP(p_cpo, CPO_VIM) != 0)
137 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200138 si->sn_save_cpo = vim_strsave(p_cpo);
Bram Moolenaar37294bd2021-03-10 13:40:08 +0100139 set_option_value((char_u *)"cpo", 0L, (char_u *)CPO_VIM, OPT_NO_REDRAW);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100140 }
Bram Moolenaar9b8d6222020-12-28 18:26:00 +0100141#else
142 // No check for this being the first command, it doesn't matter.
143 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
144#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100145}
146
Dominique Pelle748b3082022-01-08 12:41:16 +0000147#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100148/*
Bram Moolenaarae616492020-07-28 20:07:27 +0200149 * When in Vim9 script give an error and return FAIL.
150 */
151 int
152not_in_vim9(exarg_T *eap)
153{
Bram Moolenaar68e30442020-07-28 21:15:07 +0200154 if (in_vim9script())
155 switch (eap->cmdidx)
156 {
Bram Moolenaarada1d872021-02-20 08:16:51 +0100157 case CMD_k:
158 if (eap->addr_count > 0)
159 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000160 emsg(_(e_no_range_allowed));
Bram Moolenaarada1d872021-02-20 08:16:51 +0100161 return FAIL;
162 }
163 // FALLTHROUGH
Bram Moolenaar68e30442020-07-28 21:15:07 +0200164 case CMD_append:
165 case CMD_change:
Bram Moolenaarf5a48012020-08-01 17:00:03 +0200166 case CMD_insert:
Bram Moolenaar65088802021-03-13 21:07:21 +0100167 case CMD_open:
Bram Moolenaarf5a48012020-08-01 17:00:03 +0200168 case CMD_t:
Bram Moolenaar68e30442020-07-28 21:15:07 +0200169 case CMD_xit:
Bram Moolenaar9b8d6222020-12-28 18:26:00 +0100170 semsg(_(e_command_not_supported_in_vim9_script_missing_var_str), eap->cmd);
Bram Moolenaar68e30442020-07-28 21:15:07 +0200171 return FAIL;
172 default: break;
173 }
Bram Moolenaarae616492020-07-28 20:07:27 +0200174 return OK;
175}
176
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100177/*
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100178 * Give an error message if "p" points at "#{" and return TRUE.
179 * This avoids that using a legacy style #{} dictionary leads to difficult to
180 * understand errors.
181 */
182 int
183vim9_bad_comment(char_u *p)
184{
Bram Moolenaara0399ef2021-03-20 15:00:01 +0100185 if (p[0] == '#' && p[1] == '{' && p[2] != '{')
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100186 {
187 emsg(_(e_cannot_use_hash_curly_to_start_comment));
188 return TRUE;
189 }
190 return FALSE;
191}
Dominique Pelle748b3082022-01-08 12:41:16 +0000192#endif
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100193
194/*
Bram Moolenaara0399ef2021-03-20 15:00:01 +0100195 * Return TRUE if "p" points at a "#" not followed by one '{'.
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100196 * Does not check for white space.
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100197 */
198 int
199vim9_comment_start(char_u *p)
200{
Bram Moolenaara0399ef2021-03-20 15:00:01 +0100201 return p[0] == '#' && (p[1] != '{' || p[2] == '{');
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100202}
203
Bram Moolenaar9b8d6222020-12-28 18:26:00 +0100204#if defined(FEAT_EVAL) || defined(PROTO)
205
Bram Moolenaarae616492020-07-28 20:07:27 +0200206/*
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200207 * "++nr" and "--nr" commands.
208 */
209 void
210ex_incdec(exarg_T *eap)
211{
212 char_u *cmd = eap->cmd;
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200213 char_u *nextcmd = eap->nextcmd;
214 size_t len = STRLEN(eap->cmd) + 8;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200215
Bram Moolenaar22480d12021-06-25 21:31:09 +0200216 if (VIM_ISWHITE(cmd[2]))
217 {
218 semsg(_(e_no_white_space_allowed_after_str_str),
219 eap->cmdidx == CMD_increment ? "++" : "--", eap->cmd);
220 return;
221 }
222
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200223 // This works like "nr += 1" or "nr -= 1".
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200224 // Add a '|' to avoid looking in the next line.
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200225 eap->cmd = alloc(len);
226 if (eap->cmd == NULL)
227 return;
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200228 vim_snprintf((char *)eap->cmd, len, "%s %c= 1 |", cmd + 2,
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200229 eap->cmdidx == CMD_increment ? '+' : '-');
230 eap->arg = eap->cmd;
231 eap->cmdidx = CMD_var;
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200232 eap->nextcmd = NULL;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200233 ex_let(eap);
234 vim_free(eap->cmd);
235 eap->cmd = cmd;
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200236 eap->nextcmd = nextcmd;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200237}
238
239/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100240 * ":export let Name: type"
241 * ":export const Name: type"
242 * ":export def Name(..."
243 * ":export class Name ..."
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100244 */
245 void
Bram Moolenaar09689a02020-05-09 22:50:08 +0200246ex_export(exarg_T *eap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100247{
Bram Moolenaar17d36cb2022-01-12 11:46:40 +0000248 int prev_did_emsg = did_emsg;
249
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200250 if (!in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100251 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200252 emsg(_(e_export_can_only_be_used_in_vim9script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100253 return;
254 }
255
256 eap->cmd = eap->arg;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +0100257 (void)find_ex_command(eap, NULL, lookup_scriptitem, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100258 switch (eap->cmdidx)
259 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200260 case CMD_var:
261 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100262 case CMD_const:
263 case CMD_def:
Bram Moolenaar160aa862022-01-10 21:29:57 +0000264 case CMD_function:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100265 // case CMD_class:
266 is_export = TRUE;
267 do_cmdline(eap->cmd, eap->getline, eap->cookie,
268 DOCMD_VERBOSE + DOCMD_NOWAIT);
269
270 // The command will reset "is_export" when exporting an item.
271 if (is_export)
272 {
Bram Moolenaar17d36cb2022-01-12 11:46:40 +0000273 if (did_emsg == prev_did_emsg)
274 emsg(_(e_export_with_invalid_argument));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100275 is_export = FALSE;
276 }
277 break;
278 default:
Bram Moolenaar17d36cb2022-01-12 11:46:40 +0000279 if (did_emsg == prev_did_emsg)
280 emsg(_(e_invalid_command_after_export));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100281 break;
282 }
283}
284
285/*
286 * Add a new imported item entry to the current script.
287 */
288 static imported_T *
289new_imported(garray_T *gap)
290{
291 if (ga_grow(gap, 1) == OK)
292 return ((imported_T *)gap->ga_data + gap->ga_len++);
293 return NULL;
294}
295
296/*
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200297 * Free the script variables from "sn_all_vars".
298 */
299 static void
300free_all_script_vars(scriptitem_T *si)
301{
302 int todo;
303 hashtab_T *ht = &si->sn_all_vars.dv_hashtab;
304 hashitem_T *hi;
305 sallvar_T *sav;
306 sallvar_T *sav_next;
Bram Moolenaardd297bc2021-12-10 10:37:38 +0000307 int idx;
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200308
309 hash_lock(ht);
310 todo = (int)ht->ht_used;
311 for (hi = ht->ht_array; todo > 0; ++hi)
312 {
313 if (!HASHITEM_EMPTY(hi))
314 {
315 --todo;
316
317 // Free the variable. Don't remove it from the hashtab, ht_array
318 // might change then. hash_clear() takes care of it later.
319 sav = HI2SAV(hi);
320 while (sav != NULL)
321 {
322 sav_next = sav->sav_next;
323 if (sav->sav_di == NULL)
324 clear_tv(&sav->sav_tv);
325 vim_free(sav);
326 sav = sav_next;
327 }
328 }
329 }
330 hash_clear(ht);
331 hash_init(ht);
332
Bram Moolenaardd297bc2021-12-10 10:37:38 +0000333 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
334 {
335 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
336
337 if (sv->sv_type_allocated)
338 free_type(sv->sv_type);
339 }
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200340 ga_clear(&si->sn_var_vals);
341
342 // existing commands using script variable indexes are no longer valid
343 si->sn_script_seq = current_sctx.sc_seq;
344}
345
346/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100347 * Free all imported items in script "sid".
348 */
349 void
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200350free_imports_and_script_vars(int sid)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100351{
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100352 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100353 int idx;
354
355 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
356 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100357 imported_T *imp = ((imported_T *)si->sn_imports.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100358
359 vim_free(imp->imp_name);
360 }
361 ga_clear(&si->sn_imports);
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200362
363 free_all_script_vars(si);
364
Bram Moolenaar6110e792020-07-08 19:35:21 +0200365 clear_type_list(&si->sn_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100366}
367
368/*
Bram Moolenaara6294952020-12-27 13:39:50 +0100369 * Mark all imports as possible to redefine. Used when a script is loaded
370 * again but not cleared.
371 */
372 void
373mark_imports_for_reload(int sid)
374{
375 scriptitem_T *si = SCRIPT_ITEM(sid);
376 int idx;
377
378 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
379 {
380 imported_T *imp = ((imported_T *)si->sn_imports.ga_data) + idx;
381
382 imp->imp_flags |= IMP_FLAGS_RELOAD;
383 }
384}
385
386/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100387 * Part of "import" that handles a relative or absolute file name/
388 * Returns OK or FAIL.
389 */
390 static int
391handle_import_fname(char_u *fname, int is_autoload, int *sid)
392{
393 if (is_autoload)
394 {
395 scriptitem_T *si;
396
397 *sid = find_script_by_name(fname);
398 if (*sid < 0)
399 {
400 int error = OK;
401
Bram Moolenaar49d008d2022-03-31 11:51:21 +0100402 // Script does not exist yet, check name and create a new
403 // scriptitem.
404 if (!file_is_readable(fname))
405 {
406 semsg(_(mch_isdir(fname) ? e_str_is_directory
407 : e_cannot_read_from_str_2), fname);
408 return FAIL;
409 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100410 *sid = get_new_scriptitem_for_fname(&error, fname);
411 if (error == FAIL)
412 return FAIL;
413 }
414
415 si = SCRIPT_ITEM(*sid);
416 si->sn_import_autoload = TRUE;
417
418 // with testing override: load autoload script right away
419 if (!override_autoload || si->sn_state != SN_STATE_NOT_LOADED)
420 return OK;
421 }
422 return do_source(fname, FALSE, DOSO_NONE, sid);
423}
424
425/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100426 * Handle an ":import" command and add the resulting imported_T to "gap", when
427 * not NULL, or script "import_sid" sn_imports.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200428 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100429 * Returns a pointer to after the command or NULL in case of failure
430 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200431 static char_u *
Bram Moolenaar1c991142020-07-04 13:15:31 +0200432handle_import(
433 char_u *arg_start,
434 garray_T *gap,
435 int import_sid,
436 evalarg_T *evalarg,
437 void *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100438{
439 char_u *arg = arg_start;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000440 char_u *nextarg;
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000441 int is_autoload = FALSE;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000442 int getnext;
443 char_u *expr_end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100444 int ret = FAIL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000445 char_u *as_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100446 typval_T tv;
Bram Moolenaar1836d612022-01-18 13:14:47 +0000447 int sid = -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100448 int res;
Bram Moolenaar921ba522021-07-29 22:25:05 +0200449 long start_lnum = SOURCING_LNUM;
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000450 garray_T *import_gap;
451 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100452
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000453 if (STRNCMP(arg, "autoload", 8) == 0 && VIM_ISWHITE(arg[8]))
454 {
455 is_autoload = TRUE;
456 arg = skipwhite(arg + 8);
457 }
458
Bram Moolenaar4db572e2021-07-18 18:21:38 +0200459 // The name of the file can be an expression, which must evaluate to a
460 // string.
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000461 ret = eval0_retarg(arg, &tv, NULL, evalarg, &expr_end);
Bram Moolenaar4db572e2021-07-18 18:21:38 +0200462 if (ret == FAIL)
463 goto erret;
464 if (tv.v_type != VAR_STRING
465 || tv.vval.v_string == NULL || *tv.vval.v_string == NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100466 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000467 semsg(_(e_invalid_string_for_import_str), arg);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200468 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100469 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100470
Bram Moolenaar921ba522021-07-29 22:25:05 +0200471 // Give error messages for the start of the line.
472 SOURCING_LNUM = start_lnum;
473
Bram Moolenaar1c991142020-07-04 13:15:31 +0200474 /*
475 * find script file
476 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100477 if (*tv.vval.v_string == '.')
478 {
479 size_t len;
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100480 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100481 char_u *tail = gettail(si->sn_name);
482 char_u *from_name;
483
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100484 // Relative to current script: "./name.vim", "../../name.vim".
485 len = STRLEN(si->sn_name) - STRLEN(tail) + STRLEN(tv.vval.v_string) + 2;
486 from_name = alloc((int)len);
487 if (from_name == NULL)
488 goto erret;
489 vim_strncpy(from_name, si->sn_name, tail - si->sn_name);
490 add_pathsep(from_name);
491 STRCAT(from_name, tv.vval.v_string);
492 simplify_filename(from_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100493
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100494 res = handle_import_fname(from_name, is_autoload, &sid);
495 vim_free(from_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100496 }
Bram Moolenaar113b8dc2022-01-18 13:43:58 +0000497 else if (mch_isFullName(tv.vval.v_string)
498#ifdef BACKSLASH_IN_FILENAME
499 // On MS-Windows omitting the drive is still handled like an
500 // absolute path, not using 'runtimepath'.
501 || *tv.vval.v_string == '/' || *tv.vval.v_string == '\\'
502#endif
503 )
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100504 {
505 // Absolute path: "/tmp/name.vim"
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100506 res = handle_import_fname(tv.vval.v_string, is_autoload, &sid);
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000507 }
508 else if (is_autoload)
509 {
510 size_t len = 9 + STRLEN(tv.vval.v_string) + 1;
511 char_u *from_name;
512
513 // Find file in "autoload" subdirs in 'runtimepath'.
514 from_name = alloc((int)len);
515 if (from_name == NULL)
516 goto erret;
517 vim_snprintf((char *)from_name, len, "autoload/%s", tv.vval.v_string);
518 // we need a scriptitem without loading the script
519 sid = find_script_in_rtp(from_name);
520 vim_free(from_name);
Bram Moolenaard041f422022-01-12 19:54:00 +0000521 if (SCRIPT_ID_VALID(sid))
522 {
523 scriptitem_T *si = SCRIPT_ITEM(sid);
524
525 if (si->sn_autoload_prefix == NULL)
526 si->sn_autoload_prefix = get_autoload_prefix(si);
527 res = OK;
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +0000528 if (override_autoload && si->sn_state == SN_STATE_NOT_LOADED)
529 // testing override: load autoload script right away
530 (void)do_source(si->sn_name, FALSE, DOSO_NONE, NULL);
Bram Moolenaard041f422022-01-12 19:54:00 +0000531 }
532 else
533 res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100534 }
535 else
536 {
537 size_t len = 7 + STRLEN(tv.vval.v_string) + 1;
538 char_u *from_name;
539
540 // Find file in "import" subdirs in 'runtimepath'.
541 from_name = alloc((int)len);
542 if (from_name == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200543 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100544 vim_snprintf((char *)from_name, len, "import/%s", tv.vval.v_string);
545 res = source_in_path(p_rtp, from_name, DIP_NOAFTER, &sid);
546 vim_free(from_name);
547 }
548
549 if (res == FAIL || sid <= 0)
550 {
Bram Moolenaar1836d612022-01-18 13:14:47 +0000551 semsg(_(is_autoload && sid == -2
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000552 ? e_autoload_import_cannot_use_absolute_or_relative_path
553 : e_could_not_import_str), tv.vval.v_string);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200554 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100555 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100556
Bram Moolenaar779aeff2022-02-08 19:12:19 +0000557 if (sid == current_sctx.sc_sid)
558 {
559 emsg(_(e_script_cannot_import_itself));
560 goto erret;
561 }
562
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000563 import_gap = gap != NULL ? gap : &SCRIPT_ITEM(import_sid)->sn_imports;
564 for (i = 0; i < import_gap->ga_len; ++i)
565 {
566 imported_T *import = (imported_T *)import_gap->ga_data + i;
567
568 if (import->imp_sid == sid)
569 {
570 if (import->imp_flags & IMP_FLAGS_RELOAD)
571 {
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000572 // encountering same script first time on a reload is OK
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000573 import->imp_flags &= ~IMP_FLAGS_RELOAD;
574 break;
575 }
576 semsg(_(e_cannot_import_same_script_twice_str), tv.vval.v_string);
577 goto erret;
578 }
579 }
580
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000581 // Allow for the "as Name" to be in the next line.
582 nextarg = eval_next_non_blank(expr_end, evalarg, &getnext);
583 if (STRNCMP("as", nextarg, 2) == 0 && IS_WHITE_OR_NUL(nextarg[2]))
584 {
585 char_u *p;
586
587 if (getnext)
588 arg = eval_next_line(evalarg);
589 else
590 arg = nextarg;
591
592 // Skip over "as Name "; no line break allowed after "as".
593 // Do not allow for ':' and '#'.
594 arg = skipwhite(arg + 2);
595 p = arg;
596 if (eval_isnamec1(*arg))
597 while (ASCII_ISALNUM(*arg) || *arg == '_')
598 ++arg;
599 if (p == arg || !IS_WHITE_OR_NUL(*arg))
600 {
601 semsg(_(e_syntax_error_in_import_str), p);
602 goto erret;
603 }
604 as_name = vim_strnsave(p, arg - p);
605 arg = skipwhite(arg);
606 }
607 else
608 {
609 char_u *p = gettail(tv.vval.v_string);
610 char_u *end = (char_u *)strstr((char *)p, ".vim");
611
612 if (!ends_excmd2(arg_start, expr_end))
613 {
614 semsg(_(e_trailing_characters_str), expr_end);
615 goto erret;
616 }
Bram Moolenaar834d4182022-01-07 13:38:24 +0000617 if (end == NULL || end[4] != NUL)
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000618 {
Bram Moolenaar834d4182022-01-07 13:38:24 +0000619 semsg(_(e_imported_script_must_use_as_or_end_in_dot_vim_str), p);
620 goto erret;
621 }
622 if (end == p)
623 {
624 semsg(_(e_cannot_import_dot_vim_without_using_as), p);
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000625 goto erret;
626 }
627 as_name = vim_strnsave(p, end - p);
628 }
629
630 if (as_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100631 {
Bram Moolenaar0a842842021-02-27 22:41:19 +0100632 imported_T *imported;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100633
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000634 imported = find_imported(as_name, STRLEN(as_name), FALSE);
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000635 if (imported != NULL && imported->imp_sid != sid)
Bram Moolenaara6294952020-12-27 13:39:50 +0100636 {
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000637 semsg(_(e_name_already_defined_str), as_name);
638 goto erret;
Bram Moolenaara6294952020-12-27 13:39:50 +0100639 }
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000640 else if (imported == NULL
Bram Moolenaardce24412022-02-08 20:35:30 +0000641 && check_defined(as_name, STRLEN(as_name), cctx, NULL,
642 FALSE) == FAIL)
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000643 goto erret;
Bram Moolenaara6294952020-12-27 13:39:50 +0100644
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100645 if (imported == NULL)
Bram Moolenaar17d36cb2022-01-12 11:46:40 +0000646 {
647 imported = new_imported(import_gap);
648 if (imported == NULL)
649 goto erret;
650 imported->imp_name = as_name;
651 as_name = NULL;
652 imported->imp_sid = sid;
653 if (is_autoload)
654 imported->imp_flags = IMP_FLAGS_AUTOLOAD;
655 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100656 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200657
Bram Moolenaar1c991142020-07-04 13:15:31 +0200658erret:
Bram Moolenaar4db572e2021-07-18 18:21:38 +0200659 clear_tv(&tv);
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000660 vim_free(as_name);
661 return arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100662}
663
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200664/*
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000665 * ":import 'filename'"
666 * ":import 'filename' as Name"
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200667 */
668 void
669ex_import(exarg_T *eap)
670{
671 char_u *cmd_end;
672 evalarg_T evalarg;
673
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +0000674 if (!sourcing_a_script(eap))
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200675 {
676 emsg(_(e_import_can_only_be_used_in_script));
677 return;
678 }
679 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
680
681 cmd_end = handle_import(eap->arg, NULL, current_sctx.sc_sid,
682 &evalarg, NULL);
683 if (cmd_end != NULL)
684 set_nextcmd(eap, cmd_end);
685 clear_evalarg(&evalarg, eap);
686}
687
688/*
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000689 * Find an exported item in "sid" matching "name".
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000690 * Either "cctx" or "cstack" is NULL.
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200691 * When it is a variable return the index.
692 * When it is a user function return "*ufunc".
693 * When not found returns -1 and "*ufunc" is NULL.
694 */
695 int
696find_exported(
697 int sid,
698 char_u *name,
699 ufunc_T **ufunc,
700 type_T **type,
701 cctx_T *cctx,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000702 cstack_T *cstack,
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200703 int verbose)
704{
705 int idx = -1;
706 svar_T *sv;
707 scriptitem_T *script = SCRIPT_ITEM(sid);
708
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100709 if (script->sn_import_autoload && script->sn_state == SN_STATE_NOT_LOADED)
710 {
711 if (do_source(script->sn_name, FALSE, DOSO_NONE, NULL) == FAIL)
712 return -1;
713 }
714
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200715 // Find name in "script".
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000716 idx = get_script_item_idx(sid, name, 0, cctx, cstack);
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200717 if (idx >= 0)
718 {
719 sv = ((svar_T *)script->sn_var_vals.ga_data) + idx;
Bram Moolenaara909c482022-01-06 22:07:57 +0000720 *ufunc = NULL;
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200721 if (!sv->sv_export)
722 {
723 if (verbose)
724 semsg(_(e_item_not_exported_in_script_str), name);
725 return -1;
726 }
727 *type = sv->sv_type;
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200728 }
729 else
730 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +0000731 size_t len = STRLEN(name);
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200732 char_u buffer[200];
733 char_u *funcname;
734
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +0000735 // It could be a user function. Normally this is stored as
736 // "<SNR>99_name". For an autoload script a function is stored with
737 // the autoload prefix: "dir#script#name".
738 if (script->sn_autoload_prefix != NULL)
739 len += STRLEN(script->sn_autoload_prefix) + 2;
740 else
741 len += 15;
742
743 if (len < sizeof(buffer))
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200744 funcname = buffer;
745 else
746 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +0000747 funcname = alloc(len);
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200748 if (funcname == NULL)
749 return -1;
750 }
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +0000751 if (script->sn_autoload_prefix != NULL)
752 {
753 sprintf((char *)funcname, "%s%s", script->sn_autoload_prefix, name);
754 }
755 else
756 {
757 funcname[0] = K_SPECIAL;
758 funcname[1] = KS_EXTRA;
759 funcname[2] = (int)KE_SNR;
760 sprintf((char *)funcname + 3, "%ld_%s", (long)sid, name);
761 }
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000762 *ufunc = find_func(funcname, FALSE);
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200763
764 if (*ufunc == NULL)
765 {
766 if (verbose)
Bram Moolenaard02dce22022-01-18 17:43:04 +0000767 {
768 ufunc_T *alt_ufunc = NULL;
769
770 if (script->sn_autoload_prefix != NULL)
771 {
772 // try find the function by the script-local name
773 funcname[0] = K_SPECIAL;
774 funcname[1] = KS_EXTRA;
775 funcname[2] = (int)KE_SNR;
776 sprintf((char *)funcname + 3, "%ld_%s", (long)sid, name);
777 alt_ufunc = find_func(funcname, FALSE);
778 }
779 if (alt_ufunc != NULL)
780 semsg(_(e_item_not_exported_in_script_str), name);
781 else
782 semsg(_(e_item_not_found_in_script_str), name);
783 }
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200784 }
785 else if (((*ufunc)->uf_flags & FC_EXPORT) == 0)
786 {
787 if (verbose)
788 semsg(_(e_item_not_exported_in_script_str), name);
789 *ufunc = NULL;
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200790 }
Bram Moolenaard02dce22022-01-18 17:43:04 +0000791 if (funcname != buffer)
792 vim_free(funcname);
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200793 }
794
795 return idx;
796}
797
798/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200799 * Declare a script-local variable without init: "let var: type".
800 * "const" is an error since the value is missing.
801 * Returns a pointer to after the type.
802 */
803 char_u *
804vim9_declare_scriptvar(exarg_T *eap, char_u *arg)
805{
806 char_u *p;
807 char_u *name;
808 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
809 type_T *type;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200810 typval_T init_tv;
811
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200812 if (eap->cmdidx == CMD_final || eap->cmdidx == CMD_const)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200813 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200814 if (eap->cmdidx == CMD_final)
815 emsg(_(e_final_requires_a_value));
816 else
817 emsg(_(e_const_requires_a_value));
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200818 return arg + STRLEN(arg);
819 }
820
821 // Check for valid starting character.
822 if (!eval_isnamec1(*arg))
823 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000824 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200825 return arg + STRLEN(arg);
826 }
827
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200828 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
Bram Moolenaar3b74b6b2020-06-19 19:01:43 +0200829 if (*p == ':' && (VIM_ISWHITE(p[1]) || p != arg + 1))
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200830 break;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200831
832 if (*p != ':')
833 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +0200834 emsg(_(e_type_or_initialization_required));
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200835 return arg + STRLEN(arg);
836 }
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200837 if (!VIM_ISWHITE(p[1]))
838 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100839 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200840 return arg + STRLEN(arg);
841 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200842 name = vim_strnsave(arg, p - arg);
843
Bram Moolenaard0edaf92021-05-28 21:06:08 +0200844 // parse type, check for reserved name
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200845 p = skipwhite(p + 1);
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100846 type = parse_type(&p, &si->sn_type_list, TRUE);
Bram Moolenaard0edaf92021-05-28 21:06:08 +0200847 if (type == NULL || check_reserved_name(name) == FAIL)
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200848 {
849 vim_free(name);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200850 return p;
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200851 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200852
853 // Create the variable with 0/NULL value.
854 CLEAR_FIELD(init_tv);
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +0200855 if (type->tt_type == VAR_ANY)
856 // A variable of type "any" is not possible, just use zero instead
857 init_tv.v_type = VAR_NUMBER;
858 else
859 init_tv.v_type = type->tt_type;
Bram Moolenaar859cc212022-03-28 15:22:35 +0100860 set_var_const(name, 0, type, &init_tv, FALSE, ASSIGN_INIT, 0);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200861
862 vim_free(name);
863 return p;
864}
865
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200866/*
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200867 * Vim9 part of adding a script variable: add it to sn_all_vars (lookup by name
868 * with a hashtable) and sn_var_vals (lookup by index).
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100869 * When "create" is TRUE this is a new variable, otherwise find and update an
870 * existing variable.
Bram Moolenaar08251752021-01-11 21:20:18 +0100871 * "flags" can have ASSIGN_FINAL or ASSIGN_CONST.
Bram Moolenaarf2253962021-04-13 20:53:13 +0200872 * When "*type" is NULL use "tv" for the type and update "*type". If
873 * "do_member" is TRUE also use the member type, otherwise use "any".
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200874 */
875 void
Bram Moolenaar08251752021-01-11 21:20:18 +0100876update_vim9_script_var(
877 int create,
878 dictitem_T *di,
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +0000879 char_u *name,
Bram Moolenaar08251752021-01-11 21:20:18 +0100880 int flags,
881 typval_T *tv,
Bram Moolenaarf2253962021-04-13 20:53:13 +0200882 type_T **type,
883 int do_member)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200884{
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100885 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
886 hashitem_T *hi;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200887 svar_T *sv = NULL;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200888
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100889 if (create)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200890 {
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200891 sallvar_T *newsav;
892 sallvar_T *sav = NULL;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200893
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100894 // Store a pointer to the typval_T, so that it can be found by index
895 // instead of using a hastab lookup.
896 if (ga_grow(&si->sn_var_vals, 1) == FAIL)
897 return;
898
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +0000899 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200900 if (!HASHITEM_EMPTY(hi))
901 {
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200902 // Variable with this name exists, either in this block or in
903 // another block.
904 for (sav = HI2SAV(hi); ; sav = sav->sav_next)
905 {
906 if (sav->sav_block_id == si->sn_current_block_id)
907 {
908 // variable defined in a loop, re-use the entry
909 sv = ((svar_T *)si->sn_var_vals.ga_data)
910 + sav->sav_var_vals_idx;
911 // unhide the variable
912 if (sv->sv_tv == &sav->sav_tv)
913 {
914 clear_tv(&sav->sav_tv);
915 sv->sv_tv = &di->di_tv;
916 sav->sav_di = di;
917 }
918 break;
919 }
920 if (sav->sav_next == NULL)
921 break;
922 }
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200923 }
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200924
925 if (sv == NULL)
926 {
927 // Variable not defined or not defined in current block: Add a
928 // svar_T and create a new sallvar_T.
929 sv = ((svar_T *)si->sn_var_vals.ga_data) + si->sn_var_vals.ga_len;
930 newsav = (sallvar_T *)alloc_clear(
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +0000931 sizeof(sallvar_T) + STRLEN(name));
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200932 if (newsav == NULL)
933 return;
934
935 sv->sv_tv = &di->di_tv;
936 sv->sv_const = (flags & ASSIGN_FINAL) ? ASSIGN_FINAL
937 : (flags & ASSIGN_CONST) ? ASSIGN_CONST : 0;
938 sv->sv_export = is_export;
939 newsav->sav_var_vals_idx = si->sn_var_vals.ga_len;
940 ++si->sn_var_vals.ga_len;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +0000941 STRCPY(&newsav->sav_key, name);
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200942 sv->sv_name = newsav->sav_key;
943 newsav->sav_di = di;
944 newsav->sav_block_id = si->sn_current_block_id;
945
946 if (HASHITEM_EMPTY(hi))
947 // new variable name
948 hash_add(&si->sn_all_vars.dv_hashtab, newsav->sav_key);
949 else if (sav != NULL)
950 // existing name in a new block, append to the list
951 sav->sav_next = newsav;
952 }
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200953 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100954 else
955 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000956 sv = find_typval_in_script(&di->di_tv, 0);
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100957 }
958 if (sv != NULL)
959 {
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100960 if (*type == NULL)
Bram Moolenaar114dbda2022-01-03 12:28:03 +0000961 *type = typval2type(tv, get_copyID(), &si->sn_type_list,
962 do_member ? TVTT_DO_MEMBER : 0);
Bram Moolenaar859cc212022-03-28 15:22:35 +0100963 else if ((flags & ASSIGN_INIT) == 0
964 && (*type)->tt_type == VAR_BLOB && tv->v_type == VAR_BLOB
965 && tv->vval.v_blob == NULL)
966 {
967 // "var b: blob = null_blob" has a different type.
968 *type = &t_blob_null;
969 }
Bram Moolenaardd297bc2021-12-10 10:37:38 +0000970 if (sv->sv_type_allocated)
971 free_type(sv->sv_type);
972 if (*type != NULL && ((*type)->tt_type == VAR_FUNC
973 || (*type)->tt_type == VAR_PARTIAL))
974 {
975 // The type probably uses uf_type_list, which is cleared when the
976 // function is freed, but the script variable may keep the type.
977 // Make a copy to avoid using freed memory.
978 sv->sv_type = alloc_type(*type);
979 sv->sv_type_allocated = TRUE;
980 }
981 else
982 {
983 sv->sv_type = *type;
984 sv->sv_type_allocated = FALSE;
985 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100986 }
987
988 // let ex_export() know the export worked.
989 is_export = FALSE;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200990}
991
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200992/*
993 * Hide a script variable when leaving a block.
994 * "idx" is de index in sn_var_vals.
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200995 * When "func_defined" is non-zero then a function was defined in this block,
996 * the variable may be accessed by it. Otherwise the variable can be cleared.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200997 */
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200998 void
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200999hide_script_var(scriptitem_T *si, int idx, int func_defined)
Bram Moolenaar8d739de2020-10-14 19:39:19 +02001000{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001001 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8d739de2020-10-14 19:39:19 +02001002 hashtab_T *script_ht = get_script_local_ht();
1003 hashtab_T *all_ht = &si->sn_all_vars.dv_hashtab;
1004 hashitem_T *script_hi;
1005 hashitem_T *all_hi;
1006
1007 // Remove a variable declared inside the block, if it still exists.
Bram Moolenaar39ca4122020-10-20 14:25:07 +02001008 // If it was added in a nested block it will already have been removed.
Bram Moolenaar8d739de2020-10-14 19:39:19 +02001009 // The typval is moved into the sallvar_T.
1010 script_hi = hash_find(script_ht, sv->sv_name);
1011 all_hi = hash_find(all_ht, sv->sv_name);
1012 if (!HASHITEM_EMPTY(script_hi) && !HASHITEM_EMPTY(all_hi))
1013 {
1014 dictitem_T *di = HI2DI(script_hi);
1015 sallvar_T *sav = HI2SAV(all_hi);
Bram Moolenaar39ca4122020-10-20 14:25:07 +02001016 sallvar_T *sav_prev = NULL;
Bram Moolenaar8d739de2020-10-14 19:39:19 +02001017
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001018 // There can be multiple entries with the same name in different
1019 // blocks, find the right one.
1020 while (sav != NULL && sav->sav_var_vals_idx != idx)
Bram Moolenaar39ca4122020-10-20 14:25:07 +02001021 {
1022 sav_prev = sav;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001023 sav = sav->sav_next;
Bram Moolenaar39ca4122020-10-20 14:25:07 +02001024 }
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001025 if (sav != NULL)
1026 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02001027 if (func_defined)
1028 {
1029 // move the typval from the dictitem to the sallvar
1030 sav->sav_tv = di->di_tv;
1031 di->di_tv.v_type = VAR_UNKNOWN;
1032 sav->sav_flags = di->di_flags;
1033 sav->sav_di = NULL;
1034 sv->sv_tv = &sav->sav_tv;
1035 }
1036 else
1037 {
1038 if (sav_prev == NULL)
1039 hash_remove(all_ht, all_hi);
1040 else
1041 sav_prev->sav_next = sav->sav_next;
1042 sv->sv_name = NULL;
1043 vim_free(sav);
1044 }
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001045 delete_var(script_ht, script_hi);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001046 }
Bram Moolenaar8d739de2020-10-14 19:39:19 +02001047 }
1048}
1049
1050/*
Bram Moolenaar10c65862020-10-08 21:16:42 +02001051 * Find the script-local variable that links to "dest".
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001052 * If "sid" is zero use the current script.
Bram Moolenaarc967d572021-07-08 21:38:50 +02001053 * Returns NULL if not found and give an internal error.
Bram Moolenaar34db91f2020-06-13 19:00:10 +02001054 */
Bram Moolenaar10c65862020-10-08 21:16:42 +02001055 svar_T *
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001056find_typval_in_script(typval_T *dest, scid_T sid)
Bram Moolenaar34db91f2020-06-13 19:00:10 +02001057{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001058 scriptitem_T *si = SCRIPT_ITEM(sid == 0 ? current_sctx.sc_sid : sid);
Bram Moolenaar34db91f2020-06-13 19:00:10 +02001059 int idx;
1060
Bram Moolenaar81e17fb2020-08-21 21:55:43 +02001061 if (si->sn_version != SCRIPT_VERSION_VIM9)
1062 // legacy script doesn't store variable types
Bram Moolenaar10c65862020-10-08 21:16:42 +02001063 return NULL;
Bram Moolenaar81e17fb2020-08-21 21:55:43 +02001064
Bram Moolenaara06758d2021-10-15 00:18:37 +01001065 // Find the svar_T in sn_var_vals. Start at the end, in a for loop the
1066 // variable was added at the end.
1067 for (idx = si->sn_var_vals.ga_len - 1; idx >= 0; --idx)
Bram Moolenaar34db91f2020-06-13 19:00:10 +02001068 {
1069 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1070
Bram Moolenaarc0913d02020-12-05 14:44:37 +01001071 // If "sv_name" is NULL the variable was hidden when leaving a block,
1072 // don't check "sv_tv" then, it might be used for another variable now.
1073 if (sv->sv_name != NULL && sv->sv_tv == dest)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001074 return sv;
Bram Moolenaar34db91f2020-06-13 19:00:10 +02001075 }
Bram Moolenaarc967d572021-07-08 21:38:50 +02001076 iemsg("find_typval_in_script(): not found");
Bram Moolenaar10c65862020-10-08 21:16:42 +02001077 return NULL;
1078}
1079
1080/*
1081 * Check if the type of script variable "dest" allows assigning "value".
1082 * If needed convert "value" to a bool.
1083 */
1084 int
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001085check_script_var_type(
Bram Moolenaar7824fc82021-11-26 17:36:51 +00001086 svar_T *sv,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001087 typval_T *value,
1088 char_u *name,
1089 where_T where)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001090{
Bram Moolenaar10c65862020-10-08 21:16:42 +02001091 int ret;
1092
Bram Moolenaar7824fc82021-11-26 17:36:51 +00001093 if (sv->sv_const != 0)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001094 {
Bram Moolenaar7824fc82021-11-26 17:36:51 +00001095 semsg(_(e_cannot_change_readonly_variable_str), name);
1096 return FAIL;
Bram Moolenaar10c65862020-10-08 21:16:42 +02001097 }
Bram Moolenaar7824fc82021-11-26 17:36:51 +00001098 ret = check_typval_type(sv->sv_type, value, where);
1099 if (ret == OK && need_convert_to_bool(sv->sv_type, value))
1100 {
1101 int val = tv2bool(value);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001102
Bram Moolenaar7824fc82021-11-26 17:36:51 +00001103 clear_tv(value);
1104 value->v_type = VAR_BOOL;
1105 value->v_lock = 0;
1106 value->vval.v_number = val ? VVAL_TRUE : VVAL_FALSE;
1107 }
1108 return ret;
Bram Moolenaar34db91f2020-06-13 19:00:10 +02001109}
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001110
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001111// words that cannot be used as a variable
1112static char *reserved[] = {
1113 "true",
1114 "false",
1115 "null",
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00001116 "null_blob",
1117 "null_dict",
1118 "null_function",
1119 "null_list",
1120 "null_partial",
1121 "null_string",
1122 "null_channel",
1123 "null_job",
Bram Moolenaar74235772021-06-12 14:53:05 +02001124 "this",
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001125 NULL
1126};
1127
1128 int
1129check_reserved_name(char_u *name)
1130{
1131 int idx;
1132
1133 for (idx = 0; reserved[idx] != NULL; ++idx)
1134 if (STRCMP(reserved[idx], name) == 0)
1135 {
1136 semsg(_(e_cannot_use_reserved_name), name);
1137 return FAIL;
1138 }
1139 return OK;
1140}
1141
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001142#endif // FEAT_EVAL