blob: d9f103fbf57da4c89b5eb37e3fa797a14447674e [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
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 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200171 emsg(_(e_import_can_only_be_used_in_script));
Bram Moolenaar101f4812020-06-16 23:18:51 +0200172 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 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200208 semsg(_(e_item_not_exported_in_script_str), 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 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200238 semsg(_(e_item_not_found_in_script_str), 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;
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 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200300 emsg(_(e_missing_comma_in_import));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200301 goto erret;
302 }
303 }
304 if (names.ga_len == 0)
305 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200306 emsg(_(e_syntax_error_in_import));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200307 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 Moolenaar451c2e32020-08-15 16:33:28 +0200333 emsg(_(e_syntax_error_in_import));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200334 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 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200354 emsg(_(e_missing_as_after_star));
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 Moolenaar451c2e32020-08-15 16:33:28 +0200361 emsg(_(e_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 Moolenaar451c2e32020-08-15 16:33:28 +0200374 emsg(_(e_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 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200429 semsg(_(e_could_not_import_str), tv.vval.v_string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100430 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
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200513 if (eap->cmdidx == CMD_final || eap->cmdidx == CMD_const)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200514 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200515 if (eap->cmdidx == CMD_final)
516 emsg(_(e_final_requires_a_value));
517 else
518 emsg(_(e_const_requires_a_value));
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200519 return arg + STRLEN(arg);
520 }
521
522 // Check for valid starting character.
523 if (!eval_isnamec1(*arg))
524 {
525 semsg(_(e_invarg2), arg);
526 return arg + STRLEN(arg);
527 }
528
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200529 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
Bram Moolenaar3b74b6b2020-06-19 19:01:43 +0200530 if (*p == ':' && (VIM_ISWHITE(p[1]) || p != arg + 1))
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200531 break;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200532
533 if (*p != ':')
534 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +0200535 emsg(_(e_type_or_initialization_required));
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200536 return arg + STRLEN(arg);
537 }
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200538 if (!VIM_ISWHITE(p[1]))
539 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200540 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200541 return arg + STRLEN(arg);
542 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200543 name = vim_strnsave(arg, p - arg);
544
545 // parse type
546 p = skipwhite(p + 1);
547 type = parse_type(&p, &si->sn_type_list);
548 if (called_emsg != called_emsg_before)
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200549 {
550 vim_free(name);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200551 return p;
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200552 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200553
554 // Create the variable with 0/NULL value.
555 CLEAR_FIELD(init_tv);
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +0200556 if (type->tt_type == VAR_ANY)
557 // A variable of type "any" is not possible, just use zero instead
558 init_tv.v_type = VAR_NUMBER;
559 else
560 init_tv.v_type = type->tt_type;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200561 set_var_const(name, type, &init_tv, FALSE, 0);
562
563 vim_free(name);
564 return p;
565}
566
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200567/*
Bram Moolenaar10c65862020-10-08 21:16:42 +0200568 * Find the script-local variable that links to "dest".
569 * Returns NULL if not found.
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200570 */
Bram Moolenaar10c65862020-10-08 21:16:42 +0200571 svar_T *
572find_typval_in_script(typval_T *dest)
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200573{
574 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
575 int idx;
576
Bram Moolenaar81e17fb2020-08-21 21:55:43 +0200577 if (si->sn_version != SCRIPT_VERSION_VIM9)
578 // legacy script doesn't store variable types
Bram Moolenaar10c65862020-10-08 21:16:42 +0200579 return NULL;
Bram Moolenaar81e17fb2020-08-21 21:55:43 +0200580
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200581 // Find the svar_T in sn_var_vals.
582 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
583 {
584 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
585
586 if (sv->sv_tv == dest)
Bram Moolenaar10c65862020-10-08 21:16:42 +0200587 return sv;
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200588 }
589 iemsg("check_script_var_type(): not found");
Bram Moolenaar10c65862020-10-08 21:16:42 +0200590 return NULL;
591}
592
593/*
594 * Check if the type of script variable "dest" allows assigning "value".
595 * If needed convert "value" to a bool.
596 */
597 int
598check_script_var_type(typval_T *dest, typval_T *value, char_u *name)
599{
600 svar_T *sv = find_typval_in_script(dest);
601 int ret;
602
603 if (sv != NULL)
604 {
605 if (sv->sv_const)
606 {
607 semsg(_(e_readonlyvar), name);
608 return FAIL;
609 }
610 ret = check_typval_type(sv->sv_type, value, 0);
611 if (ret == OK && need_convert_to_bool(sv->sv_type, value))
612 {
613 int val = tv2bool(value);
614
615 clear_tv(value);
616 value->v_type = VAR_BOOL;
617 value->v_lock = 0;
618 value->vval.v_number = val ? VVAL_TRUE : VVAL_FALSE;
619 }
620 return ret;
621 }
622
Bram Moolenaarc785b9a2020-06-19 18:34:15 +0200623 return OK; // not really
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200624}
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200625
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100626#endif // FEAT_EVAL