diff src/vim9script.c @ 19509:17f0d6dc6a73 v8.2.0312

patch 8.2.0312: Vim9: insufficient script tests Commit: https://github.com/vim/vim/commit/f2d5c240a56853c0bbbc7979e9bff095de6c73ec Author: Bram Moolenaar <Bram@vim.org> Date: Sun Feb 23 21:25:54 2020 +0100 patch 8.2.0312: Vim9: insufficient script tests Problem: Vim9: insufficient script tests. Solution: Add more tests. Make "import * as Name" work.
author Bram Moolenaar <Bram@vim.org>
date Sun, 23 Feb 2020 21:30:03 +0100
parents 9b97da610cab
children 7e76d5fba19f
line wrap: on
line diff
--- a/src/vim9script.c
+++ b/src/vim9script.c
@@ -151,6 +151,88 @@ ex_import(exarg_T *eap)
 }
 
 /*
+ * 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,
+	char_u	    **argp,
+	int	    *name_len,
+	ufunc_T	    **ufunc,
+	type_T	    **type)
+{
+    char_u	*name = *argp;
+    char_u	*arg = *argp;
+    int		cc;
+    int		idx = -1;
+    svar_T	*sv;
+    scriptitem_T *script = SCRIPT_ITEM(sid);
+
+    // isolate one name
+    while (eval_isnamec1(*arg))
+	++arg;
+    *name_len = (int)(arg - name);
+
+    // find name in "script"
+    // TODO: also find script-local user function
+    cc = *arg;
+    *arg = NUL;
+    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)
+	{
+	    semsg(_("E1049: Item not exported in script: %s"), name);
+	    *arg = cc;
+	    return -1;
+	}
+	*type = sv->sv_type;
+	*ufunc = NULL;
+    }
+    else
+    {
+	char_u	buffer[200];
+	char_u	*funcname;
+
+	// it could be a user function.
+	if (STRLEN(name) < sizeof(buffer) - 10)
+	    funcname = buffer;
+	else
+	{
+	    funcname = alloc(STRLEN(name) + 10);
+	    if (funcname == NULL)
+	    {
+		*arg = cc;
+		return -1;
+	    }
+	}
+	funcname[0] = K_SPECIAL;
+	funcname[1] = KS_EXTRA;
+	funcname[2] = (int)KE_SNR;
+	sprintf((char *)funcname + 3, "%ld_%s", (long)sid, name);
+	*ufunc = find_func(funcname, NULL);
+	if (funcname != buffer)
+	    vim_free(funcname);
+
+	if (*ufunc == NULL)
+	{
+	    semsg(_("E1048: Item not found in script: %s"), name);
+	    *arg = cc;
+	    return -1;
+	}
+    }
+    *arg = cc;
+    arg = skipwhite(arg);
+    *argp = arg;
+
+    return idx;
+}
+
+/*
  * 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
@@ -289,8 +371,6 @@ handle_import(char_u *arg_start, garray_
     }
     else
     {
-	scriptitem_T *script = SCRIPT_ITEM(sid);
-
 	arg = arg_start;
 	if (*arg == '{')
 	    arg = skipwhite(arg + 1);
@@ -298,68 +378,15 @@ handle_import(char_u *arg_start, garray_
 	{
 	    char_u	*name = arg;
 	    int		name_len;
-	    int		cc;
 	    int		idx;
-	    svar_T	*sv;
 	    imported_T	*imported;
-	    ufunc_T	*ufunc;
-
-	    // isolate one name
-	    while (eval_isnamec1(*arg))
-		++arg;
-	    name_len = (int)(arg - name);
+	    ufunc_T	*ufunc = NULL;
+	    type_T	*type;
 
-	    // find name in "script"
-	    // TODO: also find script-local user function
-	    cc = *arg;
-	    *arg = NUL;
-	    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)
-		{
-		    semsg(_("E1049: Item not exported in script: %s"), name);
-		    *arg = cc;
-		    return NULL;
-		}
-		ufunc = NULL;
-	    }
-	    else
-	    {
-		char_u	buffer[200];
-		char_u	*funcname;
+	    idx = find_exported(sid, &arg, &name_len, &ufunc, &type);
 
-		// it could be a user function.
-		if (STRLEN(name) < sizeof(buffer) - 10)
-		    funcname = buffer;
-		else
-		{
-		    funcname = alloc(STRLEN(name) + 10);
-		    if (funcname == NULL)
-		    {
-			*arg = cc;
-			return NULL;
-		    }
-		}
-		funcname[0] = K_SPECIAL;
-		funcname[1] = KS_EXTRA;
-		funcname[2] = (int)KE_SNR;
-		sprintf((char *)funcname + 3, "%ld_%s", (long)sid, name);
-		ufunc = find_func(funcname, NULL);
-		if (funcname != buffer)
-		    vim_free(funcname);
-
-		if (ufunc == NULL)
-		{
-		    semsg(_("E1048: Item not found in script: %s"), name);
-		    *arg = cc;
-		    return NULL;
-		}
-		sv = NULL;
-	    }
-	    *arg = cc;
-	    arg = skipwhite(arg);
+	    if (idx < 0 && ufunc == NULL)
+		return NULL;
 
 	    imported = new_imported(gap != NULL ? gap
 				       : &SCRIPT_ITEM(import_sid)->sn_imports);
@@ -372,7 +399,7 @@ handle_import(char_u *arg_start, garray_
 	    imported->imp_sid = sid;
 	    if (idx >= 0)
 	    {
-		imported->imp_type = sv->sv_type;
+		imported->imp_type = type;
 		imported->imp_var_vals_idx = idx;
 	    }
 	    else