Mercurial > vim
comparison src/evalvars.c @ 24112:0346a59ed5bf v8.2.2597
patch 8.2.2597: Vim9: "import * as" does not work at script level
Commit: https://github.com/vim/vim/commit/cb4e80fab9b1ee67249bde4f784526f900cda70c
Author: Bram Moolenaar <Bram@vim.org>
Date: Sat Mar 13 20:57:19 2021 +0100
patch 8.2.2597: Vim9: "import * as" does not work at script level
Problem: Vim9: "import * as" does not work at script level.
Solution: Implement using an imported namespace.
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Sat, 13 Mar 2021 21:00:03 +0100 |
parents | c5e396fb0ebe |
children | f4061617c438 |
comparison
equal
deleted
inserted
replaced
24111:33c1e030ceae | 24112:0346a59ed5bf |
---|---|
1217 else | 1217 else |
1218 { | 1218 { |
1219 arg = skipwhite(arg); | 1219 arg = skipwhite(arg); |
1220 if (tofree != NULL) | 1220 if (tofree != NULL) |
1221 name = tofree; | 1221 name = tofree; |
1222 if (eval_variable(name, len, &tv, NULL, TRUE, FALSE) == FAIL) | 1222 if (eval_variable(name, len, &tv, NULL, |
1223 EVAL_VAR_VERBOSE) == FAIL) | |
1223 error = TRUE; | 1224 error = TRUE; |
1224 else | 1225 else |
1225 { | 1226 { |
1226 // handle d.key, l[idx], f(expr) | 1227 // handle d.key, l[idx], f(expr) |
1227 arg_subsc = arg; | 1228 arg_subsc = arg; |
2537 return oldval; | 2538 return oldval; |
2538 } | 2539 } |
2539 | 2540 |
2540 /* | 2541 /* |
2541 * Get the value of internal variable "name". | 2542 * Get the value of internal variable "name". |
2543 * If "flags" has EVAL_VAR_IMPORT may return a VAR_ANY with v_number set to the | |
2544 * imported script ID. | |
2542 * Return OK or FAIL. If OK is returned "rettv" must be cleared. | 2545 * Return OK or FAIL. If OK is returned "rettv" must be cleared. |
2543 */ | 2546 */ |
2544 int | 2547 int |
2545 eval_variable( | 2548 eval_variable( |
2546 char_u *name, | 2549 char_u *name, |
2547 int len, // length of "name" | 2550 int len, // length of "name" |
2548 typval_T *rettv, // NULL when only checking existence | 2551 typval_T *rettv, // NULL when only checking existence |
2549 dictitem_T **dip, // non-NULL when typval's dict item is needed | 2552 dictitem_T **dip, // non-NULL when typval's dict item is needed |
2550 int verbose, // may give error message | 2553 int flags) // EVAL_VAR_ flags |
2551 int no_autoload) // do not use script autoloading | |
2552 { | 2554 { |
2553 int ret = OK; | 2555 int ret = OK; |
2554 typval_T *tv = NULL; | 2556 typval_T *tv = NULL; |
2555 int foundFunc = FALSE; | 2557 int found = FALSE; |
2556 dictitem_T *v; | 2558 dictitem_T *v; |
2557 int cc; | 2559 int cc; |
2558 | 2560 |
2559 // truncate the name, so that we can use strcmp() | 2561 // truncate the name, so that we can use strcmp() |
2560 cc = name[len]; | 2562 cc = name[len]; |
2561 name[len] = NUL; | 2563 name[len] = NUL; |
2562 | 2564 |
2563 // Check for user-defined variables. | 2565 // Check for user-defined variables. |
2564 v = find_var(name, NULL, no_autoload); | 2566 v = find_var(name, NULL, flags & EVAL_VAR_NOAUTOLOAD); |
2565 if (v != NULL) | 2567 if (v != NULL) |
2566 { | 2568 { |
2567 tv = &v->di_tv; | 2569 tv = &v->di_tv; |
2568 if (dip != NULL) | 2570 if (dip != NULL) |
2569 *dip = v; | 2571 *dip = v; |
2579 // imported variable from another script | 2581 // imported variable from another script |
2580 if (import != NULL) | 2582 if (import != NULL) |
2581 { | 2583 { |
2582 if (import->imp_funcname != NULL) | 2584 if (import->imp_funcname != NULL) |
2583 { | 2585 { |
2584 foundFunc = TRUE; | 2586 found = TRUE; |
2585 if (rettv != NULL) | 2587 if (rettv != NULL) |
2586 { | 2588 { |
2587 rettv->v_type = VAR_FUNC; | 2589 rettv->v_type = VAR_FUNC; |
2588 rettv->vval.v_string = vim_strsave(import->imp_funcname); | 2590 rettv->vval.v_string = vim_strsave(import->imp_funcname); |
2589 } | 2591 } |
2590 } | 2592 } |
2591 else if (import->imp_flags & IMP_FLAGS_STAR) | 2593 else if (import->imp_flags & IMP_FLAGS_STAR) |
2592 { | 2594 { |
2593 emsg("Sorry, 'import * as X' not implemented yet"); | 2595 if ((flags & EVAL_VAR_IMPORT) == 0) |
2594 ret = FAIL; | 2596 { |
2597 if (flags & EVAL_VAR_VERBOSE) | |
2598 emsg(_(e_import_as_name_not_supported_here)); | |
2599 ret = FAIL; | |
2600 } | |
2601 else | |
2602 { | |
2603 if (rettv != NULL) | |
2604 { | |
2605 rettv->v_type = VAR_ANY; | |
2606 rettv->vval.v_number = import->imp_sid; | |
2607 } | |
2608 found = TRUE; | |
2609 } | |
2595 } | 2610 } |
2596 else | 2611 else |
2597 { | 2612 { |
2598 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid); | 2613 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid); |
2599 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) | 2614 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) |
2605 { | 2620 { |
2606 ufunc_T *ufunc = find_func(name, FALSE, NULL); | 2621 ufunc_T *ufunc = find_func(name, FALSE, NULL); |
2607 | 2622 |
2608 if (ufunc != NULL) | 2623 if (ufunc != NULL) |
2609 { | 2624 { |
2610 foundFunc = TRUE; | 2625 found = TRUE; |
2611 if (rettv != NULL) | 2626 if (rettv != NULL) |
2612 { | 2627 { |
2613 rettv->v_type = VAR_FUNC; | 2628 rettv->v_type = VAR_FUNC; |
2614 rettv->vval.v_string = vim_strsave(ufunc->uf_name); | 2629 rettv->vval.v_string = vim_strsave(ufunc->uf_name); |
2615 } | 2630 } |
2616 } | 2631 } |
2617 } | 2632 } |
2618 } | 2633 } |
2619 | 2634 |
2620 if (!foundFunc) | 2635 if (!found) |
2621 { | 2636 { |
2622 if (tv == NULL) | 2637 if (tv == NULL) |
2623 { | 2638 { |
2624 if (rettv != NULL && verbose) | 2639 if (rettv != NULL && (flags & EVAL_VAR_VERBOSE)) |
2625 semsg(_(e_undefined_variable_str), name); | 2640 semsg(_(e_undefined_variable_str), name); |
2626 ret = FAIL; | 2641 ret = FAIL; |
2627 } | 2642 } |
2628 else if (rettv != NULL) | 2643 else if (rettv != NULL) |
2629 { | 2644 { |
3693 len = get_name_len(&arg, &tofree, TRUE, FALSE); | 3708 len = get_name_len(&arg, &tofree, TRUE, FALSE); |
3694 if (len > 0) | 3709 if (len > 0) |
3695 { | 3710 { |
3696 if (tofree != NULL) | 3711 if (tofree != NULL) |
3697 name = tofree; | 3712 name = tofree; |
3698 n = (eval_variable(name, len, &tv, NULL, FALSE, TRUE) == OK); | 3713 n = (eval_variable(name, len, &tv, NULL, |
3714 EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT) == OK); | |
3699 if (n) | 3715 if (n) |
3700 { | 3716 { |
3701 // handle d.key, l[idx], f(expr) | 3717 // handle d.key, l[idx], f(expr) |
3702 arg = skipwhite(arg); | 3718 arg = skipwhite(arg); |
3703 n = (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, FALSE) == OK); | 3719 n = (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, FALSE) == OK); |