blob: 3ebeab366ae5ebc814710f3c848a4c93555f5d5c [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
Dominique Pelle748b3082022-01-08 12:41:16 +0000118#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100119/*
Bram Moolenaarae616492020-07-28 20:07:27 +0200120 * When in Vim9 script give an error and return FAIL.
121 */
122 int
123not_in_vim9(exarg_T *eap)
124{
Bram Moolenaar68e30442020-07-28 21:15:07 +0200125 if (in_vim9script())
126 switch (eap->cmdidx)
127 {
Bram Moolenaarada1d872021-02-20 08:16:51 +0100128 case CMD_k:
129 if (eap->addr_count > 0)
130 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000131 emsg(_(e_no_range_allowed));
Bram Moolenaarada1d872021-02-20 08:16:51 +0100132 return FAIL;
133 }
134 // FALLTHROUGH
Bram Moolenaar68e30442020-07-28 21:15:07 +0200135 case CMD_append:
136 case CMD_change:
Bram Moolenaarf5a48012020-08-01 17:00:03 +0200137 case CMD_insert:
Bram Moolenaar65088802021-03-13 21:07:21 +0100138 case CMD_open:
Bram Moolenaarf5a48012020-08-01 17:00:03 +0200139 case CMD_t:
Bram Moolenaar68e30442020-07-28 21:15:07 +0200140 case CMD_xit:
Bram Moolenaar9b8d6222020-12-28 18:26:00 +0100141 semsg(_(e_command_not_supported_in_vim9_script_missing_var_str), eap->cmd);
Bram Moolenaar68e30442020-07-28 21:15:07 +0200142 return FAIL;
143 default: break;
144 }
Bram Moolenaarae616492020-07-28 20:07:27 +0200145 return OK;
146}
147
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100148/*
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100149 * Give an error message if "p" points at "#{" and return TRUE.
150 * This avoids that using a legacy style #{} dictionary leads to difficult to
151 * understand errors.
152 */
153 int
154vim9_bad_comment(char_u *p)
155{
Bram Moolenaara0399ef2021-03-20 15:00:01 +0100156 if (p[0] == '#' && p[1] == '{' && p[2] != '{')
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100157 {
158 emsg(_(e_cannot_use_hash_curly_to_start_comment));
159 return TRUE;
160 }
161 return FALSE;
162}
Dominique Pelle748b3082022-01-08 12:41:16 +0000163#endif
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100164
165/*
Bram Moolenaara0399ef2021-03-20 15:00:01 +0100166 * Return TRUE if "p" points at a "#" not followed by one '{'.
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100167 * Does not check for white space.
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100168 */
169 int
170vim9_comment_start(char_u *p)
171{
Bram Moolenaara0399ef2021-03-20 15:00:01 +0100172 return p[0] == '#' && (p[1] != '{' || p[2] == '{');
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100173}
174
Bram Moolenaar9b8d6222020-12-28 18:26:00 +0100175#if defined(FEAT_EVAL) || defined(PROTO)
176
Bram Moolenaarae616492020-07-28 20:07:27 +0200177/*
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200178 * "++nr" and "--nr" commands.
179 */
180 void
181ex_incdec(exarg_T *eap)
182{
183 char_u *cmd = eap->cmd;
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200184 char_u *nextcmd = eap->nextcmd;
185 size_t len = STRLEN(eap->cmd) + 8;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200186
Bram Moolenaar22480d12021-06-25 21:31:09 +0200187 if (VIM_ISWHITE(cmd[2]))
188 {
189 semsg(_(e_no_white_space_allowed_after_str_str),
190 eap->cmdidx == CMD_increment ? "++" : "--", eap->cmd);
191 return;
192 }
193
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200194 // This works like "nr += 1" or "nr -= 1".
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200195 // Add a '|' to avoid looking in the next line.
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200196 eap->cmd = alloc(len);
197 if (eap->cmd == NULL)
198 return;
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200199 vim_snprintf((char *)eap->cmd, len, "%s %c= 1 |", cmd + 2,
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200200 eap->cmdidx == CMD_increment ? '+' : '-');
201 eap->arg = eap->cmd;
202 eap->cmdidx = CMD_var;
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200203 eap->nextcmd = NULL;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200204 ex_let(eap);
205 vim_free(eap->cmd);
206 eap->cmd = cmd;
Bram Moolenaarf3d30842021-06-25 19:29:30 +0200207 eap->nextcmd = nextcmd;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +0200208}
209
210/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100211 * ":export let Name: type"
212 * ":export const Name: type"
213 * ":export def Name(..."
214 * ":export class Name ..."
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100215 */
216 void
Bram Moolenaar09689a02020-05-09 22:50:08 +0200217ex_export(exarg_T *eap)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100218{
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200219 if (!in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100220 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200221 emsg(_(e_export_can_only_be_used_in_vim9script));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100222 return;
223 }
224
225 eap->cmd = eap->arg;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +0100226 (void)find_ex_command(eap, NULL, lookup_scriptitem, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100227 switch (eap->cmdidx)
228 {
229 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200230 case CMD_var:
231 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100232 case CMD_const:
233 case CMD_def:
234 // case CMD_class:
235 is_export = TRUE;
236 do_cmdline(eap->cmd, eap->getline, eap->cookie,
237 DOCMD_VERBOSE + DOCMD_NOWAIT);
238
239 // The command will reset "is_export" when exporting an item.
240 if (is_export)
241 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200242 emsg(_(e_export_with_invalid_argument));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100243 is_export = FALSE;
244 }
245 break;
246 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200247 emsg(_(e_invalid_command_after_export));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100248 break;
249 }
250}
251
252/*
253 * Add a new imported item entry to the current script.
254 */
255 static imported_T *
256new_imported(garray_T *gap)
257{
258 if (ga_grow(gap, 1) == OK)
259 return ((imported_T *)gap->ga_data + gap->ga_len++);
260 return NULL;
261}
262
263/*
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200264 * Free the script variables from "sn_all_vars".
265 */
266 static void
267free_all_script_vars(scriptitem_T *si)
268{
269 int todo;
270 hashtab_T *ht = &si->sn_all_vars.dv_hashtab;
271 hashitem_T *hi;
272 sallvar_T *sav;
273 sallvar_T *sav_next;
Bram Moolenaardd297bc2021-12-10 10:37:38 +0000274 int idx;
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200275
276 hash_lock(ht);
277 todo = (int)ht->ht_used;
278 for (hi = ht->ht_array; todo > 0; ++hi)
279 {
280 if (!HASHITEM_EMPTY(hi))
281 {
282 --todo;
283
284 // Free the variable. Don't remove it from the hashtab, ht_array
285 // might change then. hash_clear() takes care of it later.
286 sav = HI2SAV(hi);
287 while (sav != NULL)
288 {
289 sav_next = sav->sav_next;
290 if (sav->sav_di == NULL)
291 clear_tv(&sav->sav_tv);
292 vim_free(sav);
293 sav = sav_next;
294 }
295 }
296 }
297 hash_clear(ht);
298 hash_init(ht);
299
Bram Moolenaardd297bc2021-12-10 10:37:38 +0000300 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
301 {
302 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
303
304 if (sv->sv_type_allocated)
305 free_type(sv->sv_type);
306 }
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200307 ga_clear(&si->sn_var_vals);
308
309 // existing commands using script variable indexes are no longer valid
310 si->sn_script_seq = current_sctx.sc_seq;
311}
312
313/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100314 * Free all imported items in script "sid".
315 */
316 void
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200317free_imports_and_script_vars(int sid)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100318{
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100319 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100320 int idx;
321
322 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
323 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100324 imported_T *imp = ((imported_T *)si->sn_imports.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100325
326 vim_free(imp->imp_name);
327 }
328 ga_clear(&si->sn_imports);
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200329
330 free_all_script_vars(si);
331
Bram Moolenaar6110e792020-07-08 19:35:21 +0200332 clear_type_list(&si->sn_type_list);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100333}
334
335/*
Bram Moolenaara6294952020-12-27 13:39:50 +0100336 * Mark all imports as possible to redefine. Used when a script is loaded
337 * again but not cleared.
338 */
339 void
340mark_imports_for_reload(int sid)
341{
342 scriptitem_T *si = SCRIPT_ITEM(sid);
343 int idx;
344
345 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
346 {
347 imported_T *imp = ((imported_T *)si->sn_imports.ga_data) + idx;
348
349 imp->imp_flags |= IMP_FLAGS_RELOAD;
350 }
351}
352
353/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100354 * Handle an ":import" command and add the resulting imported_T to "gap", when
355 * not NULL, or script "import_sid" sn_imports.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200356 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100357 * Returns a pointer to after the command or NULL in case of failure
358 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200359 static char_u *
Bram Moolenaar1c991142020-07-04 13:15:31 +0200360handle_import(
361 char_u *arg_start,
362 garray_T *gap,
363 int import_sid,
364 evalarg_T *evalarg,
365 void *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100366{
367 char_u *arg = arg_start;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000368 char_u *nextarg;
369 int getnext;
370 char_u *expr_end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100371 int ret = FAIL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000372 char_u *as_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100373 typval_T tv;
374 int sid = -1;
375 int res;
Bram Moolenaar921ba522021-07-29 22:25:05 +0200376 long start_lnum = SOURCING_LNUM;
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000377 garray_T *import_gap;
378 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100379
Bram Moolenaar4db572e2021-07-18 18:21:38 +0200380 // The name of the file can be an expression, which must evaluate to a
381 // string.
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000382 ret = eval0_retarg(arg, &tv, NULL, evalarg, &expr_end);
Bram Moolenaar4db572e2021-07-18 18:21:38 +0200383 if (ret == FAIL)
384 goto erret;
385 if (tv.v_type != VAR_STRING
386 || tv.vval.v_string == NULL || *tv.vval.v_string == NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100387 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000388 semsg(_(e_invalid_string_for_import_str), arg);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200389 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100390 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100391
Bram Moolenaar921ba522021-07-29 22:25:05 +0200392 // Give error messages for the start of the line.
393 SOURCING_LNUM = start_lnum;
394
Bram Moolenaar1c991142020-07-04 13:15:31 +0200395 /*
396 * find script file
397 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100398 if (*tv.vval.v_string == '.')
399 {
400 size_t len;
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100401 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100402 char_u *tail = gettail(si->sn_name);
403 char_u *from_name;
404
405 // Relative to current script: "./name.vim", "../../name.vim".
406 len = STRLEN(si->sn_name) - STRLEN(tail) + STRLEN(tv.vval.v_string) + 2;
407 from_name = alloc((int)len);
408 if (from_name == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200409 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100410 vim_strncpy(from_name, si->sn_name, tail - si->sn_name);
411 add_pathsep(from_name);
412 STRCAT(from_name, tv.vval.v_string);
413 simplify_filename(from_name);
414
415 res = do_source(from_name, FALSE, DOSO_NONE, &sid);
416 vim_free(from_name);
417 }
418 else if (mch_isFullName(tv.vval.v_string))
419 {
420 // Absolute path: "/tmp/name.vim"
421 res = do_source(tv.vval.v_string, FALSE, DOSO_NONE, &sid);
422 }
423 else
424 {
425 size_t len = 7 + STRLEN(tv.vval.v_string) + 1;
426 char_u *from_name;
427
428 // Find file in "import" subdirs in 'runtimepath'.
429 from_name = alloc((int)len);
430 if (from_name == NULL)
431 {
Bram Moolenaar1c991142020-07-04 13:15:31 +0200432 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100433 }
434 vim_snprintf((char *)from_name, len, "import/%s", tv.vval.v_string);
435 res = source_in_path(p_rtp, from_name, DIP_NOAFTER, &sid);
436 vim_free(from_name);
437 }
438
439 if (res == FAIL || sid <= 0)
440 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200441 semsg(_(e_could_not_import_str), tv.vval.v_string);
Bram Moolenaar1c991142020-07-04 13:15:31 +0200442 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100443 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100444
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000445 import_gap = gap != NULL ? gap : &SCRIPT_ITEM(import_sid)->sn_imports;
446 for (i = 0; i < import_gap->ga_len; ++i)
447 {
448 imported_T *import = (imported_T *)import_gap->ga_data + i;
449
450 if (import->imp_sid == sid)
451 {
452 if (import->imp_flags & IMP_FLAGS_RELOAD)
453 {
454 // encountering same script first ime on a reload is OK
455 import->imp_flags &= ~IMP_FLAGS_RELOAD;
456 break;
457 }
458 semsg(_(e_cannot_import_same_script_twice_str), tv.vval.v_string);
459 goto erret;
460 }
461 }
462
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000463 // Allow for the "as Name" to be in the next line.
464 nextarg = eval_next_non_blank(expr_end, evalarg, &getnext);
465 if (STRNCMP("as", nextarg, 2) == 0 && IS_WHITE_OR_NUL(nextarg[2]))
466 {
467 char_u *p;
468
469 if (getnext)
470 arg = eval_next_line(evalarg);
471 else
472 arg = nextarg;
473
474 // Skip over "as Name "; no line break allowed after "as".
475 // Do not allow for ':' and '#'.
476 arg = skipwhite(arg + 2);
477 p = arg;
478 if (eval_isnamec1(*arg))
479 while (ASCII_ISALNUM(*arg) || *arg == '_')
480 ++arg;
481 if (p == arg || !IS_WHITE_OR_NUL(*arg))
482 {
483 semsg(_(e_syntax_error_in_import_str), p);
484 goto erret;
485 }
486 as_name = vim_strnsave(p, arg - p);
487 arg = skipwhite(arg);
488 }
489 else
490 {
491 char_u *p = gettail(tv.vval.v_string);
492 char_u *end = (char_u *)strstr((char *)p, ".vim");
493
494 if (!ends_excmd2(arg_start, expr_end))
495 {
496 semsg(_(e_trailing_characters_str), expr_end);
497 goto erret;
498 }
Bram Moolenaar834d4182022-01-07 13:38:24 +0000499 if (end == NULL || end[4] != NUL)
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000500 {
Bram Moolenaar834d4182022-01-07 13:38:24 +0000501 semsg(_(e_imported_script_must_use_as_or_end_in_dot_vim_str), p);
502 goto erret;
503 }
504 if (end == p)
505 {
506 semsg(_(e_cannot_import_dot_vim_without_using_as), p);
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000507 goto erret;
508 }
509 as_name = vim_strnsave(p, end - p);
510 }
511
512 if (as_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100513 {
Bram Moolenaar0a842842021-02-27 22:41:19 +0100514 imported_T *imported;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100515
Bram Moolenaara6294952020-12-27 13:39:50 +0100516 imported = find_imported(as_name, STRLEN(as_name), cctx);
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000517 if (imported != NULL && imported->imp_sid != sid)
Bram Moolenaara6294952020-12-27 13:39:50 +0100518 {
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000519 semsg(_(e_name_already_defined_str), as_name);
520 goto erret;
Bram Moolenaara6294952020-12-27 13:39:50 +0100521 }
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000522 else if (imported == NULL
523 && check_defined(as_name, STRLEN(as_name), cctx, FALSE) == FAIL)
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000524 goto erret;
Bram Moolenaara6294952020-12-27 13:39:50 +0100525
Bram Moolenaar7c24dfd2022-01-08 17:03:55 +0000526 imported = new_imported(import_gap);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100527 if (imported == NULL)
Bram Moolenaar1c991142020-07-04 13:15:31 +0200528 goto erret;
529 imported->imp_name = as_name;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000530 as_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100531 imported->imp_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100532 }
Bram Moolenaar1c991142020-07-04 13:15:31 +0200533
Bram Moolenaar1c991142020-07-04 13:15:31 +0200534erret:
Bram Moolenaar4db572e2021-07-18 18:21:38 +0200535 clear_tv(&tv);
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000536 vim_free(as_name);
537 return arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100538}
539
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200540/*
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000541 * ":import 'filename'"
542 * ":import 'filename' as Name"
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200543 */
544 void
545ex_import(exarg_T *eap)
546{
547 char_u *cmd_end;
548 evalarg_T evalarg;
549
550 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
551 {
552 emsg(_(e_import_can_only_be_used_in_script));
553 return;
554 }
555 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
556
557 cmd_end = handle_import(eap->arg, NULL, current_sctx.sc_sid,
558 &evalarg, NULL);
559 if (cmd_end != NULL)
560 set_nextcmd(eap, cmd_end);
561 clear_evalarg(&evalarg, eap);
562}
563
564/*
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000565 * Find an exported item in "sid" matching "name".
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200566 * When it is a variable return the index.
567 * When it is a user function return "*ufunc".
568 * When not found returns -1 and "*ufunc" is NULL.
569 */
570 int
571find_exported(
572 int sid,
573 char_u *name,
574 ufunc_T **ufunc,
575 type_T **type,
576 cctx_T *cctx,
577 int verbose)
578{
579 int idx = -1;
580 svar_T *sv;
581 scriptitem_T *script = SCRIPT_ITEM(sid);
582
583 // Find name in "script".
584 idx = get_script_item_idx(sid, name, 0, cctx);
585 if (idx >= 0)
586 {
587 sv = ((svar_T *)script->sn_var_vals.ga_data) + idx;
Bram Moolenaara909c482022-01-06 22:07:57 +0000588 *ufunc = NULL;
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200589 if (!sv->sv_export)
590 {
591 if (verbose)
592 semsg(_(e_item_not_exported_in_script_str), name);
593 return -1;
594 }
595 *type = sv->sv_type;
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200596 }
597 else
598 {
599 char_u buffer[200];
600 char_u *funcname;
601
602 // it could be a user function.
603 if (STRLEN(name) < sizeof(buffer) - 15)
604 funcname = buffer;
605 else
606 {
607 funcname = alloc(STRLEN(name) + 15);
608 if (funcname == NULL)
609 return -1;
610 }
611 funcname[0] = K_SPECIAL;
612 funcname[1] = KS_EXTRA;
613 funcname[2] = (int)KE_SNR;
614 sprintf((char *)funcname + 3, "%ld_%s", (long)sid, name);
615 *ufunc = find_func(funcname, FALSE, NULL);
616 if (funcname != buffer)
617 vim_free(funcname);
618
619 if (*ufunc == NULL)
620 {
621 if (verbose)
622 semsg(_(e_item_not_found_in_script_str), name);
623 return -1;
624 }
625 else if (((*ufunc)->uf_flags & FC_EXPORT) == 0)
626 {
627 if (verbose)
628 semsg(_(e_item_not_exported_in_script_str), name);
629 *ufunc = NULL;
630 return -1;
631 }
632 }
633
634 return idx;
635}
636
637/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200638 * Declare a script-local variable without init: "let var: type".
639 * "const" is an error since the value is missing.
640 * Returns a pointer to after the type.
641 */
642 char_u *
643vim9_declare_scriptvar(exarg_T *eap, char_u *arg)
644{
645 char_u *p;
646 char_u *name;
647 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
648 type_T *type;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200649 typval_T init_tv;
650
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200651 if (eap->cmdidx == CMD_final || eap->cmdidx == CMD_const)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200652 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200653 if (eap->cmdidx == CMD_final)
654 emsg(_(e_final_requires_a_value));
655 else
656 emsg(_(e_const_requires_a_value));
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200657 return arg + STRLEN(arg);
658 }
659
660 // Check for valid starting character.
661 if (!eval_isnamec1(*arg))
662 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000663 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200664 return arg + STRLEN(arg);
665 }
666
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200667 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
Bram Moolenaar3b74b6b2020-06-19 19:01:43 +0200668 if (*p == ':' && (VIM_ISWHITE(p[1]) || p != arg + 1))
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200669 break;
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200670
671 if (*p != ':')
672 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +0200673 emsg(_(e_type_or_initialization_required));
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200674 return arg + STRLEN(arg);
675 }
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200676 if (!VIM_ISWHITE(p[1]))
677 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100678 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar984dddb2020-06-14 12:50:24 +0200679 return arg + STRLEN(arg);
680 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200681 name = vim_strnsave(arg, p - arg);
682
Bram Moolenaard0edaf92021-05-28 21:06:08 +0200683 // parse type, check for reserved name
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200684 p = skipwhite(p + 1);
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100685 type = parse_type(&p, &si->sn_type_list, TRUE);
Bram Moolenaard0edaf92021-05-28 21:06:08 +0200686 if (type == NULL || check_reserved_name(name) == FAIL)
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200687 {
688 vim_free(name);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200689 return p;
Bram Moolenaarf3decc52020-06-13 19:56:38 +0200690 }
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200691
692 // Create the variable with 0/NULL value.
693 CLEAR_FIELD(init_tv);
Bram Moolenaarf0afd9e2020-09-13 18:57:47 +0200694 if (type->tt_type == VAR_ANY)
695 // A variable of type "any" is not possible, just use zero instead
696 init_tv.v_type = VAR_NUMBER;
697 else
698 init_tv.v_type = type->tt_type;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000699 set_var_const(name, 0, type, &init_tv, FALSE, 0, 0);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200700
701 vim_free(name);
702 return p;
703}
704
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200705/*
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200706 * Vim9 part of adding a script variable: add it to sn_all_vars (lookup by name
707 * with a hashtable) and sn_var_vals (lookup by index).
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100708 * When "create" is TRUE this is a new variable, otherwise find and update an
709 * existing variable.
Bram Moolenaar08251752021-01-11 21:20:18 +0100710 * "flags" can have ASSIGN_FINAL or ASSIGN_CONST.
Bram Moolenaarf2253962021-04-13 20:53:13 +0200711 * When "*type" is NULL use "tv" for the type and update "*type". If
712 * "do_member" is TRUE also use the member type, otherwise use "any".
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200713 */
714 void
Bram Moolenaar08251752021-01-11 21:20:18 +0100715update_vim9_script_var(
716 int create,
717 dictitem_T *di,
718 int flags,
719 typval_T *tv,
Bram Moolenaarf2253962021-04-13 20:53:13 +0200720 type_T **type,
721 int do_member)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200722{
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100723 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
724 hashitem_T *hi;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200725 svar_T *sv = NULL;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200726
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100727 if (create)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200728 {
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200729 sallvar_T *newsav;
730 sallvar_T *sav = NULL;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200731
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100732 // Store a pointer to the typval_T, so that it can be found by index
733 // instead of using a hastab lookup.
734 if (ga_grow(&si->sn_var_vals, 1) == FAIL)
735 return;
736
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200737 hi = hash_find(&si->sn_all_vars.dv_hashtab, di->di_key);
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200738 if (!HASHITEM_EMPTY(hi))
739 {
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200740 // Variable with this name exists, either in this block or in
741 // another block.
742 for (sav = HI2SAV(hi); ; sav = sav->sav_next)
743 {
744 if (sav->sav_block_id == si->sn_current_block_id)
745 {
746 // variable defined in a loop, re-use the entry
747 sv = ((svar_T *)si->sn_var_vals.ga_data)
748 + sav->sav_var_vals_idx;
749 // unhide the variable
750 if (sv->sv_tv == &sav->sav_tv)
751 {
752 clear_tv(&sav->sav_tv);
753 sv->sv_tv = &di->di_tv;
754 sav->sav_di = di;
755 }
756 break;
757 }
758 if (sav->sav_next == NULL)
759 break;
760 }
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200761 }
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200762
763 if (sv == NULL)
764 {
765 // Variable not defined or not defined in current block: Add a
766 // svar_T and create a new sallvar_T.
767 sv = ((svar_T *)si->sn_var_vals.ga_data) + si->sn_var_vals.ga_len;
768 newsav = (sallvar_T *)alloc_clear(
769 sizeof(sallvar_T) + STRLEN(di->di_key));
770 if (newsav == NULL)
771 return;
772
773 sv->sv_tv = &di->di_tv;
774 sv->sv_const = (flags & ASSIGN_FINAL) ? ASSIGN_FINAL
775 : (flags & ASSIGN_CONST) ? ASSIGN_CONST : 0;
776 sv->sv_export = is_export;
777 newsav->sav_var_vals_idx = si->sn_var_vals.ga_len;
778 ++si->sn_var_vals.ga_len;
779 STRCPY(&newsav->sav_key, di->di_key);
780 sv->sv_name = newsav->sav_key;
781 newsav->sav_di = di;
782 newsav->sav_block_id = si->sn_current_block_id;
783
784 if (HASHITEM_EMPTY(hi))
785 // new variable name
786 hash_add(&si->sn_all_vars.dv_hashtab, newsav->sav_key);
787 else if (sav != NULL)
788 // existing name in a new block, append to the list
789 sav->sav_next = newsav;
790 }
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200791 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100792 else
793 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000794 sv = find_typval_in_script(&di->di_tv, 0);
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100795 }
796 if (sv != NULL)
797 {
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100798 if (*type == NULL)
Bram Moolenaar114dbda2022-01-03 12:28:03 +0000799 *type = typval2type(tv, get_copyID(), &si->sn_type_list,
800 do_member ? TVTT_DO_MEMBER : 0);
Bram Moolenaardd297bc2021-12-10 10:37:38 +0000801 if (sv->sv_type_allocated)
802 free_type(sv->sv_type);
803 if (*type != NULL && ((*type)->tt_type == VAR_FUNC
804 || (*type)->tt_type == VAR_PARTIAL))
805 {
806 // The type probably uses uf_type_list, which is cleared when the
807 // function is freed, but the script variable may keep the type.
808 // Make a copy to avoid using freed memory.
809 sv->sv_type = alloc_type(*type);
810 sv->sv_type_allocated = TRUE;
811 }
812 else
813 {
814 sv->sv_type = *type;
815 sv->sv_type_allocated = FALSE;
816 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100817 }
818
819 // let ex_export() know the export worked.
820 is_export = FALSE;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200821}
822
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200823/*
824 * Hide a script variable when leaving a block.
825 * "idx" is de index in sn_var_vals.
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200826 * When "func_defined" is non-zero then a function was defined in this block,
827 * the variable may be accessed by it. Otherwise the variable can be cleared.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200828 */
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200829 void
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200830hide_script_var(scriptitem_T *si, int idx, int func_defined)
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200831{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200832 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200833 hashtab_T *script_ht = get_script_local_ht();
834 hashtab_T *all_ht = &si->sn_all_vars.dv_hashtab;
835 hashitem_T *script_hi;
836 hashitem_T *all_hi;
837
838 // Remove a variable declared inside the block, if it still exists.
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200839 // If it was added in a nested block it will already have been removed.
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200840 // The typval is moved into the sallvar_T.
841 script_hi = hash_find(script_ht, sv->sv_name);
842 all_hi = hash_find(all_ht, sv->sv_name);
843 if (!HASHITEM_EMPTY(script_hi) && !HASHITEM_EMPTY(all_hi))
844 {
845 dictitem_T *di = HI2DI(script_hi);
846 sallvar_T *sav = HI2SAV(all_hi);
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200847 sallvar_T *sav_prev = NULL;
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200848
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200849 // There can be multiple entries with the same name in different
850 // blocks, find the right one.
851 while (sav != NULL && sav->sav_var_vals_idx != idx)
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200852 {
853 sav_prev = sav;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200854 sav = sav->sav_next;
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200855 }
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200856 if (sav != NULL)
857 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +0200858 if (func_defined)
859 {
860 // move the typval from the dictitem to the sallvar
861 sav->sav_tv = di->di_tv;
862 di->di_tv.v_type = VAR_UNKNOWN;
863 sav->sav_flags = di->di_flags;
864 sav->sav_di = NULL;
865 sv->sv_tv = &sav->sav_tv;
866 }
867 else
868 {
869 if (sav_prev == NULL)
870 hash_remove(all_ht, all_hi);
871 else
872 sav_prev->sav_next = sav->sav_next;
873 sv->sv_name = NULL;
874 vim_free(sav);
875 }
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200876 delete_var(script_ht, script_hi);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200877 }
Bram Moolenaar8d739de2020-10-14 19:39:19 +0200878 }
879}
880
881/*
Bram Moolenaar10c65862020-10-08 21:16:42 +0200882 * Find the script-local variable that links to "dest".
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000883 * If "sid" is zero use the current script.
Bram Moolenaarc967d572021-07-08 21:38:50 +0200884 * Returns NULL if not found and give an internal error.
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200885 */
Bram Moolenaar10c65862020-10-08 21:16:42 +0200886 svar_T *
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000887find_typval_in_script(typval_T *dest, scid_T sid)
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200888{
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000889 scriptitem_T *si = SCRIPT_ITEM(sid == 0 ? current_sctx.sc_sid : sid);
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200890 int idx;
891
Bram Moolenaar81e17fb2020-08-21 21:55:43 +0200892 if (si->sn_version != SCRIPT_VERSION_VIM9)
893 // legacy script doesn't store variable types
Bram Moolenaar10c65862020-10-08 21:16:42 +0200894 return NULL;
Bram Moolenaar81e17fb2020-08-21 21:55:43 +0200895
Bram Moolenaara06758d2021-10-15 00:18:37 +0100896 // Find the svar_T in sn_var_vals. Start at the end, in a for loop the
897 // variable was added at the end.
898 for (idx = si->sn_var_vals.ga_len - 1; idx >= 0; --idx)
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200899 {
900 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
901
Bram Moolenaarc0913d02020-12-05 14:44:37 +0100902 // If "sv_name" is NULL the variable was hidden when leaving a block,
903 // don't check "sv_tv" then, it might be used for another variable now.
904 if (sv->sv_name != NULL && sv->sv_tv == dest)
Bram Moolenaar10c65862020-10-08 21:16:42 +0200905 return sv;
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200906 }
Bram Moolenaarc967d572021-07-08 21:38:50 +0200907 iemsg("find_typval_in_script(): not found");
Bram Moolenaar10c65862020-10-08 21:16:42 +0200908 return NULL;
909}
910
911/*
912 * Check if the type of script variable "dest" allows assigning "value".
913 * If needed convert "value" to a bool.
914 */
915 int
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100916check_script_var_type(
Bram Moolenaar7824fc82021-11-26 17:36:51 +0000917 svar_T *sv,
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100918 typval_T *value,
919 char_u *name,
920 where_T where)
Bram Moolenaar10c65862020-10-08 21:16:42 +0200921{
Bram Moolenaar10c65862020-10-08 21:16:42 +0200922 int ret;
923
Bram Moolenaar7824fc82021-11-26 17:36:51 +0000924 if (sv->sv_const != 0)
Bram Moolenaar10c65862020-10-08 21:16:42 +0200925 {
Bram Moolenaar7824fc82021-11-26 17:36:51 +0000926 semsg(_(e_cannot_change_readonly_variable_str), name);
927 return FAIL;
Bram Moolenaar10c65862020-10-08 21:16:42 +0200928 }
Bram Moolenaar7824fc82021-11-26 17:36:51 +0000929 ret = check_typval_type(sv->sv_type, value, where);
930 if (ret == OK && need_convert_to_bool(sv->sv_type, value))
931 {
932 int val = tv2bool(value);
Bram Moolenaar10c65862020-10-08 21:16:42 +0200933
Bram Moolenaar7824fc82021-11-26 17:36:51 +0000934 clear_tv(value);
935 value->v_type = VAR_BOOL;
936 value->v_lock = 0;
937 value->vval.v_number = val ? VVAL_TRUE : VVAL_FALSE;
938 }
939 return ret;
Bram Moolenaar34db91f2020-06-13 19:00:10 +0200940}
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200941
Bram Moolenaard0edaf92021-05-28 21:06:08 +0200942// words that cannot be used as a variable
943static char *reserved[] = {
944 "true",
945 "false",
946 "null",
Bram Moolenaar74235772021-06-12 14:53:05 +0200947 "this",
Bram Moolenaard0edaf92021-05-28 21:06:08 +0200948 NULL
949};
950
951 int
952check_reserved_name(char_u *name)
953{
954 int idx;
955
956 for (idx = 0; reserved[idx] != NULL; ++idx)
957 if (STRCMP(reserved[idx], name) == 0)
958 {
959 semsg(_(e_cannot_use_reserved_name), name);
960 return FAIL;
961 }
962 return OK;
963}
964
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100965#endif // FEAT_EVAL