blob: 90c39f97f4420d11a95298d2f2cd2d8b540bd0b3 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9script.c: :vim9script, :import, :export and friends
12 */
13
14#include "vim.h"
15
Bram Moolenaar9b8d6222020-12-28 18:26:00 +010016#if defined(FEAT_EVAL)
17# include "vim9.h"
18#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010019
Bram Moolenaare535db82021-03-31 21:07:24 +020020/*
21 * Return TRUE when currently using Vim9 script syntax.
22 * Does not go up the stack, a ":function" inside vim9script uses legacy
23 * syntax.
24 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010025 int
26in_vim9script(void)
27{
Bram Moolenaare535db82021-03-31 21:07:24 +020028 // "sc_version" is also set when compiling a ":def" function in legacy
29 // script.
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +020030 return (current_sctx.sc_version == SCRIPT_VERSION_VIM9
31 || (cmdmod.cmod_flags & CMOD_VIM9CMD))
32 && !(cmdmod.cmod_flags & CMOD_LEGACY);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010033}
34
Bram Moolenaarb91d3f82021-04-01 13:17:50 +020035#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010036/*
Bram Moolenaardd9de502021-08-15 13:49:42 +020037 * Return TRUE when currently in a script with script version smaller than
38 * "max_version" or command modifiers forced it.
39 */
40 int
41in_old_script(int max_version)
42{
Bram Moolenaar5cebca22021-08-15 14:39:13 +020043 return (current_sctx.sc_version < max_version
44 && !(cmdmod.cmod_flags & CMOD_VIM9CMD))
Bram Moolenaardd9de502021-08-15 13:49:42 +020045 || (cmdmod.cmod_flags & CMOD_LEGACY);
46}
47
48/*
Bram Moolenaare535db82021-03-31 21:07:24 +020049 * Return TRUE if the current script is Vim9 script.
50 * This also returns TRUE in a legacy function in a Vim9 script.
51 */
52 int
53current_script_is_vim9(void)
54{
55 return SCRIPT_ID_VALID(current_sctx.sc_sid)
56 && SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
57 == SCRIPT_VERSION_VIM9;
58}
Bram Moolenaarb91d3f82021-04-01 13:17:50 +020059#endif
Bram Moolenaare535db82021-03-31 21:07:24 +020060
61/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010062 * ":vim9script".
63 */
64 void
Bram Moolenaar9b8d6222020-12-28 18:26:00 +010065ex_vim9script(exarg_T *eap UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010066{
Bram Moolenaar9b8d6222020-12-28 18:26:00 +010067#ifdef FEAT_EVAL
Bram Moolenaar2b327002020-12-26 15:39:31 +010068 int sid = current_sctx.sc_sid;
Bram Moolenaar101f4812020-06-16 23:18:51 +020069 scriptitem_T *si;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010070
71 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
72 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +020073 emsg(_(e_vim9script_can_only_be_used_in_script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010074 return;
75 }
Bram Moolenaar2b327002020-12-26 15:39:31 +010076
77 si = SCRIPT_ITEM(sid);
78 if (si->sn_state == SN_STATE_HAD_COMMAND)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010079 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +020080 emsg(_(e_vim9script_must_be_first_command_in_script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010081 return;
82 }
Bram Moolenaar2b327002020-12-26 15:39:31 +010083 if (!IS_WHITE_OR_NUL(*eap->arg) && STRCMP(eap->arg, "noclear") != 0)
84 {
85 semsg(_(e_invarg2), eap->arg);
86 return;
87 }
88 if (si->sn_state == SN_STATE_RELOAD && IS_WHITE_OR_NUL(*eap->arg))
89 {
90 hashtab_T *ht = &SCRIPT_VARS(sid);
91
92 // Reloading a script without the "noclear" argument: clear
93 // script-local variables and functions.
94 hashtab_free_contents(ht);
95 hash_init(ht);
96 delete_script_functions(sid);
97
98 // old imports and script variables are no longer valid
99 free_imports_and_script_vars(sid);
100 }
101 si->sn_state = SN_STATE_HAD_COMMAND;
102
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100103 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
104 si->sn_version = SCRIPT_VERSION_VIM9;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100105
106 if (STRCMP(p_cpo, CPO_VIM) != 0)
107 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200108 si->sn_save_cpo = vim_strsave(p_cpo);
Bram Moolenaar37294bd2021-03-10 13:40:08 +0100109 set_option_value((char_u *)"cpo", 0L, (char_u *)CPO_VIM, OPT_NO_REDRAW);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100110 }
Bram Moolenaar9b8d6222020-12-28 18:26:00 +0100111#else
112 // No check for this being the first command, it doesn't matter.
113 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
114#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100115}
116
117/*
Bram Moolenaarae616492020-07-28 20:07:27 +0200118 * When in Vim9 script give an error and return FAIL.
119 */
120 int
121not_in_vim9(exarg_T *eap)
122{
Bram Moolenaar68e30442020-07-28 21:15:07 +0200123 if (in_vim9script())
124 switch (eap->cmdidx)
125 {
Bram Moolenaarada1d872021-02-20 08:16:51 +0100126 case CMD_k:
127 if (eap->addr_count > 0)
128 {
129 emsg(_(e_norange));
130 return FAIL;
131 }
132 // FALLTHROUGH
Bram Moolenaar68e30442020-07-28 21:15:07 +0200133 case CMD_append:
134 case CMD_change:
Bram Moolenaarf5a48012020-08-01 17:00:03 +0200135 case CMD_insert:
Bram Moolenaar65088802021-03-13 21:07:21 +0100136 case CMD_open:
Bram Moolenaarf5a48012020-08-01 17:00:03 +0200137 case CMD_t:
Bram Moolenaar68e30442020-07-28 21:15:07 +0200138 case CMD_xit:
Bram Moolenaar9b8d6222020-12-28 18:26:00 +0100139 semsg(_(e_command_not_supported_in_vim9_script_missing_var_str), eap->cmd);
Bram Moolenaar68e30442020-07-28 21:15:07 +0200140 return FAIL;
141 default: break;
142 }
Bram Moolenaarae616492020-07-28 20:07:27 +0200143 return OK;
144}
145
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100146/*
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100147 * Give an error message if "p" points at "#{" and return TRUE.
148 * This avoids that using a legacy style #{} dictionary leads to difficult to
149 * understand errors.
150 */
151 int
152vim9_bad_comment(char_u *p)
153{
Bram Moolenaara0399ef2021-03-20 15:00:01 +0100154 if (p[0] == '#' && p[1] == '{' && p[2] != '{')
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100155 {
156 emsg(_(e_cannot_use_hash_curly_to_start_comment));
157 return TRUE;
158 }
159 return FALSE;
160}
161
162/*
Bram Moolenaara0399ef2021-03-20 15:00:01 +0100163 * Return TRUE if "p" points at a "#" not followed by one '{'.
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100164 * Does not check for white space.
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100165 */
166 int
167vim9_comment_start(char_u *p)
168{
Bram Moolenaara0399ef2021-03-20 15:00:01 +0100169 return p[0] == '#' && (p[1] != '{' || p[2] == '{');
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100170}
171
Bram Moolenaar9b8d6222020-12-28 18:26:00 +0100172#if defined(FEAT_EVAL) || defined(PROTO)
173
Bram Moolenaarae616492020-07-28 20:07:27 +0200174/*
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200175 * "++nr" and "--nr" commands.
176 */
177 void
178ex_incdec(exarg_T *eap)
179{
180 char_u *cmd = eap->cmd;
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200181 char_u *nextcmd = eap->nextcmd;
182 size_t len = STRLEN(eap->cmd) + 8;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200183
Bram Moolenaar22480d12021-06-25 21:31:09 +0200184 if (VIM_ISWHITE(cmd[2]))
185 {
186 semsg(_(e_no_white_space_allowed_after_str_str),
187 eap->cmdidx == CMD_increment ? "++" : "--", eap->cmd);
188 return;
189 }
190
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200191 // This works like "nr += 1" or "nr -= 1".
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200192 // Add a '|' to avoid looking in the next line.
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200193 eap->cmd = alloc(len);
194 if (eap->cmd == NULL)
195 return;
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200196 vim_snprintf((char *)eap->cmd, len, "%s %c= 1 |", cmd + 2,
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200197 eap->cmdidx == CMD_increment ? '+' : '-');
198 eap->arg = eap->cmd;
199 eap->cmdidx = CMD_var;
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200200 eap->nextcmd = NULL;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200201 ex_let(eap);
202 vim_free(eap->cmd);
203 eap->cmd = cmd;
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200204 eap->nextcmd = nextcmd;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200205}
206
207/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100208 * ":export let Name: type"
209 * ":export const Name: type"
210 * ":export def Name(..."
211 * ":export class Name ..."
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100212 */
213 void
Bram Moolenaar09689a02020-05-09 22:50:08 +0200214ex_export(exarg_T *eap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100215{
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200216 if (!in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100217 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200218 emsg(_(e_export_can_only_be_used_in_vim9script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100219 return;
220 }
221
222 eap->cmd = eap->arg;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +0100223 (void)find_ex_command(eap, NULL, lookup_scriptitem, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100224 switch (eap->cmdidx)
225 {
226 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200227 case CMD_var:
228 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100229 case CMD_const:
230 case CMD_def:
231 // case CMD_class:
232 is_export = TRUE;
233 do_cmdline(eap->cmd, eap->getline, eap->cookie,
234 DOCMD_VERBOSE + DOCMD_NOWAIT);
235
236 // The command will reset "is_export" when exporting an item.
237 if (is_export)
238 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200239 emsg(_(e_export_with_invalid_argument));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100240 is_export = FALSE;
241 }
242 break;
243 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200244 emsg(_(e_invalid_command_after_export));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100245 break;
246 }
247}
248
249/*
250 * Add a new imported item entry to the current script.
251 */
252 static imported_T *
253new_imported(garray_T *gap)
254{
255 if (ga_grow(gap, 1) == OK)
256 return ((imported_T *)gap->ga_data + gap->ga_len++);
257 return NULL;
258}
259
260/*
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200261 * Free the script variables from "sn_all_vars".
262 */
263 static void
264free_all_script_vars(scriptitem_T *si)
265{
266 int todo;
267 hashtab_T *ht = &si->sn_all_vars.dv_hashtab;
268 hashitem_T *hi;
269 sallvar_T *sav;
270 sallvar_T *sav_next;
Bram Moolenaardd297bc2021-12-10 10:37:38 +0000271 int idx;
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200272
273 hash_lock(ht);
274 todo = (int)ht->ht_used;
275 for (hi = ht->ht_array; todo > 0; ++hi)
276 {
277 if (!HASHITEM_EMPTY(hi))
278 {
279 --todo;
280
281 // Free the variable. Don't remove it from the hashtab, ht_array
282 // might change then. hash_clear() takes care of it later.
283 sav = HI2SAV(hi);
284 while (sav != NULL)
285 {
286 sav_next = sav->sav_next;
287 if (sav->sav_di == NULL)
288 clear_tv(&sav->sav_tv);
289 vim_free(sav);
290 sav = sav_next;
291 }
292 }
293 }
294 hash_clear(ht);
295 hash_init(ht);
296
Bram Moolenaardd297bc2021-12-10 10:37:38 +0000297 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
298 {
299 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
300
301 if (sv->sv_type_allocated)
302 free_type(sv->sv_type);
303 }
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200304 ga_clear(&si->sn_var_vals);
305
306 // existing commands using script variable indexes are no longer valid
307 si->sn_script_seq = current_sctx.sc_seq;
308}
309
310/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100311 * Free all imported items in script "sid".
312 */
313 void
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200314free_imports_and_script_vars(int sid)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100315{
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100316 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100317 int idx;
318
319 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
320 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100321 imported_T *imp = ((imported_T *)si->sn_imports.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100322
323 vim_free(imp->imp_name);
324 }
325 ga_clear(&si->sn_imports);
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200326
327 free_all_script_vars(si);
328
Bram Moolenaar6110e792020-07-08 19:35:21 +0200329 clear_type_list(&si->sn_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100330}
331
332/*
Bram Moolenaara6294952020-12-27 13:39:50 +0100333 * Mark all imports as possible to redefine. Used when a script is loaded
334 * again but not cleared.
335 */
336 void
337mark_imports_for_reload(int sid)
338{
339 scriptitem_T *si = SCRIPT_ITEM(sid);
340 int idx;
341
342 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
343 {
344 imported_T *imp = ((imported_T *)si->sn_imports.ga_data) + idx;
345
346 imp->imp_flags |= IMP_FLAGS_RELOAD;
347 }
348}
349
350/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100351 * Handle an ":import" command and add the resulting imported_T to "gap", when
352 * not NULL, or script "import_sid" sn_imports.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200353 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100354 * Returns a pointer to after the command or NULL in case of failure
355 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200356 static char_u *
Bram Moolenaar1c991142020-07-04 13:15:31 +0200357handle_import(
358 char_u *arg_start,
359 garray_T *gap,
360 int import_sid,
361 evalarg_T *evalarg,
362 void *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100363{
364 char_u *arg = arg_start;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200365 char_u *cmd_end = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100366 int ret = FAIL;
367 typval_T tv;
368 int sid = -1;
369 int res;
Bram Moolenaar0a842842021-02-27 22:41:19 +0100370 int mult = FALSE;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200371 garray_T names;
Bram Moolenaar0a842842021-02-27 22:41:19 +0100372 garray_T as_names;
Bram Moolenaar921ba522021-07-29 22:25:05 +0200373 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100374
Bram Moolenaar4db572e2021-07-18 18:21:38 +0200375 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200376 ga_init2(&names, sizeof(char_u *), 10);
Bram Moolenaar0a842842021-02-27 22:41:19 +0100377 ga_init2(&as_names, sizeof(char_u *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100378 if (*arg == '{')
379 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200380 // "import {item, item} from ..."
Bram Moolenaar0a842842021-02-27 22:41:19 +0100381 mult = TRUE;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200382 arg = skipwhite_and_linebreak(arg + 1, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100383 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200384
Bram Moolenaar0a842842021-02-27 22:41:19 +0100385 for (;;)
386 {
387 char_u *p = arg;
388 int had_comma = FALSE;
389 char_u *as_name = NULL;
390
391 // accept "*" or "Name"
392 if (!mult && arg[0] == '*' && IS_WHITE_OR_NUL(arg[1]))
393 ++arg;
394 else
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100395 while (eval_isnamec(*arg))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100396 ++arg;
Bram Moolenaar0a842842021-02-27 22:41:19 +0100397 if (p == arg)
398 break;
399 if (ga_grow(&names, 1) == FAIL || ga_grow(&as_names, 1) == FAIL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200400 goto erret;
Bram Moolenaar0a842842021-02-27 22:41:19 +0100401 ((char_u **)names.ga_data)[names.ga_len] = vim_strnsave(p, arg - p);
402 ++names.ga_len;
Bram Moolenaar1c991142020-07-04 13:15:31 +0200403
Bram Moolenaar0a842842021-02-27 22:41:19 +0100404 arg = skipwhite_and_linebreak(arg, evalarg);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200405 if (STRNCMP("as", arg, 2) == 0 && IS_WHITE_OR_NUL(arg[2]))
406 {
Bram Moolenaara9e3d562021-09-08 12:31:35 +0200407 // Skip over "as Name "; no line break allowed after "as".
408 // Do not allow for ':' and '#'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100409 arg = skipwhite(arg + 2);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200410 p = arg;
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100411 if (eval_isnamec1(*arg))
Bram Moolenaara9e3d562021-09-08 12:31:35 +0200412 while (ASCII_ISALNUM(*arg) || *arg == '_')
Bram Moolenaarfa29c8a2020-02-23 22:35:05 +0100413 ++arg;
Bram Moolenaara9e3d562021-09-08 12:31:35 +0200414 if (p == arg || !(IS_WHITE_OR_NUL(*arg)
415 || (mult && (*arg == ',' || *arg == '}'))))
416 {
417 semsg(_(e_syntax_error_in_import_str), p);
418 goto erret;
419 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100420 if (check_defined(p, arg - p, cctx, FALSE) == FAIL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200421 goto erret;
422 as_name = vim_strnsave(p, arg - p);
423 arg = skipwhite_and_linebreak(arg, evalarg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100424 }
425 else if (*arg_start == '*')
426 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200427 emsg(_(e_missing_as_after_star));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200428 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100429 }
Bram Moolenaar0a842842021-02-27 22:41:19 +0100430 // without "as Name" the as_names entry is NULL
431 ((char_u **)as_names.ga_data)[as_names.ga_len] = as_name;
432 ++as_names.ga_len;
433
434 if (!mult)
435 break;
436 if (*arg == ',')
437 {
438 had_comma = TRUE;
439 ++arg;
440 }
441 arg = skipwhite_and_linebreak(arg, evalarg);
442 if (*arg == '}')
443 {
444 ++arg;
445 break;
446 }
447 if (!had_comma)
448 {
449 emsg(_(e_missing_comma_in_import));
450 goto erret;
451 }
452 }
453 arg = skipwhite_and_linebreak(arg, evalarg);
454
455 if (names.ga_len == 0)
456 {
Bram Moolenaara9e3d562021-09-08 12:31:35 +0200457 semsg(_(e_syntax_error_in_import_str), arg_start);
Bram Moolenaar0a842842021-02-27 22:41:19 +0100458 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100459 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200460
461 if (STRNCMP("from", arg, 4) != 0 || !IS_WHITE_OR_NUL(arg[4]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100462 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200463 emsg(_(e_missing_from));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200464 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100465 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200466
Bram Moolenaar4db572e2021-07-18 18:21:38 +0200467 // The name of the file can be an expression, which must evaluate to a
468 // string.
Bram Moolenaar962d7212020-07-04 14:15:00 +0200469 arg = skipwhite_and_linebreak(arg + 4, evalarg);
Bram Moolenaar4db572e2021-07-18 18:21:38 +0200470 ret = eval0(arg, &tv, NULL, evalarg);
471 if (ret == FAIL)
472 goto erret;
473 if (tv.v_type != VAR_STRING
474 || tv.vval.v_string == NULL || *tv.vval.v_string == NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100475 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200476 emsg(_(e_invalid_string_after_from));
Bram Moolenaar1c991142020-07-04 13:15:31 +0200477 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100478 }
479 cmd_end = arg;
480
Bram Moolenaar921ba522021-07-29 22:25:05 +0200481 // Give error messages for the start of the line.
482 SOURCING_LNUM = start_lnum;
483
Bram Moolenaar1c991142020-07-04 13:15:31 +0200484 /*
485 * find script file
486 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100487 if (*tv.vval.v_string == '.')
488 {
489 size_t len;
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100490 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100491 char_u *tail = gettail(si->sn_name);
492 char_u *from_name;
493
494 // Relative to current script: "./name.vim", "../../name.vim".
495 len = STRLEN(si->sn_name) - STRLEN(tail) + STRLEN(tv.vval.v_string) + 2;
496 from_name = alloc((int)len);
497 if (from_name == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200498 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100499 vim_strncpy(from_name, si->sn_name, tail - si->sn_name);
500 add_pathsep(from_name);
501 STRCAT(from_name, tv.vval.v_string);
502 simplify_filename(from_name);
503
504 res = do_source(from_name, FALSE, DOSO_NONE, &sid);
505 vim_free(from_name);
506 }
507 else if (mch_isFullName(tv.vval.v_string))
508 {
509 // Absolute path: "/tmp/name.vim"
510 res = do_source(tv.vval.v_string, FALSE, DOSO_NONE, &sid);
511 }
512 else
513 {
514 size_t len = 7 + STRLEN(tv.vval.v_string) + 1;
515 char_u *from_name;
516
517 // Find file in "import" subdirs in 'runtimepath'.
518 from_name = alloc((int)len);
519 if (from_name == NULL)
520 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200521 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100522 }
523 vim_snprintf((char *)from_name, len, "import/%s", tv.vval.v_string);
524 res = source_in_path(p_rtp, from_name, DIP_NOAFTER, &sid);
525 vim_free(from_name);
526 }
527
528 if (res == FAIL || sid <= 0)
529 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200530 semsg(_(e_could_not_import_str), tv.vval.v_string);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200531 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100532 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100533
534 if (*arg_start == '*')
535 {
Bram Moolenaar0a842842021-02-27 22:41:19 +0100536 imported_T *imported;
537 char_u *as_name = ((char_u **)as_names.ga_data)[0];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100538
Bram Moolenaar0a842842021-02-27 22:41:19 +0100539 // "import * as That"
Bram Moolenaara6294952020-12-27 13:39:50 +0100540 imported = find_imported(as_name, STRLEN(as_name), cctx);
541 if (imported != NULL && imported->imp_sid == sid)
542 {
543 if (imported->imp_flags & IMP_FLAGS_RELOAD)
544 // import already defined on a previous script load
545 imported->imp_flags &= ~IMP_FLAGS_RELOAD;
546 else
547 {
548 semsg(_(e_name_already_defined_str), as_name);
549 goto erret;
550 }
551 }
552
553 imported = new_imported(gap != NULL ? gap
554 : &SCRIPT_ITEM(import_sid)->sn_imports);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100555 if (imported == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200556 goto erret;
557 imported->imp_name = as_name;
Bram Moolenaar0a842842021-02-27 22:41:19 +0100558 ((char_u **)as_names.ga_data)[0] = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100559 imported->imp_sid = sid;
Bram Moolenaara6294952020-12-27 13:39:50 +0100560 imported->imp_flags = IMP_FLAGS_STAR;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100561 }
562 else
563 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200564 int i;
565
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100566 arg = arg_start;
567 if (*arg == '{')
568 arg = skipwhite(arg + 1);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200569 for (i = 0; i < names.ga_len; ++i)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100570 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200571 char_u *name = ((char_u **)names.ga_data)[i];
Bram Moolenaar0a842842021-02-27 22:41:19 +0100572 char_u *as_name = ((char_u **)as_names.ga_data)[i];
Bram Moolenaara6294952020-12-27 13:39:50 +0100573 size_t len = STRLEN(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100574 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100575 imported_T *imported;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100576 ufunc_T *ufunc = NULL;
577 type_T *type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100578
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +0100579 idx = find_exported(sid, name, &ufunc, &type, cctx, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100580
Bram Moolenaarf2d5c242020-02-23 21:25:54 +0100581 if (idx < 0 && ufunc == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200582 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100583
Bram Moolenaarc967d572021-07-08 21:38:50 +0200584 // If already imported with the same properties and the
Bram Moolenaara6294952020-12-27 13:39:50 +0100585 // IMP_FLAGS_RELOAD set then we keep that entry. Otherwise create
586 // a new one (and give an error for an existing import).
587 imported = find_imported(name, len, cctx);
588 if (imported != NULL
589 && (imported->imp_flags & IMP_FLAGS_RELOAD)
590 && imported->imp_sid == sid
591 && (idx >= 0
Bram Moolenaar60dc8272021-07-29 22:48:54 +0200592 ? (equal_type(imported->imp_type, type, 0)
Bram Moolenaara6294952020-12-27 13:39:50 +0100593 && imported->imp_var_vals_idx == idx)
Bram Moolenaar60dc8272021-07-29 22:48:54 +0200594 : (equal_type(imported->imp_type, ufunc->uf_func_type,
595 ETYPE_ARG_UNKNOWN)
Bram Moolenaara6294952020-12-27 13:39:50 +0100596 && STRCMP(imported->imp_funcname,
597 ufunc->uf_name) == 0)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100598 {
Bram Moolenaara6294952020-12-27 13:39:50 +0100599 imported->imp_flags &= ~IMP_FLAGS_RELOAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100600 }
601 else
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200602 {
Bram Moolenaar6c7cc342021-04-17 16:38:50 +0200603 if (as_name == NULL
604 && check_defined(name, len, cctx, FALSE) == FAIL)
Bram Moolenaara6294952020-12-27 13:39:50 +0100605 goto erret;
606
607 imported = new_imported(gap != NULL ? gap
608 : &SCRIPT_ITEM(import_sid)->sn_imports);
609 if (imported == NULL)
610 goto erret;
611
Bram Moolenaar0a842842021-02-27 22:41:19 +0100612 if (as_name == NULL)
613 {
614 imported->imp_name = name;
615 ((char_u **)names.ga_data)[i] = NULL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100616 }
Bram Moolenaar0a842842021-02-27 22:41:19 +0100617 else
618 {
619 // "import This as That ..."
620 imported->imp_name = as_name;
621 ((char_u **)as_names.ga_data)[i] = NULL;
622 }
Bram Moolenaara6294952020-12-27 13:39:50 +0100623 imported->imp_sid = sid;
624 if (idx >= 0)
625 {
626 imported->imp_type = type;
627 imported->imp_var_vals_idx = idx;
628 }
629 else
630 {
631 imported->imp_type = ufunc->uf_func_type;
632 imported->imp_funcname = ufunc->uf_name;
633 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200634 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100635 }
636 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200637erret:
Bram Moolenaar4db572e2021-07-18 18:21:38 +0200638 clear_tv(&tv);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200639 ga_clear_strings(&names);
Bram Moolenaar0a842842021-02-27 22:41:19 +0100640 ga_clear_strings(&as_names);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100641 return cmd_end;
642}
643
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200644/*
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200645 * ":import Item from 'filename'"
646 * ":import Item as Alias from 'filename'"
647 * ":import {Item} from 'filename'".
648 * ":import {Item as Alias} from 'filename'"
649 * ":import {Item, Item} from 'filename'"
650 * ":import {Item, Item as Alias} from 'filename'"
651 *
652 * ":import * as Name from 'filename'"
653 */
654 void
655ex_import(exarg_T *eap)
656{
657 char_u *cmd_end;
658 evalarg_T evalarg;
659
660 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
661 {
662 emsg(_(e_import_can_only_be_used_in_script));
663 return;
664 }
665 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
666
667 cmd_end = handle_import(eap->arg, NULL, current_sctx.sc_sid,
668 &evalarg, NULL);
669 if (cmd_end != NULL)
670 set_nextcmd(eap, cmd_end);
671 clear_evalarg(&evalarg, eap);
672}
673
674/*
675 * Find an exported item in "sid" matching the name at "*argp".
676 * When it is a variable return the index.
677 * When it is a user function return "*ufunc".
678 * When not found returns -1 and "*ufunc" is NULL.
679 */
680 int
681find_exported(
682 int sid,
683 char_u *name,
684 ufunc_T **ufunc,
685 type_T **type,
686 cctx_T *cctx,
687 int verbose)
688{
689 int idx = -1;
690 svar_T *sv;
691 scriptitem_T *script = SCRIPT_ITEM(sid);
692
693 // Find name in "script".
694 idx = get_script_item_idx(sid, name, 0, cctx);
695 if (idx >= 0)
696 {
697 sv = ((svar_T *)script->sn_var_vals.ga_data) + idx;
698 if (!sv->sv_export)
699 {
700 if (verbose)
701 semsg(_(e_item_not_exported_in_script_str), name);
702 return -1;
703 }
704 *type = sv->sv_type;
705 *ufunc = NULL;
706 }
707 else
708 {
709 char_u buffer[200];
710 char_u *funcname;
711
712 // it could be a user function.
713 if (STRLEN(name) < sizeof(buffer) - 15)
714 funcname = buffer;
715 else
716 {
717 funcname = alloc(STRLEN(name) + 15);
718 if (funcname == NULL)
719 return -1;
720 }
721 funcname[0] = K_SPECIAL;
722 funcname[1] = KS_EXTRA;
723 funcname[2] = (int)KE_SNR;
724 sprintf((char *)funcname + 3, "%ld_%s", (long)sid, name);
725 *ufunc = find_func(funcname, FALSE, NULL);
726 if (funcname != buffer)
727 vim_free(funcname);
728
729 if (*ufunc == NULL)
730 {
731 if (verbose)
732 semsg(_(e_item_not_found_in_script_str), name);
733 return -1;
734 }
735 else if (((*ufunc)->uf_flags & FC_EXPORT) == 0)
736 {
737 if (verbose)
738 semsg(_(e_item_not_exported_in_script_str), name);
739 *ufunc = NULL;
740 return -1;
741 }
742 }
743
744 return idx;
745}
746
747/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200748 * Declare a script-local variable without init: "let var: type".
749 * "const" is an error since the value is missing.
750 * Returns a pointer to after the type.
751 */
752 char_u *
753vim9_declare_scriptvar(exarg_T *eap, char_u *arg)
754{
755 char_u *p;
756 char_u *name;
757 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
758 type_T *type;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200759 typval_T init_tv;
760
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200761 if (eap->cmdidx == CMD_final || eap->cmdidx == CMD_const)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200762 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200763 if (eap->cmdidx == CMD_final)
764 emsg(_(e_final_requires_a_value));
765 else
766 emsg(_(e_const_requires_a_value));
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200767 return arg + STRLEN(arg);
768 }
769
770 // Check for valid starting character.
771 if (!eval_isnamec1(*arg))
772 {
773 semsg(_(e_invarg2), arg);
774 return arg + STRLEN(arg);
775 }
776
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200777 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
Bram Moolenaar3b74b6b2020-06-19 19:01:43 +0200778 if (*p == ':' && (VIM_ISWHITE(p[1]) || p != arg + 1))
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200779 break;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200780
781 if (*p != ':')
782 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +0200783 emsg(_(e_type_or_initialization_required));
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200784 return arg + STRLEN(arg);
785 }
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200786 if (!VIM_ISWHITE(p[1]))
787 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100788 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200789 return arg + STRLEN(arg);
790 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200791 name = vim_strnsave(arg, p - arg);
792
Bram Moolenaard0edaf92021-05-28 21:06:08 +0200793 // parse type, check for reserved name
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200794 p = skipwhite(p + 1);
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100795 type = parse_type(&p, &si->sn_type_list, TRUE);
Bram Moolenaard0edaf92021-05-28 21:06:08 +0200796 if (type == NULL || check_reserved_name(name) == FAIL)
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200797 {
798 vim_free(name);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200799 return p;
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200800 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200801
802 // Create the variable with 0/NULL value.
803 CLEAR_FIELD(init_tv);
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +0200804 if (type->tt_type == VAR_ANY)
805 // A variable of type "any" is not possible, just use zero instead
806 init_tv.v_type = VAR_NUMBER;
807 else
808 init_tv.v_type = type->tt_type;
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100809 set_var_const(name, type, &init_tv, FALSE, 0, 0);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200810
811 vim_free(name);
812 return p;
813}
814
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200815/*
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200816 * Vim9 part of adding a script variable: add it to sn_all_vars (lookup by name
817 * with a hashtable) and sn_var_vals (lookup by index).
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100818 * When "create" is TRUE this is a new variable, otherwise find and update an
819 * existing variable.
Bram Moolenaar08251752021-01-11 21:20:18 +0100820 * "flags" can have ASSIGN_FINAL or ASSIGN_CONST.
Bram Moolenaarf2253962021-04-13 20:53:13 +0200821 * When "*type" is NULL use "tv" for the type and update "*type". If
822 * "do_member" is TRUE also use the member type, otherwise use "any".
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200823 */
824 void
Bram Moolenaar08251752021-01-11 21:20:18 +0100825update_vim9_script_var(
826 int create,
827 dictitem_T *di,
828 int flags,
829 typval_T *tv,
Bram Moolenaarf2253962021-04-13 20:53:13 +0200830 type_T **type,
831 int do_member)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200832{
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100833 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
834 hashitem_T *hi;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200835 svar_T *sv = NULL;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200836
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100837 if (create)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200838 {
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200839 sallvar_T *newsav;
840 sallvar_T *sav = NULL;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200841
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100842 // Store a pointer to the typval_T, so that it can be found by index
843 // instead of using a hastab lookup.
844 if (ga_grow(&si->sn_var_vals, 1) == FAIL)
845 return;
846
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200847 hi = hash_find(&si->sn_all_vars.dv_hashtab, di->di_key);
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200848 if (!HASHITEM_EMPTY(hi))
849 {
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200850 // Variable with this name exists, either in this block or in
851 // another block.
852 for (sav = HI2SAV(hi); ; sav = sav->sav_next)
853 {
854 if (sav->sav_block_id == si->sn_current_block_id)
855 {
856 // variable defined in a loop, re-use the entry
857 sv = ((svar_T *)si->sn_var_vals.ga_data)
858 + sav->sav_var_vals_idx;
859 // unhide the variable
860 if (sv->sv_tv == &sav->sav_tv)
861 {
862 clear_tv(&sav->sav_tv);
863 sv->sv_tv = &di->di_tv;
864 sav->sav_di = di;
865 }
866 break;
867 }
868 if (sav->sav_next == NULL)
869 break;
870 }
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200871 }
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200872
873 if (sv == NULL)
874 {
875 // Variable not defined or not defined in current block: Add a
876 // svar_T and create a new sallvar_T.
877 sv = ((svar_T *)si->sn_var_vals.ga_data) + si->sn_var_vals.ga_len;
878 newsav = (sallvar_T *)alloc_clear(
879 sizeof(sallvar_T) + STRLEN(di->di_key));
880 if (newsav == NULL)
881 return;
882
883 sv->sv_tv = &di->di_tv;
884 sv->sv_const = (flags & ASSIGN_FINAL) ? ASSIGN_FINAL
885 : (flags & ASSIGN_CONST) ? ASSIGN_CONST : 0;
886 sv->sv_export = is_export;
887 newsav->sav_var_vals_idx = si->sn_var_vals.ga_len;
888 ++si->sn_var_vals.ga_len;
889 STRCPY(&newsav->sav_key, di->di_key);
890 sv->sv_name = newsav->sav_key;
891 newsav->sav_di = di;
892 newsav->sav_block_id = si->sn_current_block_id;
893
894 if (HASHITEM_EMPTY(hi))
895 // new variable name
896 hash_add(&si->sn_all_vars.dv_hashtab, newsav->sav_key);
897 else if (sav != NULL)
898 // existing name in a new block, append to the list
899 sav->sav_next = newsav;
900 }
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200901 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100902 else
903 {
Bram Moolenaarc967d572021-07-08 21:38:50 +0200904 sv = find_typval_in_script(&di->di_tv);
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100905 }
906 if (sv != NULL)
907 {
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100908 if (*type == NULL)
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200909 *type = typval2type(tv, get_copyID(), &si->sn_type_list, do_member);
Bram Moolenaardd297bc2021-12-10 10:37:38 +0000910 if (sv->sv_type_allocated)
911 free_type(sv->sv_type);
912 if (*type != NULL && ((*type)->tt_type == VAR_FUNC
913 || (*type)->tt_type == VAR_PARTIAL))
914 {
915 // The type probably uses uf_type_list, which is cleared when the
916 // function is freed, but the script variable may keep the type.
917 // Make a copy to avoid using freed memory.
918 sv->sv_type = alloc_type(*type);
919 sv->sv_type_allocated = TRUE;
920 }
921 else
922 {
923 sv->sv_type = *type;
924 sv->sv_type_allocated = FALSE;
925 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100926 }
927
928 // let ex_export() know the export worked.
929 is_export = FALSE;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200930}
931
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200932/*
933 * Hide a script variable when leaving a block.
934 * "idx" is de index in sn_var_vals.
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200935 * When "func_defined" is non-zero then a function was defined in this block,
936 * the variable may be accessed by it. Otherwise the variable can be cleared.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200937 */
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200938 void
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200939hide_script_var(scriptitem_T *si, int idx, int func_defined)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200940{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200941 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200942 hashtab_T *script_ht = get_script_local_ht();
943 hashtab_T *all_ht = &si->sn_all_vars.dv_hashtab;
944 hashitem_T *script_hi;
945 hashitem_T *all_hi;
946
947 // Remove a variable declared inside the block, if it still exists.
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200948 // If it was added in a nested block it will already have been removed.
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200949 // The typval is moved into the sallvar_T.
950 script_hi = hash_find(script_ht, sv->sv_name);
951 all_hi = hash_find(all_ht, sv->sv_name);
952 if (!HASHITEM_EMPTY(script_hi) && !HASHITEM_EMPTY(all_hi))
953 {
954 dictitem_T *di = HI2DI(script_hi);
955 sallvar_T *sav = HI2SAV(all_hi);
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200956 sallvar_T *sav_prev = NULL;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200957
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200958 // There can be multiple entries with the same name in different
959 // blocks, find the right one.
960 while (sav != NULL && sav->sav_var_vals_idx != idx)
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200961 {
962 sav_prev = sav;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200963 sav = sav->sav_next;
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200964 }
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200965 if (sav != NULL)
966 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200967 if (func_defined)
968 {
969 // move the typval from the dictitem to the sallvar
970 sav->sav_tv = di->di_tv;
971 di->di_tv.v_type = VAR_UNKNOWN;
972 sav->sav_flags = di->di_flags;
973 sav->sav_di = NULL;
974 sv->sv_tv = &sav->sav_tv;
975 }
976 else
977 {
978 if (sav_prev == NULL)
979 hash_remove(all_ht, all_hi);
980 else
981 sav_prev->sav_next = sav->sav_next;
982 sv->sv_name = NULL;
983 vim_free(sav);
984 }
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200985 delete_var(script_ht, script_hi);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200986 }
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200987 }
988}
989
990/*
Bram Moolenaar10c65862020-10-08 21:16:42 +0200991 * Find the script-local variable that links to "dest".
Bram Moolenaarc967d572021-07-08 21:38:50 +0200992 * Returns NULL if not found and give an internal error.
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200993 */
Bram Moolenaar10c65862020-10-08 21:16:42 +0200994 svar_T *
Bram Moolenaarc967d572021-07-08 21:38:50 +0200995find_typval_in_script(typval_T *dest)
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200996{
997 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
998 int idx;
999
Bram Moolenaar81e17fb2020-08-21 21:55:43 +02001000 if (si->sn_version != SCRIPT_VERSION_VIM9)
1001 // legacy script doesn't store variable types
Bram Moolenaar10c65862020-10-08 21:16:42 +02001002 return NULL;
Bram Moolenaar81e17fb2020-08-21 21:55:43 +02001003
Bram Moolenaara06758d2021-10-15 00:18:37 +01001004 // Find the svar_T in sn_var_vals. Start at the end, in a for loop the
1005 // variable was added at the end.
1006 for (idx = si->sn_var_vals.ga_len - 1; idx >= 0; --idx)
Bram Moolenaar34db91f2020-06-13 19:00:10 +02001007 {
1008 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1009
Bram Moolenaarc0913d02020-12-05 14:44:37 +01001010 // If "sv_name" is NULL the variable was hidden when leaving a block,
1011 // don't check "sv_tv" then, it might be used for another variable now.
1012 if (sv->sv_name != NULL && sv->sv_tv == dest)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001013 return sv;
Bram Moolenaar34db91f2020-06-13 19:00:10 +02001014 }
Bram Moolenaarc967d572021-07-08 21:38:50 +02001015 iemsg("find_typval_in_script(): not found");
Bram Moolenaar10c65862020-10-08 21:16:42 +02001016 return NULL;
1017}
1018
1019/*
1020 * Check if the type of script variable "dest" allows assigning "value".
1021 * If needed convert "value" to a bool.
1022 */
1023 int
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001024check_script_var_type(
Bram Moolenaar7824fc82021-11-26 17:36:51 +00001025 svar_T *sv,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001026 typval_T *value,
1027 char_u *name,
1028 where_T where)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001029{
Bram Moolenaar10c65862020-10-08 21:16:42 +02001030 int ret;
1031
Bram Moolenaar7824fc82021-11-26 17:36:51 +00001032 if (sv->sv_const != 0)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001033 {
Bram Moolenaar7824fc82021-11-26 17:36:51 +00001034 semsg(_(e_cannot_change_readonly_variable_str), name);
1035 return FAIL;
Bram Moolenaar10c65862020-10-08 21:16:42 +02001036 }
Bram Moolenaar7824fc82021-11-26 17:36:51 +00001037 ret = check_typval_type(sv->sv_type, value, where);
1038 if (ret == OK && need_convert_to_bool(sv->sv_type, value))
1039 {
1040 int val = tv2bool(value);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001041
Bram Moolenaar7824fc82021-11-26 17:36:51 +00001042 clear_tv(value);
1043 value->v_type = VAR_BOOL;
1044 value->v_lock = 0;
1045 value->vval.v_number = val ? VVAL_TRUE : VVAL_FALSE;
1046 }
1047 return ret;
Bram Moolenaar34db91f2020-06-13 19:00:10 +02001048}
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001049
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001050// words that cannot be used as a variable
1051static char *reserved[] = {
1052 "true",
1053 "false",
1054 "null",
Bram Moolenaar74235772021-06-12 14:53:05 +02001055 "this",
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001056 NULL
1057};
1058
1059 int
1060check_reserved_name(char_u *name)
1061{
1062 int idx;
1063
1064 for (idx = 0; reserved[idx] != NULL; ++idx)
1065 if (STRCMP(reserved[idx], name) == 0)
1066 {
1067 semsg(_(e_cannot_use_reserved_name), name);
1068 return FAIL;
1069 }
1070 return OK;
1071}
1072
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001073#endif // FEAT_EVAL