blob: 12c266a27bf0bfd2c48d20b2ecf06d6d793915ef [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
62/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010063 * ":vim9script".
64 */
65 void
Bram Moolenaar9b8d6222020-12-28 18:26:00 +010066ex_vim9script(exarg_T *eap UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010067{
Bram Moolenaar9b8d6222020-12-28 18:26:00 +010068#ifdef FEAT_EVAL
Bram Moolenaar2b327002020-12-26 15:39:31 +010069 int sid = current_sctx.sc_sid;
Bram Moolenaar101f4812020-06-16 23:18:51 +020070 scriptitem_T *si;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010071
72 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
73 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +020074 emsg(_(e_vim9script_can_only_be_used_in_script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010075 return;
76 }
Bram Moolenaar2b327002020-12-26 15:39:31 +010077
78 si = SCRIPT_ITEM(sid);
79 if (si->sn_state == SN_STATE_HAD_COMMAND)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010080 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +020081 emsg(_(e_vim9script_must_be_first_command_in_script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010082 return;
83 }
Bram Moolenaar2b327002020-12-26 15:39:31 +010084 if (!IS_WHITE_OR_NUL(*eap->arg) && STRCMP(eap->arg, "noclear") != 0)
85 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +000086 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar2b327002020-12-26 15:39:31 +010087 return;
88 }
89 if (si->sn_state == SN_STATE_RELOAD && IS_WHITE_OR_NUL(*eap->arg))
90 {
91 hashtab_T *ht = &SCRIPT_VARS(sid);
92
93 // Reloading a script without the "noclear" argument: clear
94 // script-local variables and functions.
95 hashtab_free_contents(ht);
96 hash_init(ht);
97 delete_script_functions(sid);
98
99 // old imports and script variables are no longer valid
100 free_imports_and_script_vars(sid);
101 }
102 si->sn_state = SN_STATE_HAD_COMMAND;
103
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100104 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
105 si->sn_version = SCRIPT_VERSION_VIM9;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100106
107 if (STRCMP(p_cpo, CPO_VIM) != 0)
108 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200109 si->sn_save_cpo = vim_strsave(p_cpo);
Bram Moolenaar37294bd2021-03-10 13:40:08 +0100110 set_option_value((char_u *)"cpo", 0L, (char_u *)CPO_VIM, OPT_NO_REDRAW);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100111 }
Bram Moolenaar9b8d6222020-12-28 18:26:00 +0100112#else
113 // No check for this being the first command, it doesn't matter.
114 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
115#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100116}
117
118/*
Bram Moolenaarae616492020-07-28 20:07:27 +0200119 * When in Vim9 script give an error and return FAIL.
120 */
121 int
122not_in_vim9(exarg_T *eap)
123{
Bram Moolenaar68e30442020-07-28 21:15:07 +0200124 if (in_vim9script())
125 switch (eap->cmdidx)
126 {
Bram Moolenaarada1d872021-02-20 08:16:51 +0100127 case CMD_k:
128 if (eap->addr_count > 0)
129 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000130 emsg(_(e_no_range_allowed));
Bram Moolenaarada1d872021-02-20 08:16:51 +0100131 return FAIL;
132 }
133 // FALLTHROUGH
Bram Moolenaar68e30442020-07-28 21:15:07 +0200134 case CMD_append:
135 case CMD_change:
Bram Moolenaarf5a48012020-08-01 17:00:03 +0200136 case CMD_insert:
Bram Moolenaar65088802021-03-13 21:07:21 +0100137 case CMD_open:
Bram Moolenaarf5a48012020-08-01 17:00:03 +0200138 case CMD_t:
Bram Moolenaar68e30442020-07-28 21:15:07 +0200139 case CMD_xit:
Bram Moolenaar9b8d6222020-12-28 18:26:00 +0100140 semsg(_(e_command_not_supported_in_vim9_script_missing_var_str), eap->cmd);
Bram Moolenaar68e30442020-07-28 21:15:07 +0200141 return FAIL;
142 default: break;
143 }
Bram Moolenaarae616492020-07-28 20:07:27 +0200144 return OK;
145}
146
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100147/*
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100148 * Give an error message if "p" points at "#{" and return TRUE.
149 * This avoids that using a legacy style #{} dictionary leads to difficult to
150 * understand errors.
151 */
152 int
153vim9_bad_comment(char_u *p)
154{
Bram Moolenaara0399ef2021-03-20 15:00:01 +0100155 if (p[0] == '#' && p[1] == '{' && p[2] != '{')
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100156 {
157 emsg(_(e_cannot_use_hash_curly_to_start_comment));
158 return TRUE;
159 }
160 return FALSE;
161}
162
163/*
Bram Moolenaara0399ef2021-03-20 15:00:01 +0100164 * Return TRUE if "p" points at a "#" not followed by one '{'.
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100165 * Does not check for white space.
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100166 */
167 int
168vim9_comment_start(char_u *p)
169{
Bram Moolenaara0399ef2021-03-20 15:00:01 +0100170 return p[0] == '#' && (p[1] != '{' || p[2] == '{');
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100171}
172
Bram Moolenaar9b8d6222020-12-28 18:26:00 +0100173#if defined(FEAT_EVAL) || defined(PROTO)
174
Bram Moolenaarae616492020-07-28 20:07:27 +0200175/*
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200176 * "++nr" and "--nr" commands.
177 */
178 void
179ex_incdec(exarg_T *eap)
180{
181 char_u *cmd = eap->cmd;
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200182 char_u *nextcmd = eap->nextcmd;
183 size_t len = STRLEN(eap->cmd) + 8;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200184
Bram Moolenaar22480d12021-06-25 21:31:09 +0200185 if (VIM_ISWHITE(cmd[2]))
186 {
187 semsg(_(e_no_white_space_allowed_after_str_str),
188 eap->cmdidx == CMD_increment ? "++" : "--", eap->cmd);
189 return;
190 }
191
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200192 // This works like "nr += 1" or "nr -= 1".
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200193 // Add a '|' to avoid looking in the next line.
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200194 eap->cmd = alloc(len);
195 if (eap->cmd == NULL)
196 return;
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200197 vim_snprintf((char *)eap->cmd, len, "%s %c= 1 |", cmd + 2,
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200198 eap->cmdidx == CMD_increment ? '+' : '-');
199 eap->arg = eap->cmd;
200 eap->cmdidx = CMD_var;
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200201 eap->nextcmd = NULL;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200202 ex_let(eap);
203 vim_free(eap->cmd);
204 eap->cmd = cmd;
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200205 eap->nextcmd = nextcmd;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200206}
207
208/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100209 * ":export let Name: type"
210 * ":export const Name: type"
211 * ":export def Name(..."
212 * ":export class Name ..."
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100213 */
214 void
Bram Moolenaar09689a02020-05-09 22:50:08 +0200215ex_export(exarg_T *eap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100216{
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200217 if (!in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100218 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200219 emsg(_(e_export_can_only_be_used_in_vim9script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100220 return;
221 }
222
223 eap->cmd = eap->arg;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +0100224 (void)find_ex_command(eap, NULL, lookup_scriptitem, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100225 switch (eap->cmdidx)
226 {
227 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200228 case CMD_var:
229 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100230 case CMD_const:
231 case CMD_def:
232 // case CMD_class:
233 is_export = TRUE;
234 do_cmdline(eap->cmd, eap->getline, eap->cookie,
235 DOCMD_VERBOSE + DOCMD_NOWAIT);
236
237 // The command will reset "is_export" when exporting an item.
238 if (is_export)
239 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200240 emsg(_(e_export_with_invalid_argument));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100241 is_export = FALSE;
242 }
243 break;
244 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200245 emsg(_(e_invalid_command_after_export));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100246 break;
247 }
248}
249
250/*
251 * Add a new imported item entry to the current script.
252 */
253 static imported_T *
254new_imported(garray_T *gap)
255{
256 if (ga_grow(gap, 1) == OK)
257 return ((imported_T *)gap->ga_data + gap->ga_len++);
258 return NULL;
259}
260
261/*
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200262 * Free the script variables from "sn_all_vars".
263 */
264 static void
265free_all_script_vars(scriptitem_T *si)
266{
267 int todo;
268 hashtab_T *ht = &si->sn_all_vars.dv_hashtab;
269 hashitem_T *hi;
270 sallvar_T *sav;
271 sallvar_T *sav_next;
Bram Moolenaardd297bc2021-12-10 10:37:38 +0000272 int idx;
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200273
274 hash_lock(ht);
275 todo = (int)ht->ht_used;
276 for (hi = ht->ht_array; todo > 0; ++hi)
277 {
278 if (!HASHITEM_EMPTY(hi))
279 {
280 --todo;
281
282 // Free the variable. Don't remove it from the hashtab, ht_array
283 // might change then. hash_clear() takes care of it later.
284 sav = HI2SAV(hi);
285 while (sav != NULL)
286 {
287 sav_next = sav->sav_next;
288 if (sav->sav_di == NULL)
289 clear_tv(&sav->sav_tv);
290 vim_free(sav);
291 sav = sav_next;
292 }
293 }
294 }
295 hash_clear(ht);
296 hash_init(ht);
297
Bram Moolenaardd297bc2021-12-10 10:37:38 +0000298 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
299 {
300 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
301
302 if (sv->sv_type_allocated)
303 free_type(sv->sv_type);
304 }
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200305 ga_clear(&si->sn_var_vals);
306
307 // existing commands using script variable indexes are no longer valid
308 si->sn_script_seq = current_sctx.sc_seq;
309}
310
311/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100312 * Free all imported items in script "sid".
313 */
314 void
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200315free_imports_and_script_vars(int sid)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100316{
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100317 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100318 int idx;
319
320 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
321 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100322 imported_T *imp = ((imported_T *)si->sn_imports.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100323
324 vim_free(imp->imp_name);
325 }
326 ga_clear(&si->sn_imports);
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200327
328 free_all_script_vars(si);
329
Bram Moolenaar6110e792020-07-08 19:35:21 +0200330 clear_type_list(&si->sn_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100331}
332
333/*
Bram Moolenaara6294952020-12-27 13:39:50 +0100334 * Mark all imports as possible to redefine. Used when a script is loaded
335 * again but not cleared.
336 */
337 void
338mark_imports_for_reload(int sid)
339{
340 scriptitem_T *si = SCRIPT_ITEM(sid);
341 int idx;
342
343 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
344 {
345 imported_T *imp = ((imported_T *)si->sn_imports.ga_data) + idx;
346
347 imp->imp_flags |= IMP_FLAGS_RELOAD;
348 }
349}
350
351/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100352 * Handle an ":import" command and add the resulting imported_T to "gap", when
353 * not NULL, or script "import_sid" sn_imports.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200354 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100355 * Returns a pointer to after the command or NULL in case of failure
356 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200357 static char_u *
Bram Moolenaar1c991142020-07-04 13:15:31 +0200358handle_import(
359 char_u *arg_start,
360 garray_T *gap,
361 int import_sid,
362 evalarg_T *evalarg,
363 void *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100364{
365 char_u *arg = arg_start;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200366 char_u *cmd_end = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100367 int ret = FAIL;
368 typval_T tv;
369 int sid = -1;
370 int res;
Bram Moolenaar0a842842021-02-27 22:41:19 +0100371 int mult = FALSE;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200372 garray_T names;
Bram Moolenaar0a842842021-02-27 22:41:19 +0100373 garray_T as_names;
Bram Moolenaar921ba522021-07-29 22:25:05 +0200374 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100375
Bram Moolenaar4db572e2021-07-18 18:21:38 +0200376 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200377 ga_init2(&names, sizeof(char_u *), 10);
Bram Moolenaar0a842842021-02-27 22:41:19 +0100378 ga_init2(&as_names, sizeof(char_u *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100379 if (*arg == '{')
380 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200381 // "import {item, item} from ..."
Bram Moolenaar0a842842021-02-27 22:41:19 +0100382 mult = TRUE;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200383 arg = skipwhite_and_linebreak(arg + 1, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100384 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200385
Bram Moolenaar0a842842021-02-27 22:41:19 +0100386 for (;;)
387 {
388 char_u *p = arg;
389 int had_comma = FALSE;
390 char_u *as_name = NULL;
391
392 // accept "*" or "Name"
393 if (!mult && arg[0] == '*' && IS_WHITE_OR_NUL(arg[1]))
394 ++arg;
395 else
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100396 while (eval_isnamec(*arg))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100397 ++arg;
Bram Moolenaar0a842842021-02-27 22:41:19 +0100398 if (p == arg)
399 break;
400 if (ga_grow(&names, 1) == FAIL || ga_grow(&as_names, 1) == FAIL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200401 goto erret;
Bram Moolenaar0a842842021-02-27 22:41:19 +0100402 ((char_u **)names.ga_data)[names.ga_len] = vim_strnsave(p, arg - p);
403 ++names.ga_len;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200404
Bram Moolenaar0a842842021-02-27 22:41:19 +0100405 arg = skipwhite_and_linebreak(arg, evalarg);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200406 if (STRNCMP("as", arg, 2) == 0 && IS_WHITE_OR_NUL(arg[2]))
407 {
Bram Moolenaara9e3d562021-09-08 12:31:35 +0200408 // Skip over "as Name "; no line break allowed after "as".
409 // Do not allow for ':' and '#'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100410 arg = skipwhite(arg + 2);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200411 p = arg;
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100412 if (eval_isnamec1(*arg))
Bram Moolenaara9e3d562021-09-08 12:31:35 +0200413 while (ASCII_ISALNUM(*arg) || *arg == '_')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100414 ++arg;
Bram Moolenaara9e3d562021-09-08 12:31:35 +0200415 if (p == arg || !(IS_WHITE_OR_NUL(*arg)
416 || (mult && (*arg == ',' || *arg == '}'))))
417 {
418 semsg(_(e_syntax_error_in_import_str), p);
419 goto erret;
420 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100421 if (check_defined(p, arg - p, cctx, FALSE) == FAIL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200422 goto erret;
423 as_name = vim_strnsave(p, arg - p);
424 arg = skipwhite_and_linebreak(arg, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100425 }
426 else if (*arg_start == '*')
427 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200428 emsg(_(e_missing_as_after_star));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200429 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100430 }
Bram Moolenaar0a842842021-02-27 22:41:19 +0100431 // without "as Name" the as_names entry is NULL
432 ((char_u **)as_names.ga_data)[as_names.ga_len] = as_name;
433 ++as_names.ga_len;
434
435 if (!mult)
436 break;
437 if (*arg == ',')
438 {
439 had_comma = TRUE;
440 ++arg;
441 }
442 arg = skipwhite_and_linebreak(arg, evalarg);
443 if (*arg == '}')
444 {
445 ++arg;
446 break;
447 }
448 if (!had_comma)
449 {
450 emsg(_(e_missing_comma_in_import));
451 goto erret;
452 }
453 }
454 arg = skipwhite_and_linebreak(arg, evalarg);
455
456 if (names.ga_len == 0)
457 {
Bram Moolenaara9e3d562021-09-08 12:31:35 +0200458 semsg(_(e_syntax_error_in_import_str), arg_start);
Bram Moolenaar0a842842021-02-27 22:41:19 +0100459 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100460 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200461
462 if (STRNCMP("from", arg, 4) != 0 || !IS_WHITE_OR_NUL(arg[4]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100463 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200464 emsg(_(e_missing_from));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200465 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100466 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200467
Bram Moolenaar4db572e2021-07-18 18:21:38 +0200468 // The name of the file can be an expression, which must evaluate to a
469 // string.
Bram Moolenaar962d7212020-07-04 14:15:00 +0200470 arg = skipwhite_and_linebreak(arg + 4, evalarg);
Bram Moolenaar4db572e2021-07-18 18:21:38 +0200471 ret = eval0(arg, &tv, NULL, evalarg);
472 if (ret == FAIL)
473 goto erret;
474 if (tv.v_type != VAR_STRING
475 || tv.vval.v_string == NULL || *tv.vval.v_string == NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100476 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200477 emsg(_(e_invalid_string_after_from));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200478 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100479 }
480 cmd_end = arg;
481
Bram Moolenaar921ba522021-07-29 22:25:05 +0200482 // Give error messages for the start of the line.
483 SOURCING_LNUM = start_lnum;
484
Bram Moolenaar1c991142020-07-04 13:15:31 +0200485 /*
486 * find script file
487 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100488 if (*tv.vval.v_string == '.')
489 {
490 size_t len;
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100491 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100492 char_u *tail = gettail(si->sn_name);
493 char_u *from_name;
494
495 // Relative to current script: "./name.vim", "../../name.vim".
496 len = STRLEN(si->sn_name) - STRLEN(tail) + STRLEN(tv.vval.v_string) + 2;
497 from_name = alloc((int)len);
498 if (from_name == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200499 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100500 vim_strncpy(from_name, si->sn_name, tail - si->sn_name);
501 add_pathsep(from_name);
502 STRCAT(from_name, tv.vval.v_string);
503 simplify_filename(from_name);
504
505 res = do_source(from_name, FALSE, DOSO_NONE, &sid);
506 vim_free(from_name);
507 }
508 else if (mch_isFullName(tv.vval.v_string))
509 {
510 // Absolute path: "/tmp/name.vim"
511 res = do_source(tv.vval.v_string, FALSE, DOSO_NONE, &sid);
512 }
513 else
514 {
515 size_t len = 7 + STRLEN(tv.vval.v_string) + 1;
516 char_u *from_name;
517
518 // Find file in "import" subdirs in 'runtimepath'.
519 from_name = alloc((int)len);
520 if (from_name == NULL)
521 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200522 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100523 }
524 vim_snprintf((char *)from_name, len, "import/%s", tv.vval.v_string);
525 res = source_in_path(p_rtp, from_name, DIP_NOAFTER, &sid);
526 vim_free(from_name);
527 }
528
529 if (res == FAIL || sid <= 0)
530 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200531 semsg(_(e_could_not_import_str), tv.vval.v_string);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200532 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100533 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100534
535 if (*arg_start == '*')
536 {
Bram Moolenaar0a842842021-02-27 22:41:19 +0100537 imported_T *imported;
538 char_u *as_name = ((char_u **)as_names.ga_data)[0];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100539
Bram Moolenaar0a842842021-02-27 22:41:19 +0100540 // "import * as That"
Bram Moolenaara6294952020-12-27 13:39:50 +0100541 imported = find_imported(as_name, STRLEN(as_name), cctx);
542 if (imported != NULL && imported->imp_sid == sid)
543 {
544 if (imported->imp_flags & IMP_FLAGS_RELOAD)
545 // import already defined on a previous script load
546 imported->imp_flags &= ~IMP_FLAGS_RELOAD;
547 else
548 {
549 semsg(_(e_name_already_defined_str), as_name);
550 goto erret;
551 }
552 }
553
554 imported = new_imported(gap != NULL ? gap
555 : &SCRIPT_ITEM(import_sid)->sn_imports);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100556 if (imported == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200557 goto erret;
558 imported->imp_name = as_name;
Bram Moolenaar0a842842021-02-27 22:41:19 +0100559 ((char_u **)as_names.ga_data)[0] = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100560 imported->imp_sid = sid;
Bram Moolenaara6294952020-12-27 13:39:50 +0100561 imported->imp_flags = IMP_FLAGS_STAR;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100562 }
563 else
564 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200565 int i;
566
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100567 arg = arg_start;
568 if (*arg == '{')
569 arg = skipwhite(arg + 1);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200570 for (i = 0; i < names.ga_len; ++i)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100571 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200572 char_u *name = ((char_u **)names.ga_data)[i];
Bram Moolenaar0a842842021-02-27 22:41:19 +0100573 char_u *as_name = ((char_u **)as_names.ga_data)[i];
Bram Moolenaara6294952020-12-27 13:39:50 +0100574 size_t len = STRLEN(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100575 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100576 imported_T *imported;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100577 ufunc_T *ufunc = NULL;
578 type_T *type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100579
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +0100580 idx = find_exported(sid, name, &ufunc, &type, cctx, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100581
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100582 if (idx < 0 && ufunc == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200583 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100584
Bram Moolenaarc967d572021-07-08 21:38:50 +0200585 // If already imported with the same properties and the
Bram Moolenaara6294952020-12-27 13:39:50 +0100586 // IMP_FLAGS_RELOAD set then we keep that entry. Otherwise create
587 // a new one (and give an error for an existing import).
588 imported = find_imported(name, len, cctx);
589 if (imported != NULL
590 && (imported->imp_flags & IMP_FLAGS_RELOAD)
591 && imported->imp_sid == sid
592 && (idx >= 0
Bram Moolenaar60dc8272021-07-29 22:48:54 +0200593 ? (equal_type(imported->imp_type, type, 0)
Bram Moolenaara6294952020-12-27 13:39:50 +0100594 && imported->imp_var_vals_idx == idx)
Bram Moolenaar60dc8272021-07-29 22:48:54 +0200595 : (equal_type(imported->imp_type, ufunc->uf_func_type,
596 ETYPE_ARG_UNKNOWN)
Bram Moolenaara6294952020-12-27 13:39:50 +0100597 && STRCMP(imported->imp_funcname,
598 ufunc->uf_name) == 0)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100599 {
Bram Moolenaara6294952020-12-27 13:39:50 +0100600 imported->imp_flags &= ~IMP_FLAGS_RELOAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100601 }
602 else
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200603 {
Bram Moolenaar6c7cc342021-04-17 16:38:50 +0200604 if (as_name == NULL
605 && check_defined(name, len, cctx, FALSE) == FAIL)
Bram Moolenaara6294952020-12-27 13:39:50 +0100606 goto erret;
607
608 imported = new_imported(gap != NULL ? gap
609 : &SCRIPT_ITEM(import_sid)->sn_imports);
610 if (imported == NULL)
611 goto erret;
612
Bram Moolenaar0a842842021-02-27 22:41:19 +0100613 if (as_name == NULL)
614 {
615 imported->imp_name = name;
616 ((char_u **)names.ga_data)[i] = NULL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100617 }
Bram Moolenaar0a842842021-02-27 22:41:19 +0100618 else
619 {
620 // "import This as That ..."
621 imported->imp_name = as_name;
622 ((char_u **)as_names.ga_data)[i] = NULL;
623 }
Bram Moolenaara6294952020-12-27 13:39:50 +0100624 imported->imp_sid = sid;
625 if (idx >= 0)
626 {
627 imported->imp_type = type;
628 imported->imp_var_vals_idx = idx;
629 }
630 else
631 {
632 imported->imp_type = ufunc->uf_func_type;
633 imported->imp_funcname = ufunc->uf_name;
634 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200635 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100636 }
637 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200638erret:
Bram Moolenaar4db572e2021-07-18 18:21:38 +0200639 clear_tv(&tv);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200640 ga_clear_strings(&names);
Bram Moolenaar0a842842021-02-27 22:41:19 +0100641 ga_clear_strings(&as_names);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100642 return cmd_end;
643}
644
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200645/*
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200646 * ":import Item from 'filename'"
647 * ":import Item as Alias from 'filename'"
648 * ":import {Item} from 'filename'".
649 * ":import {Item as Alias} from 'filename'"
650 * ":import {Item, Item} from 'filename'"
651 * ":import {Item, Item as Alias} from 'filename'"
652 *
653 * ":import * as Name from 'filename'"
654 */
655 void
656ex_import(exarg_T *eap)
657{
658 char_u *cmd_end;
659 evalarg_T evalarg;
660
661 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
662 {
663 emsg(_(e_import_can_only_be_used_in_script));
664 return;
665 }
666 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
667
668 cmd_end = handle_import(eap->arg, NULL, current_sctx.sc_sid,
669 &evalarg, NULL);
670 if (cmd_end != NULL)
671 set_nextcmd(eap, cmd_end);
672 clear_evalarg(&evalarg, eap);
673}
674
675/*
676 * Find an exported item in "sid" matching the name at "*argp".
677 * When it is a variable return the index.
678 * When it is a user function return "*ufunc".
679 * When not found returns -1 and "*ufunc" is NULL.
680 */
681 int
682find_exported(
683 int sid,
684 char_u *name,
685 ufunc_T **ufunc,
686 type_T **type,
687 cctx_T *cctx,
688 int verbose)
689{
690 int idx = -1;
691 svar_T *sv;
692 scriptitem_T *script = SCRIPT_ITEM(sid);
693
694 // Find name in "script".
695 idx = get_script_item_idx(sid, name, 0, cctx);
696 if (idx >= 0)
697 {
698 sv = ((svar_T *)script->sn_var_vals.ga_data) + idx;
699 if (!sv->sv_export)
700 {
701 if (verbose)
702 semsg(_(e_item_not_exported_in_script_str), name);
703 return -1;
704 }
705 *type = sv->sv_type;
706 *ufunc = NULL;
707 }
708 else
709 {
710 char_u buffer[200];
711 char_u *funcname;
712
713 // it could be a user function.
714 if (STRLEN(name) < sizeof(buffer) - 15)
715 funcname = buffer;
716 else
717 {
718 funcname = alloc(STRLEN(name) + 15);
719 if (funcname == NULL)
720 return -1;
721 }
722 funcname[0] = K_SPECIAL;
723 funcname[1] = KS_EXTRA;
724 funcname[2] = (int)KE_SNR;
725 sprintf((char *)funcname + 3, "%ld_%s", (long)sid, name);
726 *ufunc = find_func(funcname, FALSE, NULL);
727 if (funcname != buffer)
728 vim_free(funcname);
729
730 if (*ufunc == NULL)
731 {
732 if (verbose)
733 semsg(_(e_item_not_found_in_script_str), name);
734 return -1;
735 }
736 else if (((*ufunc)->uf_flags & FC_EXPORT) == 0)
737 {
738 if (verbose)
739 semsg(_(e_item_not_exported_in_script_str), name);
740 *ufunc = NULL;
741 return -1;
742 }
743 }
744
745 return idx;
746}
747
748/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200749 * Declare a script-local variable without init: "let var: type".
750 * "const" is an error since the value is missing.
751 * Returns a pointer to after the type.
752 */
753 char_u *
754vim9_declare_scriptvar(exarg_T *eap, char_u *arg)
755{
756 char_u *p;
757 char_u *name;
758 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
759 type_T *type;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200760 typval_T init_tv;
761
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200762 if (eap->cmdidx == CMD_final || eap->cmdidx == CMD_const)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200763 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200764 if (eap->cmdidx == CMD_final)
765 emsg(_(e_final_requires_a_value));
766 else
767 emsg(_(e_const_requires_a_value));
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200768 return arg + STRLEN(arg);
769 }
770
771 // Check for valid starting character.
772 if (!eval_isnamec1(*arg))
773 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000774 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200775 return arg + STRLEN(arg);
776 }
777
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200778 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
Bram Moolenaar3b74b6b2020-06-19 19:01:43 +0200779 if (*p == ':' && (VIM_ISWHITE(p[1]) || p != arg + 1))
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200780 break;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200781
782 if (*p != ':')
783 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +0200784 emsg(_(e_type_or_initialization_required));
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200785 return arg + STRLEN(arg);
786 }
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200787 if (!VIM_ISWHITE(p[1]))
788 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100789 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200790 return arg + STRLEN(arg);
791 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200792 name = vim_strnsave(arg, p - arg);
793
Bram Moolenaard0edaf92021-05-28 21:06:08 +0200794 // parse type, check for reserved name
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200795 p = skipwhite(p + 1);
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100796 type = parse_type(&p, &si->sn_type_list, TRUE);
Bram Moolenaard0edaf92021-05-28 21:06:08 +0200797 if (type == NULL || check_reserved_name(name) == FAIL)
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200798 {
799 vim_free(name);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200800 return p;
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200801 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200802
803 // Create the variable with 0/NULL value.
804 CLEAR_FIELD(init_tv);
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +0200805 if (type->tt_type == VAR_ANY)
806 // A variable of type "any" is not possible, just use zero instead
807 init_tv.v_type = VAR_NUMBER;
808 else
809 init_tv.v_type = type->tt_type;
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100810 set_var_const(name, type, &init_tv, FALSE, 0, 0);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200811
812 vim_free(name);
813 return p;
814}
815
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200816/*
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200817 * Vim9 part of adding a script variable: add it to sn_all_vars (lookup by name
818 * with a hashtable) and sn_var_vals (lookup by index).
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100819 * When "create" is TRUE this is a new variable, otherwise find and update an
820 * existing variable.
Bram Moolenaar08251752021-01-11 21:20:18 +0100821 * "flags" can have ASSIGN_FINAL or ASSIGN_CONST.
Bram Moolenaarf2253962021-04-13 20:53:13 +0200822 * When "*type" is NULL use "tv" for the type and update "*type". If
823 * "do_member" is TRUE also use the member type, otherwise use "any".
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200824 */
825 void
Bram Moolenaar08251752021-01-11 21:20:18 +0100826update_vim9_script_var(
827 int create,
828 dictitem_T *di,
829 int flags,
830 typval_T *tv,
Bram Moolenaarf2253962021-04-13 20:53:13 +0200831 type_T **type,
832 int do_member)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200833{
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100834 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
835 hashitem_T *hi;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200836 svar_T *sv = NULL;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200837
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100838 if (create)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200839 {
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200840 sallvar_T *newsav;
841 sallvar_T *sav = NULL;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200842
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100843 // Store a pointer to the typval_T, so that it can be found by index
844 // instead of using a hastab lookup.
845 if (ga_grow(&si->sn_var_vals, 1) == FAIL)
846 return;
847
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200848 hi = hash_find(&si->sn_all_vars.dv_hashtab, di->di_key);
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200849 if (!HASHITEM_EMPTY(hi))
850 {
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200851 // Variable with this name exists, either in this block or in
852 // another block.
853 for (sav = HI2SAV(hi); ; sav = sav->sav_next)
854 {
855 if (sav->sav_block_id == si->sn_current_block_id)
856 {
857 // variable defined in a loop, re-use the entry
858 sv = ((svar_T *)si->sn_var_vals.ga_data)
859 + sav->sav_var_vals_idx;
860 // unhide the variable
861 if (sv->sv_tv == &sav->sav_tv)
862 {
863 clear_tv(&sav->sav_tv);
864 sv->sv_tv = &di->di_tv;
865 sav->sav_di = di;
866 }
867 break;
868 }
869 if (sav->sav_next == NULL)
870 break;
871 }
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200872 }
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200873
874 if (sv == NULL)
875 {
876 // Variable not defined or not defined in current block: Add a
877 // svar_T and create a new sallvar_T.
878 sv = ((svar_T *)si->sn_var_vals.ga_data) + si->sn_var_vals.ga_len;
879 newsav = (sallvar_T *)alloc_clear(
880 sizeof(sallvar_T) + STRLEN(di->di_key));
881 if (newsav == NULL)
882 return;
883
884 sv->sv_tv = &di->di_tv;
885 sv->sv_const = (flags & ASSIGN_FINAL) ? ASSIGN_FINAL
886 : (flags & ASSIGN_CONST) ? ASSIGN_CONST : 0;
887 sv->sv_export = is_export;
888 newsav->sav_var_vals_idx = si->sn_var_vals.ga_len;
889 ++si->sn_var_vals.ga_len;
890 STRCPY(&newsav->sav_key, di->di_key);
891 sv->sv_name = newsav->sav_key;
892 newsav->sav_di = di;
893 newsav->sav_block_id = si->sn_current_block_id;
894
895 if (HASHITEM_EMPTY(hi))
896 // new variable name
897 hash_add(&si->sn_all_vars.dv_hashtab, newsav->sav_key);
898 else if (sav != NULL)
899 // existing name in a new block, append to the list
900 sav->sav_next = newsav;
901 }
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200902 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100903 else
904 {
Bram Moolenaarc967d572021-07-08 21:38:50 +0200905 sv = find_typval_in_script(&di->di_tv);
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100906 }
907 if (sv != NULL)
908 {
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100909 if (*type == NULL)
Bram Moolenaar114dbda2022-01-03 12:28:03 +0000910 *type = typval2type(tv, get_copyID(), &si->sn_type_list,
911 do_member ? TVTT_DO_MEMBER : 0);
Bram Moolenaardd297bc2021-12-10 10:37:38 +0000912 if (sv->sv_type_allocated)
913 free_type(sv->sv_type);
914 if (*type != NULL && ((*type)->tt_type == VAR_FUNC
915 || (*type)->tt_type == VAR_PARTIAL))
916 {
917 // The type probably uses uf_type_list, which is cleared when the
918 // function is freed, but the script variable may keep the type.
919 // Make a copy to avoid using freed memory.
920 sv->sv_type = alloc_type(*type);
921 sv->sv_type_allocated = TRUE;
922 }
923 else
924 {
925 sv->sv_type = *type;
926 sv->sv_type_allocated = FALSE;
927 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100928 }
929
930 // let ex_export() know the export worked.
931 is_export = FALSE;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200932}
933
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200934/*
935 * Hide a script variable when leaving a block.
936 * "idx" is de index in sn_var_vals.
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200937 * When "func_defined" is non-zero then a function was defined in this block,
938 * the variable may be accessed by it. Otherwise the variable can be cleared.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200939 */
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200940 void
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200941hide_script_var(scriptitem_T *si, int idx, int func_defined)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200942{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200943 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200944 hashtab_T *script_ht = get_script_local_ht();
945 hashtab_T *all_ht = &si->sn_all_vars.dv_hashtab;
946 hashitem_T *script_hi;
947 hashitem_T *all_hi;
948
949 // Remove a variable declared inside the block, if it still exists.
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200950 // If it was added in a nested block it will already have been removed.
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200951 // The typval is moved into the sallvar_T.
952 script_hi = hash_find(script_ht, sv->sv_name);
953 all_hi = hash_find(all_ht, sv->sv_name);
954 if (!HASHITEM_EMPTY(script_hi) && !HASHITEM_EMPTY(all_hi))
955 {
956 dictitem_T *di = HI2DI(script_hi);
957 sallvar_T *sav = HI2SAV(all_hi);
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200958 sallvar_T *sav_prev = NULL;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200959
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200960 // There can be multiple entries with the same name in different
961 // blocks, find the right one.
962 while (sav != NULL && sav->sav_var_vals_idx != idx)
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200963 {
964 sav_prev = sav;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200965 sav = sav->sav_next;
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200966 }
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200967 if (sav != NULL)
968 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200969 if (func_defined)
970 {
971 // move the typval from the dictitem to the sallvar
972 sav->sav_tv = di->di_tv;
973 di->di_tv.v_type = VAR_UNKNOWN;
974 sav->sav_flags = di->di_flags;
975 sav->sav_di = NULL;
976 sv->sv_tv = &sav->sav_tv;
977 }
978 else
979 {
980 if (sav_prev == NULL)
981 hash_remove(all_ht, all_hi);
982 else
983 sav_prev->sav_next = sav->sav_next;
984 sv->sv_name = NULL;
985 vim_free(sav);
986 }
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200987 delete_var(script_ht, script_hi);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200988 }
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200989 }
990}
991
992/*
Bram Moolenaar10c65862020-10-08 21:16:42 +0200993 * Find the script-local variable that links to "dest".
Bram Moolenaarc967d572021-07-08 21:38:50 +0200994 * Returns NULL if not found and give an internal error.
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200995 */
Bram Moolenaar10c65862020-10-08 21:16:42 +0200996 svar_T *
Bram Moolenaarc967d572021-07-08 21:38:50 +0200997find_typval_in_script(typval_T *dest)
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200998{
999 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
1000 int idx;
1001
Bram Moolenaar81e17fb2020-08-21 21:55:43 +02001002 if (si->sn_version != SCRIPT_VERSION_VIM9)
1003 // legacy script doesn't store variable types
Bram Moolenaar10c65862020-10-08 21:16:42 +02001004 return NULL;
Bram Moolenaar81e17fb2020-08-21 21:55:43 +02001005
Bram Moolenaara06758d2021-10-15 00:18:37 +01001006 // Find the svar_T in sn_var_vals. Start at the end, in a for loop the
1007 // variable was added at the end.
1008 for (idx = si->sn_var_vals.ga_len - 1; idx >= 0; --idx)
Bram Moolenaar34db91f2020-06-13 19:00:10 +02001009 {
1010 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1011
Bram Moolenaarc0913d02020-12-05 14:44:37 +01001012 // If "sv_name" is NULL the variable was hidden when leaving a block,
1013 // don't check "sv_tv" then, it might be used for another variable now.
1014 if (sv->sv_name != NULL && sv->sv_tv == dest)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001015 return sv;
Bram Moolenaar34db91f2020-06-13 19:00:10 +02001016 }
Bram Moolenaarc967d572021-07-08 21:38:50 +02001017 iemsg("find_typval_in_script(): not found");
Bram Moolenaar10c65862020-10-08 21:16:42 +02001018 return NULL;
1019}
1020
1021/*
1022 * Check if the type of script variable "dest" allows assigning "value".
1023 * If needed convert "value" to a bool.
1024 */
1025 int
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001026check_script_var_type(
Bram Moolenaar7824fc82021-11-26 17:36:51 +00001027 svar_T *sv,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001028 typval_T *value,
1029 char_u *name,
1030 where_T where)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001031{
Bram Moolenaar10c65862020-10-08 21:16:42 +02001032 int ret;
1033
Bram Moolenaar7824fc82021-11-26 17:36:51 +00001034 if (sv->sv_const != 0)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001035 {
Bram Moolenaar7824fc82021-11-26 17:36:51 +00001036 semsg(_(e_cannot_change_readonly_variable_str), name);
1037 return FAIL;
Bram Moolenaar10c65862020-10-08 21:16:42 +02001038 }
Bram Moolenaar7824fc82021-11-26 17:36:51 +00001039 ret = check_typval_type(sv->sv_type, value, where);
1040 if (ret == OK && need_convert_to_bool(sv->sv_type, value))
1041 {
1042 int val = tv2bool(value);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001043
Bram Moolenaar7824fc82021-11-26 17:36:51 +00001044 clear_tv(value);
1045 value->v_type = VAR_BOOL;
1046 value->v_lock = 0;
1047 value->vval.v_number = val ? VVAL_TRUE : VVAL_FALSE;
1048 }
1049 return ret;
Bram Moolenaar34db91f2020-06-13 19:00:10 +02001050}
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001051
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001052// words that cannot be used as a variable
1053static char *reserved[] = {
1054 "true",
1055 "false",
1056 "null",
Bram Moolenaar74235772021-06-12 14:53:05 +02001057 "this",
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001058 NULL
1059};
1060
1061 int
1062check_reserved_name(char_u *name)
1063{
1064 int idx;
1065
1066 for (idx = 0; reserved[idx] != NULL; ++idx)
1067 if (STRCMP(reserved[idx], name) == 0)
1068 {
1069 semsg(_(e_cannot_use_reserved_name), name);
1070 return FAIL;
1071 }
1072 return OK;
1073}
1074
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001075#endif // FEAT_EVAL