comparison 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
comparison
equal deleted inserted replaced
19508:6afcef2bfba3 19509:17f0d6dc6a73
149 eap->nextcmd = check_nextcmd(cmd_end); 149 eap->nextcmd = check_nextcmd(cmd_end);
150 } 150 }
151 } 151 }
152 152
153 /* 153 /*
154 * Find an exported item in "sid" matching the name at "*argp".
155 * When it is a variable return the index.
156 * When it is a user function return "*ufunc".
157 * When not found returns -1 and "*ufunc" is NULL.
158 */
159 int
160 find_exported(
161 int sid,
162 char_u **argp,
163 int *name_len,
164 ufunc_T **ufunc,
165 type_T **type)
166 {
167 char_u *name = *argp;
168 char_u *arg = *argp;
169 int cc;
170 int idx = -1;
171 svar_T *sv;
172 scriptitem_T *script = SCRIPT_ITEM(sid);
173
174 // isolate one name
175 while (eval_isnamec1(*arg))
176 ++arg;
177 *name_len = (int)(arg - name);
178
179 // find name in "script"
180 // TODO: also find script-local user function
181 cc = *arg;
182 *arg = NUL;
183 idx = get_script_item_idx(sid, name, FALSE);
184 if (idx >= 0)
185 {
186 sv = ((svar_T *)script->sn_var_vals.ga_data) + idx;
187 if (!sv->sv_export)
188 {
189 semsg(_("E1049: Item not exported in script: %s"), name);
190 *arg = cc;
191 return -1;
192 }
193 *type = sv->sv_type;
194 *ufunc = NULL;
195 }
196 else
197 {
198 char_u buffer[200];
199 char_u *funcname;
200
201 // it could be a user function.
202 if (STRLEN(name) < sizeof(buffer) - 10)
203 funcname = buffer;
204 else
205 {
206 funcname = alloc(STRLEN(name) + 10);
207 if (funcname == NULL)
208 {
209 *arg = cc;
210 return -1;
211 }
212 }
213 funcname[0] = K_SPECIAL;
214 funcname[1] = KS_EXTRA;
215 funcname[2] = (int)KE_SNR;
216 sprintf((char *)funcname + 3, "%ld_%s", (long)sid, name);
217 *ufunc = find_func(funcname, NULL);
218 if (funcname != buffer)
219 vim_free(funcname);
220
221 if (*ufunc == NULL)
222 {
223 semsg(_("E1048: Item not found in script: %s"), name);
224 *arg = cc;
225 return -1;
226 }
227 }
228 *arg = cc;
229 arg = skipwhite(arg);
230 *argp = arg;
231
232 return idx;
233 }
234
235 /*
154 * Handle an ":import" command and add the resulting imported_T to "gap", when 236 * Handle an ":import" command and add the resulting imported_T to "gap", when
155 * not NULL, or script "import_sid" sn_imports. 237 * not NULL, or script "import_sid" sn_imports.
156 * Returns a pointer to after the command or NULL in case of failure 238 * Returns a pointer to after the command or NULL in case of failure
157 */ 239 */
158 char_u * 240 char_u *
287 imported->imp_sid = sid; 369 imported->imp_sid = sid;
288 imported->imp_all = TRUE; 370 imported->imp_all = TRUE;
289 } 371 }
290 else 372 else
291 { 373 {
292 scriptitem_T *script = SCRIPT_ITEM(sid);
293
294 arg = arg_start; 374 arg = arg_start;
295 if (*arg == '{') 375 if (*arg == '{')
296 arg = skipwhite(arg + 1); 376 arg = skipwhite(arg + 1);
297 for (;;) 377 for (;;)
298 { 378 {
299 char_u *name = arg; 379 char_u *name = arg;
300 int name_len; 380 int name_len;
301 int cc;
302 int idx; 381 int idx;
303 svar_T *sv;
304 imported_T *imported; 382 imported_T *imported;
305 ufunc_T *ufunc; 383 ufunc_T *ufunc = NULL;
306 384 type_T *type;
307 // isolate one name 385
308 while (eval_isnamec1(*arg)) 386 idx = find_exported(sid, &arg, &name_len, &ufunc, &type);
309 ++arg; 387
310 name_len = (int)(arg - name); 388 if (idx < 0 && ufunc == NULL)
311 389 return NULL;
312 // find name in "script"
313 // TODO: also find script-local user function
314 cc = *arg;
315 *arg = NUL;
316 idx = get_script_item_idx(sid, name, FALSE);
317 if (idx >= 0)
318 {
319 sv = ((svar_T *)script->sn_var_vals.ga_data) + idx;
320 if (!sv->sv_export)
321 {
322 semsg(_("E1049: Item not exported in script: %s"), name);
323 *arg = cc;
324 return NULL;
325 }
326 ufunc = NULL;
327 }
328 else
329 {
330 char_u buffer[200];
331 char_u *funcname;
332
333 // it could be a user function.
334 if (STRLEN(name) < sizeof(buffer) - 10)
335 funcname = buffer;
336 else
337 {
338 funcname = alloc(STRLEN(name) + 10);
339 if (funcname == NULL)
340 {
341 *arg = cc;
342 return NULL;
343 }
344 }
345 funcname[0] = K_SPECIAL;
346 funcname[1] = KS_EXTRA;
347 funcname[2] = (int)KE_SNR;
348 sprintf((char *)funcname + 3, "%ld_%s", (long)sid, name);
349 ufunc = find_func(funcname, NULL);
350 if (funcname != buffer)
351 vim_free(funcname);
352
353 if (ufunc == NULL)
354 {
355 semsg(_("E1048: Item not found in script: %s"), name);
356 *arg = cc;
357 return NULL;
358 }
359 sv = NULL;
360 }
361 *arg = cc;
362 arg = skipwhite(arg);
363 390
364 imported = new_imported(gap != NULL ? gap 391 imported = new_imported(gap != NULL ? gap
365 : &SCRIPT_ITEM(import_sid)->sn_imports); 392 : &SCRIPT_ITEM(import_sid)->sn_imports);
366 if (imported == NULL) 393 if (imported == NULL)
367 return NULL; 394 return NULL;
370 // imported->imp_name = vim_strnsave(as_ptr, as_len); 397 // imported->imp_name = vim_strnsave(as_ptr, as_len);
371 imported->imp_name = vim_strnsave(name, name_len); 398 imported->imp_name = vim_strnsave(name, name_len);
372 imported->imp_sid = sid; 399 imported->imp_sid = sid;
373 if (idx >= 0) 400 if (idx >= 0)
374 { 401 {
375 imported->imp_type = sv->sv_type; 402 imported->imp_type = type;
376 imported->imp_var_vals_idx = idx; 403 imported->imp_var_vals_idx = idx;
377 } 404 }
378 else 405 else
379 imported->imp_funcname = ufunc->uf_name; 406 imported->imp_funcname = ufunc->uf_name;
380 407