comparison src/vim9compile.c @ 20943:1693ca876049 v8.2.1023

patch 8.2.1023: Vim9: redefining a function uses a new index every time Commit: https://github.com/vim/vim/commit/0cb5bcf5836de83f7d64fb01d3ce708caacaf66c Author: Bram Moolenaar <Bram@vim.org> Date: Sat Jun 20 18:19:09 2020 +0200 patch 8.2.1023: Vim9: redefining a function uses a new index every time Problem: Vim9: redefining a function uses a new index every time. Solution: When redefining a function clear the contents and re-use the index.
author Bram Moolenaar <Bram@vim.org>
date Sat, 20 Jun 2020 18:30:04 +0200
parents e2fd5f05342f
children 0653b9b72091
comparison
equal deleted inserted replaced
20942:484ce6d14734 20943:1693ca876049
1491 { 1491 {
1492 semsg(_(e_toofewarg), ufunc->uf_name); 1492 semsg(_(e_toofewarg), ufunc->uf_name);
1493 return FAIL; 1493 return FAIL;
1494 } 1494 }
1495 1495
1496 if (ufunc->uf_dfunc_idx != UF_NOT_COMPILED) 1496 if (ufunc->uf_def_status != UF_NOT_COMPILED)
1497 { 1497 {
1498 int i; 1498 int i;
1499 1499
1500 for (i = 0; i < argcount; ++i) 1500 for (i = 0; i < argcount; ++i)
1501 { 1501 {
1515 { 1515 {
1516 arg_type_mismatch(expected, actual, i + 1); 1516 arg_type_mismatch(expected, actual, i + 1);
1517 return FAIL; 1517 return FAIL;
1518 } 1518 }
1519 } 1519 }
1520 if (ufunc->uf_dfunc_idx == UF_TO_BE_COMPILED) 1520 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
1521 if (compile_def_function(ufunc, TRUE, NULL) == FAIL) 1521 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
1522 return FAIL; 1522 return FAIL;
1523 } 1523 }
1524 1524
1525 if ((isn = generate_instr(cctx, 1525 if ((isn = generate_instr(cctx,
1526 ufunc->uf_dfunc_idx != UF_NOT_COMPILED ? ISN_DCALL 1526 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
1527 : ISN_UCALL)) == NULL) 1527 : ISN_UCALL)) == NULL)
1528 return FAIL; 1528 return FAIL;
1529 if (ufunc->uf_dfunc_idx != UF_NOT_COMPILED) 1529 if (ufunc->uf_def_status != UF_NOT_COMPILED)
1530 { 1530 {
1531 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx; 1531 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1532 isn->isn_arg.dfunc.cdf_argcount = argcount; 1532 isn->isn_arg.dfunc.cdf_argcount = argcount;
1533 } 1533 }
1534 else 1534 else
3040 3040
3041 // The function will have one line: "return {expr}". 3041 // The function will have one line: "return {expr}".
3042 // Compile it into instructions. 3042 // Compile it into instructions.
3043 compile_def_function(ufunc, TRUE, cctx); 3043 compile_def_function(ufunc, TRUE, cctx);
3044 3044
3045 if (ufunc->uf_dfunc_idx >= 0) 3045 if (ufunc->uf_def_status == UF_COMPILED)
3046 return generate_FUNCREF(cctx, ufunc->uf_dfunc_idx); 3046 return generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
3047 return FAIL; 3047 return FAIL;
3048 } 3048 }
3049 3049
3050 /* 3050 /*
4537 eap->forceit = FALSE; 4537 eap->forceit = FALSE;
4538 ufunc = def_function(eap, name); 4538 ufunc = def_function(eap, name);
4539 4539
4540 if (ufunc == NULL) 4540 if (ufunc == NULL)
4541 return NULL; 4541 return NULL;
4542 if (ufunc->uf_dfunc_idx == UF_TO_BE_COMPILED 4542 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
4543 && compile_def_function(ufunc, TRUE, cctx) == FAIL) 4543 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
4544 return NULL; 4544 return NULL;
4545 4545
4546 // Define a local variable for the function reference. 4546 // Define a local variable for the function reference.
4547 lvar = reserve_local(cctx, name_start, name_end - name_start, 4547 lvar = reserve_local(cctx, name_start, name_end - name_start,
6515 return (char_u *)""; 6515 return (char_u *)"";
6516 } 6516 }
6517 6517
6518 /* 6518 /*
6519 * Add a function to the list of :def functions. 6519 * Add a function to the list of :def functions.
6520 * This "sets ufunc->uf_dfunc_idx" but the function isn't compiled yet. 6520 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
6521 */ 6521 */
6522 static int 6522 static int
6523 add_def_function(ufunc_T *ufunc) 6523 add_def_function(ufunc_T *ufunc)
6524 { 6524 {
6525 dfunc_T *dfunc; 6525 dfunc_T *dfunc;
6526
6527 if (def_functions.ga_len == 0)
6528 {
6529 // The first position is not used, so that a zero uf_dfunc_idx means it
6530 // wasn't set.
6531 if (ga_grow(&def_functions, 1) == FAIL)
6532 return FAIL;
6533 ++def_functions.ga_len;
6534 }
6526 6535
6527 // Add the function to "def_functions". 6536 // Add the function to "def_functions".
6528 if (ga_grow(&def_functions, 1) == FAIL) 6537 if (ga_grow(&def_functions, 1) == FAIL)
6529 return FAIL; 6538 return FAIL;
6530 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len; 6539 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6561 int do_estack_push; 6570 int do_estack_push;
6562 int emsg_before = called_emsg; 6571 int emsg_before = called_emsg;
6563 6572
6564 // When using a function that was compiled before: Free old instructions. 6573 // When using a function that was compiled before: Free old instructions.
6565 // Otherwise add a new entry in "def_functions". 6574 // Otherwise add a new entry in "def_functions".
6566 if (ufunc->uf_dfunc_idx >= 0) 6575 if (ufunc->uf_dfunc_idx > 0)
6567 { 6576 {
6568 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) 6577 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6569 + ufunc->uf_dfunc_idx; 6578 + ufunc->uf_dfunc_idx;
6570 delete_def_function_contents(dfunc); 6579 delete_def_function_contents(dfunc);
6571 } 6580 }
7012 dfunc->df_instr_count = instr->ga_len; 7021 dfunc->df_instr_count = instr->ga_len;
7013 dfunc->df_varcount = cctx.ctx_locals_count; 7022 dfunc->df_varcount = cctx.ctx_locals_count;
7014 dfunc->df_closure_count = cctx.ctx_closure_count; 7023 dfunc->df_closure_count = cctx.ctx_closure_count;
7015 if (cctx.ctx_outer_used) 7024 if (cctx.ctx_outer_used)
7016 ufunc->uf_flags |= FC_CLOSURE; 7025 ufunc->uf_flags |= FC_CLOSURE;
7026 ufunc->uf_def_status = UF_COMPILED;
7017 } 7027 }
7018 7028
7019 ret = OK; 7029 ret = OK;
7020 7030
7021 erret: 7031 erret:
7031 7041
7032 // if using the last entry in the table we might as well remove it 7042 // if using the last entry in the table we might as well remove it
7033 if (!dfunc->df_deleted 7043 if (!dfunc->df_deleted
7034 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1) 7044 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
7035 --def_functions.ga_len; 7045 --def_functions.ga_len;
7036 ufunc->uf_dfunc_idx = UF_NOT_COMPILED; 7046 ufunc->uf_def_status = UF_NOT_COMPILED;
7037 7047
7038 while (cctx.ctx_scope != NULL) 7048 while (cctx.ctx_scope != NULL)
7039 drop_scope(&cctx); 7049 drop_scope(&cctx);
7040 7050
7041 // Don't execute this function body. 7051 // Don't execute this function body.
7259 7269
7260 dfunc->df_deleted = TRUE; 7270 dfunc->df_deleted = TRUE;
7261 } 7271 }
7262 7272
7263 /* 7273 /*
7264 * When a user function is deleted, delete any associated def function. 7274 * When a user function is deleted, clear the contents of any associated def
7275 * function. The position in def_functions can be re-used.
7265 */ 7276 */
7266 void 7277 void
7267 delete_def_function(ufunc_T *ufunc) 7278 clear_def_function(ufunc_T *ufunc)
7268 { 7279 {
7269 if (ufunc->uf_dfunc_idx >= 0) 7280 if (ufunc->uf_dfunc_idx > 0)
7270 { 7281 {
7271 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) 7282 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7272 + ufunc->uf_dfunc_idx; 7283 + ufunc->uf_dfunc_idx;
7273 7284
7274 delete_def_function_contents(dfunc); 7285 delete_def_function_contents(dfunc);
7286 ufunc->uf_def_status = UF_NOT_COMPILED;
7275 } 7287 }
7276 } 7288 }
7277 7289
7278 #if defined(EXITFREE) || defined(PROTO) 7290 #if defined(EXITFREE) || defined(PROTO)
7279 /* 7291 /*