Mercurial > vim
diff src/vim9cmds.c @ 30291:61a688be1899 v9.0.0481
patch 9.0.0481: in :def function all closures in loop get the same variables
Commit: https://github.com/vim/vim/commit/8fa745e7be3a791ac25f93ef0227bbc48ade8a37
Author: Bram Moolenaar <Bram@vim.org>
Date: Fri Sep 16 19:04:24 2022 +0100
patch 9.0.0481: in :def function all closures in loop get the same variables
Problem: In a :def function all closures in a loop get the same variables.
Solution: Use a separate list of variables for LOADOUTER and STOREOUTER.
Not copied at end of loop yet.
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Fri, 16 Sep 2022 20:15:03 +0200 |
parents | 42a6345b91fd |
children | bee38b1d323c |
line wrap: on
line diff
--- a/src/vim9cmds.c +++ b/src/vim9cmds.c @@ -1246,6 +1246,49 @@ compile_endwhile(char_u *arg, cctx_T *cc } /* + * Get the current information about variables declared inside a loop. + * Returns zero if there are none, otherwise the count. + * "loop_var_idx" is then set to the index of the first variable. + */ + short +get_loop_var_info(cctx_T *cctx, short *loop_var_idx) +{ + scope_T *scope = cctx->ctx_scope; + int start_local_count; + + while (scope != NULL && scope->se_type != WHILE_SCOPE + && scope->se_type != FOR_SCOPE) + scope = scope->se_outer; + if (scope == NULL) + return 0; + + if (scope->se_type == WHILE_SCOPE) + start_local_count = scope->se_u.se_while.ws_local_count; + else + start_local_count = scope->se_u.se_for.fs_local_count; + if (cctx->ctx_locals.ga_len > start_local_count) + { + *loop_var_idx = (short)start_local_count; + return (short)(cctx->ctx_locals.ga_len - start_local_count); + } + return 0; +} + +/* + * Get the index of the first variable in a loop, if any. + * Returns -1 if none. + */ + int +get_loop_var_idx(cctx_T *cctx) +{ + short loop_var_idx; + + if (get_loop_var_info(cctx, &loop_var_idx) > 0) + return loop_var_idx; + return -1; +} + +/* * compile "continue" */ char_u *