blob: 796b7d13cbdf6ac993300ea0d8edfee53ff9cffc [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
Bram Moolenaar9b8d6222020-12-28 18:26:00 +010016#if defined(FEAT_EVAL)
17# include "vim9.h"
18#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010019
Bram Moolenaare535db82021-03-31 21:07:24 +020020/*
21 * Return TRUE when currently using Vim9 script syntax.
22 * Does not go up the stack, a ":function" inside vim9script uses legacy
23 * syntax.
24 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010025 int
26in_vim9script(void)
27{
Bram Moolenaare535db82021-03-31 21:07:24 +020028 // "sc_version" is also set when compiling a ":def" function in legacy
29 // script.
Bram Moolenaar9979fcd2021-02-14 13:30:01 +010030 return current_sctx.sc_version == SCRIPT_VERSION_VIM9
31 || (cmdmod.cmod_flags & CMOD_VIM9CMD);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010032}
33
Bram Moolenaarb91d3f82021-04-01 13:17:50 +020034#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010035/*
Bram Moolenaare535db82021-03-31 21:07:24 +020036 * Return TRUE if the current script is Vim9 script.
37 * This also returns TRUE in a legacy function in a Vim9 script.
38 */
39 int
40current_script_is_vim9(void)
41{
42 return SCRIPT_ID_VALID(current_sctx.sc_sid)
43 && SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
44 == SCRIPT_VERSION_VIM9;
45}
Bram Moolenaarb91d3f82021-04-01 13:17:50 +020046#endif
Bram Moolenaare535db82021-03-31 21:07:24 +020047
48/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010049 * ":vim9script".
50 */
51 void
Bram Moolenaar9b8d6222020-12-28 18:26:00 +010052ex_vim9script(exarg_T *eap UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010053{
Bram Moolenaar9b8d6222020-12-28 18:26:00 +010054#ifdef FEAT_EVAL
Bram Moolenaar2b327002020-12-26 15:39:31 +010055 int sid = current_sctx.sc_sid;
Bram Moolenaar101f4812020-06-16 23:18:51 +020056 scriptitem_T *si;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010057
58 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
59 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +020060 emsg(_(e_vim9script_can_only_be_used_in_script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010061 return;
62 }
Bram Moolenaar2b327002020-12-26 15:39:31 +010063
64 si = SCRIPT_ITEM(sid);
65 if (si->sn_state == SN_STATE_HAD_COMMAND)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010066 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +020067 emsg(_(e_vim9script_must_be_first_command_in_script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010068 return;
69 }
Bram Moolenaar2b327002020-12-26 15:39:31 +010070 if (!IS_WHITE_OR_NUL(*eap->arg) && STRCMP(eap->arg, "noclear") != 0)
71 {
72 semsg(_(e_invarg2), eap->arg);
73 return;
74 }
75 if (si->sn_state == SN_STATE_RELOAD && IS_WHITE_OR_NUL(*eap->arg))
76 {
77 hashtab_T *ht = &SCRIPT_VARS(sid);
78
79 // Reloading a script without the "noclear" argument: clear
80 // script-local variables and functions.
81 hashtab_free_contents(ht);
82 hash_init(ht);
83 delete_script_functions(sid);
84
85 // old imports and script variables are no longer valid
86 free_imports_and_script_vars(sid);
87 }
88 si->sn_state = SN_STATE_HAD_COMMAND;
89
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010090 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
91 si->sn_version = SCRIPT_VERSION_VIM9;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010092
93 if (STRCMP(p_cpo, CPO_VIM) != 0)
94 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +020095 si->sn_save_cpo = vim_strsave(p_cpo);
Bram Moolenaar37294bd2021-03-10 13:40:08 +010096 set_option_value((char_u *)"cpo", 0L, (char_u *)CPO_VIM, OPT_NO_REDRAW);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010097 }
Bram Moolenaar9b8d6222020-12-28 18:26:00 +010098#else
99 // No check for this being the first command, it doesn't matter.
100 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
101#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100102}
103
104/*
Bram Moolenaarae616492020-07-28 20:07:27 +0200105 * When in Vim9 script give an error and return FAIL.
106 */
107 int
108not_in_vim9(exarg_T *eap)
109{
Bram Moolenaar68e30442020-07-28 21:15:07 +0200110 if (in_vim9script())
111 switch (eap->cmdidx)
112 {
Bram Moolenaarada1d872021-02-20 08:16:51 +0100113 case CMD_k:
114 if (eap->addr_count > 0)
115 {
116 emsg(_(e_norange));
117 return FAIL;
118 }
119 // FALLTHROUGH
Bram Moolenaar68e30442020-07-28 21:15:07 +0200120 case CMD_append:
121 case CMD_change:
Bram Moolenaarf5a48012020-08-01 17:00:03 +0200122 case CMD_insert:
Bram Moolenaar65088802021-03-13 21:07:21 +0100123 case CMD_open:
Bram Moolenaarf5a48012020-08-01 17:00:03 +0200124 case CMD_t:
Bram Moolenaar68e30442020-07-28 21:15:07 +0200125 case CMD_xit:
Bram Moolenaar9b8d6222020-12-28 18:26:00 +0100126 semsg(_(e_command_not_supported_in_vim9_script_missing_var_str), eap->cmd);
Bram Moolenaar68e30442020-07-28 21:15:07 +0200127 return FAIL;
128 default: break;
129 }
Bram Moolenaarae616492020-07-28 20:07:27 +0200130 return OK;
131}
132
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100133/*
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100134 * Give an error message if "p" points at "#{" and return TRUE.
135 * This avoids that using a legacy style #{} dictionary leads to difficult to
136 * understand errors.
137 */
138 int
139vim9_bad_comment(char_u *p)
140{
Bram Moolenaara0399ef2021-03-20 15:00:01 +0100141 if (p[0] == '#' && p[1] == '{' && p[2] != '{')
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100142 {
143 emsg(_(e_cannot_use_hash_curly_to_start_comment));
144 return TRUE;
145 }
146 return FALSE;
147}
148
149/*
Bram Moolenaara0399ef2021-03-20 15:00:01 +0100150 * Return TRUE if "p" points at a "#" not followed by one '{'.
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100151 * Does not check for white space.
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100152 */
153 int
154vim9_comment_start(char_u *p)
155{
Bram Moolenaara0399ef2021-03-20 15:00:01 +0100156 return p[0] == '#' && (p[1] != '{' || p[2] == '{');
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100157}
158
Bram Moolenaar9b8d6222020-12-28 18:26:00 +0100159#if defined(FEAT_EVAL) || defined(PROTO)
160
Bram Moolenaarae616492020-07-28 20:07:27 +0200161/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100162 * ":export let Name: type"
163 * ":export const Name: type"
164 * ":export def Name(..."
165 * ":export class Name ..."
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100166 */
167 void
Bram Moolenaar09689a02020-05-09 22:50:08 +0200168ex_export(exarg_T *eap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100169{
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200170 if (!in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100171 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200172 emsg(_(e_export_can_only_be_used_in_vim9script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100173 return;
174 }
175
176 eap->cmd = eap->arg;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +0100177 (void)find_ex_command(eap, NULL, lookup_scriptitem, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100178 switch (eap->cmdidx)
179 {
180 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200181 case CMD_var:
182 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100183 case CMD_const:
184 case CMD_def:
185 // case CMD_class:
186 is_export = TRUE;
187 do_cmdline(eap->cmd, eap->getline, eap->cookie,
188 DOCMD_VERBOSE + DOCMD_NOWAIT);
189
190 // The command will reset "is_export" when exporting an item.
191 if (is_export)
192 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200193 emsg(_(e_export_with_invalid_argument));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100194 is_export = FALSE;
195 }
196 break;
197 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200198 emsg(_(e_invalid_command_after_export));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100199 break;
200 }
201}
202
203/*
204 * Add a new imported item entry to the current script.
205 */
206 static imported_T *
207new_imported(garray_T *gap)
208{
209 if (ga_grow(gap, 1) == OK)
210 return ((imported_T *)gap->ga_data + gap->ga_len++);
211 return NULL;
212}
213
214/*
215 * Free all imported items in script "sid".
216 */
217 void
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200218free_imports_and_script_vars(int sid)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100219{
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100220 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100221 int idx;
222
223 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
224 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100225 imported_T *imp = ((imported_T *)si->sn_imports.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100226
227 vim_free(imp->imp_name);
228 }
229 ga_clear(&si->sn_imports);
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200230
231 free_all_script_vars(si);
232
Bram Moolenaar6110e792020-07-08 19:35:21 +0200233 clear_type_list(&si->sn_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100234}
235
236/*
Bram Moolenaara6294952020-12-27 13:39:50 +0100237 * Mark all imports as possible to redefine. Used when a script is loaded
238 * again but not cleared.
239 */
240 void
241mark_imports_for_reload(int sid)
242{
243 scriptitem_T *si = SCRIPT_ITEM(sid);
244 int idx;
245
246 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
247 {
248 imported_T *imp = ((imported_T *)si->sn_imports.ga_data) + idx;
249
250 imp->imp_flags |= IMP_FLAGS_RELOAD;
251 }
252}
253
254/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100255 * ":import Item from 'filename'"
256 * ":import Item as Alias from 'filename'"
257 * ":import {Item} from 'filename'".
258 * ":import {Item as Alias} from 'filename'"
259 * ":import {Item, Item} from 'filename'"
260 * ":import {Item, Item as Alias} from 'filename'"
261 *
262 * ":import * as Name from 'filename'"
263 */
264 void
265ex_import(exarg_T *eap)
266{
Bram Moolenaar1c991142020-07-04 13:15:31 +0200267 char_u *cmd_end;
268 evalarg_T evalarg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100269
Bram Moolenaar101f4812020-06-16 23:18:51 +0200270 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
271 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200272 emsg(_(e_import_can_only_be_used_in_script));
Bram Moolenaar101f4812020-06-16 23:18:51 +0200273 return;
274 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200275 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar101f4812020-06-16 23:18:51 +0200276
Bram Moolenaar1c991142020-07-04 13:15:31 +0200277 cmd_end = handle_import(eap->arg, NULL, current_sctx.sc_sid,
278 &evalarg, NULL);
Bram Moolenaar9721fb42020-06-11 23:10:46 +0200279 if (cmd_end != NULL)
280 eap->nextcmd = check_nextcmd(cmd_end);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200281 clear_evalarg(&evalarg, eap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100282}
283
284/*
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100285 * Find an exported item in "sid" matching the name at "*argp".
286 * When it is a variable return the index.
287 * When it is a user function return "*ufunc".
288 * When not found returns -1 and "*ufunc" is NULL.
289 */
290 int
291find_exported(
292 int sid,
Bram Moolenaar1c991142020-07-04 13:15:31 +0200293 char_u *name,
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100294 ufunc_T **ufunc,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200295 type_T **type,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +0100296 cctx_T *cctx,
297 int verbose)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100298{
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100299 int idx = -1;
300 svar_T *sv;
301 scriptitem_T *script = SCRIPT_ITEM(sid);
302
Bram Moolenaar529fb5a2021-04-01 12:57:57 +0200303 // Find name in "script".
Bram Moolenaar08251752021-01-11 21:20:18 +0100304 idx = get_script_item_idx(sid, name, 0, cctx);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100305 if (idx >= 0)
306 {
307 sv = ((svar_T *)script->sn_var_vals.ga_data) + idx;
308 if (!sv->sv_export)
309 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +0100310 if (verbose)
311 semsg(_(e_item_not_exported_in_script_str), name);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100312 return -1;
313 }
314 *type = sv->sv_type;
315 *ufunc = NULL;
316 }
317 else
318 {
319 char_u buffer[200];
320 char_u *funcname;
321
322 // it could be a user function.
Bram Moolenaar5a67c372020-07-23 14:39:47 +0200323 if (STRLEN(name) < sizeof(buffer) - 15)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100324 funcname = buffer;
325 else
326 {
Bram Moolenaar5a67c372020-07-23 14:39:47 +0200327 funcname = alloc(STRLEN(name) + 15);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100328 if (funcname == NULL)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100329 return -1;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100330 }
331 funcname[0] = K_SPECIAL;
332 funcname[1] = KS_EXTRA;
333 funcname[2] = (int)KE_SNR;
334 sprintf((char *)funcname + 3, "%ld_%s", (long)sid, name);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200335 *ufunc = find_func(funcname, FALSE, NULL);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100336 if (funcname != buffer)
337 vim_free(funcname);
338
339 if (*ufunc == NULL)
340 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +0100341 if (verbose)
342 semsg(_(e_item_not_found_in_script_str), name);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100343 return -1;
344 }
Bram Moolenaar529fb5a2021-04-01 12:57:57 +0200345 else if (((*ufunc)->uf_flags & FC_EXPORT) == 0)
346 {
347 if (verbose)
348 semsg(_(e_item_not_exported_in_script_str), name);
349 *ufunc = NULL;
350 return -1;
351 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100352 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100353
354 return idx;
355}
356
357/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100358 * Handle an ":import" command and add the resulting imported_T to "gap", when
359 * not NULL, or script "import_sid" sn_imports.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200360 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100361 * Returns a pointer to after the command or NULL in case of failure
362 */
363 char_u *
Bram Moolenaar1c991142020-07-04 13:15:31 +0200364handle_import(
365 char_u *arg_start,
366 garray_T *gap,
367 int import_sid,
368 evalarg_T *evalarg,
369 void *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100370{
371 char_u *arg = arg_start;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200372 char_u *cmd_end = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100373 int ret = FAIL;
374 typval_T tv;
375 int sid = -1;
376 int res;
Bram Moolenaar0a842842021-02-27 22:41:19 +0100377 int mult = FALSE;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200378 garray_T names;
Bram Moolenaar0a842842021-02-27 22:41:19 +0100379 garray_T as_names;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100380
Bram Moolenaar1c991142020-07-04 13:15:31 +0200381 ga_init2(&names, sizeof(char_u *), 10);
Bram Moolenaar0a842842021-02-27 22:41:19 +0100382 ga_init2(&as_names, sizeof(char_u *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100383 if (*arg == '{')
384 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200385 // "import {item, item} from ..."
Bram Moolenaar0a842842021-02-27 22:41:19 +0100386 mult = TRUE;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200387 arg = skipwhite_and_linebreak(arg + 1, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100388 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200389
Bram Moolenaar0a842842021-02-27 22:41:19 +0100390 for (;;)
391 {
392 char_u *p = arg;
393 int had_comma = FALSE;
394 char_u *as_name = NULL;
395
396 // accept "*" or "Name"
397 if (!mult && arg[0] == '*' && IS_WHITE_OR_NUL(arg[1]))
398 ++arg;
399 else
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100400 while (eval_isnamec(*arg))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100401 ++arg;
Bram Moolenaar0a842842021-02-27 22:41:19 +0100402 if (p == arg)
403 break;
404 if (ga_grow(&names, 1) == FAIL || ga_grow(&as_names, 1) == FAIL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200405 goto erret;
Bram Moolenaar0a842842021-02-27 22:41:19 +0100406 ((char_u **)names.ga_data)[names.ga_len] = vim_strnsave(p, arg - p);
407 ++names.ga_len;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200408
Bram Moolenaar0a842842021-02-27 22:41:19 +0100409 arg = skipwhite_and_linebreak(arg, evalarg);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200410 if (STRNCMP("as", arg, 2) == 0 && IS_WHITE_OR_NUL(arg[2]))
411 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200412 // skip over "as Name "; no line break allowed after "as"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100413 arg = skipwhite(arg + 2);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200414 p = arg;
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100415 if (eval_isnamec1(*arg))
416 while (eval_isnamec(*arg))
417 ++arg;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100418 if (check_defined(p, arg - p, cctx, FALSE) == FAIL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200419 goto erret;
420 as_name = vim_strnsave(p, arg - p);
421 arg = skipwhite_and_linebreak(arg, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100422 }
423 else if (*arg_start == '*')
424 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200425 emsg(_(e_missing_as_after_star));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200426 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100427 }
Bram Moolenaar0a842842021-02-27 22:41:19 +0100428 // without "as Name" the as_names entry is NULL
429 ((char_u **)as_names.ga_data)[as_names.ga_len] = as_name;
430 ++as_names.ga_len;
431
432 if (!mult)
433 break;
434 if (*arg == ',')
435 {
436 had_comma = TRUE;
437 ++arg;
438 }
439 arg = skipwhite_and_linebreak(arg, evalarg);
440 if (*arg == '}')
441 {
442 ++arg;
443 break;
444 }
445 if (!had_comma)
446 {
447 emsg(_(e_missing_comma_in_import));
448 goto erret;
449 }
450 }
451 arg = skipwhite_and_linebreak(arg, evalarg);
452
453 if (names.ga_len == 0)
454 {
455 emsg(_(e_syntax_error_in_import));
456 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100457 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200458
459 if (STRNCMP("from", arg, 4) != 0 || !IS_WHITE_OR_NUL(arg[4]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100460 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200461 emsg(_(e_missing_from));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200462 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100463 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200464
Bram Moolenaar962d7212020-07-04 14:15:00 +0200465 arg = skipwhite_and_linebreak(arg + 4, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100466 tv.v_type = VAR_UNKNOWN;
467 // TODO: should we accept any expression?
468 if (*arg == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +0200469 ret = eval_lit_string(&arg, &tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100470 else if (*arg == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +0200471 ret = eval_string(&arg, &tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100472 if (ret == FAIL || tv.vval.v_string == NULL || *tv.vval.v_string == NUL)
473 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200474 emsg(_(e_invalid_string_after_from));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200475 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100476 }
477 cmd_end = arg;
478
Bram Moolenaar1c991142020-07-04 13:15:31 +0200479 /*
480 * find script file
481 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100482 if (*tv.vval.v_string == '.')
483 {
484 size_t len;
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100485 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100486 char_u *tail = gettail(si->sn_name);
487 char_u *from_name;
488
489 // Relative to current script: "./name.vim", "../../name.vim".
490 len = STRLEN(si->sn_name) - STRLEN(tail) + STRLEN(tv.vval.v_string) + 2;
491 from_name = alloc((int)len);
492 if (from_name == NULL)
493 {
494 clear_tv(&tv);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200495 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100496 }
497 vim_strncpy(from_name, si->sn_name, tail - si->sn_name);
498 add_pathsep(from_name);
499 STRCAT(from_name, tv.vval.v_string);
500 simplify_filename(from_name);
501
502 res = do_source(from_name, FALSE, DOSO_NONE, &sid);
503 vim_free(from_name);
504 }
505 else if (mch_isFullName(tv.vval.v_string))
506 {
507 // Absolute path: "/tmp/name.vim"
508 res = do_source(tv.vval.v_string, FALSE, DOSO_NONE, &sid);
509 }
510 else
511 {
512 size_t len = 7 + STRLEN(tv.vval.v_string) + 1;
513 char_u *from_name;
514
515 // Find file in "import" subdirs in 'runtimepath'.
516 from_name = alloc((int)len);
517 if (from_name == NULL)
518 {
519 clear_tv(&tv);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200520 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100521 }
522 vim_snprintf((char *)from_name, len, "import/%s", tv.vval.v_string);
523 res = source_in_path(p_rtp, from_name, DIP_NOAFTER, &sid);
524 vim_free(from_name);
525 }
526
527 if (res == FAIL || sid <= 0)
528 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200529 semsg(_(e_could_not_import_str), tv.vval.v_string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100530 clear_tv(&tv);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200531 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100532 }
533 clear_tv(&tv);
534
535 if (*arg_start == '*')
536 {
Bram Moolenaar0a842842021-02-27 22:41:19 +0100537 imported_T *imported;
538 char_u *as_name = ((char_u **)as_names.ga_data)[0];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100539
Bram Moolenaar0a842842021-02-27 22:41:19 +0100540 // "import * as That"
Bram Moolenaara6294952020-12-27 13:39:50 +0100541 imported = find_imported(as_name, STRLEN(as_name), cctx);
542 if (imported != NULL && imported->imp_sid == sid)
543 {
544 if (imported->imp_flags & IMP_FLAGS_RELOAD)
545 // import already defined on a previous script load
546 imported->imp_flags &= ~IMP_FLAGS_RELOAD;
547 else
548 {
549 semsg(_(e_name_already_defined_str), as_name);
550 goto erret;
551 }
552 }
553
554 imported = new_imported(gap != NULL ? gap
555 : &SCRIPT_ITEM(import_sid)->sn_imports);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100556 if (imported == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200557 goto erret;
558 imported->imp_name = as_name;
Bram Moolenaar0a842842021-02-27 22:41:19 +0100559 ((char_u **)as_names.ga_data)[0] = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100560 imported->imp_sid = sid;
Bram Moolenaara6294952020-12-27 13:39:50 +0100561 imported->imp_flags = IMP_FLAGS_STAR;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100562 }
563 else
564 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200565 int i;
566
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100567 arg = arg_start;
568 if (*arg == '{')
569 arg = skipwhite(arg + 1);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200570 for (i = 0; i < names.ga_len; ++i)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100571 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200572 char_u *name = ((char_u **)names.ga_data)[i];
Bram Moolenaar0a842842021-02-27 22:41:19 +0100573 char_u *as_name = ((char_u **)as_names.ga_data)[i];
Bram Moolenaara6294952020-12-27 13:39:50 +0100574 size_t len = STRLEN(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100575 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100576 imported_T *imported;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100577 ufunc_T *ufunc = NULL;
578 type_T *type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100579
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +0100580 idx = find_exported(sid, name, &ufunc, &type, cctx, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100581
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100582 if (idx < 0 && ufunc == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200583 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100584
Bram Moolenaara6294952020-12-27 13:39:50 +0100585 // If already imported with the same propertis and the
586 // IMP_FLAGS_RELOAD set then we keep that entry. Otherwise create
587 // a new one (and give an error for an existing import).
588 imported = find_imported(name, len, cctx);
589 if (imported != NULL
590 && (imported->imp_flags & IMP_FLAGS_RELOAD)
591 && imported->imp_sid == sid
592 && (idx >= 0
593 ? (equal_type(imported->imp_type, type)
594 && imported->imp_var_vals_idx == idx)
595 : (equal_type(imported->imp_type, ufunc->uf_func_type)
596 && STRCMP(imported->imp_funcname,
597 ufunc->uf_name) == 0)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100598 {
Bram Moolenaara6294952020-12-27 13:39:50 +0100599 imported->imp_flags &= ~IMP_FLAGS_RELOAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100600 }
601 else
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200602 {
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100603 if (check_defined(name, len, cctx, FALSE) == FAIL)
Bram Moolenaara6294952020-12-27 13:39:50 +0100604 goto erret;
605
606 imported = new_imported(gap != NULL ? gap
607 : &SCRIPT_ITEM(import_sid)->sn_imports);
608 if (imported == NULL)
609 goto erret;
610
Bram Moolenaar0a842842021-02-27 22:41:19 +0100611 if (as_name == NULL)
612 {
613 imported->imp_name = name;
614 ((char_u **)names.ga_data)[i] = NULL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100615 }
Bram Moolenaar0a842842021-02-27 22:41:19 +0100616 else
617 {
618 // "import This as That ..."
619 imported->imp_name = as_name;
620 ((char_u **)as_names.ga_data)[i] = NULL;
621 }
Bram Moolenaara6294952020-12-27 13:39:50 +0100622 imported->imp_sid = sid;
623 if (idx >= 0)
624 {
625 imported->imp_type = type;
626 imported->imp_var_vals_idx = idx;
627 }
628 else
629 {
630 imported->imp_type = ufunc->uf_func_type;
631 imported->imp_funcname = ufunc->uf_name;
632 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200633 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100634 }
635 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200636erret:
637 ga_clear_strings(&names);
Bram Moolenaar0a842842021-02-27 22:41:19 +0100638 ga_clear_strings(&as_names);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100639 return cmd_end;
640}
641
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200642/*
643 * Declare a script-local variable without init: "let var: type".
644 * "const" is an error since the value is missing.
645 * Returns a pointer to after the type.
646 */
647 char_u *
648vim9_declare_scriptvar(exarg_T *eap, char_u *arg)
649{
650 char_u *p;
651 char_u *name;
652 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
653 type_T *type;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200654 typval_T init_tv;
655
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200656 if (eap->cmdidx == CMD_final || eap->cmdidx == CMD_const)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200657 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200658 if (eap->cmdidx == CMD_final)
659 emsg(_(e_final_requires_a_value));
660 else
661 emsg(_(e_const_requires_a_value));
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200662 return arg + STRLEN(arg);
663 }
664
665 // Check for valid starting character.
666 if (!eval_isnamec1(*arg))
667 {
668 semsg(_(e_invarg2), arg);
669 return arg + STRLEN(arg);
670 }
671
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200672 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
Bram Moolenaar3b74b6b2020-06-19 19:01:43 +0200673 if (*p == ':' && (VIM_ISWHITE(p[1]) || p != arg + 1))
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200674 break;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200675
676 if (*p != ':')
677 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +0200678 emsg(_(e_type_or_initialization_required));
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200679 return arg + STRLEN(arg);
680 }
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200681 if (!VIM_ISWHITE(p[1]))
682 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100683 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200684 return arg + STRLEN(arg);
685 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200686 name = vim_strnsave(arg, p - arg);
687
688 // parse type
689 p = skipwhite(p + 1);
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100690 type = parse_type(&p, &si->sn_type_list, TRUE);
691 if (type == NULL)
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200692 {
693 vim_free(name);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200694 return p;
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200695 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200696
697 // Create the variable with 0/NULL value.
698 CLEAR_FIELD(init_tv);
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +0200699 if (type->tt_type == VAR_ANY)
700 // A variable of type "any" is not possible, just use zero instead
701 init_tv.v_type = VAR_NUMBER;
702 else
703 init_tv.v_type = type->tt_type;
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100704 set_var_const(name, type, &init_tv, FALSE, 0, 0);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200705
706 vim_free(name);
707 return p;
708}
709
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200710/*
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200711 * Vim9 part of adding a script variable: add it to sn_all_vars (lookup by name
712 * with a hashtable) and sn_var_vals (lookup by index).
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100713 * When "create" is TRUE this is a new variable, otherwise find and update an
714 * existing variable.
Bram Moolenaar08251752021-01-11 21:20:18 +0100715 * "flags" can have ASSIGN_FINAL or ASSIGN_CONST.
Bram Moolenaarf2253962021-04-13 20:53:13 +0200716 * When "*type" is NULL use "tv" for the type and update "*type". If
717 * "do_member" is TRUE also use the member type, otherwise use "any".
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200718 */
719 void
Bram Moolenaar08251752021-01-11 21:20:18 +0100720update_vim9_script_var(
721 int create,
722 dictitem_T *di,
723 int flags,
724 typval_T *tv,
Bram Moolenaarf2253962021-04-13 20:53:13 +0200725 type_T **type,
726 int do_member)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200727{
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100728 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
729 hashitem_T *hi;
730 svar_T *sv;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200731
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100732 if (create)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200733 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100734 sallvar_T *newsav;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200735
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100736 // Store a pointer to the typval_T, so that it can be found by index
737 // instead of using a hastab lookup.
738 if (ga_grow(&si->sn_var_vals, 1) == FAIL)
739 return;
740
741 sv = ((svar_T *)si->sn_var_vals.ga_data) + si->sn_var_vals.ga_len;
742 newsav = (sallvar_T *)alloc_clear(
743 sizeof(sallvar_T) + STRLEN(di->di_key));
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200744 if (newsav == NULL)
745 return;
746
747 sv->sv_tv = &di->di_tv;
Bram Moolenaar08251752021-01-11 21:20:18 +0100748 sv->sv_const = (flags & ASSIGN_FINAL) ? ASSIGN_FINAL
749 : (flags & ASSIGN_CONST) ? ASSIGN_CONST : 0;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200750 sv->sv_export = is_export;
751 newsav->sav_var_vals_idx = si->sn_var_vals.ga_len;
752 ++si->sn_var_vals.ga_len;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200753 STRCPY(&newsav->sav_key, di->di_key);
754 sv->sv_name = newsav->sav_key;
755 newsav->sav_di = di;
756 newsav->sav_block_id = si->sn_current_block_id;
757
758 hi = hash_find(&si->sn_all_vars.dv_hashtab, newsav->sav_key);
759 if (!HASHITEM_EMPTY(hi))
760 {
761 sallvar_T *sav = HI2SAV(hi);
762
763 // variable with this name exists in another block
764 while (sav->sav_next != NULL)
765 sav = sav->sav_next;
766 sav->sav_next = newsav;
767 }
768 else
769 // new variable name
770 hash_add(&si->sn_all_vars.dv_hashtab, newsav->sav_key);
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200771 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100772 else
773 {
774 sv = find_typval_in_script(&di->di_tv);
775 }
776 if (sv != NULL)
777 {
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100778 if (*type == NULL)
Bram Moolenaarf2253962021-04-13 20:53:13 +0200779 *type = typval2type(tv, get_copyID(), &si->sn_type_list,
780 do_member);
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100781 sv->sv_type = *type;
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100782 }
783
784 // let ex_export() know the export worked.
785 is_export = FALSE;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200786}
787
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200788/*
789 * Hide a script variable when leaving a block.
790 * "idx" is de index in sn_var_vals.
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200791 * When "func_defined" is non-zero then a function was defined in this block,
792 * the variable may be accessed by it. Otherwise the variable can be cleared.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200793 */
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200794 void
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200795hide_script_var(scriptitem_T *si, int idx, int func_defined)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200796{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200797 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200798 hashtab_T *script_ht = get_script_local_ht();
799 hashtab_T *all_ht = &si->sn_all_vars.dv_hashtab;
800 hashitem_T *script_hi;
801 hashitem_T *all_hi;
802
803 // Remove a variable declared inside the block, if it still exists.
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200804 // If it was added in a nested block it will already have been removed.
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200805 // The typval is moved into the sallvar_T.
806 script_hi = hash_find(script_ht, sv->sv_name);
807 all_hi = hash_find(all_ht, sv->sv_name);
808 if (!HASHITEM_EMPTY(script_hi) && !HASHITEM_EMPTY(all_hi))
809 {
810 dictitem_T *di = HI2DI(script_hi);
811 sallvar_T *sav = HI2SAV(all_hi);
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200812 sallvar_T *sav_prev = NULL;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200813
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200814 // There can be multiple entries with the same name in different
815 // blocks, find the right one.
816 while (sav != NULL && sav->sav_var_vals_idx != idx)
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200817 {
818 sav_prev = sav;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200819 sav = sav->sav_next;
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200820 }
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200821 if (sav != NULL)
822 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200823 if (func_defined)
824 {
825 // move the typval from the dictitem to the sallvar
826 sav->sav_tv = di->di_tv;
827 di->di_tv.v_type = VAR_UNKNOWN;
828 sav->sav_flags = di->di_flags;
829 sav->sav_di = NULL;
830 sv->sv_tv = &sav->sav_tv;
831 }
832 else
833 {
834 if (sav_prev == NULL)
835 hash_remove(all_ht, all_hi);
836 else
837 sav_prev->sav_next = sav->sav_next;
838 sv->sv_name = NULL;
839 vim_free(sav);
840 }
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200841 delete_var(script_ht, script_hi);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200842 }
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200843 }
844}
845
846/*
847 * Free the script variables from "sn_all_vars".
848 */
849 void
850free_all_script_vars(scriptitem_T *si)
851{
852 int todo;
853 hashtab_T *ht = &si->sn_all_vars.dv_hashtab;
854 hashitem_T *hi;
855 sallvar_T *sav;
856 sallvar_T *sav_next;
857
858 hash_lock(ht);
859 todo = (int)ht->ht_used;
860 for (hi = ht->ht_array; todo > 0; ++hi)
861 {
862 if (!HASHITEM_EMPTY(hi))
863 {
864 --todo;
865
866 // Free the variable. Don't remove it from the hashtab, ht_array
867 // might change then. hash_clear() takes care of it later.
868 sav = HI2SAV(hi);
869 while (sav != NULL)
870 {
871 sav_next = sav->sav_next;
872 if (sav->sav_di == NULL)
873 clear_tv(&sav->sav_tv);
874 vim_free(sav);
875 sav = sav_next;
876 }
877 }
878 }
879 hash_clear(ht);
880 hash_init(ht);
881
882 ga_clear(&si->sn_var_vals);
Bram Moolenaar2b327002020-12-26 15:39:31 +0100883
884 // existing commands using script variable indexes are no longer valid
885 si->sn_script_seq = current_sctx.sc_seq;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200886}
887
888/*
Bram Moolenaar10c65862020-10-08 21:16:42 +0200889 * Find the script-local variable that links to "dest".
890 * Returns NULL if not found.
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200891 */
Bram Moolenaar10c65862020-10-08 21:16:42 +0200892 svar_T *
893find_typval_in_script(typval_T *dest)
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200894{
895 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
896 int idx;
897
Bram Moolenaar81e17fb2020-08-21 21:55:43 +0200898 if (si->sn_version != SCRIPT_VERSION_VIM9)
899 // legacy script doesn't store variable types
Bram Moolenaar10c65862020-10-08 21:16:42 +0200900 return NULL;
Bram Moolenaar81e17fb2020-08-21 21:55:43 +0200901
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200902 // Find the svar_T in sn_var_vals.
903 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
904 {
905 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
906
Bram Moolenaarc0913d02020-12-05 14:44:37 +0100907 // If "sv_name" is NULL the variable was hidden when leaving a block,
908 // don't check "sv_tv" then, it might be used for another variable now.
909 if (sv->sv_name != NULL && sv->sv_tv == dest)
Bram Moolenaar10c65862020-10-08 21:16:42 +0200910 return sv;
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200911 }
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100912 iemsg("find_typval_in_script(): not found");
Bram Moolenaar10c65862020-10-08 21:16:42 +0200913 return NULL;
914}
915
916/*
917 * Check if the type of script variable "dest" allows assigning "value".
918 * If needed convert "value" to a bool.
919 */
920 int
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100921check_script_var_type(
922 typval_T *dest,
923 typval_T *value,
924 char_u *name,
925 where_T where)
Bram Moolenaar10c65862020-10-08 21:16:42 +0200926{
927 svar_T *sv = find_typval_in_script(dest);
928 int ret;
929
930 if (sv != NULL)
931 {
Bram Moolenaar08251752021-01-11 21:20:18 +0100932 if (sv->sv_const != 0)
Bram Moolenaar10c65862020-10-08 21:16:42 +0200933 {
934 semsg(_(e_readonlyvar), name);
935 return FAIL;
936 }
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100937 ret = check_typval_type(sv->sv_type, value, where);
Bram Moolenaar10c65862020-10-08 21:16:42 +0200938 if (ret == OK && need_convert_to_bool(sv->sv_type, value))
939 {
940 int val = tv2bool(value);
941
942 clear_tv(value);
943 value->v_type = VAR_BOOL;
944 value->v_lock = 0;
945 value->vval.v_number = val ? VVAL_TRUE : VVAL_FALSE;
946 }
947 return ret;
948 }
949
Bram Moolenaarc785b9a2020-06-19 18:34:15 +0200950 return OK; // not really
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200951}
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200952
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100953#endif // FEAT_EVAL