blob: 0dad36ff6a9678f7cf7c06af365c8c5086a2b399 [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 Moolenaardc7c3662021-12-20 15:04:29 +000016// When not generating protos this is included in proto.h
17#ifdef PROTO
Bram Moolenaar9b8d6222020-12-28 18:26:00 +010018# include "vim9.h"
19#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010020
Bram Moolenaare535db82021-03-31 21:07:24 +020021/*
22 * Return TRUE when currently using Vim9 script syntax.
23 * Does not go up the stack, a ":function" inside vim9script uses legacy
24 * syntax.
25 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010026 int
27in_vim9script(void)
28{
Bram Moolenaare535db82021-03-31 21:07:24 +020029 // "sc_version" is also set when compiling a ":def" function in legacy
30 // script.
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +020031 return (current_sctx.sc_version == SCRIPT_VERSION_VIM9
32 || (cmdmod.cmod_flags & CMOD_VIM9CMD))
33 && !(cmdmod.cmod_flags & CMOD_LEGACY);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010034}
35
Bram Moolenaarb91d3f82021-04-01 13:17:50 +020036#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010037/*
Bram Moolenaardd9de502021-08-15 13:49:42 +020038 * Return TRUE when currently in a script with script version smaller than
39 * "max_version" or command modifiers forced it.
40 */
41 int
42in_old_script(int max_version)
43{
Bram Moolenaar5cebca22021-08-15 14:39:13 +020044 return (current_sctx.sc_version < max_version
45 && !(cmdmod.cmod_flags & CMOD_VIM9CMD))
Bram Moolenaardd9de502021-08-15 13:49:42 +020046 || (cmdmod.cmod_flags & CMOD_LEGACY);
47}
48
49/*
Bram Moolenaare535db82021-03-31 21:07:24 +020050 * Return TRUE if the current script is Vim9 script.
51 * This also returns TRUE in a legacy function in a Vim9 script.
52 */
53 int
54current_script_is_vim9(void)
55{
56 return SCRIPT_ID_VALID(current_sctx.sc_sid)
57 && SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
58 == SCRIPT_VERSION_VIM9;
59}
Bram Moolenaarb91d3f82021-04-01 13:17:50 +020060#endif
Bram Moolenaare535db82021-03-31 21:07:24 +020061
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +000062#ifdef FEAT_EVAL
63/*
64 * Clear Vim9 script-local variables and functions.
65 */
66 void
67clear_vim9_scriptlocal_vars(int sid)
68{
69 hashtab_T *ht = &SCRIPT_VARS(sid);
70
71 hashtab_free_contents(ht);
72 hash_init(ht);
73 delete_script_functions(sid);
74
75 // old imports and script variables are no longer valid
76 free_imports_and_script_vars(sid);
77}
78#endif
79
Bram Moolenaare535db82021-03-31 21:07:24 +020080/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010081 * ":vim9script".
82 */
83 void
Bram Moolenaar9b8d6222020-12-28 18:26:00 +010084ex_vim9script(exarg_T *eap UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010085{
Bram Moolenaar9b8d6222020-12-28 18:26:00 +010086#ifdef FEAT_EVAL
Bram Moolenaar2b327002020-12-26 15:39:31 +010087 int sid = current_sctx.sc_sid;
Bram Moolenaar101f4812020-06-16 23:18:51 +020088 scriptitem_T *si;
Bram Moolenaardc4451d2022-01-09 21:36:37 +000089 int found_noclear = FALSE;
Bram Moolenaardc4451d2022-01-09 21:36:37 +000090 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010091
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +000092 if (!sourcing_a_script(eap))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010093 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +020094 emsg(_(e_vim9script_can_only_be_used_in_script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010095 return;
96 }
Bram Moolenaar2b327002020-12-26 15:39:31 +010097
98 si = SCRIPT_ITEM(sid);
99 if (si->sn_state == SN_STATE_HAD_COMMAND)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100100 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200101 emsg(_(e_vim9script_must_be_first_command_in_script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100102 return;
103 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000104
105 for (p = eap->arg; !IS_WHITE_OR_NUL(*p); p = skipwhite(skiptowhite(p)))
Bram Moolenaar2b327002020-12-26 15:39:31 +0100106 {
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000107 if (STRNCMP(p, "noclear", 7) == 0 && IS_WHITE_OR_NUL(p[7]))
108 {
109 if (found_noclear)
110 {
111 semsg(_(e_duplicate_argument_str), p);
112 return;
113 }
114 found_noclear = TRUE;
115 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000116 else
117 {
118 semsg(_(e_invalid_argument_str), eap->arg);
119 return;
120 }
Bram Moolenaar2b327002020-12-26 15:39:31 +0100121 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000122
123 if (si->sn_state == SN_STATE_RELOAD && !found_noclear)
Bram Moolenaar2b327002020-12-26 15:39:31 +0100124 // Reloading a script without the "noclear" argument: clear
125 // script-local variables and functions.
Yegappan Lakshmanan35dc1762022-03-22 12:13:54 +0000126 clear_vim9_scriptlocal_vars(sid);
Bram Moolenaar2b327002020-12-26 15:39:31 +0100127 si->sn_state = SN_STATE_HAD_COMMAND;
128
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000129 // Store the prefix with the script, it is used to find exported functions.
Bram Moolenaar71930f12022-01-13 13:47:43 +0000130 if (si->sn_autoload_prefix == NULL)
131 si->sn_autoload_prefix = get_autoload_prefix(si);
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000132
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100133 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
134 si->sn_version = SCRIPT_VERSION_VIM9;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100135
136 if (STRCMP(p_cpo, CPO_VIM) != 0)
137 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200138 si->sn_save_cpo = vim_strsave(p_cpo);
Bram Moolenaar37294bd2021-03-10 13:40:08 +0100139 set_option_value((char_u *)"cpo", 0L, (char_u *)CPO_VIM, OPT_NO_REDRAW);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100140 }
Bram Moolenaar9b8d6222020-12-28 18:26:00 +0100141#else
142 // No check for this being the first command, it doesn't matter.
143 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
144#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100145}
146
Dominique Pelle748b3082022-01-08 12:41:16 +0000147#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100148/*
Bram Moolenaarae616492020-07-28 20:07:27 +0200149 * When in Vim9 script give an error and return FAIL.
150 */
151 int
152not_in_vim9(exarg_T *eap)
153{
Bram Moolenaar68e30442020-07-28 21:15:07 +0200154 if (in_vim9script())
155 switch (eap->cmdidx)
156 {
Bram Moolenaarada1d872021-02-20 08:16:51 +0100157 case CMD_k:
158 if (eap->addr_count > 0)
159 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000160 emsg(_(e_no_range_allowed));
Bram Moolenaarada1d872021-02-20 08:16:51 +0100161 return FAIL;
162 }
163 // FALLTHROUGH
Bram Moolenaar68e30442020-07-28 21:15:07 +0200164 case CMD_append:
165 case CMD_change:
Bram Moolenaarf5a48012020-08-01 17:00:03 +0200166 case CMD_insert:
Bram Moolenaar65088802021-03-13 21:07:21 +0100167 case CMD_open:
Bram Moolenaarf5a48012020-08-01 17:00:03 +0200168 case CMD_t:
Bram Moolenaar68e30442020-07-28 21:15:07 +0200169 case CMD_xit:
Bram Moolenaar9b8d6222020-12-28 18:26:00 +0100170 semsg(_(e_command_not_supported_in_vim9_script_missing_var_str), eap->cmd);
Bram Moolenaar68e30442020-07-28 21:15:07 +0200171 return FAIL;
172 default: break;
173 }
Bram Moolenaarae616492020-07-28 20:07:27 +0200174 return OK;
175}
176
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100177/*
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100178 * Give an error message if "p" points at "#{" and return TRUE.
179 * This avoids that using a legacy style #{} dictionary leads to difficult to
180 * understand errors.
181 */
182 int
183vim9_bad_comment(char_u *p)
184{
Bram Moolenaara0399ef2021-03-20 15:00:01 +0100185 if (p[0] == '#' && p[1] == '{' && p[2] != '{')
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100186 {
187 emsg(_(e_cannot_use_hash_curly_to_start_comment));
188 return TRUE;
189 }
190 return FALSE;
191}
Dominique Pelle748b3082022-01-08 12:41:16 +0000192#endif
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100193
194/*
Bram Moolenaara0399ef2021-03-20 15:00:01 +0100195 * Return TRUE if "p" points at a "#" not followed by one '{'.
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100196 * Does not check for white space.
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100197 */
198 int
199vim9_comment_start(char_u *p)
200{
Bram Moolenaara0399ef2021-03-20 15:00:01 +0100201 return p[0] == '#' && (p[1] != '{' || p[2] == '{');
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100202}
203
Bram Moolenaar9b8d6222020-12-28 18:26:00 +0100204#if defined(FEAT_EVAL) || defined(PROTO)
205
Bram Moolenaarae616492020-07-28 20:07:27 +0200206/*
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200207 * "++nr" and "--nr" commands.
208 */
209 void
210ex_incdec(exarg_T *eap)
211{
212 char_u *cmd = eap->cmd;
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200213 char_u *nextcmd = eap->nextcmd;
214 size_t len = STRLEN(eap->cmd) + 8;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200215
Bram Moolenaar22480d12021-06-25 21:31:09 +0200216 if (VIM_ISWHITE(cmd[2]))
217 {
218 semsg(_(e_no_white_space_allowed_after_str_str),
219 eap->cmdidx == CMD_increment ? "++" : "--", eap->cmd);
220 return;
221 }
222
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200223 // This works like "nr += 1" or "nr -= 1".
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200224 // Add a '|' to avoid looking in the next line.
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200225 eap->cmd = alloc(len);
226 if (eap->cmd == NULL)
227 return;
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200228 vim_snprintf((char *)eap->cmd, len, "%s %c= 1 |", cmd + 2,
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200229 eap->cmdidx == CMD_increment ? '+' : '-');
230 eap->arg = eap->cmd;
231 eap->cmdidx = CMD_var;
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200232 eap->nextcmd = NULL;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200233 ex_let(eap);
234 vim_free(eap->cmd);
235 eap->cmd = cmd;
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200236 eap->nextcmd = nextcmd;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200237}
238
239/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100240 * ":export let Name: type"
241 * ":export const Name: type"
242 * ":export def Name(..."
243 * ":export class Name ..."
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100244 */
245 void
Bram Moolenaar09689a02020-05-09 22:50:08 +0200246ex_export(exarg_T *eap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100247{
Bram Moolenaar17d36cb2022-01-12 11:46:40 +0000248 int prev_did_emsg = did_emsg;
249
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200250 if (!in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100251 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200252 emsg(_(e_export_can_only_be_used_in_vim9script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100253 return;
254 }
255
256 eap->cmd = eap->arg;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +0100257 (void)find_ex_command(eap, NULL, lookup_scriptitem, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100258 switch (eap->cmdidx)
259 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200260 case CMD_var:
261 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100262 case CMD_const:
263 case CMD_def:
Bram Moolenaar160aa862022-01-10 21:29:57 +0000264 case CMD_function:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100265 // case CMD_class:
266 is_export = TRUE;
267 do_cmdline(eap->cmd, eap->getline, eap->cookie,
268 DOCMD_VERBOSE + DOCMD_NOWAIT);
269
270 // The command will reset "is_export" when exporting an item.
271 if (is_export)
272 {
Bram Moolenaar17d36cb2022-01-12 11:46:40 +0000273 if (did_emsg == prev_did_emsg)
274 emsg(_(e_export_with_invalid_argument));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100275 is_export = FALSE;
276 }
277 break;
278 default:
Bram Moolenaar17d36cb2022-01-12 11:46:40 +0000279 if (did_emsg == prev_did_emsg)
280 emsg(_(e_invalid_command_after_export));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100281 break;
282 }
283}
284
285/*
286 * Add a new imported item entry to the current script.
287 */
288 static imported_T *
289new_imported(garray_T *gap)
290{
291 if (ga_grow(gap, 1) == OK)
292 return ((imported_T *)gap->ga_data + gap->ga_len++);
293 return NULL;
294}
295
296/*
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200297 * Free the script variables from "sn_all_vars".
298 */
299 static void
300free_all_script_vars(scriptitem_T *si)
301{
302 int todo;
303 hashtab_T *ht = &si->sn_all_vars.dv_hashtab;
304 hashitem_T *hi;
305 sallvar_T *sav;
306 sallvar_T *sav_next;
Bram Moolenaardd297bc2021-12-10 10:37:38 +0000307 int idx;
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200308
309 hash_lock(ht);
310 todo = (int)ht->ht_used;
311 for (hi = ht->ht_array; todo > 0; ++hi)
312 {
313 if (!HASHITEM_EMPTY(hi))
314 {
315 --todo;
316
317 // Free the variable. Don't remove it from the hashtab, ht_array
318 // might change then. hash_clear() takes care of it later.
319 sav = HI2SAV(hi);
320 while (sav != NULL)
321 {
322 sav_next = sav->sav_next;
323 if (sav->sav_di == NULL)
324 clear_tv(&sav->sav_tv);
325 vim_free(sav);
326 sav = sav_next;
327 }
328 }
329 }
330 hash_clear(ht);
331 hash_init(ht);
332
Bram Moolenaardd297bc2021-12-10 10:37:38 +0000333 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
334 {
335 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
336
337 if (sv->sv_type_allocated)
338 free_type(sv->sv_type);
339 }
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200340 ga_clear(&si->sn_var_vals);
341
342 // existing commands using script variable indexes are no longer valid
343 si->sn_script_seq = current_sctx.sc_seq;
344}
345
346/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100347 * Free all imported items in script "sid".
348 */
349 void
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200350free_imports_and_script_vars(int sid)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100351{
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100352 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100353 int idx;
354
355 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
356 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100357 imported_T *imp = ((imported_T *)si->sn_imports.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100358
359 vim_free(imp->imp_name);
360 }
361 ga_clear(&si->sn_imports);
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200362
363 free_all_script_vars(si);
364
Bram Moolenaar6110e792020-07-08 19:35:21 +0200365 clear_type_list(&si->sn_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100366}
367
368/*
Bram Moolenaara6294952020-12-27 13:39:50 +0100369 * Mark all imports as possible to redefine. Used when a script is loaded
370 * again but not cleared.
371 */
372 void
373mark_imports_for_reload(int sid)
374{
375 scriptitem_T *si = SCRIPT_ITEM(sid);
376 int idx;
377
378 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
379 {
380 imported_T *imp = ((imported_T *)si->sn_imports.ga_data) + idx;
381
382 imp->imp_flags |= IMP_FLAGS_RELOAD;
383 }
384}
385
386/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100387 * Part of "import" that handles a relative or absolute file name/
388 * Returns OK or FAIL.
389 */
390 static int
391handle_import_fname(char_u *fname, int is_autoload, int *sid)
392{
393 if (is_autoload)
394 {
395 scriptitem_T *si;
396
397 *sid = find_script_by_name(fname);
398 if (*sid < 0)
399 {
400 int error = OK;
401
402 // script does not exist yet, create a new scriptitem
403 *sid = get_new_scriptitem_for_fname(&error, fname);
404 if (error == FAIL)
405 return FAIL;
406 }
407
408 si = SCRIPT_ITEM(*sid);
409 si->sn_import_autoload = TRUE;
410
411 // with testing override: load autoload script right away
412 if (!override_autoload || si->sn_state != SN_STATE_NOT_LOADED)
413 return OK;
414 }
415 return do_source(fname, FALSE, DOSO_NONE, sid);
416}
417
418/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100419 * Handle an ":import" command and add the resulting imported_T to "gap", when
420 * not NULL, or script "import_sid" sn_imports.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200421 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100422 * Returns a pointer to after the command or NULL in case of failure
423 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200424 static char_u *
Bram Moolenaar1c991142020-07-04 13:15:31 +0200425handle_import(
426 char_u *arg_start,
427 garray_T *gap,
428 int import_sid,
429 evalarg_T *evalarg,
430 void *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100431{
432 char_u *arg = arg_start;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000433 char_u *nextarg;
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000434 int is_autoload = FALSE;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000435 int getnext;
436 char_u *expr_end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100437 int ret = FAIL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000438 char_u *as_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100439 typval_T tv;
Bram Moolenaar1836d612022-01-18 13:14:47 +0000440 int sid = -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100441 int res;
Bram Moolenaar921ba522021-07-29 22:25:05 +0200442 long start_lnum = SOURCING_LNUM;
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000443 garray_T *import_gap;
444 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100445
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000446 if (STRNCMP(arg, "autoload", 8) == 0 && VIM_ISWHITE(arg[8]))
447 {
448 is_autoload = TRUE;
449 arg = skipwhite(arg + 8);
450 }
451
Bram Moolenaar4db572e2021-07-18 18:21:38 +0200452 // The name of the file can be an expression, which must evaluate to a
453 // string.
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000454 ret = eval0_retarg(arg, &tv, NULL, evalarg, &expr_end);
Bram Moolenaar4db572e2021-07-18 18:21:38 +0200455 if (ret == FAIL)
456 goto erret;
457 if (tv.v_type != VAR_STRING
458 || tv.vval.v_string == NULL || *tv.vval.v_string == NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100459 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000460 semsg(_(e_invalid_string_for_import_str), arg);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200461 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100462 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100463
Bram Moolenaar921ba522021-07-29 22:25:05 +0200464 // Give error messages for the start of the line.
465 SOURCING_LNUM = start_lnum;
466
Bram Moolenaar1c991142020-07-04 13:15:31 +0200467 /*
468 * find script file
469 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100470 if (*tv.vval.v_string == '.')
471 {
472 size_t len;
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100473 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100474 char_u *tail = gettail(si->sn_name);
475 char_u *from_name;
476
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100477 // Relative to current script: "./name.vim", "../../name.vim".
478 len = STRLEN(si->sn_name) - STRLEN(tail) + STRLEN(tv.vval.v_string) + 2;
479 from_name = alloc((int)len);
480 if (from_name == NULL)
481 goto erret;
482 vim_strncpy(from_name, si->sn_name, tail - si->sn_name);
483 add_pathsep(from_name);
484 STRCAT(from_name, tv.vval.v_string);
485 simplify_filename(from_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100486
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100487 res = handle_import_fname(from_name, is_autoload, &sid);
488 vim_free(from_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100489 }
Bram Moolenaar113b8dc2022-01-18 13:43:58 +0000490 else if (mch_isFullName(tv.vval.v_string)
491#ifdef BACKSLASH_IN_FILENAME
492 // On MS-Windows omitting the drive is still handled like an
493 // absolute path, not using 'runtimepath'.
494 || *tv.vval.v_string == '/' || *tv.vval.v_string == '\\'
495#endif
496 )
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100497 {
498 // Absolute path: "/tmp/name.vim"
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100499 res = handle_import_fname(tv.vval.v_string, is_autoload, &sid);
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000500 }
501 else if (is_autoload)
502 {
503 size_t len = 9 + STRLEN(tv.vval.v_string) + 1;
504 char_u *from_name;
505
506 // Find file in "autoload" subdirs in 'runtimepath'.
507 from_name = alloc((int)len);
508 if (from_name == NULL)
509 goto erret;
510 vim_snprintf((char *)from_name, len, "autoload/%s", tv.vval.v_string);
511 // we need a scriptitem without loading the script
512 sid = find_script_in_rtp(from_name);
513 vim_free(from_name);
Bram Moolenaard041f422022-01-12 19:54:00 +0000514 if (SCRIPT_ID_VALID(sid))
515 {
516 scriptitem_T *si = SCRIPT_ITEM(sid);
517
518 if (si->sn_autoload_prefix == NULL)
519 si->sn_autoload_prefix = get_autoload_prefix(si);
520 res = OK;
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +0000521 if (override_autoload && si->sn_state == SN_STATE_NOT_LOADED)
522 // testing override: load autoload script right away
523 (void)do_source(si->sn_name, FALSE, DOSO_NONE, NULL);
Bram Moolenaard041f422022-01-12 19:54:00 +0000524 }
525 else
526 res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100527 }
528 else
529 {
530 size_t len = 7 + STRLEN(tv.vval.v_string) + 1;
531 char_u *from_name;
532
533 // Find file in "import" subdirs in 'runtimepath'.
534 from_name = alloc((int)len);
535 if (from_name == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200536 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100537 vim_snprintf((char *)from_name, len, "import/%s", tv.vval.v_string);
538 res = source_in_path(p_rtp, from_name, DIP_NOAFTER, &sid);
539 vim_free(from_name);
540 }
541
542 if (res == FAIL || sid <= 0)
543 {
Bram Moolenaar1836d612022-01-18 13:14:47 +0000544 semsg(_(is_autoload && sid == -2
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000545 ? e_autoload_import_cannot_use_absolute_or_relative_path
546 : e_could_not_import_str), tv.vval.v_string);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200547 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100548 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100549
Bram Moolenaar779aeff2022-02-08 19:12:19 +0000550 if (sid == current_sctx.sc_sid)
551 {
552 emsg(_(e_script_cannot_import_itself));
553 goto erret;
554 }
555
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000556 import_gap = gap != NULL ? gap : &SCRIPT_ITEM(import_sid)->sn_imports;
557 for (i = 0; i < import_gap->ga_len; ++i)
558 {
559 imported_T *import = (imported_T *)import_gap->ga_data + i;
560
561 if (import->imp_sid == sid)
562 {
563 if (import->imp_flags & IMP_FLAGS_RELOAD)
564 {
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000565 // encountering same script first time on a reload is OK
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000566 import->imp_flags &= ~IMP_FLAGS_RELOAD;
567 break;
568 }
569 semsg(_(e_cannot_import_same_script_twice_str), tv.vval.v_string);
570 goto erret;
571 }
572 }
573
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000574 // Allow for the "as Name" to be in the next line.
575 nextarg = eval_next_non_blank(expr_end, evalarg, &getnext);
576 if (STRNCMP("as", nextarg, 2) == 0 && IS_WHITE_OR_NUL(nextarg[2]))
577 {
578 char_u *p;
579
580 if (getnext)
581 arg = eval_next_line(evalarg);
582 else
583 arg = nextarg;
584
585 // Skip over "as Name "; no line break allowed after "as".
586 // Do not allow for ':' and '#'.
587 arg = skipwhite(arg + 2);
588 p = arg;
589 if (eval_isnamec1(*arg))
590 while (ASCII_ISALNUM(*arg) || *arg == '_')
591 ++arg;
592 if (p == arg || !IS_WHITE_OR_NUL(*arg))
593 {
594 semsg(_(e_syntax_error_in_import_str), p);
595 goto erret;
596 }
597 as_name = vim_strnsave(p, arg - p);
598 arg = skipwhite(arg);
599 }
600 else
601 {
602 char_u *p = gettail(tv.vval.v_string);
603 char_u *end = (char_u *)strstr((char *)p, ".vim");
604
605 if (!ends_excmd2(arg_start, expr_end))
606 {
607 semsg(_(e_trailing_characters_str), expr_end);
608 goto erret;
609 }
Bram Moolenaar834d4182022-01-07 13:38:24 +0000610 if (end == NULL || end[4] != NUL)
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000611 {
Bram Moolenaar834d4182022-01-07 13:38:24 +0000612 semsg(_(e_imported_script_must_use_as_or_end_in_dot_vim_str), p);
613 goto erret;
614 }
615 if (end == p)
616 {
617 semsg(_(e_cannot_import_dot_vim_without_using_as), p);
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000618 goto erret;
619 }
620 as_name = vim_strnsave(p, end - p);
621 }
622
623 if (as_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100624 {
Bram Moolenaar0a842842021-02-27 22:41:19 +0100625 imported_T *imported;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100626
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000627 imported = find_imported(as_name, STRLEN(as_name), FALSE);
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000628 if (imported != NULL && imported->imp_sid != sid)
Bram Moolenaara6294952020-12-27 13:39:50 +0100629 {
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000630 semsg(_(e_name_already_defined_str), as_name);
631 goto erret;
Bram Moolenaara6294952020-12-27 13:39:50 +0100632 }
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000633 else if (imported == NULL
Bram Moolenaardce24412022-02-08 20:35:30 +0000634 && check_defined(as_name, STRLEN(as_name), cctx, NULL,
635 FALSE) == FAIL)
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000636 goto erret;
Bram Moolenaara6294952020-12-27 13:39:50 +0100637
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100638 if (imported == NULL)
Bram Moolenaar17d36cb2022-01-12 11:46:40 +0000639 {
640 imported = new_imported(import_gap);
641 if (imported == NULL)
642 goto erret;
643 imported->imp_name = as_name;
644 as_name = NULL;
645 imported->imp_sid = sid;
646 if (is_autoload)
647 imported->imp_flags = IMP_FLAGS_AUTOLOAD;
648 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100649 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200650
Bram Moolenaar1c991142020-07-04 13:15:31 +0200651erret:
Bram Moolenaar4db572e2021-07-18 18:21:38 +0200652 clear_tv(&tv);
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000653 vim_free(as_name);
654 return arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100655}
656
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200657/*
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000658 * ":import 'filename'"
659 * ":import 'filename' as Name"
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200660 */
661 void
662ex_import(exarg_T *eap)
663{
664 char_u *cmd_end;
665 evalarg_T evalarg;
666
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +0000667 if (!sourcing_a_script(eap))
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200668 {
669 emsg(_(e_import_can_only_be_used_in_script));
670 return;
671 }
672 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
673
674 cmd_end = handle_import(eap->arg, NULL, current_sctx.sc_sid,
675 &evalarg, NULL);
676 if (cmd_end != NULL)
677 set_nextcmd(eap, cmd_end);
678 clear_evalarg(&evalarg, eap);
679}
680
681/*
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000682 * Find an exported item in "sid" matching "name".
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000683 * Either "cctx" or "cstack" is NULL.
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200684 * When it is a variable return the index.
685 * When it is a user function return "*ufunc".
686 * When not found returns -1 and "*ufunc" is NULL.
687 */
688 int
689find_exported(
690 int sid,
691 char_u *name,
692 ufunc_T **ufunc,
693 type_T **type,
694 cctx_T *cctx,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000695 cstack_T *cstack,
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200696 int verbose)
697{
698 int idx = -1;
699 svar_T *sv;
700 scriptitem_T *script = SCRIPT_ITEM(sid);
701
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100702 if (script->sn_import_autoload && script->sn_state == SN_STATE_NOT_LOADED)
703 {
704 if (do_source(script->sn_name, FALSE, DOSO_NONE, NULL) == FAIL)
705 return -1;
706 }
707
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200708 // Find name in "script".
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000709 idx = get_script_item_idx(sid, name, 0, cctx, cstack);
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200710 if (idx >= 0)
711 {
712 sv = ((svar_T *)script->sn_var_vals.ga_data) + idx;
Bram Moolenaara909c482022-01-06 22:07:57 +0000713 *ufunc = NULL;
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200714 if (!sv->sv_export)
715 {
716 if (verbose)
717 semsg(_(e_item_not_exported_in_script_str), name);
718 return -1;
719 }
720 *type = sv->sv_type;
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200721 }
722 else
723 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +0000724 size_t len = STRLEN(name);
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200725 char_u buffer[200];
726 char_u *funcname;
727
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +0000728 // It could be a user function. Normally this is stored as
729 // "<SNR>99_name". For an autoload script a function is stored with
730 // the autoload prefix: "dir#script#name".
731 if (script->sn_autoload_prefix != NULL)
732 len += STRLEN(script->sn_autoload_prefix) + 2;
733 else
734 len += 15;
735
736 if (len < sizeof(buffer))
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200737 funcname = buffer;
738 else
739 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +0000740 funcname = alloc(len);
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200741 if (funcname == NULL)
742 return -1;
743 }
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +0000744 if (script->sn_autoload_prefix != NULL)
745 {
746 sprintf((char *)funcname, "%s%s", script->sn_autoload_prefix, name);
747 }
748 else
749 {
750 funcname[0] = K_SPECIAL;
751 funcname[1] = KS_EXTRA;
752 funcname[2] = (int)KE_SNR;
753 sprintf((char *)funcname + 3, "%ld_%s", (long)sid, name);
754 }
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000755 *ufunc = find_func(funcname, FALSE);
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200756
757 if (*ufunc == NULL)
758 {
759 if (verbose)
Bram Moolenaard02dce22022-01-18 17:43:04 +0000760 {
761 ufunc_T *alt_ufunc = NULL;
762
763 if (script->sn_autoload_prefix != NULL)
764 {
765 // try find the function by the script-local name
766 funcname[0] = K_SPECIAL;
767 funcname[1] = KS_EXTRA;
768 funcname[2] = (int)KE_SNR;
769 sprintf((char *)funcname + 3, "%ld_%s", (long)sid, name);
770 alt_ufunc = find_func(funcname, FALSE);
771 }
772 if (alt_ufunc != NULL)
773 semsg(_(e_item_not_exported_in_script_str), name);
774 else
775 semsg(_(e_item_not_found_in_script_str), name);
776 }
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200777 }
778 else if (((*ufunc)->uf_flags & FC_EXPORT) == 0)
779 {
780 if (verbose)
781 semsg(_(e_item_not_exported_in_script_str), name);
782 *ufunc = NULL;
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200783 }
Bram Moolenaard02dce22022-01-18 17:43:04 +0000784 if (funcname != buffer)
785 vim_free(funcname);
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200786 }
787
788 return idx;
789}
790
791/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200792 * Declare a script-local variable without init: "let var: type".
793 * "const" is an error since the value is missing.
794 * Returns a pointer to after the type.
795 */
796 char_u *
797vim9_declare_scriptvar(exarg_T *eap, char_u *arg)
798{
799 char_u *p;
800 char_u *name;
801 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
802 type_T *type;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200803 typval_T init_tv;
804
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200805 if (eap->cmdidx == CMD_final || eap->cmdidx == CMD_const)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200806 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200807 if (eap->cmdidx == CMD_final)
808 emsg(_(e_final_requires_a_value));
809 else
810 emsg(_(e_const_requires_a_value));
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200811 return arg + STRLEN(arg);
812 }
813
814 // Check for valid starting character.
815 if (!eval_isnamec1(*arg))
816 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000817 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200818 return arg + STRLEN(arg);
819 }
820
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200821 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
Bram Moolenaar3b74b6b2020-06-19 19:01:43 +0200822 if (*p == ':' && (VIM_ISWHITE(p[1]) || p != arg + 1))
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200823 break;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200824
825 if (*p != ':')
826 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +0200827 emsg(_(e_type_or_initialization_required));
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200828 return arg + STRLEN(arg);
829 }
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200830 if (!VIM_ISWHITE(p[1]))
831 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100832 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200833 return arg + STRLEN(arg);
834 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200835 name = vim_strnsave(arg, p - arg);
836
Bram Moolenaard0edaf92021-05-28 21:06:08 +0200837 // parse type, check for reserved name
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200838 p = skipwhite(p + 1);
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100839 type = parse_type(&p, &si->sn_type_list, TRUE);
Bram Moolenaard0edaf92021-05-28 21:06:08 +0200840 if (type == NULL || check_reserved_name(name) == FAIL)
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200841 {
842 vim_free(name);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200843 return p;
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200844 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200845
846 // Create the variable with 0/NULL value.
847 CLEAR_FIELD(init_tv);
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +0200848 if (type->tt_type == VAR_ANY)
849 // A variable of type "any" is not possible, just use zero instead
850 init_tv.v_type = VAR_NUMBER;
851 else
852 init_tv.v_type = type->tt_type;
Bram Moolenaar859cc212022-03-28 15:22:35 +0100853 set_var_const(name, 0, type, &init_tv, FALSE, ASSIGN_INIT, 0);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200854
855 vim_free(name);
856 return p;
857}
858
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200859/*
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200860 * Vim9 part of adding a script variable: add it to sn_all_vars (lookup by name
861 * with a hashtable) and sn_var_vals (lookup by index).
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100862 * When "create" is TRUE this is a new variable, otherwise find and update an
863 * existing variable.
Bram Moolenaar08251752021-01-11 21:20:18 +0100864 * "flags" can have ASSIGN_FINAL or ASSIGN_CONST.
Bram Moolenaarf2253962021-04-13 20:53:13 +0200865 * When "*type" is NULL use "tv" for the type and update "*type". If
866 * "do_member" is TRUE also use the member type, otherwise use "any".
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200867 */
868 void
Bram Moolenaar08251752021-01-11 21:20:18 +0100869update_vim9_script_var(
870 int create,
871 dictitem_T *di,
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +0000872 char_u *name,
Bram Moolenaar08251752021-01-11 21:20:18 +0100873 int flags,
874 typval_T *tv,
Bram Moolenaarf2253962021-04-13 20:53:13 +0200875 type_T **type,
876 int do_member)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200877{
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100878 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
879 hashitem_T *hi;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200880 svar_T *sv = NULL;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200881
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100882 if (create)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200883 {
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200884 sallvar_T *newsav;
885 sallvar_T *sav = NULL;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200886
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100887 // Store a pointer to the typval_T, so that it can be found by index
888 // instead of using a hastab lookup.
889 if (ga_grow(&si->sn_var_vals, 1) == FAIL)
890 return;
891
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +0000892 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200893 if (!HASHITEM_EMPTY(hi))
894 {
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200895 // Variable with this name exists, either in this block or in
896 // another block.
897 for (sav = HI2SAV(hi); ; sav = sav->sav_next)
898 {
899 if (sav->sav_block_id == si->sn_current_block_id)
900 {
901 // variable defined in a loop, re-use the entry
902 sv = ((svar_T *)si->sn_var_vals.ga_data)
903 + sav->sav_var_vals_idx;
904 // unhide the variable
905 if (sv->sv_tv == &sav->sav_tv)
906 {
907 clear_tv(&sav->sav_tv);
908 sv->sv_tv = &di->di_tv;
909 sav->sav_di = di;
910 }
911 break;
912 }
913 if (sav->sav_next == NULL)
914 break;
915 }
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200916 }
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200917
918 if (sv == NULL)
919 {
920 // Variable not defined or not defined in current block: Add a
921 // svar_T and create a new sallvar_T.
922 sv = ((svar_T *)si->sn_var_vals.ga_data) + si->sn_var_vals.ga_len;
923 newsav = (sallvar_T *)alloc_clear(
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +0000924 sizeof(sallvar_T) + STRLEN(name));
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200925 if (newsav == NULL)
926 return;
927
928 sv->sv_tv = &di->di_tv;
929 sv->sv_const = (flags & ASSIGN_FINAL) ? ASSIGN_FINAL
930 : (flags & ASSIGN_CONST) ? ASSIGN_CONST : 0;
931 sv->sv_export = is_export;
932 newsav->sav_var_vals_idx = si->sn_var_vals.ga_len;
933 ++si->sn_var_vals.ga_len;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +0000934 STRCPY(&newsav->sav_key, name);
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200935 sv->sv_name = newsav->sav_key;
936 newsav->sav_di = di;
937 newsav->sav_block_id = si->sn_current_block_id;
938
939 if (HASHITEM_EMPTY(hi))
940 // new variable name
941 hash_add(&si->sn_all_vars.dv_hashtab, newsav->sav_key);
942 else if (sav != NULL)
943 // existing name in a new block, append to the list
944 sav->sav_next = newsav;
945 }
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200946 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100947 else
948 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000949 sv = find_typval_in_script(&di->di_tv, 0);
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100950 }
951 if (sv != NULL)
952 {
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100953 if (*type == NULL)
Bram Moolenaar114dbda2022-01-03 12:28:03 +0000954 *type = typval2type(tv, get_copyID(), &si->sn_type_list,
955 do_member ? TVTT_DO_MEMBER : 0);
Bram Moolenaar859cc212022-03-28 15:22:35 +0100956 else if ((flags & ASSIGN_INIT) == 0
957 && (*type)->tt_type == VAR_BLOB && tv->v_type == VAR_BLOB
958 && tv->vval.v_blob == NULL)
959 {
960 // "var b: blob = null_blob" has a different type.
961 *type = &t_blob_null;
962 }
Bram Moolenaardd297bc2021-12-10 10:37:38 +0000963 if (sv->sv_type_allocated)
964 free_type(sv->sv_type);
965 if (*type != NULL && ((*type)->tt_type == VAR_FUNC
966 || (*type)->tt_type == VAR_PARTIAL))
967 {
968 // The type probably uses uf_type_list, which is cleared when the
969 // function is freed, but the script variable may keep the type.
970 // Make a copy to avoid using freed memory.
971 sv->sv_type = alloc_type(*type);
972 sv->sv_type_allocated = TRUE;
973 }
974 else
975 {
976 sv->sv_type = *type;
977 sv->sv_type_allocated = FALSE;
978 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100979 }
980
981 // let ex_export() know the export worked.
982 is_export = FALSE;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200983}
984
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200985/*
986 * Hide a script variable when leaving a block.
987 * "idx" is de index in sn_var_vals.
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200988 * When "func_defined" is non-zero then a function was defined in this block,
989 * the variable may be accessed by it. Otherwise the variable can be cleared.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200990 */
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200991 void
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200992hide_script_var(scriptitem_T *si, int idx, int func_defined)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200993{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200994 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200995 hashtab_T *script_ht = get_script_local_ht();
996 hashtab_T *all_ht = &si->sn_all_vars.dv_hashtab;
997 hashitem_T *script_hi;
998 hashitem_T *all_hi;
999
1000 // Remove a variable declared inside the block, if it still exists.
Bram Moolenaar39ca4122020-10-20 14:25:07 +02001001 // If it was added in a nested block it will already have been removed.
Bram Moolenaar8d739de2020-10-14 19:39:19 +02001002 // The typval is moved into the sallvar_T.
1003 script_hi = hash_find(script_ht, sv->sv_name);
1004 all_hi = hash_find(all_ht, sv->sv_name);
1005 if (!HASHITEM_EMPTY(script_hi) && !HASHITEM_EMPTY(all_hi))
1006 {
1007 dictitem_T *di = HI2DI(script_hi);
1008 sallvar_T *sav = HI2SAV(all_hi);
Bram Moolenaar39ca4122020-10-20 14:25:07 +02001009 sallvar_T *sav_prev = NULL;
Bram Moolenaar8d739de2020-10-14 19:39:19 +02001010
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001011 // There can be multiple entries with the same name in different
1012 // blocks, find the right one.
1013 while (sav != NULL && sav->sav_var_vals_idx != idx)
Bram Moolenaar39ca4122020-10-20 14:25:07 +02001014 {
1015 sav_prev = sav;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001016 sav = sav->sav_next;
Bram Moolenaar39ca4122020-10-20 14:25:07 +02001017 }
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001018 if (sav != NULL)
1019 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02001020 if (func_defined)
1021 {
1022 // move the typval from the dictitem to the sallvar
1023 sav->sav_tv = di->di_tv;
1024 di->di_tv.v_type = VAR_UNKNOWN;
1025 sav->sav_flags = di->di_flags;
1026 sav->sav_di = NULL;
1027 sv->sv_tv = &sav->sav_tv;
1028 }
1029 else
1030 {
1031 if (sav_prev == NULL)
1032 hash_remove(all_ht, all_hi);
1033 else
1034 sav_prev->sav_next = sav->sav_next;
1035 sv->sv_name = NULL;
1036 vim_free(sav);
1037 }
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001038 delete_var(script_ht, script_hi);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001039 }
Bram Moolenaar8d739de2020-10-14 19:39:19 +02001040 }
1041}
1042
1043/*
Bram Moolenaar10c65862020-10-08 21:16:42 +02001044 * Find the script-local variable that links to "dest".
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001045 * If "sid" is zero use the current script.
Bram Moolenaarc967d572021-07-08 21:38:50 +02001046 * Returns NULL if not found and give an internal error.
Bram Moolenaar34db91f2020-06-13 19:00:10 +02001047 */
Bram Moolenaar10c65862020-10-08 21:16:42 +02001048 svar_T *
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001049find_typval_in_script(typval_T *dest, scid_T sid)
Bram Moolenaar34db91f2020-06-13 19:00:10 +02001050{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001051 scriptitem_T *si = SCRIPT_ITEM(sid == 0 ? current_sctx.sc_sid : sid);
Bram Moolenaar34db91f2020-06-13 19:00:10 +02001052 int idx;
1053
Bram Moolenaar81e17fb2020-08-21 21:55:43 +02001054 if (si->sn_version != SCRIPT_VERSION_VIM9)
1055 // legacy script doesn't store variable types
Bram Moolenaar10c65862020-10-08 21:16:42 +02001056 return NULL;
Bram Moolenaar81e17fb2020-08-21 21:55:43 +02001057
Bram Moolenaara06758d2021-10-15 00:18:37 +01001058 // Find the svar_T in sn_var_vals. Start at the end, in a for loop the
1059 // variable was added at the end.
1060 for (idx = si->sn_var_vals.ga_len - 1; idx >= 0; --idx)
Bram Moolenaar34db91f2020-06-13 19:00:10 +02001061 {
1062 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1063
Bram Moolenaarc0913d02020-12-05 14:44:37 +01001064 // If "sv_name" is NULL the variable was hidden when leaving a block,
1065 // don't check "sv_tv" then, it might be used for another variable now.
1066 if (sv->sv_name != NULL && sv->sv_tv == dest)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001067 return sv;
Bram Moolenaar34db91f2020-06-13 19:00:10 +02001068 }
Bram Moolenaarc967d572021-07-08 21:38:50 +02001069 iemsg("find_typval_in_script(): not found");
Bram Moolenaar10c65862020-10-08 21:16:42 +02001070 return NULL;
1071}
1072
1073/*
1074 * Check if the type of script variable "dest" allows assigning "value".
1075 * If needed convert "value" to a bool.
1076 */
1077 int
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001078check_script_var_type(
Bram Moolenaar7824fc82021-11-26 17:36:51 +00001079 svar_T *sv,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001080 typval_T *value,
1081 char_u *name,
1082 where_T where)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001083{
Bram Moolenaar10c65862020-10-08 21:16:42 +02001084 int ret;
1085
Bram Moolenaar7824fc82021-11-26 17:36:51 +00001086 if (sv->sv_const != 0)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001087 {
Bram Moolenaar7824fc82021-11-26 17:36:51 +00001088 semsg(_(e_cannot_change_readonly_variable_str), name);
1089 return FAIL;
Bram Moolenaar10c65862020-10-08 21:16:42 +02001090 }
Bram Moolenaar7824fc82021-11-26 17:36:51 +00001091 ret = check_typval_type(sv->sv_type, value, where);
1092 if (ret == OK && need_convert_to_bool(sv->sv_type, value))
1093 {
1094 int val = tv2bool(value);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001095
Bram Moolenaar7824fc82021-11-26 17:36:51 +00001096 clear_tv(value);
1097 value->v_type = VAR_BOOL;
1098 value->v_lock = 0;
1099 value->vval.v_number = val ? VVAL_TRUE : VVAL_FALSE;
1100 }
1101 return ret;
Bram Moolenaar34db91f2020-06-13 19:00:10 +02001102}
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001103
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001104// words that cannot be used as a variable
1105static char *reserved[] = {
1106 "true",
1107 "false",
1108 "null",
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00001109 "null_blob",
1110 "null_dict",
1111 "null_function",
1112 "null_list",
1113 "null_partial",
1114 "null_string",
1115 "null_channel",
1116 "null_job",
Bram Moolenaar74235772021-06-12 14:53:05 +02001117 "this",
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001118 NULL
1119};
1120
1121 int
1122check_reserved_name(char_u *name)
1123{
1124 int idx;
1125
1126 for (idx = 0; reserved[idx] != NULL; ++idx)
1127 if (STRCMP(reserved[idx], name) == 0)
1128 {
1129 semsg(_(e_cannot_use_reserved_name), name);
1130 return FAIL;
1131 }
1132 return OK;
1133}
1134
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001135#endif // FEAT_EVAL