Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move FFI::DataConverter to Ruby #661

Merged
merged 3 commits into from Jan 22, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 */

37 changes: 37 additions & 0 deletions lib/ffi/data_converter.rb
@@ -0,0 +1,37 @@
module FFI
eregon marked this conversation as resolved.
Show resolved Hide resolved
# 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