blob: a60fbdd21ed9a4e974b02d089e4c52b061224e6e [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
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200137free_imports_and_script_vars(int sid)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100138{
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 Moolenaar8d739de2020-10-14 19:39:19 +0200149
150 free_all_script_vars(si);
151
Bram Moolenaar6110e792020-07-08 19:35:21 +0200152 clear_type_list(&si->sn_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100153}
154
155/*
156 * ":import Item from 'filename'"
157 * ":import Item as Alias from 'filename'"
158 * ":import {Item} from 'filename'".
159 * ":import {Item as Alias} from 'filename'"
160 * ":import {Item, Item} from 'filename'"
161 * ":import {Item, Item as Alias} from 'filename'"
162 *
163 * ":import * as Name from 'filename'"
164 */
165 void
166ex_import(exarg_T *eap)
167{
Bram Moolenaar1c991142020-07-04 13:15:31 +0200168 char_u *cmd_end;
169 evalarg_T evalarg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100170
Bram Moolenaar101f4812020-06-16 23:18:51 +0200171 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
172 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200173 emsg(_(e_import_can_only_be_used_in_script));
Bram Moolenaar101f4812020-06-16 23:18:51 +0200174 return;
175 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200176 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar101f4812020-06-16 23:18:51 +0200177
Bram Moolenaar1c991142020-07-04 13:15:31 +0200178 cmd_end = handle_import(eap->arg, NULL, current_sctx.sc_sid,
179 &evalarg, NULL);
Bram Moolenaar9721fb42020-06-11 23:10:46 +0200180 if (cmd_end != NULL)
181 eap->nextcmd = check_nextcmd(cmd_end);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200182 clear_evalarg(&evalarg, eap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100183}
184
185/*
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100186 * Find an exported item in "sid" matching the name at "*argp".
187 * When it is a variable return the index.
188 * When it is a user function return "*ufunc".
189 * When not found returns -1 and "*ufunc" is NULL.
190 */
191 int
192find_exported(
193 int sid,
Bram Moolenaar1c991142020-07-04 13:15:31 +0200194 char_u *name,
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100195 ufunc_T **ufunc,
196 type_T **type)
197{
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100198 int idx = -1;
199 svar_T *sv;
200 scriptitem_T *script = SCRIPT_ITEM(sid);
201
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100202 // find name in "script"
203 // TODO: also find script-local user function
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100204 idx = get_script_item_idx(sid, name, FALSE);
205 if (idx >= 0)
206 {
207 sv = ((svar_T *)script->sn_var_vals.ga_data) + idx;
208 if (!sv->sv_export)
209 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200210 semsg(_(e_item_not_exported_in_script_str), name);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100211 return -1;
212 }
213 *type = sv->sv_type;
214 *ufunc = NULL;
215 }
216 else
217 {
218 char_u buffer[200];
219 char_u *funcname;
220
221 // it could be a user function.
Bram Moolenaar5a67c372020-07-23 14:39:47 +0200222 if (STRLEN(name) < sizeof(buffer) - 15)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100223 funcname = buffer;
224 else
225 {
Bram Moolenaar5a67c372020-07-23 14:39:47 +0200226 funcname = alloc(STRLEN(name) + 15);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100227 if (funcname == NULL)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100228 return -1;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100229 }
230 funcname[0] = K_SPECIAL;
231 funcname[1] = KS_EXTRA;
232 funcname[2] = (int)KE_SNR;
233 sprintf((char *)funcname + 3, "%ld_%s", (long)sid, name);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200234 *ufunc = find_func(funcname, FALSE, NULL);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100235 if (funcname != buffer)
236 vim_free(funcname);
237
238 if (*ufunc == NULL)
239 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200240 semsg(_(e_item_not_found_in_script_str), name);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100241 return -1;
242 }
243 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100244
245 return idx;
246}
247
248/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100249 * Handle an ":import" command and add the resulting imported_T to "gap", when
250 * not NULL, or script "import_sid" sn_imports.
251 * Returns a pointer to after the command or NULL in case of failure
252 */
253 char_u *
Bram Moolenaar1c991142020-07-04 13:15:31 +0200254handle_import(
255 char_u *arg_start,
256 garray_T *gap,
257 int import_sid,
258 evalarg_T *evalarg,
259 void *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100260{
261 char_u *arg = arg_start;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200262 char_u *cmd_end = NULL;
263 char_u *as_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100264 int ret = FAIL;
265 typval_T tv;
266 int sid = -1;
267 int res;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200268 garray_T names;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100269
Bram Moolenaar1c991142020-07-04 13:15:31 +0200270 ga_init2(&names, sizeof(char_u *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100271 if (*arg == '{')
272 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200273 // "import {item, item} from ..."
274 arg = skipwhite_and_linebreak(arg + 1, evalarg);
275 for (;;)
276 {
277 char_u *p = arg;
278 int had_comma = FALSE;
279
280 while (eval_isnamec(*arg))
281 ++arg;
282 if (p == arg)
283 break;
284 if (ga_grow(&names, 1) == FAIL)
285 goto erret;
286 ((char_u **)names.ga_data)[names.ga_len] =
287 vim_strnsave(p, arg - p);
288 ++names.ga_len;
289 if (*arg == ',')
290 {
291 had_comma = TRUE;
292 ++arg;
293 }
294 arg = skipwhite_and_linebreak(arg, evalarg);
295 if (*arg == '}')
296 {
297 arg = skipwhite_and_linebreak(arg + 1, evalarg);
298 break;
299 }
300 if (!had_comma)
301 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200302 emsg(_(e_missing_comma_in_import));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200303 goto erret;
304 }
305 }
306 if (names.ga_len == 0)
307 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200308 emsg(_(e_syntax_error_in_import));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200309 goto erret;
310 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100311 }
312 else
313 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200314 // "import Name from ..."
315 // "import * as Name from ..."
316 // "import item [as Name] from ..."
317 arg = skipwhite_and_linebreak(arg, evalarg);
318 if (arg[0] == '*' && IS_WHITE_OR_NUL(arg[1]))
319 arg = skipwhite_and_linebreak(arg + 1, evalarg);
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100320 else if (eval_isnamec1(*arg))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100321 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200322 char_u *p = arg;
323
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100324 while (eval_isnamec(*arg))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100325 ++arg;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200326 if (ga_grow(&names, 1) == FAIL)
327 goto erret;
328 ((char_u **)names.ga_data)[names.ga_len] =
329 vim_strnsave(p, arg - p);
330 ++names.ga_len;
331 arg = skipwhite_and_linebreak(arg, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100332 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200333 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100334 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200335 emsg(_(e_syntax_error_in_import));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200336 goto erret;
337 }
338
339 if (STRNCMP("as", arg, 2) == 0 && IS_WHITE_OR_NUL(arg[2]))
340 {
341 char_u *p;
342
343 // skip over "as Name "; no line break allowed after "as"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100344 arg = skipwhite(arg + 2);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200345 p = arg;
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100346 if (eval_isnamec1(*arg))
347 while (eval_isnamec(*arg))
348 ++arg;
Bram Moolenaarcbb6bdc2020-07-06 21:53:17 +0200349 if (check_defined(p, arg - p, cctx) == FAIL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200350 goto erret;
351 as_name = vim_strnsave(p, arg - p);
352 arg = skipwhite_and_linebreak(arg, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100353 }
354 else if (*arg_start == '*')
355 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200356 emsg(_(e_missing_as_after_star));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200357 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100358 }
359 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200360
361 if (STRNCMP("from", arg, 4) != 0 || !IS_WHITE_OR_NUL(arg[4]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100362 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200363 emsg(_(e_missing_from));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200364 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100365 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200366
Bram Moolenaar962d7212020-07-04 14:15:00 +0200367 arg = skipwhite_and_linebreak(arg + 4, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100368 tv.v_type = VAR_UNKNOWN;
369 // TODO: should we accept any expression?
370 if (*arg == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +0200371 ret = eval_lit_string(&arg, &tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100372 else if (*arg == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +0200373 ret = eval_string(&arg, &tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100374 if (ret == FAIL || tv.vval.v_string == NULL || *tv.vval.v_string == NUL)
375 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200376 emsg(_(e_invalid_string_after_from));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200377 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100378 }
379 cmd_end = arg;
380
Bram Moolenaar1c991142020-07-04 13:15:31 +0200381 /*
382 * find script file
383 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100384 if (*tv.vval.v_string == '.')
385 {
386 size_t len;
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100387 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100388 char_u *tail = gettail(si->sn_name);
389 char_u *from_name;
390
391 // Relative to current script: "./name.vim", "../../name.vim".
392 len = STRLEN(si->sn_name) - STRLEN(tail) + STRLEN(tv.vval.v_string) + 2;
393 from_name = alloc((int)len);
394 if (from_name == NULL)
395 {
396 clear_tv(&tv);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200397 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100398 }
399 vim_strncpy(from_name, si->sn_name, tail - si->sn_name);
400 add_pathsep(from_name);
401 STRCAT(from_name, tv.vval.v_string);
402 simplify_filename(from_name);
403
404 res = do_source(from_name, FALSE, DOSO_NONE, &sid);
405 vim_free(from_name);
406 }
407 else if (mch_isFullName(tv.vval.v_string))
408 {
409 // Absolute path: "/tmp/name.vim"
410 res = do_source(tv.vval.v_string, FALSE, DOSO_NONE, &sid);
411 }
412 else
413 {
414 size_t len = 7 + STRLEN(tv.vval.v_string) + 1;
415 char_u *from_name;
416
417 // Find file in "import" subdirs in 'runtimepath'.
418 from_name = alloc((int)len);
419 if (from_name == NULL)
420 {
421 clear_tv(&tv);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200422 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100423 }
424 vim_snprintf((char *)from_name, len, "import/%s", tv.vval.v_string);
425 res = source_in_path(p_rtp, from_name, DIP_NOAFTER, &sid);
426 vim_free(from_name);
427 }
428
429 if (res == FAIL || sid <= 0)
430 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200431 semsg(_(e_could_not_import_str), tv.vval.v_string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100432 clear_tv(&tv);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200433 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100434 }
435 clear_tv(&tv);
436
437 if (*arg_start == '*')
438 {
439 imported_T *imported = new_imported(gap != NULL ? gap
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100440 : &SCRIPT_ITEM(import_sid)->sn_imports);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100441
442 if (imported == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200443 goto erret;
444 imported->imp_name = as_name;
445 as_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100446 imported->imp_sid = sid;
447 imported->imp_all = TRUE;
448 }
449 else
450 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200451 int i;
452
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100453 arg = arg_start;
454 if (*arg == '{')
455 arg = skipwhite(arg + 1);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200456 for (i = 0; i < names.ga_len; ++i)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100457 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200458 char_u *name = ((char_u **)names.ga_data)[i];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100459 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100460 imported_T *imported;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100461 ufunc_T *ufunc = NULL;
462 type_T *type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100463
Bram Moolenaar1c991142020-07-04 13:15:31 +0200464 idx = find_exported(sid, name, &ufunc, &type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100465
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100466 if (idx < 0 && ufunc == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200467 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100468
Bram Moolenaar1c991142020-07-04 13:15:31 +0200469 if (check_defined(name, STRLEN(name), cctx) == FAIL)
470 goto erret;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100471
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100472 imported = new_imported(gap != NULL ? gap
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100473 : &SCRIPT_ITEM(import_sid)->sn_imports);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100474 if (imported == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200475 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100476
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100477 // TODO: check for "as" following
Bram Moolenaar1c991142020-07-04 13:15:31 +0200478 // imported->imp_name = vim_strsave(as_name);
479 imported->imp_name = name;
480 ((char_u **)names.ga_data)[i] = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100481 imported->imp_sid = sid;
482 if (idx >= 0)
483 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100484 imported->imp_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100485 imported->imp_var_vals_idx = idx;
486 }
487 else
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200488 {
489 imported->imp_type = ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100490 imported->imp_funcname = ufunc->uf_name;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200491 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100492 }
493 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200494erret:
495 ga_clear_strings(&names);
496 vim_free(as_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100497 return cmd_end;
498}
499
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200500/*
501 * Declare a script-local variable without init: "let var: type".
502 * "const" is an error since the value is missing.
503 * Returns a pointer to after the type.
504 */
505 char_u *
506vim9_declare_scriptvar(exarg_T *eap, char_u *arg)
507{
508 char_u *p;
509 char_u *name;
510 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
511 type_T *type;
512 int called_emsg_before = called_emsg;
513 typval_T init_tv;
514
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200515 if (eap->cmdidx == CMD_final || eap->cmdidx == CMD_const)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200516 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200517 if (eap->cmdidx == CMD_final)
518 emsg(_(e_final_requires_a_value));
519 else
520 emsg(_(e_const_requires_a_value));
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200521 return arg + STRLEN(arg);
522 }
523
524 // Check for valid starting character.
525 if (!eval_isnamec1(*arg))
526 {
527 semsg(_(e_invarg2), arg);
528 return arg + STRLEN(arg);
529 }
530
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200531 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
Bram Moolenaar3b74b6b2020-06-19 19:01:43 +0200532 if (*p == ':' && (VIM_ISWHITE(p[1]) || p != arg + 1))
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200533 break;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200534
535 if (*p != ':')
536 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +0200537 emsg(_(e_type_or_initialization_required));
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200538 return arg + STRLEN(arg);
539 }
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200540 if (!VIM_ISWHITE(p[1]))
541 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200542 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200543 return arg + STRLEN(arg);
544 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200545 name = vim_strnsave(arg, p - arg);
546
547 // parse type
548 p = skipwhite(p + 1);
549 type = parse_type(&p, &si->sn_type_list);
550 if (called_emsg != called_emsg_before)
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200551 {
552 vim_free(name);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200553 return p;
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200554 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200555
556 // Create the variable with 0/NULL value.
557 CLEAR_FIELD(init_tv);
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +0200558 if (type->tt_type == VAR_ANY)
559 // A variable of type "any" is not possible, just use zero instead
560 init_tv.v_type = VAR_NUMBER;
561 else
562 init_tv.v_type = type->tt_type;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200563 set_var_const(name, type, &init_tv, FALSE, 0);
564
565 vim_free(name);
566 return p;
567}
568
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200569/*
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200570 * Vim9 part of adding a script variable: add it to sn_all_vars and
571 * sn_var_vals.
572 * When "type" is NULL use "tv" for the type.
573 */
574 void
575add_vim9_script_var(dictitem_T *di, typval_T *tv, type_T *type)
576{
577 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
578
579 // Store a pointer to the typval_T, so that it can be found by
580 // index instead of using a hastab lookup.
581 if (ga_grow(&si->sn_var_vals, 1) == OK)
582 {
583 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
584 + si->sn_var_vals.ga_len;
585 hashitem_T *hi;
586 sallvar_T *newsav = (sallvar_T *)alloc_clear(
587 sizeof(sallvar_T) + STRLEN(di->di_key));
588
589 if (newsav == NULL)
590 return;
591
592 sv->sv_tv = &di->di_tv;
593 if (type == NULL)
594 sv->sv_type = typval2type(tv, &si->sn_type_list);
595 else
596 sv->sv_type = type;
597 sv->sv_const = (di->di_flags & DI_FLAGS_LOCK) ? ASSIGN_CONST : 0;
598 sv->sv_export = is_export;
599 newsav->sav_var_vals_idx = si->sn_var_vals.ga_len;
600 ++si->sn_var_vals.ga_len;
601
602 STRCPY(&newsav->sav_key, di->di_key);
603 sv->sv_name = newsav->sav_key;
604 newsav->sav_di = di;
605 newsav->sav_block_id = si->sn_current_block_id;
606
607 hi = hash_find(&si->sn_all_vars.dv_hashtab, newsav->sav_key);
608 if (!HASHITEM_EMPTY(hi))
609 {
610 sallvar_T *sav = HI2SAV(hi);
611
612 // variable with this name exists in another block
613 while (sav->sav_next != NULL)
614 sav = sav->sav_next;
615 sav->sav_next = newsav;
616 }
617 else
618 // new variable name
619 hash_add(&si->sn_all_vars.dv_hashtab, newsav->sav_key);
620
621 // let ex_export() know the export worked.
622 is_export = FALSE;
623 }
624}
625
626 void
627hide_script_var(scriptitem_T *si, svar_T *sv)
628{
629 hashtab_T *script_ht = get_script_local_ht();
630 hashtab_T *all_ht = &si->sn_all_vars.dv_hashtab;
631 hashitem_T *script_hi;
632 hashitem_T *all_hi;
633
634 // Remove a variable declared inside the block, if it still exists.
635 // The typval is moved into the sallvar_T.
636 script_hi = hash_find(script_ht, sv->sv_name);
637 all_hi = hash_find(all_ht, sv->sv_name);
638 if (!HASHITEM_EMPTY(script_hi) && !HASHITEM_EMPTY(all_hi))
639 {
640 dictitem_T *di = HI2DI(script_hi);
641 sallvar_T *sav = HI2SAV(all_hi);
642
643 sav->sav_tv = di->di_tv;
644 di->di_tv.v_type = VAR_UNKNOWN;
645 sav->sav_flags = di->di_flags;
646 sav->sav_di = NULL;
647 delete_var(script_ht, script_hi);
648 }
649}
650
651/*
652 * Free the script variables from "sn_all_vars".
653 */
654 void
655free_all_script_vars(scriptitem_T *si)
656{
657 int todo;
658 hashtab_T *ht = &si->sn_all_vars.dv_hashtab;
659 hashitem_T *hi;
660 sallvar_T *sav;
661 sallvar_T *sav_next;
662
663 hash_lock(ht);
664 todo = (int)ht->ht_used;
665 for (hi = ht->ht_array; todo > 0; ++hi)
666 {
667 if (!HASHITEM_EMPTY(hi))
668 {
669 --todo;
670
671 // Free the variable. Don't remove it from the hashtab, ht_array
672 // might change then. hash_clear() takes care of it later.
673 sav = HI2SAV(hi);
674 while (sav != NULL)
675 {
676 sav_next = sav->sav_next;
677 if (sav->sav_di == NULL)
678 clear_tv(&sav->sav_tv);
679 vim_free(sav);
680 sav = sav_next;
681 }
682 }
683 }
684 hash_clear(ht);
685 hash_init(ht);
686
687 ga_clear(&si->sn_var_vals);
688}
689
690/*
Bram Moolenaar10c65862020-10-08 21:16:42 +0200691 * Find the script-local variable that links to "dest".
692 * Returns NULL if not found.
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200693 */
Bram Moolenaar10c65862020-10-08 21:16:42 +0200694 svar_T *
695find_typval_in_script(typval_T *dest)
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200696{
697 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
698 int idx;
699
Bram Moolenaar81e17fb2020-08-21 21:55:43 +0200700 if (si->sn_version != SCRIPT_VERSION_VIM9)
701 // legacy script doesn't store variable types
Bram Moolenaar10c65862020-10-08 21:16:42 +0200702 return NULL;
Bram Moolenaar81e17fb2020-08-21 21:55:43 +0200703
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200704 // Find the svar_T in sn_var_vals.
705 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
706 {
707 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
708
709 if (sv->sv_tv == dest)
Bram Moolenaar10c65862020-10-08 21:16:42 +0200710 return sv;
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200711 }
712 iemsg("check_script_var_type(): not found");
Bram Moolenaar10c65862020-10-08 21:16:42 +0200713 return NULL;
714}
715
716/*
717 * Check if the type of script variable "dest" allows assigning "value".
718 * If needed convert "value" to a bool.
719 */
720 int
721check_script_var_type(typval_T *dest, typval_T *value, char_u *name)
722{
723 svar_T *sv = find_typval_in_script(dest);
724 int ret;
725
726 if (sv != NULL)
727 {
728 if (sv->sv_const)
729 {
730 semsg(_(e_readonlyvar), name);
731 return FAIL;
732 }
733 ret = check_typval_type(sv->sv_type, value, 0);
734 if (ret == OK && need_convert_to_bool(sv->sv_type, value))
735 {
736 int val = tv2bool(value);
737
738 clear_tv(value);
739 value->v_type = VAR_BOOL;
740 value->v_lock = 0;
741 value->vval.v_number = val ? VVAL_TRUE : VVAL_FALSE;
742 }
743 return ret;
744 }
745
Bram Moolenaarc785b9a2020-06-19 18:34:15 +0200746 return OK; // not really
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200747}
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200748
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100749#endif // FEAT_EVAL