Skip to content

Commit

Permalink
Move FFI::DataConverter to Ruby (#661)
Browse files Browse the repository at this point in the history
* Move FFI::DataConverter to Ruby

* Include DataConverter in StructByReference in Ruby code to simplify
  ordering.

* Remove no longer used rbffi_Type_Find()

* Add license header

* The same as errno.rb. DataConverter.c was added by Wayne Meissner.
  • Loading branch information
tduehr committed Jan 22, 2019
2 parents d0cfb72 + 516e8f8 commit 353d787
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 122 deletions.
91 changes: 0 additions & 91 deletions ext/ffi_c/DataConverter.c

This file was deleted.

8 changes: 1 addition & 7 deletions ext/ffi_c/StructByReference.c
Expand Up @@ -172,19 +172,13 @@ sbr_from_native(VALUE self, VALUE value, VALUE ctx)
void
rbffi_StructByReference_Init(VALUE moduleFFI)
{
/*
* Document-class: FFI::StructByReference
* This class includes {FFI::DataConverter} module.
*/
rbffi_StructByReferenceClass = rb_define_class_under(moduleFFI, "StructByReference", rb_cObject);
rb_global_variable(&rbffi_StructByReferenceClass);
rb_include_module(rbffi_StructByReferenceClass, rb_const_get(moduleFFI, rb_intern("DataConverter")));


rb_define_alloc_func(rbffi_StructByReferenceClass, sbr_allocate);
rb_define_method(rbffi_StructByReferenceClass, "initialize", sbr_initialize, 1);
rb_define_method(rbffi_StructByReferenceClass, "struct_class", sbr_struct_class, 0);
rb_define_method(rbffi_StructByReferenceClass, "native_type", sbr_native_type, 0);
rb_define_method(rbffi_StructByReferenceClass, "to_native", sbr_to_native, 2);
rb_define_method(rbffi_StructByReferenceClass, "from_native", sbr_from_native, 2);
}

18 changes: 0 additions & 18 deletions ext/ffi_c/Type.c
Expand Up @@ -235,24 +235,6 @@ rbffi_Type_Lookup(VALUE name)
return Qnil;
}

/**
* rbffi_Type_Find() is like rbffi_Type_Lookup, but an error is raised if the
* type is not found.
*/
VALUE
rbffi_Type_Find(VALUE name)
{
VALUE rbType = rbffi_Type_Lookup(name);

if (!RTEST(rbType)) {
VALUE s = rb_inspect(name);
rb_raise(rb_eTypeError, "invalid type, %s", RSTRING_PTR(s));
RB_GC_GUARD(s);
}

return rbType;
}

void
rbffi_Type_Init(VALUE moduleFFI)
{
Expand Down
1 change: 0 additions & 1 deletion ext/ffi_c/Type.h
Expand Up @@ -52,7 +52,6 @@ struct Type_ {

extern VALUE rbffi_TypeClass;
extern VALUE rbffi_Type_Lookup(VALUE type);
extern VALUE rbffi_Type_Find(VALUE type);

#ifdef __cplusplus
}
Expand Down
3 changes: 0 additions & 3 deletions ext/ffi_c/ffi.c
Expand Up @@ -74,8 +74,6 @@ Init_ffi_c(void)
/* FFI::Type needs to be initialized before most other classes */
rbffi_Type_Init(moduleFFI);

rbffi_DataConverter_Init(moduleFFI);

rbffi_ArrayType_Init(moduleFFI);
rbffi_LastError_Init(moduleFFI);
rbffi_Call_Init(moduleFFI);
Expand All @@ -95,4 +93,3 @@ Init_ffi_c(void)
rbffi_Types_Init(moduleFFI);
rbffi_MappedType_Init(moduleFFI);
}

2 changes: 0 additions & 2 deletions ext/ffi_c/rbffi.h
Expand Up @@ -44,7 +44,6 @@ extern void rbffi_Type_Init(VALUE ffiModule);
extern void rbffi_Buffer_Init(VALUE ffiModule);
extern void rbffi_Invoker_Init(VALUE ffiModule);
extern void rbffi_Variadic_Init(VALUE ffiModule);
extern void rbffi_DataConverter_Init(VALUE ffiModule);
extern VALUE rbffi_AbstractMemoryClass, rbffi_InvokerClass;
extern int rbffi_type_size(VALUE type);
extern void rbffi_Thread_Init(VALUE moduleFFI);
Expand All @@ -54,4 +53,3 @@ extern void rbffi_Thread_Init(VALUE moduleFFI);
#endif

#endif /* RBFFI_RBFFI_H */

67 changes: 67 additions & 0 deletions lib/ffi/data_converter.rb
@@ -0,0 +1,67 @@
#
# Copyright (C) 2008-2010 Wayne Meissner
#
# This file is part of ruby-ffi.
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * Neither the name of the Ruby FFI project nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.#

module FFI
# This module is used to extend somes classes and give then a common API.
#
# Most of methods defined here must be overriden.
module DataConverter
# Get native type.
#
# @overload native_type(type)
# @param [String, Symbol, Type] type
# @return [Type]
# Get native type from +type+.
#
# @overload native_type
# @raise {NotImplementedError} This method must be overriden.
def native_type(type = nil)
if type
@native_type = FFI.find_type(type)
else
native_type = @native_type
unless native_type
raise NotImplementedError, 'native_type method not overridden and no native_type set'
end
native_type
end
end

# Convert to a native type.
def to_native(value, ctx)
value
end

# Convert from a native type.
def from_native(value, ctx)
value
end
end
end
1 change: 1 addition & 0 deletions lib/ffi/ffi.rb
Expand Up @@ -29,6 +29,7 @@
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

require 'ffi/platform'
require 'ffi/data_converter'
require 'ffi/types'
require 'ffi/library'
require 'ffi/errno'
Expand Down
5 changes: 5 additions & 0 deletions lib/ffi/struct.rb
Expand Up @@ -368,4 +368,9 @@ def array_layout(builder, spec)
end
end
end

# This class includes the {FFI::DataConverter} module.
class StructByReference
include DataConverter
end
end

0 comments on commit 353d787

Please sign in to comment.