summaryrefslogtreecommitdiff
path: root/src/vim9script.c
diff options
context:
space:
mode:
authorBram Moolenaar <Bram@vim.org>2022-01-08 17:03:55 +0000
committerBram Moolenaar <Bram@vim.org>2022-01-08 17:03:55 +0000
commit7c24dfddc28776eeff7464982ae5b94e187b6135 (patch)
tree2b84bb1e2900eeaf8a892830bb01a35dfe7b9dfd /src/vim9script.c
parent04935fb17e5f0f66b82cf4546b9752d3d1fa650e (diff)
downloadvim-git-7c24dfddc28776eeff7464982ae5b94e187b6135.tar.gz
patch 8.2.4044: Vim9: no error when importing the same script twicev8.2.4044
Problem: Vim9: no error when importing the same script twice. Solution: Give an error, unless it is a reload.
Diffstat (limited to 'src/vim9script.c')
-rw-r--r--src/vim9script.c38
1 files changed, 26 insertions, 12 deletions
diff --git a/src/vim9script.c b/src/vim9script.c
index e5e26959a..3ebeab366 100644
--- a/src/vim9script.c
+++ b/src/vim9script.c
@@ -374,6 +374,8 @@ handle_import(
int sid = -1;
int res;
long start_lnum = SOURCING_LNUM;
+ garray_T *import_gap;
+ int i;
// The name of the file can be an expression, which must evaluate to a
// string.
@@ -440,6 +442,24 @@ handle_import(
goto erret;
}
+ import_gap = gap != NULL ? gap : &SCRIPT_ITEM(import_sid)->sn_imports;
+ for (i = 0; i < import_gap->ga_len; ++i)
+ {
+ imported_T *import = (imported_T *)import_gap->ga_data + i;
+
+ if (import->imp_sid == sid)
+ {
+ if (import->imp_flags & IMP_FLAGS_RELOAD)
+ {
+ // encountering same script first ime on a reload is OK
+ import->imp_flags &= ~IMP_FLAGS_RELOAD;
+ break;
+ }
+ semsg(_(e_cannot_import_same_script_twice_str), tv.vval.v_string);
+ goto erret;
+ }
+ }
+
// Allow for the "as Name" to be in the next line.
nextarg = eval_next_non_blank(expr_end, evalarg, &getnext);
if (STRNCMP("as", nextarg, 2) == 0 && IS_WHITE_OR_NUL(nextarg[2]))
@@ -494,22 +514,16 @@ handle_import(
imported_T *imported;
imported = find_imported(as_name, STRLEN(as_name), cctx);
- if (imported != NULL && imported->imp_sid == sid)
+ if (imported != NULL && imported->imp_sid != sid)
{
- if (imported->imp_flags & IMP_FLAGS_RELOAD)
- // import already defined on a previous script load
- imported->imp_flags &= ~IMP_FLAGS_RELOAD;
- else
- {
- semsg(_(e_name_already_defined_str), as_name);
- goto erret;
- }
+ semsg(_(e_name_already_defined_str), as_name);
+ goto erret;
}
- else if (check_defined(as_name, STRLEN(as_name), cctx, FALSE) == FAIL)
+ else if (imported == NULL
+ && check_defined(as_name, STRLEN(as_name), cctx, FALSE) == FAIL)
goto erret;
- imported = new_imported(gap != NULL ? gap
- : &SCRIPT_ITEM(import_sid)->sn_imports);
+ imported = new_imported(import_gap);
if (imported == NULL)
goto erret;
imported->imp_name = as_name;