blob: 269300c5a7650627ffad7acbe93ec52eb3295ef5 [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 Moolenaardcc58e02020-12-28 20:53:21 +0100106/*
107 * Return TRUE if "p" points at a "#". Does not check for white space.
108 */
109 int
110vim9_comment_start(char_u *p)
111{
112 return p[0] == '#';
113}
114
Bram Moolenaar9b8d6222020-12-28 18:26:00 +0100115#if defined(FEAT_EVAL) || defined(PROTO)
116
Bram Moolenaarae616492020-07-28 20:07:27 +0200117/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100118 * ":export let Name: type"
119 * ":export const Name: type"
120 * ":export def Name(..."
121 * ":export class Name ..."
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100122 */
123 void
Bram Moolenaar09689a02020-05-09 22:50:08 +0200124ex_export(exarg_T *eap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100125{
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200126 if (!in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100127 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200128 emsg(_(e_export_can_only_be_used_in_vim9script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100129 return;
130 }
131
132 eap->cmd = eap->arg;
133 (void)find_ex_command(eap, NULL, lookup_scriptvar, NULL);
134 switch (eap->cmdidx)
135 {
136 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200137 case CMD_var:
138 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100139 case CMD_const:
140 case CMD_def:
141 // case CMD_class:
142 is_export = TRUE;
143 do_cmdline(eap->cmd, eap->getline, eap->cookie,
144 DOCMD_VERBOSE + DOCMD_NOWAIT);
145
146 // The command will reset "is_export" when exporting an item.
147 if (is_export)
148 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200149 emsg(_(e_export_with_invalid_argument));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100150 is_export = FALSE;
151 }
152 break;
153 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200154 emsg(_(e_invalid_command_after_export));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100155 break;
156 }
157}
158
159/*
160 * Add a new imported item entry to the current script.
161 */
162 static imported_T *
163new_imported(garray_T *gap)
164{
165 if (ga_grow(gap, 1) == OK)
166 return ((imported_T *)gap->ga_data + gap->ga_len++);
167 return NULL;
168}
169
170/*
171 * Free all imported items in script "sid".
172 */
173 void
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200174free_imports_and_script_vars(int sid)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100175{
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100176 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100177 int idx;
178
179 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
180 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100181 imported_T *imp = ((imported_T *)si->sn_imports.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100182
183 vim_free(imp->imp_name);
184 }
185 ga_clear(&si->sn_imports);
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200186
187 free_all_script_vars(si);
188
Bram Moolenaar6110e792020-07-08 19:35:21 +0200189 clear_type_list(&si->sn_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100190}
191
192/*
Bram Moolenaara6294952020-12-27 13:39:50 +0100193 * Mark all imports as possible to redefine. Used when a script is loaded
194 * again but not cleared.
195 */
196 void
197mark_imports_for_reload(int sid)
198{
199 scriptitem_T *si = SCRIPT_ITEM(sid);
200 int idx;
201
202 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
203 {
204 imported_T *imp = ((imported_T *)si->sn_imports.ga_data) + idx;
205
206 imp->imp_flags |= IMP_FLAGS_RELOAD;
207 }
208}
209
210/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100211 * ":import Item from 'filename'"
212 * ":import Item as Alias from 'filename'"
213 * ":import {Item} from 'filename'".
214 * ":import {Item as Alias} from 'filename'"
215 * ":import {Item, Item} from 'filename'"
216 * ":import {Item, Item as Alias} from 'filename'"
217 *
218 * ":import * as Name from 'filename'"
219 */
220 void
221ex_import(exarg_T *eap)
222{
Bram Moolenaar1c991142020-07-04 13:15:31 +0200223 char_u *cmd_end;
224 evalarg_T evalarg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100225
Bram Moolenaar101f4812020-06-16 23:18:51 +0200226 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
227 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200228 emsg(_(e_import_can_only_be_used_in_script));
Bram Moolenaar101f4812020-06-16 23:18:51 +0200229 return;
230 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200231 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar101f4812020-06-16 23:18:51 +0200232
Bram Moolenaar1c991142020-07-04 13:15:31 +0200233 cmd_end = handle_import(eap->arg, NULL, current_sctx.sc_sid,
234 &evalarg, NULL);
Bram Moolenaar9721fb42020-06-11 23:10:46 +0200235 if (cmd_end != NULL)
236 eap->nextcmd = check_nextcmd(cmd_end);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200237 clear_evalarg(&evalarg, eap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100238}
239
240/*
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100241 * Find an exported item in "sid" matching the name at "*argp".
242 * When it is a variable return the index.
243 * When it is a user function return "*ufunc".
244 * When not found returns -1 and "*ufunc" is NULL.
245 */
246 int
247find_exported(
248 int sid,
Bram Moolenaar1c991142020-07-04 13:15:31 +0200249 char_u *name,
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100250 ufunc_T **ufunc,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200251 type_T **type,
252 cctx_T *cctx)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100253{
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100254 int idx = -1;
255 svar_T *sv;
256 scriptitem_T *script = SCRIPT_ITEM(sid);
257
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100258 // find name in "script"
259 // TODO: also find script-local user function
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200260 idx = get_script_item_idx(sid, name, FALSE, cctx);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100261 if (idx >= 0)
262 {
263 sv = ((svar_T *)script->sn_var_vals.ga_data) + idx;
264 if (!sv->sv_export)
265 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200266 semsg(_(e_item_not_exported_in_script_str), name);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100267 return -1;
268 }
269 *type = sv->sv_type;
270 *ufunc = NULL;
271 }
272 else
273 {
274 char_u buffer[200];
275 char_u *funcname;
276
277 // it could be a user function.
Bram Moolenaar5a67c372020-07-23 14:39:47 +0200278 if (STRLEN(name) < sizeof(buffer) - 15)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100279 funcname = buffer;
280 else
281 {
Bram Moolenaar5a67c372020-07-23 14:39:47 +0200282 funcname = alloc(STRLEN(name) + 15);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100283 if (funcname == NULL)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100284 return -1;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100285 }
286 funcname[0] = K_SPECIAL;
287 funcname[1] = KS_EXTRA;
288 funcname[2] = (int)KE_SNR;
289 sprintf((char *)funcname + 3, "%ld_%s", (long)sid, name);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200290 *ufunc = find_func(funcname, FALSE, NULL);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100291 if (funcname != buffer)
292 vim_free(funcname);
293
294 if (*ufunc == NULL)
295 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200296 semsg(_(e_item_not_found_in_script_str), name);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100297 return -1;
298 }
299 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100300
301 return idx;
302}
303
304/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100305 * Handle an ":import" command and add the resulting imported_T to "gap", when
306 * not NULL, or script "import_sid" sn_imports.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200307 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100308 * Returns a pointer to after the command or NULL in case of failure
309 */
310 char_u *
Bram Moolenaar1c991142020-07-04 13:15:31 +0200311handle_import(
312 char_u *arg_start,
313 garray_T *gap,
314 int import_sid,
315 evalarg_T *evalarg,
316 void *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100317{
318 char_u *arg = arg_start;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200319 char_u *cmd_end = NULL;
320 char_u *as_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100321 int ret = FAIL;
322 typval_T tv;
323 int sid = -1;
324 int res;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200325 garray_T names;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100326
Bram Moolenaar1c991142020-07-04 13:15:31 +0200327 ga_init2(&names, sizeof(char_u *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100328 if (*arg == '{')
329 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200330 // "import {item, item} from ..."
331 arg = skipwhite_and_linebreak(arg + 1, evalarg);
332 for (;;)
333 {
334 char_u *p = arg;
335 int had_comma = FALSE;
336
337 while (eval_isnamec(*arg))
338 ++arg;
339 if (p == arg)
340 break;
341 if (ga_grow(&names, 1) == FAIL)
342 goto erret;
343 ((char_u **)names.ga_data)[names.ga_len] =
344 vim_strnsave(p, arg - p);
345 ++names.ga_len;
346 if (*arg == ',')
347 {
348 had_comma = TRUE;
349 ++arg;
350 }
351 arg = skipwhite_and_linebreak(arg, evalarg);
352 if (*arg == '}')
353 {
354 arg = skipwhite_and_linebreak(arg + 1, evalarg);
355 break;
356 }
357 if (!had_comma)
358 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200359 emsg(_(e_missing_comma_in_import));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200360 goto erret;
361 }
362 }
363 if (names.ga_len == 0)
364 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200365 emsg(_(e_syntax_error_in_import));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200366 goto erret;
367 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100368 }
369 else
370 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200371 // "import Name from ..."
372 // "import * as Name from ..."
373 // "import item [as Name] from ..."
374 arg = skipwhite_and_linebreak(arg, evalarg);
375 if (arg[0] == '*' && IS_WHITE_OR_NUL(arg[1]))
376 arg = skipwhite_and_linebreak(arg + 1, evalarg);
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100377 else if (eval_isnamec1(*arg))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100378 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200379 char_u *p = arg;
380
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100381 while (eval_isnamec(*arg))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100382 ++arg;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200383 if (ga_grow(&names, 1) == FAIL)
384 goto erret;
385 ((char_u **)names.ga_data)[names.ga_len] =
386 vim_strnsave(p, arg - p);
387 ++names.ga_len;
388 arg = skipwhite_and_linebreak(arg, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100389 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200390 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100391 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200392 emsg(_(e_syntax_error_in_import));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200393 goto erret;
394 }
395
396 if (STRNCMP("as", arg, 2) == 0 && IS_WHITE_OR_NUL(arg[2]))
397 {
398 char_u *p;
399
400 // skip over "as Name "; no line break allowed after "as"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100401 arg = skipwhite(arg + 2);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200402 p = arg;
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100403 if (eval_isnamec1(*arg))
404 while (eval_isnamec(*arg))
405 ++arg;
Bram Moolenaarcbb6bdc2020-07-06 21:53:17 +0200406 if (check_defined(p, arg - p, cctx) == FAIL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200407 goto erret;
408 as_name = vim_strnsave(p, arg - p);
409 arg = skipwhite_and_linebreak(arg, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100410 }
411 else if (*arg_start == '*')
412 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200413 emsg(_(e_missing_as_after_star));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200414 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100415 }
416 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200417
418 if (STRNCMP("from", arg, 4) != 0 || !IS_WHITE_OR_NUL(arg[4]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100419 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200420 emsg(_(e_missing_from));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200421 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100422 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200423
Bram Moolenaar962d7212020-07-04 14:15:00 +0200424 arg = skipwhite_and_linebreak(arg + 4, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100425 tv.v_type = VAR_UNKNOWN;
426 // TODO: should we accept any expression?
427 if (*arg == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +0200428 ret = eval_lit_string(&arg, &tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100429 else if (*arg == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +0200430 ret = eval_string(&arg, &tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100431 if (ret == FAIL || tv.vval.v_string == NULL || *tv.vval.v_string == NUL)
432 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200433 emsg(_(e_invalid_string_after_from));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200434 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100435 }
436 cmd_end = arg;
437
Bram Moolenaar1c991142020-07-04 13:15:31 +0200438 /*
439 * find script file
440 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100441 if (*tv.vval.v_string == '.')
442 {
443 size_t len;
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100444 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100445 char_u *tail = gettail(si->sn_name);
446 char_u *from_name;
447
448 // Relative to current script: "./name.vim", "../../name.vim".
449 len = STRLEN(si->sn_name) - STRLEN(tail) + STRLEN(tv.vval.v_string) + 2;
450 from_name = alloc((int)len);
451 if (from_name == NULL)
452 {
453 clear_tv(&tv);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200454 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100455 }
456 vim_strncpy(from_name, si->sn_name, tail - si->sn_name);
457 add_pathsep(from_name);
458 STRCAT(from_name, tv.vval.v_string);
459 simplify_filename(from_name);
460
461 res = do_source(from_name, FALSE, DOSO_NONE, &sid);
462 vim_free(from_name);
463 }
464 else if (mch_isFullName(tv.vval.v_string))
465 {
466 // Absolute path: "/tmp/name.vim"
467 res = do_source(tv.vval.v_string, FALSE, DOSO_NONE, &sid);
468 }
469 else
470 {
471 size_t len = 7 + STRLEN(tv.vval.v_string) + 1;
472 char_u *from_name;
473
474 // Find file in "import" subdirs in 'runtimepath'.
475 from_name = alloc((int)len);
476 if (from_name == NULL)
477 {
478 clear_tv(&tv);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200479 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100480 }
481 vim_snprintf((char *)from_name, len, "import/%s", tv.vval.v_string);
482 res = source_in_path(p_rtp, from_name, DIP_NOAFTER, &sid);
483 vim_free(from_name);
484 }
485
486 if (res == FAIL || sid <= 0)
487 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200488 semsg(_(e_could_not_import_str), tv.vval.v_string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100489 clear_tv(&tv);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200490 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100491 }
492 clear_tv(&tv);
493
494 if (*arg_start == '*')
495 {
Bram Moolenaara6294952020-12-27 13:39:50 +0100496 imported_T *imported;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100497
Bram Moolenaara6294952020-12-27 13:39:50 +0100498 imported = find_imported(as_name, STRLEN(as_name), cctx);
499 if (imported != NULL && imported->imp_sid == sid)
500 {
501 if (imported->imp_flags & IMP_FLAGS_RELOAD)
502 // import already defined on a previous script load
503 imported->imp_flags &= ~IMP_FLAGS_RELOAD;
504 else
505 {
506 semsg(_(e_name_already_defined_str), as_name);
507 goto erret;
508 }
509 }
510
511 imported = new_imported(gap != NULL ? gap
512 : &SCRIPT_ITEM(import_sid)->sn_imports);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100513 if (imported == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200514 goto erret;
515 imported->imp_name = as_name;
516 as_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100517 imported->imp_sid = sid;
Bram Moolenaara6294952020-12-27 13:39:50 +0100518 imported->imp_flags = IMP_FLAGS_STAR;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100519 }
520 else
521 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200522 int i;
523
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100524 arg = arg_start;
525 if (*arg == '{')
526 arg = skipwhite(arg + 1);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200527 for (i = 0; i < names.ga_len; ++i)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100528 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200529 char_u *name = ((char_u **)names.ga_data)[i];
Bram Moolenaara6294952020-12-27 13:39:50 +0100530 size_t len = STRLEN(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100531 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100532 imported_T *imported;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100533 ufunc_T *ufunc = NULL;
534 type_T *type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100535
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200536 idx = find_exported(sid, name, &ufunc, &type, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100537
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100538 if (idx < 0 && ufunc == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200539 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100540
Bram Moolenaara6294952020-12-27 13:39:50 +0100541 // If already imported with the same propertis and the
542 // IMP_FLAGS_RELOAD set then we keep that entry. Otherwise create
543 // a new one (and give an error for an existing import).
544 imported = find_imported(name, len, cctx);
545 if (imported != NULL
546 && (imported->imp_flags & IMP_FLAGS_RELOAD)
547 && imported->imp_sid == sid
548 && (idx >= 0
549 ? (equal_type(imported->imp_type, type)
550 && imported->imp_var_vals_idx == idx)
551 : (equal_type(imported->imp_type, ufunc->uf_func_type)
552 && STRCMP(imported->imp_funcname,
553 ufunc->uf_name) == 0)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100554 {
Bram Moolenaara6294952020-12-27 13:39:50 +0100555 imported->imp_flags &= ~IMP_FLAGS_RELOAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100556 }
557 else
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200558 {
Bram Moolenaara6294952020-12-27 13:39:50 +0100559 if (check_defined(name, len, cctx) == FAIL)
560 goto erret;
561
562 imported = new_imported(gap != NULL ? gap
563 : &SCRIPT_ITEM(import_sid)->sn_imports);
564 if (imported == NULL)
565 goto erret;
566
567 // TODO: check for "as" following
568 // imported->imp_name = vim_strsave(as_name);
569 imported->imp_name = name;
570 ((char_u **)names.ga_data)[i] = NULL;
571 imported->imp_sid = sid;
572 if (idx >= 0)
573 {
574 imported->imp_type = type;
575 imported->imp_var_vals_idx = idx;
576 }
577 else
578 {
579 imported->imp_type = ufunc->uf_func_type;
580 imported->imp_funcname = ufunc->uf_name;
581 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200582 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100583 }
584 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200585erret:
586 ga_clear_strings(&names);
587 vim_free(as_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100588 return cmd_end;
589}
590
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200591/*
592 * Declare a script-local variable without init: "let var: type".
593 * "const" is an error since the value is missing.
594 * Returns a pointer to after the type.
595 */
596 char_u *
597vim9_declare_scriptvar(exarg_T *eap, char_u *arg)
598{
599 char_u *p;
600 char_u *name;
601 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
602 type_T *type;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200603 typval_T init_tv;
604
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200605 if (eap->cmdidx == CMD_final || eap->cmdidx == CMD_const)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200606 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200607 if (eap->cmdidx == CMD_final)
608 emsg(_(e_final_requires_a_value));
609 else
610 emsg(_(e_const_requires_a_value));
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200611 return arg + STRLEN(arg);
612 }
613
614 // Check for valid starting character.
615 if (!eval_isnamec1(*arg))
616 {
617 semsg(_(e_invarg2), arg);
618 return arg + STRLEN(arg);
619 }
620
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200621 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
Bram Moolenaar3b74b6b2020-06-19 19:01:43 +0200622 if (*p == ':' && (VIM_ISWHITE(p[1]) || p != arg + 1))
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200623 break;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200624
625 if (*p != ':')
626 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +0200627 emsg(_(e_type_or_initialization_required));
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200628 return arg + STRLEN(arg);
629 }
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200630 if (!VIM_ISWHITE(p[1]))
631 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200632 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200633 return arg + STRLEN(arg);
634 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200635 name = vim_strnsave(arg, p - arg);
636
637 // parse type
638 p = skipwhite(p + 1);
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100639 type = parse_type(&p, &si->sn_type_list, TRUE);
640 if (type == NULL)
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200641 {
642 vim_free(name);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200643 return p;
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200644 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200645
646 // Create the variable with 0/NULL value.
647 CLEAR_FIELD(init_tv);
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +0200648 if (type->tt_type == VAR_ANY)
649 // A variable of type "any" is not possible, just use zero instead
650 init_tv.v_type = VAR_NUMBER;
651 else
652 init_tv.v_type = type->tt_type;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200653 set_var_const(name, type, &init_tv, FALSE, 0);
654
655 vim_free(name);
656 return p;
657}
658
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200659/*
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200660 * Vim9 part of adding a script variable: add it to sn_all_vars (lookup by name
661 * with a hashtable) and sn_var_vals (lookup by index).
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100662 * When "create" is TRUE this is a new variable, otherwise find and update an
663 * existing variable.
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200664 * When "type" is NULL use "tv" for the type.
665 */
666 void
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100667update_vim9_script_var(int create, dictitem_T *di, typval_T *tv, type_T *type)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200668{
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100669 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
670 hashitem_T *hi;
671 svar_T *sv;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200672
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100673 if (create)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200674 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100675 sallvar_T *newsav;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200676
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100677 // Store a pointer to the typval_T, so that it can be found by index
678 // instead of using a hastab lookup.
679 if (ga_grow(&si->sn_var_vals, 1) == FAIL)
680 return;
681
682 sv = ((svar_T *)si->sn_var_vals.ga_data) + si->sn_var_vals.ga_len;
683 newsav = (sallvar_T *)alloc_clear(
684 sizeof(sallvar_T) + STRLEN(di->di_key));
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200685 if (newsav == NULL)
686 return;
687
688 sv->sv_tv = &di->di_tv;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200689 sv->sv_const = (di->di_flags & DI_FLAGS_LOCK) ? ASSIGN_CONST : 0;
690 sv->sv_export = is_export;
691 newsav->sav_var_vals_idx = si->sn_var_vals.ga_len;
692 ++si->sn_var_vals.ga_len;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200693 STRCPY(&newsav->sav_key, di->di_key);
694 sv->sv_name = newsav->sav_key;
695 newsav->sav_di = di;
696 newsav->sav_block_id = si->sn_current_block_id;
697
698 hi = hash_find(&si->sn_all_vars.dv_hashtab, newsav->sav_key);
699 if (!HASHITEM_EMPTY(hi))
700 {
701 sallvar_T *sav = HI2SAV(hi);
702
703 // variable with this name exists in another block
704 while (sav->sav_next != NULL)
705 sav = sav->sav_next;
706 sav->sav_next = newsav;
707 }
708 else
709 // new variable name
710 hash_add(&si->sn_all_vars.dv_hashtab, newsav->sav_key);
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200711 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100712 else
713 {
714 sv = find_typval_in_script(&di->di_tv);
715 }
716 if (sv != NULL)
717 {
718 if (type == NULL)
719 sv->sv_type = typval2type(tv, &si->sn_type_list);
720 else
721 sv->sv_type = type;
722 }
723
724 // let ex_export() know the export worked.
725 is_export = FALSE;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200726}
727
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200728/*
729 * Hide a script variable when leaving a block.
730 * "idx" is de index in sn_var_vals.
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200731 * When "func_defined" is non-zero then a function was defined in this block,
732 * the variable may be accessed by it. Otherwise the variable can be cleared.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200733 */
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200734 void
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200735hide_script_var(scriptitem_T *si, int idx, int func_defined)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200736{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200737 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200738 hashtab_T *script_ht = get_script_local_ht();
739 hashtab_T *all_ht = &si->sn_all_vars.dv_hashtab;
740 hashitem_T *script_hi;
741 hashitem_T *all_hi;
742
743 // Remove a variable declared inside the block, if it still exists.
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200744 // If it was added in a nested block it will already have been removed.
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200745 // The typval is moved into the sallvar_T.
746 script_hi = hash_find(script_ht, sv->sv_name);
747 all_hi = hash_find(all_ht, sv->sv_name);
748 if (!HASHITEM_EMPTY(script_hi) && !HASHITEM_EMPTY(all_hi))
749 {
750 dictitem_T *di = HI2DI(script_hi);
751 sallvar_T *sav = HI2SAV(all_hi);
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200752 sallvar_T *sav_prev = NULL;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200753
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200754 // There can be multiple entries with the same name in different
755 // blocks, find the right one.
756 while (sav != NULL && sav->sav_var_vals_idx != idx)
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200757 {
758 sav_prev = sav;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200759 sav = sav->sav_next;
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200760 }
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200761 if (sav != NULL)
762 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200763 if (func_defined)
764 {
765 // move the typval from the dictitem to the sallvar
766 sav->sav_tv = di->di_tv;
767 di->di_tv.v_type = VAR_UNKNOWN;
768 sav->sav_flags = di->di_flags;
769 sav->sav_di = NULL;
770 sv->sv_tv = &sav->sav_tv;
771 }
772 else
773 {
774 if (sav_prev == NULL)
775 hash_remove(all_ht, all_hi);
776 else
777 sav_prev->sav_next = sav->sav_next;
778 sv->sv_name = NULL;
779 vim_free(sav);
780 }
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200781 delete_var(script_ht, script_hi);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200782 }
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200783 }
784}
785
786/*
787 * Free the script variables from "sn_all_vars".
788 */
789 void
790free_all_script_vars(scriptitem_T *si)
791{
792 int todo;
793 hashtab_T *ht = &si->sn_all_vars.dv_hashtab;
794 hashitem_T *hi;
795 sallvar_T *sav;
796 sallvar_T *sav_next;
797
798 hash_lock(ht);
799 todo = (int)ht->ht_used;
800 for (hi = ht->ht_array; todo > 0; ++hi)
801 {
802 if (!HASHITEM_EMPTY(hi))
803 {
804 --todo;
805
806 // Free the variable. Don't remove it from the hashtab, ht_array
807 // might change then. hash_clear() takes care of it later.
808 sav = HI2SAV(hi);
809 while (sav != NULL)
810 {
811 sav_next = sav->sav_next;
812 if (sav->sav_di == NULL)
813 clear_tv(&sav->sav_tv);
814 vim_free(sav);
815 sav = sav_next;
816 }
817 }
818 }
819 hash_clear(ht);
820 hash_init(ht);
821
822 ga_clear(&si->sn_var_vals);
Bram Moolenaar2b327002020-12-26 15:39:31 +0100823
824 // existing commands using script variable indexes are no longer valid
825 si->sn_script_seq = current_sctx.sc_seq;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200826}
827
828/*
Bram Moolenaar10c65862020-10-08 21:16:42 +0200829 * Find the script-local variable that links to "dest".
830 * Returns NULL if not found.
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200831 */
Bram Moolenaar10c65862020-10-08 21:16:42 +0200832 svar_T *
833find_typval_in_script(typval_T *dest)
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200834{
835 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
836 int idx;
837
Bram Moolenaar81e17fb2020-08-21 21:55:43 +0200838 if (si->sn_version != SCRIPT_VERSION_VIM9)
839 // legacy script doesn't store variable types
Bram Moolenaar10c65862020-10-08 21:16:42 +0200840 return NULL;
Bram Moolenaar81e17fb2020-08-21 21:55:43 +0200841
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200842 // Find the svar_T in sn_var_vals.
843 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
844 {
845 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
846
Bram Moolenaarc0913d02020-12-05 14:44:37 +0100847 // If "sv_name" is NULL the variable was hidden when leaving a block,
848 // don't check "sv_tv" then, it might be used for another variable now.
849 if (sv->sv_name != NULL && sv->sv_tv == dest)
Bram Moolenaar10c65862020-10-08 21:16:42 +0200850 return sv;
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200851 }
852 iemsg("check_script_var_type(): not found");
Bram Moolenaar10c65862020-10-08 21:16:42 +0200853 return NULL;
854}
855
856/*
857 * Check if the type of script variable "dest" allows assigning "value".
858 * If needed convert "value" to a bool.
859 */
860 int
861check_script_var_type(typval_T *dest, typval_T *value, char_u *name)
862{
863 svar_T *sv = find_typval_in_script(dest);
864 int ret;
865
866 if (sv != NULL)
867 {
868 if (sv->sv_const)
869 {
870 semsg(_(e_readonlyvar), name);
871 return FAIL;
872 }
873 ret = check_typval_type(sv->sv_type, value, 0);
874 if (ret == OK && need_convert_to_bool(sv->sv_type, value))
875 {
876 int val = tv2bool(value);
877
878 clear_tv(value);
879 value->v_type = VAR_BOOL;
880 value->v_lock = 0;
881 value->vval.v_number = val ? VVAL_TRUE : VVAL_FALSE;
882 }
883 return ret;
884 }
885
Bram Moolenaarc785b9a2020-06-19 18:34:15 +0200886 return OK; // not really
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200887}
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200888
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100889#endif // FEAT_EVAL