diff options
author | Kim Burgestrand <kim@burgestrand.se> | 2012-07-31 13:59:22 +0200 |
---|---|---|
committer | Wayne Meissner <wmeissner@gmail.com> | 2012-08-01 07:36:25 +1000 |
commit | 815cb09c1ded70203ccba46e5bb502f8f8cbc311 (patch) | |
tree | a0419eed16b4726f8fd6a8b5cd8b86756919a7a5 | |
parent | d2e9bdab8b44641851ed702e43897226beb3b19d (diff) | |
download | ffi-815cb09c1ded70203ccba46e5bb502f8f8cbc311.tar.gz |
MemoryPointer.from_string calls to_str (closes #215)
Being able to call to_str on an object means that object is intended
to be able to be used as a string for all intents and purposes. This
should make it safe for our purposes.
-rw-r--r-- | ext/ffi_c/MemoryPointer.c | 3 | ||||
-rw-r--r-- | spec/ffi/rbx/memory_pointer_spec.rb | 4 |
2 files changed, 6 insertions, 1 deletions
diff --git a/ext/ffi_c/MemoryPointer.c b/ext/ffi_c/MemoryPointer.c index eaa4f92..85679fa 100644 --- a/ext/ffi_c/MemoryPointer.c +++ b/ext/ffi_c/MemoryPointer.c @@ -144,8 +144,9 @@ memptr_mark(Pointer* ptr) } static VALUE -memptr_s_from_string(VALUE klass, VALUE s) +memptr_s_from_string(VALUE klass, VALUE to_str) { + VALUE s = StringValue(to_str); VALUE args[] = { INT2FIX(1), LONG2NUM(RSTRING_LEN(s) + 1), Qfalse }; VALUE obj = rb_class_new_instance(3, args, klass); rb_funcall(obj, rb_intern("put_string"), 2, INT2FIX(0), s); diff --git a/spec/ffi/rbx/memory_pointer_spec.rb b/spec/ffi/rbx/memory_pointer_spec.rb index ec3c5fc..3186edb 100644 --- a/spec/ffi/rbx/memory_pointer_spec.rb +++ b/spec/ffi/rbx/memory_pointer_spec.rb @@ -16,6 +16,10 @@ describe "MemoryPointer" do m.type_size.should eq 1 end + it "does not make a pointer from non-strings" do + expect { FFI::MemoryPointer.from_string(nil) }.to raise_error(TypeError) + end + it "makes a pointer from a string with multibyte characters" do m = FFI::MemoryPointer.from_string("ぱんだ") m.total.should eq 10 |