Skip to content
Browse files

Stop using symbols for property keys during deserialization. Fixes #9.

  • Loading branch information...
1 parent 1b47dac commit 3f16cf841d9c36045f720b307bbffa61fb63a7ac @warhammerkid warhammerkid committed
View
32 ext/rocketamf_ext/deserializer.c
@@ -111,21 +111,6 @@ VALUE des_read_string(AMF_DESERIALIZER *des, unsigned int len) {
}
/*
- * Because Ruby 1.8 doesn't have a good optimization for looking up symbols from
- * C strings, this function does the lookup without requiring any additional
- * allocations.
- */
-VALUE des_read_sym(AMF_DESERIALIZER *des, unsigned int len) {
- DES_BOUNDS_CHECK(des, len);
- char end = des->stream[des->pos+len];
- des->stream[des->pos+len] = '\0';
- VALUE sym = ID2SYM(rb_intern(des->stream + des->pos));
- des->stream[des->pos+len] = end;
- des->pos += len;
- return sym;
-}
-
-/*
* Set the source of the amf reader to a StringIO object, creating a new one to
* wrap the source if it's only a string
*/
@@ -164,10 +149,9 @@ static VALUE des0_read_amf3(VALUE self) {
}
/*
- * Reads an AMF0 hash, with a configurable key reading function - either
- * des_read_string or des_read_sym
+ * Reads an AMF0 hash
*/
-static void des0_read_props(VALUE self, VALUE hash, VALUE(*read_key)(AMF_DESERIALIZER*, unsigned int)) {
+static void des0_read_props(VALUE self, VALUE hash) {
AMF_DESERIALIZER *des;
Data_Get_Struct(self, AMF_DESERIALIZER, des);
@@ -177,7 +161,7 @@ static void des0_read_props(VALUE self, VALUE hash, VALUE(*read_key)(AMF_DESERIA
des_read_byte(des); // Read type byte
return;
} else {
- VALUE key = read_key(des, len);
+ VALUE key = des_read_string(des, len);
char type = des_read_byte(des);
rb_hash_aset(hash, key, des0_deserialize(self, type));
}
@@ -194,7 +178,7 @@ static VALUE des0_read_object(VALUE self) {
// Populate object
VALUE props = rb_hash_new();
- des0_read_props(self, props, des_read_sym);
+ des0_read_props(self, props);
rb_funcall(des->class_mapper, id_populate_ruby_obj, 2, obj, props);
return obj;
@@ -211,7 +195,7 @@ static VALUE des0_read_typed_object(VALUE self) {
// Populate object
VALUE props = rb_hash_new();
- des0_read_props(self, props, des_read_sym);
+ des0_read_props(self, props);
rb_funcall(des->class_mapper, id_populate_ruby_obj, 2, obj, props);
return obj;
@@ -223,7 +207,7 @@ static VALUE des0_read_hash(VALUE self) {
des_read_uint32(des); // Hash size, but there's no optimization I can perform with this
VALUE obj = rb_hash_new();
rb_ary_push(des->obj_cache, obj);
- des0_read_props(self, obj, des_read_string);
+ des0_read_props(self, obj);
return obj;
}
@@ -409,7 +393,7 @@ static VALUE des3_read_object(VALUE self) {
VALUE props = rb_hash_new();
for(i = 0; i < members_len; i++) {
- rb_hash_aset(props, rb_str_intern(RARRAY_PTR(members)[i]), des3_deserialize(self));
+ rb_hash_aset(props, RARRAY_PTR(members)[i], des3_deserialize(self));
}
VALUE dynamic_props = Qnil;
@@ -418,7 +402,7 @@ static VALUE des3_read_object(VALUE self) {
while(1) {
VALUE key = des3_read_string(des);
if(RSTRING_LEN(key) == 0) break;
- rb_hash_aset(dynamic_props, rb_str_intern(key), des3_deserialize(self));
+ rb_hash_aset(dynamic_props, key, des3_deserialize(self));
}
}
View
9 lib/rocketamf/pure/deserializer.rb
@@ -129,12 +129,11 @@ def amf0_read_date
time
end
- def amf0_read_props obj={}, key_type=:sym
+ def amf0_read_props obj={}
while true
key = amf0_read_string
type = read_int8 @source
break if type == AMF0_OBJECT_END_MARKER
- key = key.to_sym if key_type == :sym
obj[key] = amf0_deserialize(type)
end
obj
@@ -144,7 +143,7 @@ def amf0_read_hash
len = read_word32_network(@source) # Read and ignore length
obj = {}
@ref_cache << obj
- amf0_read_props obj, :string
+ amf0_read_props obj
end
def amf0_read_object add_to_ref_cache=true
@@ -364,7 +363,7 @@ def amf3_read_object
props = {}
traits[:members].each do |key|
value = amf3_deserialize
- props[key.to_sym] = value
+ props[key] = value
end
dynamic_props = nil
@@ -372,7 +371,7 @@ def amf3_read_object
dynamic_props = {}
while (key = amf3_read_string) && key.length != 0 do # read next key
value = amf3_deserialize
- dynamic_props[key.to_sym] = value
+ dynamic_props[key] = value
end
end
View
34 spec/deserializer_spec.rb
@@ -85,7 +85,7 @@
it "should deserialize anonymous objects" do
input = object_fixture('amf0-object.bin')
output = RocketAMF.deserialize(input, 0)
- output.should == {:foo => 'baz', :bar => 3.14}
+ output.should == {'foo' => 'baz', 'bar' => 3.14}
output.type.should == ""
end
@@ -94,7 +94,7 @@
output = RocketAMF.deserialize(input, 0)
output.type.should == 'org.amf.ASClass'
- output.should == {:foo => 'bar', :baz => nil}
+ output.should == {'foo' => 'bar', 'baz' => nil}
end
it "should deserialize a mapped object as a mapped ruby class instance" do
@@ -210,9 +210,9 @@
output = RocketAMF.deserialize(input, 3)
expected = {
- :property_one => 'foo',
- :nil_property => nil,
- :another_public_property => 'a_public_value'
+ 'property_one' => 'foo',
+ 'nil_property' => nil,
+ 'another_public_property' => 'a_public_value'
}
output.should == expected
output.type.should == ""
@@ -243,7 +243,7 @@
it "should deserialize a hash as a dynamic anonymous object" do
input = object_fixture("amf3-hash.bin")
output = RocketAMF.deserialize(input, 3)
- output.should == {:foo => "bar", :answer => 42}
+ output.should == {'foo' => "bar", 'answer' => 42}
end
it "should deserialize an empty array" do
@@ -268,9 +268,9 @@
input = object_fixture("amf3-mixed-array.bin")
output = RocketAMF.deserialize(input, 3)
- h1 = {:foo_one => "bar_one"}
- h2 = {:foo_two => ""}
- so1 = {:foo_three => 42}
+ h1 = {'foo_one' => "bar_one"}
+ h2 = {'foo_two' => ""}
+ so1 = {'foo_three' => 42}
output.should == [h1, h2, so1, {}, [h1, h2, so1], [], 42, "", [], "", {}, "bar_one", so1]
end
@@ -344,9 +344,9 @@
it "should deserialize Vector.<Object>" do
input = object_fixture('amf3-vector-object.bin')
output = RocketAMF.deserialize(input, 3)
- output[0][:foo].should == 'foo'
+ output[0]['foo'].should == 'foo'
output[1].type.should == 'org.amf.ASClass'
- output[2][:foo].should == 'baz'
+ output[2]['foo'].should == 'baz'
end
end
@@ -357,7 +357,7 @@
foo = "foo"
bar = "str"
- output.should == [foo, bar, foo, bar, foo, {:str => "foo"}]
+ output.should == [foo, bar, foo, bar, foo, {'str' => "foo"}]
end
it "should not reference the empty string" do
@@ -380,8 +380,8 @@
input = object_fixture("amf3-object-ref.bin")
output = RocketAMF.deserialize(input, 3)
- obj1 = {:foo => "bar"}
- obj2 = {:foo => obj1[:foo]}
+ obj1 = {'foo' => "bar"}
+ obj2 = {'foo' => obj1['foo']}
output.should == [[obj1, obj2], "bar", [obj1, obj2]]
end
@@ -430,9 +430,9 @@
input = object_fixture("amf3-graph-member.bin")
output = RocketAMF.deserialize(input, 3)
- output[:children][0][:parent].should === output
- output[:parent].should === nil
- output[:children].length.should == 2
+ output['children'][0]['parent'].should === output
+ output['parent'].should === nil
+ output['children'].length.should == 2
# Expected object:
# parent = Hash.new
# child1 = Hash.new
View
4 spec/fast_class_mapping_spec.rb
@@ -78,8 +78,8 @@
end
it "should populate a typed hash" do
- obj = @mapper.populate_ruby_obj RocketAMF::Values::TypedHash.new('UnmappedClass'), {:prop_a => 'Data'}
- obj[:prop_a].should == 'Data'
+ obj = @mapper.populate_ruby_obj RocketAMF::Values::TypedHash.new('UnmappedClass'), {'prop_a' => 'Data'}
+ obj['prop_a'].should == 'Data'
end
end

0 comments on commit 3f16cf8

Please sign in to comment.
Something went wrong with that request. Please try again.