Fix C extension to handle BSON::Code (no longer a subclass of String)

This commit is contained in:
Kyle Banker 2010-10-01 17:03:26 -04:00
parent 3b5aac6de6
commit 2ed0adbbc6

View File

@ -324,28 +324,6 @@ static int write_element(VALUE key, VALUE value, VALUE extra, int allow_id) {
} }
case T_STRING: case T_STRING:
{ {
if (strcmp(rb_obj_classname(value),
"BSON::Code") == 0) {
buffer_position length_location, start_position, total_length;
int length;
write_name_and_type(buffer, key, 0x0F);
start_position = buffer_get_position(buffer);
length_location = buffer_save_space(buffer, 4);
if (length_location == -1) {
rb_raise(rb_eNoMemError, "failed to allocate memory in buffer.c");
}
length = RSTRING_LEN(value) + 1;
SAFE_WRITE(buffer, (char*)&length, 4);
SAFE_WRITE(buffer, RSTRING_PTR(value), length - 1);
SAFE_WRITE(buffer, &zero, 1);
write_doc(buffer, rb_funcall(value, rb_intern("scope"), 0), Qfalse, Qfalse);
total_length = buffer_get_position(buffer) - start_position;
SAFE_WRITE_AT_POS(buffer, length_location, (const char*)&total_length, 4);
break;
} else {
int length; int length;
write_name_and_type(buffer, key, 0x02); write_name_and_type(buffer, key, 0x02);
value = TO_UTF8(value); value = TO_UTF8(value);
@ -355,7 +333,6 @@ static int write_element(VALUE key, VALUE value, VALUE extra, int allow_id) {
SAFE_WRITE(buffer, &zero, 1); SAFE_WRITE(buffer, &zero, 1);
break; break;
} }
}
case T_SYMBOL: case T_SYMBOL:
{ {
const char* str_value = rb_id2name(SYM2ID(value)); const char* str_value = rb_id2name(SYM2ID(value));
@ -422,6 +399,28 @@ static int write_element(VALUE key, VALUE value, VALUE extra, int allow_id) {
SAFE_WRITE_AT_POS(buffer, length_location, (const char*)&obj_length, 4); SAFE_WRITE_AT_POS(buffer, length_location, (const char*)&obj_length, 4);
break; break;
} }
if (strcmp(cls, "BSON::Code") == 0) {
buffer_position length_location, start_position, total_length;
int length;
write_name_and_type(buffer, key, 0x0F);
start_position = buffer_get_position(buffer);
length_location = buffer_save_space(buffer, 4);
if (length_location == -1) {
rb_raise(rb_eNoMemError, "failed to allocate memory in buffer.c");
}
VALUE code_str = rb_funcall(value, rb_intern("code"), 0);
length = RSTRING_LEN(code_str) + 1;
SAFE_WRITE(buffer, (char*)&length, 4);
SAFE_WRITE(buffer, RSTRING_PTR(code_str), length - 1);
SAFE_WRITE(buffer, &zero, 1);
write_doc(buffer, rb_funcall(value, rb_intern("scope"), 0), Qfalse, Qfalse);
total_length = buffer_get_position(buffer) - start_position;
SAFE_WRITE_AT_POS(buffer, length_location, (const char*)&total_length, 4);
break;
}
if (strcmp(cls, "BSON::MaxKey") == 0) { if (strcmp(cls, "BSON::MaxKey") == 0) {
write_name_and_type(buffer, key, 0x7f); write_name_and_type(buffer, key, 0x7f);
break; break;