comparison src/if_ruby.c @ 2090:53d475dff0e3 v7.2.374

updated for version 7.2.374 Problem: Ruby eval() doesn't understand Vim types. Solution: Add the vim_to_ruby() function. (George Gensure)
author Bram Moolenaar <bram@zimbu.org>
date Wed, 24 Feb 2010 15:48:04 +0100
parents 4bac7ed34007
children 09cc86b66653
comparison
equal deleted inserted replaced
2089:fd8864aeb52a 2090:53d475dff0e3
658 { 658 {
659 do_cmdline_cmd((char_u *)StringValuePtr(str)); 659 do_cmdline_cmd((char_u *)StringValuePtr(str));
660 return Qnil; 660 return Qnil;
661 } 661 }
662 662
663 #ifdef FEAT_EVAL
664 static VALUE vim_to_ruby(typval_T *tv)
665 {
666 VALUE result = Qnil;
667
668 if (tv->v_type == VAR_STRING)
669 {
670 result = rb_str_new2((char *)tv->vval.v_string);
671 }
672 else if (tv->v_type == VAR_NUMBER)
673 {
674 result = INT2NUM(tv->vval.v_number);
675 }
676 # ifdef FEAT_FLOAT
677 else if (tv->v_type == VAR_FLOAT)
678 {
679 result = rb_float_new(tv->vval.v_float);
680 }
681 # endif
682 else if (tv->v_type == VAR_LIST)
683 {
684 list_T *list = tv->vval.v_list;
685 listitem_T *curr;
686
687 result = rb_ary_new();
688
689 if (list != NULL)
690 {
691 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
692 {
693 rb_ary_push(result, vim_to_ruby(&curr->li_tv));
694 }
695 }
696 }
697 else if (tv->v_type == VAR_DICT)
698 {
699 result = rb_hash_new();
700
701 if (tv->vval.v_dict != NULL)
702 {
703 hashtab_T *ht = &tv->vval.v_dict->dv_hashtab;
704 long_u todo = ht->ht_used;
705 hashitem_T *hi;
706 dictitem_T *di;
707
708 for (hi = ht->ht_array; todo > 0; ++hi)
709 {
710 if (!HASHITEM_EMPTY(hi))
711 {
712 --todo;
713
714 di = dict_lookup(hi);
715 rb_hash_aset(result, rb_str_new2((char *)hi->hi_key),
716 vim_to_ruby(&di->di_tv));
717 }
718 }
719 }
720 } /* else return Qnil; */
721
722 return result;
723 }
724 #endif
725
663 static VALUE vim_evaluate(VALUE self UNUSED, VALUE str) 726 static VALUE vim_evaluate(VALUE self UNUSED, VALUE str)
664 { 727 {
665 #ifdef FEAT_EVAL 728 #ifdef FEAT_EVAL
666 char_u *value = eval_to_string((char_u *)StringValuePtr(str), NULL, TRUE); 729 typval_T *tv;
667 730 VALUE result;
668 if (value != NULL) 731
669 { 732 tv = eval_expr((char_u *)StringValuePtr(str), NULL);
670 VALUE val = rb_str_new2((char *)value); 733 if (tv == NULL)
671 vim_free(value); 734 {
672 return val; 735 return Qnil;
673 } 736 }
674 else 737 result = vim_to_ruby(tv);
675 #endif 738
676 return Qnil; 739 free_tv(tv);
740
741 return result;
742 #else
743 return Qnil;
744 #endif
677 } 745 }
678 746
679 static VALUE buffer_new(buf_T *buf) 747 static VALUE buffer_new(buf_T *buf)
680 { 748 {
681 if (buf->b_ruby_ref) 749 if (buf->b_ruby_ref)