blob: a6ed4ba79a6217b659c39610a388a3f715188a70 [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 Moolenaar101f4812020-06-16 23:18:51 +020035 scriptitem_T *si;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010036
37 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
38 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +020039 emsg(_(e_vim9script_can_only_be_used_in_script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010040 return;
41 }
Bram Moolenaar101f4812020-06-16 23:18:51 +020042 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010043 if (si->sn_had_command)
44 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +020045 emsg(_(e_vim9script_must_be_first_command_in_script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010046 return;
47 }
48 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
49 si->sn_version = SCRIPT_VERSION_VIM9;
50 si->sn_had_command = TRUE;
51
52 if (STRCMP(p_cpo, CPO_VIM) != 0)
53 {
54 si->sn_save_cpo = p_cpo;
55 p_cpo = vim_strsave((char_u *)CPO_VIM);
56 }
57}
58
59/*
Bram Moolenaarae616492020-07-28 20:07:27 +020060 * When in Vim9 script give an error and return FAIL.
61 */
62 int
63not_in_vim9(exarg_T *eap)
64{
Bram Moolenaar68e30442020-07-28 21:15:07 +020065 if (in_vim9script())
66 switch (eap->cmdidx)
67 {
Bram Moolenaar68e30442020-07-28 21:15:07 +020068 case CMD_append:
69 case CMD_change:
Bram Moolenaarf5a48012020-08-01 17:00:03 +020070 case CMD_insert:
71 case CMD_t:
Bram Moolenaar68e30442020-07-28 21:15:07 +020072 case CMD_xit:
Bram Moolenaar451c2e32020-08-15 16:33:28 +020073 semsg(_(e_missing_let_str), eap->cmd);
Bram Moolenaar68e30442020-07-28 21:15:07 +020074 return FAIL;
75 default: break;
76 }
Bram Moolenaarae616492020-07-28 20:07:27 +020077 return OK;
78}
79
80/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010081 * ":export let Name: type"
82 * ":export const Name: type"
83 * ":export def Name(..."
84 * ":export class Name ..."
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010085 */
86 void
Bram Moolenaar09689a02020-05-09 22:50:08 +020087ex_export(exarg_T *eap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010088{
Bram Moolenaareb6880b2020-07-12 17:07:05 +020089 if (!in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010090 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +020091 emsg(_(e_export_can_only_be_used_in_vim9script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010092 return;
93 }
94
95 eap->cmd = eap->arg;
96 (void)find_ex_command(eap, NULL, lookup_scriptvar, NULL);
97 switch (eap->cmdidx)
98 {
99 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200100 case CMD_var:
101 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100102 case CMD_const:
103 case CMD_def:
104 // case CMD_class:
105 is_export = TRUE;
106 do_cmdline(eap->cmd, eap->getline, eap->cookie,
107 DOCMD_VERBOSE + DOCMD_NOWAIT);
108
109 // The command will reset "is_export" when exporting an item.
110 if (is_export)
111 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200112 emsg(_(e_export_with_invalid_argument));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100113 is_export = FALSE;
114 }
115 break;
116 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200117 emsg(_(e_invalid_command_after_export));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100118 break;
119 }
120}
121
122/*
123 * Add a new imported item entry to the current script.
124 */
125 static imported_T *
126new_imported(garray_T *gap)
127{
128 if (ga_grow(gap, 1) == OK)
129 return ((imported_T *)gap->ga_data + gap->ga_len++);
130 return NULL;
131}
132
133/*
134 * Free all imported items in script "sid".
135 */
136 void
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200137free_imports_and_script_vars(int sid)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100138{
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100139 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100140 int idx;
141
142 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
143 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100144 imported_T *imp = ((imported_T *)si->sn_imports.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100145
146 vim_free(imp->imp_name);
147 }
148 ga_clear(&si->sn_imports);
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200149
150 free_all_script_vars(si);
151
Bram Moolenaar6110e792020-07-08 19:35:21 +0200152 clear_type_list(&si->sn_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100153}
154
155/*
156 * ":import Item from 'filename'"
157 * ":import Item as Alias from 'filename'"
158 * ":import {Item} from 'filename'".
159 * ":import {Item as Alias} from 'filename'"
160 * ":import {Item, Item} from 'filename'"
161 * ":import {Item, Item as Alias} from 'filename'"
162 *
163 * ":import * as Name from 'filename'"
164 */
165 void
166ex_import(exarg_T *eap)
167{
Bram Moolenaar1c991142020-07-04 13:15:31 +0200168 char_u *cmd_end;
169 evalarg_T evalarg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100170
Bram Moolenaar101f4812020-06-16 23:18:51 +0200171 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
172 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200173 emsg(_(e_import_can_only_be_used_in_script));
Bram Moolenaar101f4812020-06-16 23:18:51 +0200174 return;
175 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200176 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar101f4812020-06-16 23:18:51 +0200177
Bram Moolenaar1c991142020-07-04 13:15:31 +0200178 cmd_end = handle_import(eap->arg, NULL, current_sctx.sc_sid,
179 &evalarg, NULL);
Bram Moolenaar9721fb42020-06-11 23:10:46 +0200180 if (cmd_end != NULL)
181 eap->nextcmd = check_nextcmd(cmd_end);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200182 clear_evalarg(&evalarg, eap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100183}
184
185/*
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100186 * Find an exported item in "sid" matching the name at "*argp".
187 * When it is a variable return the index.
188 * When it is a user function return "*ufunc".
189 * When not found returns -1 and "*ufunc" is NULL.
190 */
191 int
192find_exported(
193 int sid,
Bram Moolenaar1c991142020-07-04 13:15:31 +0200194 char_u *name,
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100195 ufunc_T **ufunc,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200196 type_T **type,
197 cctx_T *cctx)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100198{
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100199 int idx = -1;
200 svar_T *sv;
201 scriptitem_T *script = SCRIPT_ITEM(sid);
202
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100203 // find name in "script"
204 // TODO: also find script-local user function
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200205 idx = get_script_item_idx(sid, name, FALSE, cctx);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100206 if (idx >= 0)
207 {
208 sv = ((svar_T *)script->sn_var_vals.ga_data) + idx;
209 if (!sv->sv_export)
210 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200211 semsg(_(e_item_not_exported_in_script_str), name);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100212 return -1;
213 }
214 *type = sv->sv_type;
215 *ufunc = NULL;
216 }
217 else
218 {
219 char_u buffer[200];
220 char_u *funcname;
221
222 // it could be a user function.
Bram Moolenaar5a67c372020-07-23 14:39:47 +0200223 if (STRLEN(name) < sizeof(buffer) - 15)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100224 funcname = buffer;
225 else
226 {
Bram Moolenaar5a67c372020-07-23 14:39:47 +0200227 funcname = alloc(STRLEN(name) + 15);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100228 if (funcname == NULL)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100229 return -1;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100230 }
231 funcname[0] = K_SPECIAL;
232 funcname[1] = KS_EXTRA;
233 funcname[2] = (int)KE_SNR;
234 sprintf((char *)funcname + 3, "%ld_%s", (long)sid, name);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200235 *ufunc = find_func(funcname, FALSE, NULL);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100236 if (funcname != buffer)
237 vim_free(funcname);
238
239 if (*ufunc == NULL)
240 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200241 semsg(_(e_item_not_found_in_script_str), name);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100242 return -1;
243 }
244 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100245
246 return idx;
247}
248
249/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100250 * Handle an ":import" command and add the resulting imported_T to "gap", when
251 * not NULL, or script "import_sid" sn_imports.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200252 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100253 * Returns a pointer to after the command or NULL in case of failure
254 */
255 char_u *
Bram Moolenaar1c991142020-07-04 13:15:31 +0200256handle_import(
257 char_u *arg_start,
258 garray_T *gap,
259 int import_sid,
260 evalarg_T *evalarg,
261 void *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100262{
263 char_u *arg = arg_start;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200264 char_u *cmd_end = NULL;
265 char_u *as_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100266 int ret = FAIL;
267 typval_T tv;
268 int sid = -1;
269 int res;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200270 garray_T names;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100271
Bram Moolenaar1c991142020-07-04 13:15:31 +0200272 ga_init2(&names, sizeof(char_u *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100273 if (*arg == '{')
274 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200275 // "import {item, item} from ..."
276 arg = skipwhite_and_linebreak(arg + 1, evalarg);
277 for (;;)
278 {
279 char_u *p = arg;
280 int had_comma = FALSE;
281
282 while (eval_isnamec(*arg))
283 ++arg;
284 if (p == arg)
285 break;
286 if (ga_grow(&names, 1) == FAIL)
287 goto erret;
288 ((char_u **)names.ga_data)[names.ga_len] =
289 vim_strnsave(p, arg - p);
290 ++names.ga_len;
291 if (*arg == ',')
292 {
293 had_comma = TRUE;
294 ++arg;
295 }
296 arg = skipwhite_and_linebreak(arg, evalarg);
297 if (*arg == '}')
298 {
299 arg = skipwhite_and_linebreak(arg + 1, evalarg);
300 break;
301 }
302 if (!had_comma)
303 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200304 emsg(_(e_missing_comma_in_import));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200305 goto erret;
306 }
307 }
308 if (names.ga_len == 0)
309 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200310 emsg(_(e_syntax_error_in_import));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200311 goto erret;
312 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100313 }
314 else
315 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200316 // "import Name from ..."
317 // "import * as Name from ..."
318 // "import item [as Name] from ..."
319 arg = skipwhite_and_linebreak(arg, evalarg);
320 if (arg[0] == '*' && IS_WHITE_OR_NUL(arg[1]))
321 arg = skipwhite_and_linebreak(arg + 1, evalarg);
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100322 else if (eval_isnamec1(*arg))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100323 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200324 char_u *p = arg;
325
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100326 while (eval_isnamec(*arg))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100327 ++arg;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200328 if (ga_grow(&names, 1) == FAIL)
329 goto erret;
330 ((char_u **)names.ga_data)[names.ga_len] =
331 vim_strnsave(p, arg - p);
332 ++names.ga_len;
333 arg = skipwhite_and_linebreak(arg, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100334 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200335 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100336 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200337 emsg(_(e_syntax_error_in_import));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200338 goto erret;
339 }
340
341 if (STRNCMP("as", arg, 2) == 0 && IS_WHITE_OR_NUL(arg[2]))
342 {
343 char_u *p;
344
345 // skip over "as Name "; no line break allowed after "as"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100346 arg = skipwhite(arg + 2);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200347 p = arg;
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100348 if (eval_isnamec1(*arg))
349 while (eval_isnamec(*arg))
350 ++arg;
Bram Moolenaarcbb6bdc2020-07-06 21:53:17 +0200351 if (check_defined(p, arg - p, cctx) == FAIL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200352 goto erret;
353 as_name = vim_strnsave(p, arg - p);
354 arg = skipwhite_and_linebreak(arg, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100355 }
356 else if (*arg_start == '*')
357 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200358 emsg(_(e_missing_as_after_star));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200359 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100360 }
361 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200362
363 if (STRNCMP("from", arg, 4) != 0 || !IS_WHITE_OR_NUL(arg[4]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100364 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200365 emsg(_(e_missing_from));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200366 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100367 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200368
Bram Moolenaar962d7212020-07-04 14:15:00 +0200369 arg = skipwhite_and_linebreak(arg + 4, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100370 tv.v_type = VAR_UNKNOWN;
371 // TODO: should we accept any expression?
372 if (*arg == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +0200373 ret = eval_lit_string(&arg, &tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100374 else if (*arg == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +0200375 ret = eval_string(&arg, &tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100376 if (ret == FAIL || tv.vval.v_string == NULL || *tv.vval.v_string == NUL)
377 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200378 emsg(_(e_invalid_string_after_from));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200379 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100380 }
381 cmd_end = arg;
382
Bram Moolenaar1c991142020-07-04 13:15:31 +0200383 /*
384 * find script file
385 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100386 if (*tv.vval.v_string == '.')
387 {
388 size_t len;
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100389 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100390 char_u *tail = gettail(si->sn_name);
391 char_u *from_name;
392
393 // Relative to current script: "./name.vim", "../../name.vim".
394 len = STRLEN(si->sn_name) - STRLEN(tail) + STRLEN(tv.vval.v_string) + 2;
395 from_name = alloc((int)len);
396 if (from_name == NULL)
397 {
398 clear_tv(&tv);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200399 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100400 }
401 vim_strncpy(from_name, si->sn_name, tail - si->sn_name);
402 add_pathsep(from_name);
403 STRCAT(from_name, tv.vval.v_string);
404 simplify_filename(from_name);
405
406 res = do_source(from_name, FALSE, DOSO_NONE, &sid);
407 vim_free(from_name);
408 }
409 else if (mch_isFullName(tv.vval.v_string))
410 {
411 // Absolute path: "/tmp/name.vim"
412 res = do_source(tv.vval.v_string, FALSE, DOSO_NONE, &sid);
413 }
414 else
415 {
416 size_t len = 7 + STRLEN(tv.vval.v_string) + 1;
417 char_u *from_name;
418
419 // Find file in "import" subdirs in 'runtimepath'.
420 from_name = alloc((int)len);
421 if (from_name == NULL)
422 {
423 clear_tv(&tv);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200424 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100425 }
426 vim_snprintf((char *)from_name, len, "import/%s", tv.vval.v_string);
427 res = source_in_path(p_rtp, from_name, DIP_NOAFTER, &sid);
428 vim_free(from_name);
429 }
430
431 if (res == FAIL || sid <= 0)
432 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200433 semsg(_(e_could_not_import_str), tv.vval.v_string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100434 clear_tv(&tv);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200435 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100436 }
437 clear_tv(&tv);
438
439 if (*arg_start == '*')
440 {
441 imported_T *imported = new_imported(gap != NULL ? gap
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100442 : &SCRIPT_ITEM(import_sid)->sn_imports);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100443
444 if (imported == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200445 goto erret;
446 imported->imp_name = as_name;
447 as_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100448 imported->imp_sid = sid;
449 imported->imp_all = TRUE;
450 }
451 else
452 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200453 int i;
454
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100455 arg = arg_start;
456 if (*arg == '{')
457 arg = skipwhite(arg + 1);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200458 for (i = 0; i < names.ga_len; ++i)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100459 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200460 char_u *name = ((char_u **)names.ga_data)[i];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100461 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100462 imported_T *imported;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100463 ufunc_T *ufunc = NULL;
464 type_T *type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100465
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200466 idx = find_exported(sid, name, &ufunc, &type, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100467
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100468 if (idx < 0 && ufunc == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200469 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100470
Bram Moolenaar1c991142020-07-04 13:15:31 +0200471 if (check_defined(name, STRLEN(name), cctx) == FAIL)
472 goto erret;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100473
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100474 imported = new_imported(gap != NULL ? gap
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100475 : &SCRIPT_ITEM(import_sid)->sn_imports);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100476 if (imported == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200477 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100478
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100479 // TODO: check for "as" following
Bram Moolenaar1c991142020-07-04 13:15:31 +0200480 // imported->imp_name = vim_strsave(as_name);
481 imported->imp_name = name;
482 ((char_u **)names.ga_data)[i] = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100483 imported->imp_sid = sid;
484 if (idx >= 0)
485 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100486 imported->imp_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100487 imported->imp_var_vals_idx = idx;
488 }
489 else
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200490 {
491 imported->imp_type = ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100492 imported->imp_funcname = ufunc->uf_name;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200493 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100494 }
495 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200496erret:
497 ga_clear_strings(&names);
498 vim_free(as_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100499 return cmd_end;
500}
501
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200502/*
503 * Declare a script-local variable without init: "let var: type".
504 * "const" is an error since the value is missing.
505 * Returns a pointer to after the type.
506 */
507 char_u *
508vim9_declare_scriptvar(exarg_T *eap, char_u *arg)
509{
510 char_u *p;
511 char_u *name;
512 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
513 type_T *type;
514 int called_emsg_before = called_emsg;
515 typval_T init_tv;
516
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200517 if (eap->cmdidx == CMD_final || eap->cmdidx == CMD_const)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200518 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200519 if (eap->cmdidx == CMD_final)
520 emsg(_(e_final_requires_a_value));
521 else
522 emsg(_(e_const_requires_a_value));
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200523 return arg + STRLEN(arg);
524 }
525
526 // Check for valid starting character.
527 if (!eval_isnamec1(*arg))
528 {
529 semsg(_(e_invarg2), arg);
530 return arg + STRLEN(arg);
531 }
532
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200533 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
Bram Moolenaar3b74b6b2020-06-19 19:01:43 +0200534 if (*p == ':' && (VIM_ISWHITE(p[1]) || p != arg + 1))
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200535 break;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200536
537 if (*p != ':')
538 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +0200539 emsg(_(e_type_or_initialization_required));
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200540 return arg + STRLEN(arg);
541 }
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200542 if (!VIM_ISWHITE(p[1]))
543 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200544 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200545 return arg + STRLEN(arg);
546 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200547 name = vim_strnsave(arg, p - arg);
548
549 // parse type
550 p = skipwhite(p + 1);
551 type = parse_type(&p, &si->sn_type_list);
552 if (called_emsg != called_emsg_before)
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200553 {
554 vim_free(name);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200555 return p;
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200556 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200557
558 // Create the variable with 0/NULL value.
559 CLEAR_FIELD(init_tv);
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +0200560 if (type->tt_type == VAR_ANY)
561 // A variable of type "any" is not possible, just use zero instead
562 init_tv.v_type = VAR_NUMBER;
563 else
564 init_tv.v_type = type->tt_type;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200565 set_var_const(name, type, &init_tv, FALSE, 0);
566
567 vim_free(name);
568 return p;
569}
570
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200571/*
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200572 * Vim9 part of adding a script variable: add it to sn_all_vars and
573 * sn_var_vals.
574 * When "type" is NULL use "tv" for the type.
575 */
576 void
577add_vim9_script_var(dictitem_T *di, typval_T *tv, type_T *type)
578{
579 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
580
581 // Store a pointer to the typval_T, so that it can be found by
582 // index instead of using a hastab lookup.
583 if (ga_grow(&si->sn_var_vals, 1) == OK)
584 {
585 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
586 + si->sn_var_vals.ga_len;
587 hashitem_T *hi;
588 sallvar_T *newsav = (sallvar_T *)alloc_clear(
589 sizeof(sallvar_T) + STRLEN(di->di_key));
590
591 if (newsav == NULL)
592 return;
593
594 sv->sv_tv = &di->di_tv;
595 if (type == NULL)
596 sv->sv_type = typval2type(tv, &si->sn_type_list);
597 else
598 sv->sv_type = type;
599 sv->sv_const = (di->di_flags & DI_FLAGS_LOCK) ? ASSIGN_CONST : 0;
600 sv->sv_export = is_export;
601 newsav->sav_var_vals_idx = si->sn_var_vals.ga_len;
602 ++si->sn_var_vals.ga_len;
603
604 STRCPY(&newsav->sav_key, di->di_key);
605 sv->sv_name = newsav->sav_key;
606 newsav->sav_di = di;
607 newsav->sav_block_id = si->sn_current_block_id;
608
609 hi = hash_find(&si->sn_all_vars.dv_hashtab, newsav->sav_key);
610 if (!HASHITEM_EMPTY(hi))
611 {
612 sallvar_T *sav = HI2SAV(hi);
613
614 // variable with this name exists in another block
615 while (sav->sav_next != NULL)
616 sav = sav->sav_next;
617 sav->sav_next = newsav;
618 }
619 else
620 // new variable name
621 hash_add(&si->sn_all_vars.dv_hashtab, newsav->sav_key);
622
623 // let ex_export() know the export worked.
624 is_export = FALSE;
625 }
626}
627
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200628/*
629 * Hide a script variable when leaving a block.
630 * "idx" is de index in sn_var_vals.
631 */
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200632 void
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200633hide_script_var(scriptitem_T *si, int idx)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200634{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200635 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200636 hashtab_T *script_ht = get_script_local_ht();
637 hashtab_T *all_ht = &si->sn_all_vars.dv_hashtab;
638 hashitem_T *script_hi;
639 hashitem_T *all_hi;
640
641 // Remove a variable declared inside the block, if it still exists.
642 // The typval is moved into the sallvar_T.
643 script_hi = hash_find(script_ht, sv->sv_name);
644 all_hi = hash_find(all_ht, sv->sv_name);
645 if (!HASHITEM_EMPTY(script_hi) && !HASHITEM_EMPTY(all_hi))
646 {
647 dictitem_T *di = HI2DI(script_hi);
648 sallvar_T *sav = HI2SAV(all_hi);
649
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200650 // There can be multiple entries with the same name in different
651 // blocks, find the right one.
652 while (sav != NULL && sav->sav_var_vals_idx != idx)
653 sav = sav->sav_next;
654 if (sav != NULL)
655 {
656 sav->sav_tv = di->di_tv;
657 di->di_tv.v_type = VAR_UNKNOWN;
658 sav->sav_flags = di->di_flags;
659 sav->sav_di = NULL;
660 delete_var(script_ht, script_hi);
661 sv->sv_tv = &sav->sav_tv;
662 }
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200663 }
664}
665
666/*
667 * Free the script variables from "sn_all_vars".
668 */
669 void
670free_all_script_vars(scriptitem_T *si)
671{
672 int todo;
673 hashtab_T *ht = &si->sn_all_vars.dv_hashtab;
674 hashitem_T *hi;
675 sallvar_T *sav;
676 sallvar_T *sav_next;
677
678 hash_lock(ht);
679 todo = (int)ht->ht_used;
680 for (hi = ht->ht_array; todo > 0; ++hi)
681 {
682 if (!HASHITEM_EMPTY(hi))
683 {
684 --todo;
685
686 // Free the variable. Don't remove it from the hashtab, ht_array
687 // might change then. hash_clear() takes care of it later.
688 sav = HI2SAV(hi);
689 while (sav != NULL)
690 {
691 sav_next = sav->sav_next;
692 if (sav->sav_di == NULL)
693 clear_tv(&sav->sav_tv);
694 vim_free(sav);
695 sav = sav_next;
696 }
697 }
698 }
699 hash_clear(ht);
700 hash_init(ht);
701
702 ga_clear(&si->sn_var_vals);
703}
704
705/*
Bram Moolenaar10c65862020-10-08 21:16:42 +0200706 * Find the script-local variable that links to "dest".
707 * Returns NULL if not found.
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200708 */
Bram Moolenaar10c65862020-10-08 21:16:42 +0200709 svar_T *
710find_typval_in_script(typval_T *dest)
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200711{
712 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
713 int idx;
714
Bram Moolenaar81e17fb2020-08-21 21:55:43 +0200715 if (si->sn_version != SCRIPT_VERSION_VIM9)
716 // legacy script doesn't store variable types
Bram Moolenaar10c65862020-10-08 21:16:42 +0200717 return NULL;
Bram Moolenaar81e17fb2020-08-21 21:55:43 +0200718
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200719 // Find the svar_T in sn_var_vals.
720 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
721 {
722 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
723
724 if (sv->sv_tv == dest)
Bram Moolenaar10c65862020-10-08 21:16:42 +0200725 return sv;
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200726 }
727 iemsg("check_script_var_type(): not found");
Bram Moolenaar10c65862020-10-08 21:16:42 +0200728 return NULL;
729}
730
731/*
732 * Check if the type of script variable "dest" allows assigning "value".
733 * If needed convert "value" to a bool.
734 */
735 int
736check_script_var_type(typval_T *dest, typval_T *value, char_u *name)
737{
738 svar_T *sv = find_typval_in_script(dest);
739 int ret;
740
741 if (sv != NULL)
742 {
743 if (sv->sv_const)
744 {
745 semsg(_(e_readonlyvar), name);
746 return FAIL;
747 }
748 ret = check_typval_type(sv->sv_type, value, 0);
749 if (ret == OK && need_convert_to_bool(sv->sv_type, value))
750 {
751 int val = tv2bool(value);
752
753 clear_tv(value);
754 value->v_type = VAR_BOOL;
755 value->v_lock = 0;
756 value->vval.v_number = val ? VVAL_TRUE : VVAL_FALSE;
757 }
758 return ret;
759 }
760
Bram Moolenaarc785b9a2020-06-19 18:34:15 +0200761 return OK; // not really
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200762}
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200763
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100764#endif // FEAT_EVAL