blob: a2f8ff932b6e19f4fd29a591d8b99f26627bc107 [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 Moolenaar8a7d6542020-01-26 15:56:19 +010026 return current_sctx.sc_version == SCRIPT_VERSION_VIM9;
27}
28
29/*
30 * ":vim9script".
31 */
32 void
Bram Moolenaar9b8d6222020-12-28 18:26:00 +010033ex_vim9script(exarg_T *eap UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010034{
Bram Moolenaar9b8d6222020-12-28 18:26:00 +010035#ifdef FEAT_EVAL
Bram Moolenaar2b327002020-12-26 15:39:31 +010036 int sid = current_sctx.sc_sid;
Bram Moolenaar101f4812020-06-16 23:18:51 +020037 scriptitem_T *si;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010038
39 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
40 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +020041 emsg(_(e_vim9script_can_only_be_used_in_script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010042 return;
43 }
Bram Moolenaar2b327002020-12-26 15:39:31 +010044
45 si = SCRIPT_ITEM(sid);
46 if (si->sn_state == SN_STATE_HAD_COMMAND)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010047 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +020048 emsg(_(e_vim9script_must_be_first_command_in_script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010049 return;
50 }
Bram Moolenaar2b327002020-12-26 15:39:31 +010051 if (!IS_WHITE_OR_NUL(*eap->arg) && STRCMP(eap->arg, "noclear") != 0)
52 {
53 semsg(_(e_invarg2), eap->arg);
54 return;
55 }
56 if (si->sn_state == SN_STATE_RELOAD && IS_WHITE_OR_NUL(*eap->arg))
57 {
58 hashtab_T *ht = &SCRIPT_VARS(sid);
59
60 // Reloading a script without the "noclear" argument: clear
61 // script-local variables and functions.
62 hashtab_free_contents(ht);
63 hash_init(ht);
64 delete_script_functions(sid);
65
66 // old imports and script variables are no longer valid
67 free_imports_and_script_vars(sid);
68 }
69 si->sn_state = SN_STATE_HAD_COMMAND;
70
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010071 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
72 si->sn_version = SCRIPT_VERSION_VIM9;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010073
74 if (STRCMP(p_cpo, CPO_VIM) != 0)
75 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +020076 si->sn_save_cpo = vim_strsave(p_cpo);
77 set_option_value((char_u *)"cpo", 0L, (char_u *)CPO_VIM, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010078 }
Bram Moolenaar9b8d6222020-12-28 18:26:00 +010079#else
80 // No check for this being the first command, it doesn't matter.
81 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
82#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010083}
84
85/*
Bram Moolenaarae616492020-07-28 20:07:27 +020086 * When in Vim9 script give an error and return FAIL.
87 */
88 int
89not_in_vim9(exarg_T *eap)
90{
Bram Moolenaar68e30442020-07-28 21:15:07 +020091 if (in_vim9script())
92 switch (eap->cmdidx)
93 {
Bram Moolenaar68e30442020-07-28 21:15:07 +020094 case CMD_append:
95 case CMD_change:
Bram Moolenaarf5a48012020-08-01 17:00:03 +020096 case CMD_insert:
97 case CMD_t:
Bram Moolenaar68e30442020-07-28 21:15:07 +020098 case CMD_xit:
Bram Moolenaar9b8d6222020-12-28 18:26:00 +010099 semsg(_(e_command_not_supported_in_vim9_script_missing_var_str), eap->cmd);
Bram Moolenaar68e30442020-07-28 21:15:07 +0200100 return FAIL;
101 default: break;
102 }
Bram Moolenaarae616492020-07-28 20:07:27 +0200103 return OK;
104}
105
Bram Moolenaar9b8d6222020-12-28 18:26:00 +0100106#if defined(FEAT_EVAL) || defined(PROTO)
107
Bram Moolenaarae616492020-07-28 20:07:27 +0200108/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100109 * ":export let Name: type"
110 * ":export const Name: type"
111 * ":export def Name(..."
112 * ":export class Name ..."
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100113 */
114 void
Bram Moolenaar09689a02020-05-09 22:50:08 +0200115ex_export(exarg_T *eap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100116{
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200117 if (!in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100118 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200119 emsg(_(e_export_can_only_be_used_in_vim9script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100120 return;
121 }
122
123 eap->cmd = eap->arg;
124 (void)find_ex_command(eap, NULL, lookup_scriptvar, NULL);
125 switch (eap->cmdidx)
126 {
127 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200128 case CMD_var:
129 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100130 case CMD_const:
131 case CMD_def:
132 // case CMD_class:
133 is_export = TRUE;
134 do_cmdline(eap->cmd, eap->getline, eap->cookie,
135 DOCMD_VERBOSE + DOCMD_NOWAIT);
136
137 // The command will reset "is_export" when exporting an item.
138 if (is_export)
139 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200140 emsg(_(e_export_with_invalid_argument));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100141 is_export = FALSE;
142 }
143 break;
144 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200145 emsg(_(e_invalid_command_after_export));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100146 break;
147 }
148}
149
150/*
151 * Add a new imported item entry to the current script.
152 */
153 static imported_T *
154new_imported(garray_T *gap)
155{
156 if (ga_grow(gap, 1) == OK)
157 return ((imported_T *)gap->ga_data + gap->ga_len++);
158 return NULL;
159}
160
161/*
162 * Free all imported items in script "sid".
163 */
164 void
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200165free_imports_and_script_vars(int sid)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100166{
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100167 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100168 int idx;
169
170 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
171 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100172 imported_T *imp = ((imported_T *)si->sn_imports.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100173
174 vim_free(imp->imp_name);
175 }
176 ga_clear(&si->sn_imports);
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200177
178 free_all_script_vars(si);
179
Bram Moolenaar6110e792020-07-08 19:35:21 +0200180 clear_type_list(&si->sn_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100181}
182
183/*
Bram Moolenaara6294952020-12-27 13:39:50 +0100184 * Mark all imports as possible to redefine. Used when a script is loaded
185 * again but not cleared.
186 */
187 void
188mark_imports_for_reload(int sid)
189{
190 scriptitem_T *si = SCRIPT_ITEM(sid);
191 int idx;
192
193 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
194 {
195 imported_T *imp = ((imported_T *)si->sn_imports.ga_data) + idx;
196
197 imp->imp_flags |= IMP_FLAGS_RELOAD;
198 }
199}
200
201/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100202 * ":import Item from 'filename'"
203 * ":import Item as Alias from 'filename'"
204 * ":import {Item} from 'filename'".
205 * ":import {Item as Alias} from 'filename'"
206 * ":import {Item, Item} from 'filename'"
207 * ":import {Item, Item as Alias} from 'filename'"
208 *
209 * ":import * as Name from 'filename'"
210 */
211 void
212ex_import(exarg_T *eap)
213{
Bram Moolenaar1c991142020-07-04 13:15:31 +0200214 char_u *cmd_end;
215 evalarg_T evalarg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100216
Bram Moolenaar101f4812020-06-16 23:18:51 +0200217 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
218 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200219 emsg(_(e_import_can_only_be_used_in_script));
Bram Moolenaar101f4812020-06-16 23:18:51 +0200220 return;
221 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200222 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar101f4812020-06-16 23:18:51 +0200223
Bram Moolenaar1c991142020-07-04 13:15:31 +0200224 cmd_end = handle_import(eap->arg, NULL, current_sctx.sc_sid,
225 &evalarg, NULL);
Bram Moolenaar9721fb42020-06-11 23:10:46 +0200226 if (cmd_end != NULL)
227 eap->nextcmd = check_nextcmd(cmd_end);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200228 clear_evalarg(&evalarg, eap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100229}
230
231/*
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100232 * Find an exported item in "sid" matching the name at "*argp".
233 * When it is a variable return the index.
234 * When it is a user function return "*ufunc".
235 * When not found returns -1 and "*ufunc" is NULL.
236 */
237 int
238find_exported(
239 int sid,
Bram Moolenaar1c991142020-07-04 13:15:31 +0200240 char_u *name,
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100241 ufunc_T **ufunc,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200242 type_T **type,
243 cctx_T *cctx)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100244{
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100245 int idx = -1;
246 svar_T *sv;
247 scriptitem_T *script = SCRIPT_ITEM(sid);
248
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100249 // find name in "script"
250 // TODO: also find script-local user function
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200251 idx = get_script_item_idx(sid, name, FALSE, cctx);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100252 if (idx >= 0)
253 {
254 sv = ((svar_T *)script->sn_var_vals.ga_data) + idx;
255 if (!sv->sv_export)
256 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200257 semsg(_(e_item_not_exported_in_script_str), name);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100258 return -1;
259 }
260 *type = sv->sv_type;
261 *ufunc = NULL;
262 }
263 else
264 {
265 char_u buffer[200];
266 char_u *funcname;
267
268 // it could be a user function.
Bram Moolenaar5a67c372020-07-23 14:39:47 +0200269 if (STRLEN(name) < sizeof(buffer) - 15)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100270 funcname = buffer;
271 else
272 {
Bram Moolenaar5a67c372020-07-23 14:39:47 +0200273 funcname = alloc(STRLEN(name) + 15);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100274 if (funcname == NULL)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100275 return -1;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100276 }
277 funcname[0] = K_SPECIAL;
278 funcname[1] = KS_EXTRA;
279 funcname[2] = (int)KE_SNR;
280 sprintf((char *)funcname + 3, "%ld_%s", (long)sid, name);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200281 *ufunc = find_func(funcname, FALSE, NULL);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100282 if (funcname != buffer)
283 vim_free(funcname);
284
285 if (*ufunc == NULL)
286 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200287 semsg(_(e_item_not_found_in_script_str), name);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100288 return -1;
289 }
290 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100291
292 return idx;
293}
294
295/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100296 * Handle an ":import" command and add the resulting imported_T to "gap", when
297 * not NULL, or script "import_sid" sn_imports.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200298 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100299 * Returns a pointer to after the command or NULL in case of failure
300 */
301 char_u *
Bram Moolenaar1c991142020-07-04 13:15:31 +0200302handle_import(
303 char_u *arg_start,
304 garray_T *gap,
305 int import_sid,
306 evalarg_T *evalarg,
307 void *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100308{
309 char_u *arg = arg_start;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200310 char_u *cmd_end = NULL;
311 char_u *as_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100312 int ret = FAIL;
313 typval_T tv;
314 int sid = -1;
315 int res;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200316 garray_T names;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100317
Bram Moolenaar1c991142020-07-04 13:15:31 +0200318 ga_init2(&names, sizeof(char_u *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100319 if (*arg == '{')
320 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200321 // "import {item, item} from ..."
322 arg = skipwhite_and_linebreak(arg + 1, evalarg);
323 for (;;)
324 {
325 char_u *p = arg;
326 int had_comma = FALSE;
327
328 while (eval_isnamec(*arg))
329 ++arg;
330 if (p == arg)
331 break;
332 if (ga_grow(&names, 1) == FAIL)
333 goto erret;
334 ((char_u **)names.ga_data)[names.ga_len] =
335 vim_strnsave(p, arg - p);
336 ++names.ga_len;
337 if (*arg == ',')
338 {
339 had_comma = TRUE;
340 ++arg;
341 }
342 arg = skipwhite_and_linebreak(arg, evalarg);
343 if (*arg == '}')
344 {
345 arg = skipwhite_and_linebreak(arg + 1, evalarg);
346 break;
347 }
348 if (!had_comma)
349 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200350 emsg(_(e_missing_comma_in_import));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200351 goto erret;
352 }
353 }
354 if (names.ga_len == 0)
355 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200356 emsg(_(e_syntax_error_in_import));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200357 goto erret;
358 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100359 }
360 else
361 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200362 // "import Name from ..."
363 // "import * as Name from ..."
364 // "import item [as Name] from ..."
365 arg = skipwhite_and_linebreak(arg, evalarg);
366 if (arg[0] == '*' && IS_WHITE_OR_NUL(arg[1]))
367 arg = skipwhite_and_linebreak(arg + 1, evalarg);
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100368 else if (eval_isnamec1(*arg))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100369 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200370 char_u *p = arg;
371
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100372 while (eval_isnamec(*arg))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100373 ++arg;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200374 if (ga_grow(&names, 1) == FAIL)
375 goto erret;
376 ((char_u **)names.ga_data)[names.ga_len] =
377 vim_strnsave(p, arg - p);
378 ++names.ga_len;
379 arg = skipwhite_and_linebreak(arg, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100380 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200381 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100382 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200383 emsg(_(e_syntax_error_in_import));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200384 goto erret;
385 }
386
387 if (STRNCMP("as", arg, 2) == 0 && IS_WHITE_OR_NUL(arg[2]))
388 {
389 char_u *p;
390
391 // skip over "as Name "; no line break allowed after "as"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100392 arg = skipwhite(arg + 2);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200393 p = arg;
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100394 if (eval_isnamec1(*arg))
395 while (eval_isnamec(*arg))
396 ++arg;
Bram Moolenaarcbb6bdc2020-07-06 21:53:17 +0200397 if (check_defined(p, arg - p, cctx) == FAIL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200398 goto erret;
399 as_name = vim_strnsave(p, arg - p);
400 arg = skipwhite_and_linebreak(arg, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100401 }
402 else if (*arg_start == '*')
403 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200404 emsg(_(e_missing_as_after_star));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200405 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100406 }
407 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200408
409 if (STRNCMP("from", arg, 4) != 0 || !IS_WHITE_OR_NUL(arg[4]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100410 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200411 emsg(_(e_missing_from));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200412 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100413 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200414
Bram Moolenaar962d7212020-07-04 14:15:00 +0200415 arg = skipwhite_and_linebreak(arg + 4, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100416 tv.v_type = VAR_UNKNOWN;
417 // TODO: should we accept any expression?
418 if (*arg == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +0200419 ret = eval_lit_string(&arg, &tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100420 else if (*arg == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +0200421 ret = eval_string(&arg, &tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100422 if (ret == FAIL || tv.vval.v_string == NULL || *tv.vval.v_string == NUL)
423 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200424 emsg(_(e_invalid_string_after_from));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200425 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100426 }
427 cmd_end = arg;
428
Bram Moolenaar1c991142020-07-04 13:15:31 +0200429 /*
430 * find script file
431 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100432 if (*tv.vval.v_string == '.')
433 {
434 size_t len;
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100435 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100436 char_u *tail = gettail(si->sn_name);
437 char_u *from_name;
438
439 // Relative to current script: "./name.vim", "../../name.vim".
440 len = STRLEN(si->sn_name) - STRLEN(tail) + STRLEN(tv.vval.v_string) + 2;
441 from_name = alloc((int)len);
442 if (from_name == NULL)
443 {
444 clear_tv(&tv);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200445 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100446 }
447 vim_strncpy(from_name, si->sn_name, tail - si->sn_name);
448 add_pathsep(from_name);
449 STRCAT(from_name, tv.vval.v_string);
450 simplify_filename(from_name);
451
452 res = do_source(from_name, FALSE, DOSO_NONE, &sid);
453 vim_free(from_name);
454 }
455 else if (mch_isFullName(tv.vval.v_string))
456 {
457 // Absolute path: "/tmp/name.vim"
458 res = do_source(tv.vval.v_string, FALSE, DOSO_NONE, &sid);
459 }
460 else
461 {
462 size_t len = 7 + STRLEN(tv.vval.v_string) + 1;
463 char_u *from_name;
464
465 // Find file in "import" subdirs in 'runtimepath'.
466 from_name = alloc((int)len);
467 if (from_name == NULL)
468 {
469 clear_tv(&tv);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200470 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100471 }
472 vim_snprintf((char *)from_name, len, "import/%s", tv.vval.v_string);
473 res = source_in_path(p_rtp, from_name, DIP_NOAFTER, &sid);
474 vim_free(from_name);
475 }
476
477 if (res == FAIL || sid <= 0)
478 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200479 semsg(_(e_could_not_import_str), tv.vval.v_string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100480 clear_tv(&tv);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200481 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100482 }
483 clear_tv(&tv);
484
485 if (*arg_start == '*')
486 {
Bram Moolenaara6294952020-12-27 13:39:50 +0100487 imported_T *imported;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100488
Bram Moolenaara6294952020-12-27 13:39:50 +0100489 imported = find_imported(as_name, STRLEN(as_name), cctx);
490 if (imported != NULL && imported->imp_sid == sid)
491 {
492 if (imported->imp_flags & IMP_FLAGS_RELOAD)
493 // import already defined on a previous script load
494 imported->imp_flags &= ~IMP_FLAGS_RELOAD;
495 else
496 {
497 semsg(_(e_name_already_defined_str), as_name);
498 goto erret;
499 }
500 }
501
502 imported = new_imported(gap != NULL ? gap
503 : &SCRIPT_ITEM(import_sid)->sn_imports);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100504 if (imported == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200505 goto erret;
506 imported->imp_name = as_name;
507 as_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100508 imported->imp_sid = sid;
Bram Moolenaara6294952020-12-27 13:39:50 +0100509 imported->imp_flags = IMP_FLAGS_STAR;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100510 }
511 else
512 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200513 int i;
514
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100515 arg = arg_start;
516 if (*arg == '{')
517 arg = skipwhite(arg + 1);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200518 for (i = 0; i < names.ga_len; ++i)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100519 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200520 char_u *name = ((char_u **)names.ga_data)[i];
Bram Moolenaara6294952020-12-27 13:39:50 +0100521 size_t len = STRLEN(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100522 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100523 imported_T *imported;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100524 ufunc_T *ufunc = NULL;
525 type_T *type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100526
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200527 idx = find_exported(sid, name, &ufunc, &type, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100528
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100529 if (idx < 0 && ufunc == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200530 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100531
Bram Moolenaara6294952020-12-27 13:39:50 +0100532 // If already imported with the same propertis and the
533 // IMP_FLAGS_RELOAD set then we keep that entry. Otherwise create
534 // a new one (and give an error for an existing import).
535 imported = find_imported(name, len, cctx);
536 if (imported != NULL
537 && (imported->imp_flags & IMP_FLAGS_RELOAD)
538 && imported->imp_sid == sid
539 && (idx >= 0
540 ? (equal_type(imported->imp_type, type)
541 && imported->imp_var_vals_idx == idx)
542 : (equal_type(imported->imp_type, ufunc->uf_func_type)
543 && STRCMP(imported->imp_funcname,
544 ufunc->uf_name) == 0)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100545 {
Bram Moolenaara6294952020-12-27 13:39:50 +0100546 imported->imp_flags &= ~IMP_FLAGS_RELOAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100547 }
548 else
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200549 {
Bram Moolenaara6294952020-12-27 13:39:50 +0100550 if (check_defined(name, len, cctx) == FAIL)
551 goto erret;
552
553 imported = new_imported(gap != NULL ? gap
554 : &SCRIPT_ITEM(import_sid)->sn_imports);
555 if (imported == NULL)
556 goto erret;
557
558 // TODO: check for "as" following
559 // imported->imp_name = vim_strsave(as_name);
560 imported->imp_name = name;
561 ((char_u **)names.ga_data)[i] = NULL;
562 imported->imp_sid = sid;
563 if (idx >= 0)
564 {
565 imported->imp_type = type;
566 imported->imp_var_vals_idx = idx;
567 }
568 else
569 {
570 imported->imp_type = ufunc->uf_func_type;
571 imported->imp_funcname = ufunc->uf_name;
572 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200573 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100574 }
575 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200576erret:
577 ga_clear_strings(&names);
578 vim_free(as_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100579 return cmd_end;
580}
581
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200582/*
583 * Declare a script-local variable without init: "let var: type".
584 * "const" is an error since the value is missing.
585 * Returns a pointer to after the type.
586 */
587 char_u *
588vim9_declare_scriptvar(exarg_T *eap, char_u *arg)
589{
590 char_u *p;
591 char_u *name;
592 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
593 type_T *type;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200594 typval_T init_tv;
595
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200596 if (eap->cmdidx == CMD_final || eap->cmdidx == CMD_const)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200597 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200598 if (eap->cmdidx == CMD_final)
599 emsg(_(e_final_requires_a_value));
600 else
601 emsg(_(e_const_requires_a_value));
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200602 return arg + STRLEN(arg);
603 }
604
605 // Check for valid starting character.
606 if (!eval_isnamec1(*arg))
607 {
608 semsg(_(e_invarg2), arg);
609 return arg + STRLEN(arg);
610 }
611
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200612 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
Bram Moolenaar3b74b6b2020-06-19 19:01:43 +0200613 if (*p == ':' && (VIM_ISWHITE(p[1]) || p != arg + 1))
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200614 break;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200615
616 if (*p != ':')
617 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +0200618 emsg(_(e_type_or_initialization_required));
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200619 return arg + STRLEN(arg);
620 }
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200621 if (!VIM_ISWHITE(p[1]))
622 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200623 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200624 return arg + STRLEN(arg);
625 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200626 name = vim_strnsave(arg, p - arg);
627
628 // parse type
629 p = skipwhite(p + 1);
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100630 type = parse_type(&p, &si->sn_type_list, TRUE);
631 if (type == NULL)
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200632 {
633 vim_free(name);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200634 return p;
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200635 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200636
637 // Create the variable with 0/NULL value.
638 CLEAR_FIELD(init_tv);
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +0200639 if (type->tt_type == VAR_ANY)
640 // A variable of type "any" is not possible, just use zero instead
641 init_tv.v_type = VAR_NUMBER;
642 else
643 init_tv.v_type = type->tt_type;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200644 set_var_const(name, type, &init_tv, FALSE, 0);
645
646 vim_free(name);
647 return p;
648}
649
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200650/*
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200651 * Vim9 part of adding a script variable: add it to sn_all_vars (lookup by name
652 * with a hashtable) and sn_var_vals (lookup by index).
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100653 * When "create" is TRUE this is a new variable, otherwise find and update an
654 * existing variable.
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200655 * When "type" is NULL use "tv" for the type.
656 */
657 void
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100658update_vim9_script_var(int create, dictitem_T *di, typval_T *tv, type_T *type)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200659{
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100660 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
661 hashitem_T *hi;
662 svar_T *sv;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200663
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100664 if (create)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200665 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100666 sallvar_T *newsav;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200667
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100668 // Store a pointer to the typval_T, so that it can be found by index
669 // instead of using a hastab lookup.
670 if (ga_grow(&si->sn_var_vals, 1) == FAIL)
671 return;
672
673 sv = ((svar_T *)si->sn_var_vals.ga_data) + si->sn_var_vals.ga_len;
674 newsav = (sallvar_T *)alloc_clear(
675 sizeof(sallvar_T) + STRLEN(di->di_key));
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200676 if (newsav == NULL)
677 return;
678
679 sv->sv_tv = &di->di_tv;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200680 sv->sv_const = (di->di_flags & DI_FLAGS_LOCK) ? ASSIGN_CONST : 0;
681 sv->sv_export = is_export;
682 newsav->sav_var_vals_idx = si->sn_var_vals.ga_len;
683 ++si->sn_var_vals.ga_len;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200684 STRCPY(&newsav->sav_key, di->di_key);
685 sv->sv_name = newsav->sav_key;
686 newsav->sav_di = di;
687 newsav->sav_block_id = si->sn_current_block_id;
688
689 hi = hash_find(&si->sn_all_vars.dv_hashtab, newsav->sav_key);
690 if (!HASHITEM_EMPTY(hi))
691 {
692 sallvar_T *sav = HI2SAV(hi);
693
694 // variable with this name exists in another block
695 while (sav->sav_next != NULL)
696 sav = sav->sav_next;
697 sav->sav_next = newsav;
698 }
699 else
700 // new variable name
701 hash_add(&si->sn_all_vars.dv_hashtab, newsav->sav_key);
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200702 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100703 else
704 {
705 sv = find_typval_in_script(&di->di_tv);
706 }
707 if (sv != NULL)
708 {
709 if (type == NULL)
710 sv->sv_type = typval2type(tv, &si->sn_type_list);
711 else
712 sv->sv_type = type;
713 }
714
715 // let ex_export() know the export worked.
716 is_export = FALSE;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200717}
718
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200719/*
720 * Hide a script variable when leaving a block.
721 * "idx" is de index in sn_var_vals.
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200722 * When "func_defined" is non-zero then a function was defined in this block,
723 * the variable may be accessed by it. Otherwise the variable can be cleared.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200724 */
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200725 void
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200726hide_script_var(scriptitem_T *si, int idx, int func_defined)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200727{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200728 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200729 hashtab_T *script_ht = get_script_local_ht();
730 hashtab_T *all_ht = &si->sn_all_vars.dv_hashtab;
731 hashitem_T *script_hi;
732 hashitem_T *all_hi;
733
734 // Remove a variable declared inside the block, if it still exists.
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200735 // If it was added in a nested block it will already have been removed.
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200736 // The typval is moved into the sallvar_T.
737 script_hi = hash_find(script_ht, sv->sv_name);
738 all_hi = hash_find(all_ht, sv->sv_name);
739 if (!HASHITEM_EMPTY(script_hi) && !HASHITEM_EMPTY(all_hi))
740 {
741 dictitem_T *di = HI2DI(script_hi);
742 sallvar_T *sav = HI2SAV(all_hi);
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200743 sallvar_T *sav_prev = NULL;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200744
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200745 // There can be multiple entries with the same name in different
746 // blocks, find the right one.
747 while (sav != NULL && sav->sav_var_vals_idx != idx)
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200748 {
749 sav_prev = sav;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200750 sav = sav->sav_next;
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200751 }
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200752 if (sav != NULL)
753 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200754 if (func_defined)
755 {
756 // move the typval from the dictitem to the sallvar
757 sav->sav_tv = di->di_tv;
758 di->di_tv.v_type = VAR_UNKNOWN;
759 sav->sav_flags = di->di_flags;
760 sav->sav_di = NULL;
761 sv->sv_tv = &sav->sav_tv;
762 }
763 else
764 {
765 if (sav_prev == NULL)
766 hash_remove(all_ht, all_hi);
767 else
768 sav_prev->sav_next = sav->sav_next;
769 sv->sv_name = NULL;
770 vim_free(sav);
771 }
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200772 delete_var(script_ht, script_hi);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200773 }
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200774 }
775}
776
777/*
778 * Free the script variables from "sn_all_vars".
779 */
780 void
781free_all_script_vars(scriptitem_T *si)
782{
783 int todo;
784 hashtab_T *ht = &si->sn_all_vars.dv_hashtab;
785 hashitem_T *hi;
786 sallvar_T *sav;
787 sallvar_T *sav_next;
788
789 hash_lock(ht);
790 todo = (int)ht->ht_used;
791 for (hi = ht->ht_array; todo > 0; ++hi)
792 {
793 if (!HASHITEM_EMPTY(hi))
794 {
795 --todo;
796
797 // Free the variable. Don't remove it from the hashtab, ht_array
798 // might change then. hash_clear() takes care of it later.
799 sav = HI2SAV(hi);
800 while (sav != NULL)
801 {
802 sav_next = sav->sav_next;
803 if (sav->sav_di == NULL)
804 clear_tv(&sav->sav_tv);
805 vim_free(sav);
806 sav = sav_next;
807 }
808 }
809 }
810 hash_clear(ht);
811 hash_init(ht);
812
813 ga_clear(&si->sn_var_vals);
Bram Moolenaar2b327002020-12-26 15:39:31 +0100814
815 // existing commands using script variable indexes are no longer valid
816 si->sn_script_seq = current_sctx.sc_seq;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200817}
818
819/*
Bram Moolenaar10c65862020-10-08 21:16:42 +0200820 * Find the script-local variable that links to "dest".
821 * Returns NULL if not found.
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200822 */
Bram Moolenaar10c65862020-10-08 21:16:42 +0200823 svar_T *
824find_typval_in_script(typval_T *dest)
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200825{
826 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
827 int idx;
828
Bram Moolenaar81e17fb2020-08-21 21:55:43 +0200829 if (si->sn_version != SCRIPT_VERSION_VIM9)
830 // legacy script doesn't store variable types
Bram Moolenaar10c65862020-10-08 21:16:42 +0200831 return NULL;
Bram Moolenaar81e17fb2020-08-21 21:55:43 +0200832
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200833 // Find the svar_T in sn_var_vals.
834 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
835 {
836 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
837
Bram Moolenaarc0913d02020-12-05 14:44:37 +0100838 // If "sv_name" is NULL the variable was hidden when leaving a block,
839 // don't check "sv_tv" then, it might be used for another variable now.
840 if (sv->sv_name != NULL && sv->sv_tv == dest)
Bram Moolenaar10c65862020-10-08 21:16:42 +0200841 return sv;
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200842 }
843 iemsg("check_script_var_type(): not found");
Bram Moolenaar10c65862020-10-08 21:16:42 +0200844 return NULL;
845}
846
847/*
848 * Check if the type of script variable "dest" allows assigning "value".
849 * If needed convert "value" to a bool.
850 */
851 int
852check_script_var_type(typval_T *dest, typval_T *value, char_u *name)
853{
854 svar_T *sv = find_typval_in_script(dest);
855 int ret;
856
857 if (sv != NULL)
858 {
859 if (sv->sv_const)
860 {
861 semsg(_(e_readonlyvar), name);
862 return FAIL;
863 }
864 ret = check_typval_type(sv->sv_type, value, 0);
865 if (ret == OK && need_convert_to_bool(sv->sv_type, value))
866 {
867 int val = tv2bool(value);
868
869 clear_tv(value);
870 value->v_type = VAR_BOOL;
871 value->v_lock = 0;
872 value->vval.v_number = val ? VVAL_TRUE : VVAL_FALSE;
873 }
874 return ret;
875 }
876
Bram Moolenaarc785b9a2020-06-19 18:34:15 +0200877 return OK; // not really
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200878}
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200879
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100880#endif // FEAT_EVAL