1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* ext/syck/rubyext.c (syck_loader_transfer): check type conversion.

* ext/syck/rubyext.c (syck_parser_assign_io, rb_new_syck_node): duck
  typing.

* ext/syck/rubyext.c (syck_parser_s_alloc, syck_parser_initialize):
  allocation framework.

* ext/syck/rubyext.c (syck_emitter_s_alloc, syck_emitter_initialize):
  ditto.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@7647 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2004-12-23 17:23:02 +00:00
parent ca5b499241
commit 4bdc2a662d
2 changed files with 230 additions and 210 deletions

View file

@ -1,3 +1,16 @@
Fri Dec 24 02:22:53 2004 Nobuyoshi Nakada <nobu@ruby-lang.org>
* ext/syck/rubyext.c (syck_loader_transfer): check type conversion.
* ext/syck/rubyext.c (syck_parser_assign_io, rb_new_syck_node): duck
typing.
* ext/syck/rubyext.c (syck_parser_s_alloc, syck_parser_initialize):
allocation framework.
* ext/syck/rubyext.c (syck_emitter_s_alloc, syck_emitter_initialize):
ditto.
Fri Dec 24 01:21:00 2004 Hidetoshi NAGAI <nagai@ai.kyutech.ac.jp>
* ext/tk/lib/tkextlib/blt.rb: add BLT extension support

View file

