2020-01-26 15:56:19 +01:00
|
|
|
/* vi:set ts=8 sts=4 sw=4 noet:
|
|
|
|
*
|
|
|
|
* VIM - Vi IMproved by Bram Moolenaar
|
|
|
|
*
|
|
|
|
* Do ":help uganda" in Vim to read copying and usage conditions.
|
|
|
|
* Do ":help credits" in Vim to see a list of people who contributed.
|
|
|
|
* See README.txt for an overview of the Vim source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* vim9script.c: :vim9script, :import, :export and friends
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "vim.h"
|
|
|
|
|
|
|
|
#if defined(FEAT_EVAL) || defined(PROTO)
|
|
|
|
|
|
|
|
#include "vim9.h"
|
|
|
|
|
|
|
|
int
|
|
|
|
in_vim9script(void)
|
|
|
|
{
|
2020-07-12 17:07:05 +02:00
|
|
|
// Do not go up the stack, a ":function" inside vim9script uses legacy
|
|
|
|
// syntax. "sc_version" is also set when compiling a ":def" function in
|
|
|
|
// legacy script.
|
2020-01-26 15:56:19 +01:00
|
|
|
return current_sctx.sc_version == SCRIPT_VERSION_VIM9;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ":vim9script".
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
ex_vim9script(exarg_T *eap)
|
|
|
|
{
|
2020-06-16 23:18:51 +02:00
|
|
|
scriptitem_T *si;
|
2020-01-26 15:56:19 +01:00
|
|
|
|
|
|
|
if (!getline_equal(eap->getline, eap->cookie, getsourceline))
|
|
|
|
{
|
2020-08-15 16:33:28 +02:00
|
|
|
emsg(_(e_vim9script_can_only_be_used_in_script));
|
2020-01-26 15:56:19 +01:00
|
|
|
return;
|
|
|
|
}
|
2020-06-16 23:18:51 +02:00
|
|
|
si = SCRIPT_ITEM(current_sctx.sc_sid);
|
2020-01-26 15:56:19 +01:00
|
|
|
if (si->sn_had_command)
|
|
|
|
{
|
2020-08-15 16:33:28 +02:00
|
|
|
emsg(_(e_vim9script_must_be_first_command_in_script));
|
2020-01-26 15:56:19 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
current_sctx.sc_version = SCRIPT_VERSION_VIM9;
|
|
|
|
si->sn_version = SCRIPT_VERSION_VIM9;
|
|
|
|
si->sn_had_command = TRUE;
|
|
|
|
|
|
|
|
if (STRCMP(p_cpo, CPO_VIM) != 0)
|
|
|
|
{
|
|
|
|
si->sn_save_cpo = p_cpo;
|
|
|
|
p_cpo = vim_strsave((char_u *)CPO_VIM);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-28 20:07:27 +02:00
|
|
|
/*
|
|
|
|
* When in Vim9 script give an error and return FAIL.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
not_in_vim9(exarg_T *eap)
|
|
|
|
{
|
2020-07-28 21:15:07 +02:00
|
|
|
if (in_vim9script())
|
|
|
|
switch (eap->cmdidx)
|
|
|
|
{
|
|
|
|
case CMD_append:
|
|
|
|
case CMD_change:
|
2020-08-01 17:00:03 +02:00
|
|
|
case CMD_insert:
|
|
|
|
case CMD_t:
|
2020-07-28 21:15:07 +02:00
|
|
|
case CMD_xit:
|
2020-08-15 16:33:28 +02:00
|
|
|
semsg(_(e_missing_let_str), eap->cmd);
|
2020-07-28 21:15:07 +02:00
|
|
|
return FAIL;
|
|
|
|
default: break;
|
|
|
|
}
|
2020-07-28 20:07:27 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2020-01-26 15:56:19 +01:00
|
|
|
/*
|
|
|
|
* ":export let Name: type"
|
|
|
|
* ":export const Name: type"
|
|
|
|
* ":export def Name(..."
|
|
|
|
* ":export class Name ..."
|
|
|
|
*/
|
|
|
|
void
|
2020-05-09 22:50:08 +02:00
|
|
|
ex_export(exarg_T *eap)
|
2020-01-26 15:56:19 +01:00
|
|
|
{
|
2020-07-12 17:07:05 +02:00
|
|
|
if (!in_vim9script())
|
2020-01-26 15:56:19 +01:00
|
|
|
{
|
2020-08-15 16:33:28 +02:00
|
|
|
emsg(_(e_export_can_only_be_used_in_vim9script));
|
2020-01-26 15:56:19 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
eap->cmd = eap->arg;
|
|
|
|
(void)find_ex_command(eap, NULL, lookup_scriptvar, NULL);
|
|
|
|
switch (eap->cmdidx)
|
|
|
|
{
|
|
|
|
case CMD_let:
|
|
|
|
case CMD_const:
|
|
|
|
case CMD_def:
|
|
|
|
// case CMD_class:
|
|
|
|
is_export = TRUE;
|
|
|
|
do_cmdline(eap->cmd, eap->getline, eap->cookie,
|
|
|
|
DOCMD_VERBOSE + DOCMD_NOWAIT);
|
|
|
|
|
|
|
|
// The command will reset "is_export" when exporting an item.
|
|
|
|
if (is_export)
|
|
|
|
{
|
2020-08-15 16:33:28 +02:00
|
|
|
emsg(_(e_export_with_invalid_argument));
|
2020-01-26 15:56:19 +01:00
|
|
|
is_export = FALSE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2020-08-15 16:33:28 +02:00
|
|
|
emsg(_(e_invalid_command_after_export));
|
2020-01-26 15:56:19 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add a new imported item entry to the current script.
|
|
|
|
*/
|
|
|
|
static imported_T *
|
|
|
|
new_imported(garray_T *gap)
|
|
|
|
{
|
|
|
|
if (ga_grow(gap, 1) == OK)
|
|
|
|
return ((imported_T *)gap->ga_data + gap->ga_len++);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free all imported items in script "sid".
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
free_imports(int sid)
|
|
|
|
{
|
2020-01-26 19:26:46 +01:00
|
|
|
scriptitem_T *si = SCRIPT_ITEM(sid);
|
2020-01-26 15:56:19 +01:00
|
|
|
int idx;
|
|
|
|
|
|
|
|
for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
|
|
|
|
{
|
2020-03-20 18:39:46 +01:00
|
|
|
imported_T *imp = ((imported_T *)si->sn_imports.ga_data) + idx;
|
2020-01-26 15:56:19 +01:00
|
|
|
|
|
|
|
vim_free(imp->imp_name);
|
|
|
|
}
|
|
|
|
ga_clear(&si->sn_imports);
|
2020-03-20 18:39:46 +01:00
|
|
|
ga_clear(&si->sn_var_vals);
|
2020-07-08 19:35:21 +02:00
|
|
|
clear_type_list(&si->sn_type_list);
|
2020-01-26 15:56:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ":import Item from 'filename'"
|
|
|
|
* ":import Item as Alias from 'filename'"
|
|
|
|
* ":import {Item} from 'filename'".
|
|
|
|
* ":import {Item as Alias} from 'filename'"
|
|
|
|
* ":import {Item, Item} from 'filename'"
|
|
|
|
* ":import {Item, Item as Alias} from 'filename'"
|
|
|
|
*
|
|
|
|
* ":import * as Name from 'filename'"
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
ex_import(exarg_T *eap)
|
|
|
|
{
|
2020-07-04 13:15:31 +02:00
|
|
|
char_u *cmd_end;
|
|
|
|
evalarg_T evalarg;
|
2020-01-26 15:56:19 +01:00
|
|
|
|
2020-06-16 23:18:51 +02:00
|
|
|
if (!getline_equal(eap->getline, eap->cookie, getsourceline))
|
|
|
|
{
|
2020-08-15 16:33:28 +02:00
|
|
|
emsg(_(e_import_can_only_be_used_in_script));
|
2020-06-16 23:18:51 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-07-04 13:15:31 +02:00
|
|
|
fill_evalarg_from_eap(&evalarg, eap, eap->skip);
|
2020-06-16 23:18:51 +02:00
|
|
|
|
2020-07-04 13:15:31 +02:00
|
|
|
cmd_end = handle_import(eap->arg, NULL, current_sctx.sc_sid,
|
|
|
|
&evalarg, NULL);
|
2020-06-11 23:10:46 +02:00
|
|
|
if (cmd_end != NULL)
|
|
|
|
eap->nextcmd = check_nextcmd(cmd_end);
|
2020-07-04 13:15:31 +02:00
|
|
|
clear_evalarg(&evalarg, eap);
|
2020-01-26 15:56:19 +01:00
|
|
|
}
|
|
|
|
|
2020-02-23 21:25:54 +01:00
|
|
|
/*
|
|
|
|
* Find an exported item in "sid" matching the name at "*argp".
|
|
|
|
* When it is a variable return the index.
|
|
|
|
* When it is a user function return "*ufunc".
|
|
|
|
* When not found returns -1 and "*ufunc" is NULL.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
find_exported(
|
|
|
|
int sid,
|
2020-07-04 13:15:31 +02:00
|
|
|
char_u *name,
|
2020-02-23 21:25:54 +01:00
|
|
|
ufunc_T **ufunc,
|
|
|
|
type_T **type)
|
|
|
|
{
|
|
|
|
int idx = -1;
|
|
|
|
svar_T *sv;
|
|
|
|
scriptitem_T *script = SCRIPT_ITEM(sid);
|
|
|
|
|
|
|
|
// find name in "script"
|
|
|
|
// TODO: also find script-local user function
|
|
|
|
idx = get_script_item_idx(sid, name, FALSE);
|
|
|
|
if (idx >= 0)
|
|
|
|
{
|
|
|
|
sv = ((svar_T *)script->sn_var_vals.ga_data) + idx;
|
|
|
|
if (!sv->sv_export)
|
|
|
|
{
|
2020-08-15 16:33:28 +02:00
|
|
|
semsg(_(e_item_not_exported_in_script_str), name);
|
2020-02-23 21:25:54 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*type = sv->sv_type;
|
|
|
|
*ufunc = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char_u buffer[200];
|
|
|
|
char_u *funcname;
|
|
|
|
|
|
|
|
// it could be a user function.
|
2020-07-23 14:39:47 +02:00
|
|
|
if (STRLEN(name) < sizeof(buffer) - 15)
|
2020-02-23 21:25:54 +01:00
|
|
|
funcname = buffer;
|
|
|
|
else
|
|
|
|
{
|
2020-07-23 14:39:47 +02:00
|
|
|
funcname = alloc(STRLEN(name) + 15);
|
2020-02-23 21:25:54 +01:00
|
|
|
if (funcname == NULL)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
funcname[0] = K_SPECIAL;
|
|
|
|
funcname[1] = KS_EXTRA;
|
|
|
|
funcname[2] = (int)KE_SNR;
|
|
|
|
sprintf((char *)funcname + 3, "%ld_%s", (long)sid, name);
|
2020-04-27 22:47:51 +02:00
|
|
|
*ufunc = find_func(funcname, FALSE, NULL);
|
2020-02-23 21:25:54 +01:00
|
|
|
if (funcname != buffer)
|
|
|
|
vim_free(funcname);
|
|
|
|
|
|
|
|
if (*ufunc == NULL)
|
|
|
|
{
|
2020-08-15 16:33:28 +02:00
|
|
|
semsg(_(e_item_not_found_in_script_str), name);
|
2020-02-23 21:25:54 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
2020-01-26 15:56:19 +01:00
|
|
|
/*
|
|
|
|
* Handle an ":import" command and add the resulting imported_T to "gap", when
|
|
|
|
* not NULL, or script "import_sid" sn_imports.
|
|
|
|
* Returns a pointer to after the command or NULL in case of failure
|
|
|
|
*/
|
|
|
|
char_u *
|
2020-07-04 13:15:31 +02:00
|
|
|
handle_import(
|
|
|
|
char_u *arg_start,
|
|
|
|
garray_T *gap,
|
|
|
|
int import_sid,
|
|
|
|
evalarg_T *evalarg,
|
|
|
|
void *cctx)
|
2020-01-26 15:56:19 +01:00
|
|
|
{
|
|
|
|
char_u *arg = arg_start;
|
2020-07-04 13:15:31 +02:00
|
|
|
char_u *cmd_end = NULL;
|
|
|
|
char_u *as_name = NULL;
|
2020-01-26 15:56:19 +01:00
|
|
|
int ret = FAIL;
|
|
|
|
typval_T tv;
|
|
|
|
int sid = -1;
|
|
|
|
int res;
|
2020-07-04 13:15:31 +02:00
|
|
|
garray_T names;
|
2020-01-26 15:56:19 +01:00
|
|
|
|
2020-07-04 13:15:31 +02:00
|
|
|
ga_init2(&names, sizeof(char_u *), 10);
|
2020-01-26 15:56:19 +01:00
|
|
|
if (*arg == '{')
|
|
|
|
{
|
2020-07-04 13:15:31 +02:00
|
|
|
// "import {item, item} from ..."
|
|
|
|
arg = skipwhite_and_linebreak(arg + 1, evalarg);
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
char_u *p = arg;
|
|
|
|
int had_comma = FALSE;
|
|
|
|
|
|
|
|
while (eval_isnamec(*arg))
|
|
|
|
++arg;
|
|
|
|
if (p == arg)
|
|
|
|
break;
|
|
|
|
if (ga_grow(&names, 1) == FAIL)
|
|
|
|
goto erret;
|
|
|
|
((char_u **)names.ga_data)[names.ga_len] =
|
|
|
|
vim_strnsave(p, arg - p);
|
|
|
|
++names.ga_len;
|
|
|
|
if (*arg == ',')
|
|
|
|
{
|
|
|
|
had_comma = TRUE;
|
|
|
|
++arg;
|
|
|
|
}
|
|
|
|
arg = skipwhite_and_linebreak(arg, evalarg);
|
|
|
|
if (*arg == '}')
|
|
|
|
{
|
|
|
|
arg = skipwhite_and_linebreak(arg + 1, evalarg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!had_comma)
|
|
|
|
{
|
2020-08-15 16:33:28 +02:00
|
|
|
emsg(_(e_missing_comma_in_import));
|
2020-07-04 13:15:31 +02:00
|
|
|
goto erret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (names.ga_len == 0)
|
|
|
|
{
|
2020-08-15 16:33:28 +02:00
|
|
|
emsg(_(e_syntax_error_in_import));
|
2020-07-04 13:15:31 +02:00
|
|
|
goto erret;
|
|
|
|
}
|
2020-01-26 15:56:19 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-07-04 13:15:31 +02:00
|
|
|
// "import Name from ..."
|
|
|
|
// "import * as Name from ..."
|
|
|
|
// "import item [as Name] from ..."
|
|
|
|
arg = skipwhite_and_linebreak(arg, evalarg);
|
|
|
|
if (arg[0] == '*' && IS_WHITE_OR_NUL(arg[1]))
|
|
|
|
arg = skipwhite_and_linebreak(arg + 1, evalarg);
|
2020-02-23 22:35:05 +01:00
|
|
|
else if (eval_isnamec1(*arg))
|
2020-01-26 15:56:19 +01:00
|
|
|
{
|
2020-07-04 13:15:31 +02:00
|
|
|
char_u *p = arg;
|
|
|
|
|
2020-02-23 22:35:05 +01:00
|
|
|
while (eval_isnamec(*arg))
|
2020-01-26 15:56:19 +01:00
|
|
|
++arg;
|
2020-07-04 13:15:31 +02:00
|
|
|
if (ga_grow(&names, 1) == FAIL)
|
|
|
|
goto erret;
|
|
|
|
((char_u **)names.ga_data)[names.ga_len] =
|
|
|
|
vim_strnsave(p, arg - p);
|
|
|
|
++names.ga_len;
|
|
|
|
arg = skipwhite_and_linebreak(arg, evalarg);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-08-15 16:33:28 +02:00
|
|
|
emsg(_(e_syntax_error_in_import));
|
2020-07-04 13:15:31 +02:00
|
|
|
goto erret;
|
2020-01-26 15:56:19 +01:00
|
|
|
}
|
2020-07-04 13:15:31 +02:00
|
|
|
|
|
|
|
if (STRNCMP("as", arg, 2) == 0 && IS_WHITE_OR_NUL(arg[2]))
|
2020-01-26 15:56:19 +01:00
|
|
|
{
|
2020-07-04 13:15:31 +02:00
|
|
|
char_u *p;
|
|
|
|
|
|
|
|
// skip over "as Name "; no line break allowed after "as"
|
2020-01-26 15:56:19 +01:00
|
|
|
arg = skipwhite(arg + 2);
|
2020-07-04 13:15:31 +02:00
|
|
|
p = arg;
|
2020-02-23 22:35:05 +01:00
|
|
|
if (eval_isnamec1(*arg))
|
|
|
|
while (eval_isnamec(*arg))
|
|
|
|
++arg;
|
2020-07-06 21:53:17 +02:00
|
|
|
if (check_defined(p, arg - p, cctx) == FAIL)
|
2020-07-04 13:15:31 +02:00
|
|
|
goto erret;
|
|
|
|
as_name = vim_strnsave(p, arg - p);
|
|
|
|
arg = skipwhite_and_linebreak(arg, evalarg);
|
2020-01-26 15:56:19 +01:00
|
|
|
}
|
|
|
|
else if (*arg_start == '*')
|
|
|
|
{
|
2020-08-15 16:33:28 +02:00
|
|
|
emsg(_(e_missing_as_after_star));
|
2020-07-04 13:15:31 +02:00
|
|
|
goto erret;
|
2020-01-26 15:56:19 +01:00
|
|
|
}
|
|
|
|
}
|
2020-07-04 13:15:31 +02:00
|
|
|
|
|
|
|
if (STRNCMP("from", arg, 4) != 0 || !IS_WHITE_OR_NUL(arg[4]))
|
2020-01-26 15:56:19 +01:00
|
|
|
{
|
2020-08-15 16:33:28 +02:00
|
|
|
emsg(_(e_missing_from));
|
2020-07-04 13:15:31 +02:00
|
|
|
goto erret;
|
2020-01-26 15:56:19 +01:00
|
|
|
}
|
2020-07-04 13:15:31 +02:00
|
|
|
|
2020-07-04 14:15:00 +02:00
|
|
|
arg = skipwhite_and_linebreak(arg + 4, evalarg);
|
2020-01-26 15:56:19 +01:00
|
|
|
tv.v_type = VAR_UNKNOWN;
|
|
|
|
// TODO: should we accept any expression?
|
|
|
|
if (*arg == '\'')
|
2020-07-01 18:29:55 +02:00
|
|
|
ret = eval_lit_string(&arg, &tv, TRUE);
|
2020-01-26 15:56:19 +01:00
|
|
|
else if (*arg == '"')
|
2020-07-01 18:29:55 +02:00
|
|
|
ret = eval_string(&arg, &tv, TRUE);
|
2020-01-26 15:56:19 +01:00
|
|
|
if (ret == FAIL || tv.vval.v_string == NULL || *tv.vval.v_string == NUL)
|
|
|
|
{
|
2020-08-15 16:33:28 +02:00
|
|
|
emsg(_(e_invalid_string_after_from));
|
2020-07-04 13:15:31 +02:00
|
|
|
goto erret;
|
2020-01-26 15:56:19 +01:00
|
|
|
}
|
|
|
|
cmd_end = arg;
|
|
|
|
|
2020-07-04 13:15:31 +02:00
|
|
|
/*
|
|
|
|
* find script file
|
|
|
|
*/
|
2020-01-26 15:56:19 +01:00
|
|
|
if (*tv.vval.v_string == '.')
|
|
|
|
{
|
|
|
|
size_t len;
|
2020-01-26 19:26:46 +01:00
|
|
|
scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
|
2020-01-26 15:56:19 +01:00
|
|
|
char_u *tail = gettail(si->sn_name);
|
|
|
|
char_u *from_name;
|
|
|
|
|
|
|
|
// Relative to current script: "./name.vim", "../../name.vim".
|
|
|
|
len = STRLEN(si->sn_name) - STRLEN(tail) + STRLEN(tv.vval.v_string) + 2;
|
|
|
|
from_name = alloc((int)len);
|
|
|
|
if (from_name == NULL)
|
|
|
|
{
|
|
|
|
clear_tv(&tv);
|
2020-07-04 13:15:31 +02:00
|
|
|
goto erret;
|
2020-01-26 15:56:19 +01:00
|
|
|
}
|
|
|
|
vim_strncpy(from_name, si->sn_name, tail - si->sn_name);
|
|
|
|
add_pathsep(from_name);
|
|
|
|
STRCAT(from_name, tv.vval.v_string);
|
|
|
|
simplify_filename(from_name);
|
|
|
|
|
|
|
|
res = do_source(from_name, FALSE, DOSO_NONE, &sid);
|
|
|
|
vim_free(from_name);
|
|
|
|
}
|
|
|
|
else if (mch_isFullName(tv.vval.v_string))
|
|
|
|
{
|
|
|
|
// Absolute path: "/tmp/name.vim"
|
|
|
|
res = do_source(tv.vval.v_string, FALSE, DOSO_NONE, &sid);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
size_t len = 7 + STRLEN(tv.vval.v_string) + 1;
|
|
|
|
char_u *from_name;
|
|
|
|
|
|
|
|
// Find file in "import" subdirs in 'runtimepath'.
|
|
|
|
from_name = alloc((int)len);
|
|
|
|
if (from_name == NULL)
|
|
|
|
{
|
|
|
|
clear_tv(&tv);
|
2020-07-04 13:15:31 +02:00
|
|
|
goto erret;
|
2020-01-26 15:56:19 +01:00
|
|
|
}
|
|
|
|
vim_snprintf((char *)from_name, len, "import/%s", tv.vval.v_string);
|
|
|
|
res = source_in_path(p_rtp, from_name, DIP_NOAFTER, &sid);
|
|
|
|
vim_free(from_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res == FAIL || sid <= 0)
|
|
|
|
{
|
2020-08-15 16:33:28 +02:00
|
|
|
semsg(_(e_could_not_import_str), tv.vval.v_string);
|
2020-01-26 15:56:19 +01:00
|
|
|
clear_tv(&tv);
|
2020-07-04 13:15:31 +02:00
|
|
|
goto erret;
|
2020-01-26 15:56:19 +01:00
|
|
|
}
|
|
|
|
clear_tv(&tv);
|
|
|
|
|
|
|
|
if (*arg_start == '*')
|
|
|
|
{
|
|
|
|
imported_T *imported = new_imported(gap != NULL ? gap
|
2020-01-26 19:26:46 +01:00
|
|
|
: &SCRIPT_ITEM(import_sid)->sn_imports);
|
2020-01-26 15:56:19 +01:00
|
|
|
|
|
|
|
if (imported == NULL)
|
2020-07-04 13:15:31 +02:00
|
|
|
goto erret;
|
|
|
|
imported->imp_name = as_name;
|
|
|
|
as_name = NULL;
|
2020-01-26 15:56:19 +01:00
|
|
|
imported->imp_sid = sid;
|
|
|
|
imported->imp_all = TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-07-04 13:15:31 +02:00
|
|
|
int i;
|
|
|
|
|
2020-01-26 15:56:19 +01:00
|
|
|
arg = arg_start;
|
|
|
|
if (*arg == '{')
|
|
|
|
arg = skipwhite(arg + 1);
|
2020-07-04 13:15:31 +02:00
|
|
|
for (i = 0; i < names.ga_len; ++i)
|
2020-01-26 15:56:19 +01:00
|
|
|
{
|
2020-07-04 13:15:31 +02:00
|
|
|
char_u *name = ((char_u **)names.ga_data)[i];
|
2020-01-26 15:56:19 +01:00
|
|
|
int idx;
|
|
|
|
imported_T *imported;
|
2020-02-23 21:25:54 +01:00
|
|
|
ufunc_T *ufunc = NULL;
|
|
|
|
type_T *type;
|
2020-01-26 15:56:19 +01:00
|
|
|
|
2020-07-04 13:15:31 +02:00
|
|
|
idx = find_exported(sid, name, &ufunc, &type);
|
2020-01-26 15:56:19 +01:00
|
|
|
|
2020-02-23 21:25:54 +01:00
|
|
|
if (idx < 0 && ufunc == NULL)
|
2020-07-04 13:15:31 +02:00
|
|
|
goto erret;
|
2020-01-26 15:56:19 +01:00
|
|
|
|
2020-07-04 13:15:31 +02:00
|
|
|
if (check_defined(name, STRLEN(name), cctx) == FAIL)
|
|
|
|
goto erret;
|
2020-03-09 19:25:27 +01:00
|
|
|
|
2020-01-26 15:56:19 +01:00
|
|
|
imported = new_imported(gap != NULL ? gap
|
2020-01-26 19:26:46 +01:00
|
|
|
: &SCRIPT_ITEM(import_sid)->sn_imports);
|
2020-01-26 15:56:19 +01:00
|
|
|
if (imported == NULL)
|
2020-07-04 13:15:31 +02:00
|
|
|
goto erret;
|
2020-01-26 15:56:19 +01:00
|
|
|
|
|
|
|
// TODO: check for "as" following
|
2020-07-04 13:15:31 +02:00
|
|
|
// imported->imp_name = vim_strsave(as_name);
|
|
|
|
imported->imp_name = name;
|
|
|
|
((char_u **)names.ga_data)[i] = NULL;
|
2020-01-26 15:56:19 +01:00
|
|
|
imported->imp_sid = sid;
|
|
|
|
if (idx >= 0)
|
|
|
|
{
|
2020-02-23 21:25:54 +01:00
|
|
|
imported->imp_type = type;
|
2020-01-26 15:56:19 +01:00
|
|
|
imported->imp_var_vals_idx = idx;
|
|
|
|
}
|
|
|
|
else
|
2020-07-23 22:41:43 +02:00
|
|
|
{
|
|
|
|
imported->imp_type = ufunc->uf_func_type;
|
2020-01-26 15:56:19 +01:00
|
|
|
imported->imp_funcname = ufunc->uf_name;
|
2020-07-23 22:41:43 +02:00
|
|
|
}
|
2020-01-26 15:56:19 +01:00
|
|
|
}
|
|
|
|
}
|
2020-07-04 13:15:31 +02:00
|
|
|
erret:
|
|
|
|
ga_clear_strings(&names);
|
|
|
|
vim_free(as_name);
|
2020-01-26 15:56:19 +01:00
|
|
|
return cmd_end;
|
|
|
|
}
|
|
|
|
|
2020-06-13 18:09:19 +02:00
|
|
|
/*
|
|
|
|
* Declare a script-local variable without init: "let var: type".
|
|
|
|
* "const" is an error since the value is missing.
|
|
|
|
* Returns a pointer to after the type.
|
|
|
|
*/
|
|
|
|
char_u *
|
|
|
|
vim9_declare_scriptvar(exarg_T *eap, char_u *arg)
|
|
|
|
{
|
|
|
|
char_u *p;
|
|
|
|
char_u *name;
|
|
|
|
scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
|
|
|
|
type_T *type;
|
|
|
|
int called_emsg_before = called_emsg;
|
|
|
|
typval_T init_tv;
|
|
|
|
|
|
|
|
if (eap->cmdidx == CMD_const)
|
|
|
|
{
|
2020-08-13 22:47:35 +02:00
|
|
|
emsg(_(e_const_requires_a_value));
|
2020-06-13 18:09:19 +02:00
|
|
|
return arg + STRLEN(arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for valid starting character.
|
|
|
|
if (!eval_isnamec1(*arg))
|
|
|
|
{
|
|
|
|
semsg(_(e_invarg2), arg);
|
|
|
|
return arg + STRLEN(arg);
|
|
|
|
}
|
|
|
|
|
2020-06-14 12:50:24 +02:00
|
|
|
for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
|
2020-06-19 19:01:43 +02:00
|
|
|
if (*p == ':' && (VIM_ISWHITE(p[1]) || p != arg + 1))
|
2020-06-14 12:50:24 +02:00
|
|
|
break;
|
2020-06-13 18:09:19 +02:00
|
|
|
|
|
|
|
if (*p != ':')
|
|
|
|
{
|
2020-08-13 22:47:35 +02:00
|
|
|
emsg(_(e_type_or_initialization_required));
|
2020-06-13 18:09:19 +02:00
|
|
|
return arg + STRLEN(arg);
|
|
|
|
}
|
2020-06-14 12:50:24 +02:00
|
|
|
if (!VIM_ISWHITE(p[1]))
|
|
|
|
{
|
2020-08-21 22:36:47 +02:00
|
|
|
semsg(_(e_white_space_required_after_str), ":");
|
2020-06-14 12:50:24 +02:00
|
|
|
return arg + STRLEN(arg);
|
|
|
|
}
|
2020-06-13 18:09:19 +02:00
|
|
|
name = vim_strnsave(arg, p - arg);
|
|
|
|
|
|
|
|
// parse type
|
|
|
|
p = skipwhite(p + 1);
|
|
|
|
type = parse_type(&p, &si->sn_type_list);
|
|
|
|
if (called_emsg != called_emsg_before)
|
2020-06-13 19:56:38 +02:00
|
|
|
{
|
|
|
|
vim_free(name);
|
2020-06-13 18:09:19 +02:00
|
|
|
return p;
|
2020-06-13 19:56:38 +02:00
|
|
|
}
|
2020-06-13 18:09:19 +02:00
|
|
|
|
|
|
|
// Create the variable with 0/NULL value.
|
|
|
|
CLEAR_FIELD(init_tv);
|
|
|
|
init_tv.v_type = type->tt_type;
|
|
|
|
set_var_const(name, type, &init_tv, FALSE, 0);
|
|
|
|
|
|
|
|
vim_free(name);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2020-06-13 19:00:10 +02:00
|
|
|
/*
|
|
|
|
* Check if the type of script variable "dest" allows assigning "value".
|
|
|
|
*/
|
2020-06-19 18:34:15 +02:00
|
|
|
int
|
2020-06-13 19:00:10 +02:00
|
|
|
check_script_var_type(typval_T *dest, typval_T *value, char_u *name)
|
|
|
|
{
|
|
|
|
scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
|
|
|
|
int idx;
|
|
|
|
|
2020-08-21 21:55:43 +02:00
|
|
|
if (si->sn_version != SCRIPT_VERSION_VIM9)
|
|
|
|
// legacy script doesn't store variable types
|
|
|
|
return OK;
|
|
|
|
|
2020-06-13 19:00:10 +02:00
|
|
|
// Find the svar_T in sn_var_vals.
|
|
|
|
for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
|
|
|
|
{
|
|
|
|
svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
|
|
|
|
|
|
|
|
if (sv->sv_tv == dest)
|
|
|
|
{
|
|
|
|
if (sv->sv_const)
|
2020-06-19 18:34:15 +02:00
|
|
|
{
|
2020-06-13 19:00:10 +02:00
|
|
|
semsg(_(e_readonlyvar), name);
|
2020-06-19 18:34:15 +02:00
|
|
|
return FAIL;
|
|
|
|
}
|
2020-07-22 21:45:14 +02:00
|
|
|
return check_typval_type(sv->sv_type, value);
|
2020-06-13 19:00:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
iemsg("check_script_var_type(): not found");
|
2020-06-19 18:34:15 +02:00
|
|
|
return OK; // not really
|
2020-06-13 19:00:10 +02:00
|
|
|
}
|
2020-06-13 18:09:19 +02:00
|
|
|
|
2020-01-26 15:56:19 +01:00
|
|
|
#endif // FEAT_EVAL
|