comparison src/typval.c @ 25314:7e620652bd13 v8.2.3194

patch 8.2.3194: Vim9: argument types are not checked at compile time Commit: https://github.com/vim/vim/commit/cd9172077bc8c0aafddf2e5367cc0ae2c00c8ff7 Author: Yegappan Lakshmanan <yegappan@yahoo.com> Date: Wed Jul 21 19:09:09 2021 +0200 patch 8.2.3194: Vim9: argument types are not checked at compile time Problem: Vim9: argument types are not checked at compile time. Solution: Add several more type checks, simplify some. (Yegappan Lakshmanan, closes #8598)
author Bram Moolenaar <Bram@vim.org>
date Wed, 21 Jul 2021 19:15:04 +0200
parents 4d3c68196d05
children e2be9f3c5907
comparison
equal deleted inserted replaced
25313:afe0651cb4aa 25314:7e620652bd13
573 } 573 }
574 return OK; 574 return OK;
575 } 575 }
576 576
577 /* 577 /*
578 * Check for an optional string or number argument at 'idx'.
579 */
580 int
581 check_for_opt_string_or_number_arg(typval_T *args, int idx)
582 {
583 return (args[idx].v_type == VAR_UNKNOWN
584 || check_for_string_or_number_arg(args, idx) != FAIL);
585 }
586
587 /*
588 * Give an error and return FAIL unless "args[idx]" is a buffer number.
589 * Buffer number can be a number or a string.
590 */
591 int
592 check_for_buffer_arg(typval_T *args, int idx)
593 {
594 return check_for_string_or_number_arg(args, idx);
595 }
596
597 /*
598 * Check for an optional buffer argument at 'idx'
599 */
600 int
601 check_for_opt_buffer_arg(typval_T *args, int idx)
602 {
603 return (args[idx].v_type == VAR_UNKNOWN
604 || check_for_buffer_arg(args, idx));
605 }
606
607 /*
608 * Give an error and return FAIL unless "args[idx]" is a line number.
609 * Line number can be a number or a string.
610 */
611 int
612 check_for_lnum_arg(typval_T *args, int idx)
613 {
614 return check_for_string_or_number_arg(args, idx);
615 }
616
617 /*
618 * Check for an optional line number argument at 'idx'
619 */
620 int
621 check_for_opt_lnum_arg(typval_T *args, int idx)
622 {
623 return (args[idx].v_type == VAR_UNKNOWN
624 || check_for_lnum_arg(args, idx));
625 }
626
627 /*
578 * Give an error and return FAIL unless "args[idx]" is a string or 628 * Give an error and return FAIL unless "args[idx]" is a string or
579 * a number (buffer) 629 * a blob.
580 */ 630 */
581 int 631 int
582 check_for_buffer_arg(typval_T *args, int idx) 632 check_for_string_or_blob_arg(typval_T *args, int idx)
583 { 633 {
584 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_NUMBER) 634 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_BLOB)
585 { 635 {
586 if (idx >= 0) 636 if (idx >= 0)
587 semsg(_(e_string_required_for_argument_nr), idx + 1); 637 semsg(_(e_string_required_for_argument_nr), idx + 1);
588 else 638 else
589 emsg(_(e_stringreq)); 639 emsg(_(e_stringreq));
592 return OK; 642 return OK;
593 } 643 }
594 644
595 /* 645 /*
596 * Give an error and return FAIL unless "args[idx]" is a string or 646 * Give an error and return FAIL unless "args[idx]" is a string or
597 * a number (line) 647 * a list.
598 */ 648 */
599 int 649 int
600 check_for_lnum_arg(typval_T *args, int idx) 650 check_for_string_or_list_arg(typval_T *args, int idx)
601 { 651 {
602 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_NUMBER) 652 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_LIST)
603 { 653 {
604 if (idx >= 0) 654 if (idx >= 0)
605 semsg(_(e_string_required_for_argument_nr), idx + 1); 655 semsg(_(e_string_required_for_argument_nr), idx + 1);
606 else 656 else
607 emsg(_(e_stringreq)); 657 emsg(_(e_stringreq));
609 } 659 }
610 return OK; 660 return OK;
611 } 661 }
612 662
613 /* 663 /*
614 * Give an error and return FAIL unless "args[idx]" is a string or 664 * Give an error and return FAIL unless "args[idx]" is a buffer
615 * a number (line) 665 * number or a dict.
616 */ 666 */
617 int 667 int
618 check_for_opt_lnum_arg(typval_T *args, int idx) 668 check_for_buffer_or_dict_arg(typval_T *args, int idx)
619 { 669 {
620 if (args[idx].v_type != VAR_UNKNOWN 670 if (args[idx].v_type != VAR_STRING
621 && args[idx].v_type != VAR_STRING 671 && args[idx].v_type != VAR_NUMBER
622 && args[idx].v_type != VAR_NUMBER) 672 && args[idx].v_type != VAR_DICT)
623 {
624 if (idx >= 0)
625 semsg(_(e_string_required_for_argument_nr), idx + 1);
626 else
627 emsg(_(e_stringreq));
628 return FAIL;
629 }
630 return OK;
631 }
632
633 /*
634 * Check for an optional string or number argument at 'idx'
635 */
636 int
637 check_for_opt_string_or_number_arg(typval_T *args, int idx)
638 {
639 return (args[idx].v_type == VAR_UNKNOWN
640 || check_for_string_or_number_arg(args, idx) != FAIL);
641 }
642
643 /*
644 * Give an error and return FAIL unless "args[idx]" is a string or
645 * a blob.
646 */
647 int
648 check_for_string_or_blob_arg(typval_T *args, int idx)
649 {
650 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_BLOB)
651 { 673 {
652 if (idx >= 0) 674 if (idx >= 0)
653 semsg(_(e_string_required_for_argument_nr), idx + 1); 675 semsg(_(e_string_required_for_argument_nr), idx + 1);
654 else 676 else
655 emsg(_(e_stringreq)); 677 emsg(_(e_stringreq));