blob: 751d8fb7701de6525571386e20085cdbe89619eb [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9script.c: :vim9script, :import, :export and friends
12 */
13
14#include "vim.h"
15
Bram Moolenaar9b8d6222020-12-28 18:26:00 +010016#if defined(FEAT_EVAL)
17# include "vim9.h"
18#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010019
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010020 int
21in_vim9script(void)
22{
Bram Moolenaareb6880b2020-07-12 17:07:05 +020023 // Do not go up the stack, a ":function" inside vim9script uses legacy
24 // syntax. "sc_version" is also set when compiling a ":def" function in
25 // legacy script.
Bram Moolenaar9979fcd2021-02-14 13:30:01 +010026 return current_sctx.sc_version == SCRIPT_VERSION_VIM9
27 || (cmdmod.cmod_flags & CMOD_VIM9CMD);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010028}
29
30/*
31 * ":vim9script".
32 */
33 void
Bram Moolenaar9b8d6222020-12-28 18:26:00 +010034ex_vim9script(exarg_T *eap UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010035{
Bram Moolenaar9b8d6222020-12-28 18:26:00 +010036#ifdef FEAT_EVAL
Bram Moolenaar2b327002020-12-26 15:39:31 +010037 int sid = current_sctx.sc_sid;
Bram Moolenaar101f4812020-06-16 23:18:51 +020038 scriptitem_T *si;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010039
40 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
41 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +020042 emsg(_(e_vim9script_can_only_be_used_in_script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010043 return;
44 }
Bram Moolenaar2b327002020-12-26 15:39:31 +010045
46 si = SCRIPT_ITEM(sid);
47 if (si->sn_state == SN_STATE_HAD_COMMAND)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010048 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +020049 emsg(_(e_vim9script_must_be_first_command_in_script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010050 return;
51 }
Bram Moolenaar2b327002020-12-26 15:39:31 +010052 if (!IS_WHITE_OR_NUL(*eap->arg) && STRCMP(eap->arg, "noclear") != 0)
53 {
54 semsg(_(e_invarg2), eap->arg);
55 return;
56 }
57 if (si->sn_state == SN_STATE_RELOAD && IS_WHITE_OR_NUL(*eap->arg))
58 {
59 hashtab_T *ht = &SCRIPT_VARS(sid);
60
61 // Reloading a script without the "noclear" argument: clear
62 // script-local variables and functions.
63 hashtab_free_contents(ht);
64 hash_init(ht);
65 delete_script_functions(sid);
66
67 // old imports and script variables are no longer valid
68 free_imports_and_script_vars(sid);
69 }
70 si->sn_state = SN_STATE_HAD_COMMAND;
71
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010072 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
73 si->sn_version = SCRIPT_VERSION_VIM9;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010074
75 if (STRCMP(p_cpo, CPO_VIM) != 0)
76 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +020077 si->sn_save_cpo = vim_strsave(p_cpo);
78 set_option_value((char_u *)"cpo", 0L, (char_u *)CPO_VIM, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010079 }
Bram Moolenaar9b8d6222020-12-28 18:26:00 +010080#else
81 // No check for this being the first command, it doesn't matter.
82 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
83#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010084}
85
86/*
Bram Moolenaarae616492020-07-28 20:07:27 +020087 * When in Vim9 script give an error and return FAIL.
88 */
89 int
90not_in_vim9(exarg_T *eap)
91{
Bram Moolenaar68e30442020-07-28 21:15:07 +020092 if (in_vim9script())
93 switch (eap->cmdidx)
94 {
Bram Moolenaar68e30442020-07-28 21:15:07 +020095 case CMD_append:
96 case CMD_change:
Bram Moolenaarf5a48012020-08-01 17:00:03 +020097 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +010098 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +020099 case CMD_t:
Bram Moolenaar68e30442020-07-28 21:15:07 +0200100 case CMD_xit:
Bram Moolenaar9b8d6222020-12-28 18:26:00 +0100101 semsg(_(e_command_not_supported_in_vim9_script_missing_var_str), eap->cmd);
Bram Moolenaar68e30442020-07-28 21:15:07 +0200102 return FAIL;
103 default: break;
104 }
Bram Moolenaarae616492020-07-28 20:07:27 +0200105 return OK;
106}
107
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100108/*
109 * Return TRUE if "p" points at a "#". Does not check for white space.
110 */
111 int
112vim9_comment_start(char_u *p)
113{
114 return p[0] == '#';
115}
116
Bram Moolenaar9b8d6222020-12-28 18:26:00 +0100117#if defined(FEAT_EVAL) || defined(PROTO)
118
Bram Moolenaarae616492020-07-28 20:07:27 +0200119/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100120 * ":export let Name: type"
121 * ":export const Name: type"
122 * ":export def Name(..."
123 * ":export class Name ..."
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100124 */
125 void
Bram Moolenaar09689a02020-05-09 22:50:08 +0200126ex_export(exarg_T *eap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100127{
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200128 if (!in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100129 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200130 emsg(_(e_export_can_only_be_used_in_vim9script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100131 return;
132 }
133
134 eap->cmd = eap->arg;
135 (void)find_ex_command(eap, NULL, lookup_scriptvar, NULL);
136 switch (eap->cmdidx)
137 {
138 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200139 case CMD_var:
140 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100141 case CMD_const:
142 case CMD_def:
143 // case CMD_class:
144 is_export = TRUE;
145 do_cmdline(eap->cmd, eap->getline, eap->cookie,
146 DOCMD_VERBOSE + DOCMD_NOWAIT);
147
148 // The command will reset "is_export" when exporting an item.
149 if (is_export)
150 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200151 emsg(_(e_export_with_invalid_argument));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100152 is_export = FALSE;
153 }
154 break;
155 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200156 emsg(_(e_invalid_command_after_export));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100157 break;
158 }
159}
160
161/*
162 * Add a new imported item entry to the current script.
163 */
164 static imported_T *
165new_imported(garray_T *gap)
166{
167 if (ga_grow(gap, 1) == OK)
168 return ((imported_T *)gap->ga_data + gap->ga_len++);
169 return NULL;
170}
171
172/*
173 * Free all imported items in script "sid".
174 */
175 void
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200176free_imports_and_script_vars(int sid)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100177{
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100178 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100179 int idx;
180
181 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
182 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100183 imported_T *imp = ((imported_T *)si->sn_imports.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100184
185 vim_free(imp->imp_name);
186 }
187 ga_clear(&si->sn_imports);
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200188
189 free_all_script_vars(si);
190
Bram Moolenaar6110e792020-07-08 19:35:21 +0200191 clear_type_list(&si->sn_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100192}
193
194/*
Bram Moolenaara6294952020-12-27 13:39:50 +0100195 * Mark all imports as possible to redefine. Used when a script is loaded
196 * again but not cleared.
197 */
198 void
199mark_imports_for_reload(int sid)
200{
201 scriptitem_T *si = SCRIPT_ITEM(sid);
202 int idx;
203
204 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
205 {
206 imported_T *imp = ((imported_T *)si->sn_imports.ga_data) + idx;
207
208 imp->imp_flags |= IMP_FLAGS_RELOAD;
209 }
210}
211
212/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100213 * ":import Item from 'filename'"
214 * ":import Item as Alias from 'filename'"
215 * ":import {Item} from 'filename'".
216 * ":import {Item as Alias} from 'filename'"
217 * ":import {Item, Item} from 'filename'"
218 * ":import {Item, Item as Alias} from 'filename'"
219 *
220 * ":import * as Name from 'filename'"
221 */
222 void
223ex_import(exarg_T *eap)
224{
Bram Moolenaar1c991142020-07-04 13:15:31 +0200225 char_u *cmd_end;
226 evalarg_T evalarg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100227
Bram Moolenaar101f4812020-06-16 23:18:51 +0200228 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
229 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200230 emsg(_(e_import_can_only_be_used_in_script));
Bram Moolenaar101f4812020-06-16 23:18:51 +0200231 return;
232 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200233 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar101f4812020-06-16 23:18:51 +0200234
Bram Moolenaar1c991142020-07-04 13:15:31 +0200235 cmd_end = handle_import(eap->arg, NULL, current_sctx.sc_sid,
236 &evalarg, NULL);
Bram Moolenaar9721fb42020-06-11 23:10:46 +0200237 if (cmd_end != NULL)
238 eap->nextcmd = check_nextcmd(cmd_end);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200239 clear_evalarg(&evalarg, eap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100240}
241
242/*
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100243 * Find an exported item in "sid" matching the name at "*argp".
244 * When it is a variable return the index.
245 * When it is a user function return "*ufunc".
246 * When not found returns -1 and "*ufunc" is NULL.
247 */
248 int
249find_exported(
250 int sid,
Bram Moolenaar1c991142020-07-04 13:15:31 +0200251 char_u *name,
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100252 ufunc_T **ufunc,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200253 type_T **type,
254 cctx_T *cctx)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100255{
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100256 int idx = -1;
257 svar_T *sv;
258 scriptitem_T *script = SCRIPT_ITEM(sid);
259
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100260 // find name in "script"
261 // TODO: also find script-local user function
Bram Moolenaar08251752021-01-11 21:20:18 +0100262 idx = get_script_item_idx(sid, name, 0, cctx);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100263 if (idx >= 0)
264 {
265 sv = ((svar_T *)script->sn_var_vals.ga_data) + idx;
266 if (!sv->sv_export)
267 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200268 semsg(_(e_item_not_exported_in_script_str), name);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100269 return -1;
270 }
271 *type = sv->sv_type;
272 *ufunc = NULL;
273 }
274 else
275 {
276 char_u buffer[200];
277 char_u *funcname;
278
279 // it could be a user function.
Bram Moolenaar5a67c372020-07-23 14:39:47 +0200280 if (STRLEN(name) < sizeof(buffer) - 15)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100281 funcname = buffer;
282 else
283 {
Bram Moolenaar5a67c372020-07-23 14:39:47 +0200284 funcname = alloc(STRLEN(name) + 15);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100285 if (funcname == NULL)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100286 return -1;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100287 }
288 funcname[0] = K_SPECIAL;
289 funcname[1] = KS_EXTRA;
290 funcname[2] = (int)KE_SNR;
291 sprintf((char *)funcname + 3, "%ld_%s", (long)sid, name);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200292 *ufunc = find_func(funcname, FALSE, NULL);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100293 if (funcname != buffer)
294 vim_free(funcname);
295
296 if (*ufunc == NULL)
297 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200298 semsg(_(e_item_not_found_in_script_str), name);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100299 return -1;
300 }
301 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100302
303 return idx;
304}
305
306/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100307 * Handle an ":import" command and add the resulting imported_T to "gap", when
308 * not NULL, or script "import_sid" sn_imports.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200309 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100310 * Returns a pointer to after the command or NULL in case of failure
311 */
312 char_u *
Bram Moolenaar1c991142020-07-04 13:15:31 +0200313handle_import(
314 char_u *arg_start,
315 garray_T *gap,
316 int import_sid,
317 evalarg_T *evalarg,
318 void *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100319{
320 char_u *arg = arg_start;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200321 char_u *cmd_end = NULL;
322 char_u *as_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100323 int ret = FAIL;
324 typval_T tv;
325 int sid = -1;
326 int res;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200327 garray_T names;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100328
Bram Moolenaar1c991142020-07-04 13:15:31 +0200329 ga_init2(&names, sizeof(char_u *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100330 if (*arg == '{')
331 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200332 // "import {item, item} from ..."
333 arg = skipwhite_and_linebreak(arg + 1, evalarg);
334 for (;;)
335 {
336 char_u *p = arg;
337 int had_comma = FALSE;
338
339 while (eval_isnamec(*arg))
340 ++arg;
341 if (p == arg)
342 break;
343 if (ga_grow(&names, 1) == FAIL)
344 goto erret;
345 ((char_u **)names.ga_data)[names.ga_len] =
346 vim_strnsave(p, arg - p);
347 ++names.ga_len;
348 if (*arg == ',')
349 {
350 had_comma = TRUE;
351 ++arg;
352 }
353 arg = skipwhite_and_linebreak(arg, evalarg);
354 if (*arg == '}')
355 {
356 arg = skipwhite_and_linebreak(arg + 1, evalarg);
357 break;
358 }
359 if (!had_comma)
360 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200361 emsg(_(e_missing_comma_in_import));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200362 goto erret;
363 }
364 }
365 if (names.ga_len == 0)
366 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200367 emsg(_(e_syntax_error_in_import));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200368 goto erret;
369 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100370 }
371 else
372 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200373 // "import Name from ..."
374 // "import * as Name from ..."
375 // "import item [as Name] from ..."
376 arg = skipwhite_and_linebreak(arg, evalarg);
377 if (arg[0] == '*' && IS_WHITE_OR_NUL(arg[1]))
378 arg = skipwhite_and_linebreak(arg + 1, evalarg);
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100379 else if (eval_isnamec1(*arg))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100380 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200381 char_u *p = arg;
382
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100383 while (eval_isnamec(*arg))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100384 ++arg;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200385 if (ga_grow(&names, 1) == FAIL)
386 goto erret;
387 ((char_u **)names.ga_data)[names.ga_len] =
388 vim_strnsave(p, arg - p);
389 ++names.ga_len;
390 arg = skipwhite_and_linebreak(arg, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100391 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200392 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100393 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200394 emsg(_(e_syntax_error_in_import));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200395 goto erret;
396 }
397
398 if (STRNCMP("as", arg, 2) == 0 && IS_WHITE_OR_NUL(arg[2]))
399 {
400 char_u *p;
401
402 // skip over "as Name "; no line break allowed after "as"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100403 arg = skipwhite(arg + 2);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200404 p = arg;
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100405 if (eval_isnamec1(*arg))
406 while (eval_isnamec(*arg))
407 ++arg;
Bram Moolenaarcbb6bdc2020-07-06 21:53:17 +0200408 if (check_defined(p, arg - p, cctx) == FAIL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200409 goto erret;
410 as_name = vim_strnsave(p, arg - p);
411 arg = skipwhite_and_linebreak(arg, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100412 }
413 else if (*arg_start == '*')
414 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200415 emsg(_(e_missing_as_after_star));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200416 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100417 }
418 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200419
420 if (STRNCMP("from", arg, 4) != 0 || !IS_WHITE_OR_NUL(arg[4]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100421 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200422 emsg(_(e_missing_from));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200423 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100424 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200425
Bram Moolenaar962d7212020-07-04 14:15:00 +0200426 arg = skipwhite_and_linebreak(arg + 4, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100427 tv.v_type = VAR_UNKNOWN;
428 // TODO: should we accept any expression?
429 if (*arg == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +0200430 ret = eval_lit_string(&arg, &tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100431 else if (*arg == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +0200432 ret = eval_string(&arg, &tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100433 if (ret == FAIL || tv.vval.v_string == NULL || *tv.vval.v_string == NUL)
434 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200435 emsg(_(e_invalid_string_after_from));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200436 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100437 }
438 cmd_end = arg;
439
Bram Moolenaar1c991142020-07-04 13:15:31 +0200440 /*
441 * find script file
442 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100443 if (*tv.vval.v_string == '.')
444 {
445 size_t len;
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100446 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100447 char_u *tail = gettail(si->sn_name);
448 char_u *from_name;
449
450 // Relative to current script: "./name.vim", "../../name.vim".
451 len = STRLEN(si->sn_name) - STRLEN(tail) + STRLEN(tv.vval.v_string) + 2;
452 from_name = alloc((int)len);
453 if (from_name == NULL)
454 {
455 clear_tv(&tv);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200456 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100457 }
458 vim_strncpy(from_name, si->sn_name, tail - si->sn_name);
459 add_pathsep(from_name);
460 STRCAT(from_name, tv.vval.v_string);
461 simplify_filename(from_name);
462
463 res = do_source(from_name, FALSE, DOSO_NONE, &sid);
464 vim_free(from_name);
465 }
466 else if (mch_isFullName(tv.vval.v_string))
467 {
468 // Absolute path: "/tmp/name.vim"
469 res = do_source(tv.vval.v_string, FALSE, DOSO_NONE, &sid);
470 }
471 else
472 {
473 size_t len = 7 + STRLEN(tv.vval.v_string) + 1;
474 char_u *from_name;
475
476 // Find file in "import" subdirs in 'runtimepath'.
477 from_name = alloc((int)len);
478 if (from_name == NULL)
479 {
480 clear_tv(&tv);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200481 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100482 }
483 vim_snprintf((char *)from_name, len, "import/%s", tv.vval.v_string);
484 res = source_in_path(p_rtp, from_name, DIP_NOAFTER, &sid);
485 vim_free(from_name);
486 }
487
488 if (res == FAIL || sid <= 0)
489 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200490 semsg(_(e_could_not_import_str), tv.vval.v_string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100491 clear_tv(&tv);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200492 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100493 }
494 clear_tv(&tv);
495
496 if (*arg_start == '*')
497 {
Bram Moolenaara6294952020-12-27 13:39:50 +0100498 imported_T *imported;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100499
Bram Moolenaara6294952020-12-27 13:39:50 +0100500 imported = find_imported(as_name, STRLEN(as_name), cctx);
501 if (imported != NULL && imported->imp_sid == sid)
502 {
503 if (imported->imp_flags & IMP_FLAGS_RELOAD)
504 // import already defined on a previous script load
505 imported->imp_flags &= ~IMP_FLAGS_RELOAD;
506 else
507 {
508 semsg(_(e_name_already_defined_str), as_name);
509 goto erret;
510 }
511 }
512
513 imported = new_imported(gap != NULL ? gap
514 : &SCRIPT_ITEM(import_sid)->sn_imports);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100515 if (imported == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200516 goto erret;
517 imported->imp_name = as_name;
518 as_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100519 imported->imp_sid = sid;
Bram Moolenaara6294952020-12-27 13:39:50 +0100520 imported->imp_flags = IMP_FLAGS_STAR;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100521 }
522 else
523 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200524 int i;
525
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100526 arg = arg_start;
527 if (*arg == '{')
528 arg = skipwhite(arg + 1);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200529 for (i = 0; i < names.ga_len; ++i)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100530 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200531 char_u *name = ((char_u **)names.ga_data)[i];
Bram Moolenaara6294952020-12-27 13:39:50 +0100532 size_t len = STRLEN(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100533 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100534 imported_T *imported;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100535 ufunc_T *ufunc = NULL;
536 type_T *type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100537
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200538 idx = find_exported(sid, name, &ufunc, &type, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100539
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100540 if (idx < 0 && ufunc == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200541 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100542
Bram Moolenaara6294952020-12-27 13:39:50 +0100543 // If already imported with the same propertis and the
544 // IMP_FLAGS_RELOAD set then we keep that entry. Otherwise create
545 // a new one (and give an error for an existing import).
546 imported = find_imported(name, len, cctx);
547 if (imported != NULL
548 && (imported->imp_flags & IMP_FLAGS_RELOAD)
549 && imported->imp_sid == sid
550 && (idx >= 0
551 ? (equal_type(imported->imp_type, type)
552 && imported->imp_var_vals_idx == idx)
553 : (equal_type(imported->imp_type, ufunc->uf_func_type)
554 && STRCMP(imported->imp_funcname,
555 ufunc->uf_name) == 0)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100556 {
Bram Moolenaara6294952020-12-27 13:39:50 +0100557 imported->imp_flags &= ~IMP_FLAGS_RELOAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100558 }
559 else
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200560 {
Bram Moolenaara6294952020-12-27 13:39:50 +0100561 if (check_defined(name, len, cctx) == FAIL)
562 goto erret;
563
564 imported = new_imported(gap != NULL ? gap
565 : &SCRIPT_ITEM(import_sid)->sn_imports);
566 if (imported == NULL)
567 goto erret;
568
569 // TODO: check for "as" following
570 // imported->imp_name = vim_strsave(as_name);
571 imported->imp_name = name;
572 ((char_u **)names.ga_data)[i] = NULL;
573 imported->imp_sid = sid;
574 if (idx >= 0)
575 {
576 imported->imp_type = type;
577 imported->imp_var_vals_idx = idx;
578 }
579 else
580 {
581 imported->imp_type = ufunc->uf_func_type;
582 imported->imp_funcname = ufunc->uf_name;
583 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200584 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100585 }
586 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200587erret:
588 ga_clear_strings(&names);
589 vim_free(as_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100590 return cmd_end;
591}
592
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200593/*
594 * Declare a script-local variable without init: "let var: type".
595 * "const" is an error since the value is missing.
596 * Returns a pointer to after the type.
597 */
598 char_u *
599vim9_declare_scriptvar(exarg_T *eap, char_u *arg)
600{
601 char_u *p;
602 char_u *name;
603 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
604 type_T *type;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200605 typval_T init_tv;
606
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200607 if (eap->cmdidx == CMD_final || eap->cmdidx == CMD_const)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200608 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200609 if (eap->cmdidx == CMD_final)
610 emsg(_(e_final_requires_a_value));
611 else
612 emsg(_(e_const_requires_a_value));
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200613 return arg + STRLEN(arg);
614 }
615
616 // Check for valid starting character.
617 if (!eval_isnamec1(*arg))
618 {
619 semsg(_(e_invarg2), arg);
620 return arg + STRLEN(arg);
621 }
622
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200623 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
Bram Moolenaar3b74b6b2020-06-19 19:01:43 +0200624 if (*p == ':' && (VIM_ISWHITE(p[1]) || p != arg + 1))
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200625 break;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200626
627 if (*p != ':')
628 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +0200629 emsg(_(e_type_or_initialization_required));
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200630 return arg + STRLEN(arg);
631 }
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200632 if (!VIM_ISWHITE(p[1]))
633 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100634 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200635 return arg + STRLEN(arg);
636 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200637 name = vim_strnsave(arg, p - arg);
638
639 // parse type
640 p = skipwhite(p + 1);
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100641 type = parse_type(&p, &si->sn_type_list, TRUE);
642 if (type == NULL)
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200643 {
644 vim_free(name);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200645 return p;
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200646 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200647
648 // Create the variable with 0/NULL value.
649 CLEAR_FIELD(init_tv);
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +0200650 if (type->tt_type == VAR_ANY)
651 // A variable of type "any" is not possible, just use zero instead
652 init_tv.v_type = VAR_NUMBER;
653 else
654 init_tv.v_type = type->tt_type;
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100655 set_var_const(name, type, &init_tv, FALSE, 0, 0);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200656
657 vim_free(name);
658 return p;
659}
660
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200661/*
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200662 * Vim9 part of adding a script variable: add it to sn_all_vars (lookup by name
663 * with a hashtable) and sn_var_vals (lookup by index).
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100664 * When "create" is TRUE this is a new variable, otherwise find and update an
665 * existing variable.
Bram Moolenaar08251752021-01-11 21:20:18 +0100666 * "flags" can have ASSIGN_FINAL or ASSIGN_CONST.
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100667 * When "*type" is NULL use "tv" for the type and update "*type".
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200668 */
669 void
Bram Moolenaar08251752021-01-11 21:20:18 +0100670update_vim9_script_var(
671 int create,
672 dictitem_T *di,
673 int flags,
674 typval_T *tv,
675 type_T **type)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200676{
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100677 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
678 hashitem_T *hi;
679 svar_T *sv;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200680
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100681 if (create)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200682 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100683 sallvar_T *newsav;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200684
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100685 // Store a pointer to the typval_T, so that it can be found by index
686 // instead of using a hastab lookup.
687 if (ga_grow(&si->sn_var_vals, 1) == FAIL)
688 return;
689
690 sv = ((svar_T *)si->sn_var_vals.ga_data) + si->sn_var_vals.ga_len;
691 newsav = (sallvar_T *)alloc_clear(
692 sizeof(sallvar_T) + STRLEN(di->di_key));
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200693 if (newsav == NULL)
694 return;
695
696 sv->sv_tv = &di->di_tv;
Bram Moolenaar08251752021-01-11 21:20:18 +0100697 sv->sv_const = (flags & ASSIGN_FINAL) ? ASSIGN_FINAL
698 : (flags & ASSIGN_CONST) ? ASSIGN_CONST : 0;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200699 sv->sv_export = is_export;
700 newsav->sav_var_vals_idx = si->sn_var_vals.ga_len;
701 ++si->sn_var_vals.ga_len;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200702 STRCPY(&newsav->sav_key, di->di_key);
703 sv->sv_name = newsav->sav_key;
704 newsav->sav_di = di;
705 newsav->sav_block_id = si->sn_current_block_id;
706
707 hi = hash_find(&si->sn_all_vars.dv_hashtab, newsav->sav_key);
708 if (!HASHITEM_EMPTY(hi))
709 {
710 sallvar_T *sav = HI2SAV(hi);
711
712 // variable with this name exists in another block
713 while (sav->sav_next != NULL)
714 sav = sav->sav_next;
715 sav->sav_next = newsav;
716 }
717 else
718 // new variable name
719 hash_add(&si->sn_all_vars.dv_hashtab, newsav->sav_key);
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200720 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100721 else
722 {
723 sv = find_typval_in_script(&di->di_tv);
724 }
725 if (sv != NULL)
726 {
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100727 if (*type == NULL)
728 *type = typval2type(tv, &si->sn_type_list);
729 sv->sv_type = *type;
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100730 }
731
732 // let ex_export() know the export worked.
733 is_export = FALSE;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200734}
735
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200736/*
737 * Hide a script variable when leaving a block.
738 * "idx" is de index in sn_var_vals.
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200739 * When "func_defined" is non-zero then a function was defined in this block,
740 * the variable may be accessed by it. Otherwise the variable can be cleared.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200741 */
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200742 void
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200743hide_script_var(scriptitem_T *si, int idx, int func_defined)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200744{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200745 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200746 hashtab_T *script_ht = get_script_local_ht();
747 hashtab_T *all_ht = &si->sn_all_vars.dv_hashtab;
748 hashitem_T *script_hi;
749 hashitem_T *all_hi;
750
751 // Remove a variable declared inside the block, if it still exists.
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200752 // If it was added in a nested block it will already have been removed.
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200753 // The typval is moved into the sallvar_T.
754 script_hi = hash_find(script_ht, sv->sv_name);
755 all_hi = hash_find(all_ht, sv->sv_name);
756 if (!HASHITEM_EMPTY(script_hi) && !HASHITEM_EMPTY(all_hi))
757 {
758 dictitem_T *di = HI2DI(script_hi);
759 sallvar_T *sav = HI2SAV(all_hi);
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200760 sallvar_T *sav_prev = NULL;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200761
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200762 // There can be multiple entries with the same name in different
763 // blocks, find the right one.
764 while (sav != NULL && sav->sav_var_vals_idx != idx)
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200765 {
766 sav_prev = sav;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200767 sav = sav->sav_next;
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200768 }
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200769 if (sav != NULL)
770 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200771 if (func_defined)
772 {
773 // move the typval from the dictitem to the sallvar
774 sav->sav_tv = di->di_tv;
775 di->di_tv.v_type = VAR_UNKNOWN;
776 sav->sav_flags = di->di_flags;
777 sav->sav_di = NULL;
778 sv->sv_tv = &sav->sav_tv;
779 }
780 else
781 {
782 if (sav_prev == NULL)
783 hash_remove(all_ht, all_hi);
784 else
785 sav_prev->sav_next = sav->sav_next;
786 sv->sv_name = NULL;
787 vim_free(sav);
788 }
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200789 delete_var(script_ht, script_hi);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200790 }
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200791 }
792}
793
794/*
795 * Free the script variables from "sn_all_vars".
796 */
797 void
798free_all_script_vars(scriptitem_T *si)
799{
800 int todo;
801 hashtab_T *ht = &si->sn_all_vars.dv_hashtab;
802 hashitem_T *hi;
803 sallvar_T *sav;
804 sallvar_T *sav_next;
805
806 hash_lock(ht);
807 todo = (int)ht->ht_used;
808 for (hi = ht->ht_array; todo > 0; ++hi)
809 {
810 if (!HASHITEM_EMPTY(hi))
811 {
812 --todo;
813
814 // Free the variable. Don't remove it from the hashtab, ht_array
815 // might change then. hash_clear() takes care of it later.
816 sav = HI2SAV(hi);
817 while (sav != NULL)
818 {
819 sav_next = sav->sav_next;
820 if (sav->sav_di == NULL)
821 clear_tv(&sav->sav_tv);
822 vim_free(sav);
823 sav = sav_next;
824 }
825 }
826 }
827 hash_clear(ht);
828 hash_init(ht);
829
830 ga_clear(&si->sn_var_vals);
Bram Moolenaar2b327002020-12-26 15:39:31 +0100831
832 // existing commands using script variable indexes are no longer valid
833 si->sn_script_seq = current_sctx.sc_seq;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200834}
835
836/*
Bram Moolenaar10c65862020-10-08 21:16:42 +0200837 * Find the script-local variable that links to "dest".
838 * Returns NULL if not found.
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200839 */
Bram Moolenaar10c65862020-10-08 21:16:42 +0200840 svar_T *
841find_typval_in_script(typval_T *dest)
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200842{
843 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
844 int idx;
845
Bram Moolenaar81e17fb2020-08-21 21:55:43 +0200846 if (si->sn_version != SCRIPT_VERSION_VIM9)
847 // legacy script doesn't store variable types
Bram Moolenaar10c65862020-10-08 21:16:42 +0200848 return NULL;
Bram Moolenaar81e17fb2020-08-21 21:55:43 +0200849
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200850 // Find the svar_T in sn_var_vals.
851 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
852 {
853 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
854
Bram Moolenaarc0913d02020-12-05 14:44:37 +0100855 // If "sv_name" is NULL the variable was hidden when leaving a block,
856 // don't check "sv_tv" then, it might be used for another variable now.
857 if (sv->sv_name != NULL && sv->sv_tv == dest)
Bram Moolenaar10c65862020-10-08 21:16:42 +0200858 return sv;
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200859 }
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100860 iemsg("find_typval_in_script(): not found");
Bram Moolenaar10c65862020-10-08 21:16:42 +0200861 return NULL;
862}
863
864/*
865 * Check if the type of script variable "dest" allows assigning "value".
866 * If needed convert "value" to a bool.
867 */
868 int
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100869check_script_var_type(
870 typval_T *dest,
871 typval_T *value,
872 char_u *name,
873 where_T where)
Bram Moolenaar10c65862020-10-08 21:16:42 +0200874{
875 svar_T *sv = find_typval_in_script(dest);
876 int ret;
877
878 if (sv != NULL)
879 {
Bram Moolenaar08251752021-01-11 21:20:18 +0100880 if (sv->sv_const != 0)
Bram Moolenaar10c65862020-10-08 21:16:42 +0200881 {
882 semsg(_(e_readonlyvar), name);
883 return FAIL;
884 }
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100885 ret = check_typval_type(sv->sv_type, value, where);
Bram Moolenaar10c65862020-10-08 21:16:42 +0200886 if (ret == OK && need_convert_to_bool(sv->sv_type, value))
887 {
888 int val = tv2bool(value);
889
890 clear_tv(value);
891 value->v_type = VAR_BOOL;
892 value->v_lock = 0;
893 value->vval.v_number = val ? VVAL_TRUE : VVAL_FALSE;
894 }
895 return ret;
896 }
897
Bram Moolenaarc785b9a2020-06-19 18:34:15 +0200898 return OK; // not really
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200899}
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200900
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100901#endif // FEAT_EVAL