blob: bb5f0299dc0ccc61e97a2102b6aed80ab241f39f [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 Moolenaar31e5c602022-04-15 13:53:33 +0100139 set_option_value_give_err((char_u *)"cpo",
140 0L, (char_u *)CPO_VIM, OPT_NO_REDRAW);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100141 }
Bram Moolenaar9b8d6222020-12-28 18:26:00 +0100142#else
143 // No check for this being the first command, it doesn't matter.
144 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
145#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100146}
147
Dominique Pelle748b3082022-01-08 12:41:16 +0000148#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100149/*
Bram Moolenaarae616492020-07-28 20:07:27 +0200150 * When in Vim9 script give an error and return FAIL.
151 */
152 int
153not_in_vim9(exarg_T *eap)
154{
Bram Moolenaar68e30442020-07-28 21:15:07 +0200155 if (in_vim9script())
156 switch (eap->cmdidx)
157 {
Bram Moolenaarada1d872021-02-20 08:16:51 +0100158 case CMD_k:
159 if (eap->addr_count > 0)
160 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000161 emsg(_(e_no_range_allowed));
Bram Moolenaarada1d872021-02-20 08:16:51 +0100162 return FAIL;
163 }
164 // FALLTHROUGH
Bram Moolenaar68e30442020-07-28 21:15:07 +0200165 case CMD_append:
166 case CMD_change:
Bram Moolenaarf5a48012020-08-01 17:00:03 +0200167 case CMD_insert:
Bram Moolenaar65088802021-03-13 21:07:21 +0100168 case CMD_open:
Bram Moolenaarf5a48012020-08-01 17:00:03 +0200169 case CMD_t:
Bram Moolenaar68e30442020-07-28 21:15:07 +0200170 case CMD_xit:
Bram Moolenaar9b8d6222020-12-28 18:26:00 +0100171 semsg(_(e_command_not_supported_in_vim9_script_missing_var_str), eap->cmd);
Bram Moolenaar68e30442020-07-28 21:15:07 +0200172 return FAIL;
173 default: break;
174 }
Bram Moolenaarae616492020-07-28 20:07:27 +0200175 return OK;
176}
177
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100178/*
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100179 * Give an error message if "p" points at "#{" and return TRUE.
180 * This avoids that using a legacy style #{} dictionary leads to difficult to
181 * understand errors.
182 */
183 int
184vim9_bad_comment(char_u *p)
185{
Bram Moolenaara0399ef2021-03-20 15:00:01 +0100186 if (p[0] == '#' && p[1] == '{' && p[2] != '{')
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100187 {
188 emsg(_(e_cannot_use_hash_curly_to_start_comment));
189 return TRUE;
190 }
191 return FALSE;
192}
Dominique Pelle748b3082022-01-08 12:41:16 +0000193#endif
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100194
195/*
Bram Moolenaara0399ef2021-03-20 15:00:01 +0100196 * Return TRUE if "p" points at a "#" not followed by one '{'.
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100197 * Does not check for white space.
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100198 */
199 int
200vim9_comment_start(char_u *p)
201{
Bram Moolenaara0399ef2021-03-20 15:00:01 +0100202 return p[0] == '#' && (p[1] != '{' || p[2] == '{');
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100203}
204
Bram Moolenaar9b8d6222020-12-28 18:26:00 +0100205#if defined(FEAT_EVAL) || defined(PROTO)
206
Bram Moolenaarae616492020-07-28 20:07:27 +0200207/*
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200208 * "++nr" and "--nr" commands.
209 */
210 void
211ex_incdec(exarg_T *eap)
212{
213 char_u *cmd = eap->cmd;
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200214 char_u *nextcmd = eap->nextcmd;
215 size_t len = STRLEN(eap->cmd) + 8;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200216
Bram Moolenaar22480d12021-06-25 21:31:09 +0200217 if (VIM_ISWHITE(cmd[2]))
218 {
219 semsg(_(e_no_white_space_allowed_after_str_str),
220 eap->cmdidx == CMD_increment ? "++" : "--", eap->cmd);
221 return;
222 }
223
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200224 // This works like "nr += 1" or "nr -= 1".
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200225 // Add a '|' to avoid looking in the next line.
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200226 eap->cmd = alloc(len);
227 if (eap->cmd == NULL)
228 return;
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200229 vim_snprintf((char *)eap->cmd, len, "%s %c= 1 |", cmd + 2,
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200230 eap->cmdidx == CMD_increment ? '+' : '-');
231 eap->arg = eap->cmd;
232 eap->cmdidx = CMD_var;
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200233 eap->nextcmd = NULL;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200234 ex_let(eap);
235 vim_free(eap->cmd);
236 eap->cmd = cmd;
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200237 eap->nextcmd = nextcmd;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200238}
239
240/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100241 * ":export let Name: type"
242 * ":export const Name: type"
243 * ":export def Name(..."
244 * ":export class Name ..."
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100245 */
246 void
Bram Moolenaar09689a02020-05-09 22:50:08 +0200247ex_export(exarg_T *eap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100248{
Bram Moolenaar17d36cb2022-01-12 11:46:40 +0000249 int prev_did_emsg = did_emsg;
250
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200251 if (!in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100252 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200253 emsg(_(e_export_can_only_be_used_in_vim9script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100254 return;
255 }
256
257 eap->cmd = eap->arg;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +0100258 (void)find_ex_command(eap, NULL, lookup_scriptitem, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100259 switch (eap->cmdidx)
260 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200261 case CMD_var:
262 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100263 case CMD_const:
264 case CMD_def:
Bram Moolenaar160aa862022-01-10 21:29:57 +0000265 case CMD_function:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100266 // case CMD_class:
267 is_export = TRUE;
268 do_cmdline(eap->cmd, eap->getline, eap->cookie,
269 DOCMD_VERBOSE + DOCMD_NOWAIT);
270
271 // The command will reset "is_export" when exporting an item.
272 if (is_export)
273 {
Bram Moolenaar17d36cb2022-01-12 11:46:40 +0000274 if (did_emsg == prev_did_emsg)
275 emsg(_(e_export_with_invalid_argument));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100276 is_export = FALSE;
277 }
278 break;
279 default:
Bram Moolenaar17d36cb2022-01-12 11:46:40 +0000280 if (did_emsg == prev_did_emsg)
281 emsg(_(e_invalid_command_after_export));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100282 break;
283 }
284}
285
286/*
287 * Add a new imported item entry to the current script.
288 */
289 static imported_T *
290new_imported(garray_T *gap)
291{
292 if (ga_grow(gap, 1) == OK)
293 return ((imported_T *)gap->ga_data + gap->ga_len++);
294 return NULL;
295}
296
297/*
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200298 * Free the script variables from "sn_all_vars".
299 */
300 static void
301free_all_script_vars(scriptitem_T *si)
302{
303 int todo;
304 hashtab_T *ht = &si->sn_all_vars.dv_hashtab;
305 hashitem_T *hi;
306 sallvar_T *sav;
307 sallvar_T *sav_next;
Bram Moolenaardd297bc2021-12-10 10:37:38 +0000308 int idx;
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200309
310 hash_lock(ht);
311 todo = (int)ht->ht_used;
312 for (hi = ht->ht_array; todo > 0; ++hi)
313 {
314 if (!HASHITEM_EMPTY(hi))
315 {
316 --todo;
317
318 // Free the variable. Don't remove it from the hashtab, ht_array
319 // might change then. hash_clear() takes care of it later.
320 sav = HI2SAV(hi);
321 while (sav != NULL)
322 {
323 sav_next = sav->sav_next;
324 if (sav->sav_di == NULL)
325 clear_tv(&sav->sav_tv);
326 vim_free(sav);
327 sav = sav_next;
328 }
329 }
330 }
331 hash_clear(ht);
332 hash_init(ht);
333
Bram Moolenaardd297bc2021-12-10 10:37:38 +0000334 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
335 {
336 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
337
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +0100338 if (sv->sv_flags & SVFLAG_TYPE_ALLOCATED)
Bram Moolenaardd297bc2021-12-10 10:37:38 +0000339 free_type(sv->sv_type);
340 }
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200341 ga_clear(&si->sn_var_vals);
342
343 // existing commands using script variable indexes are no longer valid
344 si->sn_script_seq = current_sctx.sc_seq;
345}
346
347/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100348 * Free all imported items in script "sid".
349 */
350 void
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200351free_imports_and_script_vars(int sid)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100352{
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100353 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100354 int idx;
355
356 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
357 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100358 imported_T *imp = ((imported_T *)si->sn_imports.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100359
360 vim_free(imp->imp_name);
361 }
362 ga_clear(&si->sn_imports);
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200363
364 free_all_script_vars(si);
365
Bram Moolenaar6110e792020-07-08 19:35:21 +0200366 clear_type_list(&si->sn_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100367}
368
369/*
Bram Moolenaara6294952020-12-27 13:39:50 +0100370 * Mark all imports as possible to redefine. Used when a script is loaded
371 * again but not cleared.
372 */
373 void
374mark_imports_for_reload(int sid)
375{
376 scriptitem_T *si = SCRIPT_ITEM(sid);
377 int idx;
378
379 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
380 {
381 imported_T *imp = ((imported_T *)si->sn_imports.ga_data) + idx;
382
383 imp->imp_flags |= IMP_FLAGS_RELOAD;
384 }
385}
386
387/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100388 * Part of "import" that handles a relative or absolute file name/
389 * Returns OK or FAIL.
390 */
391 static int
392handle_import_fname(char_u *fname, int is_autoload, int *sid)
393{
394 if (is_autoload)
395 {
396 scriptitem_T *si;
397
398 *sid = find_script_by_name(fname);
399 if (*sid < 0)
400 {
401 int error = OK;
402
Bram Moolenaar49d008d2022-03-31 11:51:21 +0100403 // Script does not exist yet, check name and create a new
404 // scriptitem.
405 if (!file_is_readable(fname))
406 {
407 semsg(_(mch_isdir(fname) ? e_str_is_directory
408 : e_cannot_read_from_str_2), fname);
409 return FAIL;
410 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100411 *sid = get_new_scriptitem_for_fname(&error, fname);
412 if (error == FAIL)
413 return FAIL;
414 }
415
416 si = SCRIPT_ITEM(*sid);
417 si->sn_import_autoload = TRUE;
418
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100419 if (si->sn_autoload_prefix == NULL)
420 si->sn_autoload_prefix = get_autoload_prefix(si);
421
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100422 // with testing override: load autoload script right away
423 if (!override_autoload || si->sn_state != SN_STATE_NOT_LOADED)
424 return OK;
425 }
426 return do_source(fname, FALSE, DOSO_NONE, sid);
427}
428
429/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100430 * Handle an ":import" command and add the resulting imported_T to "gap", when
431 * not NULL, or script "import_sid" sn_imports.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200432 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100433 * Returns a pointer to after the command or NULL in case of failure
434 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200435 static char_u *
Bram Moolenaar1c991142020-07-04 13:15:31 +0200436handle_import(
437 char_u *arg_start,
438 garray_T *gap,
439 int import_sid,
440 evalarg_T *evalarg,
441 void *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100442{
443 char_u *arg = arg_start;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000444 char_u *nextarg;
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000445 int is_autoload = FALSE;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000446 int getnext;
447 char_u *expr_end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100448 int ret = FAIL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000449 char_u *as_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100450 typval_T tv;
Bram Moolenaar1836d612022-01-18 13:14:47 +0000451 int sid = -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100452 int res;
Bram Moolenaar921ba522021-07-29 22:25:05 +0200453 long start_lnum = SOURCING_LNUM;
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000454 garray_T *import_gap;
455 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100456
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000457 if (STRNCMP(arg, "autoload", 8) == 0 && VIM_ISWHITE(arg[8]))
458 {
459 is_autoload = TRUE;
460 arg = skipwhite(arg + 8);
461 }
462
Bram Moolenaar4db572e2021-07-18 18:21:38 +0200463 // The name of the file can be an expression, which must evaluate to a
464 // string.
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000465 ret = eval0_retarg(arg, &tv, NULL, evalarg, &expr_end);
Bram Moolenaar4db572e2021-07-18 18:21:38 +0200466 if (ret == FAIL)
467 goto erret;
468 if (tv.v_type != VAR_STRING
469 || tv.vval.v_string == NULL || *tv.vval.v_string == NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100470 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000471 semsg(_(e_invalid_string_for_import_str), arg);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200472 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100473 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100474
Bram Moolenaar921ba522021-07-29 22:25:05 +0200475 // Give error messages for the start of the line.
476 SOURCING_LNUM = start_lnum;
477
Bram Moolenaar1c991142020-07-04 13:15:31 +0200478 /*
479 * find script file
480 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100481 if (*tv.vval.v_string == '.')
482 {
483 size_t len;
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100484 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100485 char_u *tail = gettail(si->sn_name);
486 char_u *from_name;
487
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100488 // Relative to current script: "./name.vim", "../../name.vim".
489 len = STRLEN(si->sn_name) - STRLEN(tail) + STRLEN(tv.vval.v_string) + 2;
490 from_name = alloc((int)len);
491 if (from_name == NULL)
492 goto erret;
493 vim_strncpy(from_name, si->sn_name, tail - si->sn_name);
494 add_pathsep(from_name);
495 STRCAT(from_name, tv.vval.v_string);
496 simplify_filename(from_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100497
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100498 res = handle_import_fname(from_name, is_autoload, &sid);
499 vim_free(from_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100500 }
Bram Moolenaar113b8dc2022-01-18 13:43:58 +0000501 else if (mch_isFullName(tv.vval.v_string)
502#ifdef BACKSLASH_IN_FILENAME
503 // On MS-Windows omitting the drive is still handled like an
504 // absolute path, not using 'runtimepath'.
505 || *tv.vval.v_string == '/' || *tv.vval.v_string == '\\'
506#endif
507 )
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100508 {
509 // Absolute path: "/tmp/name.vim"
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100510 res = handle_import_fname(tv.vval.v_string, is_autoload, &sid);
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000511 }
512 else if (is_autoload)
513 {
514 size_t len = 9 + STRLEN(tv.vval.v_string) + 1;
515 char_u *from_name;
516
517 // Find file in "autoload" subdirs in 'runtimepath'.
518 from_name = alloc((int)len);
519 if (from_name == NULL)
520 goto erret;
521 vim_snprintf((char *)from_name, len, "autoload/%s", tv.vval.v_string);
522 // we need a scriptitem without loading the script
523 sid = find_script_in_rtp(from_name);
524 vim_free(from_name);
Bram Moolenaard041f422022-01-12 19:54:00 +0000525 if (SCRIPT_ID_VALID(sid))
526 {
527 scriptitem_T *si = SCRIPT_ITEM(sid);
528
529 if (si->sn_autoload_prefix == NULL)
530 si->sn_autoload_prefix = get_autoload_prefix(si);
531 res = OK;
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +0000532 if (override_autoload && si->sn_state == SN_STATE_NOT_LOADED)
533 // testing override: load autoload script right away
534 (void)do_source(si->sn_name, FALSE, DOSO_NONE, NULL);
Bram Moolenaard041f422022-01-12 19:54:00 +0000535 }
536 else
537 res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100538 }
539 else
540 {
541 size_t len = 7 + STRLEN(tv.vval.v_string) + 1;
542 char_u *from_name;
543
544 // Find file in "import" subdirs in 'runtimepath'.
545 from_name = alloc((int)len);
546 if (from_name == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200547 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100548 vim_snprintf((char *)from_name, len, "import/%s", tv.vval.v_string);
549 res = source_in_path(p_rtp, from_name, DIP_NOAFTER, &sid);
550 vim_free(from_name);
551 }
552
553 if (res == FAIL || sid <= 0)
554 {
Bram Moolenaar1836d612022-01-18 13:14:47 +0000555 semsg(_(is_autoload && sid == -2
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000556 ? e_autoload_import_cannot_use_absolute_or_relative_path
557 : e_could_not_import_str), tv.vval.v_string);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200558 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100559 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100560
Bram Moolenaar779aeff2022-02-08 19:12:19 +0000561 if (sid == current_sctx.sc_sid)
562 {
563 emsg(_(e_script_cannot_import_itself));
564 goto erret;
565 }
566
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000567 import_gap = gap != NULL ? gap : &SCRIPT_ITEM(import_sid)->sn_imports;
568 for (i = 0; i < import_gap->ga_len; ++i)
569 {
570 imported_T *import = (imported_T *)import_gap->ga_data + i;
571
572 if (import->imp_sid == sid)
573 {
574 if (import->imp_flags & IMP_FLAGS_RELOAD)
575 {
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000576 // encountering same script first time on a reload is OK
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000577 import->imp_flags &= ~IMP_FLAGS_RELOAD;
578 break;
579 }
580 semsg(_(e_cannot_import_same_script_twice_str), tv.vval.v_string);
581 goto erret;
582 }
583 }
584
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000585 // Allow for the "as Name" to be in the next line.
586 nextarg = eval_next_non_blank(expr_end, evalarg, &getnext);
587 if (STRNCMP("as", nextarg, 2) == 0 && IS_WHITE_OR_NUL(nextarg[2]))
588 {
589 char_u *p;
590
591 if (getnext)
592 arg = eval_next_line(evalarg);
593 else
594 arg = nextarg;
595
596 // Skip over "as Name "; no line break allowed after "as".
597 // Do not allow for ':' and '#'.
598 arg = skipwhite(arg + 2);
599 p = arg;
600 if (eval_isnamec1(*arg))
601 while (ASCII_ISALNUM(*arg) || *arg == '_')
602 ++arg;
603 if (p == arg || !IS_WHITE_OR_NUL(*arg))
604 {
605 semsg(_(e_syntax_error_in_import_str), p);
606 goto erret;
607 }
608 as_name = vim_strnsave(p, arg - p);
609 arg = skipwhite(arg);
610 }
611 else
612 {
613 char_u *p = gettail(tv.vval.v_string);
614 char_u *end = (char_u *)strstr((char *)p, ".vim");
615
616 if (!ends_excmd2(arg_start, expr_end))
617 {
618 semsg(_(e_trailing_characters_str), expr_end);
619 goto erret;
620 }
Bram Moolenaar834d4182022-01-07 13:38:24 +0000621 if (end == NULL || end[4] != NUL)
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000622 {
Bram Moolenaar834d4182022-01-07 13:38:24 +0000623 semsg(_(e_imported_script_must_use_as_or_end_in_dot_vim_str), p);
624 goto erret;
625 }
626 if (end == p)
627 {
628 semsg(_(e_cannot_import_dot_vim_without_using_as), p);
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000629 goto erret;
630 }
631 as_name = vim_strnsave(p, end - p);
632 }
633
634 if (as_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100635 {
Bram Moolenaar0a842842021-02-27 22:41:19 +0100636 imported_T *imported;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100637
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000638 imported = find_imported(as_name, STRLEN(as_name), FALSE);
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000639 if (imported != NULL && imported->imp_sid != sid)
Bram Moolenaara6294952020-12-27 13:39:50 +0100640 {
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000641 semsg(_(e_name_already_defined_str), as_name);
642 goto erret;
Bram Moolenaara6294952020-12-27 13:39:50 +0100643 }
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000644 else if (imported == NULL
Bram Moolenaardce24412022-02-08 20:35:30 +0000645 && check_defined(as_name, STRLEN(as_name), cctx, NULL,
646 FALSE) == FAIL)
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000647 goto erret;
Bram Moolenaara6294952020-12-27 13:39:50 +0100648
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100649 if (imported == NULL)
Bram Moolenaar17d36cb2022-01-12 11:46:40 +0000650 {
651 imported = new_imported(import_gap);
652 if (imported == NULL)
653 goto erret;
654 imported->imp_name = as_name;
655 as_name = NULL;
656 imported->imp_sid = sid;
657 if (is_autoload)
658 imported->imp_flags = IMP_FLAGS_AUTOLOAD;
659 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100660 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200661
Bram Moolenaar1c991142020-07-04 13:15:31 +0200662erret:
Bram Moolenaar4db572e2021-07-18 18:21:38 +0200663 clear_tv(&tv);
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000664 vim_free(as_name);
665 return arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100666}
667
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200668/*
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000669 * ":import 'filename'"
670 * ":import 'filename' as Name"
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200671 */
672 void
673ex_import(exarg_T *eap)
674{
675 char_u *cmd_end;
676 evalarg_T evalarg;
677
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +0000678 if (!sourcing_a_script(eap))
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200679 {
680 emsg(_(e_import_can_only_be_used_in_script));
681 return;
682 }
683 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
684
685 cmd_end = handle_import(eap->arg, NULL, current_sctx.sc_sid,
686 &evalarg, NULL);
687 if (cmd_end != NULL)
688 set_nextcmd(eap, cmd_end);
689 clear_evalarg(&evalarg, eap);
690}
691
692/*
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000693 * Find an exported item in "sid" matching "name".
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000694 * Either "cctx" or "cstack" is NULL.
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200695 * When it is a variable return the index.
696 * When it is a user function return "*ufunc".
697 * When not found returns -1 and "*ufunc" is NULL.
698 */
699 int
700find_exported(
701 int sid,
702 char_u *name,
703 ufunc_T **ufunc,
704 type_T **type,
705 cctx_T *cctx,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000706 cstack_T *cstack,
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200707 int verbose)
708{
709 int idx = -1;
710 svar_T *sv;
711 scriptitem_T *script = SCRIPT_ITEM(sid);
712
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100713 if (script->sn_import_autoload && script->sn_state == SN_STATE_NOT_LOADED)
714 {
715 if (do_source(script->sn_name, FALSE, DOSO_NONE, NULL) == FAIL)
Bram Moolenaaraac12da2022-04-24 21:33:20 +0100716 {
717 semsg(_(e_cant_open_file_str), script->sn_name);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100718 return -1;
Bram Moolenaaraac12da2022-04-24 21:33:20 +0100719 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100720 }
721
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200722 // Find name in "script".
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000723 idx = get_script_item_idx(sid, name, 0, cctx, cstack);
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200724 if (idx >= 0)
725 {
726 sv = ((svar_T *)script->sn_var_vals.ga_data) + idx;
Bram Moolenaara909c482022-01-06 22:07:57 +0000727 *ufunc = NULL;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +0100728 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200729 {
730 if (verbose)
731 semsg(_(e_item_not_exported_in_script_str), name);
732 return -1;
733 }
734 *type = sv->sv_type;
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200735 }
736 else
737 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +0000738 size_t len = STRLEN(name);
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200739 char_u buffer[200];
740 char_u *funcname;
741
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +0000742 // It could be a user function. Normally this is stored as
743 // "<SNR>99_name". For an autoload script a function is stored with
744 // the autoload prefix: "dir#script#name".
745 if (script->sn_autoload_prefix != NULL)
746 len += STRLEN(script->sn_autoload_prefix) + 2;
747 else
748 len += 15;
749
750 if (len < sizeof(buffer))
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200751 funcname = buffer;
752 else
753 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +0000754 funcname = alloc(len);
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200755 if (funcname == NULL)
756 return -1;
757 }
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +0000758 if (script->sn_autoload_prefix != NULL)
759 {
760 sprintf((char *)funcname, "%s%s", script->sn_autoload_prefix, name);
761 }
762 else
763 {
764 funcname[0] = K_SPECIAL;
765 funcname[1] = KS_EXTRA;
766 funcname[2] = (int)KE_SNR;
767 sprintf((char *)funcname + 3, "%ld_%s", (long)sid, name);
768 }
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000769 *ufunc = find_func(funcname, FALSE);
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200770
771 if (*ufunc == NULL)
772 {
773 if (verbose)
Bram Moolenaard02dce22022-01-18 17:43:04 +0000774 {
775 ufunc_T *alt_ufunc = NULL;
776
777 if (script->sn_autoload_prefix != NULL)
778 {
779 // try find the function by the script-local name
780 funcname[0] = K_SPECIAL;
781 funcname[1] = KS_EXTRA;
782 funcname[2] = (int)KE_SNR;
783 sprintf((char *)funcname + 3, "%ld_%s", (long)sid, name);
784 alt_ufunc = find_func(funcname, FALSE);
785 }
786 if (alt_ufunc != NULL)
787 semsg(_(e_item_not_exported_in_script_str), name);
788 else
789 semsg(_(e_item_not_found_in_script_str), name);
790 }
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200791 }
792 else if (((*ufunc)->uf_flags & FC_EXPORT) == 0)
793 {
794 if (verbose)
795 semsg(_(e_item_not_exported_in_script_str), name);
796 *ufunc = NULL;
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200797 }
Bram Moolenaard02dce22022-01-18 17:43:04 +0000798 if (funcname != buffer)
799 vim_free(funcname);
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200800 }
801
802 return idx;
803}
804
805/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200806 * Declare a script-local variable without init: "let var: type".
807 * "const" is an error since the value is missing.
808 * Returns a pointer to after the type.
809 */
810 char_u *
811vim9_declare_scriptvar(exarg_T *eap, char_u *arg)
812{
813 char_u *p;
814 char_u *name;
815 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
816 type_T *type;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200817 typval_T init_tv;
818
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200819 if (eap->cmdidx == CMD_final || eap->cmdidx == CMD_const)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200820 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200821 if (eap->cmdidx == CMD_final)
822 emsg(_(e_final_requires_a_value));
823 else
824 emsg(_(e_const_requires_a_value));
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200825 return arg + STRLEN(arg);
826 }
827
828 // Check for valid starting character.
829 if (!eval_isnamec1(*arg))
830 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000831 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200832 return arg + STRLEN(arg);
833 }
834
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200835 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
Bram Moolenaar3b74b6b2020-06-19 19:01:43 +0200836 if (*p == ':' && (VIM_ISWHITE(p[1]) || p != arg + 1))
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200837 break;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200838
839 if (*p != ':')
840 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +0200841 emsg(_(e_type_or_initialization_required));
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200842 return arg + STRLEN(arg);
843 }
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200844 if (!VIM_ISWHITE(p[1]))
845 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100846 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200847 return arg + STRLEN(arg);
848 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200849 name = vim_strnsave(arg, p - arg);
850
Bram Moolenaard0edaf92021-05-28 21:06:08 +0200851 // parse type, check for reserved name
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200852 p = skipwhite(p + 1);
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100853 type = parse_type(&p, &si->sn_type_list, TRUE);
Bram Moolenaard0edaf92021-05-28 21:06:08 +0200854 if (type == NULL || check_reserved_name(name) == FAIL)
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200855 {
856 vim_free(name);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200857 return p;
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200858 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200859
860 // Create the variable with 0/NULL value.
861 CLEAR_FIELD(init_tv);
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +0200862 if (type->tt_type == VAR_ANY)
863 // A variable of type "any" is not possible, just use zero instead
864 init_tv.v_type = VAR_NUMBER;
865 else
866 init_tv.v_type = type->tt_type;
Bram Moolenaar859cc212022-03-28 15:22:35 +0100867 set_var_const(name, 0, type, &init_tv, FALSE, ASSIGN_INIT, 0);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200868
869 vim_free(name);
870 return p;
871}
872
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200873/*
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200874 * Vim9 part of adding a script variable: add it to sn_all_vars (lookup by name
875 * with a hashtable) and sn_var_vals (lookup by index).
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100876 * When "create" is TRUE this is a new variable, otherwise find and update an
877 * existing variable.
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +0100878 * "flags" can have ASSIGN_FINAL, ASSIGN_CONST or ASSIGN_INIT.
Bram Moolenaarf2253962021-04-13 20:53:13 +0200879 * When "*type" is NULL use "tv" for the type and update "*type". If
880 * "do_member" is TRUE also use the member type, otherwise use "any".
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200881 */
882 void
Bram Moolenaar08251752021-01-11 21:20:18 +0100883update_vim9_script_var(
884 int create,
885 dictitem_T *di,
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +0000886 char_u *name,
Bram Moolenaar08251752021-01-11 21:20:18 +0100887 int flags,
888 typval_T *tv,
Bram Moolenaarf2253962021-04-13 20:53:13 +0200889 type_T **type,
890 int do_member)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200891{
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100892 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
893 hashitem_T *hi;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200894 svar_T *sv = NULL;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200895
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100896 if (create)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200897 {
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200898 sallvar_T *newsav;
899 sallvar_T *sav = NULL;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200900
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100901 // Store a pointer to the typval_T, so that it can be found by index
902 // instead of using a hastab lookup.
903 if (ga_grow(&si->sn_var_vals, 1) == FAIL)
904 return;
905
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +0000906 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200907 if (!HASHITEM_EMPTY(hi))
908 {
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200909 // Variable with this name exists, either in this block or in
910 // another block.
911 for (sav = HI2SAV(hi); ; sav = sav->sav_next)
912 {
913 if (sav->sav_block_id == si->sn_current_block_id)
914 {
915 // variable defined in a loop, re-use the entry
916 sv = ((svar_T *)si->sn_var_vals.ga_data)
917 + sav->sav_var_vals_idx;
918 // unhide the variable
919 if (sv->sv_tv == &sav->sav_tv)
920 {
921 clear_tv(&sav->sav_tv);
922 sv->sv_tv = &di->di_tv;
923 sav->sav_di = di;
924 }
925 break;
926 }
927 if (sav->sav_next == NULL)
928 break;
929 }
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200930 }
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200931
932 if (sv == NULL)
933 {
934 // Variable not defined or not defined in current block: Add a
935 // svar_T and create a new sallvar_T.
936 sv = ((svar_T *)si->sn_var_vals.ga_data) + si->sn_var_vals.ga_len;
937 newsav = (sallvar_T *)alloc_clear(
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +0000938 sizeof(sallvar_T) + STRLEN(name));
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200939 if (newsav == NULL)
940 return;
941
942 sv->sv_tv = &di->di_tv;
943 sv->sv_const = (flags & ASSIGN_FINAL) ? ASSIGN_FINAL
944 : (flags & ASSIGN_CONST) ? ASSIGN_CONST : 0;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +0100945 sv->sv_flags = is_export ? SVFLAG_EXPORTED : 0;
946 if ((flags & ASSIGN_INIT) == 0)
947 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200948 newsav->sav_var_vals_idx = si->sn_var_vals.ga_len;
949 ++si->sn_var_vals.ga_len;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +0000950 STRCPY(&newsav->sav_key, name);
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200951 sv->sv_name = newsav->sav_key;
952 newsav->sav_di = di;
953 newsav->sav_block_id = si->sn_current_block_id;
954
955 if (HASHITEM_EMPTY(hi))
956 // new variable name
957 hash_add(&si->sn_all_vars.dv_hashtab, newsav->sav_key);
958 else if (sav != NULL)
959 // existing name in a new block, append to the list
960 sav->sav_next = newsav;
961 }
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200962 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100963 else
964 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +0100965 sv = find_typval_in_script(&di->di_tv, 0, TRUE);
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100966 }
967 if (sv != NULL)
968 {
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100969 if (*type == NULL)
Bram Moolenaar114dbda2022-01-03 12:28:03 +0000970 *type = typval2type(tv, get_copyID(), &si->sn_type_list,
971 do_member ? TVTT_DO_MEMBER : 0);
Bram Moolenaar859cc212022-03-28 15:22:35 +0100972 else if ((flags & ASSIGN_INIT) == 0
973 && (*type)->tt_type == VAR_BLOB && tv->v_type == VAR_BLOB
974 && tv->vval.v_blob == NULL)
975 {
976 // "var b: blob = null_blob" has a different type.
977 *type = &t_blob_null;
978 }
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +0100979 if (sv->sv_flags & SVFLAG_TYPE_ALLOCATED)
Bram Moolenaardd297bc2021-12-10 10:37:38 +0000980 free_type(sv->sv_type);
981 if (*type != NULL && ((*type)->tt_type == VAR_FUNC
982 || (*type)->tt_type == VAR_PARTIAL))
983 {
984 // The type probably uses uf_type_list, which is cleared when the
985 // function is freed, but the script variable may keep the type.
986 // Make a copy to avoid using freed memory.
987 sv->sv_type = alloc_type(*type);
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +0100988 sv->sv_flags |= SVFLAG_TYPE_ALLOCATED;
Bram Moolenaardd297bc2021-12-10 10:37:38 +0000989 }
990 else
991 {
992 sv->sv_type = *type;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +0100993 sv->sv_flags &= ~SVFLAG_TYPE_ALLOCATED;
Bram Moolenaardd297bc2021-12-10 10:37:38 +0000994 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100995 }
996
997 // let ex_export() know the export worked.
998 is_export = FALSE;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200999}
1000
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001001/*
1002 * Hide a script variable when leaving a block.
1003 * "idx" is de index in sn_var_vals.
Bram Moolenaar39ca4122020-10-20 14:25:07 +02001004 * When "func_defined" is non-zero then a function was defined in this block,
1005 * the variable may be accessed by it. Otherwise the variable can be cleared.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001006 */
Bram Moolenaar8d739de2020-10-14 19:39:19 +02001007 void
Bram Moolenaar39ca4122020-10-20 14:25:07 +02001008hide_script_var(scriptitem_T *si, int idx, int func_defined)
Bram Moolenaar8d739de2020-10-14 19:39:19 +02001009{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001010 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8d739de2020-10-14 19:39:19 +02001011 hashtab_T *script_ht = get_script_local_ht();
1012 hashtab_T *all_ht = &si->sn_all_vars.dv_hashtab;
1013 hashitem_T *script_hi;
1014 hashitem_T *all_hi;
1015
1016 // Remove a variable declared inside the block, if it still exists.
Bram Moolenaar39ca4122020-10-20 14:25:07 +02001017 // If it was added in a nested block it will already have been removed.
Bram Moolenaar8d739de2020-10-14 19:39:19 +02001018 // The typval is moved into the sallvar_T.
1019 script_hi = hash_find(script_ht, sv->sv_name);
1020 all_hi = hash_find(all_ht, sv->sv_name);
1021 if (!HASHITEM_EMPTY(script_hi) && !HASHITEM_EMPTY(all_hi))
1022 {
1023 dictitem_T *di = HI2DI(script_hi);
1024 sallvar_T *sav = HI2SAV(all_hi);
Bram Moolenaar39ca4122020-10-20 14:25:07 +02001025 sallvar_T *sav_prev = NULL;
Bram Moolenaar8d739de2020-10-14 19:39:19 +02001026
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001027 // There can be multiple entries with the same name in different
1028 // blocks, find the right one.
1029 while (sav != NULL && sav->sav_var_vals_idx != idx)
Bram Moolenaar39ca4122020-10-20 14:25:07 +02001030 {
1031 sav_prev = sav;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001032 sav = sav->sav_next;
Bram Moolenaar39ca4122020-10-20 14:25:07 +02001033 }
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001034 if (sav != NULL)
1035 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02001036 if (func_defined)
1037 {
1038 // move the typval from the dictitem to the sallvar
1039 sav->sav_tv = di->di_tv;
1040 di->di_tv.v_type = VAR_UNKNOWN;
1041 sav->sav_flags = di->di_flags;
1042 sav->sav_di = NULL;
1043 sv->sv_tv = &sav->sav_tv;
1044 }
1045 else
1046 {
1047 if (sav_prev == NULL)
1048 hash_remove(all_ht, all_hi);
1049 else
1050 sav_prev->sav_next = sav->sav_next;
1051 sv->sv_name = NULL;
1052 vim_free(sav);
1053 }
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001054 delete_var(script_ht, script_hi);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001055 }
Bram Moolenaar8d739de2020-10-14 19:39:19 +02001056 }
1057}
1058
1059/*
Bram Moolenaar10c65862020-10-08 21:16:42 +02001060 * Find the script-local variable that links to "dest".
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001061 * If "sid" is zero use the current script.
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001062 * if "must_find" is TRUE and "dest" cannot be found report an internal error.
Bram Moolenaarc967d572021-07-08 21:38:50 +02001063 * Returns NULL if not found and give an internal error.
Bram Moolenaar34db91f2020-06-13 19:00:10 +02001064 */
Bram Moolenaar10c65862020-10-08 21:16:42 +02001065 svar_T *
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001066find_typval_in_script(typval_T *dest, scid_T sid, int must_find)
Bram Moolenaar34db91f2020-06-13 19:00:10 +02001067{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001068 scriptitem_T *si = SCRIPT_ITEM(sid == 0 ? current_sctx.sc_sid : sid);
Bram Moolenaar34db91f2020-06-13 19:00:10 +02001069 int idx;
1070
Bram Moolenaar81e17fb2020-08-21 21:55:43 +02001071 if (si->sn_version != SCRIPT_VERSION_VIM9)
1072 // legacy script doesn't store variable types
Bram Moolenaar10c65862020-10-08 21:16:42 +02001073 return NULL;
Bram Moolenaar81e17fb2020-08-21 21:55:43 +02001074
Bram Moolenaara06758d2021-10-15 00:18:37 +01001075 // Find the svar_T in sn_var_vals. Start at the end, in a for loop the
1076 // variable was added at the end.
1077 for (idx = si->sn_var_vals.ga_len - 1; idx >= 0; --idx)
Bram Moolenaar34db91f2020-06-13 19:00:10 +02001078 {
1079 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1080
Bram Moolenaarc0913d02020-12-05 14:44:37 +01001081 // If "sv_name" is NULL the variable was hidden when leaving a block,
1082 // don't check "sv_tv" then, it might be used for another variable now.
1083 if (sv->sv_name != NULL && sv->sv_tv == dest)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001084 return sv;
Bram Moolenaar34db91f2020-06-13 19:00:10 +02001085 }
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001086 if (must_find)
1087 iemsg("find_typval_in_script(): not found");
Bram Moolenaar10c65862020-10-08 21:16:42 +02001088 return NULL;
1089}
1090
1091/*
1092 * Check if the type of script variable "dest" allows assigning "value".
1093 * If needed convert "value" to a bool.
1094 */
1095 int
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001096check_script_var_type(
Bram Moolenaar7824fc82021-11-26 17:36:51 +00001097 svar_T *sv,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001098 typval_T *value,
1099 char_u *name,
1100 where_T where)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001101{
Bram Moolenaar10c65862020-10-08 21:16:42 +02001102 int ret;
1103
Bram Moolenaar7824fc82021-11-26 17:36:51 +00001104 if (sv->sv_const != 0)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001105 {
Bram Moolenaar7824fc82021-11-26 17:36:51 +00001106 semsg(_(e_cannot_change_readonly_variable_str), name);
1107 return FAIL;
Bram Moolenaar10c65862020-10-08 21:16:42 +02001108 }
Bram Moolenaar7824fc82021-11-26 17:36:51 +00001109 ret = check_typval_type(sv->sv_type, value, where);
1110 if (ret == OK && need_convert_to_bool(sv->sv_type, value))
1111 {
1112 int val = tv2bool(value);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001113
Bram Moolenaar7824fc82021-11-26 17:36:51 +00001114 clear_tv(value);
1115 value->v_type = VAR_BOOL;
1116 value->v_lock = 0;
1117 value->vval.v_number = val ? VVAL_TRUE : VVAL_FALSE;
1118 }
1119 return ret;
Bram Moolenaar34db91f2020-06-13 19:00:10 +02001120}
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001121
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001122// words that cannot be used as a variable
1123static char *reserved[] = {
1124 "true",
1125 "false",
1126 "null",
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00001127 "null_blob",
1128 "null_dict",
1129 "null_function",
1130 "null_list",
1131 "null_partial",
1132 "null_string",
1133 "null_channel",
1134 "null_job",
Bram Moolenaar74235772021-06-12 14:53:05 +02001135 "this",
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001136 NULL
1137};
1138
1139 int
1140check_reserved_name(char_u *name)
1141{
1142 int idx;
1143
1144 for (idx = 0; reserved[idx] != NULL; ++idx)
1145 if (STRCMP(reserved[idx], name) == 0)
1146 {
1147 semsg(_(e_cannot_use_reserved_name), name);
1148 return FAIL;
1149 }
1150 return OK;
1151}
1152
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001153#endif // FEAT_EVAL