blob: 2e94b8e295539602c59d9d5760a5450bcb02f779 [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{
Bram Moolenaar68e30442020-07-28 21:15:07 +020067 if (in_vim9script())
68 switch (eap->cmdidx)
69 {
70 case CMD_insert:
71 case CMD_append:
72 case CMD_change:
73 case CMD_xit:
74 semsg(_("E1100: Missing :let: %s"), eap->cmd);
75 return FAIL;
76 default: break;
77 }
Bram Moolenaarae616492020-07-28 20:07:27 +020078 return OK;
79}
80
81/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010082 * ":export let Name: type"
83 * ":export const Name: type"
84 * ":export def Name(..."
85 * ":export class Name ..."
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010086 */
87 void
Bram Moolenaar09689a02020-05-09 22:50:08 +020088ex_export(exarg_T *eap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010089{
Bram Moolenaareb6880b2020-07-12 17:07:05 +020090 if (!in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010091 {
92 emsg(_(e_needs_vim9));
93 return;
94 }
95
96 eap->cmd = eap->arg;
97 (void)find_ex_command(eap, NULL, lookup_scriptvar, NULL);
98 switch (eap->cmdidx)
99 {
100 case CMD_let:
101 case CMD_const:
102 case CMD_def:
103 // case CMD_class:
104 is_export = TRUE;
105 do_cmdline(eap->cmd, eap->getline, eap->cookie,
106 DOCMD_VERBOSE + DOCMD_NOWAIT);
107
108 // The command will reset "is_export" when exporting an item.
109 if (is_export)
110 {
111 emsg(_("E1044: export with invalid argument"));
112 is_export = FALSE;
113 }
114 break;
115 default:
116 emsg(_("E1043: Invalid command after :export"));
117 break;
118 }
119}
120
121/*
122 * Add a new imported item entry to the current script.
123 */
124 static imported_T *
125new_imported(garray_T *gap)
126{
127 if (ga_grow(gap, 1) == OK)
128 return ((imported_T *)gap->ga_data + gap->ga_len++);
129 return NULL;
130}
131
132/*
133 * Free all imported items in script "sid".
134 */
135 void
136free_imports(int sid)
137{
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100138 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100139 int idx;
140
141 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
142 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100143 imported_T *imp = ((imported_T *)si->sn_imports.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100144
145 vim_free(imp->imp_name);
146 }
147 ga_clear(&si->sn_imports);
Bram Moolenaar20431c92020-03-20 18:39:46 +0100148 ga_clear(&si->sn_var_vals);
Bram Moolenaar6110e792020-07-08 19:35:21 +0200149 clear_type_list(&si->sn_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100150}
151
152/*
153 * ":import Item from 'filename'"
154 * ":import Item as Alias from 'filename'"
155 * ":import {Item} from 'filename'".
156 * ":import {Item as Alias} from 'filename'"
157 * ":import {Item, Item} from 'filename'"
158 * ":import {Item, Item as Alias} from 'filename'"
159 *
160 * ":import * as Name from 'filename'"
161 */
162 void
163ex_import(exarg_T *eap)
164{
Bram Moolenaar1c991142020-07-04 13:15:31 +0200165 char_u *cmd_end;
166 evalarg_T evalarg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100167
Bram Moolenaar101f4812020-06-16 23:18:51 +0200168 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
169 {
170 emsg(_("E1094: import can only be used in a script"));
171 return;
172 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200173 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar101f4812020-06-16 23:18:51 +0200174
Bram Moolenaar1c991142020-07-04 13:15:31 +0200175 cmd_end = handle_import(eap->arg, NULL, current_sctx.sc_sid,
176 &evalarg, NULL);
Bram Moolenaar9721fb42020-06-11 23:10:46 +0200177 if (cmd_end != NULL)
178 eap->nextcmd = check_nextcmd(cmd_end);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200179 clear_evalarg(&evalarg, eap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100180}
181
182/*
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100183 * Find an exported item in "sid" matching the name at "*argp".
184 * When it is a variable return the index.
185 * When it is a user function return "*ufunc".
186 * When not found returns -1 and "*ufunc" is NULL.
187 */
188 int
189find_exported(
190 int sid,
Bram Moolenaar1c991142020-07-04 13:15:31 +0200191 char_u *name,
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100192 ufunc_T **ufunc,
193 type_T **type)
194{
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100195 int idx = -1;
196 svar_T *sv;
197 scriptitem_T *script = SCRIPT_ITEM(sid);
198
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100199 // find name in "script"
200 // TODO: also find script-local user function
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100201 idx = get_script_item_idx(sid, name, FALSE);
202 if (idx >= 0)
203 {
204 sv = ((svar_T *)script->sn_var_vals.ga_data) + idx;
205 if (!sv->sv_export)
206 {
207 semsg(_("E1049: Item not exported in script: %s"), name);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100208 return -1;
209 }
210 *type = sv->sv_type;
211 *ufunc = NULL;
212 }
213 else
214 {
215 char_u buffer[200];
216 char_u *funcname;
217
218 // it could be a user function.
Bram Moolenaar5a67c372020-07-23 14:39:47 +0200219 if (STRLEN(name) < sizeof(buffer) - 15)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100220 funcname = buffer;
221 else
222 {
Bram Moolenaar5a67c372020-07-23 14:39:47 +0200223 funcname = alloc(STRLEN(name) + 15);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100224 if (funcname == NULL)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100225 return -1;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100226 }
227 funcname[0] = K_SPECIAL;
228 funcname[1] = KS_EXTRA;
229 funcname[2] = (int)KE_SNR;
230 sprintf((char *)funcname + 3, "%ld_%s", (long)sid, name);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200231 *ufunc = find_func(funcname, FALSE, NULL);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100232 if (funcname != buffer)
233 vim_free(funcname);
234
235 if (*ufunc == NULL)
236 {
237 semsg(_("E1048: Item not found in script: %s"), name);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100238 return -1;
239 }
240 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100241
242 return idx;
243}
244
245/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100246 * Handle an ":import" command and add the resulting imported_T to "gap", when
247 * not NULL, or script "import_sid" sn_imports.
248 * Returns a pointer to after the command or NULL in case of failure
249 */
250 char_u *
Bram Moolenaar1c991142020-07-04 13:15:31 +0200251handle_import(
252 char_u *arg_start,
253 garray_T *gap,
254 int import_sid,
255 evalarg_T *evalarg,
256 void *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100257{
258 char_u *arg = arg_start;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200259 char_u *cmd_end = NULL;
260 char_u *as_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100261 int ret = FAIL;
262 typval_T tv;
263 int sid = -1;
264 int res;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200265 garray_T names;
266 static char e_import_syntax[] = N_("E1047: syntax error in import");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100267
Bram Moolenaar1c991142020-07-04 13:15:31 +0200268 ga_init2(&names, sizeof(char_u *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100269 if (*arg == '{')
270 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200271 // "import {item, item} from ..."
272 arg = skipwhite_and_linebreak(arg + 1, evalarg);
273 for (;;)
274 {
275 char_u *p = arg;
276 int had_comma = FALSE;
277
278 while (eval_isnamec(*arg))
279 ++arg;
280 if (p == arg)
281 break;
282 if (ga_grow(&names, 1) == FAIL)
283 goto erret;
284 ((char_u **)names.ga_data)[names.ga_len] =
285 vim_strnsave(p, arg - p);
286 ++names.ga_len;
287 if (*arg == ',')
288 {
289 had_comma = TRUE;
290 ++arg;
291 }
292 arg = skipwhite_and_linebreak(arg, evalarg);
293 if (*arg == '}')
294 {
295 arg = skipwhite_and_linebreak(arg + 1, evalarg);
296 break;
297 }
298 if (!had_comma)
299 {
300 emsg(_("E1046: Missing comma in import"));
301 goto erret;
302 }
303 }
304 if (names.ga_len == 0)
305 {
306 emsg(_(e_import_syntax));
307 goto erret;
308 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100309 }
310 else
311 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200312 // "import Name from ..."
313 // "import * as Name from ..."
314 // "import item [as Name] from ..."
315 arg = skipwhite_and_linebreak(arg, evalarg);
316 if (arg[0] == '*' && IS_WHITE_OR_NUL(arg[1]))
317 arg = skipwhite_and_linebreak(arg + 1, evalarg);
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100318 else if (eval_isnamec1(*arg))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100319 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200320 char_u *p = arg;
321
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100322 while (eval_isnamec(*arg))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100323 ++arg;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200324 if (ga_grow(&names, 1) == FAIL)
325 goto erret;
326 ((char_u **)names.ga_data)[names.ga_len] =
327 vim_strnsave(p, arg - p);
328 ++names.ga_len;
329 arg = skipwhite_and_linebreak(arg, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100330 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200331 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100332 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200333 emsg(_(e_import_syntax));
334 goto erret;
335 }
336
337 if (STRNCMP("as", arg, 2) == 0 && IS_WHITE_OR_NUL(arg[2]))
338 {
339 char_u *p;
340
341 // skip over "as Name "; no line break allowed after "as"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100342 arg = skipwhite(arg + 2);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200343 p = arg;
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100344 if (eval_isnamec1(*arg))
345 while (eval_isnamec(*arg))
346 ++arg;
Bram Moolenaarcbb6bdc2020-07-06 21:53:17 +0200347 if (check_defined(p, arg - p, cctx) == FAIL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200348 goto erret;
349 as_name = vim_strnsave(p, arg - p);
350 arg = skipwhite_and_linebreak(arg, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100351 }
352 else if (*arg_start == '*')
353 {
354 emsg(_("E1045: Missing \"as\" after *"));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200355 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100356 }
357 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200358
359 if (STRNCMP("from", arg, 4) != 0 || !IS_WHITE_OR_NUL(arg[4]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100360 {
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100361 emsg(_("E1070: Missing \"from\""));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200362 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100363 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200364
Bram Moolenaar962d7212020-07-04 14:15:00 +0200365 arg = skipwhite_and_linebreak(arg + 4, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100366 tv.v_type = VAR_UNKNOWN;
367 // TODO: should we accept any expression?
368 if (*arg == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +0200369 ret = eval_lit_string(&arg, &tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100370 else if (*arg == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +0200371 ret = eval_string(&arg, &tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100372 if (ret == FAIL || tv.vval.v_string == NULL || *tv.vval.v_string == NUL)
373 {
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100374 emsg(_("E1071: Invalid string after \"from\""));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200375 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100376 }
377 cmd_end = arg;
378
Bram Moolenaar1c991142020-07-04 13:15:31 +0200379 /*
380 * find script file
381 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100382 if (*tv.vval.v_string == '.')
383 {
384 size_t len;
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100385 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100386 char_u *tail = gettail(si->sn_name);
387 char_u *from_name;
388
389 // Relative to current script: "./name.vim", "../../name.vim".
390 len = STRLEN(si->sn_name) - STRLEN(tail) + STRLEN(tv.vval.v_string) + 2;
391 from_name = alloc((int)len);
392 if (from_name == NULL)
393 {
394 clear_tv(&tv);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200395 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100396 }
397 vim_strncpy(from_name, si->sn_name, tail - si->sn_name);
398 add_pathsep(from_name);
399 STRCAT(from_name, tv.vval.v_string);
400 simplify_filename(from_name);
401
402 res = do_source(from_name, FALSE, DOSO_NONE, &sid);
403 vim_free(from_name);
404 }
405 else if (mch_isFullName(tv.vval.v_string))
406 {
407 // Absolute path: "/tmp/name.vim"
408 res = do_source(tv.vval.v_string, FALSE, DOSO_NONE, &sid);
409 }
410 else
411 {
412 size_t len = 7 + STRLEN(tv.vval.v_string) + 1;
413 char_u *from_name;
414
415 // Find file in "import" subdirs in 'runtimepath'.
416 from_name = alloc((int)len);
417 if (from_name == NULL)
418 {
419 clear_tv(&tv);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200420 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100421 }
422 vim_snprintf((char *)from_name, len, "import/%s", tv.vval.v_string);
423 res = source_in_path(p_rtp, from_name, DIP_NOAFTER, &sid);
424 vim_free(from_name);
425 }
426
427 if (res == FAIL || sid <= 0)
428 {
429 semsg(_("E1053: Could not import \"%s\""), tv.vval.v_string);
430 clear_tv(&tv);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200431 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100432 }
433 clear_tv(&tv);
434
435 if (*arg_start == '*')
436 {
437 imported_T *imported = new_imported(gap != NULL ? gap
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100438 : &SCRIPT_ITEM(import_sid)->sn_imports);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100439
440 if (imported == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200441 goto erret;
442 imported->imp_name = as_name;
443 as_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100444 imported->imp_sid = sid;
445 imported->imp_all = TRUE;
446 }
447 else
448 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200449 int i;
450
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100451 arg = arg_start;
452 if (*arg == '{')
453 arg = skipwhite(arg + 1);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200454 for (i = 0; i < names.ga_len; ++i)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100455 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200456 char_u *name = ((char_u **)names.ga_data)[i];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100457 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100458 imported_T *imported;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100459 ufunc_T *ufunc = NULL;
460 type_T *type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100461
Bram Moolenaar1c991142020-07-04 13:15:31 +0200462 idx = find_exported(sid, name, &ufunc, &type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100463
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100464 if (idx < 0 && ufunc == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200465 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100466
Bram Moolenaar1c991142020-07-04 13:15:31 +0200467 if (check_defined(name, STRLEN(name), cctx) == FAIL)
468 goto erret;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100469
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100470 imported = new_imported(gap != NULL ? gap
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100471 : &SCRIPT_ITEM(import_sid)->sn_imports);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100472 if (imported == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200473 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100474
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100475 // TODO: check for "as" following
Bram Moolenaar1c991142020-07-04 13:15:31 +0200476 // imported->imp_name = vim_strsave(as_name);
477 imported->imp_name = name;
478 ((char_u **)names.ga_data)[i] = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100479 imported->imp_sid = sid;
480 if (idx >= 0)
481 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100482 imported->imp_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100483 imported->imp_var_vals_idx = idx;
484 }
485 else
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200486 {
487 imported->imp_type = ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100488 imported->imp_funcname = ufunc->uf_name;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200489 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100490 }
491 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200492erret:
493 ga_clear_strings(&names);
494 vim_free(as_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100495 return cmd_end;
496}
497
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200498/*
499 * Declare a script-local variable without init: "let var: type".
500 * "const" is an error since the value is missing.
501 * Returns a pointer to after the type.
502 */
503 char_u *
504vim9_declare_scriptvar(exarg_T *eap, char_u *arg)
505{
506 char_u *p;
507 char_u *name;
508 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
509 type_T *type;
510 int called_emsg_before = called_emsg;
511 typval_T init_tv;
512
513 if (eap->cmdidx == CMD_const)
514 {
515 emsg(_(e_const_req_value));
516 return arg + STRLEN(arg);
517 }
518
519 // Check for valid starting character.
520 if (!eval_isnamec1(*arg))
521 {
522 semsg(_(e_invarg2), arg);
523 return arg + STRLEN(arg);
524 }
525
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200526 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
Bram Moolenaar3b74b6b2020-06-19 19:01:43 +0200527 if (*p == ':' && (VIM_ISWHITE(p[1]) || p != arg + 1))
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200528 break;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200529
530 if (*p != ':')
531 {
532 emsg(_(e_type_req));
533 return arg + STRLEN(arg);
534 }
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200535 if (!VIM_ISWHITE(p[1]))
536 {
537 semsg(_(e_white_after), ":");
538 return arg + STRLEN(arg);
539 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200540 name = vim_strnsave(arg, p - arg);
541
542 // parse type
543 p = skipwhite(p + 1);
544 type = parse_type(&p, &si->sn_type_list);
545 if (called_emsg != called_emsg_before)
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200546 {
547 vim_free(name);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200548 return p;
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200549 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200550
551 // Create the variable with 0/NULL value.
552 CLEAR_FIELD(init_tv);
553 init_tv.v_type = type->tt_type;
554 set_var_const(name, type, &init_tv, FALSE, 0);
555
556 vim_free(name);
557 return p;
558}
559
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200560/*
561 * Check if the type of script variable "dest" allows assigning "value".
562 */
Bram Moolenaarc785b9a2020-06-19 18:34:15 +0200563 int
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200564check_script_var_type(typval_T *dest, typval_T *value, char_u *name)
565{
566 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
567 int idx;
568
569 // Find the svar_T in sn_var_vals.
570 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
571 {
572 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
573
574 if (sv->sv_tv == dest)
575 {
576 if (sv->sv_const)
Bram Moolenaarc785b9a2020-06-19 18:34:15 +0200577 {
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200578 semsg(_(e_readonlyvar), name);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +0200579 return FAIL;
580 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +0200581 return check_typval_type(sv->sv_type, value);
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200582 }
583 }
584 iemsg("check_script_var_type(): not found");
Bram Moolenaarc785b9a2020-06-19 18:34:15 +0200585 return OK; // not really
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200586}
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200587
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100588#endif // FEAT_EVAL