blob: 8806de3137a7a48b3b34f50779d4ad56e02b914b [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 Moolenaar8a7d6542020-01-26 15:56:19 +010020 int
21in_vim9script(void)
22{
Bram Moolenaareb6880b2020-07-12 17:07:05 +020023 // Do not go up the stack, a ":function" inside vim9script uses legacy
24 // syntax. "sc_version" is also set when compiling a ":def" function in
25 // legacy script.
Bram Moolenaar9979fcd2021-02-14 13:30:01 +010026 return current_sctx.sc_version == SCRIPT_VERSION_VIM9
27 || (cmdmod.cmod_flags & CMOD_VIM9CMD);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010028}
29
30/*
31 * ":vim9script".
32 */
33 void
Bram Moolenaar9b8d6222020-12-28 18:26:00 +010034ex_vim9script(exarg_T *eap UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010035{
Bram Moolenaar9b8d6222020-12-28 18:26:00 +010036#ifdef FEAT_EVAL
Bram Moolenaar2b327002020-12-26 15:39:31 +010037 int sid = current_sctx.sc_sid;
Bram Moolenaar101f4812020-06-16 23:18:51 +020038 scriptitem_T *si;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010039
40 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
41 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +020042 emsg(_(e_vim9script_can_only_be_used_in_script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010043 return;
44 }
Bram Moolenaar2b327002020-12-26 15:39:31 +010045
46 si = SCRIPT_ITEM(sid);
47 if (si->sn_state == SN_STATE_HAD_COMMAND)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010048 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +020049 emsg(_(e_vim9script_must_be_first_command_in_script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010050 return;
51 }
Bram Moolenaar2b327002020-12-26 15:39:31 +010052 if (!IS_WHITE_OR_NUL(*eap->arg) && STRCMP(eap->arg, "noclear") != 0)
53 {
54 semsg(_(e_invarg2), eap->arg);
55 return;
56 }
57 if (si->sn_state == SN_STATE_RELOAD && IS_WHITE_OR_NUL(*eap->arg))
58 {
59 hashtab_T *ht = &SCRIPT_VARS(sid);
60
61 // Reloading a script without the "noclear" argument: clear
62 // script-local variables and functions.
63 hashtab_free_contents(ht);
64 hash_init(ht);
65 delete_script_functions(sid);
66
67 // old imports and script variables are no longer valid
68 free_imports_and_script_vars(sid);
69 }
70 si->sn_state = SN_STATE_HAD_COMMAND;
71
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010072 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
73 si->sn_version = SCRIPT_VERSION_VIM9;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010074
75 if (STRCMP(p_cpo, CPO_VIM) != 0)
76 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +020077 si->sn_save_cpo = vim_strsave(p_cpo);
Bram Moolenaar37294bd2021-03-10 13:40:08 +010078 set_option_value((char_u *)"cpo", 0L, (char_u *)CPO_VIM, OPT_NO_REDRAW);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010079 }
Bram Moolenaar9b8d6222020-12-28 18:26:00 +010080#else
81 // No check for this being the first command, it doesn't matter.
82 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
83#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010084}
85
86/*
Bram Moolenaarae616492020-07-28 20:07:27 +020087 * When in Vim9 script give an error and return FAIL.
88 */
89 int
90not_in_vim9(exarg_T *eap)
91{
Bram Moolenaar68e30442020-07-28 21:15:07 +020092 if (in_vim9script())
93 switch (eap->cmdidx)
94 {
Bram Moolenaarada1d872021-02-20 08:16:51 +010095 case CMD_k:
96 if (eap->addr_count > 0)
97 {
98 emsg(_(e_norange));
99 return FAIL;
100 }
101 // FALLTHROUGH
Bram Moolenaar68e30442020-07-28 21:15:07 +0200102 case CMD_append:
103 case CMD_change:
Bram Moolenaarf5a48012020-08-01 17:00:03 +0200104 case CMD_insert:
Bram Moolenaar65088802021-03-13 21:07:21 +0100105 case CMD_open:
Bram Moolenaarf5a48012020-08-01 17:00:03 +0200106 case CMD_t:
Bram Moolenaar68e30442020-07-28 21:15:07 +0200107 case CMD_xit:
Bram Moolenaar9b8d6222020-12-28 18:26:00 +0100108 semsg(_(e_command_not_supported_in_vim9_script_missing_var_str), eap->cmd);
Bram Moolenaar68e30442020-07-28 21:15:07 +0200109 return FAIL;
110 default: break;
111 }
Bram Moolenaarae616492020-07-28 20:07:27 +0200112 return OK;
113}
114
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100115/*
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100116 * Give an error message if "p" points at "#{" and return TRUE.
117 * This avoids that using a legacy style #{} dictionary leads to difficult to
118 * understand errors.
119 */
120 int
121vim9_bad_comment(char_u *p)
122{
Bram Moolenaara0399ef2021-03-20 15:00:01 +0100123 if (p[0] == '#' && p[1] == '{' && p[2] != '{')
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100124 {
125 emsg(_(e_cannot_use_hash_curly_to_start_comment));
126 return TRUE;
127 }
128 return FALSE;
129}
130
131/*
Bram Moolenaara0399ef2021-03-20 15:00:01 +0100132 * Return TRUE if "p" points at a "#" not followed by one '{'.
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100133 * Does not check for white space.
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100134 */
135 int
136vim9_comment_start(char_u *p)
137{
Bram Moolenaara0399ef2021-03-20 15:00:01 +0100138 return p[0] == '#' && (p[1] != '{' || p[2] == '{');
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100139}
140
Bram Moolenaar9b8d6222020-12-28 18:26:00 +0100141#if defined(FEAT_EVAL) || defined(PROTO)
142
Bram Moolenaarae616492020-07-28 20:07:27 +0200143/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100144 * ":export let Name: type"
145 * ":export const Name: type"
146 * ":export def Name(..."
147 * ":export class Name ..."
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100148 */
149 void
Bram Moolenaar09689a02020-05-09 22:50:08 +0200150ex_export(exarg_T *eap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100151{
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200152 if (!in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100153 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200154 emsg(_(e_export_can_only_be_used_in_vim9script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100155 return;
156 }
157
158 eap->cmd = eap->arg;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +0100159 (void)find_ex_command(eap, NULL, lookup_scriptitem, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100160 switch (eap->cmdidx)
161 {
162 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200163 case CMD_var:
164 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100165 case CMD_const:
166 case CMD_def:
167 // case CMD_class:
168 is_export = TRUE;
169 do_cmdline(eap->cmd, eap->getline, eap->cookie,
170 DOCMD_VERBOSE + DOCMD_NOWAIT);
171
172 // The command will reset "is_export" when exporting an item.
173 if (is_export)
174 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200175 emsg(_(e_export_with_invalid_argument));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100176 is_export = FALSE;
177 }
178 break;
179 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200180 emsg(_(e_invalid_command_after_export));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100181 break;
182 }
183}
184
185/*
186 * Add a new imported item entry to the current script.
187 */
188 static imported_T *
189new_imported(garray_T *gap)
190{
191 if (ga_grow(gap, 1) == OK)
192 return ((imported_T *)gap->ga_data + gap->ga_len++);
193 return NULL;
194}
195
196/*
197 * Free all imported items in script "sid".
198 */
199 void
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200200free_imports_and_script_vars(int sid)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100201{
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100202 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100203 int idx;
204
205 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
206 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100207 imported_T *imp = ((imported_T *)si->sn_imports.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100208
209 vim_free(imp->imp_name);
210 }
211 ga_clear(&si->sn_imports);
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200212
213 free_all_script_vars(si);
214
Bram Moolenaar6110e792020-07-08 19:35:21 +0200215 clear_type_list(&si->sn_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100216}
217
218/*
Bram Moolenaara6294952020-12-27 13:39:50 +0100219 * Mark all imports as possible to redefine. Used when a script is loaded
220 * again but not cleared.
221 */
222 void
223mark_imports_for_reload(int sid)
224{
225 scriptitem_T *si = SCRIPT_ITEM(sid);
226 int idx;
227
228 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
229 {
230 imported_T *imp = ((imported_T *)si->sn_imports.ga_data) + idx;
231
232 imp->imp_flags |= IMP_FLAGS_RELOAD;
233 }
234}
235
236/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100237 * ":import Item from 'filename'"
238 * ":import Item as Alias from 'filename'"
239 * ":import {Item} from 'filename'".
240 * ":import {Item as Alias} from 'filename'"
241 * ":import {Item, Item} from 'filename'"
242 * ":import {Item, Item as Alias} from 'filename'"
243 *
244 * ":import * as Name from 'filename'"
245 */
246 void
247ex_import(exarg_T *eap)
248{
Bram Moolenaar1c991142020-07-04 13:15:31 +0200249 char_u *cmd_end;
250 evalarg_T evalarg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100251
Bram Moolenaar101f4812020-06-16 23:18:51 +0200252 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
253 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200254 emsg(_(e_import_can_only_be_used_in_script));
Bram Moolenaar101f4812020-06-16 23:18:51 +0200255 return;
256 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200257 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar101f4812020-06-16 23:18:51 +0200258
Bram Moolenaar1c991142020-07-04 13:15:31 +0200259 cmd_end = handle_import(eap->arg, NULL, current_sctx.sc_sid,
260 &evalarg, NULL);
Bram Moolenaar9721fb42020-06-11 23:10:46 +0200261 if (cmd_end != NULL)
262 eap->nextcmd = check_nextcmd(cmd_end);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200263 clear_evalarg(&evalarg, eap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100264}
265
266/*
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100267 * Find an exported item in "sid" matching the name at "*argp".
268 * When it is a variable return the index.
269 * When it is a user function return "*ufunc".
270 * When not found returns -1 and "*ufunc" is NULL.
271 */
272 int
273find_exported(
274 int sid,
Bram Moolenaar1c991142020-07-04 13:15:31 +0200275 char_u *name,
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100276 ufunc_T **ufunc,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200277 type_T **type,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +0100278 cctx_T *cctx,
279 int verbose)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100280{
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100281 int idx = -1;
282 svar_T *sv;
283 scriptitem_T *script = SCRIPT_ITEM(sid);
284
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100285 // find name in "script"
286 // TODO: also find script-local user function
Bram Moolenaar08251752021-01-11 21:20:18 +0100287 idx = get_script_item_idx(sid, name, 0, cctx);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100288 if (idx >= 0)
289 {
290 sv = ((svar_T *)script->sn_var_vals.ga_data) + idx;
291 if (!sv->sv_export)
292 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +0100293 if (verbose)
294 semsg(_(e_item_not_exported_in_script_str), name);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100295 return -1;
296 }
297 *type = sv->sv_type;
298 *ufunc = NULL;
299 }
300 else
301 {
302 char_u buffer[200];
303 char_u *funcname;
304
305 // it could be a user function.
Bram Moolenaar5a67c372020-07-23 14:39:47 +0200306 if (STRLEN(name) < sizeof(buffer) - 15)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100307 funcname = buffer;
308 else
309 {
Bram Moolenaar5a67c372020-07-23 14:39:47 +0200310 funcname = alloc(STRLEN(name) + 15);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100311 if (funcname == NULL)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100312 return -1;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100313 }
314 funcname[0] = K_SPECIAL;
315 funcname[1] = KS_EXTRA;
316 funcname[2] = (int)KE_SNR;
317 sprintf((char *)funcname + 3, "%ld_%s", (long)sid, name);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200318 *ufunc = find_func(funcname, FALSE, NULL);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100319 if (funcname != buffer)
320 vim_free(funcname);
321
322 if (*ufunc == NULL)
323 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +0100324 if (verbose)
325 semsg(_(e_item_not_found_in_script_str), name);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100326 return -1;
327 }
328 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100329
330 return idx;
331}
332
333/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100334 * Handle an ":import" command and add the resulting imported_T to "gap", when
335 * not NULL, or script "import_sid" sn_imports.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200336 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100337 * Returns a pointer to after the command or NULL in case of failure
338 */
339 char_u *
Bram Moolenaar1c991142020-07-04 13:15:31 +0200340handle_import(
341 char_u *arg_start,
342 garray_T *gap,
343 int import_sid,
344 evalarg_T *evalarg,
345 void *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100346{
347 char_u *arg = arg_start;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200348 char_u *cmd_end = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100349 int ret = FAIL;
350 typval_T tv;
351 int sid = -1;
352 int res;
Bram Moolenaar0a842842021-02-27 22:41:19 +0100353 int mult = FALSE;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200354 garray_T names;
Bram Moolenaar0a842842021-02-27 22:41:19 +0100355 garray_T as_names;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100356
Bram Moolenaar1c991142020-07-04 13:15:31 +0200357 ga_init2(&names, sizeof(char_u *), 10);
Bram Moolenaar0a842842021-02-27 22:41:19 +0100358 ga_init2(&as_names, sizeof(char_u *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100359 if (*arg == '{')
360 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200361 // "import {item, item} from ..."
Bram Moolenaar0a842842021-02-27 22:41:19 +0100362 mult = TRUE;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200363 arg = skipwhite_and_linebreak(arg + 1, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100364 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200365
Bram Moolenaar0a842842021-02-27 22:41:19 +0100366 for (;;)
367 {
368 char_u *p = arg;
369 int had_comma = FALSE;
370 char_u *as_name = NULL;
371
372 // accept "*" or "Name"
373 if (!mult && arg[0] == '*' && IS_WHITE_OR_NUL(arg[1]))
374 ++arg;
375 else
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100376 while (eval_isnamec(*arg))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100377 ++arg;
Bram Moolenaar0a842842021-02-27 22:41:19 +0100378 if (p == arg)
379 break;
380 if (ga_grow(&names, 1) == FAIL || ga_grow(&as_names, 1) == FAIL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200381 goto erret;
Bram Moolenaar0a842842021-02-27 22:41:19 +0100382 ((char_u **)names.ga_data)[names.ga_len] = vim_strnsave(p, arg - p);
383 ++names.ga_len;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200384
Bram Moolenaar0a842842021-02-27 22:41:19 +0100385 arg = skipwhite_and_linebreak(arg, evalarg);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200386 if (STRNCMP("as", arg, 2) == 0 && IS_WHITE_OR_NUL(arg[2]))
387 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200388 // skip over "as Name "; no line break allowed after "as"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100389 arg = skipwhite(arg + 2);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200390 p = arg;
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100391 if (eval_isnamec1(*arg))
392 while (eval_isnamec(*arg))
393 ++arg;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100394 if (check_defined(p, arg - p, cctx, FALSE) == FAIL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200395 goto erret;
396 as_name = vim_strnsave(p, arg - p);
397 arg = skipwhite_and_linebreak(arg, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100398 }
399 else if (*arg_start == '*')
400 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200401 emsg(_(e_missing_as_after_star));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200402 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100403 }
Bram Moolenaar0a842842021-02-27 22:41:19 +0100404 // without "as Name" the as_names entry is NULL
405 ((char_u **)as_names.ga_data)[as_names.ga_len] = as_name;
406 ++as_names.ga_len;
407
408 if (!mult)
409 break;
410 if (*arg == ',')
411 {
412 had_comma = TRUE;
413 ++arg;
414 }
415 arg = skipwhite_and_linebreak(arg, evalarg);
416 if (*arg == '}')
417 {
418 ++arg;
419 break;
420 }
421 if (!had_comma)
422 {
423 emsg(_(e_missing_comma_in_import));
424 goto erret;
425 }
426 }
427 arg = skipwhite_and_linebreak(arg, evalarg);
428
429 if (names.ga_len == 0)
430 {
431 emsg(_(e_syntax_error_in_import));
432 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100433 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200434
435 if (STRNCMP("from", arg, 4) != 0 || !IS_WHITE_OR_NUL(arg[4]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100436 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200437 emsg(_(e_missing_from));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200438 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100439 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200440
Bram Moolenaar962d7212020-07-04 14:15:00 +0200441 arg = skipwhite_and_linebreak(arg + 4, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100442 tv.v_type = VAR_UNKNOWN;
443 // TODO: should we accept any expression?
444 if (*arg == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +0200445 ret = eval_lit_string(&arg, &tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100446 else if (*arg == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +0200447 ret = eval_string(&arg, &tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100448 if (ret == FAIL || tv.vval.v_string == NULL || *tv.vval.v_string == NUL)
449 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200450 emsg(_(e_invalid_string_after_from));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200451 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100452 }
453 cmd_end = arg;
454
Bram Moolenaar1c991142020-07-04 13:15:31 +0200455 /*
456 * find script file
457 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100458 if (*tv.vval.v_string == '.')
459 {
460 size_t len;
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100461 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100462 char_u *tail = gettail(si->sn_name);
463 char_u *from_name;
464
465 // Relative to current script: "./name.vim", "../../name.vim".
466 len = STRLEN(si->sn_name) - STRLEN(tail) + STRLEN(tv.vval.v_string) + 2;
467 from_name = alloc((int)len);
468 if (from_name == NULL)
469 {
470 clear_tv(&tv);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200471 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100472 }
473 vim_strncpy(from_name, si->sn_name, tail - si->sn_name);
474 add_pathsep(from_name);
475 STRCAT(from_name, tv.vval.v_string);
476 simplify_filename(from_name);
477
478 res = do_source(from_name, FALSE, DOSO_NONE, &sid);
479 vim_free(from_name);
480 }
481 else if (mch_isFullName(tv.vval.v_string))
482 {
483 // Absolute path: "/tmp/name.vim"
484 res = do_source(tv.vval.v_string, FALSE, DOSO_NONE, &sid);
485 }
486 else
487 {
488 size_t len = 7 + STRLEN(tv.vval.v_string) + 1;
489 char_u *from_name;
490
491 // Find file in "import" subdirs in 'runtimepath'.
492 from_name = alloc((int)len);
493 if (from_name == NULL)
494 {
495 clear_tv(&tv);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200496 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100497 }
498 vim_snprintf((char *)from_name, len, "import/%s", tv.vval.v_string);
499 res = source_in_path(p_rtp, from_name, DIP_NOAFTER, &sid);
500 vim_free(from_name);
501 }
502
503 if (res == FAIL || sid <= 0)
504 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200505 semsg(_(e_could_not_import_str), tv.vval.v_string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100506 clear_tv(&tv);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200507 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100508 }
509 clear_tv(&tv);
510
511 if (*arg_start == '*')
512 {
Bram Moolenaar0a842842021-02-27 22:41:19 +0100513 imported_T *imported;
514 char_u *as_name = ((char_u **)as_names.ga_data)[0];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100515
Bram Moolenaar0a842842021-02-27 22:41:19 +0100516 // "import * as That"
Bram Moolenaara6294952020-12-27 13:39:50 +0100517 imported = find_imported(as_name, STRLEN(as_name), cctx);
518 if (imported != NULL && imported->imp_sid == sid)
519 {
520 if (imported->imp_flags & IMP_FLAGS_RELOAD)
521 // import already defined on a previous script load
522 imported->imp_flags &= ~IMP_FLAGS_RELOAD;
523 else
524 {
525 semsg(_(e_name_already_defined_str), as_name);
526 goto erret;
527 }
528 }
529
530 imported = new_imported(gap != NULL ? gap
531 : &SCRIPT_ITEM(import_sid)->sn_imports);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100532 if (imported == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200533 goto erret;
534 imported->imp_name = as_name;
Bram Moolenaar0a842842021-02-27 22:41:19 +0100535 ((char_u **)as_names.ga_data)[0] = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100536 imported->imp_sid = sid;
Bram Moolenaara6294952020-12-27 13:39:50 +0100537 imported->imp_flags = IMP_FLAGS_STAR;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100538 }
539 else
540 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200541 int i;
542
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100543 arg = arg_start;
544 if (*arg == '{')
545 arg = skipwhite(arg + 1);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200546 for (i = 0; i < names.ga_len; ++i)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100547 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200548 char_u *name = ((char_u **)names.ga_data)[i];
Bram Moolenaar0a842842021-02-27 22:41:19 +0100549 char_u *as_name = ((char_u **)as_names.ga_data)[i];
Bram Moolenaara6294952020-12-27 13:39:50 +0100550 size_t len = STRLEN(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100551 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100552 imported_T *imported;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100553 ufunc_T *ufunc = NULL;
554 type_T *type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100555
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +0100556 idx = find_exported(sid, name, &ufunc, &type, cctx, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100557
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100558 if (idx < 0 && ufunc == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200559 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100560
Bram Moolenaara6294952020-12-27 13:39:50 +0100561 // If already imported with the same propertis and the
562 // IMP_FLAGS_RELOAD set then we keep that entry. Otherwise create
563 // a new one (and give an error for an existing import).
564 imported = find_imported(name, len, cctx);
565 if (imported != NULL
566 && (imported->imp_flags & IMP_FLAGS_RELOAD)
567 && imported->imp_sid == sid
568 && (idx >= 0
569 ? (equal_type(imported->imp_type, type)
570 && imported->imp_var_vals_idx == idx)
571 : (equal_type(imported->imp_type, ufunc->uf_func_type)
572 && STRCMP(imported->imp_funcname,
573 ufunc->uf_name) == 0)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100574 {
Bram Moolenaara6294952020-12-27 13:39:50 +0100575 imported->imp_flags &= ~IMP_FLAGS_RELOAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100576 }
577 else
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200578 {
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100579 if (check_defined(name, len, cctx, FALSE) == FAIL)
Bram Moolenaara6294952020-12-27 13:39:50 +0100580 goto erret;
581
582 imported = new_imported(gap != NULL ? gap
583 : &SCRIPT_ITEM(import_sid)->sn_imports);
584 if (imported == NULL)
585 goto erret;
586
Bram Moolenaar0a842842021-02-27 22:41:19 +0100587 if (as_name == NULL)
588 {
589 imported->imp_name = name;
590 ((char_u **)names.ga_data)[i] = NULL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100591 }
Bram Moolenaar0a842842021-02-27 22:41:19 +0100592 else
593 {
594 // "import This as That ..."
595 imported->imp_name = as_name;
596 ((char_u **)as_names.ga_data)[i] = NULL;
597 }
Bram Moolenaara6294952020-12-27 13:39:50 +0100598 imported->imp_sid = sid;
599 if (idx >= 0)
600 {
601 imported->imp_type = type;
602 imported->imp_var_vals_idx = idx;
603 }
604 else
605 {
606 imported->imp_type = ufunc->uf_func_type;
607 imported->imp_funcname = ufunc->uf_name;
608 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200609 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100610 }
611 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200612erret:
613 ga_clear_strings(&names);
Bram Moolenaar0a842842021-02-27 22:41:19 +0100614 ga_clear_strings(&as_names);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100615 return cmd_end;
616}
617
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200618/*
619 * Declare a script-local variable without init: "let var: type".
620 * "const" is an error since the value is missing.
621 * Returns a pointer to after the type.
622 */
623 char_u *
624vim9_declare_scriptvar(exarg_T *eap, char_u *arg)
625{
626 char_u *p;
627 char_u *name;
628 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
629 type_T *type;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200630 typval_T init_tv;
631
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200632 if (eap->cmdidx == CMD_final || eap->cmdidx == CMD_const)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200633 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200634 if (eap->cmdidx == CMD_final)
635 emsg(_(e_final_requires_a_value));
636 else
637 emsg(_(e_const_requires_a_value));
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200638 return arg + STRLEN(arg);
639 }
640
641 // Check for valid starting character.
642 if (!eval_isnamec1(*arg))
643 {
644 semsg(_(e_invarg2), arg);
645 return arg + STRLEN(arg);
646 }
647
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200648 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
Bram Moolenaar3b74b6b2020-06-19 19:01:43 +0200649 if (*p == ':' && (VIM_ISWHITE(p[1]) || p != arg + 1))
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200650 break;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200651
652 if (*p != ':')
653 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +0200654 emsg(_(e_type_or_initialization_required));
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200655 return arg + STRLEN(arg);
656 }
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200657 if (!VIM_ISWHITE(p[1]))
658 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100659 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200660 return arg + STRLEN(arg);
661 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200662 name = vim_strnsave(arg, p - arg);
663
664 // parse type
665 p = skipwhite(p + 1);
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100666 type = parse_type(&p, &si->sn_type_list, TRUE);
667 if (type == NULL)
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200668 {
669 vim_free(name);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200670 return p;
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200671 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200672
673 // Create the variable with 0/NULL value.
674 CLEAR_FIELD(init_tv);
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +0200675 if (type->tt_type == VAR_ANY)
676 // A variable of type "any" is not possible, just use zero instead
677 init_tv.v_type = VAR_NUMBER;
678 else
679 init_tv.v_type = type->tt_type;
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100680 set_var_const(name, type, &init_tv, FALSE, 0, 0);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200681
682 vim_free(name);
683 return p;
684}
685
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200686/*
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200687 * Vim9 part of adding a script variable: add it to sn_all_vars (lookup by name
688 * with a hashtable) and sn_var_vals (lookup by index).
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100689 * When "create" is TRUE this is a new variable, otherwise find and update an
690 * existing variable.
Bram Moolenaar08251752021-01-11 21:20:18 +0100691 * "flags" can have ASSIGN_FINAL or ASSIGN_CONST.
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100692 * When "*type" is NULL use "tv" for the type and update "*type".
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200693 */
694 void
Bram Moolenaar08251752021-01-11 21:20:18 +0100695update_vim9_script_var(
696 int create,
697 dictitem_T *di,
698 int flags,
699 typval_T *tv,
700 type_T **type)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200701{
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100702 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
703 hashitem_T *hi;
704 svar_T *sv;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200705
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100706 if (create)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200707 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100708 sallvar_T *newsav;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200709
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100710 // Store a pointer to the typval_T, so that it can be found by index
711 // instead of using a hastab lookup.
712 if (ga_grow(&si->sn_var_vals, 1) == FAIL)
713 return;
714
715 sv = ((svar_T *)si->sn_var_vals.ga_data) + si->sn_var_vals.ga_len;
716 newsav = (sallvar_T *)alloc_clear(
717 sizeof(sallvar_T) + STRLEN(di->di_key));
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200718 if (newsav == NULL)
719 return;
720
721 sv->sv_tv = &di->di_tv;
Bram Moolenaar08251752021-01-11 21:20:18 +0100722 sv->sv_const = (flags & ASSIGN_FINAL) ? ASSIGN_FINAL
723 : (flags & ASSIGN_CONST) ? ASSIGN_CONST : 0;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200724 sv->sv_export = is_export;
725 newsav->sav_var_vals_idx = si->sn_var_vals.ga_len;
726 ++si->sn_var_vals.ga_len;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200727 STRCPY(&newsav->sav_key, di->di_key);
728 sv->sv_name = newsav->sav_key;
729 newsav->sav_di = di;
730 newsav->sav_block_id = si->sn_current_block_id;
731
732 hi = hash_find(&si->sn_all_vars.dv_hashtab, newsav->sav_key);
733 if (!HASHITEM_EMPTY(hi))
734 {
735 sallvar_T *sav = HI2SAV(hi);
736
737 // variable with this name exists in another block
738 while (sav->sav_next != NULL)
739 sav = sav->sav_next;
740 sav->sav_next = newsav;
741 }
742 else
743 // new variable name
744 hash_add(&si->sn_all_vars.dv_hashtab, newsav->sav_key);
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200745 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100746 else
747 {
748 sv = find_typval_in_script(&di->di_tv);
749 }
750 if (sv != NULL)
751 {
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100752 if (*type == NULL)
Bram Moolenaar108cf012021-03-18 22:15:04 +0100753 *type = typval2type(tv, get_copyID(), &si->sn_type_list);
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100754 sv->sv_type = *type;
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100755 }
756
757 // let ex_export() know the export worked.
758 is_export = FALSE;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200759}
760
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200761/*
762 * Hide a script variable when leaving a block.
763 * "idx" is de index in sn_var_vals.
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200764 * When "func_defined" is non-zero then a function was defined in this block,
765 * the variable may be accessed by it. Otherwise the variable can be cleared.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200766 */
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200767 void
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200768hide_script_var(scriptitem_T *si, int idx, int func_defined)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200769{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200770 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200771 hashtab_T *script_ht = get_script_local_ht();
772 hashtab_T *all_ht = &si->sn_all_vars.dv_hashtab;
773 hashitem_T *script_hi;
774 hashitem_T *all_hi;
775
776 // Remove a variable declared inside the block, if it still exists.
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200777 // If it was added in a nested block it will already have been removed.
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200778 // The typval is moved into the sallvar_T.
779 script_hi = hash_find(script_ht, sv->sv_name);
780 all_hi = hash_find(all_ht, sv->sv_name);
781 if (!HASHITEM_EMPTY(script_hi) && !HASHITEM_EMPTY(all_hi))
782 {
783 dictitem_T *di = HI2DI(script_hi);
784 sallvar_T *sav = HI2SAV(all_hi);
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200785 sallvar_T *sav_prev = NULL;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200786
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200787 // There can be multiple entries with the same name in different
788 // blocks, find the right one.
789 while (sav != NULL && sav->sav_var_vals_idx != idx)
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200790 {
791 sav_prev = sav;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200792 sav = sav->sav_next;
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200793 }
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200794 if (sav != NULL)
795 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200796 if (func_defined)
797 {
798 // move the typval from the dictitem to the sallvar
799 sav->sav_tv = di->di_tv;
800 di->di_tv.v_type = VAR_UNKNOWN;
801 sav->sav_flags = di->di_flags;
802 sav->sav_di = NULL;
803 sv->sv_tv = &sav->sav_tv;
804 }
805 else
806 {
807 if (sav_prev == NULL)
808 hash_remove(all_ht, all_hi);
809 else
810 sav_prev->sav_next = sav->sav_next;
811 sv->sv_name = NULL;
812 vim_free(sav);
813 }
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200814 delete_var(script_ht, script_hi);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200815 }
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200816 }
817}
818
819/*
820 * Free the script variables from "sn_all_vars".
821 */
822 void
823free_all_script_vars(scriptitem_T *si)
824{
825 int todo;
826 hashtab_T *ht = &si->sn_all_vars.dv_hashtab;
827 hashitem_T *hi;
828 sallvar_T *sav;
829 sallvar_T *sav_next;
830
831 hash_lock(ht);
832 todo = (int)ht->ht_used;
833 for (hi = ht->ht_array; todo > 0; ++hi)
834 {
835 if (!HASHITEM_EMPTY(hi))
836 {
837 --todo;
838
839 // Free the variable. Don't remove it from the hashtab, ht_array
840 // might change then. hash_clear() takes care of it later.
841 sav = HI2SAV(hi);
842 while (sav != NULL)
843 {
844 sav_next = sav->sav_next;
845 if (sav->sav_di == NULL)
846 clear_tv(&sav->sav_tv);
847 vim_free(sav);
848 sav = sav_next;
849 }
850 }
851 }
852 hash_clear(ht);
853 hash_init(ht);
854
855 ga_clear(&si->sn_var_vals);
Bram Moolenaar2b327002020-12-26 15:39:31 +0100856
857 // existing commands using script variable indexes are no longer valid
858 si->sn_script_seq = current_sctx.sc_seq;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200859}
860
861/*
Bram Moolenaar10c65862020-10-08 21:16:42 +0200862 * Find the script-local variable that links to "dest".
863 * Returns NULL if not found.
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200864 */
Bram Moolenaar10c65862020-10-08 21:16:42 +0200865 svar_T *
866find_typval_in_script(typval_T *dest)
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200867{
868 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
869 int idx;
870
Bram Moolenaar81e17fb2020-08-21 21:55:43 +0200871 if (si->sn_version != SCRIPT_VERSION_VIM9)
872 // legacy script doesn't store variable types
Bram Moolenaar10c65862020-10-08 21:16:42 +0200873 return NULL;
Bram Moolenaar81e17fb2020-08-21 21:55:43 +0200874
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200875 // Find the svar_T in sn_var_vals.
876 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
877 {
878 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
879
Bram Moolenaarc0913d02020-12-05 14:44:37 +0100880 // If "sv_name" is NULL the variable was hidden when leaving a block,
881 // don't check "sv_tv" then, it might be used for another variable now.
882 if (sv->sv_name != NULL && sv->sv_tv == dest)
Bram Moolenaar10c65862020-10-08 21:16:42 +0200883 return sv;
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200884 }
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100885 iemsg("find_typval_in_script(): not found");
Bram Moolenaar10c65862020-10-08 21:16:42 +0200886 return NULL;
887}
888
889/*
890 * Check if the type of script variable "dest" allows assigning "value".
891 * If needed convert "value" to a bool.
892 */
893 int
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100894check_script_var_type(
895 typval_T *dest,
896 typval_T *value,
897 char_u *name,
898 where_T where)
Bram Moolenaar10c65862020-10-08 21:16:42 +0200899{
900 svar_T *sv = find_typval_in_script(dest);
901 int ret;
902
903 if (sv != NULL)
904 {
Bram Moolenaar08251752021-01-11 21:20:18 +0100905 if (sv->sv_const != 0)
Bram Moolenaar10c65862020-10-08 21:16:42 +0200906 {
907 semsg(_(e_readonlyvar), name);
908 return FAIL;
909 }
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100910 ret = check_typval_type(sv->sv_type, value, where);
Bram Moolenaar10c65862020-10-08 21:16:42 +0200911 if (ret == OK && need_convert_to_bool(sv->sv_type, value))
912 {
913 int val = tv2bool(value);
914
915 clear_tv(value);
916 value->v_type = VAR_BOOL;
917 value->v_lock = 0;
918 value->vval.v_number = val ? VVAL_TRUE : VVAL_FALSE;
919 }
920 return ret;
921 }
922
Bram Moolenaarc785b9a2020-06-19 18:34:15 +0200923 return OK; // not really
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200924}
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200925
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100926#endif // FEAT_EVAL