@ -49,7 +49,7 @@ typedef struct {
/*
* symbols and constants
*/
static ID s_new, s_utc, s_at, s_to_f, s_to_i, s_read, s_binmode, s_call, s_cmp, s_transfer, s_update, s_dup, s_match, s_keys, s_to_str, s_unpack, s_tr_bang, s_anchors, s_default_set;
static ID s_new, s_utc, s_at, s_to_f, s_to_i, s_read, s_binmode, s_call, s_cmp, s_transfer, s_update, s_dup, s_match, s_keys, s_unpack, s_tr_bang, s_anchors, s_default_set;
static ID s_anchors, s_domain, s_families, s_kind, s_name, s_options, s_private_types, s_type_id, s_value;
static VALUE sym_model, sym_generic, sym_input, sym_bytecode;
static VALUE sym_scalar, sym_seq, sym_map;
@ -158,9 +158,11 @@ syck_parser_assign_io(parser, port)
VALUE port;
{
int taint = Qtrue;
if (rb_respond_to(port, s_to_str)) {
VALUE tmp;
if (!NIL_P(tmp = rb_check_string_type(port))) {
taint = OBJ_TAINTED(port); /* original taintedness */
StringValue(port); /* possible conversion */
port = tmp;
syck_parser_str( parser, RSTRING(port)->ptr, RSTRING(port)->len, NULL );
}
else if (rb_respond_to(port, s_read)) {
@ -365,23 +367,25 @@ rb_new_syck_node( obj, type_id )
{
long i = 0;
SyckNode *n = NULL;
VALUE tmp;
if (rb_respond_to(obj, s_to_str))
if (!NIL_P(tmp = rb_check_string_type(obj)))
{
StringValue(obj); /* possible conversion */
obj = tmp;
n = syck_alloc_str();
n->data.str->ptr = RSTRING(obj)->ptr;
n->data.str->len = RSTRING(obj)->len;
}
else if ( rb_obj_is_kind_of( obj, rb_cArray ) )
else if (!NIL_P(tmp = rb_check_array_type(obj)))
{
obj = tmp;
n = syck_alloc_seq();
for ( i = 0; i < RARRAY(obj)->len; i++ )
{
syck_seq_add(n, rb_ary_entry(obj, i));
}
}
else if ( rb_obj_is_kind_of( obj, rb_cHash ) )
else if (!NIL_P(tmp = rb_check_convert_type(obj, T_HASH, "Hash", "to_hash")))
{
VALUE keys;
n = syck_alloc_map();
@ -393,9 +397,9 @@ rb_new_syck_node( obj, type_id )
}
}
if ( n!= NULL && rb_respond_to( type_id, s_to_str ) )
if (n != NULL && !NIL_P(tmp = rb_check_string_type(type_id)))
{
StringValue(type_id);
type_id = tmp;
n->type_id = syck_strndup( RSTRING(type_id)->ptr, RSTRING(type_id)->len );
}
@ -780,25 +784,15 @@ syck_mark_parser(parser)
* YAML::Syck::Parser.new
*/
VALUE
syck_parser_new(argc, argv, class)
int argc;
VALUE *argv;
syck_parser_s_alloc(class)
VALUE class;
{
VALUE pobj, options, init_argv[1];
VALUE pobj;
SyckParser *parser = syck_new_parser();
rb_scan_args(argc, argv, "01", &options);
pobj = Data_Wrap_Struct( class, syck_mark_parser, syck_free_parser, parser );
syck_parser_set_root_on_error( parser, Qnil );
if ( ! rb_obj_is_instance_of( options, rb_cHash ) )
{
options = rb_hash_new();
}
init_argv[0] = options;
rb_obj_call_init(pobj, 1, init_argv);
return pobj;
}
@ -806,9 +800,20 @@ syck_parser_new(argc, argv, class)
* YAML::Syck::Parser.initialize( options )
*/
static VALUE
syck_parser_initialize( self, options )
VALUE self, options;
syck_parser_initialize(argc, argv, self)
int argc;
VALUE *argv;
VALUE self;
{
VALUE options;
if (rb_scan_args(argc, argv, "01", &options) == 0)
{
options = rb_hash_new();
}
else {
Check_Type(options, T_HASH);
}
rb_ivar_set(self, s_options, options);
return self;
}
@ -1068,14 +1073,15 @@ syck_loader_transfer( self, type, val )
{
char *taguri = NULL;
if (NIL_P(type) || !RSTRING(type)->ptr || RSTRING(type)->len == 0)
if (NIL_P(type) || RSTRING(StringValue(type))->len == 0)
{
/*
* Empty transfer, detect type
*/
if ( TYPE(val) == T_STRING )
VALUE tmp = rb_check_string_type(val);
if (!NIL_P(tmp))
{
StringValue(val);
val = tmp;
taguri = syck_match_implicit( RSTRING(val)->ptr, RSTRING(val)->len );
taguri = syck_taguri( YAML_DOMAIN, taguri, strlen( taguri ) );
}
@ -1282,7 +1288,7 @@ rb_syck_output_handler( emitter, str, len )
long len;
{
VALUE dest = (VALUE)emitter->bonus;
if ( rb_respond_to( dest, s_to_str ) ) {
if (TYPE(dest) == T_STRING) {
rb_str_cat( dest, str, len );
} else {
rb_io_write( dest, rb_str_new( str, len ) );
@ -1307,26 +1313,17 @@ syck_mark_emitter(emitter)
* YAML::Syck::Emitter.new
*/
VALUE
syck_emitter_new(argc, argv, class)
int argc;
VALUE *argv;
syck_emitter_s_alloc(class)
VALUE class;
{
VALUE pobj, options, init_argv[1];
VALUE pobj;
SyckEmitter *emitter = syck_new_emitter();
rb_scan_args(argc, argv, "01", &options);
pobj = Data_Wrap_Struct( class, syck_mark_emitter, syck_free_emitter, emitter );
syck_emitter_ignore_id( emitter, Qnil );
syck_emitter_handler( emitter, rb_syck_output_handler );
emitter->bonus = (void *)rb_str_new2( "" );
if ( ! rb_obj_is_instance_of( options, rb_cHash ) )
{
options = rb_hash_new();
}
init_argv[0] = options;
rb_obj_call_init(pobj, 1, init_argv);
return pobj;
}
@ -1334,9 +1331,20 @@ syck_emitter_new(argc, argv, class)
* YAML::Syck::Emitter.initialize( options )
*/
static VALUE
syck_emitter_initialize( self, options )
VALUE self, options;
syck_emitter_initialize(argc, argv, self)
int argc;
VALUE *argv;
VALUE self;
{
VALUE options;
if (rb_scan_args(argc, argv, "01", &options) == 0)
{
options = rb_hash_new();
}
else {
Check_Type(options, T_HASH);
}
rb_ivar_set(self, s_options, options);
return self;
}
@ -1468,7 +1476,6 @@ Init_syck()
s_default_set = rb_intern("default=");
s_match = rb_intern("match");
s_keys = rb_intern("keys");
s_to_str = rb_intern("to_str");
s_tr_bang = rb_intern("tr!");
s_unpack = rb_intern("unpack");
@ -1515,8 +1522,8 @@ Init_syck()
*/
cParser = rb_define_class_under( rb_syck, "Parser", rb_cObject );
rb_define_attr( cParser, "options", 1, 1 );
rb_define_singleton_method( cParser, "new", syck_parser_new, -1 );
rb_define_method(cParser, "initialize", syck_parser_initialize, 1);
rb_define_alloc_func( cParser, syck_parser_s_alloc );
rb_define_method(cParser, "initialize", syck_parser_initialize, -1);
rb_define_method(cParser, "load", syck_parser_load, -1);
rb_define_method(cParser, "load_documents", syck_parser_load_documents, -1);
@ -1571,8 +1578,8 @@ Init_syck()
* Define YAML::Syck::Emitter class
*/
cEmitter = rb_define_class_under( rb_syck, "Emitter", rb_cObject );
rb_define_singleton_method( cEmitter, "new", syck_emitter_new, -1 );
rb_define_method( cEmitter, "initialize", syck_emitter_initialize, 1 );
rb_define_alloc_func( cEmitter, syck_emitter_s_alloc );
rb_define_method( cEmitter, "initialize", syck_emitter_initialize, -1 );
rb_define_method( cEmitter, "level", syck_emitter_level_m, 0 );
rb_define_method( cEmitter, "write", syck_emitter_write_m, 1 );
rb_define_method( cEmitter, "<<", syck_emitter_write_m, 1 );