blob: 70efc4065ba55120cccb639f65fb5a24cf4c3fb1 [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/*
177 * ":import Item from 'filename'"
178 * ":import Item as Alias from 'filename'"
179 * ":import {Item} from 'filename'".
180 * ":import {Item as Alias} from 'filename'"
181 * ":import {Item, Item} from 'filename'"
182 * ":import {Item, Item as Alias} from 'filename'"
183 *
184 * ":import * as Name from 'filename'"
185 */
186 void
187ex_import(exarg_T *eap)
188{
Bram Moolenaar1c991142020-07-04 13:15:31 +0200189 char_u *cmd_end;
190 evalarg_T evalarg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100191
Bram Moolenaar101f4812020-06-16 23:18:51 +0200192 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
193 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200194 emsg(_(e_import_can_only_be_used_in_script));
Bram Moolenaar101f4812020-06-16 23:18:51 +0200195 return;
196 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200197 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar101f4812020-06-16 23:18:51 +0200198
Bram Moolenaar1c991142020-07-04 13:15:31 +0200199 cmd_end = handle_import(eap->arg, NULL, current_sctx.sc_sid,
200 &evalarg, NULL);
Bram Moolenaar9721fb42020-06-11 23:10:46 +0200201 if (cmd_end != NULL)
202 eap->nextcmd = check_nextcmd(cmd_end);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200203 clear_evalarg(&evalarg, eap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100204}
205
206/*
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100207 * Find an exported item in "sid" matching the name at "*argp".
208 * When it is a variable return the index.
209 * When it is a user function return "*ufunc".
210 * When not found returns -1 and "*ufunc" is NULL.
211 */
212 int
213find_exported(
214 int sid,
Bram Moolenaar1c991142020-07-04 13:15:31 +0200215 char_u *name,
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100216 ufunc_T **ufunc,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200217 type_T **type,
218 cctx_T *cctx)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100219{
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100220 int idx = -1;
221 svar_T *sv;
222 scriptitem_T *script = SCRIPT_ITEM(sid);
223
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100224 // find name in "script"
225 // TODO: also find script-local user function
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200226 idx = get_script_item_idx(sid, name, FALSE, cctx);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100227 if (idx >= 0)
228 {
229 sv = ((svar_T *)script->sn_var_vals.ga_data) + idx;
230 if (!sv->sv_export)
231 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200232 semsg(_(e_item_not_exported_in_script_str), name);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100233 return -1;
234 }
235 *type = sv->sv_type;
236 *ufunc = NULL;
237 }
238 else
239 {
240 char_u buffer[200];
241 char_u *funcname;
242
243 // it could be a user function.
Bram Moolenaar5a67c372020-07-23 14:39:47 +0200244 if (STRLEN(name) < sizeof(buffer) - 15)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100245 funcname = buffer;
246 else
247 {
Bram Moolenaar5a67c372020-07-23 14:39:47 +0200248 funcname = alloc(STRLEN(name) + 15);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100249 if (funcname == NULL)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100250 return -1;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100251 }
252 funcname[0] = K_SPECIAL;
253 funcname[1] = KS_EXTRA;
254 funcname[2] = (int)KE_SNR;
255 sprintf((char *)funcname + 3, "%ld_%s", (long)sid, name);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200256 *ufunc = find_func(funcname, FALSE, NULL);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100257 if (funcname != buffer)
258 vim_free(funcname);
259
260 if (*ufunc == NULL)
261 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200262 semsg(_(e_item_not_found_in_script_str), name);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100263 return -1;
264 }
265 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100266
267 return idx;
268}
269
270/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100271 * Handle an ":import" command and add the resulting imported_T to "gap", when
272 * not NULL, or script "import_sid" sn_imports.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200273 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100274 * Returns a pointer to after the command or NULL in case of failure
275 */
276 char_u *
Bram Moolenaar1c991142020-07-04 13:15:31 +0200277handle_import(
278 char_u *arg_start,
279 garray_T *gap,
280 int import_sid,
281 evalarg_T *evalarg,
282 void *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100283{
284 char_u *arg = arg_start;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200285 char_u *cmd_end = NULL;
286 char_u *as_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100287 int ret = FAIL;
288 typval_T tv;
289 int sid = -1;
290 int res;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200291 garray_T names;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100292
Bram Moolenaar1c991142020-07-04 13:15:31 +0200293 ga_init2(&names, sizeof(char_u *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100294 if (*arg == '{')
295 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200296 // "import {item, item} from ..."
297 arg = skipwhite_and_linebreak(arg + 1, evalarg);
298 for (;;)
299 {
300 char_u *p = arg;
301 int had_comma = FALSE;
302
303 while (eval_isnamec(*arg))
304 ++arg;
305 if (p == arg)
306 break;
307 if (ga_grow(&names, 1) == FAIL)
308 goto erret;
309 ((char_u **)names.ga_data)[names.ga_len] =
310 vim_strnsave(p, arg - p);
311 ++names.ga_len;
312 if (*arg == ',')
313 {
314 had_comma = TRUE;
315 ++arg;
316 }
317 arg = skipwhite_and_linebreak(arg, evalarg);
318 if (*arg == '}')
319 {
320 arg = skipwhite_and_linebreak(arg + 1, evalarg);
321 break;
322 }
323 if (!had_comma)
324 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200325 emsg(_(e_missing_comma_in_import));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200326 goto erret;
327 }
328 }
329 if (names.ga_len == 0)
330 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200331 emsg(_(e_syntax_error_in_import));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200332 goto erret;
333 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100334 }
335 else
336 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200337 // "import Name from ..."
338 // "import * as Name from ..."
339 // "import item [as Name] from ..."
340 arg = skipwhite_and_linebreak(arg, evalarg);
341 if (arg[0] == '*' && IS_WHITE_OR_NUL(arg[1]))
342 arg = skipwhite_and_linebreak(arg + 1, evalarg);
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100343 else if (eval_isnamec1(*arg))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100344 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200345 char_u *p = arg;
346
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100347 while (eval_isnamec(*arg))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100348 ++arg;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200349 if (ga_grow(&names, 1) == FAIL)
350 goto erret;
351 ((char_u **)names.ga_data)[names.ga_len] =
352 vim_strnsave(p, arg - p);
353 ++names.ga_len;
354 arg = skipwhite_and_linebreak(arg, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100355 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200356 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100357 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200358 emsg(_(e_syntax_error_in_import));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200359 goto erret;
360 }
361
362 if (STRNCMP("as", arg, 2) == 0 && IS_WHITE_OR_NUL(arg[2]))
363 {
364 char_u *p;
365
366 // skip over "as Name "; no line break allowed after "as"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100367 arg = skipwhite(arg + 2);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200368 p = arg;
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100369 if (eval_isnamec1(*arg))
370 while (eval_isnamec(*arg))
371 ++arg;
Bram Moolenaarcbb6bdc2020-07-06 21:53:17 +0200372 if (check_defined(p, arg - p, cctx) == FAIL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200373 goto erret;
374 as_name = vim_strnsave(p, arg - p);
375 arg = skipwhite_and_linebreak(arg, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100376 }
377 else if (*arg_start == '*')
378 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200379 emsg(_(e_missing_as_after_star));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200380 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100381 }
382 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200383
384 if (STRNCMP("from", arg, 4) != 0 || !IS_WHITE_OR_NUL(arg[4]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100385 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200386 emsg(_(e_missing_from));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200387 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100388 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200389
Bram Moolenaar962d7212020-07-04 14:15:00 +0200390 arg = skipwhite_and_linebreak(arg + 4, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100391 tv.v_type = VAR_UNKNOWN;
392 // TODO: should we accept any expression?
393 if (*arg == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +0200394 ret = eval_lit_string(&arg, &tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100395 else if (*arg == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +0200396 ret = eval_string(&arg, &tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100397 if (ret == FAIL || tv.vval.v_string == NULL || *tv.vval.v_string == NUL)
398 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200399 emsg(_(e_invalid_string_after_from));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200400 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100401 }
402 cmd_end = arg;
403
Bram Moolenaar1c991142020-07-04 13:15:31 +0200404 /*
405 * find script file
406 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100407 if (*tv.vval.v_string == '.')
408 {
409 size_t len;
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100410 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100411 char_u *tail = gettail(si->sn_name);
412 char_u *from_name;
413
414 // Relative to current script: "./name.vim", "../../name.vim".
415 len = STRLEN(si->sn_name) - STRLEN(tail) + STRLEN(tv.vval.v_string) + 2;
416 from_name = alloc((int)len);
417 if (from_name == NULL)
418 {
419 clear_tv(&tv);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200420 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100421 }
422 vim_strncpy(from_name, si->sn_name, tail - si->sn_name);
423 add_pathsep(from_name);
424 STRCAT(from_name, tv.vval.v_string);
425 simplify_filename(from_name);
426
427 res = do_source(from_name, FALSE, DOSO_NONE, &sid);
428 vim_free(from_name);
429 }
430 else if (mch_isFullName(tv.vval.v_string))
431 {
432 // Absolute path: "/tmp/name.vim"
433 res = do_source(tv.vval.v_string, FALSE, DOSO_NONE, &sid);
434 }
435 else
436 {
437 size_t len = 7 + STRLEN(tv.vval.v_string) + 1;
438 char_u *from_name;
439
440 // Find file in "import" subdirs in 'runtimepath'.
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_snprintf((char *)from_name, len, "import/%s", tv.vval.v_string);
448 res = source_in_path(p_rtp, from_name, DIP_NOAFTER, &sid);
449 vim_free(from_name);
450 }
451
452 if (res == FAIL || sid <= 0)
453 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200454 semsg(_(e_could_not_import_str), tv.vval.v_string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100455 clear_tv(&tv);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200456 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100457 }
458 clear_tv(&tv);
459
460 if (*arg_start == '*')
461 {
462 imported_T *imported = new_imported(gap != NULL ? gap
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100463 : &SCRIPT_ITEM(import_sid)->sn_imports);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100464
465 if (imported == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200466 goto erret;
467 imported->imp_name = as_name;
468 as_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100469 imported->imp_sid = sid;
470 imported->imp_all = TRUE;
471 }
472 else
473 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200474 int i;
475
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100476 arg = arg_start;
477 if (*arg == '{')
478 arg = skipwhite(arg + 1);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200479 for (i = 0; i < names.ga_len; ++i)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100480 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200481 char_u *name = ((char_u **)names.ga_data)[i];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100482 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100483 imported_T *imported;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100484 ufunc_T *ufunc = NULL;
485 type_T *type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100486
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200487 idx = find_exported(sid, name, &ufunc, &type, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100488
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100489 if (idx < 0 && ufunc == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200490 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100491
Bram Moolenaar1c991142020-07-04 13:15:31 +0200492 if (check_defined(name, STRLEN(name), cctx) == FAIL)
493 goto erret;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100494
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100495 imported = new_imported(gap != NULL ? gap
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100496 : &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;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100499
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100500 // TODO: check for "as" following
Bram Moolenaar1c991142020-07-04 13:15:31 +0200501 // imported->imp_name = vim_strsave(as_name);
502 imported->imp_name = name;
503 ((char_u **)names.ga_data)[i] = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100504 imported->imp_sid = sid;
505 if (idx >= 0)
506 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100507 imported->imp_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100508 imported->imp_var_vals_idx = idx;
509 }
510 else
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200511 {
512 imported->imp_type = ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100513 imported->imp_funcname = ufunc->uf_name;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200514 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100515 }
516 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200517erret:
518 ga_clear_strings(&names);
519 vim_free(as_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100520 return cmd_end;
521}
522
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200523/*
524 * Declare a script-local variable without init: "let var: type".
525 * "const" is an error since the value is missing.
526 * Returns a pointer to after the type.
527 */
528 char_u *
529vim9_declare_scriptvar(exarg_T *eap, char_u *arg)
530{
531 char_u *p;
532 char_u *name;
533 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
534 type_T *type;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200535 typval_T init_tv;
536
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200537 if (eap->cmdidx == CMD_final || eap->cmdidx == CMD_const)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200538 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200539 if (eap->cmdidx == CMD_final)
540 emsg(_(e_final_requires_a_value));
541 else
542 emsg(_(e_const_requires_a_value));
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200543 return arg + STRLEN(arg);
544 }
545
546 // Check for valid starting character.
547 if (!eval_isnamec1(*arg))
548 {
549 semsg(_(e_invarg2), arg);
550 return arg + STRLEN(arg);
551 }
552
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200553 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
Bram Moolenaar3b74b6b2020-06-19 19:01:43 +0200554 if (*p == ':' && (VIM_ISWHITE(p[1]) || p != arg + 1))
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200555 break;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200556
557 if (*p != ':')
558 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +0200559 emsg(_(e_type_or_initialization_required));
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200560 return arg + STRLEN(arg);
561 }
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200562 if (!VIM_ISWHITE(p[1]))
563 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200564 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200565 return arg + STRLEN(arg);
566 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200567 name = vim_strnsave(arg, p - arg);
568
569 // parse type
570 p = skipwhite(p + 1);
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100571 type = parse_type(&p, &si->sn_type_list, TRUE);
572 if (type == NULL)
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200573 {
574 vim_free(name);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200575 return p;
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200576 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200577
578 // Create the variable with 0/NULL value.
579 CLEAR_FIELD(init_tv);
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +0200580 if (type->tt_type == VAR_ANY)
581 // A variable of type "any" is not possible, just use zero instead
582 init_tv.v_type = VAR_NUMBER;
583 else
584 init_tv.v_type = type->tt_type;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200585 set_var_const(name, type, &init_tv, FALSE, 0);
586
587 vim_free(name);
588 return p;
589}
590
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200591/*
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200592 * Vim9 part of adding a script variable: add it to sn_all_vars (lookup by name
593 * with a hashtable) and sn_var_vals (lookup by index).
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200594 * When "type" is NULL use "tv" for the type.
595 */
596 void
597add_vim9_script_var(dictitem_T *di, typval_T *tv, type_T *type)
598{
599 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
600
601 // Store a pointer to the typval_T, so that it can be found by
602 // index instead of using a hastab lookup.
603 if (ga_grow(&si->sn_var_vals, 1) == OK)
604 {
605 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
606 + si->sn_var_vals.ga_len;
607 hashitem_T *hi;
608 sallvar_T *newsav = (sallvar_T *)alloc_clear(
609 sizeof(sallvar_T) + STRLEN(di->di_key));
610
611 if (newsav == NULL)
612 return;
613
614 sv->sv_tv = &di->di_tv;
615 if (type == NULL)
616 sv->sv_type = typval2type(tv, &si->sn_type_list);
617 else
618 sv->sv_type = type;
619 sv->sv_const = (di->di_flags & DI_FLAGS_LOCK) ? ASSIGN_CONST : 0;
620 sv->sv_export = is_export;
621 newsav->sav_var_vals_idx = si->sn_var_vals.ga_len;
622 ++si->sn_var_vals.ga_len;
623
624 STRCPY(&newsav->sav_key, di->di_key);
625 sv->sv_name = newsav->sav_key;
626 newsav->sav_di = di;
627 newsav->sav_block_id = si->sn_current_block_id;
628
629 hi = hash_find(&si->sn_all_vars.dv_hashtab, newsav->sav_key);
630 if (!HASHITEM_EMPTY(hi))
631 {
632 sallvar_T *sav = HI2SAV(hi);
633
634 // variable with this name exists in another block
635 while (sav->sav_next != NULL)
636 sav = sav->sav_next;
637 sav->sav_next = newsav;
638 }
639 else
640 // new variable name
641 hash_add(&si->sn_all_vars.dv_hashtab, newsav->sav_key);
642
643 // let ex_export() know the export worked.
644 is_export = FALSE;
645 }
646}
647
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200648/*
649 * Hide a script variable when leaving a block.
650 * "idx" is de index in sn_var_vals.
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200651 * When "func_defined" is non-zero then a function was defined in this block,
652 * the variable may be accessed by it. Otherwise the variable can be cleared.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200653 */
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200654 void
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200655hide_script_var(scriptitem_T *si, int idx, int func_defined)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200656{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200657 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200658 hashtab_T *script_ht = get_script_local_ht();
659 hashtab_T *all_ht = &si->sn_all_vars.dv_hashtab;
660 hashitem_T *script_hi;
661 hashitem_T *all_hi;
662
663 // Remove a variable declared inside the block, if it still exists.
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200664 // If it was added in a nested block it will already have been removed.
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200665 // The typval is moved into the sallvar_T.
666 script_hi = hash_find(script_ht, sv->sv_name);
667 all_hi = hash_find(all_ht, sv->sv_name);
668 if (!HASHITEM_EMPTY(script_hi) && !HASHITEM_EMPTY(all_hi))
669 {
670 dictitem_T *di = HI2DI(script_hi);
671 sallvar_T *sav = HI2SAV(all_hi);
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200672 sallvar_T *sav_prev = NULL;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200673
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200674 // There can be multiple entries with the same name in different
675 // blocks, find the right one.
676 while (sav != NULL && sav->sav_var_vals_idx != idx)
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200677 {
678 sav_prev = sav;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200679 sav = sav->sav_next;
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200680 }
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200681 if (sav != NULL)
682 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200683 if (func_defined)
684 {
685 // move the typval from the dictitem to the sallvar
686 sav->sav_tv = di->di_tv;
687 di->di_tv.v_type = VAR_UNKNOWN;
688 sav->sav_flags = di->di_flags;
689 sav->sav_di = NULL;
690 sv->sv_tv = &sav->sav_tv;
691 }
692 else
693 {
694 if (sav_prev == NULL)
695 hash_remove(all_ht, all_hi);
696 else
697 sav_prev->sav_next = sav->sav_next;
698 sv->sv_name = NULL;
699 vim_free(sav);
700 }
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200701 delete_var(script_ht, script_hi);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200702 }
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200703 }
704}
705
706/*
707 * Free the script variables from "sn_all_vars".
708 */
709 void
710free_all_script_vars(scriptitem_T *si)
711{
712 int todo;
713 hashtab_T *ht = &si->sn_all_vars.dv_hashtab;
714 hashitem_T *hi;
715 sallvar_T *sav;
716 sallvar_T *sav_next;
717
718 hash_lock(ht);
719 todo = (int)ht->ht_used;
720 for (hi = ht->ht_array; todo > 0; ++hi)
721 {
722 if (!HASHITEM_EMPTY(hi))
723 {
724 --todo;
725
726 // Free the variable. Don't remove it from the hashtab, ht_array
727 // might change then. hash_clear() takes care of it later.
728 sav = HI2SAV(hi);
729 while (sav != NULL)
730 {
731 sav_next = sav->sav_next;
732 if (sav->sav_di == NULL)
733 clear_tv(&sav->sav_tv);
734 vim_free(sav);
735 sav = sav_next;
736 }
737 }
738 }
739 hash_clear(ht);
740 hash_init(ht);
741
742 ga_clear(&si->sn_var_vals);
Bram Moolenaar2b327002020-12-26 15:39:31 +0100743
744 // existing commands using script variable indexes are no longer valid
745 si->sn_script_seq = current_sctx.sc_seq;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200746}
747
748/*
Bram Moolenaar10c65862020-10-08 21:16:42 +0200749 * Find the script-local variable that links to "dest".
750 * Returns NULL if not found.
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200751 */
Bram Moolenaar10c65862020-10-08 21:16:42 +0200752 svar_T *
753find_typval_in_script(typval_T *dest)
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200754{
755 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
756 int idx;
757
Bram Moolenaar81e17fb2020-08-21 21:55:43 +0200758 if (si->sn_version != SCRIPT_VERSION_VIM9)
759 // legacy script doesn't store variable types
Bram Moolenaar10c65862020-10-08 21:16:42 +0200760 return NULL;
Bram Moolenaar81e17fb2020-08-21 21:55:43 +0200761
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200762 // Find the svar_T in sn_var_vals.
763 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
764 {
765 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
766
Bram Moolenaarc0913d02020-12-05 14:44:37 +0100767 // If "sv_name" is NULL the variable was hidden when leaving a block,
768 // don't check "sv_tv" then, it might be used for another variable now.
769 if (sv->sv_name != NULL && sv->sv_tv == dest)
Bram Moolenaar10c65862020-10-08 21:16:42 +0200770 return sv;
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200771 }
772 iemsg("check_script_var_type(): not found");
Bram Moolenaar10c65862020-10-08 21:16:42 +0200773 return NULL;
774}
775
776/*
777 * Check if the type of script variable "dest" allows assigning "value".
778 * If needed convert "value" to a bool.
779 */
780 int
781check_script_var_type(typval_T *dest, typval_T *value, char_u *name)
782{
783 svar_T *sv = find_typval_in_script(dest);
784 int ret;
785
786 if (sv != NULL)
787 {
788 if (sv->sv_const)
789 {
790 semsg(_(e_readonlyvar), name);
791 return FAIL;
792 }
793 ret = check_typval_type(sv->sv_type, value, 0);
794 if (ret == OK && need_convert_to_bool(sv->sv_type, value))
795 {
796 int val = tv2bool(value);
797
798 clear_tv(value);
799 value->v_type = VAR_BOOL;
800 value->v_lock = 0;
801 value->vval.v_number = val ? VVAL_TRUE : VVAL_FALSE;
802 }
803 return ret;
804 }
805
Bram Moolenaarc785b9a2020-06-19 18:34:15 +0200806 return OK; // not really
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200807}
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200808
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100809#endif // FEAT_EVAL