mobiruby / mruby-cfunc Goto Github PK
View Code? Open in Web Editor NEWInterface to C functions on mruby
License: MIT License
Interface to C functions on mruby
License: MIT License
Not sure if its in mruby or cfunc, but i call
a CFunc::FunctionPointer
that takes a String
, and results a CFunc::Pointer
, the next time i call it, the previous result has become the argument.
dlh = CFunc::call(CFunc::Pointer, "dlopen", "libglib-2.0.so.0", CFunc::Int.new(1))
fun_ptr = CFunc::call(CFunc::Pointer, :dlsym, dlh, "g_strreverse")
f = CFunc::FunctionPointer.new(fun_ptr)
f.result_type = CFunc::Pointer
f.arguments_type = [String]
100.times do
p f.call("reverse").to_s
# results in this pattern
# "esrever" => correct
# "reverse" => incorrect
# ...
end
$ rake
In file included from /Users/Daniel/Projekte/mobiruby-ios/mobiruby-ios/main.m:20:
In file included from /Users/Daniel/Projekte/mobiruby-ios/submodules/mruby-cfunc/include/cfunc_pointer.h:11:
/Users/Daniel/Projekte/mobiruby-ios/submodules/mruby-cfunc/include/cfunc_type.h:12:10: fatal error: 'ffi.h' file not found
#include "ffi.h"
^
1 error generated.
CompileC build/mobiruby-ios.build/Debug-iphonesimulator/mobiruby-ios.build/Objects-normal/i386/AFHTTPClient.o vendors/AFNetworking/AFHTTPClient.m normal i386 objective-c com.apple.compilers.llvm.clang.1_0.compiler
cd /Users/Daniel/Projekte/mobiruby-ios
setenv LANG en_US.US-ASCII
setenv PATH "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:/Users/Daniel/.rbenv/versions/2.0.0-p0/bin:/usr/local/Cellar/rbenv/0.4.0/libexec:/Users/Daniel/.rbenv/shims:/Users/Daniel/.rbenv/shims:/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/share/npm/bin"
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -x objective-c -arch i386 -fmessage-length=0 -std=gnu99 -Wno-trigraphs -fpascal-strings -O0 -Wno-missing-field-initializers -Wno-missing-prototypes -Wreturn-type -Wno-implicit-atomic-properties -Wno-receiver-is-weak -Wduplicate-method-match -Wformat -Wno-missing-braces -Wparentheses -Wswitch -Wno-unused-function -Wno-unused-label -Wno-unused-parameter -Wunused-variable -Wunused-value -Wno-empty-body -Wuninitialized -Wno-unknown-pragmas -Wno-shadow -Wno-four-char-constants -Wno-conversion -Wconstant-conversion -Wint-conversion -Wenum-conversion -Wno-shorten-64-to-32 -Wpointer-sign -Wno-newline-eof -Wno-selector -Wno-strict-selector-match -Wno-undeclared-selector -Wno-deprecated-implementations -DDEBUG=1 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk -fexceptions -fasm-blocks -fstrict-aliasing -Wprotocol -Wdeprecated-declarations -g -Wno-sign-conversion -fobjc-abi-version=2 -fobjc-legacy-dispatch -mios-simulator-version-min=5.0 -iquote /Users/Daniel/Projekte/mobiruby-ios/build/mobiruby-ios.build/Debug-iphonesimulator/mobiruby-ios.build/mobiruby-ios-generated-files.hmap -I/Users/Daniel/Projekte/mobiruby-ios/build/mobiruby-ios.build/Debug-iphonesimulator/mobiruby-ios.build/mobiruby-ios-own-target-headers.hmap -I/Users/Daniel/Projekte/mobiruby-ios/build/mobiruby-ios.build/Debug-iphonesimulator/mobiruby-ios.build/mobiruby-ios-all-target-headers.hmap -iquote /Users/Daniel/Projekte/mobiruby-ios/build/mobiruby-ios.build/Debug-iphonesimulator/mobiruby-ios.build/mobiruby-ios-project-headers.hmap -I/Users/Daniel/Projekte/mobiruby-ios/build/Debug-iphonesimulator/include -I/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include -I/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include -I/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include -I/Users/Daniel/Projekte/mobiruby-ios/build/mobiruby-ios.build/Debug-iphonesimulator/mobiruby-ios.build/DerivedSources/i386 -I/Users/Daniel/Projekte/mobiruby-ios/build/mobiruby-ios.build/Debug-iphonesimulator/mobiruby-ios.build/DerivedSources -F/Users/Daniel/Projekte/mobiruby-ios/build/Debug-iphonesimulator -I/Users/Daniel/Projekte/mobiruby-ios/submodules/mruby/include -I/Users/Daniel/Projekte/mobiruby-ios/submodules/libffi/build/include -I/Users/Daniel/Projekte/mobiruby-ios/submodules/mobiruby-common/include -I/Users/Daniel/Projekte/mobiruby-ios/submodules/mruby-cocoa/include -I/Users/Daniel/Projekte/mobiruby-ios/submodules/mruby-cfunc/include -DALLOC_PADDING=8 -DUSE_MRBC_DATA -fobjc-arc -include /var/folders/nk/3n70lspj49506yk9jgnylfx00000gn/C/com.apple.Xcode.501/SharedPrecompiledHeaders/mobiruby-ios-Prefix-ayfartbqydrngecnlnzvzohkzeek/mobiruby-ios-Prefix.pch -MMD -MT dependencies -MF /Users/Daniel/Projekte/mobiruby-ios/build/mobiruby-ios.build/Debug-iphonesimulator/mobiruby-ios.build/Objects-normal/i386/AFHTTPClient.d --serialize-diagnostics /Users/Daniel/Projekte/mobiruby-ios/build/mobiruby-ios.build/Debug-iphonesimulator/mobiruby-ios.build/Objects-normal/i386/AFHTTPClient.dia -c /Users/Daniel/Projekte/mobiruby-ios/vendors/AFNetworking/AFHTTPClient.m -o /Users/Daniel/Projekte/mobiruby-ios/build/mobiruby-ios.build/Debug-iphonesimulator/mobiruby-ios.build/Objects-normal/i386/AFHTTPClient.o
In file included from /Users/Daniel/Projekte/mobiruby-ios/vendors/AFNetworking/AFHTTPClient.m:25:
/Users/Daniel/Projekte/mobiruby-ios/vendors/AFNetworking/AFHTTPClient.h:83:6: warning: SystemConfiguration framework not found in project, or not included in precompiled header. Network reachability functionality will not be available. [-W#warnings]
#warning SystemConfiguration framework not found in project, or not included in precompiled header. Network reachability functionality will not be available.
^
/Users/Daniel/Projekte/mobiruby-ios/vendors/AFNetworking/AFHTTPClient.h:88:10: warning: MobileCoreServices framework not found in project, or not included in precompiled header. Automatic MIME type detection when uploading files in multipart requests will not be available. [-W#warnings]
#warning MobileCoreServices framework not found in project, or not included in precompiled header. Automatic MIME type detection when uploading files in multipart requests will not be available.
^
/Users/Daniel/Projekte/mobiruby-ios/vendors/AFNetworking/AFHTTPClient.m:570:38: warning: capturing 'operation' strongly in this block is likely to lead to a retain cycle [-Warc-retain-cycles]
dispatch_queue_t queue = operation.successCallbackQueue ?: dispatch_get_main_queue();
^~~~~~~~~
/Users/Daniel/Projekte/mobiruby-ios/vendors/AFNetworking/AFHTTPClient.m:569:9: note: block will be retained by the captured object
operation.completionBlock = ^{
^~~~~~~~~
3 warnings generated.
** BUILD FAILED **
The following build commands failed:
CompileC build/mobiruby-ios.build/Debug-iphonesimulator/mobiruby-ios.build/Objects-normal/i386/main.o mobiruby-ios/main.m normal i386 objective-c com.apple.compilers.llvm.clang.1_0.compiler
(1 failure)
rake aborted!
Command failed with status (65): [xcodebuild -configuration Debug -target mo...]
tasks/build.rake:22:in `block in <top (required)>'
Tasks: TOP => default => run => build/Debug-iphonesimulator/mobiruby-ios.app
(See full trace by running task with --trace)
module CFunc
def self.Int(val); Int.new(val) end
def self.UInt8(val); UIn8.new(val) end # here is problem
I can't find how to do this and this is driving me crazy as that is the last piece of the puzzle I need, can you map the following structure with cfunc and if yes how ?
struct buffer_struct {
uint8_t id;
uint8_t name[5];
};
With the ffi gem this would be mapped with:
class BufferStruct < FFI::Struct
layout(
:id, :uint8,
:name, [:uint8, 5]
)
end
but I can't find an equivalent with cfunc.
class Foo < CFunc::Struct
define CFunc::Int,:int
end
class Bar < CFunc::Struct
define Foo,:foo
end
b=Bar.new
#will raise here
b[:foo] = Foo.new
#
b[:foo][:int]=1
p b[:foo][:int] #=> 1
crashed make test on closures.
test_10_define_closure: make[1]: *** [run] Bus error: 10```
c:/mingw/bin/../lib/gcc/mingw32/4.7.2/../../../../mingw32/bin/ld.exe: 警告: PE
ターゲットに対して --export-dynamic はサポートされていません。--export-all-symbo
ls を行いたいのではないですか?
LD build/host/bin/mirb.exe
c:/mingw/bin/../lib/gcc/mingw32/4.7.2/../../../../mingw32/bin/ld.exe: 警告: PE
ターゲットに対して --export-dynamic はサポートされていません。--export-all-symbo
ls を行いたいのではないですか?
--export-dynamic
in mrbgem.rake should be removed on windows.
RString
has changed.
I made some changes and had gotten it to build, however, CFunc::call
segfaults, i had only made changes concerning s->member
ie (s->as.heap.member
)
On OSX objc_allocateClassPair parameters or return value gets mangled somehow.
The last output line of this example should be "Test" and not a blank string ""
obj = CFunc::call(CFunc::Pointer, "objc_getClass", 'NSObject')
v = CFunc::Int.new
v.value = 0
klass = CFunc::call(CFunc::Pointer, "objc_allocateClassPair", obj, 'Test', v)
name = CFunc::call(CFunc::Pointer, "class_getName", obj)
puts name.to_s.inspect # => "NSObject"
name = CFunc::call(CFunc::Pointer, "class_getName", klass)
puts name.to_s.inspect # => ""
Both ffi library itself using c and ruby ffi behaves as expected with the exact same sequence of calls.
I cannot figure if it is something I did wrong or if this is an issue with cfunc itself, first here is the relevant C headers:
typedef struct {
unsigned long number;
unsigned long size;
sigar_cpu_t *data;
} sigar_cpu_list_t;
int sigar_cpu_list_get(sigar_t *sigar, sigar_cpu_list_t *cpulist);
and here is my test code:
lib = CFunc::call(CFunc::Pointer, "dlopen", "libsigar.dylib", CFunc::Int.new(2))
sigar_cpu_list_get_func = CFunc::call(CFunc::Pointer, "dlsym", lib, "sigar_cpu_list_get")
class CpuListStruct < CFunc::Struct
define(
CFunc::UInt64, :number,
CFunc::UInt64, :size,
CFunc::Pointer, :data
)
end
cpus = CpuListStruct2.new
ret = CFunc::call(CFunc::Int, sigar_cpu_list_get_func, sigar, cpus)
I get a segfault on the last line, "sigar" is an opaque object which was initialized previously but I know this part works because if I replace the last two lines with the following it works as expected:
cpus = CFunc::Pointer.malloc(3 * 8)
ret = CFunc::call(CFunc::Int, sigar_cpu_list_get_func, sigar, cpus)
p [:ret, ret.value]
p CFunc::UInt64.get(cpus)
I got this:
[:ret, 0]
4
Which is correct since I have 4 cores.
Any idea why the struct code segfault ? I tried to trace it and here is the result:
Reason: KERN_INVALID_ADDRESS at address: 0x0000000000000000
sigar_cpu_list_create (cpulist=0x0) at sigar.c:582
582 cpulist->number = 0;
(gdb) bt
#0 sigar_cpu_list_create (cpulist=0x0) at sigar.c:582
#1 0x000000010020e956 in sigar_cpu_list_get (sigar=0x10013fbe0, cpulist=0x0) at darwin_sigar.c:875
#2 0x00000001000a55b4 in ffi_call_unix64 ()
#3 0x0000000100141130 in ?? ()
a null pointer is passed to the c function instead of the structure address :(
I also tried using cpus.addr as the last argument but it crashes later in cfunc_uint64_class_get also trying to access a 0x0 address.
Ps: sorry for the multiple edit, github keyboard shortcuts are stupid...
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.