comparison src/vim9compile.c @ 19509:17f0d6dc6a73 v8.2.0312

patch 8.2.0312: Vim9: insufficient script tests Commit: https://github.com/vim/vim/commit/f2d5c240a56853c0bbbc7979e9bff095de6c73ec Author: Bram Moolenaar <Bram@vim.org> Date: Sun Feb 23 21:25:54 2020 +0100 patch 8.2.0312: Vim9: insufficient script tests Problem: Vim9: insufficient script tests. Solution: Add more tests. Make "import * as Name" work.
author Bram Moolenaar <Bram@vim.org>
date Sun, 23 Feb 2020 21:30:03 +0100
parents c27837cbe922
children 860b39ed0e0b
comparison
equal deleted inserted replaced
19508:6afcef2bfba3 19509:17f0d6dc6a73
1520 1520
1521 /* 1521 /*
1522 * Generate an instruction to load script-local variable "name". 1522 * Generate an instruction to load script-local variable "name".
1523 */ 1523 */
1524 static int 1524 static int
1525 compile_load_scriptvar(cctx_T *cctx, char_u *name) 1525 compile_load_scriptvar(
1526 cctx_T *cctx,
1527 char_u *name, // variable NUL terminated
1528 char_u *start, // start of variable
1529 char_u **end) // end of variable
1526 { 1530 {
1527 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid); 1531 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
1528 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE); 1532 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
1529 imported_T *import; 1533 imported_T *import;
1530 1534
1544 } 1548 }
1545 1549
1546 import = find_imported(name, 0, cctx); 1550 import = find_imported(name, 0, cctx);
1547 if (import != NULL) 1551 if (import != NULL)
1548 { 1552 {
1549 // TODO: check this is a variable, not a function 1553 if (import->imp_all)
1550 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, 1554 {
1551 import->imp_sid, 1555 char_u *p = skipwhite(*end);
1552 import->imp_var_vals_idx, 1556 int name_len;
1553 import->imp_type); 1557 ufunc_T *ufunc;
1558 type_T *type;
1559
1560 // Used "import * as Name", need to lookup the member.
1561 if (*p != '.')
1562 {
1563 semsg(_("E1060: expected dot after name: %s"), start);
1564 return FAIL;
1565 }
1566 ++p;
1567
1568 idx = find_exported(import->imp_sid, &p, &name_len, &ufunc, &type);
1569 // TODO: what if it is a function?
1570 if (idx < 0)
1571 return FAIL;
1572 *end = p;
1573
1574 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
1575 import->imp_sid,
1576 idx,
1577 type);
1578 }
1579 else
1580 {
1581 // TODO: check this is a variable, not a function
1582 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
1583 import->imp_sid,
1584 import->imp_var_vals_idx,
1585 import->imp_type);
1586 }
1554 return OK; 1587 return OK;
1555 } 1588 }
1556 1589
1557 semsg(_("E1050: Item not found: %s"), name); 1590 semsg(_("E1050: Item not found: %s"), name);
1558 return FAIL; 1591 return FAIL;
1562 * Compile a variable name into a load instruction. 1595 * Compile a variable name into a load instruction.
1563 * "end" points to just after the name. 1596 * "end" points to just after the name.
1564 * When "error" is FALSE do not give an error when not found. 1597 * When "error" is FALSE do not give an error when not found.
1565 */ 1598 */
1566 static int 1599 static int
1567 compile_load(char_u **arg, char_u *end, cctx_T *cctx, int error) 1600 compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
1568 { 1601 {
1569 type_T *type; 1602 type_T *type;
1570 char_u *name; 1603 char_u *name;
1604 char_u *end = end_arg;
1571 int res = FAIL; 1605 int res = FAIL;
1572 1606
1573 if (*(*arg + 1) == ':') 1607 if (*(*arg + 1) == ':')
1574 { 1608 {
1575 // load namespaced variable 1609 // load namespaced variable
1587 // exists, give error at runtime. 1621 // exists, give error at runtime.
1588 res = generate_LOAD(cctx, ISN_LOADG, 0, name, &t_any); 1622 res = generate_LOAD(cctx, ISN_LOADG, 0, name, &t_any);
1589 } 1623 }
1590 else if (**arg == 's') 1624 else if (**arg == 's')
1591 { 1625 {
1592 res = compile_load_scriptvar(cctx, name); 1626 res = compile_load_scriptvar(cctx, name, NULL, NULL);
1593 } 1627 }
1594 else 1628 else
1595 { 1629 {
1596 semsg("Namespace not supported yet: %s", **arg); 1630 semsg("Namespace not supported yet: %s", **arg);
1597 goto theend; 1631 goto theend;
1643 res = generate_PUSHBOOL(cctx, **arg == 't' 1677 res = generate_PUSHBOOL(cctx, **arg == 't'
1644 ? VVAL_TRUE : VVAL_FALSE); 1678 ? VVAL_TRUE : VVAL_FALSE);
1645 else if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version 1679 else if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
1646 == SCRIPT_VERSION_VIM9) 1680 == SCRIPT_VERSION_VIM9)
1647 // in Vim9 script "var" can be script-local. 1681 // in Vim9 script "var" can be script-local.
1648 res = compile_load_scriptvar(cctx, name); 1682 res = compile_load_scriptvar(cctx, name, *arg, &end);
1649 } 1683 }
1650 } 1684 }
1651 if (gen_load) 1685 if (gen_load)
1652 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type); 1686 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
1653 } 1687 }
3410 break; 3444 break;
3411 case dest_global: 3445 case dest_global:
3412 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type); 3446 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
3413 break; 3447 break;
3414 case dest_script: 3448 case dest_script:
3415 compile_load_scriptvar(cctx, name + (name[1] == ':' ? 2 : 0)); 3449 compile_load_scriptvar(cctx, name + (name[1] == ':' ? 2 : 0), NULL, NULL);
3416 break; 3450 break;
3417 case dest_env: 3451 case dest_env:
3418 // Include $ in the name here 3452 // Include $ in the name here
3419 generate_LOAD(cctx, ISN_LOADENV, 0, name, type); 3453 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
3420 break; 3454 break;