blob: 7c6526b659af053721301e5d68b41c34c57f4504 [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 Moolenaare535db82021-03-31 21:07:24 +020037 * Return TRUE if the current script is Vim9 script.
38 * This also returns TRUE in a legacy function in a Vim9 script.
39 */
40 int
41current_script_is_vim9(void)
42{
43 return SCRIPT_ID_VALID(current_sctx.sc_sid)
44 && SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
45 == SCRIPT_VERSION_VIM9;
46}
Bram Moolenaarb91d3f82021-04-01 13:17:50 +020047#endif
Bram Moolenaare535db82021-03-31 21:07:24 +020048
49/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010050 * ":vim9script".
51 */
52 void
Bram Moolenaar9b8d6222020-12-28 18:26:00 +010053ex_vim9script(exarg_T *eap UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010054{
Bram Moolenaar9b8d6222020-12-28 18:26:00 +010055#ifdef FEAT_EVAL
Bram Moolenaar2b327002020-12-26 15:39:31 +010056 int sid = current_sctx.sc_sid;
Bram Moolenaar101f4812020-06-16 23:18:51 +020057 scriptitem_T *si;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010058
59 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
60 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +020061 emsg(_(e_vim9script_can_only_be_used_in_script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010062 return;
63 }
Bram Moolenaar2b327002020-12-26 15:39:31 +010064
65 si = SCRIPT_ITEM(sid);
66 if (si->sn_state == SN_STATE_HAD_COMMAND)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010067 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +020068 emsg(_(e_vim9script_must_be_first_command_in_script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010069 return;
70 }
Bram Moolenaar2b327002020-12-26 15:39:31 +010071 if (!IS_WHITE_OR_NUL(*eap->arg) && STRCMP(eap->arg, "noclear") != 0)
72 {
73 semsg(_(e_invarg2), eap->arg);
74 return;
75 }
76 if (si->sn_state == SN_STATE_RELOAD && IS_WHITE_OR_NUL(*eap->arg))
77 {
78 hashtab_T *ht = &SCRIPT_VARS(sid);
79
80 // Reloading a script without the "noclear" argument: clear
81 // script-local variables and functions.
82 hashtab_free_contents(ht);
83 hash_init(ht);
84 delete_script_functions(sid);
85
86 // old imports and script variables are no longer valid
87 free_imports_and_script_vars(sid);
88 }
89 si->sn_state = SN_STATE_HAD_COMMAND;
90
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010091 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
92 si->sn_version = SCRIPT_VERSION_VIM9;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010093
94 if (STRCMP(p_cpo, CPO_VIM) != 0)
95 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +020096 si->sn_save_cpo = vim_strsave(p_cpo);
Bram Moolenaar37294bd2021-03-10 13:40:08 +010097 set_option_value((char_u *)"cpo", 0L, (char_u *)CPO_VIM, OPT_NO_REDRAW);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010098 }
Bram Moolenaar9b8d6222020-12-28 18:26:00 +010099#else
100 // No check for this being the first command, it doesn't matter.
101 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
102#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100103}
104
105/*
Bram Moolenaarae616492020-07-28 20:07:27 +0200106 * When in Vim9 script give an error and return FAIL.
107 */
108 int
109not_in_vim9(exarg_T *eap)
110{
Bram Moolenaar68e30442020-07-28 21:15:07 +0200111 if (in_vim9script())
112 switch (eap->cmdidx)
113 {
Bram Moolenaarada1d872021-02-20 08:16:51 +0100114 case CMD_k:
115 if (eap->addr_count > 0)
116 {
117 emsg(_(e_norange));
118 return FAIL;
119 }
120 // FALLTHROUGH
Bram Moolenaar68e30442020-07-28 21:15:07 +0200121 case CMD_append:
122 case CMD_change:
Bram Moolenaarf5a48012020-08-01 17:00:03 +0200123 case CMD_insert:
Bram Moolenaar65088802021-03-13 21:07:21 +0100124 case CMD_open:
Bram Moolenaarf5a48012020-08-01 17:00:03 +0200125 case CMD_t:
Bram Moolenaar68e30442020-07-28 21:15:07 +0200126 case CMD_xit:
Bram Moolenaar9b8d6222020-12-28 18:26:00 +0100127 semsg(_(e_command_not_supported_in_vim9_script_missing_var_str), eap->cmd);
Bram Moolenaar68e30442020-07-28 21:15:07 +0200128 return FAIL;
129 default: break;
130 }
Bram Moolenaarae616492020-07-28 20:07:27 +0200131 return OK;
132}
133
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100134/*
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100135 * Give an error message if "p" points at "#{" and return TRUE.
136 * This avoids that using a legacy style #{} dictionary leads to difficult to
137 * understand errors.
138 */
139 int
140vim9_bad_comment(char_u *p)
141{
Bram Moolenaara0399ef2021-03-20 15:00:01 +0100142 if (p[0] == '#' && p[1] == '{' && p[2] != '{')
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100143 {
144 emsg(_(e_cannot_use_hash_curly_to_start_comment));
145 return TRUE;
146 }
147 return FALSE;
148}
149
150/*
Bram Moolenaara0399ef2021-03-20 15:00:01 +0100151 * Return TRUE if "p" points at a "#" not followed by one '{'.
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100152 * Does not check for white space.
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100153 */
154 int
155vim9_comment_start(char_u *p)
156{
Bram Moolenaara0399ef2021-03-20 15:00:01 +0100157 return p[0] == '#' && (p[1] != '{' || p[2] == '{');
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100158}
159
Bram Moolenaar9b8d6222020-12-28 18:26:00 +0100160#if defined(FEAT_EVAL) || defined(PROTO)
161
Bram Moolenaarae616492020-07-28 20:07:27 +0200162/*
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200163 * "++nr" and "--nr" commands.
164 */
165 void
166ex_incdec(exarg_T *eap)
167{
168 char_u *cmd = eap->cmd;
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200169 char_u *nextcmd = eap->nextcmd;
170 size_t len = STRLEN(eap->cmd) + 8;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200171
172 // This works like "nr += 1" or "nr -= 1".
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200173 // Add a '|' to avoid looking in the next line.
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200174 eap->cmd = alloc(len);
175 if (eap->cmd == NULL)
176 return;
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200177 vim_snprintf((char *)eap->cmd, len, "%s %c= 1 |", cmd + 2,
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200178 eap->cmdidx == CMD_increment ? '+' : '-');
179 eap->arg = eap->cmd;
180 eap->cmdidx = CMD_var;
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200181 eap->nextcmd = NULL;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200182 ex_let(eap);
183 vim_free(eap->cmd);
184 eap->cmd = cmd;
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200185 eap->nextcmd = nextcmd;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200186}
187
188/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100189 * ":export let Name: type"
190 * ":export const Name: type"
191 * ":export def Name(..."
192 * ":export class Name ..."
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100193 */
194 void
Bram Moolenaar09689a02020-05-09 22:50:08 +0200195ex_export(exarg_T *eap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100196{
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200197 if (!in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100198 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200199 emsg(_(e_export_can_only_be_used_in_vim9script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100200 return;
201 }
202
203 eap->cmd = eap->arg;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +0100204 (void)find_ex_command(eap, NULL, lookup_scriptitem, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100205 switch (eap->cmdidx)
206 {
207 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200208 case CMD_var:
209 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100210 case CMD_const:
211 case CMD_def:
212 // case CMD_class:
213 is_export = TRUE;
214 do_cmdline(eap->cmd, eap->getline, eap->cookie,
215 DOCMD_VERBOSE + DOCMD_NOWAIT);
216
217 // The command will reset "is_export" when exporting an item.
218 if (is_export)
219 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200220 emsg(_(e_export_with_invalid_argument));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100221 is_export = FALSE;
222 }
223 break;
224 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200225 emsg(_(e_invalid_command_after_export));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100226 break;
227 }
228}
229
230/*
231 * Add a new imported item entry to the current script.
232 */
233 static imported_T *
234new_imported(garray_T *gap)
235{
236 if (ga_grow(gap, 1) == OK)
237 return ((imported_T *)gap->ga_data + gap->ga_len++);
238 return NULL;
239}
240
241/*
242 * Free all imported items in script "sid".
243 */
244 void
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200245free_imports_and_script_vars(int sid)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100246{
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100247 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100248 int idx;
249
250 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
251 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100252 imported_T *imp = ((imported_T *)si->sn_imports.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100253
254 vim_free(imp->imp_name);
255 }
256 ga_clear(&si->sn_imports);
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200257
258 free_all_script_vars(si);
259
Bram Moolenaar6110e792020-07-08 19:35:21 +0200260 clear_type_list(&si->sn_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100261}
262
263/*
Bram Moolenaara6294952020-12-27 13:39:50 +0100264 * Mark all imports as possible to redefine. Used when a script is loaded
265 * again but not cleared.
266 */
267 void
268mark_imports_for_reload(int sid)
269{
270 scriptitem_T *si = SCRIPT_ITEM(sid);
271 int idx;
272
273 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
274 {
275 imported_T *imp = ((imported_T *)si->sn_imports.ga_data) + idx;
276
277 imp->imp_flags |= IMP_FLAGS_RELOAD;
278 }
279}
280
281/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100282 * ":import Item from 'filename'"
283 * ":import Item as Alias from 'filename'"
284 * ":import {Item} from 'filename'".
285 * ":import {Item as Alias} from 'filename'"
286 * ":import {Item, Item} from 'filename'"
287 * ":import {Item, Item as Alias} from 'filename'"
288 *
289 * ":import * as Name from 'filename'"
290 */
291 void
292ex_import(exarg_T *eap)
293{
Bram Moolenaar1c991142020-07-04 13:15:31 +0200294 char_u *cmd_end;
295 evalarg_T evalarg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100296
Bram Moolenaar101f4812020-06-16 23:18:51 +0200297 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
298 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200299 emsg(_(e_import_can_only_be_used_in_script));
Bram Moolenaar101f4812020-06-16 23:18:51 +0200300 return;
301 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200302 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar101f4812020-06-16 23:18:51 +0200303
Bram Moolenaar1c991142020-07-04 13:15:31 +0200304 cmd_end = handle_import(eap->arg, NULL, current_sctx.sc_sid,
305 &evalarg, NULL);
Bram Moolenaar9721fb42020-06-11 23:10:46 +0200306 if (cmd_end != NULL)
307 eap->nextcmd = check_nextcmd(cmd_end);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200308 clear_evalarg(&evalarg, eap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100309}
310
311/*
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100312 * Find an exported item in "sid" matching the name at "*argp".
313 * When it is a variable return the index.
314 * When it is a user function return "*ufunc".
315 * When not found returns -1 and "*ufunc" is NULL.
316 */
317 int
318find_exported(
319 int sid,
Bram Moolenaar1c991142020-07-04 13:15:31 +0200320 char_u *name,
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100321 ufunc_T **ufunc,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200322 type_T **type,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +0100323 cctx_T *cctx,
324 int verbose)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100325{
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100326 int idx = -1;
327 svar_T *sv;
328 scriptitem_T *script = SCRIPT_ITEM(sid);
329
Bram Moolenaar529fb5a2021-04-01 12:57:57 +0200330 // Find name in "script".
Bram Moolenaar08251752021-01-11 21:20:18 +0100331 idx = get_script_item_idx(sid, name, 0, cctx);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100332 if (idx >= 0)
333 {
334 sv = ((svar_T *)script->sn_var_vals.ga_data) + idx;
335 if (!sv->sv_export)
336 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +0100337 if (verbose)
338 semsg(_(e_item_not_exported_in_script_str), name);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100339 return -1;
340 }
341 *type = sv->sv_type;
342 *ufunc = NULL;
343 }
344 else
345 {
346 char_u buffer[200];
347 char_u *funcname;
348
349 // it could be a user function.
Bram Moolenaar5a67c372020-07-23 14:39:47 +0200350 if (STRLEN(name) < sizeof(buffer) - 15)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100351 funcname = buffer;
352 else
353 {
Bram Moolenaar5a67c372020-07-23 14:39:47 +0200354 funcname = alloc(STRLEN(name) + 15);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100355 if (funcname == NULL)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100356 return -1;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100357 }
358 funcname[0] = K_SPECIAL;
359 funcname[1] = KS_EXTRA;
360 funcname[2] = (int)KE_SNR;
361 sprintf((char *)funcname + 3, "%ld_%s", (long)sid, name);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200362 *ufunc = find_func(funcname, FALSE, NULL);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100363 if (funcname != buffer)
364 vim_free(funcname);
365
366 if (*ufunc == NULL)
367 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +0100368 if (verbose)
369 semsg(_(e_item_not_found_in_script_str), name);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100370 return -1;
371 }
Bram Moolenaar529fb5a2021-04-01 12:57:57 +0200372 else if (((*ufunc)->uf_flags & FC_EXPORT) == 0)
373 {
374 if (verbose)
375 semsg(_(e_item_not_exported_in_script_str), name);
376 *ufunc = NULL;
377 return -1;
378 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100379 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100380
381 return idx;
382}
383
384/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100385 * Handle an ":import" command and add the resulting imported_T to "gap", when
386 * not NULL, or script "import_sid" sn_imports.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200387 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100388 * Returns a pointer to after the command or NULL in case of failure
389 */
390 char_u *
Bram Moolenaar1c991142020-07-04 13:15:31 +0200391handle_import(
392 char_u *arg_start,
393 garray_T *gap,
394 int import_sid,
395 evalarg_T *evalarg,
396 void *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100397{
398 char_u *arg = arg_start;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200399 char_u *cmd_end = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100400 int ret = FAIL;
401 typval_T tv;
402 int sid = -1;
403 int res;
Bram Moolenaar0a842842021-02-27 22:41:19 +0100404 int mult = FALSE;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200405 garray_T names;
Bram Moolenaar0a842842021-02-27 22:41:19 +0100406 garray_T as_names;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100407
Bram Moolenaar1c991142020-07-04 13:15:31 +0200408 ga_init2(&names, sizeof(char_u *), 10);
Bram Moolenaar0a842842021-02-27 22:41:19 +0100409 ga_init2(&as_names, sizeof(char_u *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100410 if (*arg == '{')
411 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200412 // "import {item, item} from ..."
Bram Moolenaar0a842842021-02-27 22:41:19 +0100413 mult = TRUE;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200414 arg = skipwhite_and_linebreak(arg + 1, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100415 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200416
Bram Moolenaar0a842842021-02-27 22:41:19 +0100417 for (;;)
418 {
419 char_u *p = arg;
420 int had_comma = FALSE;
421 char_u *as_name = NULL;
422
423 // accept "*" or "Name"
424 if (!mult && arg[0] == '*' && IS_WHITE_OR_NUL(arg[1]))
425 ++arg;
426 else
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100427 while (eval_isnamec(*arg))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100428 ++arg;
Bram Moolenaar0a842842021-02-27 22:41:19 +0100429 if (p == arg)
430 break;
431 if (ga_grow(&names, 1) == FAIL || ga_grow(&as_names, 1) == FAIL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200432 goto erret;
Bram Moolenaar0a842842021-02-27 22:41:19 +0100433 ((char_u **)names.ga_data)[names.ga_len] = vim_strnsave(p, arg - p);
434 ++names.ga_len;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200435
Bram Moolenaar0a842842021-02-27 22:41:19 +0100436 arg = skipwhite_and_linebreak(arg, evalarg);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200437 if (STRNCMP("as", arg, 2) == 0 && IS_WHITE_OR_NUL(arg[2]))
438 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200439 // skip over "as Name "; no line break allowed after "as"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100440 arg = skipwhite(arg + 2);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200441 p = arg;
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100442 if (eval_isnamec1(*arg))
443 while (eval_isnamec(*arg))
444 ++arg;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100445 if (check_defined(p, arg - p, cctx, FALSE) == FAIL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200446 goto erret;
447 as_name = vim_strnsave(p, arg - p);
448 arg = skipwhite_and_linebreak(arg, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100449 }
450 else if (*arg_start == '*')
451 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200452 emsg(_(e_missing_as_after_star));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200453 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100454 }
Bram Moolenaar0a842842021-02-27 22:41:19 +0100455 // without "as Name" the as_names entry is NULL
456 ((char_u **)as_names.ga_data)[as_names.ga_len] = as_name;
457 ++as_names.ga_len;
458
459 if (!mult)
460 break;
461 if (*arg == ',')
462 {
463 had_comma = TRUE;
464 ++arg;
465 }
466 arg = skipwhite_and_linebreak(arg, evalarg);
467 if (*arg == '}')
468 {
469 ++arg;
470 break;
471 }
472 if (!had_comma)
473 {
474 emsg(_(e_missing_comma_in_import));
475 goto erret;
476 }
477 }
478 arg = skipwhite_and_linebreak(arg, evalarg);
479
480 if (names.ga_len == 0)
481 {
482 emsg(_(e_syntax_error_in_import));
483 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100484 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200485
486 if (STRNCMP("from", arg, 4) != 0 || !IS_WHITE_OR_NUL(arg[4]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100487 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200488 emsg(_(e_missing_from));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200489 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100490 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200491
Bram Moolenaar962d7212020-07-04 14:15:00 +0200492 arg = skipwhite_and_linebreak(arg + 4, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100493 tv.v_type = VAR_UNKNOWN;
494 // TODO: should we accept any expression?
495 if (*arg == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +0200496 ret = eval_lit_string(&arg, &tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100497 else if (*arg == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +0200498 ret = eval_string(&arg, &tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100499 if (ret == FAIL || tv.vval.v_string == NULL || *tv.vval.v_string == NUL)
500 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200501 emsg(_(e_invalid_string_after_from));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200502 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100503 }
504 cmd_end = arg;
505
Bram Moolenaar1c991142020-07-04 13:15:31 +0200506 /*
507 * find script file
508 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100509 if (*tv.vval.v_string == '.')
510 {
511 size_t len;
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100512 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100513 char_u *tail = gettail(si->sn_name);
514 char_u *from_name;
515
516 // Relative to current script: "./name.vim", "../../name.vim".
517 len = STRLEN(si->sn_name) - STRLEN(tail) + STRLEN(tv.vval.v_string) + 2;
518 from_name = alloc((int)len);
519 if (from_name == NULL)
520 {
521 clear_tv(&tv);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200522 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100523 }
524 vim_strncpy(from_name, si->sn_name, tail - si->sn_name);
525 add_pathsep(from_name);
526 STRCAT(from_name, tv.vval.v_string);
527 simplify_filename(from_name);
528
529 res = do_source(from_name, FALSE, DOSO_NONE, &sid);
530 vim_free(from_name);
531 }
532 else if (mch_isFullName(tv.vval.v_string))
533 {
534 // Absolute path: "/tmp/name.vim"
535 res = do_source(tv.vval.v_string, FALSE, DOSO_NONE, &sid);
536 }
537 else
538 {
539 size_t len = 7 + STRLEN(tv.vval.v_string) + 1;
540 char_u *from_name;
541
542 // Find file in "import" subdirs in 'runtimepath'.
543 from_name = alloc((int)len);
544 if (from_name == NULL)
545 {
546 clear_tv(&tv);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200547 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100548 }
549 vim_snprintf((char *)from_name, len, "import/%s", tv.vval.v_string);
550 res = source_in_path(p_rtp, from_name, DIP_NOAFTER, &sid);
551 vim_free(from_name);
552 }
553
554 if (res == FAIL || sid <= 0)
555 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200556 semsg(_(e_could_not_import_str), tv.vval.v_string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100557 clear_tv(&tv);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200558 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100559 }
560 clear_tv(&tv);
561
562 if (*arg_start == '*')
563 {
Bram Moolenaar0a842842021-02-27 22:41:19 +0100564 imported_T *imported;
565 char_u *as_name = ((char_u **)as_names.ga_data)[0];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100566
Bram Moolenaar0a842842021-02-27 22:41:19 +0100567 // "import * as That"
Bram Moolenaara6294952020-12-27 13:39:50 +0100568 imported = find_imported(as_name, STRLEN(as_name), cctx);
569 if (imported != NULL && imported->imp_sid == sid)
570 {
571 if (imported->imp_flags & IMP_FLAGS_RELOAD)
572 // import already defined on a previous script load
573 imported->imp_flags &= ~IMP_FLAGS_RELOAD;
574 else
575 {
576 semsg(_(e_name_already_defined_str), as_name);
577 goto erret;
578 }
579 }
580
581 imported = new_imported(gap != NULL ? gap
582 : &SCRIPT_ITEM(import_sid)->sn_imports);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100583 if (imported == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200584 goto erret;
585 imported->imp_name = as_name;
Bram Moolenaar0a842842021-02-27 22:41:19 +0100586 ((char_u **)as_names.ga_data)[0] = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100587 imported->imp_sid = sid;
Bram Moolenaara6294952020-12-27 13:39:50 +0100588 imported->imp_flags = IMP_FLAGS_STAR;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100589 }
590 else
591 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200592 int i;
593
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100594 arg = arg_start;
595 if (*arg == '{')
596 arg = skipwhite(arg + 1);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200597 for (i = 0; i < names.ga_len; ++i)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100598 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200599 char_u *name = ((char_u **)names.ga_data)[i];
Bram Moolenaar0a842842021-02-27 22:41:19 +0100600 char_u *as_name = ((char_u **)as_names.ga_data)[i];
Bram Moolenaara6294952020-12-27 13:39:50 +0100601 size_t len = STRLEN(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100602 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100603 imported_T *imported;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100604 ufunc_T *ufunc = NULL;
605 type_T *type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100606
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +0100607 idx = find_exported(sid, name, &ufunc, &type, cctx, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100608
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100609 if (idx < 0 && ufunc == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200610 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100611
Bram Moolenaara6294952020-12-27 13:39:50 +0100612 // If already imported with the same propertis and the
613 // IMP_FLAGS_RELOAD set then we keep that entry. Otherwise create
614 // a new one (and give an error for an existing import).
615 imported = find_imported(name, len, cctx);
616 if (imported != NULL
617 && (imported->imp_flags & IMP_FLAGS_RELOAD)
618 && imported->imp_sid == sid
619 && (idx >= 0
620 ? (equal_type(imported->imp_type, type)
621 && imported->imp_var_vals_idx == idx)
622 : (equal_type(imported->imp_type, ufunc->uf_func_type)
623 && STRCMP(imported->imp_funcname,
624 ufunc->uf_name) == 0)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100625 {
Bram Moolenaara6294952020-12-27 13:39:50 +0100626 imported->imp_flags &= ~IMP_FLAGS_RELOAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100627 }
628 else
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200629 {
Bram Moolenaar6c7cc342021-04-17 16:38:50 +0200630 if (as_name == NULL
631 && check_defined(name, len, cctx, FALSE) == FAIL)
Bram Moolenaara6294952020-12-27 13:39:50 +0100632 goto erret;
633
634 imported = new_imported(gap != NULL ? gap
635 : &SCRIPT_ITEM(import_sid)->sn_imports);
636 if (imported == NULL)
637 goto erret;
638
Bram Moolenaar0a842842021-02-27 22:41:19 +0100639 if (as_name == NULL)
640 {
641 imported->imp_name = name;
642 ((char_u **)names.ga_data)[i] = NULL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100643 }
Bram Moolenaar0a842842021-02-27 22:41:19 +0100644 else
645 {
646 // "import This as That ..."
647 imported->imp_name = as_name;
648 ((char_u **)as_names.ga_data)[i] = NULL;
649 }
Bram Moolenaara6294952020-12-27 13:39:50 +0100650 imported->imp_sid = sid;
651 if (idx >= 0)
652 {
653 imported->imp_type = type;
654 imported->imp_var_vals_idx = idx;
655 }
656 else
657 {
658 imported->imp_type = ufunc->uf_func_type;
659 imported->imp_funcname = ufunc->uf_name;
660 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200661 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100662 }
663 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200664erret:
665 ga_clear_strings(&names);
Bram Moolenaar0a842842021-02-27 22:41:19 +0100666 ga_clear_strings(&as_names);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100667 return cmd_end;
668}
669
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200670/*
671 * Declare a script-local variable without init: "let var: type".
672 * "const" is an error since the value is missing.
673 * Returns a pointer to after the type.
674 */
675 char_u *
676vim9_declare_scriptvar(exarg_T *eap, char_u *arg)
677{
678 char_u *p;
679 char_u *name;
680 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
681 type_T *type;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200682 typval_T init_tv;
683
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200684 if (eap->cmdidx == CMD_final || eap->cmdidx == CMD_const)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200685 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200686 if (eap->cmdidx == CMD_final)
687 emsg(_(e_final_requires_a_value));
688 else
689 emsg(_(e_const_requires_a_value));
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200690 return arg + STRLEN(arg);
691 }
692
693 // Check for valid starting character.
694 if (!eval_isnamec1(*arg))
695 {
696 semsg(_(e_invarg2), arg);
697 return arg + STRLEN(arg);
698 }
699
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200700 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
Bram Moolenaar3b74b6b2020-06-19 19:01:43 +0200701 if (*p == ':' && (VIM_ISWHITE(p[1]) || p != arg + 1))
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200702 break;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200703
704 if (*p != ':')
705 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +0200706 emsg(_(e_type_or_initialization_required));
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200707 return arg + STRLEN(arg);
708 }
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200709 if (!VIM_ISWHITE(p[1]))
710 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100711 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200712 return arg + STRLEN(arg);
713 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200714 name = vim_strnsave(arg, p - arg);
715
Bram Moolenaard0edaf92021-05-28 21:06:08 +0200716 // parse type, check for reserved name
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200717 p = skipwhite(p + 1);
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100718 type = parse_type(&p, &si->sn_type_list, TRUE);
Bram Moolenaard0edaf92021-05-28 21:06:08 +0200719 if (type == NULL || check_reserved_name(name) == FAIL)
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200720 {
721 vim_free(name);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200722 return p;
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200723 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200724
725 // Create the variable with 0/NULL value.
726 CLEAR_FIELD(init_tv);
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +0200727 if (type->tt_type == VAR_ANY)
728 // A variable of type "any" is not possible, just use zero instead
729 init_tv.v_type = VAR_NUMBER;
730 else
731 init_tv.v_type = type->tt_type;
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100732 set_var_const(name, type, &init_tv, FALSE, 0, 0);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200733
734 vim_free(name);
735 return p;
736}
737
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200738/*
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200739 * Vim9 part of adding a script variable: add it to sn_all_vars (lookup by name
740 * with a hashtable) and sn_var_vals (lookup by index).
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100741 * When "create" is TRUE this is a new variable, otherwise find and update an
742 * existing variable.
Bram Moolenaar08251752021-01-11 21:20:18 +0100743 * "flags" can have ASSIGN_FINAL or ASSIGN_CONST.
Bram Moolenaarf2253962021-04-13 20:53:13 +0200744 * When "*type" is NULL use "tv" for the type and update "*type". If
745 * "do_member" is TRUE also use the member type, otherwise use "any".
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200746 */
747 void
Bram Moolenaar08251752021-01-11 21:20:18 +0100748update_vim9_script_var(
749 int create,
750 dictitem_T *di,
751 int flags,
752 typval_T *tv,
Bram Moolenaarf2253962021-04-13 20:53:13 +0200753 type_T **type,
754 int do_member)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200755{
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100756 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
757 hashitem_T *hi;
758 svar_T *sv;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200759
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100760 if (create)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200761 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100762 sallvar_T *newsav;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200763
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100764 // Store a pointer to the typval_T, so that it can be found by index
765 // instead of using a hastab lookup.
766 if (ga_grow(&si->sn_var_vals, 1) == FAIL)
767 return;
768
769 sv = ((svar_T *)si->sn_var_vals.ga_data) + si->sn_var_vals.ga_len;
770 newsav = (sallvar_T *)alloc_clear(
771 sizeof(sallvar_T) + STRLEN(di->di_key));
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200772 if (newsav == NULL)
773 return;
774
775 sv->sv_tv = &di->di_tv;
Bram Moolenaar08251752021-01-11 21:20:18 +0100776 sv->sv_const = (flags & ASSIGN_FINAL) ? ASSIGN_FINAL
777 : (flags & ASSIGN_CONST) ? ASSIGN_CONST : 0;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200778 sv->sv_export = is_export;
779 newsav->sav_var_vals_idx = si->sn_var_vals.ga_len;
780 ++si->sn_var_vals.ga_len;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200781 STRCPY(&newsav->sav_key, di->di_key);
782 sv->sv_name = newsav->sav_key;
783 newsav->sav_di = di;
784 newsav->sav_block_id = si->sn_current_block_id;
785
786 hi = hash_find(&si->sn_all_vars.dv_hashtab, newsav->sav_key);
787 if (!HASHITEM_EMPTY(hi))
788 {
789 sallvar_T *sav = HI2SAV(hi);
790
791 // variable with this name exists in another block
792 while (sav->sav_next != NULL)
793 sav = sav->sav_next;
794 sav->sav_next = newsav;
795 }
796 else
797 // new variable name
798 hash_add(&si->sn_all_vars.dv_hashtab, newsav->sav_key);
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200799 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100800 else
801 {
802 sv = find_typval_in_script(&di->di_tv);
803 }
804 if (sv != NULL)
805 {
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100806 if (*type == NULL)
Bram Moolenaarf2253962021-04-13 20:53:13 +0200807 *type = typval2type(tv, get_copyID(), &si->sn_type_list,
808 do_member);
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100809 sv->sv_type = *type;
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100810 }
811
812 // let ex_export() know the export worked.
813 is_export = FALSE;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200814}
815
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200816/*
817 * Hide a script variable when leaving a block.
818 * "idx" is de index in sn_var_vals.
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200819 * When "func_defined" is non-zero then a function was defined in this block,
820 * the variable may be accessed by it. Otherwise the variable can be cleared.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200821 */
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200822 void
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200823hide_script_var(scriptitem_T *si, int idx, int func_defined)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200824{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200825 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200826 hashtab_T *script_ht = get_script_local_ht();
827 hashtab_T *all_ht = &si->sn_all_vars.dv_hashtab;
828 hashitem_T *script_hi;
829 hashitem_T *all_hi;
830
831 // Remove a variable declared inside the block, if it still exists.
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200832 // If it was added in a nested block it will already have been removed.
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200833 // The typval is moved into the sallvar_T.
834 script_hi = hash_find(script_ht, sv->sv_name);
835 all_hi = hash_find(all_ht, sv->sv_name);
836 if (!HASHITEM_EMPTY(script_hi) && !HASHITEM_EMPTY(all_hi))
837 {
838 dictitem_T *di = HI2DI(script_hi);
839 sallvar_T *sav = HI2SAV(all_hi);
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200840 sallvar_T *sav_prev = NULL;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200841
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200842 // There can be multiple entries with the same name in different
843 // blocks, find the right one.
844 while (sav != NULL && sav->sav_var_vals_idx != idx)
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200845 {
846 sav_prev = sav;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200847 sav = sav->sav_next;
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200848 }
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200849 if (sav != NULL)
850 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200851 if (func_defined)
852 {
853 // move the typval from the dictitem to the sallvar
854 sav->sav_tv = di->di_tv;
855 di->di_tv.v_type = VAR_UNKNOWN;
856 sav->sav_flags = di->di_flags;
857 sav->sav_di = NULL;
858 sv->sv_tv = &sav->sav_tv;
859 }
860 else
861 {
862 if (sav_prev == NULL)
863 hash_remove(all_ht, all_hi);
864 else
865 sav_prev->sav_next = sav->sav_next;
866 sv->sv_name = NULL;
867 vim_free(sav);
868 }
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200869 delete_var(script_ht, script_hi);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200870 }
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200871 }
872}
873
874/*
875 * Free the script variables from "sn_all_vars".
876 */
877 void
878free_all_script_vars(scriptitem_T *si)
879{
880 int todo;
881 hashtab_T *ht = &si->sn_all_vars.dv_hashtab;
882 hashitem_T *hi;
883 sallvar_T *sav;
884 sallvar_T *sav_next;
885
886 hash_lock(ht);
887 todo = (int)ht->ht_used;
888 for (hi = ht->ht_array; todo > 0; ++hi)
889 {
890 if (!HASHITEM_EMPTY(hi))
891 {
892 --todo;
893
894 // Free the variable. Don't remove it from the hashtab, ht_array
895 // might change then. hash_clear() takes care of it later.
896 sav = HI2SAV(hi);
897 while (sav != NULL)
898 {
899 sav_next = sav->sav_next;
900 if (sav->sav_di == NULL)
901 clear_tv(&sav->sav_tv);
902 vim_free(sav);
903 sav = sav_next;
904 }
905 }
906 }
907 hash_clear(ht);
908 hash_init(ht);
909
910 ga_clear(&si->sn_var_vals);
Bram Moolenaar2b327002020-12-26 15:39:31 +0100911
912 // existing commands using script variable indexes are no longer valid
913 si->sn_script_seq = current_sctx.sc_seq;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200914}
915
916/*
Bram Moolenaar10c65862020-10-08 21:16:42 +0200917 * Find the script-local variable that links to "dest".
918 * Returns NULL if not found.
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200919 */
Bram Moolenaar10c65862020-10-08 21:16:42 +0200920 svar_T *
921find_typval_in_script(typval_T *dest)
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200922{
923 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
924 int idx;
925
Bram Moolenaar81e17fb2020-08-21 21:55:43 +0200926 if (si->sn_version != SCRIPT_VERSION_VIM9)
927 // legacy script doesn't store variable types
Bram Moolenaar10c65862020-10-08 21:16:42 +0200928 return NULL;
Bram Moolenaar81e17fb2020-08-21 21:55:43 +0200929
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200930 // Find the svar_T in sn_var_vals.
931 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
932 {
933 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
934
Bram Moolenaarc0913d02020-12-05 14:44:37 +0100935 // If "sv_name" is NULL the variable was hidden when leaving a block,
936 // don't check "sv_tv" then, it might be used for another variable now.
937 if (sv->sv_name != NULL && sv->sv_tv == dest)
Bram Moolenaar10c65862020-10-08 21:16:42 +0200938 return sv;
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200939 }
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100940 iemsg("find_typval_in_script(): not found");
Bram Moolenaar10c65862020-10-08 21:16:42 +0200941 return NULL;
942}
943
944/*
945 * Check if the type of script variable "dest" allows assigning "value".
946 * If needed convert "value" to a bool.
947 */
948 int
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100949check_script_var_type(
950 typval_T *dest,
951 typval_T *value,
952 char_u *name,
953 where_T where)
Bram Moolenaar10c65862020-10-08 21:16:42 +0200954{
955 svar_T *sv = find_typval_in_script(dest);
956 int ret;
957
958 if (sv != NULL)
959 {
Bram Moolenaar08251752021-01-11 21:20:18 +0100960 if (sv->sv_const != 0)
Bram Moolenaar10c65862020-10-08 21:16:42 +0200961 {
962 semsg(_(e_readonlyvar), name);
963 return FAIL;
964 }
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100965 ret = check_typval_type(sv->sv_type, value, where);
Bram Moolenaar10c65862020-10-08 21:16:42 +0200966 if (ret == OK && need_convert_to_bool(sv->sv_type, value))
967 {
968 int val = tv2bool(value);
969
970 clear_tv(value);
971 value->v_type = VAR_BOOL;
972 value->v_lock = 0;
973 value->vval.v_number = val ? VVAL_TRUE : VVAL_FALSE;
974 }
975 return ret;
976 }
977
Bram Moolenaarc785b9a2020-06-19 18:34:15 +0200978 return OK; // not really
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200979}
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200980
Bram Moolenaard0edaf92021-05-28 21:06:08 +0200981// words that cannot be used as a variable
982static char *reserved[] = {
983 "true",
984 "false",
985 "null",
Bram Moolenaar74235772021-06-12 14:53:05 +0200986 "this",
Bram Moolenaard0edaf92021-05-28 21:06:08 +0200987 NULL
988};
989
990 int
991check_reserved_name(char_u *name)
992{
993 int idx;
994
995 for (idx = 0; reserved[idx] != NULL; ++idx)
996 if (STRCMP(reserved[idx], name) == 0)
997 {
998 semsg(_(e_cannot_use_reserved_name), name);
999 return FAIL;
1000 }
1001 return OK;
1002}
1003
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001004#endif // FEAT_EVAL