blob: 74d0579f35e695d99b8dec7dd54163ae61d0f857 [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:
100 case CMD_const:
101 case CMD_def:
102 // case CMD_class:
103 is_export = TRUE;
104 do_cmdline(eap->cmd, eap->getline, eap->cookie,
105 DOCMD_VERBOSE + DOCMD_NOWAIT);
106
107 // The command will reset "is_export" when exporting an item.
108 if (is_export)
109 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200110 emsg(_(e_export_with_invalid_argument));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100111 is_export = FALSE;
112 }
113 break;
114 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200115 emsg(_(e_invalid_command_after_export));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100116 break;
117 }
118}
119
120/*
121 * Add a new imported item entry to the current script.
122 */
123 static imported_T *
124new_imported(garray_T *gap)
125{
126 if (ga_grow(gap, 1) == OK)
127 return ((imported_T *)gap->ga_data + gap->ga_len++);
128 return NULL;
129}
130
131/*
132 * Free all imported items in script "sid".
133 */
134 void
135free_imports(int sid)
136{
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100137 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100138 int idx;
139
140 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
141 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100142 imported_T *imp = ((imported_T *)si->sn_imports.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100143
144 vim_free(imp->imp_name);
145 }
146 ga_clear(&si->sn_imports);
Bram Moolenaar20431c92020-03-20 18:39:46 +0100147 ga_clear(&si->sn_var_vals);
Bram Moolenaar6110e792020-07-08 19:35:21 +0200148 clear_type_list(&si->sn_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100149}
150
151/*
152 * ":import Item from 'filename'"
153 * ":import Item as Alias from 'filename'"
154 * ":import {Item} from 'filename'".
155 * ":import {Item as Alias} from 'filename'"
156 * ":import {Item, Item} from 'filename'"
157 * ":import {Item, Item as Alias} from 'filename'"
158 *
159 * ":import * as Name from 'filename'"
160 */
161 void
162ex_import(exarg_T *eap)
163{
Bram Moolenaar1c991142020-07-04 13:15:31 +0200164 char_u *cmd_end;
165 evalarg_T evalarg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100166
Bram Moolenaar101f4812020-06-16 23:18:51 +0200167 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
168 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200169 emsg(_(e_import_can_only_be_used_in_script));
Bram Moolenaar101f4812020-06-16 23:18:51 +0200170 return;
171 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200172 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar101f4812020-06-16 23:18:51 +0200173
Bram Moolenaar1c991142020-07-04 13:15:31 +0200174 cmd_end = handle_import(eap->arg, NULL, current_sctx.sc_sid,
175 &evalarg, NULL);
Bram Moolenaar9721fb42020-06-11 23:10:46 +0200176 if (cmd_end != NULL)
177 eap->nextcmd = check_nextcmd(cmd_end);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200178 clear_evalarg(&evalarg, eap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100179}
180
181/*
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100182 * Find an exported item in "sid" matching the name at "*argp".
183 * When it is a variable return the index.
184 * When it is a user function return "*ufunc".
185 * When not found returns -1 and "*ufunc" is NULL.
186 */
187 int
188find_exported(
189 int sid,
Bram Moolenaar1c991142020-07-04 13:15:31 +0200190 char_u *name,
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100191 ufunc_T **ufunc,
192 type_T **type)
193{
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100194 int idx = -1;
195 svar_T *sv;
196 scriptitem_T *script = SCRIPT_ITEM(sid);
197
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100198 // find name in "script"
199 // TODO: also find script-local user function
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100200 idx = get_script_item_idx(sid, name, FALSE);
201 if (idx >= 0)
202 {
203 sv = ((svar_T *)script->sn_var_vals.ga_data) + idx;
204 if (!sv->sv_export)
205 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200206 semsg(_(e_item_not_exported_in_script_str), name);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100207 return -1;
208 }
209 *type = sv->sv_type;
210 *ufunc = NULL;
211 }
212 else
213 {
214 char_u buffer[200];
215 char_u *funcname;
216
217 // it could be a user function.
Bram Moolenaar5a67c372020-07-23 14:39:47 +0200218 if (STRLEN(name) < sizeof(buffer) - 15)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100219 funcname = buffer;
220 else
221 {
Bram Moolenaar5a67c372020-07-23 14:39:47 +0200222 funcname = alloc(STRLEN(name) + 15);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100223 if (funcname == NULL)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100224 return -1;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100225 }
226 funcname[0] = K_SPECIAL;
227 funcname[1] = KS_EXTRA;
228 funcname[2] = (int)KE_SNR;
229 sprintf((char *)funcname + 3, "%ld_%s", (long)sid, name);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200230 *ufunc = find_func(funcname, FALSE, NULL);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100231 if (funcname != buffer)
232 vim_free(funcname);
233
234 if (*ufunc == NULL)
235 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200236 semsg(_(e_item_not_found_in_script_str), name);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100237 return -1;
238 }
239 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100240
241 return idx;
242}
243
244/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100245 * Handle an ":import" command and add the resulting imported_T to "gap", when
246 * not NULL, or script "import_sid" sn_imports.
247 * Returns a pointer to after the command or NULL in case of failure
248 */
249 char_u *
Bram Moolenaar1c991142020-07-04 13:15:31 +0200250handle_import(
251 char_u *arg_start,
252 garray_T *gap,
253 int import_sid,
254 evalarg_T *evalarg,
255 void *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100256{
257 char_u *arg = arg_start;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200258 char_u *cmd_end = NULL;
259 char_u *as_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100260 int ret = FAIL;
261 typval_T tv;
262 int sid = -1;
263 int res;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200264 garray_T names;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100265
Bram Moolenaar1c991142020-07-04 13:15:31 +0200266 ga_init2(&names, sizeof(char_u *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100267 if (*arg == '{')
268 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200269 // "import {item, item} from ..."
270 arg = skipwhite_and_linebreak(arg + 1, evalarg);
271 for (;;)
272 {
273 char_u *p = arg;
274 int had_comma = FALSE;
275
276 while (eval_isnamec(*arg))
277 ++arg;
278 if (p == arg)
279 break;
280 if (ga_grow(&names, 1) == FAIL)
281 goto erret;
282 ((char_u **)names.ga_data)[names.ga_len] =
283 vim_strnsave(p, arg - p);
284 ++names.ga_len;
285 if (*arg == ',')
286 {
287 had_comma = TRUE;
288 ++arg;
289 }
290 arg = skipwhite_and_linebreak(arg, evalarg);
291 if (*arg == '}')
292 {
293 arg = skipwhite_and_linebreak(arg + 1, evalarg);
294 break;
295 }
296 if (!had_comma)
297 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200298 emsg(_(e_missing_comma_in_import));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200299 goto erret;
300 }
301 }
302 if (names.ga_len == 0)
303 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200304 emsg(_(e_syntax_error_in_import));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200305 goto erret;
306 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100307 }
308 else
309 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200310 // "import Name from ..."
311 // "import * as Name from ..."
312 // "import item [as Name] from ..."
313 arg = skipwhite_and_linebreak(arg, evalarg);
314 if (arg[0] == '*' && IS_WHITE_OR_NUL(arg[1]))
315 arg = skipwhite_and_linebreak(arg + 1, evalarg);
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100316 else if (eval_isnamec1(*arg))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100317 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200318 char_u *p = arg;
319
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100320 while (eval_isnamec(*arg))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100321 ++arg;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200322 if (ga_grow(&names, 1) == FAIL)
323 goto erret;
324 ((char_u **)names.ga_data)[names.ga_len] =
325 vim_strnsave(p, arg - p);
326 ++names.ga_len;
327 arg = skipwhite_and_linebreak(arg, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100328 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200329 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100330 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200331 emsg(_(e_syntax_error_in_import));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200332 goto erret;
333 }
334
335 if (STRNCMP("as", arg, 2) == 0 && IS_WHITE_OR_NUL(arg[2]))
336 {
337 char_u *p;
338
339 // skip over "as Name "; no line break allowed after "as"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100340 arg = skipwhite(arg + 2);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200341 p = arg;
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100342 if (eval_isnamec1(*arg))
343 while (eval_isnamec(*arg))
344 ++arg;
Bram Moolenaarcbb6bdc2020-07-06 21:53:17 +0200345 if (check_defined(p, arg - p, cctx) == FAIL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200346 goto erret;
347 as_name = vim_strnsave(p, arg - p);
348 arg = skipwhite_and_linebreak(arg, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100349 }
350 else if (*arg_start == '*')
351 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200352 emsg(_(e_missing_as_after_star));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200353 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100354 }
355 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200356
357 if (STRNCMP("from", arg, 4) != 0 || !IS_WHITE_OR_NUL(arg[4]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100358 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200359 emsg(_(e_missing_from));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200360 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100361 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200362
Bram Moolenaar962d7212020-07-04 14:15:00 +0200363 arg = skipwhite_and_linebreak(arg + 4, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100364 tv.v_type = VAR_UNKNOWN;
365 // TODO: should we accept any expression?
366 if (*arg == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +0200367 ret = eval_lit_string(&arg, &tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100368 else if (*arg == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +0200369 ret = eval_string(&arg, &tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100370 if (ret == FAIL || tv.vval.v_string == NULL || *tv.vval.v_string == NUL)
371 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200372 emsg(_(e_invalid_string_after_from));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200373 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100374 }
375 cmd_end = arg;
376
Bram Moolenaar1c991142020-07-04 13:15:31 +0200377 /*
378 * find script file
379 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100380 if (*tv.vval.v_string == '.')
381 {
382 size_t len;
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100383 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100384 char_u *tail = gettail(si->sn_name);
385 char_u *from_name;
386
387 // Relative to current script: "./name.vim", "../../name.vim".
388 len = STRLEN(si->sn_name) - STRLEN(tail) + STRLEN(tv.vval.v_string) + 2;
389 from_name = alloc((int)len);
390 if (from_name == NULL)
391 {
392 clear_tv(&tv);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200393 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100394 }
395 vim_strncpy(from_name, si->sn_name, tail - si->sn_name);
396 add_pathsep(from_name);
397 STRCAT(from_name, tv.vval.v_string);
398 simplify_filename(from_name);
399
400 res = do_source(from_name, FALSE, DOSO_NONE, &sid);
401 vim_free(from_name);
402 }
403 else if (mch_isFullName(tv.vval.v_string))
404 {
405 // Absolute path: "/tmp/name.vim"
406 res = do_source(tv.vval.v_string, FALSE, DOSO_NONE, &sid);
407 }
408 else
409 {
410 size_t len = 7 + STRLEN(tv.vval.v_string) + 1;
411 char_u *from_name;
412
413 // Find file in "import" subdirs in 'runtimepath'.
414 from_name = alloc((int)len);
415 if (from_name == NULL)
416 {
417 clear_tv(&tv);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200418 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100419 }
420 vim_snprintf((char *)from_name, len, "import/%s", tv.vval.v_string);
421 res = source_in_path(p_rtp, from_name, DIP_NOAFTER, &sid);
422 vim_free(from_name);
423 }
424
425 if (res == FAIL || sid <= 0)
426 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200427 semsg(_(e_could_not_import_str), tv.vval.v_string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100428 clear_tv(&tv);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200429 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100430 }
431 clear_tv(&tv);
432
433 if (*arg_start == '*')
434 {
435 imported_T *imported = new_imported(gap != NULL ? gap
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100436 : &SCRIPT_ITEM(import_sid)->sn_imports);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100437
438 if (imported == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200439 goto erret;
440 imported->imp_name = as_name;
441 as_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100442 imported->imp_sid = sid;
443 imported->imp_all = TRUE;
444 }
445 else
446 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200447 int i;
448
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100449 arg = arg_start;
450 if (*arg == '{')
451 arg = skipwhite(arg + 1);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200452 for (i = 0; i < names.ga_len; ++i)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100453 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200454 char_u *name = ((char_u **)names.ga_data)[i];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100455 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100456 imported_T *imported;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100457 ufunc_T *ufunc = NULL;
458 type_T *type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100459
Bram Moolenaar1c991142020-07-04 13:15:31 +0200460 idx = find_exported(sid, name, &ufunc, &type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100461
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100462 if (idx < 0 && ufunc == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200463 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100464
Bram Moolenaar1c991142020-07-04 13:15:31 +0200465 if (check_defined(name, STRLEN(name), cctx) == FAIL)
466 goto erret;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100467
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100468 imported = new_imported(gap != NULL ? gap
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100469 : &SCRIPT_ITEM(import_sid)->sn_imports);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100470 if (imported == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200471 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100472
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100473 // TODO: check for "as" following
Bram Moolenaar1c991142020-07-04 13:15:31 +0200474 // imported->imp_name = vim_strsave(as_name);
475 imported->imp_name = name;
476 ((char_u **)names.ga_data)[i] = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100477 imported->imp_sid = sid;
478 if (idx >= 0)
479 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100480 imported->imp_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100481 imported->imp_var_vals_idx = idx;
482 }
483 else
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200484 {
485 imported->imp_type = ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100486 imported->imp_funcname = ufunc->uf_name;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200487 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100488 }
489 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200490erret:
491 ga_clear_strings(&names);
492 vim_free(as_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100493 return cmd_end;
494}
495
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200496/*
497 * Declare a script-local variable without init: "let var: type".
498 * "const" is an error since the value is missing.
499 * Returns a pointer to after the type.
500 */
501 char_u *
502vim9_declare_scriptvar(exarg_T *eap, char_u *arg)
503{
504 char_u *p;
505 char_u *name;
506 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
507 type_T *type;
508 int called_emsg_before = called_emsg;
509 typval_T init_tv;
510
511 if (eap->cmdidx == CMD_const)
512 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +0200513 emsg(_(e_const_requires_a_value));
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200514 return arg + STRLEN(arg);
515 }
516
517 // Check for valid starting character.
518 if (!eval_isnamec1(*arg))
519 {
520 semsg(_(e_invarg2), arg);
521 return arg + STRLEN(arg);
522 }
523
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200524 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
Bram Moolenaar3b74b6b2020-06-19 19:01:43 +0200525 if (*p == ':' && (VIM_ISWHITE(p[1]) || p != arg + 1))
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200526 break;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200527
528 if (*p != ':')
529 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +0200530 emsg(_(e_type_or_initialization_required));
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200531 return arg + STRLEN(arg);
532 }
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200533 if (!VIM_ISWHITE(p[1]))
534 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200535 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200536 return arg + STRLEN(arg);
537 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200538 name = vim_strnsave(arg, p - arg);
539
540 // parse type
541 p = skipwhite(p + 1);
542 type = parse_type(&p, &si->sn_type_list);
543 if (called_emsg != called_emsg_before)
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200544 {
545 vim_free(name);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200546 return p;
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200547 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200548
549 // Create the variable with 0/NULL value.
550 CLEAR_FIELD(init_tv);
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +0200551 if (type->tt_type == VAR_ANY)
552 // A variable of type "any" is not possible, just use zero instead
553 init_tv.v_type = VAR_NUMBER;
554 else
555 init_tv.v_type = type->tt_type;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200556 set_var_const(name, type, &init_tv, FALSE, 0);
557
558 vim_free(name);
559 return p;
560}
561
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200562/*
563 * Check if the type of script variable "dest" allows assigning "value".
Bram Moolenaarc1ec0422020-09-09 22:27:58 +0200564 * If needed convert "value" to a bool.
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200565 */
Bram Moolenaarc785b9a2020-06-19 18:34:15 +0200566 int
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200567check_script_var_type(typval_T *dest, typval_T *value, char_u *name)
568{
569 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
570 int idx;
571
Bram Moolenaar81e17fb2020-08-21 21:55:43 +0200572 if (si->sn_version != SCRIPT_VERSION_VIM9)
573 // legacy script doesn't store variable types
574 return OK;
575
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200576 // Find the svar_T in sn_var_vals.
577 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
578 {
579 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
580
581 if (sv->sv_tv == dest)
582 {
Bram Moolenaarc1ec0422020-09-09 22:27:58 +0200583 int ret;
584
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200585 if (sv->sv_const)
Bram Moolenaarc785b9a2020-06-19 18:34:15 +0200586 {
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200587 semsg(_(e_readonlyvar), name);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +0200588 return FAIL;
589 }
Bram Moolenaarc1ec0422020-09-09 22:27:58 +0200590 ret = check_typval_type(sv->sv_type, value, 0);
591 if (ret == OK && need_convert_to_bool(sv->sv_type, value))
592 {
593 int val = tv2bool(value);
594
595 clear_tv(value);
596 value->v_type = VAR_BOOL;
597 value->v_lock = 0;
598 value->vval.v_number = val ? VVAL_TRUE : VVAL_FALSE;
599 }
600 return ret;
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200601 }
602 }
603 iemsg("check_script_var_type(): not found");
Bram Moolenaarc785b9a2020-06-19 18:34:15 +0200604 return OK; // not really
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200605}
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200606
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100607#endif // FEAT_EVAL