blob: a8b76bb6c470d85639a0c435ab0817cb82b1bffa [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9script.c: :vim9script, :import, :export and friends
12 */
13
14#include "vim.h"
15
Bram Moolenaardc7c3662021-12-20 15:04:29 +000016// When not generating protos this is included in proto.h
17#ifdef PROTO
Bram Moolenaar9b8d6222020-12-28 18:26:00 +010018# include "vim9.h"
19#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010020
Bram Moolenaare535db82021-03-31 21:07:24 +020021/*
22 * Return TRUE when currently using Vim9 script syntax.
23 * Does not go up the stack, a ":function" inside vim9script uses legacy
24 * syntax.
25 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010026 int
27in_vim9script(void)
28{
Bram Moolenaare535db82021-03-31 21:07:24 +020029 // "sc_version" is also set when compiling a ":def" function in legacy
30 // script.
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +020031 return (current_sctx.sc_version == SCRIPT_VERSION_VIM9
32 || (cmdmod.cmod_flags & CMOD_VIM9CMD))
33 && !(cmdmod.cmod_flags & CMOD_LEGACY);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010034}
35
Bram Moolenaarb91d3f82021-04-01 13:17:50 +020036#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010037/*
Bram Moolenaardd9de502021-08-15 13:49:42 +020038 * Return TRUE when currently in a script with script version smaller than
39 * "max_version" or command modifiers forced it.
40 */
41 int
42in_old_script(int max_version)
43{
Bram Moolenaar5cebca22021-08-15 14:39:13 +020044 return (current_sctx.sc_version < max_version
45 && !(cmdmod.cmod_flags & CMOD_VIM9CMD))
Bram Moolenaardd9de502021-08-15 13:49:42 +020046 || (cmdmod.cmod_flags & CMOD_LEGACY);
47}
48
49/*
Bram Moolenaare535db82021-03-31 21:07:24 +020050 * Return TRUE if the current script is Vim9 script.
51 * This also returns TRUE in a legacy function in a Vim9 script.
52 */
53 int
54current_script_is_vim9(void)
55{
56 return SCRIPT_ID_VALID(current_sctx.sc_sid)
57 && SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
58 == SCRIPT_VERSION_VIM9;
59}
Bram Moolenaarb91d3f82021-04-01 13:17:50 +020060#endif
Bram Moolenaare535db82021-03-31 21:07:24 +020061
62/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010063 * ":vim9script".
64 */
65 void
Bram Moolenaar9b8d6222020-12-28 18:26:00 +010066ex_vim9script(exarg_T *eap UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010067{
Bram Moolenaar9b8d6222020-12-28 18:26:00 +010068#ifdef FEAT_EVAL
Bram Moolenaar2b327002020-12-26 15:39:31 +010069 int sid = current_sctx.sc_sid;
Bram Moolenaar101f4812020-06-16 23:18:51 +020070 scriptitem_T *si;
Bram Moolenaardc4451d2022-01-09 21:36:37 +000071 int found_noclear = FALSE;
72 int found_autoload = FALSE;
73 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010074
75 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
76 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +020077 emsg(_(e_vim9script_can_only_be_used_in_script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010078 return;
79 }
Bram Moolenaar2b327002020-12-26 15:39:31 +010080
81 si = SCRIPT_ITEM(sid);
82 if (si->sn_state == SN_STATE_HAD_COMMAND)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010083 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +020084 emsg(_(e_vim9script_must_be_first_command_in_script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010085 return;
86 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +000087
88 for (p = eap->arg; !IS_WHITE_OR_NUL(*p); p = skipwhite(skiptowhite(p)))
Bram Moolenaar2b327002020-12-26 15:39:31 +010089 {
Bram Moolenaardc4451d2022-01-09 21:36:37 +000090 if (STRNCMP(p, "noclear", 7) == 0 && IS_WHITE_OR_NUL(p[7]))
91 {
92 if (found_noclear)
93 {
94 semsg(_(e_duplicate_argument_str), p);
95 return;
96 }
97 found_noclear = TRUE;
98 }
99 else if (STRNCMP(p, "autoload", 8) == 0 && IS_WHITE_OR_NUL(p[8]))
100 {
101 if (found_autoload)
102 {
103 semsg(_(e_duplicate_argument_str), p);
104 return;
105 }
106 found_autoload = TRUE;
107 if (script_name_after_autoload(si) == NULL)
108 {
109 emsg(_(e_using_autoload_in_script_not_under_autoload_directory));
110 return;
111 }
112 }
113 else
114 {
115 semsg(_(e_invalid_argument_str), eap->arg);
116 return;
117 }
Bram Moolenaar2b327002020-12-26 15:39:31 +0100118 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000119
120 if (si->sn_state == SN_STATE_RELOAD && !found_noclear)
Bram Moolenaar2b327002020-12-26 15:39:31 +0100121 {
122 hashtab_T *ht = &SCRIPT_VARS(sid);
123
124 // Reloading a script without the "noclear" argument: clear
125 // script-local variables and functions.
126 hashtab_free_contents(ht);
127 hash_init(ht);
128 delete_script_functions(sid);
129
130 // old imports and script variables are no longer valid
131 free_imports_and_script_vars(sid);
132 }
133 si->sn_state = SN_STATE_HAD_COMMAND;
134
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000135 si->sn_is_autoload = found_autoload;
136
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100137 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
138 si->sn_version = SCRIPT_VERSION_VIM9;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100139
140 if (STRCMP(p_cpo, CPO_VIM) != 0)
141 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200142 si->sn_save_cpo = vim_strsave(p_cpo);
Bram Moolenaar37294bd2021-03-10 13:40:08 +0100143 set_option_value((char_u *)"cpo", 0L, (char_u *)CPO_VIM, OPT_NO_REDRAW);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100144 }
Bram Moolenaar9b8d6222020-12-28 18:26:00 +0100145#else
146 // No check for this being the first command, it doesn't matter.
147 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
148#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100149}
150
Dominique Pelle748b3082022-01-08 12:41:16 +0000151#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100152/*
Bram Moolenaarae616492020-07-28 20:07:27 +0200153 * When in Vim9 script give an error and return FAIL.
154 */
155 int
156not_in_vim9(exarg_T *eap)
157{
Bram Moolenaar68e30442020-07-28 21:15:07 +0200158 if (in_vim9script())
159 switch (eap->cmdidx)
160 {
Bram Moolenaarada1d872021-02-20 08:16:51 +0100161 case CMD_k:
162 if (eap->addr_count > 0)
163 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000164 emsg(_(e_no_range_allowed));
Bram Moolenaarada1d872021-02-20 08:16:51 +0100165 return FAIL;
166 }
167 // FALLTHROUGH
Bram Moolenaar68e30442020-07-28 21:15:07 +0200168 case CMD_append:
169 case CMD_change:
Bram Moolenaarf5a48012020-08-01 17:00:03 +0200170 case CMD_insert:
Bram Moolenaar65088802021-03-13 21:07:21 +0100171 case CMD_open:
Bram Moolenaarf5a48012020-08-01 17:00:03 +0200172 case CMD_t:
Bram Moolenaar68e30442020-07-28 21:15:07 +0200173 case CMD_xit:
Bram Moolenaar9b8d6222020-12-28 18:26:00 +0100174 semsg(_(e_command_not_supported_in_vim9_script_missing_var_str), eap->cmd);
Bram Moolenaar68e30442020-07-28 21:15:07 +0200175 return FAIL;
176 default: break;
177 }
Bram Moolenaarae616492020-07-28 20:07:27 +0200178 return OK;
179}
180
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100181/*
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100182 * Give an error message if "p" points at "#{" and return TRUE.
183 * This avoids that using a legacy style #{} dictionary leads to difficult to
184 * understand errors.
185 */
186 int
187vim9_bad_comment(char_u *p)
188{
Bram Moolenaara0399ef2021-03-20 15:00:01 +0100189 if (p[0] == '#' && p[1] == '{' && p[2] != '{')
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100190 {
191 emsg(_(e_cannot_use_hash_curly_to_start_comment));
192 return TRUE;
193 }
194 return FALSE;
195}
Dominique Pelle748b3082022-01-08 12:41:16 +0000196#endif
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100197
198/*
Bram Moolenaara0399ef2021-03-20 15:00:01 +0100199 * Return TRUE if "p" points at a "#" not followed by one '{'.
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100200 * Does not check for white space.
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100201 */
202 int
203vim9_comment_start(char_u *p)
204{
Bram Moolenaara0399ef2021-03-20 15:00:01 +0100205 return p[0] == '#' && (p[1] != '{' || p[2] == '{');
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100206}
207
Bram Moolenaar9b8d6222020-12-28 18:26:00 +0100208#if defined(FEAT_EVAL) || defined(PROTO)
209
Bram Moolenaarae616492020-07-28 20:07:27 +0200210/*
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200211 * "++nr" and "--nr" commands.
212 */
213 void
214ex_incdec(exarg_T *eap)
215{
216 char_u *cmd = eap->cmd;
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200217 char_u *nextcmd = eap->nextcmd;
218 size_t len = STRLEN(eap->cmd) + 8;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200219
Bram Moolenaar22480d12021-06-25 21:31:09 +0200220 if (VIM_ISWHITE(cmd[2]))
221 {
222 semsg(_(e_no_white_space_allowed_after_str_str),
223 eap->cmdidx == CMD_increment ? "++" : "--", eap->cmd);
224 return;
225 }
226
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200227 // This works like "nr += 1" or "nr -= 1".
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200228 // Add a '|' to avoid looking in the next line.
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200229 eap->cmd = alloc(len);
230 if (eap->cmd == NULL)
231 return;
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200232 vim_snprintf((char *)eap->cmd, len, "%s %c= 1 |", cmd + 2,
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200233 eap->cmdidx == CMD_increment ? '+' : '-');
234 eap->arg = eap->cmd;
235 eap->cmdidx = CMD_var;
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200236 eap->nextcmd = NULL;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200237 ex_let(eap);
238 vim_free(eap->cmd);
239 eap->cmd = cmd;
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200240 eap->nextcmd = nextcmd;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200241}
242
243/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100244 * ":export let Name: type"
245 * ":export const Name: type"
246 * ":export def Name(..."
247 * ":export class Name ..."
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100248 */
249 void
Bram Moolenaar09689a02020-05-09 22:50:08 +0200250ex_export(exarg_T *eap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100251{
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200252 if (!in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100253 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200254 emsg(_(e_export_can_only_be_used_in_vim9script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100255 return;
256 }
257
258 eap->cmd = eap->arg;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +0100259 (void)find_ex_command(eap, NULL, lookup_scriptitem, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100260 switch (eap->cmdidx)
261 {
262 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200263 case CMD_var:
264 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100265 case CMD_const:
266 case CMD_def:
267 // case CMD_class:
268 is_export = TRUE;
269 do_cmdline(eap->cmd, eap->getline, eap->cookie,
270 DOCMD_VERBOSE + DOCMD_NOWAIT);
271
272 // The command will reset "is_export" when exporting an item.
273 if (is_export)
274 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200275 emsg(_(e_export_with_invalid_argument));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100276 is_export = FALSE;
277 }
278 break;
279 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200280 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 Moolenaar8a7d6542020-01-26 15:56:19 +0100387 * Handle an ":import" command and add the resulting imported_T to "gap", when
388 * not NULL, or script "import_sid" sn_imports.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200389 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100390 * Returns a pointer to after the command or NULL in case of failure
391 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200392 static char_u *
Bram Moolenaar1c991142020-07-04 13:15:31 +0200393handle_import(
394 char_u *arg_start,
395 garray_T *gap,
396 int import_sid,
397 evalarg_T *evalarg,
398 void *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100399{
400 char_u *arg = arg_start;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000401 char_u *nextarg;
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000402 int is_autoload = FALSE;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000403 int getnext;
404 char_u *expr_end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100405 int ret = FAIL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000406 char_u *as_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100407 typval_T tv;
408 int sid = -1;
409 int res;
Bram Moolenaar921ba522021-07-29 22:25:05 +0200410 long start_lnum = SOURCING_LNUM;
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000411 garray_T *import_gap;
412 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100413
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000414 if (STRNCMP(arg, "autoload", 8) == 0 && VIM_ISWHITE(arg[8]))
415 {
416 is_autoload = TRUE;
417 arg = skipwhite(arg + 8);
418 }
419
Bram Moolenaar4db572e2021-07-18 18:21:38 +0200420 // The name of the file can be an expression, which must evaluate to a
421 // string.
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000422 ret = eval0_retarg(arg, &tv, NULL, evalarg, &expr_end);
Bram Moolenaar4db572e2021-07-18 18:21:38 +0200423 if (ret == FAIL)
424 goto erret;
425 if (tv.v_type != VAR_STRING
426 || tv.vval.v_string == NULL || *tv.vval.v_string == NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100427 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000428 semsg(_(e_invalid_string_for_import_str), arg);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200429 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100430 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100431
Bram Moolenaar921ba522021-07-29 22:25:05 +0200432 // Give error messages for the start of the line.
433 SOURCING_LNUM = start_lnum;
434
Bram Moolenaar1c991142020-07-04 13:15:31 +0200435 /*
436 * find script file
437 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100438 if (*tv.vval.v_string == '.')
439 {
440 size_t len;
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100441 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100442 char_u *tail = gettail(si->sn_name);
443 char_u *from_name;
444
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000445 if (is_autoload)
446 res = FAIL;
447 else
448 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100449
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000450 // Relative to current script: "./name.vim", "../../name.vim".
451 len = STRLEN(si->sn_name) - STRLEN(tail)
452 + STRLEN(tv.vval.v_string) + 2;
453 from_name = alloc((int)len);
454 if (from_name == NULL)
455 goto erret;
456 vim_strncpy(from_name, si->sn_name, tail - si->sn_name);
457 add_pathsep(from_name);
458 STRCAT(from_name, tv.vval.v_string);
459 simplify_filename(from_name);
460
461 res = do_source(from_name, FALSE, DOSO_NONE, &sid);
462 vim_free(from_name);
463 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100464 }
465 else if (mch_isFullName(tv.vval.v_string))
466 {
467 // Absolute path: "/tmp/name.vim"
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000468 if (is_autoload)
469 res = FAIL;
470 else
471 res = do_source(tv.vval.v_string, FALSE, DOSO_NONE, &sid);
472 }
473 else if (is_autoload)
474 {
475 size_t len = 9 + STRLEN(tv.vval.v_string) + 1;
476 char_u *from_name;
477
478 // Find file in "autoload" subdirs in 'runtimepath'.
479 from_name = alloc((int)len);
480 if (from_name == NULL)
481 goto erret;
482 vim_snprintf((char *)from_name, len, "autoload/%s", tv.vval.v_string);
483 // we need a scriptitem without loading the script
484 sid = find_script_in_rtp(from_name);
485 vim_free(from_name);
486 res = SCRIPT_ID_VALID(sid) ? OK : FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100487 }
488 else
489 {
490 size_t len = 7 + STRLEN(tv.vval.v_string) + 1;
491 char_u *from_name;
492
493 // Find file in "import" subdirs in 'runtimepath'.
494 from_name = alloc((int)len);
495 if (from_name == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200496 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100497 vim_snprintf((char *)from_name, len, "import/%s", tv.vval.v_string);
498 res = source_in_path(p_rtp, from_name, DIP_NOAFTER, &sid);
499 vim_free(from_name);
500 }
501
502 if (res == FAIL || sid <= 0)
503 {
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000504 semsg(_(is_autoload && sid <= 0
505 ? e_autoload_import_cannot_use_absolute_or_relative_path
506 : e_could_not_import_str), tv.vval.v_string);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200507 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100508 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100509
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000510 import_gap = gap != NULL ? gap : &SCRIPT_ITEM(import_sid)->sn_imports;
511 for (i = 0; i < import_gap->ga_len; ++i)
512 {
513 imported_T *import = (imported_T *)import_gap->ga_data + i;
514
515 if (import->imp_sid == sid)
516 {
517 if (import->imp_flags & IMP_FLAGS_RELOAD)
518 {
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000519 // encountering same script first time on a reload is OK
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000520 import->imp_flags &= ~IMP_FLAGS_RELOAD;
521 break;
522 }
523 semsg(_(e_cannot_import_same_script_twice_str), tv.vval.v_string);
524 goto erret;
525 }
526 }
527
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000528 // Allow for the "as Name" to be in the next line.
529 nextarg = eval_next_non_blank(expr_end, evalarg, &getnext);
530 if (STRNCMP("as", nextarg, 2) == 0 && IS_WHITE_OR_NUL(nextarg[2]))
531 {
532 char_u *p;
533
534 if (getnext)
535 arg = eval_next_line(evalarg);
536 else
537 arg = nextarg;
538
539 // Skip over "as Name "; no line break allowed after "as".
540 // Do not allow for ':' and '#'.
541 arg = skipwhite(arg + 2);
542 p = arg;
543 if (eval_isnamec1(*arg))
544 while (ASCII_ISALNUM(*arg) || *arg == '_')
545 ++arg;
546 if (p == arg || !IS_WHITE_OR_NUL(*arg))
547 {
548 semsg(_(e_syntax_error_in_import_str), p);
549 goto erret;
550 }
551 as_name = vim_strnsave(p, arg - p);
552 arg = skipwhite(arg);
553 }
554 else
555 {
556 char_u *p = gettail(tv.vval.v_string);
557 char_u *end = (char_u *)strstr((char *)p, ".vim");
558
559 if (!ends_excmd2(arg_start, expr_end))
560 {
561 semsg(_(e_trailing_characters_str), expr_end);
562 goto erret;
563 }
Bram Moolenaar834d4182022-01-07 13:38:24 +0000564 if (end == NULL || end[4] != NUL)
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000565 {
Bram Moolenaar834d4182022-01-07 13:38:24 +0000566 semsg(_(e_imported_script_must_use_as_or_end_in_dot_vim_str), p);
567 goto erret;
568 }
569 if (end == p)
570 {
571 semsg(_(e_cannot_import_dot_vim_without_using_as), p);
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000572 goto erret;
573 }
574 as_name = vim_strnsave(p, end - p);
575 }
576
577 if (as_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100578 {
Bram Moolenaar0a842842021-02-27 22:41:19 +0100579 imported_T *imported;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100580
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000581 imported = find_imported(as_name, FALSE, STRLEN(as_name), cctx);
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000582 if (imported != NULL && imported->imp_sid != sid)
Bram Moolenaara6294952020-12-27 13:39:50 +0100583 {
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000584 semsg(_(e_name_already_defined_str), as_name);
585 goto erret;
Bram Moolenaara6294952020-12-27 13:39:50 +0100586 }
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000587 else if (imported == NULL
588 && check_defined(as_name, STRLEN(as_name), cctx, FALSE) == FAIL)
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000589 goto erret;
Bram Moolenaara6294952020-12-27 13:39:50 +0100590
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000591 imported = new_imported(import_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100592 if (imported == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200593 goto erret;
594 imported->imp_name = as_name;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000595 as_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100596 imported->imp_sid = sid;
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000597 if (is_autoload)
598 imported->imp_flags = IMP_FLAGS_AUTOLOAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100599 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200600
Bram Moolenaar1c991142020-07-04 13:15:31 +0200601erret:
Bram Moolenaar4db572e2021-07-18 18:21:38 +0200602 clear_tv(&tv);
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000603 vim_free(as_name);
604 return arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100605}
606
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200607/*
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000608 * ":import 'filename'"
609 * ":import 'filename' as Name"
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200610 */
611 void
612ex_import(exarg_T *eap)
613{
614 char_u *cmd_end;
615 evalarg_T evalarg;
616
617 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
618 {
619 emsg(_(e_import_can_only_be_used_in_script));
620 return;
621 }
622 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
623
624 cmd_end = handle_import(eap->arg, NULL, current_sctx.sc_sid,
625 &evalarg, NULL);
626 if (cmd_end != NULL)
627 set_nextcmd(eap, cmd_end);
628 clear_evalarg(&evalarg, eap);
629}
630
631/*
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000632 * Find an exported item in "sid" matching "name".
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200633 * When it is a variable return the index.
634 * When it is a user function return "*ufunc".
635 * When not found returns -1 and "*ufunc" is NULL.
636 */
637 int
638find_exported(
639 int sid,
640 char_u *name,
641 ufunc_T **ufunc,
642 type_T **type,
643 cctx_T *cctx,
644 int verbose)
645{
646 int idx = -1;
647 svar_T *sv;
648 scriptitem_T *script = SCRIPT_ITEM(sid);
649
650 // Find name in "script".
651 idx = get_script_item_idx(sid, name, 0, cctx);
652 if (idx >= 0)
653 {
654 sv = ((svar_T *)script->sn_var_vals.ga_data) + idx;
Bram Moolenaara909c482022-01-06 22:07:57 +0000655 *ufunc = NULL;
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200656 if (!sv->sv_export)
657 {
658 if (verbose)
659 semsg(_(e_item_not_exported_in_script_str), name);
660 return -1;
661 }
662 *type = sv->sv_type;
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200663 }
664 else
665 {
666 char_u buffer[200];
667 char_u *funcname;
668
669 // it could be a user function.
670 if (STRLEN(name) < sizeof(buffer) - 15)
671 funcname = buffer;
672 else
673 {
674 funcname = alloc(STRLEN(name) + 15);
675 if (funcname == NULL)
676 return -1;
677 }
678 funcname[0] = K_SPECIAL;
679 funcname[1] = KS_EXTRA;
680 funcname[2] = (int)KE_SNR;
681 sprintf((char *)funcname + 3, "%ld_%s", (long)sid, name);
682 *ufunc = find_func(funcname, FALSE, NULL);
683 if (funcname != buffer)
684 vim_free(funcname);
685
686 if (*ufunc == NULL)
687 {
688 if (verbose)
689 semsg(_(e_item_not_found_in_script_str), name);
690 return -1;
691 }
692 else if (((*ufunc)->uf_flags & FC_EXPORT) == 0)
693 {
694 if (verbose)
695 semsg(_(e_item_not_exported_in_script_str), name);
696 *ufunc = NULL;
697 return -1;
698 }
699 }
700
701 return idx;
702}
703
704/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200705 * Declare a script-local variable without init: "let var: type".
706 * "const" is an error since the value is missing.
707 * Returns a pointer to after the type.
708 */
709 char_u *
710vim9_declare_scriptvar(exarg_T *eap, char_u *arg)
711{
712 char_u *p;
713 char_u *name;
714 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
715 type_T *type;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200716 typval_T init_tv;
717
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200718 if (eap->cmdidx == CMD_final || eap->cmdidx == CMD_const)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200719 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200720 if (eap->cmdidx == CMD_final)
721 emsg(_(e_final_requires_a_value));
722 else
723 emsg(_(e_const_requires_a_value));
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200724 return arg + STRLEN(arg);
725 }
726
727 // Check for valid starting character.
728 if (!eval_isnamec1(*arg))
729 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000730 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200731 return arg + STRLEN(arg);
732 }
733
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200734 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
Bram Moolenaar3b74b6b2020-06-19 19:01:43 +0200735 if (*p == ':' && (VIM_ISWHITE(p[1]) || p != arg + 1))
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200736 break;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200737
738 if (*p != ':')
739 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +0200740 emsg(_(e_type_or_initialization_required));
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200741 return arg + STRLEN(arg);
742 }
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200743 if (!VIM_ISWHITE(p[1]))
744 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100745 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200746 return arg + STRLEN(arg);
747 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200748 name = vim_strnsave(arg, p - arg);
749
Bram Moolenaard0edaf92021-05-28 21:06:08 +0200750 // parse type, check for reserved name
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200751 p = skipwhite(p + 1);
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100752 type = parse_type(&p, &si->sn_type_list, TRUE);
Bram Moolenaard0edaf92021-05-28 21:06:08 +0200753 if (type == NULL || check_reserved_name(name) == FAIL)
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200754 {
755 vim_free(name);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200756 return p;
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200757 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200758
759 // Create the variable with 0/NULL value.
760 CLEAR_FIELD(init_tv);
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +0200761 if (type->tt_type == VAR_ANY)
762 // A variable of type "any" is not possible, just use zero instead
763 init_tv.v_type = VAR_NUMBER;
764 else
765 init_tv.v_type = type->tt_type;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000766 set_var_const(name, 0, type, &init_tv, FALSE, 0, 0);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200767
768 vim_free(name);
769 return p;
770}
771
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200772/*
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200773 * Vim9 part of adding a script variable: add it to sn_all_vars (lookup by name
774 * with a hashtable) and sn_var_vals (lookup by index).
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100775 * When "create" is TRUE this is a new variable, otherwise find and update an
776 * existing variable.
Bram Moolenaar08251752021-01-11 21:20:18 +0100777 * "flags" can have ASSIGN_FINAL or ASSIGN_CONST.
Bram Moolenaarf2253962021-04-13 20:53:13 +0200778 * When "*type" is NULL use "tv" for the type and update "*type". If
779 * "do_member" is TRUE also use the member type, otherwise use "any".
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200780 */
781 void
Bram Moolenaar08251752021-01-11 21:20:18 +0100782update_vim9_script_var(
783 int create,
784 dictitem_T *di,
785 int flags,
786 typval_T *tv,
Bram Moolenaarf2253962021-04-13 20:53:13 +0200787 type_T **type,
788 int do_member)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200789{
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100790 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
791 hashitem_T *hi;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200792 svar_T *sv = NULL;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200793
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100794 if (create)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200795 {
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200796 sallvar_T *newsav;
797 sallvar_T *sav = NULL;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200798
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100799 // Store a pointer to the typval_T, so that it can be found by index
800 // instead of using a hastab lookup.
801 if (ga_grow(&si->sn_var_vals, 1) == FAIL)
802 return;
803
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200804 hi = hash_find(&si->sn_all_vars.dv_hashtab, di->di_key);
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200805 if (!HASHITEM_EMPTY(hi))
806 {
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200807 // Variable with this name exists, either in this block or in
808 // another block.
809 for (sav = HI2SAV(hi); ; sav = sav->sav_next)
810 {
811 if (sav->sav_block_id == si->sn_current_block_id)
812 {
813 // variable defined in a loop, re-use the entry
814 sv = ((svar_T *)si->sn_var_vals.ga_data)
815 + sav->sav_var_vals_idx;
816 // unhide the variable
817 if (sv->sv_tv == &sav->sav_tv)
818 {
819 clear_tv(&sav->sav_tv);
820 sv->sv_tv = &di->di_tv;
821 sav->sav_di = di;
822 }
823 break;
824 }
825 if (sav->sav_next == NULL)
826 break;
827 }
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200828 }
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200829
830 if (sv == NULL)
831 {
832 // Variable not defined or not defined in current block: Add a
833 // svar_T and create a new sallvar_T.
834 sv = ((svar_T *)si->sn_var_vals.ga_data) + si->sn_var_vals.ga_len;
835 newsav = (sallvar_T *)alloc_clear(
836 sizeof(sallvar_T) + STRLEN(di->di_key));
837 if (newsav == NULL)
838 return;
839
840 sv->sv_tv = &di->di_tv;
841 sv->sv_const = (flags & ASSIGN_FINAL) ? ASSIGN_FINAL
842 : (flags & ASSIGN_CONST) ? ASSIGN_CONST : 0;
843 sv->sv_export = is_export;
844 newsav->sav_var_vals_idx = si->sn_var_vals.ga_len;
845 ++si->sn_var_vals.ga_len;
846 STRCPY(&newsav->sav_key, di->di_key);
847 sv->sv_name = newsav->sav_key;
848 newsav->sav_di = di;
849 newsav->sav_block_id = si->sn_current_block_id;
850
851 if (HASHITEM_EMPTY(hi))
852 // new variable name
853 hash_add(&si->sn_all_vars.dv_hashtab, newsav->sav_key);
854 else if (sav != NULL)
855 // existing name in a new block, append to the list
856 sav->sav_next = newsav;
857 }
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200858 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100859 else
860 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000861 sv = find_typval_in_script(&di->di_tv, 0);
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100862 }
863 if (sv != NULL)
864 {
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100865 if (*type == NULL)
Bram Moolenaar114dbda2022-01-03 12:28:03 +0000866 *type = typval2type(tv, get_copyID(), &si->sn_type_list,
867 do_member ? TVTT_DO_MEMBER : 0);
Bram Moolenaardd297bc2021-12-10 10:37:38 +0000868 if (sv->sv_type_allocated)
869 free_type(sv->sv_type);
870 if (*type != NULL && ((*type)->tt_type == VAR_FUNC
871 || (*type)->tt_type == VAR_PARTIAL))
872 {
873 // The type probably uses uf_type_list, which is cleared when the
874 // function is freed, but the script variable may keep the type.
875 // Make a copy to avoid using freed memory.
876 sv->sv_type = alloc_type(*type);
877 sv->sv_type_allocated = TRUE;
878 }
879 else
880 {
881 sv->sv_type = *type;
882 sv->sv_type_allocated = FALSE;
883 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100884 }
885
886 // let ex_export() know the export worked.
887 is_export = FALSE;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200888}
889
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200890/*
891 * Hide a script variable when leaving a block.
892 * "idx" is de index in sn_var_vals.
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200893 * When "func_defined" is non-zero then a function was defined in this block,
894 * the variable may be accessed by it. Otherwise the variable can be cleared.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200895 */
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200896 void
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200897hide_script_var(scriptitem_T *si, int idx, int func_defined)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200898{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200899 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200900 hashtab_T *script_ht = get_script_local_ht();
901 hashtab_T *all_ht = &si->sn_all_vars.dv_hashtab;
902 hashitem_T *script_hi;
903 hashitem_T *all_hi;
904
905 // Remove a variable declared inside the block, if it still exists.
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200906 // If it was added in a nested block it will already have been removed.
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200907 // The typval is moved into the sallvar_T.
908 script_hi = hash_find(script_ht, sv->sv_name);
909 all_hi = hash_find(all_ht, sv->sv_name);
910 if (!HASHITEM_EMPTY(script_hi) && !HASHITEM_EMPTY(all_hi))
911 {
912 dictitem_T *di = HI2DI(script_hi);
913 sallvar_T *sav = HI2SAV(all_hi);
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200914 sallvar_T *sav_prev = NULL;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200915
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200916 // There can be multiple entries with the same name in different
917 // blocks, find the right one.
918 while (sav != NULL && sav->sav_var_vals_idx != idx)
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200919 {
920 sav_prev = sav;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200921 sav = sav->sav_next;
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200922 }
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200923 if (sav != NULL)
924 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200925 if (func_defined)
926 {
927 // move the typval from the dictitem to the sallvar
928 sav->sav_tv = di->di_tv;
929 di->di_tv.v_type = VAR_UNKNOWN;
930 sav->sav_flags = di->di_flags;
931 sav->sav_di = NULL;
932 sv->sv_tv = &sav->sav_tv;
933 }
934 else
935 {
936 if (sav_prev == NULL)
937 hash_remove(all_ht, all_hi);
938 else
939 sav_prev->sav_next = sav->sav_next;
940 sv->sv_name = NULL;
941 vim_free(sav);
942 }
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200943 delete_var(script_ht, script_hi);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200944 }
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200945 }
946}
947
948/*
Bram Moolenaar10c65862020-10-08 21:16:42 +0200949 * Find the script-local variable that links to "dest".
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000950 * If "sid" is zero use the current script.
Bram Moolenaarc967d572021-07-08 21:38:50 +0200951 * Returns NULL if not found and give an internal error.
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200952 */
Bram Moolenaar10c65862020-10-08 21:16:42 +0200953 svar_T *
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000954find_typval_in_script(typval_T *dest, scid_T sid)
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200955{
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000956 scriptitem_T *si = SCRIPT_ITEM(sid == 0 ? current_sctx.sc_sid : sid);
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200957 int idx;
958
Bram Moolenaar81e17fb2020-08-21 21:55:43 +0200959 if (si->sn_version != SCRIPT_VERSION_VIM9)
960 // legacy script doesn't store variable types
Bram Moolenaar10c65862020-10-08 21:16:42 +0200961 return NULL;
Bram Moolenaar81e17fb2020-08-21 21:55:43 +0200962
Bram Moolenaara06758d2021-10-15 00:18:37 +0100963 // Find the svar_T in sn_var_vals. Start at the end, in a for loop the
964 // variable was added at the end.
965 for (idx = si->sn_var_vals.ga_len - 1; idx >= 0; --idx)
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200966 {
967 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
968
Bram Moolenaarc0913d02020-12-05 14:44:37 +0100969 // If "sv_name" is NULL the variable was hidden when leaving a block,
970 // don't check "sv_tv" then, it might be used for another variable now.
971 if (sv->sv_name != NULL && sv->sv_tv == dest)
Bram Moolenaar10c65862020-10-08 21:16:42 +0200972 return sv;
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200973 }
Bram Moolenaarc967d572021-07-08 21:38:50 +0200974 iemsg("find_typval_in_script(): not found");
Bram Moolenaar10c65862020-10-08 21:16:42 +0200975 return NULL;
976}
977
978/*
979 * Check if the type of script variable "dest" allows assigning "value".
980 * If needed convert "value" to a bool.
981 */
982 int
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100983check_script_var_type(
Bram Moolenaar7824fc82021-11-26 17:36:51 +0000984 svar_T *sv,
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100985 typval_T *value,
986 char_u *name,
987 where_T where)
Bram Moolenaar10c65862020-10-08 21:16:42 +0200988{
Bram Moolenaar10c65862020-10-08 21:16:42 +0200989 int ret;
990
Bram Moolenaar7824fc82021-11-26 17:36:51 +0000991 if (sv->sv_const != 0)
Bram Moolenaar10c65862020-10-08 21:16:42 +0200992 {
Bram Moolenaar7824fc82021-11-26 17:36:51 +0000993 semsg(_(e_cannot_change_readonly_variable_str), name);
994 return FAIL;
Bram Moolenaar10c65862020-10-08 21:16:42 +0200995 }
Bram Moolenaar7824fc82021-11-26 17:36:51 +0000996 ret = check_typval_type(sv->sv_type, value, where);
997 if (ret == OK && need_convert_to_bool(sv->sv_type, value))
998 {
999 int val = tv2bool(value);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001000
Bram Moolenaar7824fc82021-11-26 17:36:51 +00001001 clear_tv(value);
1002 value->v_type = VAR_BOOL;
1003 value->v_lock = 0;
1004 value->vval.v_number = val ? VVAL_TRUE : VVAL_FALSE;
1005 }
1006 return ret;
Bram Moolenaar34db91f2020-06-13 19:00:10 +02001007}
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001008
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001009// words that cannot be used as a variable
1010static char *reserved[] = {
1011 "true",
1012 "false",
1013 "null",
Bram Moolenaar74235772021-06-12 14:53:05 +02001014 "this",
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001015 NULL
1016};
1017
1018 int
1019check_reserved_name(char_u *name)
1020{
1021 int idx;
1022
1023 for (idx = 0; reserved[idx] != NULL; ++idx)
1024 if (STRCMP(reserved[idx], name) == 0)
1025 {
1026 semsg(_(e_cannot_use_reserved_name), name);
1027 return FAIL;
1028 }
1029 return OK;
1030}
1031
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001032#endif // FEAT_EVAL