blob: e423b1c8028503ccfea4ced00fcd485b4c1f5106 [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
16#if defined(FEAT_EVAL) || defined(PROTO)
17
18#include "vim9.h"
19
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
33ex_vim9script(exarg_T *eap)
34{
Bram Moolenaar2b327002020-12-26 15:39:31 +010035 int sid = current_sctx.sc_sid;
Bram Moolenaar101f4812020-06-16 23:18:51 +020036 scriptitem_T *si;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010037
38 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
39 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +020040 emsg(_(e_vim9script_can_only_be_used_in_script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010041 return;
42 }
Bram Moolenaar2b327002020-12-26 15:39:31 +010043
44 si = SCRIPT_ITEM(sid);
45 if (si->sn_state == SN_STATE_HAD_COMMAND)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010046 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +020047 emsg(_(e_vim9script_must_be_first_command_in_script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010048 return;
49 }
Bram Moolenaar2b327002020-12-26 15:39:31 +010050 if (!IS_WHITE_OR_NUL(*eap->arg) && STRCMP(eap->arg, "noclear") != 0)
51 {
52 semsg(_(e_invarg2), eap->arg);
53 return;
54 }
55 if (si->sn_state == SN_STATE_RELOAD && IS_WHITE_OR_NUL(*eap->arg))
56 {
57 hashtab_T *ht = &SCRIPT_VARS(sid);
58
59 // Reloading a script without the "noclear" argument: clear
60 // script-local variables and functions.
61 hashtab_free_contents(ht);
62 hash_init(ht);
63 delete_script_functions(sid);
64
65 // old imports and script variables are no longer valid
66 free_imports_and_script_vars(sid);
67 }
68 si->sn_state = SN_STATE_HAD_COMMAND;
69
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010070 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
71 si->sn_version = SCRIPT_VERSION_VIM9;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010072
73 if (STRCMP(p_cpo, CPO_VIM) != 0)
74 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +020075 si->sn_save_cpo = vim_strsave(p_cpo);
76 set_option_value((char_u *)"cpo", 0L, (char_u *)CPO_VIM, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010077 }
78}
79
80/*
Bram Moolenaarae616492020-07-28 20:07:27 +020081 * When in Vim9 script give an error and return FAIL.
82 */
83 int
84not_in_vim9(exarg_T *eap)
85{
Bram Moolenaar68e30442020-07-28 21:15:07 +020086 if (in_vim9script())
87 switch (eap->cmdidx)
88 {
Bram Moolenaar68e30442020-07-28 21:15:07 +020089 case CMD_append:
90 case CMD_change:
Bram Moolenaarf5a48012020-08-01 17:00:03 +020091 case CMD_insert:
92 case CMD_t:
Bram Moolenaar68e30442020-07-28 21:15:07 +020093 case CMD_xit:
Bram Moolenaar3e2534e2020-10-28 17:55:31 +010094 semsg(_(e_missing_var_str), eap->cmd);
Bram Moolenaar68e30442020-07-28 21:15:07 +020095 return FAIL;
96 default: break;
97 }
Bram Moolenaarae616492020-07-28 20:07:27 +020098 return OK;
99}
100
101/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100102 * ":export let Name: type"
103 * ":export const Name: type"
104 * ":export def Name(..."
105 * ":export class Name ..."
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100106 */
107 void
Bram Moolenaar09689a02020-05-09 22:50:08 +0200108ex_export(exarg_T *eap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100109{
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200110 if (!in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100111 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200112 emsg(_(e_export_can_only_be_used_in_vim9script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100113 return;
114 }
115
116 eap->cmd = eap->arg;
117 (void)find_ex_command(eap, NULL, lookup_scriptvar, NULL);
118 switch (eap->cmdidx)
119 {
120 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200121 case CMD_var:
122 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100123 case CMD_const:
124 case CMD_def:
125 // case CMD_class:
126 is_export = TRUE;
127 do_cmdline(eap->cmd, eap->getline, eap->cookie,
128 DOCMD_VERBOSE + DOCMD_NOWAIT);
129
130 // The command will reset "is_export" when exporting an item.
131 if (is_export)
132 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200133 emsg(_(e_export_with_invalid_argument));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100134 is_export = FALSE;
135 }
136 break;
137 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200138 emsg(_(e_invalid_command_after_export));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100139 break;
140 }
141}
142
143/*
144 * Add a new imported item entry to the current script.
145 */
146 static imported_T *
147new_imported(garray_T *gap)
148{
149 if (ga_grow(gap, 1) == OK)
150 return ((imported_T *)gap->ga_data + gap->ga_len++);
151 return NULL;
152}
153
154/*
155 * Free all imported items in script "sid".
156 */
157 void
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200158free_imports_and_script_vars(int sid)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100159{
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100160 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100161 int idx;
162
163 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
164 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100165 imported_T *imp = ((imported_T *)si->sn_imports.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100166
167 vim_free(imp->imp_name);
168 }
169 ga_clear(&si->sn_imports);
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200170
171 free_all_script_vars(si);
172
Bram Moolenaar6110e792020-07-08 19:35:21 +0200173 clear_type_list(&si->sn_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100174}
175
176/*
Bram Moolenaara6294952020-12-27 13:39:50 +0100177 * Mark all imports as possible to redefine. Used when a script is loaded
178 * again but not cleared.
179 */
180 void
181mark_imports_for_reload(int sid)
182{
183 scriptitem_T *si = SCRIPT_ITEM(sid);
184 int idx;
185
186 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
187 {
188 imported_T *imp = ((imported_T *)si->sn_imports.ga_data) + idx;
189
190 imp->imp_flags |= IMP_FLAGS_RELOAD;
191 }
192}
193
194/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100195 * ":import Item from 'filename'"
196 * ":import Item as Alias from 'filename'"
197 * ":import {Item} from 'filename'".
198 * ":import {Item as Alias} from 'filename'"
199 * ":import {Item, Item} from 'filename'"
200 * ":import {Item, Item as Alias} from 'filename'"
201 *
202 * ":import * as Name from 'filename'"
203 */
204 void
205ex_import(exarg_T *eap)
206{
Bram Moolenaar1c991142020-07-04 13:15:31 +0200207 char_u *cmd_end;
208 evalarg_T evalarg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100209
Bram Moolenaar101f4812020-06-16 23:18:51 +0200210 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
211 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200212 emsg(_(e_import_can_only_be_used_in_script));
Bram Moolenaar101f4812020-06-16 23:18:51 +0200213 return;
214 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200215 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar101f4812020-06-16 23:18:51 +0200216
Bram Moolenaar1c991142020-07-04 13:15:31 +0200217 cmd_end = handle_import(eap->arg, NULL, current_sctx.sc_sid,
218 &evalarg, NULL);
Bram Moolenaar9721fb42020-06-11 23:10:46 +0200219 if (cmd_end != NULL)
220 eap->nextcmd = check_nextcmd(cmd_end);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200221 clear_evalarg(&evalarg, eap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100222}
223
224/*
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100225 * Find an exported item in "sid" matching the name at "*argp".
226 * When it is a variable return the index.
227 * When it is a user function return "*ufunc".
228 * When not found returns -1 and "*ufunc" is NULL.
229 */
230 int
231find_exported(
232 int sid,
Bram Moolenaar1c991142020-07-04 13:15:31 +0200233 char_u *name,
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100234 ufunc_T **ufunc,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200235 type_T **type,
236 cctx_T *cctx)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100237{
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100238 int idx = -1;
239 svar_T *sv;
240 scriptitem_T *script = SCRIPT_ITEM(sid);
241
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100242 // find name in "script"
243 // TODO: also find script-local user function
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200244 idx = get_script_item_idx(sid, name, FALSE, cctx);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100245 if (idx >= 0)
246 {
247 sv = ((svar_T *)script->sn_var_vals.ga_data) + idx;
248 if (!sv->sv_export)
249 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200250 semsg(_(e_item_not_exported_in_script_str), name);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100251 return -1;
252 }
253 *type = sv->sv_type;
254 *ufunc = NULL;
255 }
256 else
257 {
258 char_u buffer[200];
259 char_u *funcname;
260
261 // it could be a user function.
Bram Moolenaar5a67c372020-07-23 14:39:47 +0200262 if (STRLEN(name) < sizeof(buffer) - 15)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100263 funcname = buffer;
264 else
265 {
Bram Moolenaar5a67c372020-07-23 14:39:47 +0200266 funcname = alloc(STRLEN(name) + 15);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100267 if (funcname == NULL)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100268 return -1;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100269 }
270 funcname[0] = K_SPECIAL;
271 funcname[1] = KS_EXTRA;
272 funcname[2] = (int)KE_SNR;
273 sprintf((char *)funcname + 3, "%ld_%s", (long)sid, name);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200274 *ufunc = find_func(funcname, FALSE, NULL);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100275 if (funcname != buffer)
276 vim_free(funcname);
277
278 if (*ufunc == NULL)
279 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200280 semsg(_(e_item_not_found_in_script_str), name);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100281 return -1;
282 }
283 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100284
285 return idx;
286}
287
288/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100289 * Handle an ":import" command and add the resulting imported_T to "gap", when
290 * not NULL, or script "import_sid" sn_imports.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200291 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100292 * Returns a pointer to after the command or NULL in case of failure
293 */
294 char_u *
Bram Moolenaar1c991142020-07-04 13:15:31 +0200295handle_import(
296 char_u *arg_start,
297 garray_T *gap,
298 int import_sid,
299 evalarg_T *evalarg,
300 void *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100301{
302 char_u *arg = arg_start;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200303 char_u *cmd_end = NULL;
304 char_u *as_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100305 int ret = FAIL;
306 typval_T tv;
307 int sid = -1;
308 int res;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200309 garray_T names;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100310
Bram Moolenaar1c991142020-07-04 13:15:31 +0200311 ga_init2(&names, sizeof(char_u *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100312 if (*arg == '{')
313 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200314 // "import {item, item} from ..."
315 arg = skipwhite_and_linebreak(arg + 1, evalarg);
316 for (;;)
317 {
318 char_u *p = arg;
319 int had_comma = FALSE;
320
321 while (eval_isnamec(*arg))
322 ++arg;
323 if (p == arg)
324 break;
325 if (ga_grow(&names, 1) == FAIL)
326 goto erret;
327 ((char_u **)names.ga_data)[names.ga_len] =
328 vim_strnsave(p, arg - p);
329 ++names.ga_len;
330 if (*arg == ',')
331 {
332 had_comma = TRUE;
333 ++arg;
334 }
335 arg = skipwhite_and_linebreak(arg, evalarg);
336 if (*arg == '}')
337 {
338 arg = skipwhite_and_linebreak(arg + 1, evalarg);
339 break;
340 }
341 if (!had_comma)
342 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200343 emsg(_(e_missing_comma_in_import));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200344 goto erret;
345 }
346 }
347 if (names.ga_len == 0)
348 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200349 emsg(_(e_syntax_error_in_import));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200350 goto erret;
351 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100352 }
353 else
354 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200355 // "import Name from ..."
356 // "import * as Name from ..."
357 // "import item [as Name] from ..."
358 arg = skipwhite_and_linebreak(arg, evalarg);
359 if (arg[0] == '*' && IS_WHITE_OR_NUL(arg[1]))
360 arg = skipwhite_and_linebreak(arg + 1, evalarg);
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100361 else if (eval_isnamec1(*arg))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100362 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200363 char_u *p = arg;
364
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100365 while (eval_isnamec(*arg))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100366 ++arg;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200367 if (ga_grow(&names, 1) == FAIL)
368 goto erret;
369 ((char_u **)names.ga_data)[names.ga_len] =
370 vim_strnsave(p, arg - p);
371 ++names.ga_len;
372 arg = skipwhite_and_linebreak(arg, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100373 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200374 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100375 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200376 emsg(_(e_syntax_error_in_import));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200377 goto erret;
378 }
379
380 if (STRNCMP("as", arg, 2) == 0 && IS_WHITE_OR_NUL(arg[2]))
381 {
382 char_u *p;
383
384 // skip over "as Name "; no line break allowed after "as"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100385 arg = skipwhite(arg + 2);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200386 p = arg;
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100387 if (eval_isnamec1(*arg))
388 while (eval_isnamec(*arg))
389 ++arg;
Bram Moolenaarcbb6bdc2020-07-06 21:53:17 +0200390 if (check_defined(p, arg - p, cctx) == FAIL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200391 goto erret;
392 as_name = vim_strnsave(p, arg - p);
393 arg = skipwhite_and_linebreak(arg, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100394 }
395 else if (*arg_start == '*')
396 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200397 emsg(_(e_missing_as_after_star));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200398 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100399 }
400 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200401
402 if (STRNCMP("from", arg, 4) != 0 || !IS_WHITE_OR_NUL(arg[4]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100403 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200404 emsg(_(e_missing_from));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200405 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100406 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200407
Bram Moolenaar962d7212020-07-04 14:15:00 +0200408 arg = skipwhite_and_linebreak(arg + 4, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100409 tv.v_type = VAR_UNKNOWN;
410 // TODO: should we accept any expression?
411 if (*arg == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +0200412 ret = eval_lit_string(&arg, &tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100413 else if (*arg == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +0200414 ret = eval_string(&arg, &tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100415 if (ret == FAIL || tv.vval.v_string == NULL || *tv.vval.v_string == NUL)
416 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200417 emsg(_(e_invalid_string_after_from));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200418 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100419 }
420 cmd_end = arg;
421
Bram Moolenaar1c991142020-07-04 13:15:31 +0200422 /*
423 * find script file
424 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100425 if (*tv.vval.v_string == '.')
426 {
427 size_t len;
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100428 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100429 char_u *tail = gettail(si->sn_name);
430 char_u *from_name;
431
432 // Relative to current script: "./name.vim", "../../name.vim".
433 len = STRLEN(si->sn_name) - STRLEN(tail) + STRLEN(tv.vval.v_string) + 2;
434 from_name = alloc((int)len);
435 if (from_name == NULL)
436 {
437 clear_tv(&tv);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200438 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100439 }
440 vim_strncpy(from_name, si->sn_name, tail - si->sn_name);
441 add_pathsep(from_name);
442 STRCAT(from_name, tv.vval.v_string);
443 simplify_filename(from_name);
444
445 res = do_source(from_name, FALSE, DOSO_NONE, &sid);
446 vim_free(from_name);
447 }
448 else if (mch_isFullName(tv.vval.v_string))
449 {
450 // Absolute path: "/tmp/name.vim"
451 res = do_source(tv.vval.v_string, FALSE, DOSO_NONE, &sid);
452 }
453 else
454 {
455 size_t len = 7 + STRLEN(tv.vval.v_string) + 1;
456 char_u *from_name;
457
458 // Find file in "import" subdirs in 'runtimepath'.
459 from_name = alloc((int)len);
460 if (from_name == NULL)
461 {
462 clear_tv(&tv);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200463 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100464 }
465 vim_snprintf((char *)from_name, len, "import/%s", tv.vval.v_string);
466 res = source_in_path(p_rtp, from_name, DIP_NOAFTER, &sid);
467 vim_free(from_name);
468 }
469
470 if (res == FAIL || sid <= 0)
471 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200472 semsg(_(e_could_not_import_str), tv.vval.v_string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100473 clear_tv(&tv);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200474 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100475 }
476 clear_tv(&tv);
477
478 if (*arg_start == '*')
479 {
Bram Moolenaara6294952020-12-27 13:39:50 +0100480 imported_T *imported;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100481
Bram Moolenaara6294952020-12-27 13:39:50 +0100482 imported = find_imported(as_name, STRLEN(as_name), cctx);
483 if (imported != NULL && imported->imp_sid == sid)
484 {
485 if (imported->imp_flags & IMP_FLAGS_RELOAD)
486 // import already defined on a previous script load
487 imported->imp_flags &= ~IMP_FLAGS_RELOAD;
488 else
489 {
490 semsg(_(e_name_already_defined_str), as_name);
491 goto erret;
492 }
493 }
494
495 imported = new_imported(gap != NULL ? gap
496 : &SCRIPT_ITEM(import_sid)->sn_imports);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100497 if (imported == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200498 goto erret;
499 imported->imp_name = as_name;
500 as_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100501 imported->imp_sid = sid;
Bram Moolenaara6294952020-12-27 13:39:50 +0100502 imported->imp_flags = IMP_FLAGS_STAR;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100503 }
504 else
505 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200506 int i;
507
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100508 arg = arg_start;
509 if (*arg == '{')
510 arg = skipwhite(arg + 1);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200511 for (i = 0; i < names.ga_len; ++i)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100512 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200513 char_u *name = ((char_u **)names.ga_data)[i];
Bram Moolenaara6294952020-12-27 13:39:50 +0100514 size_t len = STRLEN(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100515 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100516 imported_T *imported;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100517 ufunc_T *ufunc = NULL;
518 type_T *type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100519
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200520 idx = find_exported(sid, name, &ufunc, &type, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100521
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100522 if (idx < 0 && ufunc == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200523 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100524
Bram Moolenaara6294952020-12-27 13:39:50 +0100525 // If already imported with the same propertis and the
526 // IMP_FLAGS_RELOAD set then we keep that entry. Otherwise create
527 // a new one (and give an error for an existing import).
528 imported = find_imported(name, len, cctx);
529 if (imported != NULL
530 && (imported->imp_flags & IMP_FLAGS_RELOAD)
531 && imported->imp_sid == sid
532 && (idx >= 0
533 ? (equal_type(imported->imp_type, type)
534 && imported->imp_var_vals_idx == idx)
535 : (equal_type(imported->imp_type, ufunc->uf_func_type)
536 && STRCMP(imported->imp_funcname,
537 ufunc->uf_name) == 0)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100538 {
Bram Moolenaara6294952020-12-27 13:39:50 +0100539 imported->imp_flags &= ~IMP_FLAGS_RELOAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100540 }
541 else
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200542 {
Bram Moolenaara6294952020-12-27 13:39:50 +0100543 if (check_defined(name, len, cctx) == FAIL)
544 goto erret;
545
546 imported = new_imported(gap != NULL ? gap
547 : &SCRIPT_ITEM(import_sid)->sn_imports);
548 if (imported == NULL)
549 goto erret;
550
551 // TODO: check for "as" following
552 // imported->imp_name = vim_strsave(as_name);
553 imported->imp_name = name;
554 ((char_u **)names.ga_data)[i] = NULL;
555 imported->imp_sid = sid;
556 if (idx >= 0)
557 {
558 imported->imp_type = type;
559 imported->imp_var_vals_idx = idx;
560 }
561 else
562 {
563 imported->imp_type = ufunc->uf_func_type;
564 imported->imp_funcname = ufunc->uf_name;
565 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200566 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100567 }
568 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200569erret:
570 ga_clear_strings(&names);
571 vim_free(as_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100572 return cmd_end;
573}
574
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200575/*
576 * Declare a script-local variable without init: "let var: type".
577 * "const" is an error since the value is missing.
578 * Returns a pointer to after the type.
579 */
580 char_u *
581vim9_declare_scriptvar(exarg_T *eap, char_u *arg)
582{
583 char_u *p;
584 char_u *name;
585 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
586 type_T *type;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200587 typval_T init_tv;
588
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200589 if (eap->cmdidx == CMD_final || eap->cmdidx == CMD_const)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200590 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200591 if (eap->cmdidx == CMD_final)
592 emsg(_(e_final_requires_a_value));
593 else
594 emsg(_(e_const_requires_a_value));
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200595 return arg + STRLEN(arg);
596 }
597
598 // Check for valid starting character.
599 if (!eval_isnamec1(*arg))
600 {
601 semsg(_(e_invarg2), arg);
602 return arg + STRLEN(arg);
603 }
604
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200605 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
Bram Moolenaar3b74b6b2020-06-19 19:01:43 +0200606 if (*p == ':' && (VIM_ISWHITE(p[1]) || p != arg + 1))
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200607 break;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200608
609 if (*p != ':')
610 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +0200611 emsg(_(e_type_or_initialization_required));
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200612 return arg + STRLEN(arg);
613 }
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200614 if (!VIM_ISWHITE(p[1]))
615 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200616 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200617 return arg + STRLEN(arg);
618 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200619 name = vim_strnsave(arg, p - arg);
620
621 // parse type
622 p = skipwhite(p + 1);
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100623 type = parse_type(&p, &si->sn_type_list, TRUE);
624 if (type == NULL)
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200625 {
626 vim_free(name);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200627 return p;
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200628 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200629
630 // Create the variable with 0/NULL value.
631 CLEAR_FIELD(init_tv);
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +0200632 if (type->tt_type == VAR_ANY)
633 // A variable of type "any" is not possible, just use zero instead
634 init_tv.v_type = VAR_NUMBER;
635 else
636 init_tv.v_type = type->tt_type;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200637 set_var_const(name, type, &init_tv, FALSE, 0);
638
639 vim_free(name);
640 return p;
641}
642
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200643/*
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200644 * Vim9 part of adding a script variable: add it to sn_all_vars (lookup by name
645 * with a hashtable) and sn_var_vals (lookup by index).
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100646 * When "create" is TRUE this is a new variable, otherwise find and update an
647 * existing variable.
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200648 * When "type" is NULL use "tv" for the type.
649 */
650 void
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100651update_vim9_script_var(int create, dictitem_T *di, typval_T *tv, type_T *type)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200652{
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100653 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
654 hashitem_T *hi;
655 svar_T *sv;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200656
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100657 if (create)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200658 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100659 sallvar_T *newsav;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200660
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100661 // Store a pointer to the typval_T, so that it can be found by index
662 // instead of using a hastab lookup.
663 if (ga_grow(&si->sn_var_vals, 1) == FAIL)
664 return;
665
666 sv = ((svar_T *)si->sn_var_vals.ga_data) + si->sn_var_vals.ga_len;
667 newsav = (sallvar_T *)alloc_clear(
668 sizeof(sallvar_T) + STRLEN(di->di_key));
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200669 if (newsav == NULL)
670 return;
671
672 sv->sv_tv = &di->di_tv;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200673 sv->sv_const = (di->di_flags & DI_FLAGS_LOCK) ? ASSIGN_CONST : 0;
674 sv->sv_export = is_export;
675 newsav->sav_var_vals_idx = si->sn_var_vals.ga_len;
676 ++si->sn_var_vals.ga_len;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200677 STRCPY(&newsav->sav_key, di->di_key);
678 sv->sv_name = newsav->sav_key;
679 newsav->sav_di = di;
680 newsav->sav_block_id = si->sn_current_block_id;
681
682 hi = hash_find(&si->sn_all_vars.dv_hashtab, newsav->sav_key);
683 if (!HASHITEM_EMPTY(hi))
684 {
685 sallvar_T *sav = HI2SAV(hi);
686
687 // variable with this name exists in another block
688 while (sav->sav_next != NULL)
689 sav = sav->sav_next;
690 sav->sav_next = newsav;
691 }
692 else
693 // new variable name
694 hash_add(&si->sn_all_vars.dv_hashtab, newsav->sav_key);
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200695 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100696 else
697 {
698 sv = find_typval_in_script(&di->di_tv);
699 }
700 if (sv != NULL)
701 {
702 if (type == NULL)
703 sv->sv_type = typval2type(tv, &si->sn_type_list);
704 else
705 sv->sv_type = type;
706 }
707
708 // let ex_export() know the export worked.
709 is_export = FALSE;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200710}
711
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200712/*
713 * Hide a script variable when leaving a block.
714 * "idx" is de index in sn_var_vals.
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200715 * When "func_defined" is non-zero then a function was defined in this block,
716 * the variable may be accessed by it. Otherwise the variable can be cleared.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200717 */
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200718 void
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200719hide_script_var(scriptitem_T *si, int idx, int func_defined)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200720{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200721 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200722 hashtab_T *script_ht = get_script_local_ht();
723 hashtab_T *all_ht = &si->sn_all_vars.dv_hashtab;
724 hashitem_T *script_hi;
725 hashitem_T *all_hi;
726
727 // Remove a variable declared inside the block, if it still exists.
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200728 // If it was added in a nested block it will already have been removed.
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200729 // The typval is moved into the sallvar_T.
730 script_hi = hash_find(script_ht, sv->sv_name);
731 all_hi = hash_find(all_ht, sv->sv_name);
732 if (!HASHITEM_EMPTY(script_hi) && !HASHITEM_EMPTY(all_hi))
733 {
734 dictitem_T *di = HI2DI(script_hi);
735 sallvar_T *sav = HI2SAV(all_hi);
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200736 sallvar_T *sav_prev = NULL;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200737
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200738 // There can be multiple entries with the same name in different
739 // blocks, find the right one.
740 while (sav != NULL && sav->sav_var_vals_idx != idx)
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200741 {
742 sav_prev = sav;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200743 sav = sav->sav_next;
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200744 }
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200745 if (sav != NULL)
746 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200747 if (func_defined)
748 {
749 // move the typval from the dictitem to the sallvar
750 sav->sav_tv = di->di_tv;
751 di->di_tv.v_type = VAR_UNKNOWN;
752 sav->sav_flags = di->di_flags;
753 sav->sav_di = NULL;
754 sv->sv_tv = &sav->sav_tv;
755 }
756 else
757 {
758 if (sav_prev == NULL)
759 hash_remove(all_ht, all_hi);
760 else
761 sav_prev->sav_next = sav->sav_next;
762 sv->sv_name = NULL;
763 vim_free(sav);
764 }
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200765 delete_var(script_ht, script_hi);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200766 }
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200767 }
768}
769
770/*
771 * Free the script variables from "sn_all_vars".
772 */
773 void
774free_all_script_vars(scriptitem_T *si)
775{
776 int todo;
777 hashtab_T *ht = &si->sn_all_vars.dv_hashtab;
778 hashitem_T *hi;
779 sallvar_T *sav;
780 sallvar_T *sav_next;
781
782 hash_lock(ht);
783 todo = (int)ht->ht_used;
784 for (hi = ht->ht_array; todo > 0; ++hi)
785 {
786 if (!HASHITEM_EMPTY(hi))
787 {
788 --todo;
789
790 // Free the variable. Don't remove it from the hashtab, ht_array
791 // might change then. hash_clear() takes care of it later.
792 sav = HI2SAV(hi);
793 while (sav != NULL)
794 {
795 sav_next = sav->sav_next;
796 if (sav->sav_di == NULL)
797 clear_tv(&sav->sav_tv);
798 vim_free(sav);
799 sav = sav_next;
800 }
801 }
802 }
803 hash_clear(ht);
804 hash_init(ht);
805
806 ga_clear(&si->sn_var_vals);
Bram Moolenaar2b327002020-12-26 15:39:31 +0100807
808 // existing commands using script variable indexes are no longer valid
809 si->sn_script_seq = current_sctx.sc_seq;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200810}
811
812/*
Bram Moolenaar10c65862020-10-08 21:16:42 +0200813 * Find the script-local variable that links to "dest".
814 * Returns NULL if not found.
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200815 */
Bram Moolenaar10c65862020-10-08 21:16:42 +0200816 svar_T *
817find_typval_in_script(typval_T *dest)
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200818{
819 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
820 int idx;
821
Bram Moolenaar81e17fb2020-08-21 21:55:43 +0200822 if (si->sn_version != SCRIPT_VERSION_VIM9)
823 // legacy script doesn't store variable types
Bram Moolenaar10c65862020-10-08 21:16:42 +0200824 return NULL;
Bram Moolenaar81e17fb2020-08-21 21:55:43 +0200825
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200826 // Find the svar_T in sn_var_vals.
827 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
828 {
829 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
830
Bram Moolenaarc0913d02020-12-05 14:44:37 +0100831 // If "sv_name" is NULL the variable was hidden when leaving a block,
832 // don't check "sv_tv" then, it might be used for another variable now.
833 if (sv->sv_name != NULL && sv->sv_tv == dest)
Bram Moolenaar10c65862020-10-08 21:16:42 +0200834 return sv;
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200835 }
836 iemsg("check_script_var_type(): not found");
Bram Moolenaar10c65862020-10-08 21:16:42 +0200837 return NULL;
838}
839
840/*
841 * Check if the type of script variable "dest" allows assigning "value".
842 * If needed convert "value" to a bool.
843 */
844 int
845check_script_var_type(typval_T *dest, typval_T *value, char_u *name)
846{
847 svar_T *sv = find_typval_in_script(dest);
848 int ret;
849
850 if (sv != NULL)
851 {
852 if (sv->sv_const)
853 {
854 semsg(_(e_readonlyvar), name);
855 return FAIL;
856 }
857 ret = check_typval_type(sv->sv_type, value, 0);
858 if (ret == OK && need_convert_to_bool(sv->sv_type, value))
859 {
860 int val = tv2bool(value);
861
862 clear_tv(value);
863 value->v_type = VAR_BOOL;
864 value->v_lock = 0;
865 value->vval.v_number = val ? VVAL_TRUE : VVAL_FALSE;
866 }
867 return ret;
868 }
869
Bram Moolenaarc785b9a2020-06-19 18:34:15 +0200870 return OK; // not really
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200871}
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200872
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100873#endif // FEAT_EVAL