comparison src/evalvars.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 aa613a3084b9
children 4b8d836db103
comparison
equal deleted inserted replaced
26979:2fb4968983af 26980:8796f1384750
1230 else 1230 else
1231 { 1231 {
1232 arg = skipwhite(arg); 1232 arg = skipwhite(arg);
1233 if (tofree != NULL) 1233 if (tofree != NULL)
1234 name = tofree; 1234 name = tofree;
1235 if (eval_variable(name, len, &tv, NULL, 1235 if (eval_variable(name, len, 0, &tv, NULL,
1236 EVAL_VAR_VERBOSE) == FAIL) 1236 EVAL_VAR_VERBOSE) == FAIL)
1237 error = TRUE; 1237 error = TRUE;
1238 else 1238 else
1239 { 1239 {
1240 // handle d.key, l[idx], f(expr) 1240 // handle d.key, l[idx], f(expr)
2643 */ 2643 */
2644 int 2644 int
2645 eval_variable( 2645 eval_variable(
2646 char_u *name, 2646 char_u *name,
2647 int len, // length of "name" 2647 int len, // length of "name"
2648 scid_T sid, // script ID for imported item or zero
2648 typval_T *rettv, // NULL when only checking existence 2649 typval_T *rettv, // NULL when only checking existence
2649 dictitem_T **dip, // non-NULL when typval's dict item is needed 2650 dictitem_T **dip, // non-NULL when typval's dict item is needed
2650 int flags) // EVAL_VAR_ flags 2651 int flags) // EVAL_VAR_ flags
2651 { 2652 {
2652 int ret = OK; 2653 int ret = OK;
2676 ht = NULL; 2677 ht = NULL;
2677 } 2678 }
2678 2679
2679 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0)) 2680 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
2680 { 2681 {
2681 imported_T *import; 2682 imported_T *import = NULL;
2682 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name; 2683 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2683 2684
2684 import = find_imported(p, 0, NULL); 2685 if (sid == 0)
2686 import = find_imported(p, 0, NULL);
2685 2687
2686 // imported variable from another script 2688 // imported variable from another script
2687 if (import != NULL) 2689 if (import != NULL || sid != 0)
2688 { 2690 {
2689 if (import->imp_funcname != NULL) 2691 if ((flags & EVAL_VAR_IMPORT) == 0)
2690 { 2692 {
2691 found = TRUE; 2693 if (sid != 0 && SCRIPT_ID_VALID(sid))
2692 if (rettv != NULL)
2693 { 2694 {
2694 rettv->v_type = VAR_FUNC; 2695 ht = &SCRIPT_VARS(sid);
2695 rettv->vval.v_string = vim_strsave(import->imp_funcname); 2696 if (ht != NULL)
2697 {
2698 dictitem_T *v = find_var_in_ht(ht, 0, name,
2699 flags & EVAL_VAR_NOAUTOLOAD);
2700
2701 if (v != NULL)
2702 {
2703 tv = &v->di_tv;
2704 if (dip != NULL)
2705 *dip = v;
2706 }
2707 else
2708 ht = NULL;
2709 }
2696 } 2710 }
2697 } 2711 else
2698 else if (import->imp_flags & IMP_FLAGS_STAR)
2699 {
2700 if ((flags & EVAL_VAR_IMPORT) == 0)
2701 { 2712 {
2702 if (flags & EVAL_VAR_VERBOSE) 2713 if (flags & EVAL_VAR_VERBOSE)
2703 emsg(_(e_import_as_name_not_supported_here)); 2714 emsg(_(e_import_as_name_not_supported_here));
2704 ret = FAIL; 2715 ret = FAIL;
2705 } 2716 }
2706 else 2717 }
2718 else
2719 {
2720 if (rettv != NULL)
2707 { 2721 {
2708 if (rettv != NULL) 2722 rettv->v_type = VAR_ANY;
2709 { 2723 rettv->vval.v_number = sid != 0 ? sid : import->imp_sid;
2710 rettv->v_type = VAR_ANY;
2711 rettv->vval.v_number = import->imp_sid;
2712 }
2713 found = TRUE;
2714 } 2724 }
2715 } 2725 found = TRUE;
2716 else
2717 {
2718 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
2719 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2720 + import->imp_var_vals_idx;
2721 tv = sv->sv_tv;
2722 type = sv->sv_type;
2723 } 2726 }
2724 } 2727 }
2725 else if (in_vim9script() && (flags & EVAL_VAR_NO_FUNC) == 0) 2728 else if (in_vim9script() && (flags & EVAL_VAR_NO_FUNC) == 0)
2726 { 2729 {
2727 ufunc_T *ufunc = find_func(name, FALSE, NULL); 2730 ufunc_T *ufunc = find_func(name, FALSE, NULL);
2758 else if (rettv != NULL) 2761 else if (rettv != NULL)
2759 { 2762 {
2760 if (ht != NULL && ht == get_script_local_ht() 2763 if (ht != NULL && ht == get_script_local_ht()
2761 && tv != &SCRIPT_SV(current_sctx.sc_sid)->sv_var.di_tv) 2764 && tv != &SCRIPT_SV(current_sctx.sc_sid)->sv_var.di_tv)
2762 { 2765 {
2763 svar_T *sv = find_typval_in_script(tv); 2766 svar_T *sv = find_typval_in_script(tv, 0);
2764 2767
2765 if (sv != NULL) 2768 if (sv != NULL)
2766 type = sv->sv_type; 2769 type = sv->sv_type;
2767 } 2770 }
2768 2771
3276 set_var( 3279 set_var(
3277 char_u *name, 3280 char_u *name,
3278 typval_T *tv, 3281 typval_T *tv,
3279 int copy) // make copy of value in "tv" 3282 int copy) // make copy of value in "tv"
3280 { 3283 {
3281 set_var_const(name, NULL, tv, copy, ASSIGN_DECL, 0); 3284 set_var_const(name, 0, NULL, tv, copy, ASSIGN_DECL, 0);
3282 } 3285 }
3283 3286
3284 /* 3287 /*
3285 * Set variable "name" to value in "tv". 3288 * Set variable "name" to value in "tv".
3289 * When "sid" is non-zero "name" is in the script with this ID.
3286 * If the variable already exists and "is_const" is FALSE the value is updated. 3290 * If the variable already exists and "is_const" is FALSE the value is updated.
3287 * Otherwise the variable is created. 3291 * Otherwise the variable is created.
3288 */ 3292 */
3289 void 3293 void
3290 set_var_const( 3294 set_var_const(
3291 char_u *name, 3295 char_u *name,
3296 scid_T sid,
3292 type_T *type_arg, 3297 type_T *type_arg,
3293 typval_T *tv_arg, 3298 typval_T *tv_arg,
3294 int copy, // make copy of value in "tv" 3299 int copy, // make copy of value in "tv"
3295 int flags_arg, // ASSIGN_CONST, ASSIGN_FINAL, etc. 3300 int flags_arg, // ASSIGN_CONST, ASSIGN_FINAL, etc.
3296 int var_idx) // index for ":let [a, b] = list" 3301 int var_idx) // index for ":let [a, b] = list"
3299 type_T *type = type_arg; 3304 type_T *type = type_arg;
3300 typval_T bool_tv; 3305 typval_T bool_tv;
3301 dictitem_T *di; 3306 dictitem_T *di;
3302 typval_T *dest_tv = NULL; 3307 typval_T *dest_tv = NULL;
3303 char_u *varname; 3308 char_u *varname;
3304 hashtab_T *ht; 3309 hashtab_T *ht = NULL;
3305 int is_script_local; 3310 int is_script_local;
3306 int vim9script = in_vim9script(); 3311 int vim9script = in_vim9script();
3307 int var_in_vim9script; 3312 int var_in_vim9script;
3308 int flags = flags_arg; 3313 int flags = flags_arg;
3309 int free_tv_arg = !copy; // free tv_arg if not used 3314 int free_tv_arg = !copy; // free tv_arg if not used
3310 3315
3311 ht = find_var_ht(name, &varname); 3316 if (sid != 0)
3317 {
3318 if (SCRIPT_ID_VALID(sid))
3319 ht = &SCRIPT_VARS(sid);
3320 varname = name;
3321 }
3322 else
3323 ht = find_var_ht(name, &varname);
3312 if (ht == NULL || *varname == NUL) 3324 if (ht == NULL || *varname == NUL)
3313 { 3325 {
3314 semsg(_(e_illegal_variable_name_str), name); 3326 semsg(_(e_illegal_variable_name_str), name);
3315 goto failed; 3327 goto failed;
3316 } 3328 }
3317 is_script_local = ht == get_script_local_ht(); 3329 is_script_local = ht == get_script_local_ht() || sid != 0;
3318 3330
3319 if (vim9script 3331 if (vim9script
3320 && !is_script_local 3332 && !is_script_local
3321 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0 3333 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
3322 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0 3334 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
3345 { 3357 {
3346 imported_T *import = find_imported(varname, 0, NULL); 3358 imported_T *import = find_imported(varname, 0, NULL);
3347 3359
3348 if (import != NULL) 3360 if (import != NULL)
3349 { 3361 {
3350 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid); 3362 // imported name space cannot be used
3351 svar_T *sv;
3352 where_T where = WHERE_INIT;
3353
3354 // imported variable from another script
3355 if ((flags & ASSIGN_NO_DECL) == 0) 3363 if ((flags & ASSIGN_NO_DECL) == 0)
3356 { 3364 {
3357 semsg(_(e_redefining_imported_item_str), name); 3365 semsg(_(e_redefining_imported_item_str), name);
3358 goto failed; 3366 goto failed;
3359 } 3367 }
3360 if (import->imp_flags & IMP_FLAGS_STAR) 3368 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
3361 { 3369 goto failed;
3362 semsg(_(e_cannot_use_str_itself_it_is_imported_with_star),
3363 name);
3364 goto failed;
3365 }
3366 sv = ((svar_T *)si->sn_var_vals.ga_data) + import->imp_var_vals_idx;
3367
3368 where.wt_variable = TRUE;
3369 if (check_typval_type(sv->sv_type, tv, where) == FAIL
3370 || value_check_lock(sv->sv_tv->v_lock, name, FALSE))
3371 {
3372 goto failed;
3373 }
3374
3375 dest_tv = sv->sv_tv;
3376 clear_tv(dest_tv);
3377 } 3370 }
3378 } 3371 }
3379 3372
3380 if (dest_tv == NULL) 3373 if (dest_tv == NULL)
3381 { 3374 {
3417 } 3410 }
3418 3411
3419 if (var_in_vim9script && (flags & ASSIGN_FOR_LOOP) == 0) 3412 if (var_in_vim9script && (flags & ASSIGN_FOR_LOOP) == 0)
3420 { 3413 {
3421 where_T where = WHERE_INIT; 3414 where_T where = WHERE_INIT;
3422 svar_T *sv = find_typval_in_script(&di->di_tv); 3415 svar_T *sv = find_typval_in_script(&di->di_tv, sid);
3423 3416
3424 if (sv != NULL) 3417 if (sv != NULL)
3425 { 3418 {
3426 // check the type and adjust to bool if needed 3419 // check the type and adjust to bool if needed
3427 where.wt_index = var_idx; 3420 where.wt_index = var_idx;
3954 len = get_name_len(&arg, &tofree, TRUE, FALSE); 3947 len = get_name_len(&arg, &tofree, TRUE, FALSE);
3955 if (len > 0) 3948 if (len > 0)
3956 { 3949 {
3957 if (tofree != NULL) 3950 if (tofree != NULL)
3958 name = tofree; 3951 name = tofree;
3959 n = (eval_variable(name, len, &tv, NULL, 3952 n = (eval_variable(name, len, 0, &tv, NULL,
3960 EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT) == OK); 3953 EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT) == OK);
3961 if (n) 3954 if (n)
3962 { 3955 {
3963 // handle d.key, l[idx], f(expr) 3956 // handle d.key, l[idx], f(expr)
3964 arg = skipwhite(arg); 3957 arg = skipwhite(arg);