blob: c13da69b9a363ec542b650e085cd93c3f47eacfe [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 {
Bram Moolenaar68e30442020-07-28 21:15:07 +020070 case CMD_append:
71 case CMD_change:
Bram Moolenaarf5a48012020-08-01 17:00:03 +020072 case CMD_insert:
73 case CMD_t:
Bram Moolenaar68e30442020-07-28 21:15:07 +020074 case CMD_xit:
75 semsg(_("E1100: Missing :let: %s"), eap->cmd);
76 return FAIL;
77 default: break;
78 }
Bram Moolenaarae616492020-07-28 20:07:27 +020079 return OK;
80}
81
82/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010083 * ":export let Name: type"
84 * ":export const Name: type"
85 * ":export def Name(..."
86 * ":export class Name ..."
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010087 */
88 void
Bram Moolenaar09689a02020-05-09 22:50:08 +020089ex_export(exarg_T *eap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010090{
Bram Moolenaareb6880b2020-07-12 17:07:05 +020091 if (!in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010092 {
93 emsg(_(e_needs_vim9));
94 return;
95 }
96
97 eap->cmd = eap->arg;
98 (void)find_ex_command(eap, NULL, lookup_scriptvar, NULL);
99 switch (eap->cmdidx)
100 {
101 case CMD_let:
102 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 {
112 emsg(_("E1044: export with invalid argument"));
113 is_export = FALSE;
114 }
115 break;
116 default:
117 emsg(_("E1043: Invalid command after :export"));
118 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
137free_imports(int sid)
138{
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 Moolenaar20431c92020-03-20 18:39:46 +0100149 ga_clear(&si->sn_var_vals);
Bram Moolenaar6110e792020-07-08 19:35:21 +0200150 clear_type_list(&si->sn_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100151}
152
153/*
154 * ":import Item from 'filename'"
155 * ":import Item as Alias from 'filename'"
156 * ":import {Item} from 'filename'".
157 * ":import {Item as Alias} from 'filename'"
158 * ":import {Item, Item} from 'filename'"
159 * ":import {Item, Item as Alias} from 'filename'"
160 *
161 * ":import * as Name from 'filename'"
162 */
163 void
164ex_import(exarg_T *eap)
165{
Bram Moolenaar1c991142020-07-04 13:15:31 +0200166 char_u *cmd_end;
167 evalarg_T evalarg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100168
Bram Moolenaar101f4812020-06-16 23:18:51 +0200169 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
170 {
171 emsg(_("E1094: import can only be used in a script"));
172 return;
173 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200174 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar101f4812020-06-16 23:18:51 +0200175
Bram Moolenaar1c991142020-07-04 13:15:31 +0200176 cmd_end = handle_import(eap->arg, NULL, current_sctx.sc_sid,
177 &evalarg, NULL);
Bram Moolenaar9721fb42020-06-11 23:10:46 +0200178 if (cmd_end != NULL)
179 eap->nextcmd = check_nextcmd(cmd_end);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200180 clear_evalarg(&evalarg, eap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100181}
182
183/*
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100184 * Find an exported item in "sid" matching the name at "*argp".
185 * When it is a variable return the index.
186 * When it is a user function return "*ufunc".
187 * When not found returns -1 and "*ufunc" is NULL.
188 */
189 int
190find_exported(
191 int sid,
Bram Moolenaar1c991142020-07-04 13:15:31 +0200192 char_u *name,
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100193 ufunc_T **ufunc,
194 type_T **type)
195{
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100196 int idx = -1;
197 svar_T *sv;
198 scriptitem_T *script = SCRIPT_ITEM(sid);
199
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100200 // find name in "script"
201 // TODO: also find script-local user function
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100202 idx = get_script_item_idx(sid, name, FALSE);
203 if (idx >= 0)
204 {
205 sv = ((svar_T *)script->sn_var_vals.ga_data) + idx;
206 if (!sv->sv_export)
207 {
208 semsg(_("E1049: Item not exported in script: %s"), name);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100209 return -1;
210 }
211 *type = sv->sv_type;
212 *ufunc = NULL;
213 }
214 else
215 {
216 char_u buffer[200];
217 char_u *funcname;
218
219 // it could be a user function.
Bram Moolenaar5a67c372020-07-23 14:39:47 +0200220 if (STRLEN(name) < sizeof(buffer) - 15)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100221 funcname = buffer;
222 else
223 {
Bram Moolenaar5a67c372020-07-23 14:39:47 +0200224 funcname = alloc(STRLEN(name) + 15);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100225 if (funcname == NULL)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100226 return -1;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100227 }
228 funcname[0] = K_SPECIAL;
229 funcname[1] = KS_EXTRA;
230 funcname[2] = (int)KE_SNR;
231 sprintf((char *)funcname + 3, "%ld_%s", (long)sid, name);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200232 *ufunc = find_func(funcname, FALSE, NULL);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100233 if (funcname != buffer)
234 vim_free(funcname);
235
236 if (*ufunc == NULL)
237 {
238 semsg(_("E1048: Item not found in script: %s"), name);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100239 return -1;
240 }
241 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100242
243 return idx;
244}
245
246/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100247 * Handle an ":import" command and add the resulting imported_T to "gap", when
248 * not NULL, or script "import_sid" sn_imports.
249 * Returns a pointer to after the command or NULL in case of failure
250 */
251 char_u *
Bram Moolenaar1c991142020-07-04 13:15:31 +0200252handle_import(
253 char_u *arg_start,
254 garray_T *gap,
255 int import_sid,
256 evalarg_T *evalarg,
257 void *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100258{
259 char_u *arg = arg_start;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200260 char_u *cmd_end = NULL;
261 char_u *as_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100262 int ret = FAIL;
263 typval_T tv;
264 int sid = -1;
265 int res;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200266 garray_T names;
267 static char e_import_syntax[] = N_("E1047: syntax error in import");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100268
Bram Moolenaar1c991142020-07-04 13:15:31 +0200269 ga_init2(&names, sizeof(char_u *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100270 if (*arg == '{')
271 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200272 // "import {item, item} from ..."
273 arg = skipwhite_and_linebreak(arg + 1, evalarg);
274 for (;;)
275 {
276 char_u *p = arg;
277 int had_comma = FALSE;
278
279 while (eval_isnamec(*arg))
280 ++arg;
281 if (p == arg)
282 break;
283 if (ga_grow(&names, 1) == FAIL)
284 goto erret;
285 ((char_u **)names.ga_data)[names.ga_len] =
286 vim_strnsave(p, arg - p);
287 ++names.ga_len;
288 if (*arg == ',')
289 {
290 had_comma = TRUE;
291 ++arg;
292 }
293 arg = skipwhite_and_linebreak(arg, evalarg);
294 if (*arg == '}')
295 {
296 arg = skipwhite_and_linebreak(arg + 1, evalarg);
297 break;
298 }
299 if (!had_comma)
300 {
301 emsg(_("E1046: Missing comma in import"));
302 goto erret;
303 }
304 }
305 if (names.ga_len == 0)
306 {
307 emsg(_(e_import_syntax));
308 goto erret;
309 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100310 }
311 else
312 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200313 // "import Name from ..."
314 // "import * as Name from ..."
315 // "import item [as Name] from ..."
316 arg = skipwhite_and_linebreak(arg, evalarg);
317 if (arg[0] == '*' && IS_WHITE_OR_NUL(arg[1]))
318 arg = skipwhite_and_linebreak(arg + 1, evalarg);
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100319 else if (eval_isnamec1(*arg))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100320 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200321 char_u *p = arg;
322
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100323 while (eval_isnamec(*arg))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100324 ++arg;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200325 if (ga_grow(&names, 1) == FAIL)
326 goto erret;
327 ((char_u **)names.ga_data)[names.ga_len] =
328 vim_strnsave(p, arg - p);
329 ++names.ga_len;
330 arg = skipwhite_and_linebreak(arg, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100331 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200332 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100333 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200334 emsg(_(e_import_syntax));
335 goto erret;
336 }
337
338 if (STRNCMP("as", arg, 2) == 0 && IS_WHITE_OR_NUL(arg[2]))
339 {
340 char_u *p;
341
342 // skip over "as Name "; no line break allowed after "as"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100343 arg = skipwhite(arg + 2);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200344 p = arg;
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100345 if (eval_isnamec1(*arg))
346 while (eval_isnamec(*arg))
347 ++arg;
Bram Moolenaarcbb6bdc2020-07-06 21:53:17 +0200348 if (check_defined(p, arg - p, cctx) == FAIL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200349 goto erret;
350 as_name = vim_strnsave(p, arg - p);
351 arg = skipwhite_and_linebreak(arg, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100352 }
353 else if (*arg_start == '*')
354 {
355 emsg(_("E1045: Missing \"as\" after *"));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200356 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100357 }
358 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200359
360 if (STRNCMP("from", arg, 4) != 0 || !IS_WHITE_OR_NUL(arg[4]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100361 {
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100362 emsg(_("E1070: Missing \"from\""));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200363 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100364 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200365
Bram Moolenaar962d7212020-07-04 14:15:00 +0200366 arg = skipwhite_and_linebreak(arg + 4, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100367 tv.v_type = VAR_UNKNOWN;
368 // TODO: should we accept any expression?
369 if (*arg == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +0200370 ret = eval_lit_string(&arg, &tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100371 else if (*arg == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +0200372 ret = eval_string(&arg, &tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100373 if (ret == FAIL || tv.vval.v_string == NULL || *tv.vval.v_string == NUL)
374 {
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100375 emsg(_("E1071: Invalid string after \"from\""));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200376 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100377 }
378 cmd_end = arg;
379
Bram Moolenaar1c991142020-07-04 13:15:31 +0200380 /*
381 * find script file
382 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100383 if (*tv.vval.v_string == '.')
384 {
385 size_t len;
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100386 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100387 char_u *tail = gettail(si->sn_name);
388 char_u *from_name;
389
390 // Relative to current script: "./name.vim", "../../name.vim".
391 len = STRLEN(si->sn_name) - STRLEN(tail) + STRLEN(tv.vval.v_string) + 2;
392 from_name = alloc((int)len);
393 if (from_name == NULL)
394 {
395 clear_tv(&tv);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200396 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100397 }
398 vim_strncpy(from_name, si->sn_name, tail - si->sn_name);
399 add_pathsep(from_name);
400 STRCAT(from_name, tv.vval.v_string);
401 simplify_filename(from_name);
402
403 res = do_source(from_name, FALSE, DOSO_NONE, &sid);
404 vim_free(from_name);
405 }
406 else if (mch_isFullName(tv.vval.v_string))
407 {
408 // Absolute path: "/tmp/name.vim"
409 res = do_source(tv.vval.v_string, FALSE, DOSO_NONE, &sid);
410 }
411 else
412 {
413 size_t len = 7 + STRLEN(tv.vval.v_string) + 1;
414 char_u *from_name;
415
416 // Find file in "import" subdirs in 'runtimepath'.
417 from_name = alloc((int)len);
418 if (from_name == NULL)
419 {
420 clear_tv(&tv);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200421 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100422 }
423 vim_snprintf((char *)from_name, len, "import/%s", tv.vval.v_string);
424 res = source_in_path(p_rtp, from_name, DIP_NOAFTER, &sid);
425 vim_free(from_name);
426 }
427
428 if (res == FAIL || sid <= 0)
429 {
430 semsg(_("E1053: Could not import \"%s\""), tv.vval.v_string);
431 clear_tv(&tv);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200432 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100433 }
434 clear_tv(&tv);
435
436 if (*arg_start == '*')
437 {
438 imported_T *imported = new_imported(gap != NULL ? gap
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100439 : &SCRIPT_ITEM(import_sid)->sn_imports);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100440
441 if (imported == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200442 goto erret;
443 imported->imp_name = as_name;
444 as_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100445 imported->imp_sid = sid;
446 imported->imp_all = TRUE;
447 }
448 else
449 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200450 int i;
451
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100452 arg = arg_start;
453 if (*arg == '{')
454 arg = skipwhite(arg + 1);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200455 for (i = 0; i < names.ga_len; ++i)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100456 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200457 char_u *name = ((char_u **)names.ga_data)[i];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100458 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100459 imported_T *imported;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100460 ufunc_T *ufunc = NULL;
461 type_T *type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100462
Bram Moolenaar1c991142020-07-04 13:15:31 +0200463 idx = find_exported(sid, name, &ufunc, &type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100464
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100465 if (idx < 0 && ufunc == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200466 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100467
Bram Moolenaar1c991142020-07-04 13:15:31 +0200468 if (check_defined(name, STRLEN(name), cctx) == FAIL)
469 goto erret;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100470
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100471 imported = new_imported(gap != NULL ? gap
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100472 : &SCRIPT_ITEM(import_sid)->sn_imports);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100473 if (imported == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200474 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100475
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100476 // TODO: check for "as" following
Bram Moolenaar1c991142020-07-04 13:15:31 +0200477 // imported->imp_name = vim_strsave(as_name);
478 imported->imp_name = name;
479 ((char_u **)names.ga_data)[i] = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100480 imported->imp_sid = sid;
481 if (idx >= 0)
482 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100483 imported->imp_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100484 imported->imp_var_vals_idx = idx;
485 }
486 else
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200487 {
488 imported->imp_type = ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100489 imported->imp_funcname = ufunc->uf_name;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200490 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100491 }
492 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200493erret:
494 ga_clear_strings(&names);
495 vim_free(as_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100496 return cmd_end;
497}
498
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200499/*
500 * Declare a script-local variable without init: "let var: type".
501 * "const" is an error since the value is missing.
502 * Returns a pointer to after the type.
503 */
504 char_u *
505vim9_declare_scriptvar(exarg_T *eap, char_u *arg)
506{
507 char_u *p;
508 char_u *name;
509 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
510 type_T *type;
511 int called_emsg_before = called_emsg;
512 typval_T init_tv;
513
514 if (eap->cmdidx == CMD_const)
515 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +0200516 emsg(_(e_const_requires_a_value));
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200517 return arg + STRLEN(arg);
518 }
519
520 // Check for valid starting character.
521 if (!eval_isnamec1(*arg))
522 {
523 semsg(_(e_invarg2), arg);
524 return arg + STRLEN(arg);
525 }
526
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200527 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
Bram Moolenaar3b74b6b2020-06-19 19:01:43 +0200528 if (*p == ':' && (VIM_ISWHITE(p[1]) || p != arg + 1))
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200529 break;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200530
531 if (*p != ':')
532 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +0200533 emsg(_(e_type_or_initialization_required));
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200534 return arg + STRLEN(arg);
535 }
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200536 if (!VIM_ISWHITE(p[1]))
537 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +0200538 semsg(_(e_white_space_required_after), ":");
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200539 return arg + STRLEN(arg);
540 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200541 name = vim_strnsave(arg, p - arg);
542
543 // parse type
544 p = skipwhite(p + 1);
545 type = parse_type(&p, &si->sn_type_list);
546 if (called_emsg != called_emsg_before)
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200547 {
548 vim_free(name);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200549 return p;
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200550 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200551
552 // Create the variable with 0/NULL value.
553 CLEAR_FIELD(init_tv);
554 init_tv.v_type = type->tt_type;
555 set_var_const(name, type, &init_tv, FALSE, 0);
556
557 vim_free(name);
558 return p;
559}
560
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200561/*
562 * Check if the type of script variable "dest" allows assigning "value".
563 */
Bram Moolenaarc785b9a2020-06-19 18:34:15 +0200564 int
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200565check_script_var_type(typval_T *dest, typval_T *value, char_u *name)
566{
567 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
568 int idx;
569
570 // Find the svar_T in sn_var_vals.
571 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
572 {
573 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
574
575 if (sv->sv_tv == dest)
576 {
577 if (sv->sv_const)
Bram Moolenaarc785b9a2020-06-19 18:34:15 +0200578 {
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200579 semsg(_(e_readonlyvar), name);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +0200580 return FAIL;
581 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +0200582 return check_typval_type(sv->sv_type, value);
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200583 }
584 }
585 iemsg("check_script_var_type(): not found");
Bram Moolenaarc785b9a2020-06-19 18:34:15 +0200586 return OK; // not really
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200587}
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200588
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100589#endif // FEAT_EVAL