comparison src/typval.c @ 25302:4d3c68196d05 v8.2.3188

patch 8.2.3188: Vim9: argument types are not checked at compile time Commit: https://github.com/vim/vim/commit/83494b4ac61898f687d6ef9dce4bad5802fb8e51 Author: Yegappan Lakshmanan <yegappan@yahoo.com> Date: Tue Jul 20 17:51:51 2021 +0200 patch 8.2.3188: 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, also at runtime. (Yegappan Lakshmanan, closes #8587)
author Bram Moolenaar <Bram@vim.org>
date Tue, 20 Jul 2021 18:00:06 +0200
parents 55c85c3a43a0
children 7e620652bd13
comparison
equal deleted inserted replaced
25301:fe178301fc04 25302:4d3c68196d05
383 } 383 }
384 return OK; 384 return OK;
385 } 385 }
386 386
387 /* 387 /*
388 * Check for an optional string argument at 'idx'
389 */
390 int
391 check_for_opt_string_arg(typval_T *args, int idx)
392 {
393 return (args[idx].v_type == VAR_UNKNOWN
394 || check_for_string_arg(args, idx) != FAIL);
395 }
396
397 /*
388 * Give an error and return FAIL unless "args[idx]" is a number. 398 * Give an error and return FAIL unless "args[idx]" is a number.
389 */ 399 */
390 int 400 int
391 check_for_number_arg(typval_T *args, int idx) 401 check_for_number_arg(typval_T *args, int idx)
392 { 402 {
400 } 410 }
401 return OK; 411 return OK;
402 } 412 }
403 413
404 /* 414 /*
415 * Check for an optional number argument at 'idx'
416 */
417 int
418 check_for_opt_number_arg(typval_T *args, int idx)
419 {
420 return (args[idx].v_type == VAR_UNKNOWN
421 || check_for_number_arg(args, idx) != FAIL);
422 }
423
424 /*
405 * Give an error and return FAIL unless "args[idx]" is a bool. 425 * Give an error and return FAIL unless "args[idx]" is a bool.
406 */ 426 */
407 int 427 int
408 check_for_bool_arg(typval_T *args, int idx) 428 check_for_bool_arg(typval_T *args, int idx)
409 { 429 {
420 } 440 }
421 return OK; 441 return OK;
422 } 442 }
423 443
424 /* 444 /*
445 * Check for an optional bool argument at 'idx'
446 */
447 int
448 check_for_opt_bool_arg(typval_T *args, int idx)
449 {
450 return (args[idx].v_type == VAR_UNKNOWN
451 || check_for_bool_arg(args, idx) != FAIL);
452 }
453
454 /*
425 * Give an error and return FAIL unless "args[idx]" is a list. 455 * Give an error and return FAIL unless "args[idx]" is a list.
426 */ 456 */
427 int 457 int
428 check_for_list_arg(typval_T *args, int idx) 458 check_for_list_arg(typval_T *args, int idx)
429 { 459 {
437 } 467 }
438 return OK; 468 return OK;
439 } 469 }
440 470
441 /* 471 /*
472 * Check for an optional list argument at 'idx'
473 */
474 int
475 check_for_opt_list_arg(typval_T *args, int idx)
476 {
477 return (args[idx].v_type == VAR_UNKNOWN
478 || check_for_list_arg(args, idx) != FAIL);
479 }
480
481 /*
442 * Give an error and return FAIL unless "args[idx]" is a dict. 482 * Give an error and return FAIL unless "args[idx]" is a dict.
443 */ 483 */
444 int 484 int
445 check_for_dict_arg(typval_T *args, int idx) 485 check_for_dict_arg(typval_T *args, int idx)
446 { 486 {
448 { 488 {
449 if (idx >= 0) 489 if (idx >= 0)
450 semsg(_(e_dict_required_for_argument_nr), idx + 1); 490 semsg(_(e_dict_required_for_argument_nr), idx + 1);
451 else 491 else
452 emsg(_(e_dictreq)); 492 emsg(_(e_dictreq));
493 return FAIL;
494 }
495 return OK;
496 }
497
498 /*
499 * Check for an optional dict argument at 'idx'
500 */
501 int
502 check_for_opt_dict_arg(typval_T *args, int idx)
503 {
504 return (args[idx].v_type == VAR_UNKNOWN
505 || check_for_dict_arg(args, idx) != FAIL);
506 }
507
508 /*
509 * Give an error and return FAIL unless "args[idx]" is a blob.
510 */
511 int
512 check_for_blob_arg(typval_T *args, int idx)
513 {
514 if (args[idx].v_type != VAR_BLOB)
515 {
516 if (idx >= 0)
517 semsg(_(e_blob_required_for_argument_nr), idx + 1);
518 else
519 emsg(_(e_blobreq));
520 return FAIL;
521 }
522 return OK;
523 }
524
525 /*
526 * Give an error and return FAIL unless "args[idx]" is a channel or a job.
527 */
528 int
529 check_for_chan_or_job_arg(typval_T *args, int idx)
530 {
531 if (args[idx].v_type != VAR_CHANNEL && args[idx].v_type != VAR_JOB)
532 {
533 if (idx >= 0)
534 semsg(_(e_chan_or_job_required_for_argument_nr), idx + 1);
535 else
536 emsg(_(e_chan_or_job_req));
537 return FAIL;
538 }
539 return OK;
540 }
541
542 /*
543 * Give an error and return FAIL unless "args[idx]" is a job.
544 */
545 int
546 check_for_job_arg(typval_T *args, int idx)
547 {
548 if (args[idx].v_type != VAR_JOB)
549 {
550 if (idx >= 0)
551 semsg(_(e_job_required_for_argument_nr), idx + 1);
552 else
553 emsg(_(e_jobreq));
554 return FAIL;
555 }
556 return OK;
557 }
558
559 /*
560 * Give an error and return FAIL unless "args[idx]" is a string or
561 * a number.
562 */
563 int
564 check_for_string_or_number_arg(typval_T *args, int idx)
565 {
566 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_NUMBER)
567 {
568 if (idx >= 0)
569 semsg(_(e_string_required_for_argument_nr), idx + 1);
570 else
571 emsg(_(e_stringreq));
572 return FAIL;
573 }
574 return OK;
575 }
576
577 /*
578 * Give an error and return FAIL unless "args[idx]" is a string or
579 * a number (buffer)
580 */
581 int
582 check_for_buffer_arg(typval_T *args, int idx)
583 {
584 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_NUMBER)
585 {
586 if (idx >= 0)
587 semsg(_(e_string_required_for_argument_nr), idx + 1);
588 else
589 emsg(_(e_stringreq));
590 return FAIL;
591 }
592 return OK;
593 }
594
595 /*
596 * Give an error and return FAIL unless "args[idx]" is a string or
597 * a number (line)
598 */
599 int
600 check_for_lnum_arg(typval_T *args, int idx)
601 {
602 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_NUMBER)
603 {
604 if (idx >= 0)
605 semsg(_(e_string_required_for_argument_nr), idx + 1);
606 else
607 emsg(_(e_stringreq));
608 return FAIL;
609 }
610 return OK;
611 }
612
613 /*
614 * Give an error and return FAIL unless "args[idx]" is a string or
615 * a number (line)
616 */
617 int
618 check_for_opt_lnum_arg(typval_T *args, int idx)
619 {
620 if (args[idx].v_type != VAR_UNKNOWN
621 && args[idx].v_type != VAR_STRING
622 && args[idx].v_type != VAR_NUMBER)
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 {
652 if (idx >= 0)
653 semsg(_(e_string_required_for_argument_nr), idx + 1);
654 else
655 emsg(_(e_stringreq));
453 return FAIL; 656 return FAIL;
454 } 657 }
455 return OK; 658 return OK;
456 } 659 }
457 660