blob: 9839a06cfb9293e27df6c3e29166bf3f5eee55fe [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
34/*
Bram Moolenaare535db82021-03-31 21:07:24 +020035 * Return TRUE if the current script is Vim9 script.
36 * This also returns TRUE in a legacy function in a Vim9 script.
37 */
38 int
39current_script_is_vim9(void)
40{
41 return SCRIPT_ID_VALID(current_sctx.sc_sid)
42 && SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
43 == SCRIPT_VERSION_VIM9;
44}
45
46/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010047 * ":vim9script".
48 */
49 void
Bram Moolenaar9b8d6222020-12-28 18:26:00 +010050ex_vim9script(exarg_T *eap UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010051{
Bram Moolenaar9b8d6222020-12-28 18:26:00 +010052#ifdef FEAT_EVAL
Bram Moolenaar2b327002020-12-26 15:39:31 +010053 int sid = current_sctx.sc_sid;
Bram Moolenaar101f4812020-06-16 23:18:51 +020054 scriptitem_T *si;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010055
56 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
57 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +020058 emsg(_(e_vim9script_can_only_be_used_in_script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010059 return;
60 }
Bram Moolenaar2b327002020-12-26 15:39:31 +010061
62 si = SCRIPT_ITEM(sid);
63 if (si->sn_state == SN_STATE_HAD_COMMAND)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010064 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +020065 emsg(_(e_vim9script_must_be_first_command_in_script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010066 return;
67 }
Bram Moolenaar2b327002020-12-26 15:39:31 +010068 if (!IS_WHITE_OR_NUL(*eap->arg) && STRCMP(eap->arg, "noclear") != 0)
69 {
70 semsg(_(e_invarg2), eap->arg);
71 return;
72 }
73 if (si->sn_state == SN_STATE_RELOAD && IS_WHITE_OR_NUL(*eap->arg))
74 {
75 hashtab_T *ht = &SCRIPT_VARS(sid);
76
77 // Reloading a script without the "noclear" argument: clear
78 // script-local variables and functions.
79 hashtab_free_contents(ht);
80 hash_init(ht);
81 delete_script_functions(sid);
82
83 // old imports and script variables are no longer valid
84 free_imports_and_script_vars(sid);
85 }
86 si->sn_state = SN_STATE_HAD_COMMAND;
87
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010088 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
89 si->sn_version = SCRIPT_VERSION_VIM9;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010090
91 if (STRCMP(p_cpo, CPO_VIM) != 0)
92 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +020093 si->sn_save_cpo = vim_strsave(p_cpo);
Bram Moolenaar37294bd2021-03-10 13:40:08 +010094 set_option_value((char_u *)"cpo", 0L, (char_u *)CPO_VIM, OPT_NO_REDRAW);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010095 }
Bram Moolenaar9b8d6222020-12-28 18:26:00 +010096#else
97 // No check for this being the first command, it doesn't matter.
98 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
99#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100100}
101
102/*
Bram Moolenaarae616492020-07-28 20:07:27 +0200103 * When in Vim9 script give an error and return FAIL.
104 */
105 int
106not_in_vim9(exarg_T *eap)
107{
Bram Moolenaar68e30442020-07-28 21:15:07 +0200108 if (in_vim9script())
109 switch (eap->cmdidx)
110 {
Bram Moolenaarada1d872021-02-20 08:16:51 +0100111 case CMD_k:
112 if (eap->addr_count > 0)
113 {
114 emsg(_(e_norange));
115 return FAIL;
116 }
117 // FALLTHROUGH
Bram Moolenaar68e30442020-07-28 21:15:07 +0200118 case CMD_append:
119 case CMD_change:
Bram Moolenaarf5a48012020-08-01 17:00:03 +0200120 case CMD_insert:
Bram Moolenaar65088802021-03-13 21:07:21 +0100121 case CMD_open:
Bram Moolenaarf5a48012020-08-01 17:00:03 +0200122 case CMD_t:
Bram Moolenaar68e30442020-07-28 21:15:07 +0200123 case CMD_xit:
Bram Moolenaar9b8d6222020-12-28 18:26:00 +0100124 semsg(_(e_command_not_supported_in_vim9_script_missing_var_str), eap->cmd);
Bram Moolenaar68e30442020-07-28 21:15:07 +0200125 return FAIL;
126 default: break;
127 }
Bram Moolenaarae616492020-07-28 20:07:27 +0200128 return OK;
129}
130
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100131/*
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100132 * Give an error message if "p" points at "#{" and return TRUE.
133 * This avoids that using a legacy style #{} dictionary leads to difficult to
134 * understand errors.
135 */
136 int
137vim9_bad_comment(char_u *p)
138{
Bram Moolenaara0399ef2021-03-20 15:00:01 +0100139 if (p[0] == '#' && p[1] == '{' && p[2] != '{')
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100140 {
141 emsg(_(e_cannot_use_hash_curly_to_start_comment));
142 return TRUE;
143 }
144 return FALSE;
145}
146
147/*
Bram Moolenaara0399ef2021-03-20 15:00:01 +0100148 * Return TRUE if "p" points at a "#" not followed by one '{'.
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100149 * Does not check for white space.
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100150 */
151 int
152vim9_comment_start(char_u *p)
153{
Bram Moolenaara0399ef2021-03-20 15:00:01 +0100154 return p[0] == '#' && (p[1] != '{' || p[2] == '{');
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100155}
156
Bram Moolenaar9b8d6222020-12-28 18:26:00 +0100157#if defined(FEAT_EVAL) || defined(PROTO)
158
Bram Moolenaarae616492020-07-28 20:07:27 +0200159/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100160 * ":export let Name: type"
161 * ":export const Name: type"
162 * ":export def Name(..."
163 * ":export class Name ..."
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100164 */
165 void
Bram Moolenaar09689a02020-05-09 22:50:08 +0200166ex_export(exarg_T *eap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100167{
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200168 if (!in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100169 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200170 emsg(_(e_export_can_only_be_used_in_vim9script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100171 return;
172 }
173
174 eap->cmd = eap->arg;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +0100175 (void)find_ex_command(eap, NULL, lookup_scriptitem, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100176 switch (eap->cmdidx)
177 {
178 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200179 case CMD_var:
180 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100181 case CMD_const:
182 case CMD_def:
183 // case CMD_class:
184 is_export = TRUE;
185 do_cmdline(eap->cmd, eap->getline, eap->cookie,
186 DOCMD_VERBOSE + DOCMD_NOWAIT);
187
188 // The command will reset "is_export" when exporting an item.
189 if (is_export)
190 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200191 emsg(_(e_export_with_invalid_argument));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100192 is_export = FALSE;
193 }
194 break;
195 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200196 emsg(_(e_invalid_command_after_export));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100197 break;
198 }
199}
200
201/*
202 * Add a new imported item entry to the current script.
203 */
204 static imported_T *
205new_imported(garray_T *gap)
206{
207 if (ga_grow(gap, 1) == OK)
208 return ((imported_T *)gap->ga_data + gap->ga_len++);
209 return NULL;
210}
211
212/*
213 * Free all imported items in script "sid".
214 */
215 void
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200216free_imports_and_script_vars(int sid)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100217{
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100218 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100219 int idx;
220
221 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
222 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100223 imported_T *imp = ((imported_T *)si->sn_imports.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100224
225 vim_free(imp->imp_name);
226 }
227 ga_clear(&si->sn_imports);
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200228
229 free_all_script_vars(si);
230
Bram Moolenaar6110e792020-07-08 19:35:21 +0200231 clear_type_list(&si->sn_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100232}
233
234/*
Bram Moolenaara6294952020-12-27 13:39:50 +0100235 * Mark all imports as possible to redefine. Used when a script is loaded
236 * again but not cleared.
237 */
238 void
239mark_imports_for_reload(int sid)
240{
241 scriptitem_T *si = SCRIPT_ITEM(sid);
242 int idx;
243
244 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
245 {
246 imported_T *imp = ((imported_T *)si->sn_imports.ga_data) + idx;
247
248 imp->imp_flags |= IMP_FLAGS_RELOAD;
249 }
250}
251
252/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100253 * ":import Item from 'filename'"
254 * ":import Item as Alias from 'filename'"
255 * ":import {Item} from 'filename'".
256 * ":import {Item as Alias} from 'filename'"
257 * ":import {Item, Item} from 'filename'"
258 * ":import {Item, Item as Alias} from 'filename'"
259 *
260 * ":import * as Name from 'filename'"
261 */
262 void
263ex_import(exarg_T *eap)
264{
Bram Moolenaar1c991142020-07-04 13:15:31 +0200265 char_u *cmd_end;
266 evalarg_T evalarg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100267
Bram Moolenaar101f4812020-06-16 23:18:51 +0200268 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
269 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200270 emsg(_(e_import_can_only_be_used_in_script));
Bram Moolenaar101f4812020-06-16 23:18:51 +0200271 return;
272 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200273 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar101f4812020-06-16 23:18:51 +0200274
Bram Moolenaar1c991142020-07-04 13:15:31 +0200275 cmd_end = handle_import(eap->arg, NULL, current_sctx.sc_sid,
276 &evalarg, NULL);
Bram Moolenaar9721fb42020-06-11 23:10:46 +0200277 if (cmd_end != NULL)
278 eap->nextcmd = check_nextcmd(cmd_end);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200279 clear_evalarg(&evalarg, eap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100280}
281
282/*
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100283 * Find an exported item in "sid" matching the name at "*argp".
284 * When it is a variable return the index.
285 * When it is a user function return "*ufunc".
286 * When not found returns -1 and "*ufunc" is NULL.
287 */
288 int
289find_exported(
290 int sid,
Bram Moolenaar1c991142020-07-04 13:15:31 +0200291 char_u *name,
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100292 ufunc_T **ufunc,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200293 type_T **type,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +0100294 cctx_T *cctx,
295 int verbose)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100296{
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100297 int idx = -1;
298 svar_T *sv;
299 scriptitem_T *script = SCRIPT_ITEM(sid);
300
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100301 // find name in "script"
302 // TODO: also find script-local user function
Bram Moolenaar08251752021-01-11 21:20:18 +0100303 idx = get_script_item_idx(sid, name, 0, cctx);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100304 if (idx >= 0)
305 {
306 sv = ((svar_T *)script->sn_var_vals.ga_data) + idx;
307 if (!sv->sv_export)
308 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +0100309 if (verbose)
310 semsg(_(e_item_not_exported_in_script_str), name);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100311 return -1;
312 }
313 *type = sv->sv_type;
314 *ufunc = NULL;
315 }
316 else
317 {
318 char_u buffer[200];
319 char_u *funcname;
320
321 // it could be a user function.
Bram Moolenaar5a67c372020-07-23 14:39:47 +0200322 if (STRLEN(name) < sizeof(buffer) - 15)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100323 funcname = buffer;
324 else
325 {
Bram Moolenaar5a67c372020-07-23 14:39:47 +0200326 funcname = alloc(STRLEN(name) + 15);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100327 if (funcname == NULL)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100328 return -1;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100329 }
330 funcname[0] = K_SPECIAL;
331 funcname[1] = KS_EXTRA;
332 funcname[2] = (int)KE_SNR;
333 sprintf((char *)funcname + 3, "%ld_%s", (long)sid, name);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200334 *ufunc = find_func(funcname, FALSE, NULL);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100335 if (funcname != buffer)
336 vim_free(funcname);
337
338 if (*ufunc == NULL)
339 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +0100340 if (verbose)
341 semsg(_(e_item_not_found_in_script_str), name);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100342 return -1;
343 }
344 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100345
346 return idx;
347}
348
349/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100350 * Handle an ":import" command and add the resulting imported_T to "gap", when
351 * not NULL, or script "import_sid" sn_imports.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200352 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100353 * Returns a pointer to after the command or NULL in case of failure
354 */
355 char_u *
Bram Moolenaar1c991142020-07-04 13:15:31 +0200356handle_import(
357 char_u *arg_start,
358 garray_T *gap,
359 int import_sid,
360 evalarg_T *evalarg,
361 void *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100362{
363 char_u *arg = arg_start;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200364 char_u *cmd_end = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100365 int ret = FAIL;
366 typval_T tv;
367 int sid = -1;
368 int res;
Bram Moolenaar0a842842021-02-27 22:41:19 +0100369 int mult = FALSE;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200370 garray_T names;
Bram Moolenaar0a842842021-02-27 22:41:19 +0100371 garray_T as_names;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100372
Bram Moolenaar1c991142020-07-04 13:15:31 +0200373 ga_init2(&names, sizeof(char_u *), 10);
Bram Moolenaar0a842842021-02-27 22:41:19 +0100374 ga_init2(&as_names, sizeof(char_u *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100375 if (*arg == '{')
376 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200377 // "import {item, item} from ..."
Bram Moolenaar0a842842021-02-27 22:41:19 +0100378 mult = TRUE;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200379 arg = skipwhite_and_linebreak(arg + 1, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100380 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200381
Bram Moolenaar0a842842021-02-27 22:41:19 +0100382 for (;;)
383 {
384 char_u *p = arg;
385 int had_comma = FALSE;
386 char_u *as_name = NULL;
387
388 // accept "*" or "Name"
389 if (!mult && arg[0] == '*' && IS_WHITE_OR_NUL(arg[1]))
390 ++arg;
391 else
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100392 while (eval_isnamec(*arg))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100393 ++arg;
Bram Moolenaar0a842842021-02-27 22:41:19 +0100394 if (p == arg)
395 break;
396 if (ga_grow(&names, 1) == FAIL || ga_grow(&as_names, 1) == FAIL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200397 goto erret;
Bram Moolenaar0a842842021-02-27 22:41:19 +0100398 ((char_u **)names.ga_data)[names.ga_len] = vim_strnsave(p, arg - p);
399 ++names.ga_len;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200400
Bram Moolenaar0a842842021-02-27 22:41:19 +0100401 arg = skipwhite_and_linebreak(arg, evalarg);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200402 if (STRNCMP("as", arg, 2) == 0 && IS_WHITE_OR_NUL(arg[2]))
403 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200404 // skip over "as Name "; no line break allowed after "as"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100405 arg = skipwhite(arg + 2);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200406 p = arg;
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100407 if (eval_isnamec1(*arg))
408 while (eval_isnamec(*arg))
409 ++arg;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100410 if (check_defined(p, arg - p, cctx, FALSE) == FAIL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200411 goto erret;
412 as_name = vim_strnsave(p, arg - p);
413 arg = skipwhite_and_linebreak(arg, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100414 }
415 else if (*arg_start == '*')
416 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200417 emsg(_(e_missing_as_after_star));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200418 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100419 }
Bram Moolenaar0a842842021-02-27 22:41:19 +0100420 // without "as Name" the as_names entry is NULL
421 ((char_u **)as_names.ga_data)[as_names.ga_len] = as_name;
422 ++as_names.ga_len;
423
424 if (!mult)
425 break;
426 if (*arg == ',')
427 {
428 had_comma = TRUE;
429 ++arg;
430 }
431 arg = skipwhite_and_linebreak(arg, evalarg);
432 if (*arg == '}')
433 {
434 ++arg;
435 break;
436 }
437 if (!had_comma)
438 {
439 emsg(_(e_missing_comma_in_import));
440 goto erret;
441 }
442 }
443 arg = skipwhite_and_linebreak(arg, evalarg);
444
445 if (names.ga_len == 0)
446 {
447 emsg(_(e_syntax_error_in_import));
448 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100449 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200450
451 if (STRNCMP("from", arg, 4) != 0 || !IS_WHITE_OR_NUL(arg[4]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100452 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200453 emsg(_(e_missing_from));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200454 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100455 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200456
Bram Moolenaar962d7212020-07-04 14:15:00 +0200457 arg = skipwhite_and_linebreak(arg + 4, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100458 tv.v_type = VAR_UNKNOWN;
459 // TODO: should we accept any expression?
460 if (*arg == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +0200461 ret = eval_lit_string(&arg, &tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100462 else if (*arg == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +0200463 ret = eval_string(&arg, &tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100464 if (ret == FAIL || tv.vval.v_string == NULL || *tv.vval.v_string == NUL)
465 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200466 emsg(_(e_invalid_string_after_from));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200467 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100468 }
469 cmd_end = arg;
470
Bram Moolenaar1c991142020-07-04 13:15:31 +0200471 /*
472 * find script file
473 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100474 if (*tv.vval.v_string == '.')
475 {
476 size_t len;
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100477 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100478 char_u *tail = gettail(si->sn_name);
479 char_u *from_name;
480
481 // Relative to current script: "./name.vim", "../../name.vim".
482 len = STRLEN(si->sn_name) - STRLEN(tail) + STRLEN(tv.vval.v_string) + 2;
483 from_name = alloc((int)len);
484 if (from_name == NULL)
485 {
486 clear_tv(&tv);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200487 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100488 }
489 vim_strncpy(from_name, si->sn_name, tail - si->sn_name);
490 add_pathsep(from_name);
491 STRCAT(from_name, tv.vval.v_string);
492 simplify_filename(from_name);
493
494 res = do_source(from_name, FALSE, DOSO_NONE, &sid);
495 vim_free(from_name);
496 }
497 else if (mch_isFullName(tv.vval.v_string))
498 {
499 // Absolute path: "/tmp/name.vim"
500 res = do_source(tv.vval.v_string, FALSE, DOSO_NONE, &sid);
501 }
502 else
503 {
504 size_t len = 7 + STRLEN(tv.vval.v_string) + 1;
505 char_u *from_name;
506
507 // Find file in "import" subdirs in 'runtimepath'.
508 from_name = alloc((int)len);
509 if (from_name == NULL)
510 {
511 clear_tv(&tv);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200512 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100513 }
514 vim_snprintf((char *)from_name, len, "import/%s", tv.vval.v_string);
515 res = source_in_path(p_rtp, from_name, DIP_NOAFTER, &sid);
516 vim_free(from_name);
517 }
518
519 if (res == FAIL || sid <= 0)
520 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200521 semsg(_(e_could_not_import_str), tv.vval.v_string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100522 clear_tv(&tv);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200523 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100524 }
525 clear_tv(&tv);
526
527 if (*arg_start == '*')
528 {
Bram Moolenaar0a842842021-02-27 22:41:19 +0100529 imported_T *imported;
530 char_u *as_name = ((char_u **)as_names.ga_data)[0];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100531
Bram Moolenaar0a842842021-02-27 22:41:19 +0100532 // "import * as That"
Bram Moolenaara6294952020-12-27 13:39:50 +0100533 imported = find_imported(as_name, STRLEN(as_name), cctx);
534 if (imported != NULL && imported->imp_sid == sid)
535 {
536 if (imported->imp_flags & IMP_FLAGS_RELOAD)
537 // import already defined on a previous script load
538 imported->imp_flags &= ~IMP_FLAGS_RELOAD;
539 else
540 {
541 semsg(_(e_name_already_defined_str), as_name);
542 goto erret;
543 }
544 }
545
546 imported = new_imported(gap != NULL ? gap
547 : &SCRIPT_ITEM(import_sid)->sn_imports);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100548 if (imported == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200549 goto erret;
550 imported->imp_name = as_name;
Bram Moolenaar0a842842021-02-27 22:41:19 +0100551 ((char_u **)as_names.ga_data)[0] = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100552 imported->imp_sid = sid;
Bram Moolenaara6294952020-12-27 13:39:50 +0100553 imported->imp_flags = IMP_FLAGS_STAR;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100554 }
555 else
556 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200557 int i;
558
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100559 arg = arg_start;
560 if (*arg == '{')
561 arg = skipwhite(arg + 1);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200562 for (i = 0; i < names.ga_len; ++i)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100563 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200564 char_u *name = ((char_u **)names.ga_data)[i];
Bram Moolenaar0a842842021-02-27 22:41:19 +0100565 char_u *as_name = ((char_u **)as_names.ga_data)[i];
Bram Moolenaara6294952020-12-27 13:39:50 +0100566 size_t len = STRLEN(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100567 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100568 imported_T *imported;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100569 ufunc_T *ufunc = NULL;
570 type_T *type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100571
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +0100572 idx = find_exported(sid, name, &ufunc, &type, cctx, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100573
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100574 if (idx < 0 && ufunc == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200575 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100576
Bram Moolenaara6294952020-12-27 13:39:50 +0100577 // If already imported with the same propertis and the
578 // IMP_FLAGS_RELOAD set then we keep that entry. Otherwise create
579 // a new one (and give an error for an existing import).
580 imported = find_imported(name, len, cctx);
581 if (imported != NULL
582 && (imported->imp_flags & IMP_FLAGS_RELOAD)
583 && imported->imp_sid == sid
584 && (idx >= 0
585 ? (equal_type(imported->imp_type, type)
586 && imported->imp_var_vals_idx == idx)
587 : (equal_type(imported->imp_type, ufunc->uf_func_type)
588 && STRCMP(imported->imp_funcname,
589 ufunc->uf_name) == 0)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100590 {
Bram Moolenaara6294952020-12-27 13:39:50 +0100591 imported->imp_flags &= ~IMP_FLAGS_RELOAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100592 }
593 else
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200594 {
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100595 if (check_defined(name, len, cctx, FALSE) == FAIL)
Bram Moolenaara6294952020-12-27 13:39:50 +0100596 goto erret;
597
598 imported = new_imported(gap != NULL ? gap
599 : &SCRIPT_ITEM(import_sid)->sn_imports);
600 if (imported == NULL)
601 goto erret;
602
Bram Moolenaar0a842842021-02-27 22:41:19 +0100603 if (as_name == NULL)
604 {
605 imported->imp_name = name;
606 ((char_u **)names.ga_data)[i] = NULL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100607 }
Bram Moolenaar0a842842021-02-27 22:41:19 +0100608 else
609 {
610 // "import This as That ..."
611 imported->imp_name = as_name;
612 ((char_u **)as_names.ga_data)[i] = NULL;
613 }
Bram Moolenaara6294952020-12-27 13:39:50 +0100614 imported->imp_sid = sid;
615 if (idx >= 0)
616 {
617 imported->imp_type = type;
618 imported->imp_var_vals_idx = idx;
619 }
620 else
621 {
622 imported->imp_type = ufunc->uf_func_type;
623 imported->imp_funcname = ufunc->uf_name;
624 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200625 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100626 }
627 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200628erret:
629 ga_clear_strings(&names);
Bram Moolenaar0a842842021-02-27 22:41:19 +0100630 ga_clear_strings(&as_names);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100631 return cmd_end;
632}
633
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200634/*
635 * Declare a script-local variable without init: "let var: type".
636 * "const" is an error since the value is missing.
637 * Returns a pointer to after the type.
638 */
639 char_u *
640vim9_declare_scriptvar(exarg_T *eap, char_u *arg)
641{
642 char_u *p;
643 char_u *name;
644 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
645 type_T *type;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200646 typval_T init_tv;
647
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200648 if (eap->cmdidx == CMD_final || eap->cmdidx == CMD_const)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200649 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200650 if (eap->cmdidx == CMD_final)
651 emsg(_(e_final_requires_a_value));
652 else
653 emsg(_(e_const_requires_a_value));
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200654 return arg + STRLEN(arg);
655 }
656
657 // Check for valid starting character.
658 if (!eval_isnamec1(*arg))
659 {
660 semsg(_(e_invarg2), arg);
661 return arg + STRLEN(arg);
662 }
663
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200664 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
Bram Moolenaar3b74b6b2020-06-19 19:01:43 +0200665 if (*p == ':' && (VIM_ISWHITE(p[1]) || p != arg + 1))
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200666 break;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200667
668 if (*p != ':')
669 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +0200670 emsg(_(e_type_or_initialization_required));
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200671 return arg + STRLEN(arg);
672 }
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200673 if (!VIM_ISWHITE(p[1]))
674 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100675 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200676 return arg + STRLEN(arg);
677 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200678 name = vim_strnsave(arg, p - arg);
679
680 // parse type
681 p = skipwhite(p + 1);
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100682 type = parse_type(&p, &si->sn_type_list, TRUE);
683 if (type == NULL)
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200684 {
685 vim_free(name);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200686 return p;
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200687 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200688
689 // Create the variable with 0/NULL value.
690 CLEAR_FIELD(init_tv);
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +0200691 if (type->tt_type == VAR_ANY)
692 // A variable of type "any" is not possible, just use zero instead
693 init_tv.v_type = VAR_NUMBER;
694 else
695 init_tv.v_type = type->tt_type;
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100696 set_var_const(name, type, &init_tv, FALSE, 0, 0);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200697
698 vim_free(name);
699 return p;
700}
701
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200702/*
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200703 * Vim9 part of adding a script variable: add it to sn_all_vars (lookup by name
704 * with a hashtable) and sn_var_vals (lookup by index).
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100705 * When "create" is TRUE this is a new variable, otherwise find and update an
706 * existing variable.
Bram Moolenaar08251752021-01-11 21:20:18 +0100707 * "flags" can have ASSIGN_FINAL or ASSIGN_CONST.
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100708 * When "*type" is NULL use "tv" for the type and update "*type".
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200709 */
710 void
Bram Moolenaar08251752021-01-11 21:20:18 +0100711update_vim9_script_var(
712 int create,
713 dictitem_T *di,
714 int flags,
715 typval_T *tv,
716 type_T **type)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200717{
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100718 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
719 hashitem_T *hi;
720 svar_T *sv;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200721
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100722 if (create)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200723 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100724 sallvar_T *newsav;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200725
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100726 // Store a pointer to the typval_T, so that it can be found by index
727 // instead of using a hastab lookup.
728 if (ga_grow(&si->sn_var_vals, 1) == FAIL)
729 return;
730
731 sv = ((svar_T *)si->sn_var_vals.ga_data) + si->sn_var_vals.ga_len;
732 newsav = (sallvar_T *)alloc_clear(
733 sizeof(sallvar_T) + STRLEN(di->di_key));
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200734 if (newsav == NULL)
735 return;
736
737 sv->sv_tv = &di->di_tv;
Bram Moolenaar08251752021-01-11 21:20:18 +0100738 sv->sv_const = (flags & ASSIGN_FINAL) ? ASSIGN_FINAL
739 : (flags & ASSIGN_CONST) ? ASSIGN_CONST : 0;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200740 sv->sv_export = is_export;
741 newsav->sav_var_vals_idx = si->sn_var_vals.ga_len;
742 ++si->sn_var_vals.ga_len;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200743 STRCPY(&newsav->sav_key, di->di_key);
744 sv->sv_name = newsav->sav_key;
745 newsav->sav_di = di;
746 newsav->sav_block_id = si->sn_current_block_id;
747
748 hi = hash_find(&si->sn_all_vars.dv_hashtab, newsav->sav_key);
749 if (!HASHITEM_EMPTY(hi))
750 {
751 sallvar_T *sav = HI2SAV(hi);
752
753 // variable with this name exists in another block
754 while (sav->sav_next != NULL)
755 sav = sav->sav_next;
756 sav->sav_next = newsav;
757 }
758 else
759 // new variable name
760 hash_add(&si->sn_all_vars.dv_hashtab, newsav->sav_key);
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200761 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100762 else
763 {
764 sv = find_typval_in_script(&di->di_tv);
765 }
766 if (sv != NULL)
767 {
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100768 if (*type == NULL)
Bram Moolenaar108cf012021-03-18 22:15:04 +0100769 *type = typval2type(tv, get_copyID(), &si->sn_type_list);
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100770 sv->sv_type = *type;
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100771 }
772
773 // let ex_export() know the export worked.
774 is_export = FALSE;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200775}
776
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200777/*
778 * Hide a script variable when leaving a block.
779 * "idx" is de index in sn_var_vals.
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200780 * When "func_defined" is non-zero then a function was defined in this block,
781 * the variable may be accessed by it. Otherwise the variable can be cleared.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200782 */
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200783 void
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200784hide_script_var(scriptitem_T *si, int idx, int func_defined)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200785{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200786 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200787 hashtab_T *script_ht = get_script_local_ht();
788 hashtab_T *all_ht = &si->sn_all_vars.dv_hashtab;
789 hashitem_T *script_hi;
790 hashitem_T *all_hi;
791
792 // Remove a variable declared inside the block, if it still exists.
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200793 // If it was added in a nested block it will already have been removed.
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200794 // The typval is moved into the sallvar_T.
795 script_hi = hash_find(script_ht, sv->sv_name);
796 all_hi = hash_find(all_ht, sv->sv_name);
797 if (!HASHITEM_EMPTY(script_hi) && !HASHITEM_EMPTY(all_hi))
798 {
799 dictitem_T *di = HI2DI(script_hi);
800 sallvar_T *sav = HI2SAV(all_hi);
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200801 sallvar_T *sav_prev = NULL;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200802
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200803 // There can be multiple entries with the same name in different
804 // blocks, find the right one.
805 while (sav != NULL && sav->sav_var_vals_idx != idx)
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200806 {
807 sav_prev = sav;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200808 sav = sav->sav_next;
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200809 }
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200810 if (sav != NULL)
811 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200812 if (func_defined)
813 {
814 // move the typval from the dictitem to the sallvar
815 sav->sav_tv = di->di_tv;
816 di->di_tv.v_type = VAR_UNKNOWN;
817 sav->sav_flags = di->di_flags;
818 sav->sav_di = NULL;
819 sv->sv_tv = &sav->sav_tv;
820 }
821 else
822 {
823 if (sav_prev == NULL)
824 hash_remove(all_ht, all_hi);
825 else
826 sav_prev->sav_next = sav->sav_next;
827 sv->sv_name = NULL;
828 vim_free(sav);
829 }
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200830 delete_var(script_ht, script_hi);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200831 }
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200832 }
833}
834
835/*
836 * Free the script variables from "sn_all_vars".
837 */
838 void
839free_all_script_vars(scriptitem_T *si)
840{
841 int todo;
842 hashtab_T *ht = &si->sn_all_vars.dv_hashtab;
843 hashitem_T *hi;
844 sallvar_T *sav;
845 sallvar_T *sav_next;
846
847 hash_lock(ht);
848 todo = (int)ht->ht_used;
849 for (hi = ht->ht_array; todo > 0; ++hi)
850 {
851 if (!HASHITEM_EMPTY(hi))
852 {
853 --todo;
854
855 // Free the variable. Don't remove it from the hashtab, ht_array
856 // might change then. hash_clear() takes care of it later.
857 sav = HI2SAV(hi);
858 while (sav != NULL)
859 {
860 sav_next = sav->sav_next;
861 if (sav->sav_di == NULL)
862 clear_tv(&sav->sav_tv);
863 vim_free(sav);
864 sav = sav_next;
865 }
866 }
867 }
868 hash_clear(ht);
869 hash_init(ht);
870
871 ga_clear(&si->sn_var_vals);
Bram Moolenaar2b327002020-12-26 15:39:31 +0100872
873 // existing commands using script variable indexes are no longer valid
874 si->sn_script_seq = current_sctx.sc_seq;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200875}
876
877/*
Bram Moolenaar10c65862020-10-08 21:16:42 +0200878 * Find the script-local variable that links to "dest".
879 * Returns NULL if not found.
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200880 */
Bram Moolenaar10c65862020-10-08 21:16:42 +0200881 svar_T *
882find_typval_in_script(typval_T *dest)
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200883{
884 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
885 int idx;
886
Bram Moolenaar81e17fb2020-08-21 21:55:43 +0200887 if (si->sn_version != SCRIPT_VERSION_VIM9)
888 // legacy script doesn't store variable types
Bram Moolenaar10c65862020-10-08 21:16:42 +0200889 return NULL;
Bram Moolenaar81e17fb2020-08-21 21:55:43 +0200890
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200891 // Find the svar_T in sn_var_vals.
892 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
893 {
894 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
895
Bram Moolenaarc0913d02020-12-05 14:44:37 +0100896 // If "sv_name" is NULL the variable was hidden when leaving a block,
897 // don't check "sv_tv" then, it might be used for another variable now.
898 if (sv->sv_name != NULL && sv->sv_tv == dest)
Bram Moolenaar10c65862020-10-08 21:16:42 +0200899 return sv;
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200900 }
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100901 iemsg("find_typval_in_script(): not found");
Bram Moolenaar10c65862020-10-08 21:16:42 +0200902 return NULL;
903}
904
905/*
906 * Check if the type of script variable "dest" allows assigning "value".
907 * If needed convert "value" to a bool.
908 */
909 int
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100910check_script_var_type(
911 typval_T *dest,
912 typval_T *value,
913 char_u *name,
914 where_T where)
Bram Moolenaar10c65862020-10-08 21:16:42 +0200915{
916 svar_T *sv = find_typval_in_script(dest);
917 int ret;
918
919 if (sv != NULL)
920 {
Bram Moolenaar08251752021-01-11 21:20:18 +0100921 if (sv->sv_const != 0)
Bram Moolenaar10c65862020-10-08 21:16:42 +0200922 {
923 semsg(_(e_readonlyvar), name);
924 return FAIL;
925 }
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100926 ret = check_typval_type(sv->sv_type, value, where);
Bram Moolenaar10c65862020-10-08 21:16:42 +0200927 if (ret == OK && need_convert_to_bool(sv->sv_type, value))
928 {
929 int val = tv2bool(value);
930
931 clear_tv(value);
932 value->v_type = VAR_BOOL;
933 value->v_lock = 0;
934 value->vval.v_number = val ? VVAL_TRUE : VVAL_FALSE;
935 }
936 return ret;
937 }
938
Bram Moolenaarc785b9a2020-06-19 18:34:15 +0200939 return OK; // not really
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200940}
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200941
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100942#endif // FEAT_EVAL