blob: 9696dfe8125a562ae059628b7e0b44977e770ecf [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 Moolenaar9b8d6222020-12-28 18:26:00 +010016#if defined(FEAT_EVAL)
17# include "vim9.h"
18#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010019
Bram Moolenaare535db82021-03-31 21:07:24 +020020/*
21 * Return TRUE when currently using Vim9 script syntax.
22 * Does not go up the stack, a ":function" inside vim9script uses legacy
23 * syntax.
24 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010025 int
26in_vim9script(void)
27{
Bram Moolenaare535db82021-03-31 21:07:24 +020028 // "sc_version" is also set when compiling a ":def" function in legacy
29 // script.
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +020030 return (current_sctx.sc_version == SCRIPT_VERSION_VIM9
31 || (cmdmod.cmod_flags & CMOD_VIM9CMD))
32 && !(cmdmod.cmod_flags & CMOD_LEGACY);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010033}
34
Bram Moolenaarb91d3f82021-04-01 13:17:50 +020035#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010036/*
Bram Moolenaardd9de502021-08-15 13:49:42 +020037 * Return TRUE when currently in a script with script version smaller than
38 * "max_version" or command modifiers forced it.
39 */
40 int
41in_old_script(int max_version)
42{
Bram Moolenaar5cebca22021-08-15 14:39:13 +020043 return (current_sctx.sc_version < max_version
44 && !(cmdmod.cmod_flags & CMOD_VIM9CMD))
Bram Moolenaardd9de502021-08-15 13:49:42 +020045 || (cmdmod.cmod_flags & CMOD_LEGACY);
46}
47
48/*
Bram Moolenaare535db82021-03-31 21:07:24 +020049 * Return TRUE if the current script is Vim9 script.
50 * This also returns TRUE in a legacy function in a Vim9 script.
51 */
52 int
53current_script_is_vim9(void)
54{
55 return SCRIPT_ID_VALID(current_sctx.sc_sid)
56 && SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
57 == SCRIPT_VERSION_VIM9;
58}
Bram Moolenaarb91d3f82021-04-01 13:17:50 +020059#endif
Bram Moolenaare535db82021-03-31 21:07:24 +020060
61/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010062 * ":vim9script".
63 */
64 void
Bram Moolenaar9b8d6222020-12-28 18:26:00 +010065ex_vim9script(exarg_T *eap UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010066{
Bram Moolenaar9b8d6222020-12-28 18:26:00 +010067#ifdef FEAT_EVAL
Bram Moolenaar2b327002020-12-26 15:39:31 +010068 int sid = current_sctx.sc_sid;
Bram Moolenaar101f4812020-06-16 23:18:51 +020069 scriptitem_T *si;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010070
71 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
72 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +020073 emsg(_(e_vim9script_can_only_be_used_in_script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010074 return;
75 }
Bram Moolenaar2b327002020-12-26 15:39:31 +010076
77 si = SCRIPT_ITEM(sid);
78 if (si->sn_state == SN_STATE_HAD_COMMAND)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010079 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +020080 emsg(_(e_vim9script_must_be_first_command_in_script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010081 return;
82 }
Bram Moolenaar2b327002020-12-26 15:39:31 +010083 if (!IS_WHITE_OR_NUL(*eap->arg) && STRCMP(eap->arg, "noclear") != 0)
84 {
85 semsg(_(e_invarg2), eap->arg);
86 return;
87 }
88 if (si->sn_state == SN_STATE_RELOAD && IS_WHITE_OR_NUL(*eap->arg))
89 {
90 hashtab_T *ht = &SCRIPT_VARS(sid);
91
92 // Reloading a script without the "noclear" argument: clear
93 // script-local variables and functions.
94 hashtab_free_contents(ht);
95 hash_init(ht);
96 delete_script_functions(sid);
97
98 // old imports and script variables are no longer valid
99 free_imports_and_script_vars(sid);
100 }
101 si->sn_state = SN_STATE_HAD_COMMAND;
102
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100103 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
104 si->sn_version = SCRIPT_VERSION_VIM9;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100105
106 if (STRCMP(p_cpo, CPO_VIM) != 0)
107 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200108 si->sn_save_cpo = vim_strsave(p_cpo);
Bram Moolenaar37294bd2021-03-10 13:40:08 +0100109 set_option_value((char_u *)"cpo", 0L, (char_u *)CPO_VIM, OPT_NO_REDRAW);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100110 }
Bram Moolenaar9b8d6222020-12-28 18:26:00 +0100111#else
112 // No check for this being the first command, it doesn't matter.
113 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
114#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100115}
116
117/*
Bram Moolenaarae616492020-07-28 20:07:27 +0200118 * When in Vim9 script give an error and return FAIL.
119 */
120 int
121not_in_vim9(exarg_T *eap)
122{
Bram Moolenaar68e30442020-07-28 21:15:07 +0200123 if (in_vim9script())
124 switch (eap->cmdidx)
125 {
Bram Moolenaarada1d872021-02-20 08:16:51 +0100126 case CMD_k:
127 if (eap->addr_count > 0)
128 {
129 emsg(_(e_norange));
130 return FAIL;
131 }
132 // FALLTHROUGH
Bram Moolenaar68e30442020-07-28 21:15:07 +0200133 case CMD_append:
134 case CMD_change:
Bram Moolenaarf5a48012020-08-01 17:00:03 +0200135 case CMD_insert:
Bram Moolenaar65088802021-03-13 21:07:21 +0100136 case CMD_open:
Bram Moolenaarf5a48012020-08-01 17:00:03 +0200137 case CMD_t:
Bram Moolenaar68e30442020-07-28 21:15:07 +0200138 case CMD_xit:
Bram Moolenaar9b8d6222020-12-28 18:26:00 +0100139 semsg(_(e_command_not_supported_in_vim9_script_missing_var_str), eap->cmd);
Bram Moolenaar68e30442020-07-28 21:15:07 +0200140 return FAIL;
141 default: break;
142 }
Bram Moolenaarae616492020-07-28 20:07:27 +0200143 return OK;
144}
145
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100146/*
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100147 * Give an error message if "p" points at "#{" and return TRUE.
148 * This avoids that using a legacy style #{} dictionary leads to difficult to
149 * understand errors.
150 */
151 int
152vim9_bad_comment(char_u *p)
153{
Bram Moolenaara0399ef2021-03-20 15:00:01 +0100154 if (p[0] == '#' && p[1] == '{' && p[2] != '{')
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100155 {
156 emsg(_(e_cannot_use_hash_curly_to_start_comment));
157 return TRUE;
158 }
159 return FALSE;
160}
161
162/*
Bram Moolenaara0399ef2021-03-20 15:00:01 +0100163 * Return TRUE if "p" points at a "#" not followed by one '{'.
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100164 * Does not check for white space.
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100165 */
166 int
167vim9_comment_start(char_u *p)
168{
Bram Moolenaara0399ef2021-03-20 15:00:01 +0100169 return p[0] == '#' && (p[1] != '{' || p[2] == '{');
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100170}
171
Bram Moolenaar9b8d6222020-12-28 18:26:00 +0100172#if defined(FEAT_EVAL) || defined(PROTO)
173
Bram Moolenaarae616492020-07-28 20:07:27 +0200174/*
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200175 * "++nr" and "--nr" commands.
176 */
177 void
178ex_incdec(exarg_T *eap)
179{
180 char_u *cmd = eap->cmd;
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200181 char_u *nextcmd = eap->nextcmd;
182 size_t len = STRLEN(eap->cmd) + 8;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200183
Bram Moolenaar22480d12021-06-25 21:31:09 +0200184 if (VIM_ISWHITE(cmd[2]))
185 {
186 semsg(_(e_no_white_space_allowed_after_str_str),
187 eap->cmdidx == CMD_increment ? "++" : "--", eap->cmd);
188 return;
189 }
190
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200191 // This works like "nr += 1" or "nr -= 1".
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200192 // Add a '|' to avoid looking in the next line.
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200193 eap->cmd = alloc(len);
194 if (eap->cmd == NULL)
195 return;
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200196 vim_snprintf((char *)eap->cmd, len, "%s %c= 1 |", cmd + 2,
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200197 eap->cmdidx == CMD_increment ? '+' : '-');
198 eap->arg = eap->cmd;
199 eap->cmdidx = CMD_var;
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200200 eap->nextcmd = NULL;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200201 ex_let(eap);
202 vim_free(eap->cmd);
203 eap->cmd = cmd;
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200204 eap->nextcmd = nextcmd;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200205}
206
207/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100208 * ":export let Name: type"
209 * ":export const Name: type"
210 * ":export def Name(..."
211 * ":export class Name ..."
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100212 */
213 void
Bram Moolenaar09689a02020-05-09 22:50:08 +0200214ex_export(exarg_T *eap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100215{
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200216 if (!in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100217 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200218 emsg(_(e_export_can_only_be_used_in_vim9script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100219 return;
220 }
221
222 eap->cmd = eap->arg;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +0100223 (void)find_ex_command(eap, NULL, lookup_scriptitem, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100224 switch (eap->cmdidx)
225 {
226 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200227 case CMD_var:
228 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100229 case CMD_const:
230 case CMD_def:
231 // case CMD_class:
232 is_export = TRUE;
233 do_cmdline(eap->cmd, eap->getline, eap->cookie,
234 DOCMD_VERBOSE + DOCMD_NOWAIT);
235
236 // The command will reset "is_export" when exporting an item.
237 if (is_export)
238 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200239 emsg(_(e_export_with_invalid_argument));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100240 is_export = FALSE;
241 }
242 break;
243 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200244 emsg(_(e_invalid_command_after_export));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100245 break;
246 }
247}
248
249/*
250 * Add a new imported item entry to the current script.
251 */
252 static imported_T *
253new_imported(garray_T *gap)
254{
255 if (ga_grow(gap, 1) == OK)
256 return ((imported_T *)gap->ga_data + gap->ga_len++);
257 return NULL;
258}
259
260/*
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200261 * Free the script variables from "sn_all_vars".
262 */
263 static void
264free_all_script_vars(scriptitem_T *si)
265{
266 int todo;
267 hashtab_T *ht = &si->sn_all_vars.dv_hashtab;
268 hashitem_T *hi;
269 sallvar_T *sav;
270 sallvar_T *sav_next;
271
272 hash_lock(ht);
273 todo = (int)ht->ht_used;
274 for (hi = ht->ht_array; todo > 0; ++hi)
275 {
276 if (!HASHITEM_EMPTY(hi))
277 {
278 --todo;
279
280 // Free the variable. Don't remove it from the hashtab, ht_array
281 // might change then. hash_clear() takes care of it later.
282 sav = HI2SAV(hi);
283 while (sav != NULL)
284 {
285 sav_next = sav->sav_next;
286 if (sav->sav_di == NULL)
287 clear_tv(&sav->sav_tv);
288 vim_free(sav);
289 sav = sav_next;
290 }
291 }
292 }
293 hash_clear(ht);
294 hash_init(ht);
295
296 ga_clear(&si->sn_var_vals);
297
298 // existing commands using script variable indexes are no longer valid
299 si->sn_script_seq = current_sctx.sc_seq;
300}
301
302/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100303 * Free all imported items in script "sid".
304 */
305 void
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200306free_imports_and_script_vars(int sid)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100307{
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100308 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100309 int idx;
310
311 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
312 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100313 imported_T *imp = ((imported_T *)si->sn_imports.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100314
315 vim_free(imp->imp_name);
316 }
317 ga_clear(&si->sn_imports);
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200318
319 free_all_script_vars(si);
320
Bram Moolenaar6110e792020-07-08 19:35:21 +0200321 clear_type_list(&si->sn_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100322}
323
324/*
Bram Moolenaara6294952020-12-27 13:39:50 +0100325 * Mark all imports as possible to redefine. Used when a script is loaded
326 * again but not cleared.
327 */
328 void
329mark_imports_for_reload(int sid)
330{
331 scriptitem_T *si = SCRIPT_ITEM(sid);
332 int idx;
333
334 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
335 {
336 imported_T *imp = ((imported_T *)si->sn_imports.ga_data) + idx;
337
338 imp->imp_flags |= IMP_FLAGS_RELOAD;
339 }
340}
341
342/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100343 * Handle an ":import" command and add the resulting imported_T to "gap", when
344 * not NULL, or script "import_sid" sn_imports.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200345 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100346 * Returns a pointer to after the command or NULL in case of failure
347 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200348 static char_u *
Bram Moolenaar1c991142020-07-04 13:15:31 +0200349handle_import(
350 char_u *arg_start,
351 garray_T *gap,
352 int import_sid,
353 evalarg_T *evalarg,
354 void *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100355{
356 char_u *arg = arg_start;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200357 char_u *cmd_end = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100358 int ret = FAIL;
359 typval_T tv;
360 int sid = -1;
361 int res;
Bram Moolenaar0a842842021-02-27 22:41:19 +0100362 int mult = FALSE;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200363 garray_T names;
Bram Moolenaar0a842842021-02-27 22:41:19 +0100364 garray_T as_names;
Bram Moolenaar921ba522021-07-29 22:25:05 +0200365 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100366
Bram Moolenaar4db572e2021-07-18 18:21:38 +0200367 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200368 ga_init2(&names, sizeof(char_u *), 10);
Bram Moolenaar0a842842021-02-27 22:41:19 +0100369 ga_init2(&as_names, sizeof(char_u *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100370 if (*arg == '{')
371 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200372 // "import {item, item} from ..."
Bram Moolenaar0a842842021-02-27 22:41:19 +0100373 mult = TRUE;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200374 arg = skipwhite_and_linebreak(arg + 1, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100375 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200376
Bram Moolenaar0a842842021-02-27 22:41:19 +0100377 for (;;)
378 {
379 char_u *p = arg;
380 int had_comma = FALSE;
381 char_u *as_name = NULL;
382
383 // accept "*" or "Name"
384 if (!mult && arg[0] == '*' && IS_WHITE_OR_NUL(arg[1]))
385 ++arg;
386 else
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100387 while (eval_isnamec(*arg))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100388 ++arg;
Bram Moolenaar0a842842021-02-27 22:41:19 +0100389 if (p == arg)
390 break;
391 if (ga_grow(&names, 1) == FAIL || ga_grow(&as_names, 1) == FAIL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200392 goto erret;
Bram Moolenaar0a842842021-02-27 22:41:19 +0100393 ((char_u **)names.ga_data)[names.ga_len] = vim_strnsave(p, arg - p);
394 ++names.ga_len;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200395
Bram Moolenaar0a842842021-02-27 22:41:19 +0100396 arg = skipwhite_and_linebreak(arg, evalarg);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200397 if (STRNCMP("as", arg, 2) == 0 && IS_WHITE_OR_NUL(arg[2]))
398 {
Bram Moolenaara9e3d562021-09-08 12:31:35 +0200399 // Skip over "as Name "; no line break allowed after "as".
400 // Do not allow for ':' and '#'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100401 arg = skipwhite(arg + 2);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200402 p = arg;
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100403 if (eval_isnamec1(*arg))
Bram Moolenaara9e3d562021-09-08 12:31:35 +0200404 while (ASCII_ISALNUM(*arg) || *arg == '_')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100405 ++arg;
Bram Moolenaara9e3d562021-09-08 12:31:35 +0200406 if (p == arg || !(IS_WHITE_OR_NUL(*arg)
407 || (mult && (*arg == ',' || *arg == '}'))))
408 {
409 semsg(_(e_syntax_error_in_import_str), p);
410 goto erret;
411 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100412 if (check_defined(p, arg - p, cctx, FALSE) == FAIL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200413 goto erret;
414 as_name = vim_strnsave(p, arg - p);
415 arg = skipwhite_and_linebreak(arg, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100416 }
417 else if (*arg_start == '*')
418 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200419 emsg(_(e_missing_as_after_star));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200420 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100421 }
Bram Moolenaar0a842842021-02-27 22:41:19 +0100422 // without "as Name" the as_names entry is NULL
423 ((char_u **)as_names.ga_data)[as_names.ga_len] = as_name;
424 ++as_names.ga_len;
425
426 if (!mult)
427 break;
428 if (*arg == ',')
429 {
430 had_comma = TRUE;
431 ++arg;
432 }
433 arg = skipwhite_and_linebreak(arg, evalarg);
434 if (*arg == '}')
435 {
436 ++arg;
437 break;
438 }
439 if (!had_comma)
440 {
441 emsg(_(e_missing_comma_in_import));
442 goto erret;
443 }
444 }
445 arg = skipwhite_and_linebreak(arg, evalarg);
446
447 if (names.ga_len == 0)
448 {
Bram Moolenaara9e3d562021-09-08 12:31:35 +0200449 semsg(_(e_syntax_error_in_import_str), arg_start);
Bram Moolenaar0a842842021-02-27 22:41:19 +0100450 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100451 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200452
453 if (STRNCMP("from", arg, 4) != 0 || !IS_WHITE_OR_NUL(arg[4]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100454 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200455 emsg(_(e_missing_from));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200456 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100457 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200458
Bram Moolenaar4db572e2021-07-18 18:21:38 +0200459 // The name of the file can be an expression, which must evaluate to a
460 // string.
Bram Moolenaar962d7212020-07-04 14:15:00 +0200461 arg = skipwhite_and_linebreak(arg + 4, evalarg);
Bram Moolenaar4db572e2021-07-18 18:21:38 +0200462 ret = eval0(arg, &tv, NULL, evalarg);
463 if (ret == FAIL)
464 goto erret;
465 if (tv.v_type != VAR_STRING
466 || tv.vval.v_string == NULL || *tv.vval.v_string == NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100467 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200468 emsg(_(e_invalid_string_after_from));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200469 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100470 }
471 cmd_end = arg;
472
Bram Moolenaar921ba522021-07-29 22:25:05 +0200473 // Give error messages for the start of the line.
474 SOURCING_LNUM = start_lnum;
475
Bram Moolenaar1c991142020-07-04 13:15:31 +0200476 /*
477 * find script file
478 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100479 if (*tv.vval.v_string == '.')
480 {
481 size_t len;
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100482 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100483 char_u *tail = gettail(si->sn_name);
484 char_u *from_name;
485
486 // Relative to current script: "./name.vim", "../../name.vim".
487 len = STRLEN(si->sn_name) - STRLEN(tail) + STRLEN(tv.vval.v_string) + 2;
488 from_name = alloc((int)len);
489 if (from_name == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200490 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100491 vim_strncpy(from_name, si->sn_name, tail - si->sn_name);
492 add_pathsep(from_name);
493 STRCAT(from_name, tv.vval.v_string);
494 simplify_filename(from_name);
495
496 res = do_source(from_name, FALSE, DOSO_NONE, &sid);
497 vim_free(from_name);
498 }
499 else if (mch_isFullName(tv.vval.v_string))
500 {
501 // Absolute path: "/tmp/name.vim"
502 res = do_source(tv.vval.v_string, FALSE, DOSO_NONE, &sid);
503 }
504 else
505 {
506 size_t len = 7 + STRLEN(tv.vval.v_string) + 1;
507 char_u *from_name;
508
509 // Find file in "import" subdirs in 'runtimepath'.
510 from_name = alloc((int)len);
511 if (from_name == NULL)
512 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200513 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100514 }
515 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 Moolenaar451c2e32020-08-15 16:33:28 +0200522 semsg(_(e_could_not_import_str), tv.vval.v_string);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200523 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100524 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100525
526 if (*arg_start == '*')
527 {
Bram Moolenaar0a842842021-02-27 22:41:19 +0100528 imported_T *imported;
529 char_u *as_name = ((char_u **)as_names.ga_data)[0];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100530
Bram Moolenaar0a842842021-02-27 22:41:19 +0100531 // "import * as That"
Bram Moolenaara6294952020-12-27 13:39:50 +0100532 imported = find_imported(as_name, STRLEN(as_name), cctx);
533 if (imported != NULL && imported->imp_sid == sid)
534 {
535 if (imported->imp_flags & IMP_FLAGS_RELOAD)
536 // import already defined on a previous script load
537 imported->imp_flags &= ~IMP_FLAGS_RELOAD;
538 else
539 {
540 semsg(_(e_name_already_defined_str), as_name);
541 goto erret;
542 }
543 }
544
545 imported = new_imported(gap != NULL ? gap
546 : &SCRIPT_ITEM(import_sid)->sn_imports);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100547 if (imported == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200548 goto erret;
549 imported->imp_name = as_name;
Bram Moolenaar0a842842021-02-27 22:41:19 +0100550 ((char_u **)as_names.ga_data)[0] = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100551 imported->imp_sid = sid;
Bram Moolenaara6294952020-12-27 13:39:50 +0100552 imported->imp_flags = IMP_FLAGS_STAR;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100553 }
554 else
555 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200556 int i;
557
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100558 arg = arg_start;
559 if (*arg == '{')
560 arg = skipwhite(arg + 1);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200561 for (i = 0; i < names.ga_len; ++i)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100562 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200563 char_u *name = ((char_u **)names.ga_data)[i];
Bram Moolenaar0a842842021-02-27 22:41:19 +0100564 char_u *as_name = ((char_u **)as_names.ga_data)[i];
Bram Moolenaara6294952020-12-27 13:39:50 +0100565 size_t len = STRLEN(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100566 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100567 imported_T *imported;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100568 ufunc_T *ufunc = NULL;
569 type_T *type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100570
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +0100571 idx = find_exported(sid, name, &ufunc, &type, cctx, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100572
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100573 if (idx < 0 && ufunc == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200574 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100575
Bram Moolenaarc967d572021-07-08 21:38:50 +0200576 // If already imported with the same properties and the
Bram Moolenaara6294952020-12-27 13:39:50 +0100577 // IMP_FLAGS_RELOAD set then we keep that entry. Otherwise create
578 // a new one (and give an error for an existing import).
579 imported = find_imported(name, len, cctx);
580 if (imported != NULL
581 && (imported->imp_flags & IMP_FLAGS_RELOAD)
582 && imported->imp_sid == sid
583 && (idx >= 0
Bram Moolenaar60dc8272021-07-29 22:48:54 +0200584 ? (equal_type(imported->imp_type, type, 0)
Bram Moolenaara6294952020-12-27 13:39:50 +0100585 && imported->imp_var_vals_idx == idx)
Bram Moolenaar60dc8272021-07-29 22:48:54 +0200586 : (equal_type(imported->imp_type, ufunc->uf_func_type,
587 ETYPE_ARG_UNKNOWN)
Bram Moolenaara6294952020-12-27 13:39:50 +0100588 && STRCMP(imported->imp_funcname,
589 ufunc->uf_name) == 0)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100590 {
Bram Moolenaara6294952020-12-27 13:39:50 +0100591 imported->imp_flags &= ~IMP_FLAGS_RELOAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100592 }
593 else
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200594 {
Bram Moolenaar6c7cc342021-04-17 16:38:50 +0200595 if (as_name == NULL
596 && check_defined(name, len, cctx, FALSE) == FAIL)
Bram Moolenaara6294952020-12-27 13:39:50 +0100597 goto erret;
598
599 imported = new_imported(gap != NULL ? gap
600 : &SCRIPT_ITEM(import_sid)->sn_imports);
601 if (imported == NULL)
602 goto erret;
603
Bram Moolenaar0a842842021-02-27 22:41:19 +0100604 if (as_name == NULL)
605 {
606 imported->imp_name = name;
607 ((char_u **)names.ga_data)[i] = NULL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100608 }
Bram Moolenaar0a842842021-02-27 22:41:19 +0100609 else
610 {
611 // "import This as That ..."
612 imported->imp_name = as_name;
613 ((char_u **)as_names.ga_data)[i] = NULL;
614 }
Bram Moolenaara6294952020-12-27 13:39:50 +0100615 imported->imp_sid = sid;
616 if (idx >= 0)
617 {
618 imported->imp_type = type;
619 imported->imp_var_vals_idx = idx;
620 }
621 else
622 {
623 imported->imp_type = ufunc->uf_func_type;
624 imported->imp_funcname = ufunc->uf_name;
625 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200626 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100627 }
628 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200629erret:
Bram Moolenaar4db572e2021-07-18 18:21:38 +0200630 clear_tv(&tv);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200631 ga_clear_strings(&names);
Bram Moolenaar0a842842021-02-27 22:41:19 +0100632 ga_clear_strings(&as_names);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100633 return cmd_end;
634}
635
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200636/*
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200637 * ":import Item from 'filename'"
638 * ":import Item as Alias from 'filename'"
639 * ":import {Item} from 'filename'".
640 * ":import {Item as Alias} from 'filename'"
641 * ":import {Item, Item} from 'filename'"
642 * ":import {Item, Item as Alias} from 'filename'"
643 *
644 * ":import * as Name from 'filename'"
645 */
646 void
647ex_import(exarg_T *eap)
648{
649 char_u *cmd_end;
650 evalarg_T evalarg;
651
652 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
653 {
654 emsg(_(e_import_can_only_be_used_in_script));
655 return;
656 }
657 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
658
659 cmd_end = handle_import(eap->arg, NULL, current_sctx.sc_sid,
660 &evalarg, NULL);
661 if (cmd_end != NULL)
662 set_nextcmd(eap, cmd_end);
663 clear_evalarg(&evalarg, eap);
664}
665
666/*
667 * Find an exported item in "sid" matching the name at "*argp".
668 * When it is a variable return the index.
669 * When it is a user function return "*ufunc".
670 * When not found returns -1 and "*ufunc" is NULL.
671 */
672 int
673find_exported(
674 int sid,
675 char_u *name,
676 ufunc_T **ufunc,
677 type_T **type,
678 cctx_T *cctx,
679 int verbose)
680{
681 int idx = -1;
682 svar_T *sv;
683 scriptitem_T *script = SCRIPT_ITEM(sid);
684
685 // Find name in "script".
686 idx = get_script_item_idx(sid, name, 0, cctx);
687 if (idx >= 0)
688 {
689 sv = ((svar_T *)script->sn_var_vals.ga_data) + idx;
690 if (!sv->sv_export)
691 {
692 if (verbose)
693 semsg(_(e_item_not_exported_in_script_str), name);
694 return -1;
695 }
696 *type = sv->sv_type;
697 *ufunc = NULL;
698 }
699 else
700 {
701 char_u buffer[200];
702 char_u *funcname;
703
704 // it could be a user function.
705 if (STRLEN(name) < sizeof(buffer) - 15)
706 funcname = buffer;
707 else
708 {
709 funcname = alloc(STRLEN(name) + 15);
710 if (funcname == NULL)
711 return -1;
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 *ufunc = find_func(funcname, FALSE, NULL);
718 if (funcname != buffer)
719 vim_free(funcname);
720
721 if (*ufunc == NULL)
722 {
723 if (verbose)
724 semsg(_(e_item_not_found_in_script_str), name);
725 return -1;
726 }
727 else if (((*ufunc)->uf_flags & FC_EXPORT) == 0)
728 {
729 if (verbose)
730 semsg(_(e_item_not_exported_in_script_str), name);
731 *ufunc = NULL;
732 return -1;
733 }
734 }
735
736 return idx;
737}
738
739/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200740 * Declare a script-local variable without init: "let var: type".
741 * "const" is an error since the value is missing.
742 * Returns a pointer to after the type.
743 */
744 char_u *
745vim9_declare_scriptvar(exarg_T *eap, char_u *arg)
746{
747 char_u *p;
748 char_u *name;
749 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
750 type_T *type;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200751 typval_T init_tv;
752
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200753 if (eap->cmdidx == CMD_final || eap->cmdidx == CMD_const)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200754 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200755 if (eap->cmdidx == CMD_final)
756 emsg(_(e_final_requires_a_value));
757 else
758 emsg(_(e_const_requires_a_value));
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200759 return arg + STRLEN(arg);
760 }
761
762 // Check for valid starting character.
763 if (!eval_isnamec1(*arg))
764 {
765 semsg(_(e_invarg2), arg);
766 return arg + STRLEN(arg);
767 }
768
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200769 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
Bram Moolenaar3b74b6b2020-06-19 19:01:43 +0200770 if (*p == ':' && (VIM_ISWHITE(p[1]) || p != arg + 1))
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200771 break;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200772
773 if (*p != ':')
774 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +0200775 emsg(_(e_type_or_initialization_required));
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200776 return arg + STRLEN(arg);
777 }
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200778 if (!VIM_ISWHITE(p[1]))
779 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100780 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200781 return arg + STRLEN(arg);
782 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200783 name = vim_strnsave(arg, p - arg);
784
Bram Moolenaard0edaf92021-05-28 21:06:08 +0200785 // parse type, check for reserved name
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200786 p = skipwhite(p + 1);
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100787 type = parse_type(&p, &si->sn_type_list, TRUE);
Bram Moolenaard0edaf92021-05-28 21:06:08 +0200788 if (type == NULL || check_reserved_name(name) == FAIL)
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200789 {
790 vim_free(name);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200791 return p;
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200792 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200793
794 // Create the variable with 0/NULL value.
795 CLEAR_FIELD(init_tv);
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +0200796 if (type->tt_type == VAR_ANY)
797 // A variable of type "any" is not possible, just use zero instead
798 init_tv.v_type = VAR_NUMBER;
799 else
800 init_tv.v_type = type->tt_type;
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100801 set_var_const(name, type, &init_tv, FALSE, 0, 0);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200802
803 vim_free(name);
804 return p;
805}
806
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200807/*
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200808 * Vim9 part of adding a script variable: add it to sn_all_vars (lookup by name
809 * with a hashtable) and sn_var_vals (lookup by index).
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100810 * When "create" is TRUE this is a new variable, otherwise find and update an
811 * existing variable.
Bram Moolenaar08251752021-01-11 21:20:18 +0100812 * "flags" can have ASSIGN_FINAL or ASSIGN_CONST.
Bram Moolenaarf2253962021-04-13 20:53:13 +0200813 * When "*type" is NULL use "tv" for the type and update "*type". If
814 * "do_member" is TRUE also use the member type, otherwise use "any".
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200815 */
816 void
Bram Moolenaar08251752021-01-11 21:20:18 +0100817update_vim9_script_var(
818 int create,
819 dictitem_T *di,
820 int flags,
821 typval_T *tv,
Bram Moolenaarf2253962021-04-13 20:53:13 +0200822 type_T **type,
823 int do_member)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200824{
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100825 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
826 hashitem_T *hi;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200827 svar_T *sv = NULL;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200828
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100829 if (create)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200830 {
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200831 sallvar_T *newsav;
832 sallvar_T *sav = NULL;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200833
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100834 // Store a pointer to the typval_T, so that it can be found by index
835 // instead of using a hastab lookup.
836 if (ga_grow(&si->sn_var_vals, 1) == FAIL)
837 return;
838
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200839 hi = hash_find(&si->sn_all_vars.dv_hashtab, di->di_key);
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200840 if (!HASHITEM_EMPTY(hi))
841 {
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200842 // Variable with this name exists, either in this block or in
843 // another block.
844 for (sav = HI2SAV(hi); ; sav = sav->sav_next)
845 {
846 if (sav->sav_block_id == si->sn_current_block_id)
847 {
848 // variable defined in a loop, re-use the entry
849 sv = ((svar_T *)si->sn_var_vals.ga_data)
850 + sav->sav_var_vals_idx;
851 // unhide the variable
852 if (sv->sv_tv == &sav->sav_tv)
853 {
854 clear_tv(&sav->sav_tv);
855 sv->sv_tv = &di->di_tv;
856 sav->sav_di = di;
857 }
858 break;
859 }
860 if (sav->sav_next == NULL)
861 break;
862 }
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200863 }
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200864
865 if (sv == NULL)
866 {
867 // Variable not defined or not defined in current block: Add a
868 // svar_T and create a new sallvar_T.
869 sv = ((svar_T *)si->sn_var_vals.ga_data) + si->sn_var_vals.ga_len;
870 newsav = (sallvar_T *)alloc_clear(
871 sizeof(sallvar_T) + STRLEN(di->di_key));
872 if (newsav == NULL)
873 return;
874
875 sv->sv_tv = &di->di_tv;
876 sv->sv_const = (flags & ASSIGN_FINAL) ? ASSIGN_FINAL
877 : (flags & ASSIGN_CONST) ? ASSIGN_CONST : 0;
878 sv->sv_export = is_export;
879 newsav->sav_var_vals_idx = si->sn_var_vals.ga_len;
880 ++si->sn_var_vals.ga_len;
881 STRCPY(&newsav->sav_key, di->di_key);
882 sv->sv_name = newsav->sav_key;
883 newsav->sav_di = di;
884 newsav->sav_block_id = si->sn_current_block_id;
885
886 if (HASHITEM_EMPTY(hi))
887 // new variable name
888 hash_add(&si->sn_all_vars.dv_hashtab, newsav->sav_key);
889 else if (sav != NULL)
890 // existing name in a new block, append to the list
891 sav->sav_next = newsav;
892 }
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200893 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100894 else
895 {
Bram Moolenaarc967d572021-07-08 21:38:50 +0200896 sv = find_typval_in_script(&di->di_tv);
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100897 }
898 if (sv != NULL)
899 {
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100900 if (*type == NULL)
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200901 *type = typval2type(tv, get_copyID(), &si->sn_type_list, do_member);
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100902 sv->sv_type = *type;
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100903 }
904
905 // let ex_export() know the export worked.
906 is_export = FALSE;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200907}
908
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200909/*
910 * Hide a script variable when leaving a block.
911 * "idx" is de index in sn_var_vals.
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200912 * When "func_defined" is non-zero then a function was defined in this block,
913 * the variable may be accessed by it. Otherwise the variable can be cleared.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200914 */
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200915 void
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200916hide_script_var(scriptitem_T *si, int idx, int func_defined)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200917{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200918 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200919 hashtab_T *script_ht = get_script_local_ht();
920 hashtab_T *all_ht = &si->sn_all_vars.dv_hashtab;
921 hashitem_T *script_hi;
922 hashitem_T *all_hi;
923
924 // Remove a variable declared inside the block, if it still exists.
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200925 // If it was added in a nested block it will already have been removed.
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200926 // The typval is moved into the sallvar_T.
927 script_hi = hash_find(script_ht, sv->sv_name);
928 all_hi = hash_find(all_ht, sv->sv_name);
929 if (!HASHITEM_EMPTY(script_hi) && !HASHITEM_EMPTY(all_hi))
930 {
931 dictitem_T *di = HI2DI(script_hi);
932 sallvar_T *sav = HI2SAV(all_hi);
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200933 sallvar_T *sav_prev = NULL;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200934
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200935 // There can be multiple entries with the same name in different
936 // blocks, find the right one.
937 while (sav != NULL && sav->sav_var_vals_idx != idx)
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200938 {
939 sav_prev = sav;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200940 sav = sav->sav_next;
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200941 }
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200942 if (sav != NULL)
943 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200944 if (func_defined)
945 {
946 // move the typval from the dictitem to the sallvar
947 sav->sav_tv = di->di_tv;
948 di->di_tv.v_type = VAR_UNKNOWN;
949 sav->sav_flags = di->di_flags;
950 sav->sav_di = NULL;
951 sv->sv_tv = &sav->sav_tv;
952 }
953 else
954 {
955 if (sav_prev == NULL)
956 hash_remove(all_ht, all_hi);
957 else
958 sav_prev->sav_next = sav->sav_next;
959 sv->sv_name = NULL;
960 vim_free(sav);
961 }
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200962 delete_var(script_ht, script_hi);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200963 }
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200964 }
965}
966
967/*
Bram Moolenaar10c65862020-10-08 21:16:42 +0200968 * Find the script-local variable that links to "dest".
Bram Moolenaarc967d572021-07-08 21:38:50 +0200969 * Returns NULL if not found and give an internal error.
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200970 */
Bram Moolenaar10c65862020-10-08 21:16:42 +0200971 svar_T *
Bram Moolenaarc967d572021-07-08 21:38:50 +0200972find_typval_in_script(typval_T *dest)
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200973{
974 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
975 int idx;
976
Bram Moolenaar81e17fb2020-08-21 21:55:43 +0200977 if (si->sn_version != SCRIPT_VERSION_VIM9)
978 // legacy script doesn't store variable types
Bram Moolenaar10c65862020-10-08 21:16:42 +0200979 return NULL;
Bram Moolenaar81e17fb2020-08-21 21:55:43 +0200980
Bram Moolenaara06758d2021-10-15 00:18:37 +0100981 // Find the svar_T in sn_var_vals. Start at the end, in a for loop the
982 // variable was added at the end.
983 for (idx = si->sn_var_vals.ga_len - 1; idx >= 0; --idx)
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200984 {
985 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
986
Bram Moolenaarc0913d02020-12-05 14:44:37 +0100987 // If "sv_name" is NULL the variable was hidden when leaving a block,
988 // don't check "sv_tv" then, it might be used for another variable now.
989 if (sv->sv_name != NULL && sv->sv_tv == dest)
Bram Moolenaar10c65862020-10-08 21:16:42 +0200990 return sv;
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200991 }
Bram Moolenaarc967d572021-07-08 21:38:50 +0200992 iemsg("find_typval_in_script(): not found");
Bram Moolenaar10c65862020-10-08 21:16:42 +0200993 return NULL;
994}
995
996/*
997 * Check if the type of script variable "dest" allows assigning "value".
998 * If needed convert "value" to a bool.
999 */
1000 int
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001001check_script_var_type(
1002 typval_T *dest,
1003 typval_T *value,
1004 char_u *name,
1005 where_T where)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001006{
Bram Moolenaarc967d572021-07-08 21:38:50 +02001007 svar_T *sv = find_typval_in_script(dest);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001008 int ret;
1009
1010 if (sv != NULL)
1011 {
Bram Moolenaar08251752021-01-11 21:20:18 +01001012 if (sv->sv_const != 0)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001013 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02001014 semsg(_(e_cannot_change_readonly_variable_str), name);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001015 return FAIL;
1016 }
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001017 ret = check_typval_type(sv->sv_type, value, where);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001018 if (ret == OK && need_convert_to_bool(sv->sv_type, value))
1019 {
1020 int val = tv2bool(value);
1021
1022 clear_tv(value);
1023 value->v_type = VAR_BOOL;
1024 value->v_lock = 0;
1025 value->vval.v_number = val ? VVAL_TRUE : VVAL_FALSE;
1026 }
1027 return ret;
1028 }
1029
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02001030 return OK; // not really
Bram Moolenaar34db91f2020-06-13 19:00:10 +02001031}
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001032
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001033// words that cannot be used as a variable
1034static char *reserved[] = {
1035 "true",
1036 "false",
1037 "null",
Bram Moolenaar74235772021-06-12 14:53:05 +02001038 "this",
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001039 NULL
1040};
1041
1042 int
1043check_reserved_name(char_u *name)
1044{
1045 int idx;
1046
1047 for (idx = 0; reserved[idx] != NULL; ++idx)
1048 if (STRCMP(reserved[idx], name) == 0)
1049 {
1050 semsg(_(e_cannot_use_reserved_name), name);
1051 return FAIL;
1052 }
1053 return OK;
1054}
1055
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001056#endif // FEAT_EVAL