blob: a72ef55095b822a0057f754beda9c1aa14f9bcaf [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 Moolenaar529fb5a2021-04-01 12:57:57 +0200301 // Find name in "script".
Bram Moolenaar08251752021-01-11 21:20:18 +0100302 idx = get_script_item_idx(sid, name, 0, cctx);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100303 if (idx >= 0)
304 {
305 sv = ((svar_T *)script->sn_var_vals.ga_data) + idx;
306 if (!sv->sv_export)
307 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +0100308 if (verbose)
309 semsg(_(e_item_not_exported_in_script_str), name);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100310 return -1;
311 }
312 *type = sv->sv_type;
313 *ufunc = NULL;
314 }
315 else
316 {
317 char_u buffer[200];
318 char_u *funcname;
319
320 // it could be a user function.
Bram Moolenaar5a67c372020-07-23 14:39:47 +0200321 if (STRLEN(name) < sizeof(buffer) - 15)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100322 funcname = buffer;
323 else
324 {
Bram Moolenaar5a67c372020-07-23 14:39:47 +0200325 funcname = alloc(STRLEN(name) + 15);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100326 if (funcname == NULL)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100327 return -1;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100328 }
329 funcname[0] = K_SPECIAL;
330 funcname[1] = KS_EXTRA;
331 funcname[2] = (int)KE_SNR;
332 sprintf((char *)funcname + 3, "%ld_%s", (long)sid, name);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200333 *ufunc = find_func(funcname, FALSE, NULL);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100334 if (funcname != buffer)
335 vim_free(funcname);
336
337 if (*ufunc == NULL)
338 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +0100339 if (verbose)
340 semsg(_(e_item_not_found_in_script_str), name);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100341 return -1;
342 }
Bram Moolenaar529fb5a2021-04-01 12:57:57 +0200343 else if (((*ufunc)->uf_flags & FC_EXPORT) == 0)
344 {
345 if (verbose)
346 semsg(_(e_item_not_exported_in_script_str), name);
347 *ufunc = NULL;
348 return -1;
349 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100350 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100351
352 return idx;
353}
354
355/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100356 * Handle an ":import" command and add the resulting imported_T to "gap", when
357 * not NULL, or script "import_sid" sn_imports.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200358 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100359 * Returns a pointer to after the command or NULL in case of failure
360 */
361 char_u *
Bram Moolenaar1c991142020-07-04 13:15:31 +0200362handle_import(
363 char_u *arg_start,
364 garray_T *gap,
365 int import_sid,
366 evalarg_T *evalarg,
367 void *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100368{
369 char_u *arg = arg_start;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200370 char_u *cmd_end = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100371 int ret = FAIL;
372 typval_T tv;
373 int sid = -1;
374 int res;
Bram Moolenaar0a842842021-02-27 22:41:19 +0100375 int mult = FALSE;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200376 garray_T names;
Bram Moolenaar0a842842021-02-27 22:41:19 +0100377 garray_T as_names;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100378
Bram Moolenaar1c991142020-07-04 13:15:31 +0200379 ga_init2(&names, sizeof(char_u *), 10);
Bram Moolenaar0a842842021-02-27 22:41:19 +0100380 ga_init2(&as_names, sizeof(char_u *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100381 if (*arg == '{')
382 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200383 // "import {item, item} from ..."
Bram Moolenaar0a842842021-02-27 22:41:19 +0100384 mult = TRUE;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200385 arg = skipwhite_and_linebreak(arg + 1, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100386 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200387
Bram Moolenaar0a842842021-02-27 22:41:19 +0100388 for (;;)
389 {
390 char_u *p = arg;
391 int had_comma = FALSE;
392 char_u *as_name = NULL;
393
394 // accept "*" or "Name"
395 if (!mult && arg[0] == '*' && IS_WHITE_OR_NUL(arg[1]))
396 ++arg;
397 else
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100398 while (eval_isnamec(*arg))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100399 ++arg;
Bram Moolenaar0a842842021-02-27 22:41:19 +0100400 if (p == arg)
401 break;
402 if (ga_grow(&names, 1) == FAIL || ga_grow(&as_names, 1) == FAIL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200403 goto erret;
Bram Moolenaar0a842842021-02-27 22:41:19 +0100404 ((char_u **)names.ga_data)[names.ga_len] = vim_strnsave(p, arg - p);
405 ++names.ga_len;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200406
Bram Moolenaar0a842842021-02-27 22:41:19 +0100407 arg = skipwhite_and_linebreak(arg, evalarg);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200408 if (STRNCMP("as", arg, 2) == 0 && IS_WHITE_OR_NUL(arg[2]))
409 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200410 // skip over "as Name "; no line break allowed after "as"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100411 arg = skipwhite(arg + 2);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200412 p = arg;
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100413 if (eval_isnamec1(*arg))
414 while (eval_isnamec(*arg))
415 ++arg;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100416 if (check_defined(p, arg - p, cctx, FALSE) == FAIL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200417 goto erret;
418 as_name = vim_strnsave(p, arg - p);
419 arg = skipwhite_and_linebreak(arg, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100420 }
421 else if (*arg_start == '*')
422 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200423 emsg(_(e_missing_as_after_star));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200424 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100425 }
Bram Moolenaar0a842842021-02-27 22:41:19 +0100426 // without "as Name" the as_names entry is NULL
427 ((char_u **)as_names.ga_data)[as_names.ga_len] = as_name;
428 ++as_names.ga_len;
429
430 if (!mult)
431 break;
432 if (*arg == ',')
433 {
434 had_comma = TRUE;
435 ++arg;
436 }
437 arg = skipwhite_and_linebreak(arg, evalarg);
438 if (*arg == '}')
439 {
440 ++arg;
441 break;
442 }
443 if (!had_comma)
444 {
445 emsg(_(e_missing_comma_in_import));
446 goto erret;
447 }
448 }
449 arg = skipwhite_and_linebreak(arg, evalarg);
450
451 if (names.ga_len == 0)
452 {
453 emsg(_(e_syntax_error_in_import));
454 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100455 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200456
457 if (STRNCMP("from", arg, 4) != 0 || !IS_WHITE_OR_NUL(arg[4]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100458 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200459 emsg(_(e_missing_from));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200460 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100461 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200462
Bram Moolenaar962d7212020-07-04 14:15:00 +0200463 arg = skipwhite_and_linebreak(arg + 4, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100464 tv.v_type = VAR_UNKNOWN;
465 // TODO: should we accept any expression?
466 if (*arg == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +0200467 ret = eval_lit_string(&arg, &tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100468 else if (*arg == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +0200469 ret = eval_string(&arg, &tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100470 if (ret == FAIL || tv.vval.v_string == NULL || *tv.vval.v_string == NUL)
471 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200472 emsg(_(e_invalid_string_after_from));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200473 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100474 }
475 cmd_end = arg;
476
Bram Moolenaar1c991142020-07-04 13:15:31 +0200477 /*
478 * find script file
479 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100480 if (*tv.vval.v_string == '.')
481 {
482 size_t len;
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100483 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100484 char_u *tail = gettail(si->sn_name);
485 char_u *from_name;
486
487 // Relative to current script: "./name.vim", "../../name.vim".
488 len = STRLEN(si->sn_name) - STRLEN(tail) + STRLEN(tv.vval.v_string) + 2;
489 from_name = alloc((int)len);
490 if (from_name == NULL)
491 {
492 clear_tv(&tv);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200493 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100494 }
495 vim_strncpy(from_name, si->sn_name, tail - si->sn_name);
496 add_pathsep(from_name);
497 STRCAT(from_name, tv.vval.v_string);
498 simplify_filename(from_name);
499
500 res = do_source(from_name, FALSE, DOSO_NONE, &sid);
501 vim_free(from_name);
502 }
503 else if (mch_isFullName(tv.vval.v_string))
504 {
505 // Absolute path: "/tmp/name.vim"
506 res = do_source(tv.vval.v_string, FALSE, DOSO_NONE, &sid);
507 }
508 else
509 {
510 size_t len = 7 + STRLEN(tv.vval.v_string) + 1;
511 char_u *from_name;
512
513 // Find file in "import" subdirs in 'runtimepath'.
514 from_name = alloc((int)len);
515 if (from_name == NULL)
516 {
517 clear_tv(&tv);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200518 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100519 }
520 vim_snprintf((char *)from_name, len, "import/%s", tv.vval.v_string);
521 res = source_in_path(p_rtp, from_name, DIP_NOAFTER, &sid);
522 vim_free(from_name);
523 }
524
525 if (res == FAIL || sid <= 0)
526 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200527 semsg(_(e_could_not_import_str), tv.vval.v_string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100528 clear_tv(&tv);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200529 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100530 }
531 clear_tv(&tv);
532
533 if (*arg_start == '*')
534 {
Bram Moolenaar0a842842021-02-27 22:41:19 +0100535 imported_T *imported;
536 char_u *as_name = ((char_u **)as_names.ga_data)[0];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100537
Bram Moolenaar0a842842021-02-27 22:41:19 +0100538 // "import * as That"
Bram Moolenaara6294952020-12-27 13:39:50 +0100539 imported = find_imported(as_name, STRLEN(as_name), cctx);
540 if (imported != NULL && imported->imp_sid == sid)
541 {
542 if (imported->imp_flags & IMP_FLAGS_RELOAD)
543 // import already defined on a previous script load
544 imported->imp_flags &= ~IMP_FLAGS_RELOAD;
545 else
546 {
547 semsg(_(e_name_already_defined_str), as_name);
548 goto erret;
549 }
550 }
551
552 imported = new_imported(gap != NULL ? gap
553 : &SCRIPT_ITEM(import_sid)->sn_imports);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100554 if (imported == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200555 goto erret;
556 imported->imp_name = as_name;
Bram Moolenaar0a842842021-02-27 22:41:19 +0100557 ((char_u **)as_names.ga_data)[0] = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100558 imported->imp_sid = sid;
Bram Moolenaara6294952020-12-27 13:39:50 +0100559 imported->imp_flags = IMP_FLAGS_STAR;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100560 }
561 else
562 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200563 int i;
564
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100565 arg = arg_start;
566 if (*arg == '{')
567 arg = skipwhite(arg + 1);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200568 for (i = 0; i < names.ga_len; ++i)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100569 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200570 char_u *name = ((char_u **)names.ga_data)[i];
Bram Moolenaar0a842842021-02-27 22:41:19 +0100571 char_u *as_name = ((char_u **)as_names.ga_data)[i];
Bram Moolenaara6294952020-12-27 13:39:50 +0100572 size_t len = STRLEN(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100573 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100574 imported_T *imported;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100575 ufunc_T *ufunc = NULL;
576 type_T *type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100577
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +0100578 idx = find_exported(sid, name, &ufunc, &type, cctx, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100579
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100580 if (idx < 0 && ufunc == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200581 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100582
Bram Moolenaara6294952020-12-27 13:39:50 +0100583 // If already imported with the same propertis and the
584 // IMP_FLAGS_RELOAD set then we keep that entry. Otherwise create
585 // a new one (and give an error for an existing import).
586 imported = find_imported(name, len, cctx);
587 if (imported != NULL
588 && (imported->imp_flags & IMP_FLAGS_RELOAD)
589 && imported->imp_sid == sid
590 && (idx >= 0
591 ? (equal_type(imported->imp_type, type)
592 && imported->imp_var_vals_idx == idx)
593 : (equal_type(imported->imp_type, ufunc->uf_func_type)
594 && STRCMP(imported->imp_funcname,
595 ufunc->uf_name) == 0)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100596 {
Bram Moolenaara6294952020-12-27 13:39:50 +0100597 imported->imp_flags &= ~IMP_FLAGS_RELOAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100598 }
599 else
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200600 {
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100601 if (check_defined(name, len, cctx, FALSE) == FAIL)
Bram Moolenaara6294952020-12-27 13:39:50 +0100602 goto erret;
603
604 imported = new_imported(gap != NULL ? gap
605 : &SCRIPT_ITEM(import_sid)->sn_imports);
606 if (imported == NULL)
607 goto erret;
608
Bram Moolenaar0a842842021-02-27 22:41:19 +0100609 if (as_name == NULL)
610 {
611 imported->imp_name = name;
612 ((char_u **)names.ga_data)[i] = NULL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100613 }
Bram Moolenaar0a842842021-02-27 22:41:19 +0100614 else
615 {
616 // "import This as That ..."
617 imported->imp_name = as_name;
618 ((char_u **)as_names.ga_data)[i] = NULL;
619 }
Bram Moolenaara6294952020-12-27 13:39:50 +0100620 imported->imp_sid = sid;
621 if (idx >= 0)
622 {
623 imported->imp_type = type;
624 imported->imp_var_vals_idx = idx;
625 }
626 else
627 {
628 imported->imp_type = ufunc->uf_func_type;
629 imported->imp_funcname = ufunc->uf_name;
630 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200631 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100632 }
633 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200634erret:
635 ga_clear_strings(&names);
Bram Moolenaar0a842842021-02-27 22:41:19 +0100636 ga_clear_strings(&as_names);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100637 return cmd_end;
638}
639
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200640/*
641 * Declare a script-local variable without init: "let var: type".
642 * "const" is an error since the value is missing.
643 * Returns a pointer to after the type.
644 */
645 char_u *
646vim9_declare_scriptvar(exarg_T *eap, char_u *arg)
647{
648 char_u *p;
649 char_u *name;
650 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
651 type_T *type;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200652 typval_T init_tv;
653
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200654 if (eap->cmdidx == CMD_final || eap->cmdidx == CMD_const)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200655 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200656 if (eap->cmdidx == CMD_final)
657 emsg(_(e_final_requires_a_value));
658 else
659 emsg(_(e_const_requires_a_value));
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200660 return arg + STRLEN(arg);
661 }
662
663 // Check for valid starting character.
664 if (!eval_isnamec1(*arg))
665 {
666 semsg(_(e_invarg2), arg);
667 return arg + STRLEN(arg);
668 }
669
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200670 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
Bram Moolenaar3b74b6b2020-06-19 19:01:43 +0200671 if (*p == ':' && (VIM_ISWHITE(p[1]) || p != arg + 1))
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200672 break;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200673
674 if (*p != ':')
675 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +0200676 emsg(_(e_type_or_initialization_required));
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200677 return arg + STRLEN(arg);
678 }
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200679 if (!VIM_ISWHITE(p[1]))
680 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100681 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200682 return arg + STRLEN(arg);
683 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200684 name = vim_strnsave(arg, p - arg);
685
686 // parse type
687 p = skipwhite(p + 1);
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100688 type = parse_type(&p, &si->sn_type_list, TRUE);
689 if (type == NULL)
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200690 {
691 vim_free(name);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200692 return p;
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200693 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200694
695 // Create the variable with 0/NULL value.
696 CLEAR_FIELD(init_tv);
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +0200697 if (type->tt_type == VAR_ANY)
698 // A variable of type "any" is not possible, just use zero instead
699 init_tv.v_type = VAR_NUMBER;
700 else
701 init_tv.v_type = type->tt_type;
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100702 set_var_const(name, type, &init_tv, FALSE, 0, 0);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200703
704 vim_free(name);
705 return p;
706}
707
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200708/*
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200709 * Vim9 part of adding a script variable: add it to sn_all_vars (lookup by name
710 * with a hashtable) and sn_var_vals (lookup by index).
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100711 * When "create" is TRUE this is a new variable, otherwise find and update an
712 * existing variable.
Bram Moolenaar08251752021-01-11 21:20:18 +0100713 * "flags" can have ASSIGN_FINAL or ASSIGN_CONST.
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100714 * When "*type" is NULL use "tv" for the type and update "*type".
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200715 */
716 void
Bram Moolenaar08251752021-01-11 21:20:18 +0100717update_vim9_script_var(
718 int create,
719 dictitem_T *di,
720 int flags,
721 typval_T *tv,
722 type_T **type)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200723{
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100724 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
725 hashitem_T *hi;
726 svar_T *sv;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200727
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100728 if (create)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200729 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100730 sallvar_T *newsav;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200731
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100732 // Store a pointer to the typval_T, so that it can be found by index
733 // instead of using a hastab lookup.
734 if (ga_grow(&si->sn_var_vals, 1) == FAIL)
735 return;
736
737 sv = ((svar_T *)si->sn_var_vals.ga_data) + si->sn_var_vals.ga_len;
738 newsav = (sallvar_T *)alloc_clear(
739 sizeof(sallvar_T) + STRLEN(di->di_key));
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200740 if (newsav == NULL)
741 return;
742
743 sv->sv_tv = &di->di_tv;
Bram Moolenaar08251752021-01-11 21:20:18 +0100744 sv->sv_const = (flags & ASSIGN_FINAL) ? ASSIGN_FINAL
745 : (flags & ASSIGN_CONST) ? ASSIGN_CONST : 0;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200746 sv->sv_export = is_export;
747 newsav->sav_var_vals_idx = si->sn_var_vals.ga_len;
748 ++si->sn_var_vals.ga_len;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200749 STRCPY(&newsav->sav_key, di->di_key);
750 sv->sv_name = newsav->sav_key;
751 newsav->sav_di = di;
752 newsav->sav_block_id = si->sn_current_block_id;
753
754 hi = hash_find(&si->sn_all_vars.dv_hashtab, newsav->sav_key);
755 if (!HASHITEM_EMPTY(hi))
756 {
757 sallvar_T *sav = HI2SAV(hi);
758
759 // variable with this name exists in another block
760 while (sav->sav_next != NULL)
761 sav = sav->sav_next;
762 sav->sav_next = newsav;
763 }
764 else
765 // new variable name
766 hash_add(&si->sn_all_vars.dv_hashtab, newsav->sav_key);
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200767 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100768 else
769 {
770 sv = find_typval_in_script(&di->di_tv);
771 }
772 if (sv != NULL)
773 {
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100774 if (*type == NULL)
Bram Moolenaar108cf012021-03-18 22:15:04 +0100775 *type = typval2type(tv, get_copyID(), &si->sn_type_list);
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100776 sv->sv_type = *type;
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100777 }
778
779 // let ex_export() know the export worked.
780 is_export = FALSE;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200781}
782
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200783/*
784 * Hide a script variable when leaving a block.
785 * "idx" is de index in sn_var_vals.
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200786 * When "func_defined" is non-zero then a function was defined in this block,
787 * the variable may be accessed by it. Otherwise the variable can be cleared.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200788 */
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200789 void
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200790hide_script_var(scriptitem_T *si, int idx, int func_defined)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200791{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200792 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200793 hashtab_T *script_ht = get_script_local_ht();
794 hashtab_T *all_ht = &si->sn_all_vars.dv_hashtab;
795 hashitem_T *script_hi;
796 hashitem_T *all_hi;
797
798 // Remove a variable declared inside the block, if it still exists.
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200799 // If it was added in a nested block it will already have been removed.
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200800 // The typval is moved into the sallvar_T.
801 script_hi = hash_find(script_ht, sv->sv_name);
802 all_hi = hash_find(all_ht, sv->sv_name);
803 if (!HASHITEM_EMPTY(script_hi) && !HASHITEM_EMPTY(all_hi))
804 {
805 dictitem_T *di = HI2DI(script_hi);
806 sallvar_T *sav = HI2SAV(all_hi);
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200807 sallvar_T *sav_prev = NULL;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200808
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200809 // There can be multiple entries with the same name in different
810 // blocks, find the right one.
811 while (sav != NULL && sav->sav_var_vals_idx != idx)
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200812 {
813 sav_prev = sav;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200814 sav = sav->sav_next;
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200815 }
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200816 if (sav != NULL)
817 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200818 if (func_defined)
819 {
820 // move the typval from the dictitem to the sallvar
821 sav->sav_tv = di->di_tv;
822 di->di_tv.v_type = VAR_UNKNOWN;
823 sav->sav_flags = di->di_flags;
824 sav->sav_di = NULL;
825 sv->sv_tv = &sav->sav_tv;
826 }
827 else
828 {
829 if (sav_prev == NULL)
830 hash_remove(all_ht, all_hi);
831 else
832 sav_prev->sav_next = sav->sav_next;
833 sv->sv_name = NULL;
834 vim_free(sav);
835 }
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200836 delete_var(script_ht, script_hi);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200837 }
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200838 }
839}
840
841/*
842 * Free the script variables from "sn_all_vars".
843 */
844 void
845free_all_script_vars(scriptitem_T *si)
846{
847 int todo;
848 hashtab_T *ht = &si->sn_all_vars.dv_hashtab;
849 hashitem_T *hi;
850 sallvar_T *sav;
851 sallvar_T *sav_next;
852
853 hash_lock(ht);
854 todo = (int)ht->ht_used;
855 for (hi = ht->ht_array; todo > 0; ++hi)
856 {
857 if (!HASHITEM_EMPTY(hi))
858 {
859 --todo;
860
861 // Free the variable. Don't remove it from the hashtab, ht_array
862 // might change then. hash_clear() takes care of it later.
863 sav = HI2SAV(hi);
864 while (sav != NULL)
865 {
866 sav_next = sav->sav_next;
867 if (sav->sav_di == NULL)
868 clear_tv(&sav->sav_tv);
869 vim_free(sav);
870 sav = sav_next;
871 }
872 }
873 }
874 hash_clear(ht);
875 hash_init(ht);
876
877 ga_clear(&si->sn_var_vals);
Bram Moolenaar2b327002020-12-26 15:39:31 +0100878
879 // existing commands using script variable indexes are no longer valid
880 si->sn_script_seq = current_sctx.sc_seq;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200881}
882
883/*
Bram Moolenaar10c65862020-10-08 21:16:42 +0200884 * Find the script-local variable that links to "dest".
885 * Returns NULL if not found.
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200886 */
Bram Moolenaar10c65862020-10-08 21:16:42 +0200887 svar_T *
888find_typval_in_script(typval_T *dest)
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200889{
890 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
891 int idx;
892
Bram Moolenaar81e17fb2020-08-21 21:55:43 +0200893 if (si->sn_version != SCRIPT_VERSION_VIM9)
894 // legacy script doesn't store variable types
Bram Moolenaar10c65862020-10-08 21:16:42 +0200895 return NULL;
Bram Moolenaar81e17fb2020-08-21 21:55:43 +0200896
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200897 // Find the svar_T in sn_var_vals.
898 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
899 {
900 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
901
Bram Moolenaarc0913d02020-12-05 14:44:37 +0100902 // If "sv_name" is NULL the variable was hidden when leaving a block,
903 // don't check "sv_tv" then, it might be used for another variable now.
904 if (sv->sv_name != NULL && sv->sv_tv == dest)
Bram Moolenaar10c65862020-10-08 21:16:42 +0200905 return sv;
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200906 }
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100907 iemsg("find_typval_in_script(): not found");
Bram Moolenaar10c65862020-10-08 21:16:42 +0200908 return NULL;
909}
910
911/*
912 * Check if the type of script variable "dest" allows assigning "value".
913 * If needed convert "value" to a bool.
914 */
915 int
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100916check_script_var_type(
917 typval_T *dest,
918 typval_T *value,
919 char_u *name,
920 where_T where)
Bram Moolenaar10c65862020-10-08 21:16:42 +0200921{
922 svar_T *sv = find_typval_in_script(dest);
923 int ret;
924
925 if (sv != NULL)
926 {
Bram Moolenaar08251752021-01-11 21:20:18 +0100927 if (sv->sv_const != 0)
Bram Moolenaar10c65862020-10-08 21:16:42 +0200928 {
929 semsg(_(e_readonlyvar), name);
930 return FAIL;
931 }
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100932 ret = check_typval_type(sv->sv_type, value, where);
Bram Moolenaar10c65862020-10-08 21:16:42 +0200933 if (ret == OK && need_convert_to_bool(sv->sv_type, value))
934 {
935 int val = tv2bool(value);
936
937 clear_tv(value);
938 value->v_type = VAR_BOOL;
939 value->v_lock = 0;
940 value->vval.v_number = val ? VVAL_TRUE : VVAL_FALSE;
941 }
942 return ret;
943 }
944
Bram Moolenaarc785b9a2020-06-19 18:34:15 +0200945 return OK; // not really
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200946}
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200947
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100948#endif // FEAT_EVAL