Mercurial > vim
comparison src/ex_eval.c @ 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 |
---|---|
912 static void | 912 static void |
913 enter_block(cstack_T *cstack) | 913 enter_block(cstack_T *cstack) |
914 { | 914 { |
915 ++cstack->cs_idx; | 915 ++cstack->cs_idx; |
916 if (in_vim9script()) | 916 if (in_vim9script()) |
917 cstack->cs_script_var_len[cstack->cs_idx] = | 917 { |
918 SCRIPT_ITEM(current_sctx.sc_sid)->sn_var_vals.ga_len; | 918 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid); |
919 | |
920 cstack->cs_script_var_len[cstack->cs_idx] = si->sn_var_vals.ga_len; | |
921 cstack->cs_block_id[cstack->cs_idx] = ++si->sn_current_block_id; | |
922 } | |
919 } | 923 } |
920 | 924 |
921 static void | 925 static void |
922 leave_block(cstack_T *cstack) | 926 leave_block(cstack_T *cstack) |
923 { | 927 { |
924 int i; | 928 if (in_vim9script() && SCRIPT_ID_VALID(current_sctx.sc_sid)) |
925 | |
926 if (in_vim9script()) | |
927 { | 929 { |
928 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid); | 930 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid); |
929 hashtab_T *ht = get_script_local_ht(); | 931 int i; |
930 | 932 |
931 if (ht != NULL) | 933 for (i = cstack->cs_script_var_len[cstack->cs_idx]; |
932 { | |
933 for (i = cstack->cs_script_var_len[cstack->cs_idx]; | |
934 i < si->sn_var_vals.ga_len; ++i) | 934 i < si->sn_var_vals.ga_len; ++i) |
935 { | 935 { |
936 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + i; | 936 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + i; |
937 hashitem_T *hi; | 937 |
938 | 938 if (sv->sv_name != NULL) |
939 if (sv->sv_name != NULL) | 939 // Remove a variable declared inside the block, if it still |
940 { | 940 // exists, from sn_vars and move the value into sn_all_vars. |
941 // Remove a variable declared inside the block, if it still | 941 hide_script_var(si, sv); |
942 // exists. | 942 } |
943 hi = hash_find(ht, sv->sv_name); | 943 |
944 if (!HASHITEM_EMPTY(hi)) | 944 // TODO: is this needed? |
945 { | 945 cstack->cs_script_var_len[cstack->cs_idx] = si->sn_var_vals.ga_len; |
946 delete_var(ht, hi); | |
947 sv->sv_name = NULL; | |
948 } | |
949 } | |
950 } | |
951 } | |
952 } | 946 } |
953 --cstack->cs_idx; | 947 --cstack->cs_idx; |
954 } | 948 } |
955 | 949 |
956 /* | 950 /* |