blob: 7d783ea9342efbbc1fb97a00daad1b2317964ccc [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 Moolenaar9721fb42020-06-11 23:10:46 +020020static char e_needs_vim9[] = N_("E1042: export can only be used in vim9script");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010021
22 int
23in_vim9script(void)
24{
Bram Moolenaareb6880b2020-07-12 17:07:05 +020025 // Do not go up the stack, a ":function" inside vim9script uses legacy
26 // syntax. "sc_version" is also set when compiling a ":def" function in
27 // legacy script.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010028 return current_sctx.sc_version == SCRIPT_VERSION_VIM9;
29}
30
31/*
32 * ":vim9script".
33 */
34 void
35ex_vim9script(exarg_T *eap)
36{
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 {
41 emsg(_("E1038: vim9script can only be used in a script"));
42 return;
43 }
Bram Moolenaar101f4812020-06-16 23:18:51 +020044 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010045 if (si->sn_had_command)
46 {
47 emsg(_("E1039: vim9script must be the first command in a script"));
48 return;
49 }
50 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
51 si->sn_version = SCRIPT_VERSION_VIM9;
52 si->sn_had_command = TRUE;
53
54 if (STRCMP(p_cpo, CPO_VIM) != 0)
55 {
56 si->sn_save_cpo = p_cpo;
57 p_cpo = vim_strsave((char_u *)CPO_VIM);
58 }
59}
60
61/*
Bram Moolenaarae616492020-07-28 20:07:27 +020062 * When in Vim9 script give an error and return FAIL.
63 */
64 int
65not_in_vim9(exarg_T *eap)
66{
67 switch (eap->cmdidx)
68 {
69 case CMD_insert:
70 case CMD_append:
71 case CMD_change:
72 case CMD_xit:
73 semsg(_("E1100: Missing :let: %s"), eap->cmd);
74 return FAIL;
75 default: break;
76 }
77 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 {
91 emsg(_(e_needs_vim9));
92 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 {
110 emsg(_("E1044: export with invalid argument"));
111 is_export = FALSE;
112 }
113 break;
114 default:
115 emsg(_("E1043: Invalid command after :export"));
116 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 {
169 emsg(_("E1094: import can only be used in a script"));
170 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 {
206 semsg(_("E1049: Item not exported in script: %s"), 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 {
236 semsg(_("E1048: Item not found in script: %s"), 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;
265 static char e_import_syntax[] = N_("E1047: syntax error in import");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100266
Bram Moolenaar1c991142020-07-04 13:15:31 +0200267 ga_init2(&names, sizeof(char_u *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100268 if (*arg == '{')
269 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200270 // "import {item, item} from ..."
271 arg = skipwhite_and_linebreak(arg + 1, evalarg);
272 for (;;)
273 {
274 char_u *p = arg;
275 int had_comma = FALSE;
276
277 while (eval_isnamec(*arg))
278 ++arg;
279 if (p == arg)
280 break;
281 if (ga_grow(&names, 1) == FAIL)
282 goto erret;
283 ((char_u **)names.ga_data)[names.ga_len] =
284 vim_strnsave(p, arg - p);
285 ++names.ga_len;
286 if (*arg == ',')
287 {
288 had_comma = TRUE;
289 ++arg;
290 }
291 arg = skipwhite_and_linebreak(arg, evalarg);
292 if (*arg == '}')
293 {
294 arg = skipwhite_and_linebreak(arg + 1, evalarg);
295 break;
296 }
297 if (!had_comma)
298 {
299 emsg(_("E1046: Missing comma in import"));
300 goto erret;
301 }
302 }
303 if (names.ga_len == 0)
304 {
305 emsg(_(e_import_syntax));
306 goto erret;
307 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100308 }
309 else
310 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200311 // "import Name from ..."
312 // "import * as Name from ..."
313 // "import item [as Name] from ..."
314 arg = skipwhite_and_linebreak(arg, evalarg);
315 if (arg[0] == '*' && IS_WHITE_OR_NUL(arg[1]))
316 arg = skipwhite_and_linebreak(arg + 1, evalarg);
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100317 else if (eval_isnamec1(*arg))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100318 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200319 char_u *p = arg;
320
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100321 while (eval_isnamec(*arg))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100322 ++arg;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200323 if (ga_grow(&names, 1) == FAIL)
324 goto erret;
325 ((char_u **)names.ga_data)[names.ga_len] =
326 vim_strnsave(p, arg - p);
327 ++names.ga_len;
328 arg = skipwhite_and_linebreak(arg, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100329 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200330 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100331 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200332 emsg(_(e_import_syntax));
333 goto erret;
334 }
335
336 if (STRNCMP("as", arg, 2) == 0 && IS_WHITE_OR_NUL(arg[2]))
337 {
338 char_u *p;
339
340 // skip over "as Name "; no line break allowed after "as"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100341 arg = skipwhite(arg + 2);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200342 p = arg;
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100343 if (eval_isnamec1(*arg))
344 while (eval_isnamec(*arg))
345 ++arg;
Bram Moolenaarcbb6bdc2020-07-06 21:53:17 +0200346 if (check_defined(p, arg - p, cctx) == FAIL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200347 goto erret;
348 as_name = vim_strnsave(p, arg - p);
349 arg = skipwhite_and_linebreak(arg, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100350 }
351 else if (*arg_start == '*')
352 {
353 emsg(_("E1045: Missing \"as\" after *"));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200354 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100355 }
356 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200357
358 if (STRNCMP("from", arg, 4) != 0 || !IS_WHITE_OR_NUL(arg[4]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100359 {
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100360 emsg(_("E1070: Missing \"from\""));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200361 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100362 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200363
Bram Moolenaar962d7212020-07-04 14:15:00 +0200364 arg = skipwhite_and_linebreak(arg + 4, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100365 tv.v_type = VAR_UNKNOWN;
366 // TODO: should we accept any expression?
367 if (*arg == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +0200368 ret = eval_lit_string(&arg, &tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100369 else if (*arg == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +0200370 ret = eval_string(&arg, &tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100371 if (ret == FAIL || tv.vval.v_string == NULL || *tv.vval.v_string == NUL)
372 {
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100373 emsg(_("E1071: Invalid string after \"from\""));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200374 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100375 }
376 cmd_end = arg;
377
Bram Moolenaar1c991142020-07-04 13:15:31 +0200378 /*
379 * find script file
380 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100381 if (*tv.vval.v_string == '.')
382 {
383 size_t len;
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100384 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100385 char_u *tail = gettail(si->sn_name);
386 char_u *from_name;
387
388 // Relative to current script: "./name.vim", "../../name.vim".
389 len = STRLEN(si->sn_name) - STRLEN(tail) + STRLEN(tv.vval.v_string) + 2;
390 from_name = alloc((int)len);
391 if (from_name == NULL)
392 {
393 clear_tv(&tv);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200394 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100395 }
396 vim_strncpy(from_name, si->sn_name, tail - si->sn_name);
397 add_pathsep(from_name);
398 STRCAT(from_name, tv.vval.v_string);
399 simplify_filename(from_name);
400
401 res = do_source(from_name, FALSE, DOSO_NONE, &sid);
402 vim_free(from_name);
403 }
404 else if (mch_isFullName(tv.vval.v_string))
405 {
406 // Absolute path: "/tmp/name.vim"
407 res = do_source(tv.vval.v_string, FALSE, DOSO_NONE, &sid);
408 }
409 else
410 {
411 size_t len = 7 + STRLEN(tv.vval.v_string) + 1;
412 char_u *from_name;
413
414 // Find file in "import" subdirs in 'runtimepath'.
415 from_name = alloc((int)len);
416 if (from_name == NULL)
417 {
418 clear_tv(&tv);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200419 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100420 }
421 vim_snprintf((char *)from_name, len, "import/%s", tv.vval.v_string);
422 res = source_in_path(p_rtp, from_name, DIP_NOAFTER, &sid);
423 vim_free(from_name);
424 }
425
426 if (res == FAIL || sid <= 0)
427 {
428 semsg(_("E1053: Could not import \"%s\""), tv.vval.v_string);
429 clear_tv(&tv);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200430 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100431 }
432 clear_tv(&tv);
433
434 if (*arg_start == '*')
435 {
436 imported_T *imported = new_imported(gap != NULL ? gap
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100437 : &SCRIPT_ITEM(import_sid)->sn_imports);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100438
439 if (imported == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200440 goto erret;
441 imported->imp_name = as_name;
442 as_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100443 imported->imp_sid = sid;
444 imported->imp_all = TRUE;
445 }
446 else
447 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200448 int i;
449
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100450 arg = arg_start;
451 if (*arg == '{')
452 arg = skipwhite(arg + 1);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200453 for (i = 0; i < names.ga_len; ++i)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100454 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200455 char_u *name = ((char_u **)names.ga_data)[i];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100456 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100457 imported_T *imported;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100458 ufunc_T *ufunc = NULL;
459 type_T *type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100460
Bram Moolenaar1c991142020-07-04 13:15:31 +0200461 idx = find_exported(sid, name, &ufunc, &type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100462
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100463 if (idx < 0 && ufunc == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200464 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100465
Bram Moolenaar1c991142020-07-04 13:15:31 +0200466 if (check_defined(name, STRLEN(name), cctx) == FAIL)
467 goto erret;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100468
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100469 imported = new_imported(gap != NULL ? gap
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100470 : &SCRIPT_ITEM(import_sid)->sn_imports);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100471 if (imported == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200472 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100473
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100474 // TODO: check for "as" following
Bram Moolenaar1c991142020-07-04 13:15:31 +0200475 // imported->imp_name = vim_strsave(as_name);
476 imported->imp_name = name;
477 ((char_u **)names.ga_data)[i] = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100478 imported->imp_sid = sid;
479 if (idx >= 0)
480 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100481 imported->imp_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100482 imported->imp_var_vals_idx = idx;
483 }
484 else
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200485 {
486 imported->imp_type = ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100487 imported->imp_funcname = ufunc->uf_name;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200488 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100489 }
490 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200491erret:
492 ga_clear_strings(&names);
493 vim_free(as_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100494 return cmd_end;
495}
496
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200497/*
498 * Declare a script-local variable without init: "let var: type".
499 * "const" is an error since the value is missing.
500 * Returns a pointer to after the type.
501 */
502 char_u *
503vim9_declare_scriptvar(exarg_T *eap, char_u *arg)
504{
505 char_u *p;
506 char_u *name;
507 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
508 type_T *type;
509 int called_emsg_before = called_emsg;
510 typval_T init_tv;
511
512 if (eap->cmdidx == CMD_const)
513 {
514 emsg(_(e_const_req_value));
515 return arg + STRLEN(arg);
516 }
517
518 // Check for valid starting character.
519 if (!eval_isnamec1(*arg))
520 {
521 semsg(_(e_invarg2), arg);
522 return arg + STRLEN(arg);
523 }
524
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200525 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
Bram Moolenaar3b74b6b2020-06-19 19:01:43 +0200526 if (*p == ':' && (VIM_ISWHITE(p[1]) || p != arg + 1))
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200527 break;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200528
529 if (*p != ':')
530 {
531 emsg(_(e_type_req));
532 return arg + STRLEN(arg);
533 }
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200534 if (!VIM_ISWHITE(p[1]))
535 {
536 semsg(_(e_white_after), ":");
537 return arg + STRLEN(arg);
538 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200539 name = vim_strnsave(arg, p - arg);
540
541 // parse type
542 p = skipwhite(p + 1);
543 type = parse_type(&p, &si->sn_type_list);
544 if (called_emsg != called_emsg_before)
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200545 {
546 vim_free(name);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200547 return p;
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200548 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200549
550 // Create the variable with 0/NULL value.
551 CLEAR_FIELD(init_tv);
552 init_tv.v_type = type->tt_type;
553 set_var_const(name, type, &init_tv, FALSE, 0);
554
555 vim_free(name);
556 return p;
557}
558
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200559/*
560 * Check if the type of script variable "dest" allows assigning "value".
561 */
Bram Moolenaarc785b9a2020-06-19 18:34:15 +0200562 int
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200563check_script_var_type(typval_T *dest, typval_T *value, char_u *name)
564{
565 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
566 int idx;
567
568 // Find the svar_T in sn_var_vals.
569 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
570 {
571 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
572
573 if (sv->sv_tv == dest)
574 {
575 if (sv->sv_const)
Bram Moolenaarc785b9a2020-06-19 18:34:15 +0200576 {
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200577 semsg(_(e_readonlyvar), name);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +0200578 return FAIL;
579 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +0200580 return check_typval_type(sv->sv_type, value);
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200581 }
582 }
583 iemsg("check_script_var_type(): not found");
Bram Moolenaarc785b9a2020-06-19 18:34:15 +0200584 return OK; // not really
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200585}
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200586
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100587#endif // FEAT_EVAL