blob: 605d0be969047a5ab47030c4dfc9c2dab3123a5d [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
Bram Moolenaar22480d12021-06-25 21:31:09 +0200172 if (VIM_ISWHITE(cmd[2]))
173 {
174 semsg(_(e_no_white_space_allowed_after_str_str),
175 eap->cmdidx == CMD_increment ? "++" : "--", eap->cmd);
176 return;
177 }
178
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200179 // This works like "nr += 1" or "nr -= 1".
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200180 // Add a '|' to avoid looking in the next line.
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200181 eap->cmd = alloc(len);
182 if (eap->cmd == NULL)
183 return;
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200184 vim_snprintf((char *)eap->cmd, len, "%s %c= 1 |", cmd + 2,
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200185 eap->cmdidx == CMD_increment ? '+' : '-');
186 eap->arg = eap->cmd;
187 eap->cmdidx = CMD_var;
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200188 eap->nextcmd = NULL;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200189 ex_let(eap);
190 vim_free(eap->cmd);
191 eap->cmd = cmd;
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200192 eap->nextcmd = nextcmd;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200193}
194
195/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100196 * ":export let Name: type"
197 * ":export const Name: type"
198 * ":export def Name(..."
199 * ":export class Name ..."
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100200 */
201 void
Bram Moolenaar09689a02020-05-09 22:50:08 +0200202ex_export(exarg_T *eap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100203{
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200204 if (!in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100205 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200206 emsg(_(e_export_can_only_be_used_in_vim9script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100207 return;
208 }
209
210 eap->cmd = eap->arg;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +0100211 (void)find_ex_command(eap, NULL, lookup_scriptitem, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100212 switch (eap->cmdidx)
213 {
214 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200215 case CMD_var:
216 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100217 case CMD_const:
218 case CMD_def:
219 // case CMD_class:
220 is_export = TRUE;
221 do_cmdline(eap->cmd, eap->getline, eap->cookie,
222 DOCMD_VERBOSE + DOCMD_NOWAIT);
223
224 // The command will reset "is_export" when exporting an item.
225 if (is_export)
226 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200227 emsg(_(e_export_with_invalid_argument));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100228 is_export = FALSE;
229 }
230 break;
231 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200232 emsg(_(e_invalid_command_after_export));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100233 break;
234 }
235}
236
237/*
238 * Add a new imported item entry to the current script.
239 */
240 static imported_T *
241new_imported(garray_T *gap)
242{
243 if (ga_grow(gap, 1) == OK)
244 return ((imported_T *)gap->ga_data + gap->ga_len++);
245 return NULL;
246}
247
248/*
249 * Free all imported items in script "sid".
250 */
251 void
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200252free_imports_and_script_vars(int sid)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100253{
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100254 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100255 int idx;
256
257 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
258 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100259 imported_T *imp = ((imported_T *)si->sn_imports.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100260
261 vim_free(imp->imp_name);
262 }
263 ga_clear(&si->sn_imports);
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200264
265 free_all_script_vars(si);
266
Bram Moolenaar6110e792020-07-08 19:35:21 +0200267 clear_type_list(&si->sn_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100268}
269
270/*
Bram Moolenaara6294952020-12-27 13:39:50 +0100271 * Mark all imports as possible to redefine. Used when a script is loaded
272 * again but not cleared.
273 */
274 void
275mark_imports_for_reload(int sid)
276{
277 scriptitem_T *si = SCRIPT_ITEM(sid);
278 int idx;
279
280 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
281 {
282 imported_T *imp = ((imported_T *)si->sn_imports.ga_data) + idx;
283
284 imp->imp_flags |= IMP_FLAGS_RELOAD;
285 }
286}
287
288/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100289 * ":import Item from 'filename'"
290 * ":import Item as Alias from 'filename'"
291 * ":import {Item} from 'filename'".
292 * ":import {Item as Alias} from 'filename'"
293 * ":import {Item, Item} from 'filename'"
294 * ":import {Item, Item as Alias} from 'filename'"
295 *
296 * ":import * as Name from 'filename'"
297 */
298 void
299ex_import(exarg_T *eap)
300{
Bram Moolenaar1c991142020-07-04 13:15:31 +0200301 char_u *cmd_end;
302 evalarg_T evalarg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100303
Bram Moolenaar101f4812020-06-16 23:18:51 +0200304 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
305 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200306 emsg(_(e_import_can_only_be_used_in_script));
Bram Moolenaar101f4812020-06-16 23:18:51 +0200307 return;
308 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200309 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar101f4812020-06-16 23:18:51 +0200310
Bram Moolenaar1c991142020-07-04 13:15:31 +0200311 cmd_end = handle_import(eap->arg, NULL, current_sctx.sc_sid,
312 &evalarg, NULL);
Bram Moolenaar9721fb42020-06-11 23:10:46 +0200313 if (cmd_end != NULL)
314 eap->nextcmd = check_nextcmd(cmd_end);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200315 clear_evalarg(&evalarg, eap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100316}
317
318/*
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100319 * Find an exported item in "sid" matching the name at "*argp".
320 * When it is a variable return the index.
321 * When it is a user function return "*ufunc".
322 * When not found returns -1 and "*ufunc" is NULL.
323 */
324 int
325find_exported(
326 int sid,
Bram Moolenaar1c991142020-07-04 13:15:31 +0200327 char_u *name,
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100328 ufunc_T **ufunc,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200329 type_T **type,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +0100330 cctx_T *cctx,
331 int verbose)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100332{
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100333 int idx = -1;
334 svar_T *sv;
335 scriptitem_T *script = SCRIPT_ITEM(sid);
336
Bram Moolenaar529fb5a2021-04-01 12:57:57 +0200337 // Find name in "script".
Bram Moolenaar08251752021-01-11 21:20:18 +0100338 idx = get_script_item_idx(sid, name, 0, cctx);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100339 if (idx >= 0)
340 {
341 sv = ((svar_T *)script->sn_var_vals.ga_data) + idx;
342 if (!sv->sv_export)
343 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +0100344 if (verbose)
345 semsg(_(e_item_not_exported_in_script_str), name);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100346 return -1;
347 }
348 *type = sv->sv_type;
349 *ufunc = NULL;
350 }
351 else
352 {
353 char_u buffer[200];
354 char_u *funcname;
355
356 // it could be a user function.
Bram Moolenaar5a67c372020-07-23 14:39:47 +0200357 if (STRLEN(name) < sizeof(buffer) - 15)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100358 funcname = buffer;
359 else
360 {
Bram Moolenaar5a67c372020-07-23 14:39:47 +0200361 funcname = alloc(STRLEN(name) + 15);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100362 if (funcname == NULL)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100363 return -1;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100364 }
365 funcname[0] = K_SPECIAL;
366 funcname[1] = KS_EXTRA;
367 funcname[2] = (int)KE_SNR;
368 sprintf((char *)funcname + 3, "%ld_%s", (long)sid, name);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200369 *ufunc = find_func(funcname, FALSE, NULL);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100370 if (funcname != buffer)
371 vim_free(funcname);
372
373 if (*ufunc == NULL)
374 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +0100375 if (verbose)
376 semsg(_(e_item_not_found_in_script_str), name);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100377 return -1;
378 }
Bram Moolenaar529fb5a2021-04-01 12:57:57 +0200379 else if (((*ufunc)->uf_flags & FC_EXPORT) == 0)
380 {
381 if (verbose)
382 semsg(_(e_item_not_exported_in_script_str), name);
383 *ufunc = NULL;
384 return -1;
385 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100386 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100387
388 return idx;
389}
390
391/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100392 * Handle an ":import" command and add the resulting imported_T to "gap", when
393 * not NULL, or script "import_sid" sn_imports.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200394 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100395 * Returns a pointer to after the command or NULL in case of failure
396 */
397 char_u *
Bram Moolenaar1c991142020-07-04 13:15:31 +0200398handle_import(
399 char_u *arg_start,
400 garray_T *gap,
401 int import_sid,
402 evalarg_T *evalarg,
403 void *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100404{
405 char_u *arg = arg_start;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200406 char_u *cmd_end = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100407 int ret = FAIL;
408 typval_T tv;
409 int sid = -1;
410 int res;
Bram Moolenaar0a842842021-02-27 22:41:19 +0100411 int mult = FALSE;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200412 garray_T names;
Bram Moolenaar0a842842021-02-27 22:41:19 +0100413 garray_T as_names;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100414
Bram Moolenaar4db572e2021-07-18 18:21:38 +0200415 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200416 ga_init2(&names, sizeof(char_u *), 10);
Bram Moolenaar0a842842021-02-27 22:41:19 +0100417 ga_init2(&as_names, sizeof(char_u *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100418 if (*arg == '{')
419 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200420 // "import {item, item} from ..."
Bram Moolenaar0a842842021-02-27 22:41:19 +0100421 mult = TRUE;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200422 arg = skipwhite_and_linebreak(arg + 1, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100423 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200424
Bram Moolenaar0a842842021-02-27 22:41:19 +0100425 for (;;)
426 {
427 char_u *p = arg;
428 int had_comma = FALSE;
429 char_u *as_name = NULL;
430
431 // accept "*" or "Name"
432 if (!mult && arg[0] == '*' && IS_WHITE_OR_NUL(arg[1]))
433 ++arg;
434 else
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100435 while (eval_isnamec(*arg))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100436 ++arg;
Bram Moolenaar0a842842021-02-27 22:41:19 +0100437 if (p == arg)
438 break;
439 if (ga_grow(&names, 1) == FAIL || ga_grow(&as_names, 1) == FAIL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200440 goto erret;
Bram Moolenaar0a842842021-02-27 22:41:19 +0100441 ((char_u **)names.ga_data)[names.ga_len] = vim_strnsave(p, arg - p);
442 ++names.ga_len;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200443
Bram Moolenaar0a842842021-02-27 22:41:19 +0100444 arg = skipwhite_and_linebreak(arg, evalarg);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200445 if (STRNCMP("as", arg, 2) == 0 && IS_WHITE_OR_NUL(arg[2]))
446 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200447 // skip over "as Name "; no line break allowed after "as"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100448 arg = skipwhite(arg + 2);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200449 p = arg;
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100450 if (eval_isnamec1(*arg))
451 while (eval_isnamec(*arg))
452 ++arg;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100453 if (check_defined(p, arg - p, cctx, FALSE) == FAIL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200454 goto erret;
455 as_name = vim_strnsave(p, arg - p);
456 arg = skipwhite_and_linebreak(arg, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100457 }
458 else if (*arg_start == '*')
459 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200460 emsg(_(e_missing_as_after_star));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200461 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100462 }
Bram Moolenaar0a842842021-02-27 22:41:19 +0100463 // without "as Name" the as_names entry is NULL
464 ((char_u **)as_names.ga_data)[as_names.ga_len] = as_name;
465 ++as_names.ga_len;
466
467 if (!mult)
468 break;
469 if (*arg == ',')
470 {
471 had_comma = TRUE;
472 ++arg;
473 }
474 arg = skipwhite_and_linebreak(arg, evalarg);
475 if (*arg == '}')
476 {
477 ++arg;
478 break;
479 }
480 if (!had_comma)
481 {
482 emsg(_(e_missing_comma_in_import));
483 goto erret;
484 }
485 }
486 arg = skipwhite_and_linebreak(arg, evalarg);
487
488 if (names.ga_len == 0)
489 {
490 emsg(_(e_syntax_error_in_import));
491 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100492 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200493
494 if (STRNCMP("from", arg, 4) != 0 || !IS_WHITE_OR_NUL(arg[4]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100495 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200496 emsg(_(e_missing_from));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200497 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100498 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200499
Bram Moolenaar4db572e2021-07-18 18:21:38 +0200500 // The name of the file can be an expression, which must evaluate to a
501 // string.
Bram Moolenaar962d7212020-07-04 14:15:00 +0200502 arg = skipwhite_and_linebreak(arg + 4, evalarg);
Bram Moolenaar4db572e2021-07-18 18:21:38 +0200503 ret = eval0(arg, &tv, NULL, evalarg);
504 if (ret == FAIL)
505 goto erret;
506 if (tv.v_type != VAR_STRING
507 || tv.vval.v_string == NULL || *tv.vval.v_string == NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100508 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200509 emsg(_(e_invalid_string_after_from));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200510 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100511 }
512 cmd_end = arg;
513
Bram Moolenaar1c991142020-07-04 13:15:31 +0200514 /*
515 * find script file
516 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100517 if (*tv.vval.v_string == '.')
518 {
519 size_t len;
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100520 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100521 char_u *tail = gettail(si->sn_name);
522 char_u *from_name;
523
524 // Relative to current script: "./name.vim", "../../name.vim".
525 len = STRLEN(si->sn_name) - STRLEN(tail) + STRLEN(tv.vval.v_string) + 2;
526 from_name = alloc((int)len);
527 if (from_name == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200528 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100529 vim_strncpy(from_name, si->sn_name, tail - si->sn_name);
530 add_pathsep(from_name);
531 STRCAT(from_name, tv.vval.v_string);
532 simplify_filename(from_name);
533
534 res = do_source(from_name, FALSE, DOSO_NONE, &sid);
535 vim_free(from_name);
536 }
537 else if (mch_isFullName(tv.vval.v_string))
538 {
539 // Absolute path: "/tmp/name.vim"
540 res = do_source(tv.vval.v_string, FALSE, DOSO_NONE, &sid);
541 }
542 else
543 {
544 size_t len = 7 + STRLEN(tv.vval.v_string) + 1;
545 char_u *from_name;
546
547 // Find file in "import" subdirs in 'runtimepath'.
548 from_name = alloc((int)len);
549 if (from_name == NULL)
550 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200551 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100552 }
553 vim_snprintf((char *)from_name, len, "import/%s", tv.vval.v_string);
554 res = source_in_path(p_rtp, from_name, DIP_NOAFTER, &sid);
555 vim_free(from_name);
556 }
557
558 if (res == FAIL || sid <= 0)
559 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200560 semsg(_(e_could_not_import_str), tv.vval.v_string);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200561 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100562 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100563
564 if (*arg_start == '*')
565 {
Bram Moolenaar0a842842021-02-27 22:41:19 +0100566 imported_T *imported;
567 char_u *as_name = ((char_u **)as_names.ga_data)[0];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100568
Bram Moolenaar0a842842021-02-27 22:41:19 +0100569 // "import * as That"
Bram Moolenaara6294952020-12-27 13:39:50 +0100570 imported = find_imported(as_name, STRLEN(as_name), cctx);
571 if (imported != NULL && imported->imp_sid == sid)
572 {
573 if (imported->imp_flags & IMP_FLAGS_RELOAD)
574 // import already defined on a previous script load
575 imported->imp_flags &= ~IMP_FLAGS_RELOAD;
576 else
577 {
578 semsg(_(e_name_already_defined_str), as_name);
579 goto erret;
580 }
581 }
582
583 imported = new_imported(gap != NULL ? gap
584 : &SCRIPT_ITEM(import_sid)->sn_imports);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100585 if (imported == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200586 goto erret;
587 imported->imp_name = as_name;
Bram Moolenaar0a842842021-02-27 22:41:19 +0100588 ((char_u **)as_names.ga_data)[0] = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100589 imported->imp_sid = sid;
Bram Moolenaara6294952020-12-27 13:39:50 +0100590 imported->imp_flags = IMP_FLAGS_STAR;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100591 }
592 else
593 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200594 int i;
595
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100596 arg = arg_start;
597 if (*arg == '{')
598 arg = skipwhite(arg + 1);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200599 for (i = 0; i < names.ga_len; ++i)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100600 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200601 char_u *name = ((char_u **)names.ga_data)[i];
Bram Moolenaar0a842842021-02-27 22:41:19 +0100602 char_u *as_name = ((char_u **)as_names.ga_data)[i];
Bram Moolenaara6294952020-12-27 13:39:50 +0100603 size_t len = STRLEN(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100604 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100605 imported_T *imported;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100606 ufunc_T *ufunc = NULL;
607 type_T *type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100608
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +0100609 idx = find_exported(sid, name, &ufunc, &type, cctx, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100610
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100611 if (idx < 0 && ufunc == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200612 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100613
Bram Moolenaarc967d572021-07-08 21:38:50 +0200614 // If already imported with the same properties and the
Bram Moolenaara6294952020-12-27 13:39:50 +0100615 // IMP_FLAGS_RELOAD set then we keep that entry. Otherwise create
616 // a new one (and give an error for an existing import).
617 imported = find_imported(name, len, cctx);
618 if (imported != NULL
619 && (imported->imp_flags & IMP_FLAGS_RELOAD)
620 && imported->imp_sid == sid
621 && (idx >= 0
622 ? (equal_type(imported->imp_type, type)
623 && imported->imp_var_vals_idx == idx)
624 : (equal_type(imported->imp_type, ufunc->uf_func_type)
625 && STRCMP(imported->imp_funcname,
626 ufunc->uf_name) == 0)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100627 {
Bram Moolenaara6294952020-12-27 13:39:50 +0100628 imported->imp_flags &= ~IMP_FLAGS_RELOAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100629 }
630 else
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200631 {
Bram Moolenaar6c7cc342021-04-17 16:38:50 +0200632 if (as_name == NULL
633 && check_defined(name, len, cctx, FALSE) == FAIL)
Bram Moolenaara6294952020-12-27 13:39:50 +0100634 goto erret;
635
636 imported = new_imported(gap != NULL ? gap
637 : &SCRIPT_ITEM(import_sid)->sn_imports);
638 if (imported == NULL)
639 goto erret;
640
Bram Moolenaar0a842842021-02-27 22:41:19 +0100641 if (as_name == NULL)
642 {
643 imported->imp_name = name;
644 ((char_u **)names.ga_data)[i] = NULL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100645 }
Bram Moolenaar0a842842021-02-27 22:41:19 +0100646 else
647 {
648 // "import This as That ..."
649 imported->imp_name = as_name;
650 ((char_u **)as_names.ga_data)[i] = NULL;
651 }
Bram Moolenaara6294952020-12-27 13:39:50 +0100652 imported->imp_sid = sid;
653 if (idx >= 0)
654 {
655 imported->imp_type = type;
656 imported->imp_var_vals_idx = idx;
657 }
658 else
659 {
660 imported->imp_type = ufunc->uf_func_type;
661 imported->imp_funcname = ufunc->uf_name;
662 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200663 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100664 }
665 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200666erret:
Bram Moolenaar4db572e2021-07-18 18:21:38 +0200667 clear_tv(&tv);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200668 ga_clear_strings(&names);
Bram Moolenaar0a842842021-02-27 22:41:19 +0100669 ga_clear_strings(&as_names);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100670 return cmd_end;
671}
672
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200673/*
674 * Declare a script-local variable without init: "let var: type".
675 * "const" is an error since the value is missing.
676 * Returns a pointer to after the type.
677 */
678 char_u *
679vim9_declare_scriptvar(exarg_T *eap, char_u *arg)
680{
681 char_u *p;
682 char_u *name;
683 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
684 type_T *type;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200685 typval_T init_tv;
686
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200687 if (eap->cmdidx == CMD_final || eap->cmdidx == CMD_const)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200688 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200689 if (eap->cmdidx == CMD_final)
690 emsg(_(e_final_requires_a_value));
691 else
692 emsg(_(e_const_requires_a_value));
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200693 return arg + STRLEN(arg);
694 }
695
696 // Check for valid starting character.
697 if (!eval_isnamec1(*arg))
698 {
699 semsg(_(e_invarg2), arg);
700 return arg + STRLEN(arg);
701 }
702
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200703 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
Bram Moolenaar3b74b6b2020-06-19 19:01:43 +0200704 if (*p == ':' && (VIM_ISWHITE(p[1]) || p != arg + 1))
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200705 break;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200706
707 if (*p != ':')
708 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +0200709 emsg(_(e_type_or_initialization_required));
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200710 return arg + STRLEN(arg);
711 }
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200712 if (!VIM_ISWHITE(p[1]))
713 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100714 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200715 return arg + STRLEN(arg);
716 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200717 name = vim_strnsave(arg, p - arg);
718
Bram Moolenaard0edaf92021-05-28 21:06:08 +0200719 // parse type, check for reserved name
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200720 p = skipwhite(p + 1);
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100721 type = parse_type(&p, &si->sn_type_list, TRUE);
Bram Moolenaard0edaf92021-05-28 21:06:08 +0200722 if (type == NULL || check_reserved_name(name) == FAIL)
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200723 {
724 vim_free(name);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200725 return p;
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200726 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200727
728 // Create the variable with 0/NULL value.
729 CLEAR_FIELD(init_tv);
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +0200730 if (type->tt_type == VAR_ANY)
731 // A variable of type "any" is not possible, just use zero instead
732 init_tv.v_type = VAR_NUMBER;
733 else
734 init_tv.v_type = type->tt_type;
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100735 set_var_const(name, type, &init_tv, FALSE, 0, 0);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200736
737 vim_free(name);
738 return p;
739}
740
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200741/*
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200742 * Vim9 part of adding a script variable: add it to sn_all_vars (lookup by name
743 * with a hashtable) and sn_var_vals (lookup by index).
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100744 * When "create" is TRUE this is a new variable, otherwise find and update an
745 * existing variable.
Bram Moolenaar08251752021-01-11 21:20:18 +0100746 * "flags" can have ASSIGN_FINAL or ASSIGN_CONST.
Bram Moolenaarf2253962021-04-13 20:53:13 +0200747 * When "*type" is NULL use "tv" for the type and update "*type". If
748 * "do_member" is TRUE also use the member type, otherwise use "any".
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200749 */
750 void
Bram Moolenaar08251752021-01-11 21:20:18 +0100751update_vim9_script_var(
752 int create,
753 dictitem_T *di,
754 int flags,
755 typval_T *tv,
Bram Moolenaarf2253962021-04-13 20:53:13 +0200756 type_T **type,
757 int do_member)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200758{
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100759 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
760 hashitem_T *hi;
761 svar_T *sv;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200762
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100763 if (create)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200764 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100765 sallvar_T *newsav;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200766
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100767 // Store a pointer to the typval_T, so that it can be found by index
768 // instead of using a hastab lookup.
769 if (ga_grow(&si->sn_var_vals, 1) == FAIL)
770 return;
771
772 sv = ((svar_T *)si->sn_var_vals.ga_data) + si->sn_var_vals.ga_len;
773 newsav = (sallvar_T *)alloc_clear(
774 sizeof(sallvar_T) + STRLEN(di->di_key));
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200775 if (newsav == NULL)
776 return;
777
778 sv->sv_tv = &di->di_tv;
Bram Moolenaar08251752021-01-11 21:20:18 +0100779 sv->sv_const = (flags & ASSIGN_FINAL) ? ASSIGN_FINAL
780 : (flags & ASSIGN_CONST) ? ASSIGN_CONST : 0;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200781 sv->sv_export = is_export;
782 newsav->sav_var_vals_idx = si->sn_var_vals.ga_len;
783 ++si->sn_var_vals.ga_len;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200784 STRCPY(&newsav->sav_key, di->di_key);
785 sv->sv_name = newsav->sav_key;
786 newsav->sav_di = di;
787 newsav->sav_block_id = si->sn_current_block_id;
788
789 hi = hash_find(&si->sn_all_vars.dv_hashtab, newsav->sav_key);
790 if (!HASHITEM_EMPTY(hi))
791 {
792 sallvar_T *sav = HI2SAV(hi);
793
794 // variable with this name exists in another block
795 while (sav->sav_next != NULL)
796 sav = sav->sav_next;
797 sav->sav_next = newsav;
798 }
799 else
800 // new variable name
801 hash_add(&si->sn_all_vars.dv_hashtab, newsav->sav_key);
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200802 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100803 else
804 {
Bram Moolenaarc967d572021-07-08 21:38:50 +0200805 sv = find_typval_in_script(&di->di_tv);
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100806 }
807 if (sv != NULL)
808 {
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100809 if (*type == NULL)
Bram Moolenaarf2253962021-04-13 20:53:13 +0200810 *type = typval2type(tv, get_copyID(), &si->sn_type_list,
811 do_member);
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100812 sv->sv_type = *type;
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100813 }
814
815 // let ex_export() know the export worked.
816 is_export = FALSE;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200817}
818
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200819/*
820 * Hide a script variable when leaving a block.
821 * "idx" is de index in sn_var_vals.
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200822 * When "func_defined" is non-zero then a function was defined in this block,
823 * the variable may be accessed by it. Otherwise the variable can be cleared.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200824 */
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200825 void
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200826hide_script_var(scriptitem_T *si, int idx, int func_defined)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200827{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200828 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200829 hashtab_T *script_ht = get_script_local_ht();
830 hashtab_T *all_ht = &si->sn_all_vars.dv_hashtab;
831 hashitem_T *script_hi;
832 hashitem_T *all_hi;
833
834 // Remove a variable declared inside the block, if it still exists.
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200835 // If it was added in a nested block it will already have been removed.
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200836 // The typval is moved into the sallvar_T.
837 script_hi = hash_find(script_ht, sv->sv_name);
838 all_hi = hash_find(all_ht, sv->sv_name);
839 if (!HASHITEM_EMPTY(script_hi) && !HASHITEM_EMPTY(all_hi))
840 {
841 dictitem_T *di = HI2DI(script_hi);
842 sallvar_T *sav = HI2SAV(all_hi);
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200843 sallvar_T *sav_prev = NULL;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200844
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200845 // There can be multiple entries with the same name in different
846 // blocks, find the right one.
847 while (sav != NULL && sav->sav_var_vals_idx != idx)
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200848 {
849 sav_prev = sav;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200850 sav = sav->sav_next;
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200851 }
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200852 if (sav != NULL)
853 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200854 if (func_defined)
855 {
856 // move the typval from the dictitem to the sallvar
857 sav->sav_tv = di->di_tv;
858 di->di_tv.v_type = VAR_UNKNOWN;
859 sav->sav_flags = di->di_flags;
860 sav->sav_di = NULL;
861 sv->sv_tv = &sav->sav_tv;
862 }
863 else
864 {
865 if (sav_prev == NULL)
866 hash_remove(all_ht, all_hi);
867 else
868 sav_prev->sav_next = sav->sav_next;
869 sv->sv_name = NULL;
870 vim_free(sav);
871 }
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200872 delete_var(script_ht, script_hi);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200873 }
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200874 }
875}
876
877/*
878 * Free the script variables from "sn_all_vars".
879 */
880 void
881free_all_script_vars(scriptitem_T *si)
882{
883 int todo;
884 hashtab_T *ht = &si->sn_all_vars.dv_hashtab;
885 hashitem_T *hi;
886 sallvar_T *sav;
887 sallvar_T *sav_next;
888
889 hash_lock(ht);
890 todo = (int)ht->ht_used;
891 for (hi = ht->ht_array; todo > 0; ++hi)
892 {
893 if (!HASHITEM_EMPTY(hi))
894 {
895 --todo;
896
897 // Free the variable. Don't remove it from the hashtab, ht_array
898 // might change then. hash_clear() takes care of it later.
899 sav = HI2SAV(hi);
900 while (sav != NULL)
901 {
902 sav_next = sav->sav_next;
903 if (sav->sav_di == NULL)
904 clear_tv(&sav->sav_tv);
905 vim_free(sav);
906 sav = sav_next;
907 }
908 }
909 }
910 hash_clear(ht);
911 hash_init(ht);
912
913 ga_clear(&si->sn_var_vals);
Bram Moolenaar2b327002020-12-26 15:39:31 +0100914
915 // existing commands using script variable indexes are no longer valid
916 si->sn_script_seq = current_sctx.sc_seq;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200917}
918
919/*
Bram Moolenaar10c65862020-10-08 21:16:42 +0200920 * Find the script-local variable that links to "dest".
Bram Moolenaarc967d572021-07-08 21:38:50 +0200921 * Returns NULL if not found and give an internal error.
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200922 */
Bram Moolenaar10c65862020-10-08 21:16:42 +0200923 svar_T *
Bram Moolenaarc967d572021-07-08 21:38:50 +0200924find_typval_in_script(typval_T *dest)
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200925{
926 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
927 int idx;
928
Bram Moolenaar81e17fb2020-08-21 21:55:43 +0200929 if (si->sn_version != SCRIPT_VERSION_VIM9)
930 // legacy script doesn't store variable types
Bram Moolenaar10c65862020-10-08 21:16:42 +0200931 return NULL;
Bram Moolenaar81e17fb2020-08-21 21:55:43 +0200932
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200933 // Find the svar_T in sn_var_vals.
934 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
935 {
936 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
937
Bram Moolenaarc0913d02020-12-05 14:44:37 +0100938 // If "sv_name" is NULL the variable was hidden when leaving a block,
939 // don't check "sv_tv" then, it might be used for another variable now.
940 if (sv->sv_name != NULL && sv->sv_tv == dest)
Bram Moolenaar10c65862020-10-08 21:16:42 +0200941 return sv;
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200942 }
Bram Moolenaarc967d572021-07-08 21:38:50 +0200943 iemsg("find_typval_in_script(): not found");
Bram Moolenaar10c65862020-10-08 21:16:42 +0200944 return NULL;
945}
946
947/*
948 * Check if the type of script variable "dest" allows assigning "value".
949 * If needed convert "value" to a bool.
950 */
951 int
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100952check_script_var_type(
953 typval_T *dest,
954 typval_T *value,
955 char_u *name,
956 where_T where)
Bram Moolenaar10c65862020-10-08 21:16:42 +0200957{
Bram Moolenaarc967d572021-07-08 21:38:50 +0200958 svar_T *sv = find_typval_in_script(dest);
Bram Moolenaar10c65862020-10-08 21:16:42 +0200959 int ret;
960
961 if (sv != NULL)
962 {
Bram Moolenaar08251752021-01-11 21:20:18 +0100963 if (sv->sv_const != 0)
Bram Moolenaar10c65862020-10-08 21:16:42 +0200964 {
Bram Moolenaard8e44472021-07-21 22:20:33 +0200965 semsg(_(e_cannot_change_readonly_variable_str), name);
Bram Moolenaar10c65862020-10-08 21:16:42 +0200966 return FAIL;
967 }
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100968 ret = check_typval_type(sv->sv_type, value, where);
Bram Moolenaar10c65862020-10-08 21:16:42 +0200969 if (ret == OK && need_convert_to_bool(sv->sv_type, value))
970 {
971 int val = tv2bool(value);
972
973 clear_tv(value);
974 value->v_type = VAR_BOOL;
975 value->v_lock = 0;
976 value->vval.v_number = val ? VVAL_TRUE : VVAL_FALSE;
977 }
978 return ret;
979 }
980
Bram Moolenaarc785b9a2020-06-19 18:34:15 +0200981 return OK; // not really
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200982}
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200983
Bram Moolenaard0edaf92021-05-28 21:06:08 +0200984// words that cannot be used as a variable
985static char *reserved[] = {
986 "true",
987 "false",
988 "null",
Bram Moolenaar74235772021-06-12 14:53:05 +0200989 "this",
Bram Moolenaard0edaf92021-05-28 21:06:08 +0200990 NULL
991};
992
993 int
994check_reserved_name(char_u *name)
995{
996 int idx;
997
998 for (idx = 0; reserved[idx] != NULL; ++idx)
999 if (STRCMP(reserved[idx], name) == 0)
1000 {
1001 semsg(_(e_cannot_use_reserved_name), name);
1002 return FAIL;
1003 }
1004 return OK;
1005}
1006
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001007#endif // FEAT_EVAL