comparison src/eval.c @ 26980:8796f1384750 v8.2.4019

patch 8.2.4019: Vim9: import mechanism is too complicated Commit: https://github.com/vim/vim/commit/d5f400c607182db6d4fbe2964471d796277f67e8 Author: Bram Moolenaar <Bram@vim.org> Date: Thu Jan 6 21:10:28 2022 +0000 patch 8.2.4019: Vim9: import mechanism is too complicated Problem: Vim9: import mechanism is too complicated. Solution: Do not use the Javascript mechanism but a much simpler one.
author Bram Moolenaar <Bram@vim.org>
date Thu, 06 Jan 2022 22:15:04 +0100
parents 11ee2667a09a
children 4b8d836db103
comparison
equal deleted inserted replaced
26979:2fb4968983af 26980:8796f1384750
54 static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string); 54 static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
55 static int eval7_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp); 55 static int eval7_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
56 56
57 static int free_unref_items(int copyID); 57 static int free_unref_items(int copyID);
58 static char_u *make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end); 58 static char_u *make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
59 static char_u *eval_next_line(evalarg_T *evalarg);
60 59
61 /* 60 /*
62 * Return "n1" divided by "n2", taking care of dividing by zero. 61 * Return "n1" divided by "n2", taking care of dividing by zero.
63 * If "failed" is not NULL set it to TRUE when dividing by zero fails. 62 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
64 */ 63 */
920 clear_type_list(type_list); 919 clear_type_list(type_list);
921 } 920 }
922 } 921 }
923 } 922 }
924 } 923 }
924 if (lp->ll_name == NULL)
925 return p;
926
927 if (*p == '.' && in_vim9script())
928 {
929 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, NULL);
930 if (import != NULL)
931 {
932 ufunc_T *ufunc;
933 type_T *type;
934
935 lp->ll_sid = import->imp_sid;
936 lp->ll_name = skipwhite(p + 1);
937 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
938 lp->ll_name_end = p;
939
940 // check the item is exported
941 cc = *p;
942 *p = NUL;
943 if (find_exported(import->imp_sid, lp->ll_name, &ufunc, &type,
944 NULL, TRUE) == -1)
945 {
946 *p = cc;
947 return FAIL;
948 }
949 *p = cc;
950 }
951 }
925 952
926 // Without [idx] or .key we are done. 953 // Without [idx] or .key we are done.
927 if ((*p != '[' && *p != '.') || lp->ll_name == NULL) 954 if ((*p != '[' && *p != '.'))
928 return p; 955 return p;
929 956
930 if (in_vim9script() && lval_root != NULL) 957 if (in_vim9script() && lval_root != NULL)
931 { 958 {
932 // using local variable 959 // using local variable
995 if (in_vim9script() && lp->ll_valtype == NULL 1022 if (in_vim9script() && lp->ll_valtype == NULL
996 && v != NULL 1023 && v != NULL
997 && lp->ll_tv == &v->di_tv 1024 && lp->ll_tv == &v->di_tv
998 && ht != NULL && ht == get_script_local_ht()) 1025 && ht != NULL && ht == get_script_local_ht())
999 { 1026 {
1000 svar_T *sv = find_typval_in_script(lp->ll_tv); 1027 svar_T *sv = find_typval_in_script(lp->ll_tv, 0);
1001 1028
1002 // Vim9 script local variable: get the type 1029 // Vim9 script local variable: get the type
1003 if (sv != NULL) 1030 if (sv != NULL)
1004 lp->ll_valtype = sv->sv_type; 1031 lp->ll_valtype = sv->sv_type;
1005 } 1032 }
1357 } 1384 }
1358 1385
1359 // handle +=, -=, *=, /=, %= and .= 1386 // handle +=, -=, *=, /=, %= and .=
1360 di = NULL; 1387 di = NULL;
1361 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name), 1388 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
1362 &tv, &di, EVAL_VAR_VERBOSE) == OK) 1389 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
1363 { 1390 {
1364 if ((di == NULL 1391 if ((di == NULL
1365 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE) 1392 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1366 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE))) 1393 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
1367 && tv_op(&tv, rettv, op) == OK) 1394 && tv_op(&tv, rettv, op) == OK)
1368 set_var_const(lp->ll_name, NULL, &tv, FALSE, 1395 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
1369 ASSIGN_NO_DECL, 0); 1396 ASSIGN_NO_DECL, 0);
1370 clear_tv(&tv); 1397 clear_tv(&tv);
1371 } 1398 }
1372 } 1399 }
1373 else 1400 else
1374 { 1401 {
1375 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv, 1402 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1376 NULL, 0) == FAIL) 1403 NULL, 0) == FAIL)
1377 return; 1404 return;
1378 set_var_const(lp->ll_name, lp->ll_type, rettv, copy, 1405 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
1379 flags, var_idx); 1406 flags, var_idx);
1380 } 1407 }
1381 *endp = cc; 1408 *endp = cc;
1382 } 1409 }
1383 else if (value_check_lock(lp->ll_newkey == NULL 1410 else if (value_check_lock(lp->ll_newkey == NULL
1387 else if (lp->ll_range) 1414 else if (lp->ll_range)
1388 { 1415 {
1389 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL)) 1416 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1390 && (flags & ASSIGN_FOR_LOOP) == 0) 1417 && (flags & ASSIGN_FOR_LOOP) == 0)
1391 { 1418 {
1392 emsg(_("E996: Cannot lock a range")); 1419 emsg(_(e_cannot_lock_range));
1393 return; 1420 return;
1394 } 1421 }
1395 1422
1396 (void)list_assign_range(lp->ll_list, rettv->vval.v_list, 1423 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1397 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name); 1424 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
1402 * Assign to a List or Dictionary item. 1429 * Assign to a List or Dictionary item.
1403 */ 1430 */
1404 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL)) 1431 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1405 && (flags & ASSIGN_FOR_LOOP) == 0) 1432 && (flags & ASSIGN_FOR_LOOP) == 0)
1406 { 1433 {
1407 emsg(_("E996: Cannot lock a list or dict")); 1434 emsg(_(e_cannot_lock_list_or_dict));
1408 return; 1435 return;
1409 } 1436 }
1410 1437
1411 if (lp->ll_valtype != NULL 1438 if (lp->ll_valtype != NULL
1412 && check_typval_arg_type(lp->ll_valtype, rettv, 1439 && check_typval_arg_type(lp->ll_valtype, rettv,
2087 * and set "getnext". 2114 * and set "getnext".
2088 * Otherwise return the next non-white at or after "arg" and set "getnext" to 2115 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2089 * FALSE. 2116 * FALSE.
2090 * "arg" must point somewhere inside a line, not at the start. 2117 * "arg" must point somewhere inside a line, not at the start.
2091 */ 2118 */
2092 static char_u * 2119 char_u *
2093 eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext) 2120 eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2094 { 2121 {
2095 char_u *p = skipwhite(arg); 2122 char_u *p = skipwhite(arg);
2096 2123
2097 *getnext = FALSE; 2124 *getnext = FALSE;
2118 2145
2119 /* 2146 /*
2120 * To be called after eval_next_non_blank() sets "getnext" to TRUE. 2147 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
2121 * Only called for Vim9 script. 2148 * Only called for Vim9 script.
2122 */ 2149 */
2123 static char_u * 2150 char_u *
2124 eval_next_line(evalarg_T *evalarg) 2151 eval_next_line(evalarg_T *evalarg)
2125 { 2152 {
2126 garray_T *gap = &evalarg->eval_ga; 2153 garray_T *gap = &evalarg->eval_ga;
2127 char_u *line; 2154 char_u *line;
2128 2155
2234 char_u *arg, 2261 char_u *arg,
2235 typval_T *rettv, 2262 typval_T *rettv,
2236 exarg_T *eap, 2263 exarg_T *eap,
2237 evalarg_T *evalarg) 2264 evalarg_T *evalarg)
2238 { 2265 {
2266 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2267 }
2268
2269 /*
2270 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2271 * expression and don't check what comes after the expression.
2272 */
2273 int
2274 eval0_retarg(
2275 char_u *arg,
2276 typval_T *rettv,
2277 exarg_T *eap,
2278 evalarg_T *evalarg,
2279 char_u **retarg)
2280 {
2239 int ret; 2281 int ret;
2240 char_u *p; 2282 char_u *p;
2241 char_u *expr_end; 2283 char_u *expr_end;
2242 int did_emsg_before = did_emsg; 2284 int did_emsg_before = did_emsg;
2243 int called_emsg_before = called_emsg; 2285 int called_emsg_before = called_emsg;
2244 int flags = evalarg == NULL ? 0 : evalarg->eval_flags; 2286 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
2245 int check_for_end = TRUE; 2287 int check_for_end = retarg == NULL;
2246 int end_error = FALSE; 2288 int end_error = FALSE;
2247 2289
2248 p = skipwhite(arg); 2290 p = skipwhite(arg);
2249 ret = eval1(&p, rettv, evalarg); 2291 ret = eval1(&p, rettv, evalarg);
2250 expr_end = p; 2292 expr_end = p;
2251 p = skipwhite(p); 2293 p = skipwhite(p);
2252 2294
2253 // In Vim9 script a command block is not split at NL characters for 2295 // In Vim9 script a command block is not split at NL characters for
2254 // commands using an expression argument. Skip over a '#' comment to check 2296 // commands using an expression argument. Skip over a '#' comment to check
2255 // for a following NL. Require white space before the '#'. 2297 // for a following NL. Require white space before the '#'.
2256 if (in_vim9script() && p > expr_end) 2298 if (in_vim9script() && p > expr_end && retarg == NULL)
2257 while (*p == '#') 2299 while (*p == '#')
2258 { 2300 {
2259 char_u *nl = vim_strchr(p, NL); 2301 char_u *nl = vim_strchr(p, NL);
2260 2302
2261 if (nl == NULL) 2303 if (nl == NULL)
2296 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|') 2338 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2297 eap->nextcmd = check_nextcmd(p); 2339 eap->nextcmd = check_nextcmd(p);
2298 return FAIL; 2340 return FAIL;
2299 } 2341 }
2300 2342
2301 if (check_for_end && eap != NULL) 2343 if (retarg != NULL)
2344 *retarg = p;
2345 else if (check_for_end && eap != NULL)
2302 set_nextcmd(eap, p); 2346 set_nextcmd(eap, p);
2303 2347
2304 return ret; 2348 return ret;
2305 } 2349 }
2306 2350
3667 rettv->v_type = VAR_SPECIAL; 3711 rettv->v_type = VAR_SPECIAL;
3668 rettv->vval.v_number = VVAL_NULL; 3712 rettv->vval.v_number = VVAL_NULL;
3669 ret = OK; 3713 ret = OK;
3670 } 3714 }
3671 else 3715 else
3672 ret = eval_variable(s, len, rettv, NULL, 3716 ret = eval_variable(s, len, 0, rettv, NULL,
3673 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT); 3717 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
3674 } 3718 }
3675 else 3719 else
3676 { 3720 {
3677 // skip the name 3721 // skip the name
5885 int cc; 5929 int cc;
5886 int idx; 5930 int idx;
5887 ufunc_T *ufunc; 5931 ufunc_T *ufunc;
5888 type_T *type; 5932 type_T *type;
5889 5933
5890 // Found script from "import * as {name}", script item name must 5934 // Found script from "import {name} as name", script item name must
5891 // follow. 5935 // follow.
5892 if (**arg != '.') 5936 if (**arg != '.')
5893 { 5937 {
5894 if (verbose) 5938 if (verbose)
5895 semsg(_(e_expected_str_but_got_str), "'.'", *arg); 5939 semsg(_(e_expected_str_but_got_str), "'.'", *arg);
5932 else 5976 else
5933 { 5977 {
5934 rettv->v_type = VAR_FUNC; 5978 rettv->v_type = VAR_FUNC;
5935 rettv->vval.v_string = vim_strsave(ufunc->uf_name); 5979 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
5936 } 5980 }
5981 continue;
5937 } 5982 }
5938 5983
5939 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC 5984 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5940 || rettv->v_type == VAR_PARTIAL)) 5985 || rettv->v_type == VAR_PARTIAL))
5941 && (!check_white || !VIM_ISWHITE(*(*arg - 1)))) 5986 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))