blob: f6c8c49a2891a21af991a42e1ebb00b623e49860 [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 Moolenaar31e5c602022-04-15 13:53:33 +0100139 set_option_value_give_err((char_u *)"cpo",
140 0L, (char_u *)CPO_VIM, OPT_NO_REDRAW);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100141 }
Bram Moolenaar9b8d6222020-12-28 18:26:00 +0100142#else
Yee Cheng Chin07eaa1e2022-10-07 16:00:04 +0100143 // No check for this being the first command, the information is not
144 // available.
Bram Moolenaar9b8d6222020-12-28 18:26:00 +0100145 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
146#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100147}
148
Dominique Pelle748b3082022-01-08 12:41:16 +0000149#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100150/*
Bram Moolenaarae616492020-07-28 20:07:27 +0200151 * When in Vim9 script give an error and return FAIL.
152 */
153 int
154not_in_vim9(exarg_T *eap)
155{
Bram Moolenaar68e30442020-07-28 21:15:07 +0200156 if (in_vim9script())
157 switch (eap->cmdidx)
158 {
Bram Moolenaarada1d872021-02-20 08:16:51 +0100159 case CMD_k:
160 if (eap->addr_count > 0)
161 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000162 emsg(_(e_no_range_allowed));
Bram Moolenaarada1d872021-02-20 08:16:51 +0100163 return FAIL;
164 }
165 // FALLTHROUGH
Bram Moolenaar68e30442020-07-28 21:15:07 +0200166 case CMD_append:
167 case CMD_change:
Bram Moolenaarf5a48012020-08-01 17:00:03 +0200168 case CMD_insert:
Bram Moolenaar65088802021-03-13 21:07:21 +0100169 case CMD_open:
Bram Moolenaarf5a48012020-08-01 17:00:03 +0200170 case CMD_t:
Bram Moolenaar68e30442020-07-28 21:15:07 +0200171 case CMD_xit:
Bram Moolenaar9b8d6222020-12-28 18:26:00 +0100172 semsg(_(e_command_not_supported_in_vim9_script_missing_var_str), eap->cmd);
Bram Moolenaar68e30442020-07-28 21:15:07 +0200173 return FAIL;
174 default: break;
175 }
Bram Moolenaarae616492020-07-28 20:07:27 +0200176 return OK;
177}
178
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100179/*
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +0100180 * Return TRUE if "p" points at "#{", not "#{{".
181 * Give an error message if not done already.
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100182 * This avoids that using a legacy style #{} dictionary leads to difficult to
183 * understand errors.
184 */
185 int
186vim9_bad_comment(char_u *p)
187{
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +0100188 if (p[0] == '#' && p[1] == '{' && p[2] != '{')
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100189 {
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +0100190 if (!did_emsg)
191 emsg(_(e_cannot_use_hash_curly_to_start_comment));
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100192 return TRUE;
193 }
194 return FALSE;
195}
Dominique Pelle748b3082022-01-08 12:41:16 +0000196#endif
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100197
198/*
Bram Moolenaara0399ef2021-03-20 15:00:01 +0100199 * Return TRUE if "p" points at a "#" not followed by one '{'.
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +0100200 * Gives an error for using "#{", not for "#{{".
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100201 * Does not check for white space.
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100202 */
203 int
204vim9_comment_start(char_u *p)
205{
Bram Moolenaarda70cf32022-08-06 22:13:03 +0100206#ifdef FEAT_EVAL
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +0100207 return p[0] == '#' && !vim9_bad_comment(p);
Bram Moolenaarda70cf32022-08-06 22:13:03 +0100208#else
209 return p[0] == '#' && (p[1] != '{' || p[2] == '{');
210#endif
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100211}
212
Bram Moolenaar9b8d6222020-12-28 18:26:00 +0100213#if defined(FEAT_EVAL) || defined(PROTO)
214
Bram Moolenaarae616492020-07-28 20:07:27 +0200215/*
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200216 * "++nr" and "--nr" commands.
217 */
218 void
219ex_incdec(exarg_T *eap)
220{
221 char_u *cmd = eap->cmd;
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200222 char_u *nextcmd = eap->nextcmd;
223 size_t len = STRLEN(eap->cmd) + 8;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200224
Bram Moolenaar22480d12021-06-25 21:31:09 +0200225 if (VIM_ISWHITE(cmd[2]))
226 {
227 semsg(_(e_no_white_space_allowed_after_str_str),
228 eap->cmdidx == CMD_increment ? "++" : "--", eap->cmd);
229 return;
230 }
231
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200232 // This works like "nr += 1" or "nr -= 1".
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200233 // Add a '|' to avoid looking in the next line.
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200234 eap->cmd = alloc(len);
235 if (eap->cmd == NULL)
236 return;
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200237 vim_snprintf((char *)eap->cmd, len, "%s %c= 1 |", cmd + 2,
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200238 eap->cmdidx == CMD_increment ? '+' : '-');
239 eap->arg = eap->cmd;
240 eap->cmdidx = CMD_var;
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200241 eap->nextcmd = NULL;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200242 ex_let(eap);
243 vim_free(eap->cmd);
244 eap->cmd = cmd;
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200245 eap->nextcmd = nextcmd;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200246}
247
248/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100249 * ":export let Name: type"
250 * ":export const Name: type"
251 * ":export def Name(..."
252 * ":export class Name ..."
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100253 */
254 void
Bram Moolenaar09689a02020-05-09 22:50:08 +0200255ex_export(exarg_T *eap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100256{
Bram Moolenaar17d36cb2022-01-12 11:46:40 +0000257 int prev_did_emsg = did_emsg;
258
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200259 if (!in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100260 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200261 emsg(_(e_export_can_only_be_used_in_vim9script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100262 return;
263 }
264
265 eap->cmd = eap->arg;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +0100266 (void)find_ex_command(eap, NULL, lookup_scriptitem, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100267 switch (eap->cmdidx)
268 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200269 case CMD_var:
270 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100271 case CMD_const:
272 case CMD_def:
Bram Moolenaar160aa862022-01-10 21:29:57 +0000273 case CMD_function:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100274 // case CMD_class:
275 is_export = TRUE;
276 do_cmdline(eap->cmd, eap->getline, eap->cookie,
277 DOCMD_VERBOSE + DOCMD_NOWAIT);
278
279 // The command will reset "is_export" when exporting an item.
280 if (is_export)
281 {
Bram Moolenaar17d36cb2022-01-12 11:46:40 +0000282 if (did_emsg == prev_did_emsg)
283 emsg(_(e_export_with_invalid_argument));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100284 is_export = FALSE;
285 }
286 break;
287 default:
Bram Moolenaar17d36cb2022-01-12 11:46:40 +0000288 if (did_emsg == prev_did_emsg)
289 emsg(_(e_invalid_command_after_export));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100290 break;
291 }
292}
293
294/*
295 * Add a new imported item entry to the current script.
296 */
297 static imported_T *
298new_imported(garray_T *gap)
299{
300 if (ga_grow(gap, 1) == OK)
301 return ((imported_T *)gap->ga_data + gap->ga_len++);
302 return NULL;
303}
304
305/*
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200306 * Free the script variables from "sn_all_vars".
307 */
308 static void
309free_all_script_vars(scriptitem_T *si)
310{
311 int todo;
312 hashtab_T *ht = &si->sn_all_vars.dv_hashtab;
313 hashitem_T *hi;
314 sallvar_T *sav;
315 sallvar_T *sav_next;
Bram Moolenaardd297bc2021-12-10 10:37:38 +0000316 int idx;
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200317
318 hash_lock(ht);
319 todo = (int)ht->ht_used;
320 for (hi = ht->ht_array; todo > 0; ++hi)
321 {
322 if (!HASHITEM_EMPTY(hi))
323 {
324 --todo;
325
326 // Free the variable. Don't remove it from the hashtab, ht_array
327 // might change then. hash_clear() takes care of it later.
328 sav = HI2SAV(hi);
329 while (sav != NULL)
330 {
331 sav_next = sav->sav_next;
332 if (sav->sav_di == NULL)
333 clear_tv(&sav->sav_tv);
334 vim_free(sav);
335 sav = sav_next;
336 }
337 }
338 }
339 hash_clear(ht);
340 hash_init(ht);
341
Bram Moolenaardd297bc2021-12-10 10:37:38 +0000342 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
343 {
344 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
345
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +0100346 if (sv->sv_flags & SVFLAG_TYPE_ALLOCATED)
Bram Moolenaardd297bc2021-12-10 10:37:38 +0000347 free_type(sv->sv_type);
348 }
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200349 ga_clear(&si->sn_var_vals);
350
351 // existing commands using script variable indexes are no longer valid
352 si->sn_script_seq = current_sctx.sc_seq;
353}
354
355/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100356 * Free all imported items in script "sid".
357 */
358 void
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200359free_imports_and_script_vars(int sid)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100360{
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100361 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100362 int idx;
363
364 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
365 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100366 imported_T *imp = ((imported_T *)si->sn_imports.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100367
368 vim_free(imp->imp_name);
369 }
370 ga_clear(&si->sn_imports);
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200371
372 free_all_script_vars(si);
373
Bram Moolenaar6110e792020-07-08 19:35:21 +0200374 clear_type_list(&si->sn_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100375}
376
377/*
Bram Moolenaara6294952020-12-27 13:39:50 +0100378 * Mark all imports as possible to redefine. Used when a script is loaded
379 * again but not cleared.
380 */
381 void
382mark_imports_for_reload(int sid)
383{
384 scriptitem_T *si = SCRIPT_ITEM(sid);
385 int idx;
386
387 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
388 {
389 imported_T *imp = ((imported_T *)si->sn_imports.ga_data) + idx;
390
391 imp->imp_flags |= IMP_FLAGS_RELOAD;
392 }
393}
394
395/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100396 * Part of "import" that handles a relative or absolute file name/
397 * Returns OK or FAIL.
398 */
399 static int
400handle_import_fname(char_u *fname, int is_autoload, int *sid)
401{
402 if (is_autoload)
403 {
404 scriptitem_T *si;
405
406 *sid = find_script_by_name(fname);
407 if (*sid < 0)
408 {
409 int error = OK;
410
Bram Moolenaar49d008d2022-03-31 11:51:21 +0100411 // Script does not exist yet, check name and create a new
412 // scriptitem.
413 if (!file_is_readable(fname))
414 {
415 semsg(_(mch_isdir(fname) ? e_str_is_directory
416 : e_cannot_read_from_str_2), fname);
417 return FAIL;
418 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100419 *sid = get_new_scriptitem_for_fname(&error, fname);
420 if (error == FAIL)
421 return FAIL;
422 }
423
424 si = SCRIPT_ITEM(*sid);
425 si->sn_import_autoload = TRUE;
426
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100427 if (si->sn_autoload_prefix == NULL)
428 si->sn_autoload_prefix = get_autoload_prefix(si);
429
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100430 // with testing override: load autoload script right away
431 if (!override_autoload || si->sn_state != SN_STATE_NOT_LOADED)
432 return OK;
433 }
434 return do_source(fname, FALSE, DOSO_NONE, sid);
435}
436
437/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100438 * Handle an ":import" command and add the resulting imported_T to "gap", when
439 * not NULL, or script "import_sid" sn_imports.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200440 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100441 * Returns a pointer to after the command or NULL in case of failure
442 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200443 static char_u *
Bram Moolenaar1c991142020-07-04 13:15:31 +0200444handle_import(
445 char_u *arg_start,
446 garray_T *gap,
447 int import_sid,
448 evalarg_T *evalarg,
449 void *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100450{
451 char_u *arg = arg_start;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000452 char_u *nextarg;
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000453 int is_autoload = FALSE;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000454 int getnext;
455 char_u *expr_end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100456 int ret = FAIL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000457 char_u *as_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100458 typval_T tv;
Bram Moolenaar1836d612022-01-18 13:14:47 +0000459 int sid = -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100460 int res;
Bram Moolenaar921ba522021-07-29 22:25:05 +0200461 long start_lnum = SOURCING_LNUM;
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000462 garray_T *import_gap;
463 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100464
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000465 if (STRNCMP(arg, "autoload", 8) == 0 && VIM_ISWHITE(arg[8]))
466 {
467 is_autoload = TRUE;
468 arg = skipwhite(arg + 8);
469 }
470
Bram Moolenaar4db572e2021-07-18 18:21:38 +0200471 // The name of the file can be an expression, which must evaluate to a
472 // string.
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000473 ret = eval0_retarg(arg, &tv, NULL, evalarg, &expr_end);
Bram Moolenaar4db572e2021-07-18 18:21:38 +0200474 if (ret == FAIL)
475 goto erret;
476 if (tv.v_type != VAR_STRING
477 || tv.vval.v_string == NULL || *tv.vval.v_string == NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100478 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000479 semsg(_(e_invalid_string_for_import_str), arg);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200480 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100481 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100482
Bram Moolenaar921ba522021-07-29 22:25:05 +0200483 // Give error messages for the start of the line.
484 SOURCING_LNUM = start_lnum;
485
Bram Moolenaar1c991142020-07-04 13:15:31 +0200486 /*
487 * find script file
488 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100489 if (*tv.vval.v_string == '.')
490 {
491 size_t len;
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100492 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100493 char_u *tail = gettail(si->sn_name);
494 char_u *from_name;
495
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100496 // Relative to current script: "./name.vim", "../../name.vim".
497 len = STRLEN(si->sn_name) - STRLEN(tail) + STRLEN(tv.vval.v_string) + 2;
498 from_name = alloc((int)len);
499 if (from_name == NULL)
500 goto erret;
501 vim_strncpy(from_name, si->sn_name, tail - si->sn_name);
502 add_pathsep(from_name);
503 STRCAT(from_name, tv.vval.v_string);
504 simplify_filename(from_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100505
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100506 res = handle_import_fname(from_name, is_autoload, &sid);
507 vim_free(from_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100508 }
Bram Moolenaar113b8dc2022-01-18 13:43:58 +0000509 else if (mch_isFullName(tv.vval.v_string)
510#ifdef BACKSLASH_IN_FILENAME
511 // On MS-Windows omitting the drive is still handled like an
512 // absolute path, not using 'runtimepath'.
513 || *tv.vval.v_string == '/' || *tv.vval.v_string == '\\'
514#endif
515 )
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100516 {
517 // Absolute path: "/tmp/name.vim"
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100518 res = handle_import_fname(tv.vval.v_string, is_autoload, &sid);
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000519 }
520 else if (is_autoload)
521 {
522 size_t len = 9 + STRLEN(tv.vval.v_string) + 1;
523 char_u *from_name;
524
525 // Find file in "autoload" subdirs in 'runtimepath'.
526 from_name = alloc((int)len);
527 if (from_name == NULL)
528 goto erret;
529 vim_snprintf((char *)from_name, len, "autoload/%s", tv.vval.v_string);
530 // we need a scriptitem without loading the script
531 sid = find_script_in_rtp(from_name);
532 vim_free(from_name);
Bram Moolenaard041f422022-01-12 19:54:00 +0000533 if (SCRIPT_ID_VALID(sid))
534 {
535 scriptitem_T *si = SCRIPT_ITEM(sid);
536
537 if (si->sn_autoload_prefix == NULL)
538 si->sn_autoload_prefix = get_autoload_prefix(si);
539 res = OK;
Bram Moolenaar3e4fa3d2022-01-13 22:05:09 +0000540 if (override_autoload && si->sn_state == SN_STATE_NOT_LOADED)
541 // testing override: load autoload script right away
542 (void)do_source(si->sn_name, FALSE, DOSO_NONE, NULL);
Bram Moolenaard041f422022-01-12 19:54:00 +0000543 }
544 else
545 res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100546 }
547 else
548 {
549 size_t len = 7 + STRLEN(tv.vval.v_string) + 1;
550 char_u *from_name;
551
552 // Find file in "import" subdirs in 'runtimepath'.
553 from_name = alloc((int)len);
554 if (from_name == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200555 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100556 vim_snprintf((char *)from_name, len, "import/%s", tv.vval.v_string);
557 res = source_in_path(p_rtp, from_name, DIP_NOAFTER, &sid);
558 vim_free(from_name);
559 }
560
561 if (res == FAIL || sid <= 0)
562 {
Bram Moolenaar1836d612022-01-18 13:14:47 +0000563 semsg(_(is_autoload && sid == -2
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000564 ? e_autoload_import_cannot_use_absolute_or_relative_path
565 : e_could_not_import_str), tv.vval.v_string);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200566 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100567 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100568
Bram Moolenaar779aeff2022-02-08 19:12:19 +0000569 if (sid == current_sctx.sc_sid)
570 {
571 emsg(_(e_script_cannot_import_itself));
572 goto erret;
573 }
574
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000575 import_gap = gap != NULL ? gap : &SCRIPT_ITEM(import_sid)->sn_imports;
576 for (i = 0; i < import_gap->ga_len; ++i)
577 {
578 imported_T *import = (imported_T *)import_gap->ga_data + i;
579
580 if (import->imp_sid == sid)
581 {
582 if (import->imp_flags & IMP_FLAGS_RELOAD)
583 {
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000584 // encountering same script first time on a reload is OK
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000585 import->imp_flags &= ~IMP_FLAGS_RELOAD;
586 break;
587 }
588 semsg(_(e_cannot_import_same_script_twice_str), tv.vval.v_string);
589 goto erret;
590 }
591 }
592
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000593 // Allow for the "as Name" to be in the next line.
594 nextarg = eval_next_non_blank(expr_end, evalarg, &getnext);
595 if (STRNCMP("as", nextarg, 2) == 0 && IS_WHITE_OR_NUL(nextarg[2]))
596 {
597 char_u *p;
598
599 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +0100600 arg = eval_next_line(expr_end, evalarg);
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000601 else
602 arg = nextarg;
603
604 // Skip over "as Name "; no line break allowed after "as".
605 // Do not allow for ':' and '#'.
606 arg = skipwhite(arg + 2);
607 p = arg;
608 if (eval_isnamec1(*arg))
609 while (ASCII_ISALNUM(*arg) || *arg == '_')
610 ++arg;
611 if (p == arg || !IS_WHITE_OR_NUL(*arg))
612 {
613 semsg(_(e_syntax_error_in_import_str), p);
614 goto erret;
615 }
616 as_name = vim_strnsave(p, arg - p);
617 arg = skipwhite(arg);
618 }
619 else
620 {
621 char_u *p = gettail(tv.vval.v_string);
622 char_u *end = (char_u *)strstr((char *)p, ".vim");
623
624 if (!ends_excmd2(arg_start, expr_end))
625 {
626 semsg(_(e_trailing_characters_str), expr_end);
627 goto erret;
628 }
Bram Moolenaar834d4182022-01-07 13:38:24 +0000629 if (end == NULL || end[4] != NUL)
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000630 {
Bram Moolenaar834d4182022-01-07 13:38:24 +0000631 semsg(_(e_imported_script_must_use_as_or_end_in_dot_vim_str), p);
632 goto erret;
633 }
634 if (end == p)
635 {
636 semsg(_(e_cannot_import_dot_vim_without_using_as), p);
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000637 goto erret;
638 }
639 as_name = vim_strnsave(p, end - p);
640 }
641
642 if (as_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100643 {
Bram Moolenaar0a842842021-02-27 22:41:19 +0100644 imported_T *imported;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100645
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000646 imported = find_imported(as_name, STRLEN(as_name), FALSE);
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000647 if (imported != NULL && imported->imp_sid != sid)
Bram Moolenaara6294952020-12-27 13:39:50 +0100648 {
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000649 semsg(_(e_name_already_defined_str), as_name);
650 goto erret;
Bram Moolenaara6294952020-12-27 13:39:50 +0100651 }
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000652 else if (imported == NULL
Bram Moolenaardce24412022-02-08 20:35:30 +0000653 && check_defined(as_name, STRLEN(as_name), cctx, NULL,
654 FALSE) == FAIL)
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000655 goto erret;
Bram Moolenaara6294952020-12-27 13:39:50 +0100656
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100657 if (imported == NULL)
Bram Moolenaar17d36cb2022-01-12 11:46:40 +0000658 {
659 imported = new_imported(import_gap);
660 if (imported == NULL)
661 goto erret;
662 imported->imp_name = as_name;
663 as_name = NULL;
664 imported->imp_sid = sid;
665 if (is_autoload)
666 imported->imp_flags = IMP_FLAGS_AUTOLOAD;
667 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100668 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200669
Bram Moolenaar1c991142020-07-04 13:15:31 +0200670erret:
Bram Moolenaar4db572e2021-07-18 18:21:38 +0200671 clear_tv(&tv);
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000672 vim_free(as_name);
673 return arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100674}
675
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200676/*
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000677 * ":import 'filename'"
678 * ":import 'filename' as Name"
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200679 */
680 void
681ex_import(exarg_T *eap)
682{
683 char_u *cmd_end;
684 evalarg_T evalarg;
685
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +0000686 if (!sourcing_a_script(eap))
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200687 {
688 emsg(_(e_import_can_only_be_used_in_script));
689 return;
690 }
691 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
692
693 cmd_end = handle_import(eap->arg, NULL, current_sctx.sc_sid,
694 &evalarg, NULL);
695 if (cmd_end != NULL)
696 set_nextcmd(eap, cmd_end);
697 clear_evalarg(&evalarg, eap);
698}
699
700/*
Bram Moolenaar753885b2022-08-24 16:30:36 +0100701 * When a script is a symlink it may be imported with one name and sourced
702 * under another name. Adjust the import script ID if needed.
703 * "*sid" must be a valid script ID.
704 */
705 void
706import_check_sourced_sid(int *sid)
707{
708 scriptitem_T *script = SCRIPT_ITEM(*sid);
709
710 if (script->sn_sourced_sid > 0)
711 *sid = script->sn_sourced_sid;
712}
713
714/*
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000715 * Find an exported item in "sid" matching "name".
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000716 * Either "cctx" or "cstack" is NULL.
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200717 * When it is a variable return the index.
718 * When it is a user function return "*ufunc".
719 * When not found returns -1 and "*ufunc" is NULL.
720 */
721 int
722find_exported(
723 int sid,
724 char_u *name,
725 ufunc_T **ufunc,
726 type_T **type,
727 cctx_T *cctx,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000728 cstack_T *cstack,
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200729 int verbose)
730{
731 int idx = -1;
732 svar_T *sv;
733 scriptitem_T *script = SCRIPT_ITEM(sid);
734
LemonBoyaf59e342022-04-24 21:55:00 +0100735 *ufunc = NULL;
736
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100737 if (script->sn_import_autoload && script->sn_state == SN_STATE_NOT_LOADED)
738 {
739 if (do_source(script->sn_name, FALSE, DOSO_NONE, NULL) == FAIL)
Bram Moolenaaraac12da2022-04-24 21:33:20 +0100740 {
741 semsg(_(e_cant_open_file_str), script->sn_name);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100742 return -1;
Bram Moolenaaraac12da2022-04-24 21:33:20 +0100743 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100744 }
745
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200746 // Find name in "script".
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000747 idx = get_script_item_idx(sid, name, 0, cctx, cstack);
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200748 if (idx >= 0)
749 {
750 sv = ((svar_T *)script->sn_var_vals.ga_data) + idx;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +0100751 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200752 {
753 if (verbose)
754 semsg(_(e_item_not_exported_in_script_str), name);
755 return -1;
756 }
757 *type = sv->sv_type;
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200758 }
759 else
760 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +0000761 size_t len = STRLEN(name);
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200762 char_u buffer[200];
763 char_u *funcname;
764
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +0000765 // It could be a user function. Normally this is stored as
766 // "<SNR>99_name". For an autoload script a function is stored with
767 // the autoload prefix: "dir#script#name".
768 if (script->sn_autoload_prefix != NULL)
769 len += STRLEN(script->sn_autoload_prefix) + 2;
770 else
771 len += 15;
772
773 if (len < sizeof(buffer))
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200774 funcname = buffer;
775 else
776 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +0000777 funcname = alloc(len);
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200778 if (funcname == NULL)
779 return -1;
780 }
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +0000781 if (script->sn_autoload_prefix != NULL)
782 {
783 sprintf((char *)funcname, "%s%s", script->sn_autoload_prefix, name);
784 }
785 else
786 {
787 funcname[0] = K_SPECIAL;
788 funcname[1] = KS_EXTRA;
789 funcname[2] = (int)KE_SNR;
790 sprintf((char *)funcname + 3, "%ld_%s", (long)sid, name);
791 }
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000792 *ufunc = find_func(funcname, FALSE);
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200793
794 if (*ufunc == NULL)
795 {
796 if (verbose)
Bram Moolenaard02dce22022-01-18 17:43:04 +0000797 {
798 ufunc_T *alt_ufunc = NULL;
799
800 if (script->sn_autoload_prefix != NULL)
801 {
802 // try find the function by the script-local name
803 funcname[0] = K_SPECIAL;
804 funcname[1] = KS_EXTRA;
805 funcname[2] = (int)KE_SNR;
806 sprintf((char *)funcname + 3, "%ld_%s", (long)sid, name);
807 alt_ufunc = find_func(funcname, FALSE);
808 }
809 if (alt_ufunc != NULL)
810 semsg(_(e_item_not_exported_in_script_str), name);
811 else
812 semsg(_(e_item_not_found_in_script_str), name);
813 }
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200814 }
815 else if (((*ufunc)->uf_flags & FC_EXPORT) == 0)
816 {
817 if (verbose)
818 semsg(_(e_item_not_exported_in_script_str), name);
819 *ufunc = NULL;
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200820 }
Bram Moolenaard02dce22022-01-18 17:43:04 +0000821 if (funcname != buffer)
822 vim_free(funcname);
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200823 }
824
825 return idx;
826}
827
828/*
Bram Moolenaar5cb53b72022-05-26 19:54:05 +0100829 * Declare a script-local variable without init: "var name: type".
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200830 * "const" is an error since the value is missing.
831 * Returns a pointer to after the type.
832 */
833 char_u *
834vim9_declare_scriptvar(exarg_T *eap, char_u *arg)
835{
836 char_u *p;
837 char_u *name;
838 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
839 type_T *type;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200840 typval_T init_tv;
841
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200842 if (eap->cmdidx == CMD_final || eap->cmdidx == CMD_const)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200843 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200844 if (eap->cmdidx == CMD_final)
845 emsg(_(e_final_requires_a_value));
846 else
847 emsg(_(e_const_requires_a_value));
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200848 return arg + STRLEN(arg);
849 }
850
851 // Check for valid starting character.
852 if (!eval_isnamec1(*arg))
853 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000854 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200855 return arg + STRLEN(arg);
856 }
857
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200858 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
Bram Moolenaar3b74b6b2020-06-19 19:01:43 +0200859 if (*p == ':' && (VIM_ISWHITE(p[1]) || p != arg + 1))
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200860 break;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200861
862 if (*p != ':')
863 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +0200864 emsg(_(e_type_or_initialization_required));
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200865 return arg + STRLEN(arg);
866 }
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200867 if (!VIM_ISWHITE(p[1]))
868 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100869 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200870 return arg + STRLEN(arg);
871 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200872 name = vim_strnsave(arg, p - arg);
873
Bram Moolenaard0edaf92021-05-28 21:06:08 +0200874 // parse type, check for reserved name
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200875 p = skipwhite(p + 1);
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100876 type = parse_type(&p, &si->sn_type_list, TRUE);
Bram Moolenaard0edaf92021-05-28 21:06:08 +0200877 if (type == NULL || check_reserved_name(name) == FAIL)
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200878 {
879 vim_free(name);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200880 return p;
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200881 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200882
883 // Create the variable with 0/NULL value.
884 CLEAR_FIELD(init_tv);
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +0200885 if (type->tt_type == VAR_ANY)
886 // A variable of type "any" is not possible, just use zero instead
887 init_tv.v_type = VAR_NUMBER;
888 else
889 init_tv.v_type = type->tt_type;
Bram Moolenaar859cc212022-03-28 15:22:35 +0100890 set_var_const(name, 0, type, &init_tv, FALSE, ASSIGN_INIT, 0);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200891
892 vim_free(name);
893 return p;
894}
895
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200896/*
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200897 * Vim9 part of adding a script variable: add it to sn_all_vars (lookup by name
898 * with a hashtable) and sn_var_vals (lookup by index).
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100899 * When "create" is TRUE this is a new variable, otherwise find and update an
900 * existing variable.
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +0100901 * "flags" can have ASSIGN_FINAL, ASSIGN_CONST or ASSIGN_INIT.
Bram Moolenaarf2253962021-04-13 20:53:13 +0200902 * When "*type" is NULL use "tv" for the type and update "*type". If
903 * "do_member" is TRUE also use the member type, otherwise use "any".
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200904 */
905 void
Bram Moolenaar08251752021-01-11 21:20:18 +0100906update_vim9_script_var(
907 int create,
908 dictitem_T *di,
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +0000909 char_u *name,
Bram Moolenaar08251752021-01-11 21:20:18 +0100910 int flags,
911 typval_T *tv,
Bram Moolenaarf2253962021-04-13 20:53:13 +0200912 type_T **type,
913 int do_member)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200914{
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100915 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
916 hashitem_T *hi;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200917 svar_T *sv = NULL;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200918
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100919 if (create)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200920 {
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200921 sallvar_T *newsav;
922 sallvar_T *sav = NULL;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200923
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100924 // Store a pointer to the typval_T, so that it can be found by index
925 // instead of using a hastab lookup.
926 if (ga_grow(&si->sn_var_vals, 1) == FAIL)
927 return;
928
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +0000929 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200930 if (!HASHITEM_EMPTY(hi))
931 {
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200932 // Variable with this name exists, either in this block or in
933 // another block.
934 for (sav = HI2SAV(hi); ; sav = sav->sav_next)
935 {
936 if (sav->sav_block_id == si->sn_current_block_id)
937 {
938 // variable defined in a loop, re-use the entry
939 sv = ((svar_T *)si->sn_var_vals.ga_data)
940 + sav->sav_var_vals_idx;
941 // unhide the variable
942 if (sv->sv_tv == &sav->sav_tv)
943 {
944 clear_tv(&sav->sav_tv);
945 sv->sv_tv = &di->di_tv;
946 sav->sav_di = di;
947 }
948 break;
949 }
950 if (sav->sav_next == NULL)
951 break;
952 }
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200953 }
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200954
955 if (sv == NULL)
956 {
957 // Variable not defined or not defined in current block: Add a
958 // svar_T and create a new sallvar_T.
959 sv = ((svar_T *)si->sn_var_vals.ga_data) + si->sn_var_vals.ga_len;
960 newsav = (sallvar_T *)alloc_clear(
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +0000961 sizeof(sallvar_T) + STRLEN(name));
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200962 if (newsav == NULL)
963 return;
964
965 sv->sv_tv = &di->di_tv;
966 sv->sv_const = (flags & ASSIGN_FINAL) ? ASSIGN_FINAL
967 : (flags & ASSIGN_CONST) ? ASSIGN_CONST : 0;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +0100968 sv->sv_flags = is_export ? SVFLAG_EXPORTED : 0;
969 if ((flags & ASSIGN_INIT) == 0)
970 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200971 newsav->sav_var_vals_idx = si->sn_var_vals.ga_len;
972 ++si->sn_var_vals.ga_len;
Yee Cheng Chin07eaa1e2022-10-07 16:00:04 +0100973 // a pointer to the first char avoids a FORTIFY_SOURCE problem
974 STRCPY(&newsav->sav_key[0], name);
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200975 sv->sv_name = newsav->sav_key;
976 newsav->sav_di = di;
977 newsav->sav_block_id = si->sn_current_block_id;
978
979 if (HASHITEM_EMPTY(hi))
980 // new variable name
981 hash_add(&si->sn_all_vars.dv_hashtab, newsav->sav_key);
982 else if (sav != NULL)
983 // existing name in a new block, append to the list
984 sav->sav_next = newsav;
985 }
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200986 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100987 else
988 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +0100989 sv = find_typval_in_script(&di->di_tv, 0, TRUE);
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100990 }
991 if (sv != NULL)
992 {
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100993 if (*type == NULL)
Bram Moolenaar114dbda2022-01-03 12:28:03 +0000994 *type = typval2type(tv, get_copyID(), &si->sn_type_list,
995 do_member ? TVTT_DO_MEMBER : 0);
Bram Moolenaar859cc212022-03-28 15:22:35 +0100996 else if ((flags & ASSIGN_INIT) == 0
997 && (*type)->tt_type == VAR_BLOB && tv->v_type == VAR_BLOB
998 && tv->vval.v_blob == NULL)
999 {
1000 // "var b: blob = null_blob" has a different type.
1001 *type = &t_blob_null;
1002 }
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01001003 if (sv->sv_flags & SVFLAG_TYPE_ALLOCATED)
Bram Moolenaardd297bc2021-12-10 10:37:38 +00001004 free_type(sv->sv_type);
1005 if (*type != NULL && ((*type)->tt_type == VAR_FUNC
1006 || (*type)->tt_type == VAR_PARTIAL))
1007 {
1008 // The type probably uses uf_type_list, which is cleared when the
1009 // function is freed, but the script variable may keep the type.
1010 // Make a copy to avoid using freed memory.
1011 sv->sv_type = alloc_type(*type);
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01001012 sv->sv_flags |= SVFLAG_TYPE_ALLOCATED;
Bram Moolenaardd297bc2021-12-10 10:37:38 +00001013 }
1014 else
1015 {
1016 sv->sv_type = *type;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01001017 sv->sv_flags &= ~SVFLAG_TYPE_ALLOCATED;
Bram Moolenaardd297bc2021-12-10 10:37:38 +00001018 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001019 }
1020
1021 // let ex_export() know the export worked.
1022 is_export = FALSE;
Bram Moolenaar8d739de2020-10-14 19:39:19 +02001023}
1024
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001025/*
1026 * Hide a script variable when leaving a block.
1027 * "idx" is de index in sn_var_vals.
Bram Moolenaar39ca4122020-10-20 14:25:07 +02001028 * When "func_defined" is non-zero then a function was defined in this block,
1029 * the variable may be accessed by it. Otherwise the variable can be cleared.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001030 */
Bram Moolenaar8d739de2020-10-14 19:39:19 +02001031 void
Bram Moolenaar39ca4122020-10-20 14:25:07 +02001032hide_script_var(scriptitem_T *si, int idx, int func_defined)
Bram Moolenaar8d739de2020-10-14 19:39:19 +02001033{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001034 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8d739de2020-10-14 19:39:19 +02001035 hashtab_T *script_ht = get_script_local_ht();
1036 hashtab_T *all_ht = &si->sn_all_vars.dv_hashtab;
1037 hashitem_T *script_hi;
1038 hashitem_T *all_hi;
1039
1040 // Remove a variable declared inside the block, if it still exists.
Bram Moolenaar39ca4122020-10-20 14:25:07 +02001041 // If it was added in a nested block it will already have been removed.
Bram Moolenaar8d739de2020-10-14 19:39:19 +02001042 // The typval is moved into the sallvar_T.
1043 script_hi = hash_find(script_ht, sv->sv_name);
1044 all_hi = hash_find(all_ht, sv->sv_name);
1045 if (!HASHITEM_EMPTY(script_hi) && !HASHITEM_EMPTY(all_hi))
1046 {
1047 dictitem_T *di = HI2DI(script_hi);
1048 sallvar_T *sav = HI2SAV(all_hi);
Bram Moolenaar39ca4122020-10-20 14:25:07 +02001049 sallvar_T *sav_prev = NULL;
Bram Moolenaar8d739de2020-10-14 19:39:19 +02001050
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001051 // There can be multiple entries with the same name in different
1052 // blocks, find the right one.
1053 while (sav != NULL && sav->sav_var_vals_idx != idx)
Bram Moolenaar39ca4122020-10-20 14:25:07 +02001054 {
1055 sav_prev = sav;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001056 sav = sav->sav_next;
Bram Moolenaar39ca4122020-10-20 14:25:07 +02001057 }
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001058 if (sav != NULL)
1059 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02001060 if (func_defined)
1061 {
1062 // move the typval from the dictitem to the sallvar
1063 sav->sav_tv = di->di_tv;
1064 di->di_tv.v_type = VAR_UNKNOWN;
1065 sav->sav_flags = di->di_flags;
1066 sav->sav_di = NULL;
1067 sv->sv_tv = &sav->sav_tv;
1068 }
1069 else
1070 {
1071 if (sav_prev == NULL)
1072 hash_remove(all_ht, all_hi);
1073 else
1074 sav_prev->sav_next = sav->sav_next;
1075 sv->sv_name = NULL;
1076 vim_free(sav);
1077 }
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001078 delete_var(script_ht, script_hi);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001079 }
Bram Moolenaar8d739de2020-10-14 19:39:19 +02001080 }
1081}
1082
1083/*
Bram Moolenaar10c65862020-10-08 21:16:42 +02001084 * Find the script-local variable that links to "dest".
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001085 * If "sid" is zero use the current script.
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001086 * if "must_find" is TRUE and "dest" cannot be found report an internal error.
Bram Moolenaarc967d572021-07-08 21:38:50 +02001087 * Returns NULL if not found and give an internal error.
Bram Moolenaar34db91f2020-06-13 19:00:10 +02001088 */
Bram Moolenaar10c65862020-10-08 21:16:42 +02001089 svar_T *
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001090find_typval_in_script(typval_T *dest, scid_T sid, int must_find)
Bram Moolenaar34db91f2020-06-13 19:00:10 +02001091{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001092 scriptitem_T *si = SCRIPT_ITEM(sid == 0 ? current_sctx.sc_sid : sid);
Bram Moolenaar34db91f2020-06-13 19:00:10 +02001093 int idx;
1094
Bram Moolenaar81e17fb2020-08-21 21:55:43 +02001095 if (si->sn_version != SCRIPT_VERSION_VIM9)
1096 // legacy script doesn't store variable types
Bram Moolenaar10c65862020-10-08 21:16:42 +02001097 return NULL;
Bram Moolenaar81e17fb2020-08-21 21:55:43 +02001098
Bram Moolenaara06758d2021-10-15 00:18:37 +01001099 // Find the svar_T in sn_var_vals. Start at the end, in a for loop the
1100 // variable was added at the end.
1101 for (idx = si->sn_var_vals.ga_len - 1; idx >= 0; --idx)
Bram Moolenaar34db91f2020-06-13 19:00:10 +02001102 {
1103 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1104
Bram Moolenaarc0913d02020-12-05 14:44:37 +01001105 // If "sv_name" is NULL the variable was hidden when leaving a block,
1106 // don't check "sv_tv" then, it might be used for another variable now.
1107 if (sv->sv_name != NULL && sv->sv_tv == dest)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001108 return sv;
Bram Moolenaar34db91f2020-06-13 19:00:10 +02001109 }
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001110 if (must_find)
1111 iemsg("find_typval_in_script(): not found");
Bram Moolenaar10c65862020-10-08 21:16:42 +02001112 return NULL;
1113}
1114
1115/*
1116 * Check if the type of script variable "dest" allows assigning "value".
1117 * If needed convert "value" to a bool.
1118 */
1119 int
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001120check_script_var_type(
Bram Moolenaar7824fc82021-11-26 17:36:51 +00001121 svar_T *sv,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001122 typval_T *value,
1123 char_u *name,
1124 where_T where)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001125{
Bram Moolenaar10c65862020-10-08 21:16:42 +02001126 int ret;
1127
Bram Moolenaar7824fc82021-11-26 17:36:51 +00001128 if (sv->sv_const != 0)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001129 {
Bram Moolenaar7824fc82021-11-26 17:36:51 +00001130 semsg(_(e_cannot_change_readonly_variable_str), name);
1131 return FAIL;
Bram Moolenaar10c65862020-10-08 21:16:42 +02001132 }
Bram Moolenaar7824fc82021-11-26 17:36:51 +00001133 ret = check_typval_type(sv->sv_type, value, where);
1134 if (ret == OK && need_convert_to_bool(sv->sv_type, value))
1135 {
1136 int val = tv2bool(value);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001137
Bram Moolenaar7824fc82021-11-26 17:36:51 +00001138 clear_tv(value);
1139 value->v_type = VAR_BOOL;
1140 value->v_lock = 0;
1141 value->vval.v_number = val ? VVAL_TRUE : VVAL_FALSE;
1142 }
1143 return ret;
Bram Moolenaar34db91f2020-06-13 19:00:10 +02001144}
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001145
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001146// words that cannot be used as a variable
1147static char *reserved[] = {
1148 "true",
1149 "false",
1150 "null",
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00001151 "null_blob",
1152 "null_dict",
1153 "null_function",
1154 "null_list",
1155 "null_partial",
1156 "null_string",
1157 "null_channel",
1158 "null_job",
Bram Moolenaar74235772021-06-12 14:53:05 +02001159 "this",
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001160 NULL
1161};
1162
1163 int
1164check_reserved_name(char_u *name)
1165{
1166 int idx;
1167
1168 for (idx = 0; reserved[idx] != NULL; ++idx)
1169 if (STRCMP(reserved[idx], name) == 0)
1170 {
1171 semsg(_(e_cannot_use_reserved_name), name);
1172 return FAIL;
1173 }
1174 return OK;
1175}
1176
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001177#endif // FEAT_EVAL