blob: 2038c78a0835cf7ad42733dab80a5c47dae699be [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 Moolenaard9d2fd02022-01-13 21:15:21 +0000135 // Store the prefix with the script, it is used to find exported functions.
Bram Moolenaar71930f12022-01-13 13:47:43 +0000136 if (si->sn_autoload_prefix == NULL)
137 si->sn_autoload_prefix = get_autoload_prefix(si);
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000138
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100139 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
140 si->sn_version = SCRIPT_VERSION_VIM9;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100141
142 if (STRCMP(p_cpo, CPO_VIM) != 0)
143 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200144 si->sn_save_cpo = vim_strsave(p_cpo);
Bram Moolenaar37294bd2021-03-10 13:40:08 +0100145 set_option_value((char_u *)"cpo", 0L, (char_u *)CPO_VIM, OPT_NO_REDRAW);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100146 }
Bram Moolenaar9b8d6222020-12-28 18:26:00 +0100147#else
148 // No check for this being the first command, it doesn't matter.
149 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
150#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100151}
152
Dominique Pelle748b3082022-01-08 12:41:16 +0000153#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100154/*
Bram Moolenaarae616492020-07-28 20:07:27 +0200155 * When in Vim9 script give an error and return FAIL.
156 */
157 int
158not_in_vim9(exarg_T *eap)
159{
Bram Moolenaar68e30442020-07-28 21:15:07 +0200160 if (in_vim9script())
161 switch (eap->cmdidx)
162 {
Bram Moolenaarada1d872021-02-20 08:16:51 +0100163 case CMD_k:
164 if (eap->addr_count > 0)
165 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000166 emsg(_(e_no_range_allowed));
Bram Moolenaarada1d872021-02-20 08:16:51 +0100167 return FAIL;
168 }
169 // FALLTHROUGH
Bram Moolenaar68e30442020-07-28 21:15:07 +0200170 case CMD_append:
171 case CMD_change:
Bram Moolenaarf5a48012020-08-01 17:00:03 +0200172 case CMD_insert:
Bram Moolenaar65088802021-03-13 21:07:21 +0100173 case CMD_open:
Bram Moolenaarf5a48012020-08-01 17:00:03 +0200174 case CMD_t:
Bram Moolenaar68e30442020-07-28 21:15:07 +0200175 case CMD_xit:
Bram Moolenaar9b8d6222020-12-28 18:26:00 +0100176 semsg(_(e_command_not_supported_in_vim9_script_missing_var_str), eap->cmd);
Bram Moolenaar68e30442020-07-28 21:15:07 +0200177 return FAIL;
178 default: break;
179 }
Bram Moolenaarae616492020-07-28 20:07:27 +0200180 return OK;
181}
182
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100183/*
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100184 * Give an error message if "p" points at "#{" and return TRUE.
185 * This avoids that using a legacy style #{} dictionary leads to difficult to
186 * understand errors.
187 */
188 int
189vim9_bad_comment(char_u *p)
190{
Bram Moolenaara0399ef2021-03-20 15:00:01 +0100191 if (p[0] == '#' && p[1] == '{' && p[2] != '{')
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100192 {
193 emsg(_(e_cannot_use_hash_curly_to_start_comment));
194 return TRUE;
195 }
196 return FALSE;
197}
Dominique Pelle748b3082022-01-08 12:41:16 +0000198#endif
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100199
200/*
Bram Moolenaara0399ef2021-03-20 15:00:01 +0100201 * Return TRUE if "p" points at a "#" not followed by one '{'.
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100202 * Does not check for white space.
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100203 */
204 int
205vim9_comment_start(char_u *p)
206{
Bram Moolenaara0399ef2021-03-20 15:00:01 +0100207 return p[0] == '#' && (p[1] != '{' || p[2] == '{');
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100208}
209
Bram Moolenaar9b8d6222020-12-28 18:26:00 +0100210#if defined(FEAT_EVAL) || defined(PROTO)
211
Bram Moolenaarae616492020-07-28 20:07:27 +0200212/*
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200213 * "++nr" and "--nr" commands.
214 */
215 void
216ex_incdec(exarg_T *eap)
217{
218 char_u *cmd = eap->cmd;
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200219 char_u *nextcmd = eap->nextcmd;
220 size_t len = STRLEN(eap->cmd) + 8;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200221
Bram Moolenaar22480d12021-06-25 21:31:09 +0200222 if (VIM_ISWHITE(cmd[2]))
223 {
224 semsg(_(e_no_white_space_allowed_after_str_str),
225 eap->cmdidx == CMD_increment ? "++" : "--", eap->cmd);
226 return;
227 }
228
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200229 // This works like "nr += 1" or "nr -= 1".
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200230 // Add a '|' to avoid looking in the next line.
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200231 eap->cmd = alloc(len);
232 if (eap->cmd == NULL)
233 return;
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200234 vim_snprintf((char *)eap->cmd, len, "%s %c= 1 |", cmd + 2,
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200235 eap->cmdidx == CMD_increment ? '+' : '-');
236 eap->arg = eap->cmd;
237 eap->cmdidx = CMD_var;
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200238 eap->nextcmd = NULL;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200239 ex_let(eap);
240 vim_free(eap->cmd);
241 eap->cmd = cmd;
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200242 eap->nextcmd = nextcmd;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200243}
244
245/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100246 * ":export let Name: type"
247 * ":export const Name: type"
248 * ":export def Name(..."
249 * ":export class Name ..."
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100250 */
251 void
Bram Moolenaar09689a02020-05-09 22:50:08 +0200252ex_export(exarg_T *eap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100253{
Bram Moolenaar17d36cb2022-01-12 11:46:40 +0000254 int prev_did_emsg = did_emsg;
255
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200256 if (!in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100257 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200258 emsg(_(e_export_can_only_be_used_in_vim9script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100259 return;
260 }
261
262 eap->cmd = eap->arg;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +0100263 (void)find_ex_command(eap, NULL, lookup_scriptitem, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100264 switch (eap->cmdidx)
265 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200266 case CMD_var:
267 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100268 case CMD_const:
269 case CMD_def:
Bram Moolenaar160aa862022-01-10 21:29:57 +0000270 case CMD_function:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100271 // case CMD_class:
272 is_export = TRUE;
273 do_cmdline(eap->cmd, eap->getline, eap->cookie,
274 DOCMD_VERBOSE + DOCMD_NOWAIT);
275
276 // The command will reset "is_export" when exporting an item.
277 if (is_export)
278 {
Bram Moolenaar17d36cb2022-01-12 11:46:40 +0000279 if (did_emsg == prev_did_emsg)
280 emsg(_(e_export_with_invalid_argument));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100281 is_export = FALSE;
282 }
283 break;
284 default:
Bram Moolenaar17d36cb2022-01-12 11:46:40 +0000285 if (did_emsg == prev_did_emsg)
286 emsg(_(e_invalid_command_after_export));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100287 break;
288 }
289}
290
291/*
292 * Add a new imported item entry to the current script.
293 */
294 static imported_T *
295new_imported(garray_T *gap)
296{
297 if (ga_grow(gap, 1) == OK)
298 return ((imported_T *)gap->ga_data + gap->ga_len++);
299 return NULL;
300}
301
302/*
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200303 * Free the script variables from "sn_all_vars".
304 */
305 static void
306free_all_script_vars(scriptitem_T *si)
307{
308 int todo;
309 hashtab_T *ht = &si->sn_all_vars.dv_hashtab;
310 hashitem_T *hi;
311 sallvar_T *sav;
312 sallvar_T *sav_next;
Bram Moolenaardd297bc2021-12-10 10:37:38 +0000313 int idx;
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200314
315 hash_lock(ht);
316 todo = (int)ht->ht_used;
317 for (hi = ht->ht_array; todo > 0; ++hi)
318 {
319 if (!HASHITEM_EMPTY(hi))
320 {
321 --todo;
322
323 // Free the variable. Don't remove it from the hashtab, ht_array
324 // might change then. hash_clear() takes care of it later.
325 sav = HI2SAV(hi);
326 while (sav != NULL)
327 {
328 sav_next = sav->sav_next;
329 if (sav->sav_di == NULL)
330 clear_tv(&sav->sav_tv);
331 vim_free(sav);
332 sav = sav_next;
333 }
334 }
335 }
336 hash_clear(ht);
337 hash_init(ht);
338
Bram Moolenaardd297bc2021-12-10 10:37:38 +0000339 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
340 {
341 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
342
343 if (sv->sv_type_allocated)
344 free_type(sv->sv_type);
345 }
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200346 ga_clear(&si->sn_var_vals);
347
348 // existing commands using script variable indexes are no longer valid
349 si->sn_script_seq = current_sctx.sc_seq;
350}
351
352/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100353 * Free all imported items in script "sid".
354 */
355 void
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200356free_imports_and_script_vars(int sid)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100357{
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100358 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100359 int idx;
360
361 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
362 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100363 imported_T *imp = ((imported_T *)si->sn_imports.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100364
365 vim_free(imp->imp_name);
366 }
367 ga_clear(&si->sn_imports);
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200368
369 free_all_script_vars(si);
370
Bram Moolenaar6110e792020-07-08 19:35:21 +0200371 clear_type_list(&si->sn_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100372}
373
374/*
Bram Moolenaara6294952020-12-27 13:39:50 +0100375 * Mark all imports as possible to redefine. Used when a script is loaded
376 * again but not cleared.
377 */
378 void
379mark_imports_for_reload(int sid)
380{
381 scriptitem_T *si = SCRIPT_ITEM(sid);
382 int idx;
383
384 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
385 {
386 imported_T *imp = ((imported_T *)si->sn_imports.ga_data) + idx;
387
388 imp->imp_flags |= IMP_FLAGS_RELOAD;
389 }
390}
391
392/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100393 * Handle an ":import" command and add the resulting imported_T to "gap", when
394 * not NULL, or script "import_sid" sn_imports.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200395 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100396 * Returns a pointer to after the command or NULL in case of failure
397 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200398 static char_u *
Bram Moolenaar1c991142020-07-04 13:15:31 +0200399handle_import(
400 char_u *arg_start,
401 garray_T *gap,
402 int import_sid,
403 evalarg_T *evalarg,
404 void *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100405{
406 char_u *arg = arg_start;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000407 char_u *nextarg;
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000408 int is_autoload = FALSE;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000409 int getnext;
410 char_u *expr_end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100411 int ret = FAIL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000412 char_u *as_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100413 typval_T tv;
414 int sid = -1;
415 int res;
Bram Moolenaar921ba522021-07-29 22:25:05 +0200416 long start_lnum = SOURCING_LNUM;
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000417 garray_T *import_gap;
418 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100419
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000420 if (STRNCMP(arg, "autoload", 8) == 0 && VIM_ISWHITE(arg[8]))
421 {
422 is_autoload = TRUE;
423 arg = skipwhite(arg + 8);
424 }
425
Bram Moolenaar4db572e2021-07-18 18:21:38 +0200426 // The name of the file can be an expression, which must evaluate to a
427 // string.
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000428 ret = eval0_retarg(arg, &tv, NULL, evalarg, &expr_end);
Bram Moolenaar4db572e2021-07-18 18:21:38 +0200429 if (ret == FAIL)
430 goto erret;
431 if (tv.v_type != VAR_STRING
432 || tv.vval.v_string == NULL || *tv.vval.v_string == NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100433 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000434 semsg(_(e_invalid_string_for_import_str), arg);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200435 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100436 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100437
Bram Moolenaar921ba522021-07-29 22:25:05 +0200438 // Give error messages for the start of the line.
439 SOURCING_LNUM = start_lnum;
440
Bram Moolenaar1c991142020-07-04 13:15:31 +0200441 /*
442 * find script file
443 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100444 if (*tv.vval.v_string == '.')
445 {
446 size_t len;
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100447 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100448 char_u *tail = gettail(si->sn_name);
449 char_u *from_name;
450
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000451 if (is_autoload)
452 res = FAIL;
453 else
454 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100455
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000456 // Relative to current script: "./name.vim", "../../name.vim".
457 len = STRLEN(si->sn_name) - STRLEN(tail)
458 + STRLEN(tv.vval.v_string) + 2;
459 from_name = alloc((int)len);
460 if (from_name == NULL)
461 goto erret;
462 vim_strncpy(from_name, si->sn_name, tail - si->sn_name);
463 add_pathsep(from_name);
464 STRCAT(from_name, tv.vval.v_string);
465 simplify_filename(from_name);
466
467 res = do_source(from_name, FALSE, DOSO_NONE, &sid);
468 vim_free(from_name);
469 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100470 }
471 else if (mch_isFullName(tv.vval.v_string))
472 {
473 // Absolute path: "/tmp/name.vim"
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000474 if (is_autoload)
475 res = FAIL;
476 else
477 res = do_source(tv.vval.v_string, FALSE, DOSO_NONE, &sid);
478 }
479 else if (is_autoload)
480 {
481 size_t len = 9 + STRLEN(tv.vval.v_string) + 1;
482 char_u *from_name;
483
484 // Find file in "autoload" subdirs in 'runtimepath'.
485 from_name = alloc((int)len);
486 if (from_name == NULL)
487 goto erret;
488 vim_snprintf((char *)from_name, len, "autoload/%s", tv.vval.v_string);
489 // we need a scriptitem without loading the script
490 sid = find_script_in_rtp(from_name);
491 vim_free(from_name);
Bram Moolenaard041f422022-01-12 19:54:00 +0000492 if (SCRIPT_ID_VALID(sid))
493 {
494 scriptitem_T *si = SCRIPT_ITEM(sid);
495
496 if (si->sn_autoload_prefix == NULL)
497 si->sn_autoload_prefix = get_autoload_prefix(si);
498 res = OK;
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +0000499 if (override_autoload && si->sn_state == SN_STATE_NOT_LOADED)
500 // testing override: load autoload script right away
501 (void)do_source(si->sn_name, FALSE, DOSO_NONE, NULL);
Bram Moolenaard041f422022-01-12 19:54:00 +0000502 }
503 else
504 res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100505 }
506 else
507 {
508 size_t len = 7 + STRLEN(tv.vval.v_string) + 1;
509 char_u *from_name;
510
511 // Find file in "import" subdirs in 'runtimepath'.
512 from_name = alloc((int)len);
513 if (from_name == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200514 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100515 vim_snprintf((char *)from_name, len, "import/%s", tv.vval.v_string);
516 res = source_in_path(p_rtp, from_name, DIP_NOAFTER, &sid);
517 vim_free(from_name);
518 }
519
520 if (res == FAIL || sid <= 0)
521 {
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000522 semsg(_(is_autoload && sid <= 0
523 ? e_autoload_import_cannot_use_absolute_or_relative_path
524 : e_could_not_import_str), tv.vval.v_string);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200525 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100526 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100527
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000528 import_gap = gap != NULL ? gap : &SCRIPT_ITEM(import_sid)->sn_imports;
529 for (i = 0; i < import_gap->ga_len; ++i)
530 {
531 imported_T *import = (imported_T *)import_gap->ga_data + i;
532
533 if (import->imp_sid == sid)
534 {
535 if (import->imp_flags & IMP_FLAGS_RELOAD)
536 {
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000537 // encountering same script first time on a reload is OK
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000538 import->imp_flags &= ~IMP_FLAGS_RELOAD;
539 break;
540 }
541 semsg(_(e_cannot_import_same_script_twice_str), tv.vval.v_string);
542 goto erret;
543 }
544 }
545
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000546 // Allow for the "as Name" to be in the next line.
547 nextarg = eval_next_non_blank(expr_end, evalarg, &getnext);
548 if (STRNCMP("as", nextarg, 2) == 0 && IS_WHITE_OR_NUL(nextarg[2]))
549 {
550 char_u *p;
551
552 if (getnext)
553 arg = eval_next_line(evalarg);
554 else
555 arg = nextarg;
556
557 // Skip over "as Name "; no line break allowed after "as".
558 // Do not allow for ':' and '#'.
559 arg = skipwhite(arg + 2);
560 p = arg;
561 if (eval_isnamec1(*arg))
562 while (ASCII_ISALNUM(*arg) || *arg == '_')
563 ++arg;
564 if (p == arg || !IS_WHITE_OR_NUL(*arg))
565 {
566 semsg(_(e_syntax_error_in_import_str), p);
567 goto erret;
568 }
569 as_name = vim_strnsave(p, arg - p);
570 arg = skipwhite(arg);
571 }
572 else
573 {
574 char_u *p = gettail(tv.vval.v_string);
575 char_u *end = (char_u *)strstr((char *)p, ".vim");
576
577 if (!ends_excmd2(arg_start, expr_end))
578 {
579 semsg(_(e_trailing_characters_str), expr_end);
580 goto erret;
581 }
Bram Moolenaar834d4182022-01-07 13:38:24 +0000582 if (end == NULL || end[4] != NUL)
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000583 {
Bram Moolenaar834d4182022-01-07 13:38:24 +0000584 semsg(_(e_imported_script_must_use_as_or_end_in_dot_vim_str), p);
585 goto erret;
586 }
587 if (end == p)
588 {
589 semsg(_(e_cannot_import_dot_vim_without_using_as), p);
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000590 goto erret;
591 }
592 as_name = vim_strnsave(p, end - p);
593 }
594
595 if (as_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100596 {
Bram Moolenaar0a842842021-02-27 22:41:19 +0100597 imported_T *imported;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100598
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000599 imported = find_imported(as_name, FALSE, STRLEN(as_name), cctx);
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000600 if (imported != NULL && imported->imp_sid != sid)
Bram Moolenaara6294952020-12-27 13:39:50 +0100601 {
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000602 semsg(_(e_name_already_defined_str), as_name);
603 goto erret;
Bram Moolenaara6294952020-12-27 13:39:50 +0100604 }
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000605 else if (imported == NULL
606 && check_defined(as_name, STRLEN(as_name), cctx, FALSE) == FAIL)
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000607 goto erret;
Bram Moolenaara6294952020-12-27 13:39:50 +0100608
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100609 if (imported == NULL)
Bram Moolenaar17d36cb2022-01-12 11:46:40 +0000610 {
611 imported = new_imported(import_gap);
612 if (imported == NULL)
613 goto erret;
614 imported->imp_name = as_name;
615 as_name = NULL;
616 imported->imp_sid = sid;
617 if (is_autoload)
618 imported->imp_flags = IMP_FLAGS_AUTOLOAD;
619 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100620 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200621
Bram Moolenaar1c991142020-07-04 13:15:31 +0200622erret:
Bram Moolenaar4db572e2021-07-18 18:21:38 +0200623 clear_tv(&tv);
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000624 vim_free(as_name);
625 return arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100626}
627
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200628/*
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000629 * ":import 'filename'"
630 * ":import 'filename' as Name"
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200631 */
632 void
633ex_import(exarg_T *eap)
634{
635 char_u *cmd_end;
636 evalarg_T evalarg;
637
638 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
639 {
640 emsg(_(e_import_can_only_be_used_in_script));
641 return;
642 }
643 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
644
645 cmd_end = handle_import(eap->arg, NULL, current_sctx.sc_sid,
646 &evalarg, NULL);
647 if (cmd_end != NULL)
648 set_nextcmd(eap, cmd_end);
649 clear_evalarg(&evalarg, eap);
650}
651
652/*
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000653 * Find an exported item in "sid" matching "name".
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200654 * When it is a variable return the index.
655 * When it is a user function return "*ufunc".
656 * When not found returns -1 and "*ufunc" is NULL.
657 */
658 int
659find_exported(
660 int sid,
661 char_u *name,
662 ufunc_T **ufunc,
663 type_T **type,
664 cctx_T *cctx,
665 int verbose)
666{
667 int idx = -1;
668 svar_T *sv;
669 scriptitem_T *script = SCRIPT_ITEM(sid);
670
671 // Find name in "script".
672 idx = get_script_item_idx(sid, name, 0, cctx);
673 if (idx >= 0)
674 {
675 sv = ((svar_T *)script->sn_var_vals.ga_data) + idx;
Bram Moolenaara909c482022-01-06 22:07:57 +0000676 *ufunc = NULL;
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200677 if (!sv->sv_export)
678 {
679 if (verbose)
680 semsg(_(e_item_not_exported_in_script_str), name);
681 return -1;
682 }
683 *type = sv->sv_type;
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200684 }
685 else
686 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +0000687 size_t len = STRLEN(name);
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200688 char_u buffer[200];
689 char_u *funcname;
690
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +0000691 // It could be a user function. Normally this is stored as
692 // "<SNR>99_name". For an autoload script a function is stored with
693 // the autoload prefix: "dir#script#name".
694 if (script->sn_autoload_prefix != NULL)
695 len += STRLEN(script->sn_autoload_prefix) + 2;
696 else
697 len += 15;
698
699 if (len < sizeof(buffer))
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200700 funcname = buffer;
701 else
702 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +0000703 funcname = alloc(len);
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200704 if (funcname == NULL)
705 return -1;
706 }
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +0000707 if (script->sn_autoload_prefix != NULL)
708 {
709 sprintf((char *)funcname, "%s%s", script->sn_autoload_prefix, name);
710 }
711 else
712 {
713 funcname[0] = K_SPECIAL;
714 funcname[1] = KS_EXTRA;
715 funcname[2] = (int)KE_SNR;
716 sprintf((char *)funcname + 3, "%ld_%s", (long)sid, name);
717 }
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000718 *ufunc = find_func(funcname, FALSE);
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200719 if (funcname != buffer)
720 vim_free(funcname);
721
722 if (*ufunc == NULL)
723 {
724 if (verbose)
725 semsg(_(e_item_not_found_in_script_str), name);
726 return -1;
727 }
728 else if (((*ufunc)->uf_flags & FC_EXPORT) == 0)
729 {
730 if (verbose)
731 semsg(_(e_item_not_exported_in_script_str), name);
732 *ufunc = NULL;
733 return -1;
734 }
735 }
736
737 return idx;
738}
739
740/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200741 * Declare a script-local variable without init: "let var: type".
742 * "const" is an error since the value is missing.
743 * Returns a pointer to after the type.
744 */
745 char_u *
746vim9_declare_scriptvar(exarg_T *eap, char_u *arg)
747{
748 char_u *p;
749 char_u *name;
750 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
751 type_T *type;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200752 typval_T init_tv;
753
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200754 if (eap->cmdidx == CMD_final || eap->cmdidx == CMD_const)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200755 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200756 if (eap->cmdidx == CMD_final)
757 emsg(_(e_final_requires_a_value));
758 else
759 emsg(_(e_const_requires_a_value));
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200760 return arg + STRLEN(arg);
761 }
762
763 // Check for valid starting character.
764 if (!eval_isnamec1(*arg))
765 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000766 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200767 return arg + STRLEN(arg);
768 }
769
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200770 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
Bram Moolenaar3b74b6b2020-06-19 19:01:43 +0200771 if (*p == ':' && (VIM_ISWHITE(p[1]) || p != arg + 1))
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200772 break;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200773
774 if (*p != ':')
775 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +0200776 emsg(_(e_type_or_initialization_required));
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200777 return arg + STRLEN(arg);
778 }
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200779 if (!VIM_ISWHITE(p[1]))
780 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100781 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200782 return arg + STRLEN(arg);
783 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200784 name = vim_strnsave(arg, p - arg);
785
Bram Moolenaard0edaf92021-05-28 21:06:08 +0200786 // parse type, check for reserved name
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200787 p = skipwhite(p + 1);
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100788 type = parse_type(&p, &si->sn_type_list, TRUE);
Bram Moolenaard0edaf92021-05-28 21:06:08 +0200789 if (type == NULL || check_reserved_name(name) == FAIL)
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200790 {
791 vim_free(name);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200792 return p;
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200793 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200794
795 // Create the variable with 0/NULL value.
796 CLEAR_FIELD(init_tv);
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +0200797 if (type->tt_type == VAR_ANY)
798 // A variable of type "any" is not possible, just use zero instead
799 init_tv.v_type = VAR_NUMBER;
800 else
801 init_tv.v_type = type->tt_type;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000802 set_var_const(name, 0, type, &init_tv, FALSE, 0, 0);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200803
804 vim_free(name);
805 return p;
806}
807
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200808/*
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200809 * Vim9 part of adding a script variable: add it to sn_all_vars (lookup by name
810 * with a hashtable) and sn_var_vals (lookup by index).
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100811 * When "create" is TRUE this is a new variable, otherwise find and update an
812 * existing variable.
Bram Moolenaar08251752021-01-11 21:20:18 +0100813 * "flags" can have ASSIGN_FINAL or ASSIGN_CONST.
Bram Moolenaarf2253962021-04-13 20:53:13 +0200814 * When "*type" is NULL use "tv" for the type and update "*type". If
815 * "do_member" is TRUE also use the member type, otherwise use "any".
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200816 */
817 void
Bram Moolenaar08251752021-01-11 21:20:18 +0100818update_vim9_script_var(
819 int create,
820 dictitem_T *di,
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +0000821 char_u *name,
Bram Moolenaar08251752021-01-11 21:20:18 +0100822 int flags,
823 typval_T *tv,
Bram Moolenaarf2253962021-04-13 20:53:13 +0200824 type_T **type,
825 int do_member)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200826{
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100827 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
828 hashitem_T *hi;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200829 svar_T *sv = NULL;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200830
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100831 if (create)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200832 {
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200833 sallvar_T *newsav;
834 sallvar_T *sav = NULL;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200835
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100836 // Store a pointer to the typval_T, so that it can be found by index
837 // instead of using a hastab lookup.
838 if (ga_grow(&si->sn_var_vals, 1) == FAIL)
839 return;
840
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +0000841 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200842 if (!HASHITEM_EMPTY(hi))
843 {
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200844 // Variable with this name exists, either in this block or in
845 // another block.
846 for (sav = HI2SAV(hi); ; sav = sav->sav_next)
847 {
848 if (sav->sav_block_id == si->sn_current_block_id)
849 {
850 // variable defined in a loop, re-use the entry
851 sv = ((svar_T *)si->sn_var_vals.ga_data)
852 + sav->sav_var_vals_idx;
853 // unhide the variable
854 if (sv->sv_tv == &sav->sav_tv)
855 {
856 clear_tv(&sav->sav_tv);
857 sv->sv_tv = &di->di_tv;
858 sav->sav_di = di;
859 }
860 break;
861 }
862 if (sav->sav_next == NULL)
863 break;
864 }
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200865 }
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200866
867 if (sv == NULL)
868 {
869 // Variable not defined or not defined in current block: Add a
870 // svar_T and create a new sallvar_T.
871 sv = ((svar_T *)si->sn_var_vals.ga_data) + si->sn_var_vals.ga_len;
872 newsav = (sallvar_T *)alloc_clear(
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +0000873 sizeof(sallvar_T) + STRLEN(name));
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200874 if (newsav == NULL)
875 return;
876
877 sv->sv_tv = &di->di_tv;
878 sv->sv_const = (flags & ASSIGN_FINAL) ? ASSIGN_FINAL
879 : (flags & ASSIGN_CONST) ? ASSIGN_CONST : 0;
880 sv->sv_export = is_export;
881 newsav->sav_var_vals_idx = si->sn_var_vals.ga_len;
882 ++si->sn_var_vals.ga_len;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +0000883 STRCPY(&newsav->sav_key, name);
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200884 sv->sv_name = newsav->sav_key;
885 newsav->sav_di = di;
886 newsav->sav_block_id = si->sn_current_block_id;
887
888 if (HASHITEM_EMPTY(hi))
889 // new variable name
890 hash_add(&si->sn_all_vars.dv_hashtab, newsav->sav_key);
891 else if (sav != NULL)
892 // existing name in a new block, append to the list
893 sav->sav_next = newsav;
894 }
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200895 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100896 else
897 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000898 sv = find_typval_in_script(&di->di_tv, 0);
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100899 }
900 if (sv != NULL)
901 {
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100902 if (*type == NULL)
Bram Moolenaar114dbda2022-01-03 12:28:03 +0000903 *type = typval2type(tv, get_copyID(), &si->sn_type_list,
904 do_member ? TVTT_DO_MEMBER : 0);
Bram Moolenaardd297bc2021-12-10 10:37:38 +0000905 if (sv->sv_type_allocated)
906 free_type(sv->sv_type);
907 if (*type != NULL && ((*type)->tt_type == VAR_FUNC
908 || (*type)->tt_type == VAR_PARTIAL))
909 {
910 // The type probably uses uf_type_list, which is cleared when the
911 // function is freed, but the script variable may keep the type.
912 // Make a copy to avoid using freed memory.
913 sv->sv_type = alloc_type(*type);
914 sv->sv_type_allocated = TRUE;
915 }
916 else
917 {
918 sv->sv_type = *type;
919 sv->sv_type_allocated = FALSE;
920 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100921 }
922
923 // let ex_export() know the export worked.
924 is_export = FALSE;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200925}
926
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200927/*
928 * Hide a script variable when leaving a block.
929 * "idx" is de index in sn_var_vals.
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200930 * When "func_defined" is non-zero then a function was defined in this block,
931 * the variable may be accessed by it. Otherwise the variable can be cleared.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200932 */
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200933 void
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200934hide_script_var(scriptitem_T *si, int idx, int func_defined)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200935{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200936 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200937 hashtab_T *script_ht = get_script_local_ht();
938 hashtab_T *all_ht = &si->sn_all_vars.dv_hashtab;
939 hashitem_T *script_hi;
940 hashitem_T *all_hi;
941
942 // Remove a variable declared inside the block, if it still exists.
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200943 // If it was added in a nested block it will already have been removed.
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200944 // The typval is moved into the sallvar_T.
945 script_hi = hash_find(script_ht, sv->sv_name);
946 all_hi = hash_find(all_ht, sv->sv_name);
947 if (!HASHITEM_EMPTY(script_hi) && !HASHITEM_EMPTY(all_hi))
948 {
949 dictitem_T *di = HI2DI(script_hi);
950 sallvar_T *sav = HI2SAV(all_hi);
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200951 sallvar_T *sav_prev = NULL;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200952
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200953 // There can be multiple entries with the same name in different
954 // blocks, find the right one.
955 while (sav != NULL && sav->sav_var_vals_idx != idx)
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200956 {
957 sav_prev = sav;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200958 sav = sav->sav_next;
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200959 }
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200960 if (sav != NULL)
961 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200962 if (func_defined)
963 {
964 // move the typval from the dictitem to the sallvar
965 sav->sav_tv = di->di_tv;
966 di->di_tv.v_type = VAR_UNKNOWN;
967 sav->sav_flags = di->di_flags;
968 sav->sav_di = NULL;
969 sv->sv_tv = &sav->sav_tv;
970 }
971 else
972 {
973 if (sav_prev == NULL)
974 hash_remove(all_ht, all_hi);
975 else
976 sav_prev->sav_next = sav->sav_next;
977 sv->sv_name = NULL;
978 vim_free(sav);
979 }
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200980 delete_var(script_ht, script_hi);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200981 }
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200982 }
983}
984
985/*
Bram Moolenaar10c65862020-10-08 21:16:42 +0200986 * Find the script-local variable that links to "dest".
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000987 * If "sid" is zero use the current script.
Bram Moolenaarc967d572021-07-08 21:38:50 +0200988 * Returns NULL if not found and give an internal error.
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200989 */
Bram Moolenaar10c65862020-10-08 21:16:42 +0200990 svar_T *
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000991find_typval_in_script(typval_T *dest, scid_T sid)
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200992{
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000993 scriptitem_T *si = SCRIPT_ITEM(sid == 0 ? current_sctx.sc_sid : sid);
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200994 int idx;
995
Bram Moolenaar81e17fb2020-08-21 21:55:43 +0200996 if (si->sn_version != SCRIPT_VERSION_VIM9)
997 // legacy script doesn't store variable types
Bram Moolenaar10c65862020-10-08 21:16:42 +0200998 return NULL;
Bram Moolenaar81e17fb2020-08-21 21:55:43 +0200999
Bram Moolenaara06758d2021-10-15 00:18:37 +01001000 // Find the svar_T in sn_var_vals. Start at the end, in a for loop the
1001 // variable was added at the end.
1002 for (idx = si->sn_var_vals.ga_len - 1; idx >= 0; --idx)
Bram Moolenaar34db91f2020-06-13 19:00:10 +02001003 {
1004 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1005
Bram Moolenaarc0913d02020-12-05 14:44:37 +01001006 // If "sv_name" is NULL the variable was hidden when leaving a block,
1007 // don't check "sv_tv" then, it might be used for another variable now.
1008 if (sv->sv_name != NULL && sv->sv_tv == dest)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001009 return sv;
Bram Moolenaar34db91f2020-06-13 19:00:10 +02001010 }
Bram Moolenaarc967d572021-07-08 21:38:50 +02001011 iemsg("find_typval_in_script(): not found");
Bram Moolenaar10c65862020-10-08 21:16:42 +02001012 return NULL;
1013}
1014
1015/*
1016 * Check if the type of script variable "dest" allows assigning "value".
1017 * If needed convert "value" to a bool.
1018 */
1019 int
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001020check_script_var_type(
Bram Moolenaar7824fc82021-11-26 17:36:51 +00001021 svar_T *sv,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001022 typval_T *value,
1023 char_u *name,
1024 where_T where)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001025{
Bram Moolenaar10c65862020-10-08 21:16:42 +02001026 int ret;
1027
Bram Moolenaar7824fc82021-11-26 17:36:51 +00001028 if (sv->sv_const != 0)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001029 {
Bram Moolenaar7824fc82021-11-26 17:36:51 +00001030 semsg(_(e_cannot_change_readonly_variable_str), name);
1031 return FAIL;
Bram Moolenaar10c65862020-10-08 21:16:42 +02001032 }
Bram Moolenaar7824fc82021-11-26 17:36:51 +00001033 ret = check_typval_type(sv->sv_type, value, where);
1034 if (ret == OK && need_convert_to_bool(sv->sv_type, value))
1035 {
1036 int val = tv2bool(value);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001037
Bram Moolenaar7824fc82021-11-26 17:36:51 +00001038 clear_tv(value);
1039 value->v_type = VAR_BOOL;
1040 value->v_lock = 0;
1041 value->vval.v_number = val ? VVAL_TRUE : VVAL_FALSE;
1042 }
1043 return ret;
Bram Moolenaar34db91f2020-06-13 19:00:10 +02001044}
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001045
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001046// words that cannot be used as a variable
1047static char *reserved[] = {
1048 "true",
1049 "false",
1050 "null",
Bram Moolenaar74235772021-06-12 14:53:05 +02001051 "this",
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001052 NULL
1053};
1054
1055 int
1056check_reserved_name(char_u *name)
1057{
1058 int idx;
1059
1060 for (idx = 0; reserved[idx] != NULL; ++idx)
1061 if (STRCMP(reserved[idx], name) == 0)
1062 {
1063 semsg(_(e_cannot_use_reserved_name), name);
1064 return FAIL;
1065 }
1066 return OK;
1067}
1068
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001069#endif // FEAT_EVAL