comparison src/structs.h @ 22594:209c7aa56325 v8.2.1845

patch 8.2.1845: Vim9: function defined in a block can't use block variables Commit: https://github.com/vim/vim/commit/8d739de43b84ef7817b3b5b826d1cbfe7572a62a Author: Bram Moolenaar <Bram@vim.org> Date: Wed Oct 14 19:39:19 2020 +0200 patch 8.2.1845: Vim9: function defined in a block can't use block variables Problem: Vim9: function defined in a block can't use variables defined in that block. Solution: First step: Make a second hashtab that holds all script variables, also block-local ones, with more information.
author Bram Moolenaar <Bram@vim.org>
date Wed, 14 Oct 2020 19:45:04 +0200
parents 7d25264c246c
children 107eae953b87
comparison
equal deleted inserted replaced
22593:a6030e19b08f 22594:209c7aa56325
887 void *csp_rv[CSTACK_LEN]; // return typeval for pending return 887 void *csp_rv[CSTACK_LEN]; // return typeval for pending return
888 void *csp_ex[CSTACK_LEN]; // exception for pending throw 888 void *csp_ex[CSTACK_LEN]; // exception for pending throw
889 } cs_pend; 889 } cs_pend;
890 void *cs_forinfo[CSTACK_LEN]; // info used by ":for" 890 void *cs_forinfo[CSTACK_LEN]; // info used by ":for"
891 int cs_line[CSTACK_LEN]; // line nr of ":while"/":for" line 891 int cs_line[CSTACK_LEN]; // line nr of ":while"/":for" line
892 int cs_block_id[CSTACK_LEN]; // block ID stack
892 int cs_script_var_len[CSTACK_LEN]; // value of sn_var_vals.ga_len 893 int cs_script_var_len[CSTACK_LEN]; // value of sn_var_vals.ga_len
893 // when entering the block 894 // when entering the block
894 int cs_idx; // current entry, or -1 if none 895 int cs_idx; // current entry, or -1 if none
895 int cs_looplevel; // nr of nested ":while"s and ":for"s 896 int cs_looplevel; // nr of nested ":while"s and ":for"s
896 int cs_trylevel; // nr of nested ":try"s 897 int cs_trylevel; // nr of nested ":try"s
1699 1700
1700 /* 1701 /*
1701 * Holds the hashtab with variables local to each sourced script. 1702 * Holds the hashtab with variables local to each sourced script.
1702 * Each item holds a variable (nameless) that points to the dict_T. 1703 * Each item holds a variable (nameless) that points to the dict_T.
1703 */ 1704 */
1704 typedef struct 1705 typedef struct {
1705 {
1706 dictitem_T sv_var; 1706 dictitem_T sv_var;
1707 dict_T sv_dict; 1707 dict_T sv_dict;
1708 } scriptvar_T; 1708 } scriptvar_T;
1709 1709
1710 /* 1710 /*
1711 * Entry for "sn_all_vars". Contains the s: variables from sn_vars plus the
1712 * block-local ones.
1713 */
1714 typedef struct sallvar_S sallvar_T;
1715 struct sallvar_S {
1716 sallvar_T *sav_next; // var with same name but different block
1717 int sav_block_id; // block ID where declared
1718 int sav_var_vals_idx; // index in sn_var_vals
1719
1720 // So long as the variable is valid (block it was defined in is still
1721 // active) "sav_di" is used. It is set to NULL when leaving the block,
1722 // then sav_tv and sav_flags are used.
1723 dictitem_T *sav_di; // dictitem with di_key and di_tv
1724 typval_T sav_tv; // type and value of the variable
1725 char_u sav_flags; // DI_FLAGS_ flags (only used for variable)
1726 char_u sav_key[1]; // key (actually longer!)
1727 };
1728
1729 /*
1730 * In the sn_all_vars hashtab item "hi_key" points to "sav_key" in a sallvar_T.
1731 * This makes it possible to store and find the sallvar_T.
1732 * SAV2HIKEY() converts a sallvar_T pointer to a hashitem key pointer.
1733 * HIKEY2SAV() converts a hashitem key pointer to a sallvar_T pointer.
1734 * HI2SAV() converts a hashitem pointer to a sallvar_T pointer.
1735 */
1736 #define SAV2HIKEY(sav) ((sav)->sav_key)
1737 #define HIKEY2SAV(p) ((sallvar_T *)(p - offsetof(sallvar_T, sav_key)))
1738 #define HI2SAV(hi) HIKEY2SAV((hi)->hi_key)
1739
1740 /*
1711 * Entry for "sn_var_vals". Used for script-local variables. 1741 * Entry for "sn_var_vals". Used for script-local variables.
1712 */ 1742 */
1713 typedef struct { 1743 typedef struct {
1714 char_u *sv_name; // points into "sn_vars" di_key 1744 char_u *sv_name; // points into "sn_all_vars" di_key
1715 typval_T *sv_tv; // points into "sn_vars" di_tv 1745 typval_T *sv_tv; // points into "sn_vars" or "sn_all_vars" di_tv
1716 type_T *sv_type; 1746 type_T *sv_type;
1717 int sv_const; 1747 int sv_const;
1718 int sv_export; // "export let var = val" 1748 int sv_export; // "export let var = val"
1719 } svar_T; 1749 } svar_T;
1720 1750
1740 */ 1770 */
1741 typedef struct 1771 typedef struct
1742 { 1772 {
1743 char_u *sn_name; 1773 char_u *sn_name;
1744 1774
1745 scriptvar_T *sn_vars; // stores s: variables for this script 1775 // "sn_vars" stores the s: variables currently valid. When leaving a block
1746 garray_T sn_var_vals; // same variables as a list of svar_T 1776 // variables local to that block are removed.
1777 scriptvar_T *sn_vars;
1778
1779 // Specific for a Vim9 script.
1780 // "sn_all_vars" stores all script variables ever declared. So long as the
1781 // variable is still valid the value is in "sn_vars->sv_dict...di_tv".
1782 // When the block of a declaration is left the value is moved to
1783 // "sn_all_vars..sav_tv".
1784 // Variables with duplicate names are possible, the sav_block_id must be
1785 // used to check that which variable is valid.
1786 dict_T sn_all_vars; // all script variables, dict of sallvar_T
1787
1788 // Stores the same variables as in "sn_all_vars" as a list of svar_T, so
1789 // that they can be quickly found by index instead of a hash table lookup.
1790 // Also stores the type.
1791 garray_T sn_var_vals;
1747 1792
1748 garray_T sn_imports; // imported items, imported_T 1793 garray_T sn_imports; // imported items, imported_T
1749
1750 garray_T sn_type_list; // keeps types used by variables 1794 garray_T sn_type_list; // keeps types used by variables
1795 int sn_current_block_id; // Unique ID for each script block
1751 1796
1752 int sn_version; // :scriptversion 1797 int sn_version; // :scriptversion
1753 int sn_had_command; // TRUE if any command was executed 1798 int sn_had_command; // TRUE if any command was executed
1754 char_u *sn_save_cpo; // 'cpo' value when :vim9script found 1799 char_u *sn_save_cpo; // 'cpo' value when :vim9script found
1755 1800