comparison src/vim9script.c @ 22643:71b57779177d v8.2.1870

patch 8.2.1870: Vim9: no need to keep all script variables Commit: https://github.com/vim/vim/commit/39ca4127a094d8aca6f77c01be4f3fea506d5cb7 Author: Bram Moolenaar <Bram@vim.org> Date: Tue Oct 20 14:25:07 2020 +0200 patch 8.2.1870: Vim9: no need to keep all script variables Problem: Vim9: no need to keep all script variables. Solution: Only keep script variables when a function was defined that could use them. Fix freeing static string on exit.
author Bram Moolenaar <Bram@vim.org>
date Tue, 20 Oct 2020 14:30:04 +0200
parents 107eae953b87
children 05ecd9d23c1d
comparison
equal deleted inserted replaced
22642:6869968c6587 22643:71b57779177d
49 si->sn_version = SCRIPT_VERSION_VIM9; 49 si->sn_version = SCRIPT_VERSION_VIM9;
50 si->sn_had_command = TRUE; 50 si->sn_had_command = TRUE;
51 51
52 if (STRCMP(p_cpo, CPO_VIM) != 0) 52 if (STRCMP(p_cpo, CPO_VIM) != 0)
53 { 53 {
54 si->sn_save_cpo = p_cpo; 54 si->sn_save_cpo = vim_strsave(p_cpo);
55 p_cpo = vim_strsave((char_u *)CPO_VIM); 55 set_option_value((char_u *)"cpo", 0L, (char_u *)CPO_VIM, 0);
56 } 56 }
57 } 57 }
58 58
59 /* 59 /*
60 * When in Vim9 script give an error and return FAIL. 60 * When in Vim9 script give an error and return FAIL.
567 vim_free(name); 567 vim_free(name);
568 return p; 568 return p;
569 } 569 }
570 570
571 /* 571 /*
572 * Vim9 part of adding a script variable: add it to sn_all_vars and 572 * Vim9 part of adding a script variable: add it to sn_all_vars (lookup by name
573 * sn_var_vals. 573 * with a hashtable) and sn_var_vals (lookup by index).
574 * When "type" is NULL use "tv" for the type. 574 * When "type" is NULL use "tv" for the type.
575 */ 575 */
576 void 576 void
577 add_vim9_script_var(dictitem_T *di, typval_T *tv, type_T *type) 577 add_vim9_script_var(dictitem_T *di, typval_T *tv, type_T *type)
578 { 578 {
626 } 626 }
627 627
628 /* 628 /*
629 * Hide a script variable when leaving a block. 629 * Hide a script variable when leaving a block.
630 * "idx" is de index in sn_var_vals. 630 * "idx" is de index in sn_var_vals.
631 * When "func_defined" is non-zero then a function was defined in this block,
632 * the variable may be accessed by it. Otherwise the variable can be cleared.
631 */ 633 */
632 void 634 void
633 hide_script_var(scriptitem_T *si, int idx) 635 hide_script_var(scriptitem_T *si, int idx, int func_defined)
634 { 636 {
635 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; 637 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
636 hashtab_T *script_ht = get_script_local_ht(); 638 hashtab_T *script_ht = get_script_local_ht();
637 hashtab_T *all_ht = &si->sn_all_vars.dv_hashtab; 639 hashtab_T *all_ht = &si->sn_all_vars.dv_hashtab;
638 hashitem_T *script_hi; 640 hashitem_T *script_hi;
639 hashitem_T *all_hi; 641 hashitem_T *all_hi;
640 642
641 // Remove a variable declared inside the block, if it still exists. 643 // Remove a variable declared inside the block, if it still exists.
644 // If it was added in a nested block it will already have been removed.
642 // The typval is moved into the sallvar_T. 645 // The typval is moved into the sallvar_T.
643 script_hi = hash_find(script_ht, sv->sv_name); 646 script_hi = hash_find(script_ht, sv->sv_name);
644 all_hi = hash_find(all_ht, sv->sv_name); 647 all_hi = hash_find(all_ht, sv->sv_name);
645 if (!HASHITEM_EMPTY(script_hi) && !HASHITEM_EMPTY(all_hi)) 648 if (!HASHITEM_EMPTY(script_hi) && !HASHITEM_EMPTY(all_hi))
646 { 649 {
647 dictitem_T *di = HI2DI(script_hi); 650 dictitem_T *di = HI2DI(script_hi);
648 sallvar_T *sav = HI2SAV(all_hi); 651 sallvar_T *sav = HI2SAV(all_hi);
652 sallvar_T *sav_prev = NULL;
649 653
650 // There can be multiple entries with the same name in different 654 // There can be multiple entries with the same name in different
651 // blocks, find the right one. 655 // blocks, find the right one.
652 while (sav != NULL && sav->sav_var_vals_idx != idx) 656 while (sav != NULL && sav->sav_var_vals_idx != idx)
657 {
658 sav_prev = sav;
653 sav = sav->sav_next; 659 sav = sav->sav_next;
660 }
654 if (sav != NULL) 661 if (sav != NULL)
655 { 662 {
656 sav->sav_tv = di->di_tv; 663 if (func_defined)
657 di->di_tv.v_type = VAR_UNKNOWN; 664 {
658 sav->sav_flags = di->di_flags; 665 // move the typval from the dictitem to the sallvar
659 sav->sav_di = NULL; 666 sav->sav_tv = di->di_tv;
667 di->di_tv.v_type = VAR_UNKNOWN;
668 sav->sav_flags = di->di_flags;
669 sav->sav_di = NULL;
670 sv->sv_tv = &sav->sav_tv;
671 }
672 else
673 {
674 if (sav_prev == NULL)
675 hash_remove(all_ht, all_hi);
676 else
677 sav_prev->sav_next = sav->sav_next;
678 sv->sv_name = NULL;
679 vim_free(sav);
680 }
660 delete_var(script_ht, script_hi); 681 delete_var(script_ht, script_hi);
661 sv->sv_tv = &sav->sav_tv;
662 } 682 }
663 } 683 }
664 } 684 }
665 685
666 /* 686 /*