blob: d40104cb7acfb268041e1c187ce6b9a65fdafec5 [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 Moolenaar8a7d6542020-01-26 15:56:19 +010020 int
21in_vim9script(void)
22{
Bram Moolenaareb6880b2020-07-12 17:07:05 +020023 // Do not go up the stack, a ":function" inside vim9script uses legacy
24 // syntax. "sc_version" is also set when compiling a ":def" function in
25 // legacy script.
Bram Moolenaar9979fcd2021-02-14 13:30:01 +010026 return current_sctx.sc_version == SCRIPT_VERSION_VIM9
27 || (cmdmod.cmod_flags & CMOD_VIM9CMD);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010028}
29
30/*
31 * ":vim9script".
32 */
33 void
Bram Moolenaar9b8d6222020-12-28 18:26:00 +010034ex_vim9script(exarg_T *eap UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010035{
Bram Moolenaar9b8d6222020-12-28 18:26:00 +010036#ifdef FEAT_EVAL
Bram Moolenaar2b327002020-12-26 15:39:31 +010037 int sid = current_sctx.sc_sid;
Bram Moolenaar101f4812020-06-16 23:18:51 +020038 scriptitem_T *si;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010039
40 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
41 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +020042 emsg(_(e_vim9script_can_only_be_used_in_script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010043 return;
44 }
Bram Moolenaar2b327002020-12-26 15:39:31 +010045
46 si = SCRIPT_ITEM(sid);
47 if (si->sn_state == SN_STATE_HAD_COMMAND)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010048 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +020049 emsg(_(e_vim9script_must_be_first_command_in_script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010050 return;
51 }
Bram Moolenaar2b327002020-12-26 15:39:31 +010052 if (!IS_WHITE_OR_NUL(*eap->arg) && STRCMP(eap->arg, "noclear") != 0)
53 {
54 semsg(_(e_invarg2), eap->arg);
55 return;
56 }
57 if (si->sn_state == SN_STATE_RELOAD && IS_WHITE_OR_NUL(*eap->arg))
58 {
59 hashtab_T *ht = &SCRIPT_VARS(sid);
60
61 // Reloading a script without the "noclear" argument: clear
62 // script-local variables and functions.
63 hashtab_free_contents(ht);
64 hash_init(ht);
65 delete_script_functions(sid);
66
67 // old imports and script variables are no longer valid
68 free_imports_and_script_vars(sid);
69 }
70 si->sn_state = SN_STATE_HAD_COMMAND;
71
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010072 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
73 si->sn_version = SCRIPT_VERSION_VIM9;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010074
75 if (STRCMP(p_cpo, CPO_VIM) != 0)
76 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +020077 si->sn_save_cpo = vim_strsave(p_cpo);
78 set_option_value((char_u *)"cpo", 0L, (char_u *)CPO_VIM, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010079 }
Bram Moolenaar9b8d6222020-12-28 18:26:00 +010080#else
81 // No check for this being the first command, it doesn't matter.
82 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
83#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010084}
85
86/*
Bram Moolenaarae616492020-07-28 20:07:27 +020087 * When in Vim9 script give an error and return FAIL.
88 */
89 int
90not_in_vim9(exarg_T *eap)
91{
Bram Moolenaar68e30442020-07-28 21:15:07 +020092 if (in_vim9script())
93 switch (eap->cmdidx)
94 {
Bram Moolenaar68e30442020-07-28 21:15:07 +020095 case CMD_append:
96 case CMD_change:
Bram Moolenaarf5a48012020-08-01 17:00:03 +020097 case CMD_insert:
98 case CMD_t:
Bram Moolenaar68e30442020-07-28 21:15:07 +020099 case CMD_xit:
Bram Moolenaar9b8d6222020-12-28 18:26:00 +0100100 semsg(_(e_command_not_supported_in_vim9_script_missing_var_str), eap->cmd);
Bram Moolenaar68e30442020-07-28 21:15:07 +0200101 return FAIL;
102 default: break;
103 }
Bram Moolenaarae616492020-07-28 20:07:27 +0200104 return OK;
105}
106
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100107/*
108 * Return TRUE if "p" points at a "#". Does not check for white space.
109 */
110 int
111vim9_comment_start(char_u *p)
112{
113 return p[0] == '#';
114}
115
Bram Moolenaar9b8d6222020-12-28 18:26:00 +0100116#if defined(FEAT_EVAL) || defined(PROTO)
117
Bram Moolenaarae616492020-07-28 20:07:27 +0200118/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100119 * ":export let Name: type"
120 * ":export const Name: type"
121 * ":export def Name(..."
122 * ":export class Name ..."
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100123 */
124 void
Bram Moolenaar09689a02020-05-09 22:50:08 +0200125ex_export(exarg_T *eap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100126{
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200127 if (!in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100128 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200129 emsg(_(e_export_can_only_be_used_in_vim9script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100130 return;
131 }
132
133 eap->cmd = eap->arg;
134 (void)find_ex_command(eap, NULL, lookup_scriptvar, NULL);
135 switch (eap->cmdidx)
136 {
137 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200138 case CMD_var:
139 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100140 case CMD_const:
141 case CMD_def:
142 // case CMD_class:
143 is_export = TRUE;
144 do_cmdline(eap->cmd, eap->getline, eap->cookie,
145 DOCMD_VERBOSE + DOCMD_NOWAIT);
146
147 // The command will reset "is_export" when exporting an item.
148 if (is_export)
149 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200150 emsg(_(e_export_with_invalid_argument));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100151 is_export = FALSE;
152 }
153 break;
154 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200155 emsg(_(e_invalid_command_after_export));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100156 break;
157 }
158}
159
160/*
161 * Add a new imported item entry to the current script.
162 */
163 static imported_T *
164new_imported(garray_T *gap)
165{
166 if (ga_grow(gap, 1) == OK)
167 return ((imported_T *)gap->ga_data + gap->ga_len++);
168 return NULL;
169}
170
171/*
172 * Free all imported items in script "sid".
173 */
174 void
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200175free_imports_and_script_vars(int sid)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100176{
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100177 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100178 int idx;
179
180 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
181 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100182 imported_T *imp = ((imported_T *)si->sn_imports.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100183
184 vim_free(imp->imp_name);
185 }
186 ga_clear(&si->sn_imports);
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200187
188 free_all_script_vars(si);
189
Bram Moolenaar6110e792020-07-08 19:35:21 +0200190 clear_type_list(&si->sn_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100191}
192
193/*
Bram Moolenaara6294952020-12-27 13:39:50 +0100194 * Mark all imports as possible to redefine. Used when a script is loaded
195 * again but not cleared.
196 */
197 void
198mark_imports_for_reload(int sid)
199{
200 scriptitem_T *si = SCRIPT_ITEM(sid);
201 int idx;
202
203 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
204 {
205 imported_T *imp = ((imported_T *)si->sn_imports.ga_data) + idx;
206
207 imp->imp_flags |= IMP_FLAGS_RELOAD;
208 }
209}
210
211/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100212 * ":import Item from 'filename'"
213 * ":import Item as Alias from 'filename'"
214 * ":import {Item} from 'filename'".
215 * ":import {Item as Alias} from 'filename'"
216 * ":import {Item, Item} from 'filename'"
217 * ":import {Item, Item as Alias} from 'filename'"
218 *
219 * ":import * as Name from 'filename'"
220 */
221 void
222ex_import(exarg_T *eap)
223{
Bram Moolenaar1c991142020-07-04 13:15:31 +0200224 char_u *cmd_end;
225 evalarg_T evalarg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100226
Bram Moolenaar101f4812020-06-16 23:18:51 +0200227 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
228 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200229 emsg(_(e_import_can_only_be_used_in_script));
Bram Moolenaar101f4812020-06-16 23:18:51 +0200230 return;
231 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200232 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar101f4812020-06-16 23:18:51 +0200233
Bram Moolenaar1c991142020-07-04 13:15:31 +0200234 cmd_end = handle_import(eap->arg, NULL, current_sctx.sc_sid,
235 &evalarg, NULL);
Bram Moolenaar9721fb42020-06-11 23:10:46 +0200236 if (cmd_end != NULL)
237 eap->nextcmd = check_nextcmd(cmd_end);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200238 clear_evalarg(&evalarg, eap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100239}
240
241/*
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100242 * Find an exported item in "sid" matching the name at "*argp".
243 * When it is a variable return the index.
244 * When it is a user function return "*ufunc".
245 * When not found returns -1 and "*ufunc" is NULL.
246 */
247 int
248find_exported(
249 int sid,
Bram Moolenaar1c991142020-07-04 13:15:31 +0200250 char_u *name,
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100251 ufunc_T **ufunc,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200252 type_T **type,
253 cctx_T *cctx)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100254{
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100255 int idx = -1;
256 svar_T *sv;
257 scriptitem_T *script = SCRIPT_ITEM(sid);
258
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100259 // find name in "script"
260 // TODO: also find script-local user function
Bram Moolenaar08251752021-01-11 21:20:18 +0100261 idx = get_script_item_idx(sid, name, 0, cctx);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100262 if (idx >= 0)
263 {
264 sv = ((svar_T *)script->sn_var_vals.ga_data) + idx;
265 if (!sv->sv_export)
266 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200267 semsg(_(e_item_not_exported_in_script_str), name);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100268 return -1;
269 }
270 *type = sv->sv_type;
271 *ufunc = NULL;
272 }
273 else
274 {
275 char_u buffer[200];
276 char_u *funcname;
277
278 // it could be a user function.
Bram Moolenaar5a67c372020-07-23 14:39:47 +0200279 if (STRLEN(name) < sizeof(buffer) - 15)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100280 funcname = buffer;
281 else
282 {
Bram Moolenaar5a67c372020-07-23 14:39:47 +0200283 funcname = alloc(STRLEN(name) + 15);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100284 if (funcname == NULL)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100285 return -1;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100286 }
287 funcname[0] = K_SPECIAL;
288 funcname[1] = KS_EXTRA;
289 funcname[2] = (int)KE_SNR;
290 sprintf((char *)funcname + 3, "%ld_%s", (long)sid, name);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200291 *ufunc = find_func(funcname, FALSE, NULL);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100292 if (funcname != buffer)
293 vim_free(funcname);
294
295 if (*ufunc == NULL)
296 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200297 semsg(_(e_item_not_found_in_script_str), name);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100298 return -1;
299 }
300 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100301
302 return idx;
303}
304
305/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100306 * Handle an ":import" command and add the resulting imported_T to "gap", when
307 * not NULL, or script "import_sid" sn_imports.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200308 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100309 * Returns a pointer to after the command or NULL in case of failure
310 */
311 char_u *
Bram Moolenaar1c991142020-07-04 13:15:31 +0200312handle_import(
313 char_u *arg_start,
314 garray_T *gap,
315 int import_sid,
316 evalarg_T *evalarg,
317 void *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100318{
319 char_u *arg = arg_start;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200320 char_u *cmd_end = NULL;
321 char_u *as_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100322 int ret = FAIL;
323 typval_T tv;
324 int sid = -1;
325 int res;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200326 garray_T names;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100327
Bram Moolenaar1c991142020-07-04 13:15:31 +0200328 ga_init2(&names, sizeof(char_u *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100329 if (*arg == '{')
330 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200331 // "import {item, item} from ..."
332 arg = skipwhite_and_linebreak(arg + 1, evalarg);
333 for (;;)
334 {
335 char_u *p = arg;
336 int had_comma = FALSE;
337
338 while (eval_isnamec(*arg))
339 ++arg;
340 if (p == arg)
341 break;
342 if (ga_grow(&names, 1) == FAIL)
343 goto erret;
344 ((char_u **)names.ga_data)[names.ga_len] =
345 vim_strnsave(p, arg - p);
346 ++names.ga_len;
347 if (*arg == ',')
348 {
349 had_comma = TRUE;
350 ++arg;
351 }
352 arg = skipwhite_and_linebreak(arg, evalarg);
353 if (*arg == '}')
354 {
355 arg = skipwhite_and_linebreak(arg + 1, evalarg);
356 break;
357 }
358 if (!had_comma)
359 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200360 emsg(_(e_missing_comma_in_import));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200361 goto erret;
362 }
363 }
364 if (names.ga_len == 0)
365 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200366 emsg(_(e_syntax_error_in_import));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200367 goto erret;
368 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100369 }
370 else
371 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200372 // "import Name from ..."
373 // "import * as Name from ..."
374 // "import item [as Name] from ..."
375 arg = skipwhite_and_linebreak(arg, evalarg);
376 if (arg[0] == '*' && IS_WHITE_OR_NUL(arg[1]))
377 arg = skipwhite_and_linebreak(arg + 1, evalarg);
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100378 else if (eval_isnamec1(*arg))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100379 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200380 char_u *p = arg;
381
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100382 while (eval_isnamec(*arg))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100383 ++arg;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200384 if (ga_grow(&names, 1) == FAIL)
385 goto erret;
386 ((char_u **)names.ga_data)[names.ga_len] =
387 vim_strnsave(p, arg - p);
388 ++names.ga_len;
389 arg = skipwhite_and_linebreak(arg, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100390 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200391 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100392 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200393 emsg(_(e_syntax_error_in_import));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200394 goto erret;
395 }
396
397 if (STRNCMP("as", arg, 2) == 0 && IS_WHITE_OR_NUL(arg[2]))
398 {
399 char_u *p;
400
401 // skip over "as Name "; no line break allowed after "as"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100402 arg = skipwhite(arg + 2);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200403 p = arg;
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100404 if (eval_isnamec1(*arg))
405 while (eval_isnamec(*arg))
406 ++arg;
Bram Moolenaarcbb6bdc2020-07-06 21:53:17 +0200407 if (check_defined(p, arg - p, cctx) == FAIL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200408 goto erret;
409 as_name = vim_strnsave(p, arg - p);
410 arg = skipwhite_and_linebreak(arg, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100411 }
412 else if (*arg_start == '*')
413 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200414 emsg(_(e_missing_as_after_star));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200415 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100416 }
417 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200418
419 if (STRNCMP("from", arg, 4) != 0 || !IS_WHITE_OR_NUL(arg[4]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100420 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200421 emsg(_(e_missing_from));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200422 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100423 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200424
Bram Moolenaar962d7212020-07-04 14:15:00 +0200425 arg = skipwhite_and_linebreak(arg + 4, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100426 tv.v_type = VAR_UNKNOWN;
427 // TODO: should we accept any expression?
428 if (*arg == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +0200429 ret = eval_lit_string(&arg, &tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100430 else if (*arg == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +0200431 ret = eval_string(&arg, &tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100432 if (ret == FAIL || tv.vval.v_string == NULL || *tv.vval.v_string == NUL)
433 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200434 emsg(_(e_invalid_string_after_from));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200435 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100436 }
437 cmd_end = arg;
438
Bram Moolenaar1c991142020-07-04 13:15:31 +0200439 /*
440 * find script file
441 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100442 if (*tv.vval.v_string == '.')
443 {
444 size_t len;
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100445 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100446 char_u *tail = gettail(si->sn_name);
447 char_u *from_name;
448
449 // Relative to current script: "./name.vim", "../../name.vim".
450 len = STRLEN(si->sn_name) - STRLEN(tail) + STRLEN(tv.vval.v_string) + 2;
451 from_name = alloc((int)len);
452 if (from_name == NULL)
453 {
454 clear_tv(&tv);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200455 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100456 }
457 vim_strncpy(from_name, si->sn_name, tail - si->sn_name);
458 add_pathsep(from_name);
459 STRCAT(from_name, tv.vval.v_string);
460 simplify_filename(from_name);
461
462 res = do_source(from_name, FALSE, DOSO_NONE, &sid);
463 vim_free(from_name);
464 }
465 else if (mch_isFullName(tv.vval.v_string))
466 {
467 // Absolute path: "/tmp/name.vim"
468 res = do_source(tv.vval.v_string, FALSE, DOSO_NONE, &sid);
469 }
470 else
471 {
472 size_t len = 7 + STRLEN(tv.vval.v_string) + 1;
473 char_u *from_name;
474
475 // Find file in "import" subdirs in 'runtimepath'.
476 from_name = alloc((int)len);
477 if (from_name == NULL)
478 {
479 clear_tv(&tv);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200480 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100481 }
482 vim_snprintf((char *)from_name, len, "import/%s", tv.vval.v_string);
483 res = source_in_path(p_rtp, from_name, DIP_NOAFTER, &sid);
484 vim_free(from_name);
485 }
486
487 if (res == FAIL || sid <= 0)
488 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200489 semsg(_(e_could_not_import_str), tv.vval.v_string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100490 clear_tv(&tv);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200491 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100492 }
493 clear_tv(&tv);
494
495 if (*arg_start == '*')
496 {
Bram Moolenaara6294952020-12-27 13:39:50 +0100497 imported_T *imported;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100498
Bram Moolenaara6294952020-12-27 13:39:50 +0100499 imported = find_imported(as_name, STRLEN(as_name), cctx);
500 if (imported != NULL && imported->imp_sid == sid)
501 {
502 if (imported->imp_flags & IMP_FLAGS_RELOAD)
503 // import already defined on a previous script load
504 imported->imp_flags &= ~IMP_FLAGS_RELOAD;
505 else
506 {
507 semsg(_(e_name_already_defined_str), as_name);
508 goto erret;
509 }
510 }
511
512 imported = new_imported(gap != NULL ? gap
513 : &SCRIPT_ITEM(import_sid)->sn_imports);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100514 if (imported == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200515 goto erret;
516 imported->imp_name = as_name;
517 as_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100518 imported->imp_sid = sid;
Bram Moolenaara6294952020-12-27 13:39:50 +0100519 imported->imp_flags = IMP_FLAGS_STAR;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100520 }
521 else
522 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200523 int i;
524
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100525 arg = arg_start;
526 if (*arg == '{')
527 arg = skipwhite(arg + 1);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200528 for (i = 0; i < names.ga_len; ++i)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100529 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200530 char_u *name = ((char_u **)names.ga_data)[i];
Bram Moolenaara6294952020-12-27 13:39:50 +0100531 size_t len = STRLEN(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100532 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100533 imported_T *imported;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100534 ufunc_T *ufunc = NULL;
535 type_T *type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100536
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200537 idx = find_exported(sid, name, &ufunc, &type, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100538
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100539 if (idx < 0 && ufunc == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200540 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100541
Bram Moolenaara6294952020-12-27 13:39:50 +0100542 // If already imported with the same propertis and the
543 // IMP_FLAGS_RELOAD set then we keep that entry. Otherwise create
544 // a new one (and give an error for an existing import).
545 imported = find_imported(name, len, cctx);
546 if (imported != NULL
547 && (imported->imp_flags & IMP_FLAGS_RELOAD)
548 && imported->imp_sid == sid
549 && (idx >= 0
550 ? (equal_type(imported->imp_type, type)
551 && imported->imp_var_vals_idx == idx)
552 : (equal_type(imported->imp_type, ufunc->uf_func_type)
553 && STRCMP(imported->imp_funcname,
554 ufunc->uf_name) == 0)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100555 {
Bram Moolenaara6294952020-12-27 13:39:50 +0100556 imported->imp_flags &= ~IMP_FLAGS_RELOAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100557 }
558 else
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200559 {
Bram Moolenaara6294952020-12-27 13:39:50 +0100560 if (check_defined(name, len, cctx) == FAIL)
561 goto erret;
562
563 imported = new_imported(gap != NULL ? gap
564 : &SCRIPT_ITEM(import_sid)->sn_imports);
565 if (imported == NULL)
566 goto erret;
567
568 // TODO: check for "as" following
569 // imported->imp_name = vim_strsave(as_name);
570 imported->imp_name = name;
571 ((char_u **)names.ga_data)[i] = NULL;
572 imported->imp_sid = sid;
573 if (idx >= 0)
574 {
575 imported->imp_type = type;
576 imported->imp_var_vals_idx = idx;
577 }
578 else
579 {
580 imported->imp_type = ufunc->uf_func_type;
581 imported->imp_funcname = ufunc->uf_name;
582 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200583 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100584 }
585 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200586erret:
587 ga_clear_strings(&names);
588 vim_free(as_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100589 return cmd_end;
590}
591
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200592/*
593 * Declare a script-local variable without init: "let var: type".
594 * "const" is an error since the value is missing.
595 * Returns a pointer to after the type.
596 */
597 char_u *
598vim9_declare_scriptvar(exarg_T *eap, char_u *arg)
599{
600 char_u *p;
601 char_u *name;
602 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
603 type_T *type;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200604 typval_T init_tv;
605
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200606 if (eap->cmdidx == CMD_final || eap->cmdidx == CMD_const)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200607 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200608 if (eap->cmdidx == CMD_final)
609 emsg(_(e_final_requires_a_value));
610 else
611 emsg(_(e_const_requires_a_value));
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200612 return arg + STRLEN(arg);
613 }
614
615 // Check for valid starting character.
616 if (!eval_isnamec1(*arg))
617 {
618 semsg(_(e_invarg2), arg);
619 return arg + STRLEN(arg);
620 }
621
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200622 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
Bram Moolenaar3b74b6b2020-06-19 19:01:43 +0200623 if (*p == ':' && (VIM_ISWHITE(p[1]) || p != arg + 1))
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200624 break;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200625
626 if (*p != ':')
627 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +0200628 emsg(_(e_type_or_initialization_required));
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200629 return arg + STRLEN(arg);
630 }
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200631 if (!VIM_ISWHITE(p[1]))
632 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100633 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200634 return arg + STRLEN(arg);
635 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200636 name = vim_strnsave(arg, p - arg);
637
638 // parse type
639 p = skipwhite(p + 1);
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100640 type = parse_type(&p, &si->sn_type_list, TRUE);
641 if (type == NULL)
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200642 {
643 vim_free(name);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200644 return p;
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200645 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200646
647 // Create the variable with 0/NULL value.
648 CLEAR_FIELD(init_tv);
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +0200649 if (type->tt_type == VAR_ANY)
650 // A variable of type "any" is not possible, just use zero instead
651 init_tv.v_type = VAR_NUMBER;
652 else
653 init_tv.v_type = type->tt_type;
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100654 set_var_const(name, type, &init_tv, FALSE, 0, 0);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200655
656 vim_free(name);
657 return p;
658}
659
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200660/*
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200661 * Vim9 part of adding a script variable: add it to sn_all_vars (lookup by name
662 * with a hashtable) and sn_var_vals (lookup by index).
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100663 * When "create" is TRUE this is a new variable, otherwise find and update an
664 * existing variable.
Bram Moolenaar08251752021-01-11 21:20:18 +0100665 * "flags" can have ASSIGN_FINAL or ASSIGN_CONST.
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100666 * When "*type" is NULL use "tv" for the type and update "*type".
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200667 */
668 void
Bram Moolenaar08251752021-01-11 21:20:18 +0100669update_vim9_script_var(
670 int create,
671 dictitem_T *di,
672 int flags,
673 typval_T *tv,
674 type_T **type)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200675{
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100676 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
677 hashitem_T *hi;
678 svar_T *sv;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200679
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100680 if (create)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200681 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100682 sallvar_T *newsav;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200683
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100684 // Store a pointer to the typval_T, so that it can be found by index
685 // instead of using a hastab lookup.
686 if (ga_grow(&si->sn_var_vals, 1) == FAIL)
687 return;
688
689 sv = ((svar_T *)si->sn_var_vals.ga_data) + si->sn_var_vals.ga_len;
690 newsav = (sallvar_T *)alloc_clear(
691 sizeof(sallvar_T) + STRLEN(di->di_key));
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200692 if (newsav == NULL)
693 return;
694
695 sv->sv_tv = &di->di_tv;
Bram Moolenaar08251752021-01-11 21:20:18 +0100696 sv->sv_const = (flags & ASSIGN_FINAL) ? ASSIGN_FINAL
697 : (flags & ASSIGN_CONST) ? ASSIGN_CONST : 0;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200698 sv->sv_export = is_export;
699 newsav->sav_var_vals_idx = si->sn_var_vals.ga_len;
700 ++si->sn_var_vals.ga_len;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200701 STRCPY(&newsav->sav_key, di->di_key);
702 sv->sv_name = newsav->sav_key;
703 newsav->sav_di = di;
704 newsav->sav_block_id = si->sn_current_block_id;
705
706 hi = hash_find(&si->sn_all_vars.dv_hashtab, newsav->sav_key);
707 if (!HASHITEM_EMPTY(hi))
708 {
709 sallvar_T *sav = HI2SAV(hi);
710
711 // variable with this name exists in another block
712 while (sav->sav_next != NULL)
713 sav = sav->sav_next;
714 sav->sav_next = newsav;
715 }
716 else
717 // new variable name
718 hash_add(&si->sn_all_vars.dv_hashtab, newsav->sav_key);
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200719 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100720 else
721 {
722 sv = find_typval_in_script(&di->di_tv);
723 }
724 if (sv != NULL)
725 {
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100726 if (*type == NULL)
727 *type = typval2type(tv, &si->sn_type_list);
728 sv->sv_type = *type;
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100729 }
730
731 // let ex_export() know the export worked.
732 is_export = FALSE;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200733}
734
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200735/*
736 * Hide a script variable when leaving a block.
737 * "idx" is de index in sn_var_vals.
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200738 * When "func_defined" is non-zero then a function was defined in this block,
739 * the variable may be accessed by it. Otherwise the variable can be cleared.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200740 */
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200741 void
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200742hide_script_var(scriptitem_T *si, int idx, int func_defined)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200743{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200744 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200745 hashtab_T *script_ht = get_script_local_ht();
746 hashtab_T *all_ht = &si->sn_all_vars.dv_hashtab;
747 hashitem_T *script_hi;
748 hashitem_T *all_hi;
749
750 // Remove a variable declared inside the block, if it still exists.
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200751 // If it was added in a nested block it will already have been removed.
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200752 // The typval is moved into the sallvar_T.
753 script_hi = hash_find(script_ht, sv->sv_name);
754 all_hi = hash_find(all_ht, sv->sv_name);
755 if (!HASHITEM_EMPTY(script_hi) && !HASHITEM_EMPTY(all_hi))
756 {
757 dictitem_T *di = HI2DI(script_hi);
758 sallvar_T *sav = HI2SAV(all_hi);
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200759 sallvar_T *sav_prev = NULL;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200760
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200761 // There can be multiple entries with the same name in different
762 // blocks, find the right one.
763 while (sav != NULL && sav->sav_var_vals_idx != idx)
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200764 {
765 sav_prev = sav;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200766 sav = sav->sav_next;
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200767 }
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200768 if (sav != NULL)
769 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200770 if (func_defined)
771 {
772 // move the typval from the dictitem to the sallvar
773 sav->sav_tv = di->di_tv;
774 di->di_tv.v_type = VAR_UNKNOWN;
775 sav->sav_flags = di->di_flags;
776 sav->sav_di = NULL;
777 sv->sv_tv = &sav->sav_tv;
778 }
779 else
780 {
781 if (sav_prev == NULL)
782 hash_remove(all_ht, all_hi);
783 else
784 sav_prev->sav_next = sav->sav_next;
785 sv->sv_name = NULL;
786 vim_free(sav);
787 }
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200788 delete_var(script_ht, script_hi);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200789 }
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200790 }
791}
792
793/*
794 * Free the script variables from "sn_all_vars".
795 */
796 void
797free_all_script_vars(scriptitem_T *si)
798{
799 int todo;
800 hashtab_T *ht = &si->sn_all_vars.dv_hashtab;
801 hashitem_T *hi;
802 sallvar_T *sav;
803 sallvar_T *sav_next;
804
805 hash_lock(ht);
806 todo = (int)ht->ht_used;
807 for (hi = ht->ht_array; todo > 0; ++hi)
808 {
809 if (!HASHITEM_EMPTY(hi))
810 {
811 --todo;
812
813 // Free the variable. Don't remove it from the hashtab, ht_array
814 // might change then. hash_clear() takes care of it later.
815 sav = HI2SAV(hi);
816 while (sav != NULL)
817 {
818 sav_next = sav->sav_next;
819 if (sav->sav_di == NULL)
820 clear_tv(&sav->sav_tv);
821 vim_free(sav);
822 sav = sav_next;
823 }
824 }
825 }
826 hash_clear(ht);
827 hash_init(ht);
828
829 ga_clear(&si->sn_var_vals);
Bram Moolenaar2b327002020-12-26 15:39:31 +0100830
831 // existing commands using script variable indexes are no longer valid
832 si->sn_script_seq = current_sctx.sc_seq;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200833}
834
835/*
Bram Moolenaar10c65862020-10-08 21:16:42 +0200836 * Find the script-local variable that links to "dest".
837 * Returns NULL if not found.
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200838 */
Bram Moolenaar10c65862020-10-08 21:16:42 +0200839 svar_T *
840find_typval_in_script(typval_T *dest)
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200841{
842 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
843 int idx;
844
Bram Moolenaar81e17fb2020-08-21 21:55:43 +0200845 if (si->sn_version != SCRIPT_VERSION_VIM9)
846 // legacy script doesn't store variable types
Bram Moolenaar10c65862020-10-08 21:16:42 +0200847 return NULL;
Bram Moolenaar81e17fb2020-08-21 21:55:43 +0200848
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200849 // Find the svar_T in sn_var_vals.
850 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
851 {
852 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
853
Bram Moolenaarc0913d02020-12-05 14:44:37 +0100854 // If "sv_name" is NULL the variable was hidden when leaving a block,
855 // don't check "sv_tv" then, it might be used for another variable now.
856 if (sv->sv_name != NULL && sv->sv_tv == dest)
Bram Moolenaar10c65862020-10-08 21:16:42 +0200857 return sv;
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200858 }
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100859 iemsg("find_typval_in_script(): not found");
Bram Moolenaar10c65862020-10-08 21:16:42 +0200860 return NULL;
861}
862
863/*
864 * Check if the type of script variable "dest" allows assigning "value".
865 * If needed convert "value" to a bool.
866 */
867 int
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100868check_script_var_type(
869 typval_T *dest,
870 typval_T *value,
871 char_u *name,
872 where_T where)
Bram Moolenaar10c65862020-10-08 21:16:42 +0200873{
874 svar_T *sv = find_typval_in_script(dest);
875 int ret;
876
877 if (sv != NULL)
878 {
Bram Moolenaar08251752021-01-11 21:20:18 +0100879 if (sv->sv_const != 0)
Bram Moolenaar10c65862020-10-08 21:16:42 +0200880 {
881 semsg(_(e_readonlyvar), name);
882 return FAIL;
883 }
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100884 ret = check_typval_type(sv->sv_type, value, where);
Bram Moolenaar10c65862020-10-08 21:16:42 +0200885 if (ret == OK && need_convert_to_bool(sv->sv_type, value))
886 {
887 int val = tv2bool(value);
888
889 clear_tv(value);
890 value->v_type = VAR_BOOL;
891 value->v_lock = 0;
892 value->vval.v_number = val ? VVAL_TRUE : VVAL_FALSE;
893 }
894 return ret;
895 }
896
Bram Moolenaarc785b9a2020-06-19 18:34:15 +0200897 return OK; // not really
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200898}
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200899
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100900#endif // FEAT_EVAL