rubyapi 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,22 @@
1
+ *.dll
2
+ *.so
3
+ *.bundle
4
+ *.o
5
+ *.gem
6
+ *.rbc
7
+ .bundle
8
+ .config
9
+ coverage
10
+ InstalledFiles
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+
19
+ # YARD artifacts
20
+ .yardoc
21
+ _yardoc
22
+ doc/
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ Copyright 2012 Kim Burgestrand. All rights reserved.
2
+
3
+ Redistribution and use in source and binary forms, with or without modification, are
4
+ permitted provided that the following conditions are met:
5
+
6
+ 1. Redistributions of source code must retain the above copyright notice, this list of
7
+ conditions and the following disclaimer.
8
+
9
+ 2. Redistributions in binary form must reproduce the above copyright notice, this list
10
+ of conditions and the following disclaimer in the documentation and/or other materials
11
+ provided with the distribution.
12
+
13
+ THIS SOFTWARE IS PROVIDED BY KIM BURGESTRAND ``AS IS'' AND ANY EXPRESS OR IMPLIED
14
+ WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
15
+ FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KIM BURGESTRAND OR
16
+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
17
+ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
18
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
19
+ ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
20
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
21
+ ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,213 @@
1
+
2
+ SHELL = /bin/sh
3
+
4
+ # V=0 quiet, V=1 verbose. other values don't work.
5
+ V = 0
6
+ Q1 = $(V:1=)
7
+ Q = $(Q1:0=@)
8
+ n=$(NULLCMD)
9
+ ECHO1 = $(V:1=@$n)
10
+ ECHO = $(ECHO1:0=@echo)
11
+
12
+ #### Start of system configuration section. ####
13
+
14
+ srcdir = .
15
+ topdir = /Users/Kim/.rvm/rubies/ruby-1.9.3-p125/include/ruby-1.9.1
16
+ hdrdir = /Users/Kim/.rvm/rubies/ruby-1.9.3-p125/include/ruby-1.9.1
17
+ arch_hdrdir = /Users/Kim/.rvm/rubies/ruby-1.9.3-p125/include/ruby-1.9.1/$(arch)
18
+ VPATH = $(srcdir):$(arch_hdrdir)/ruby:$(hdrdir)/ruby
19
+ prefix = $(DESTDIR)/Users/Kim/.rvm/rubies/ruby-1.9.3-p125
20
+ rubylibprefix = $(libdir)/$(RUBY_BASE_NAME)
21
+ exec_prefix = $(prefix)
22
+ vendorhdrdir = $(rubyhdrdir)/vendor_ruby
23
+ sitehdrdir = $(rubyhdrdir)/site_ruby
24
+ rubyhdrdir = $(includedir)/$(RUBY_BASE_NAME)-$(ruby_version)
25
+ vendordir = $(rubylibprefix)/vendor_ruby
26
+ sitedir = $(rubylibprefix)/site_ruby
27
+ ridir = $(datarootdir)/$(RI_BASE_NAME)
28
+ mandir = $(datarootdir)/man
29
+ localedir = $(datarootdir)/locale
30
+ libdir = $(exec_prefix)/lib
31
+ psdir = $(docdir)
32
+ pdfdir = $(docdir)
33
+ dvidir = $(docdir)
34
+ htmldir = $(docdir)
35
+ infodir = $(datarootdir)/info
36
+ docdir = $(datarootdir)/doc/$(PACKAGE)
37
+ oldincludedir = $(DESTDIR)/usr/include
38
+ includedir = $(prefix)/include
39
+ localstatedir = $(prefix)/var
40
+ sharedstatedir = $(prefix)/com
41
+ sysconfdir = $(prefix)/etc
42
+ datadir = $(datarootdir)
43
+ datarootdir = $(prefix)/share
44
+ libexecdir = $(exec_prefix)/libexec
45
+ sbindir = $(exec_prefix)/sbin
46
+ bindir = $(exec_prefix)/bin
47
+ rubylibdir = $(rubylibprefix)/$(ruby_version)
48
+ archdir = $(rubylibdir)/$(arch)
49
+ sitelibdir = $(sitedir)/$(ruby_version)
50
+ sitearchdir = $(sitelibdir)/$(sitearch)
51
+ vendorlibdir = $(vendordir)/$(ruby_version)
52
+ vendorarchdir = $(vendorlibdir)/$(sitearch)
53
+
54
+ NULLCMD = :
55
+
56
+ CC = /usr/bin/gcc-4.2
57
+ CXX = g++-4.2
58
+ LIBRUBY = $(LIBRUBY_SO)
59
+ LIBRUBY_A = lib$(RUBY_SO_NAME)-static.a
60
+ LIBRUBYARG_SHARED = -l$(RUBY_SO_NAME)
61
+ LIBRUBYARG_STATIC = -l$(RUBY_SO_NAME)-static
62
+ OUTFLAG = -o
63
+ COUTFLAG = -o
64
+
65
+ RUBY_EXTCONF_H =
66
+ cflags = $(optflags) $(debugflags) $(warnflags)
67
+ optflags = -O3
68
+ debugflags = -ggdb
69
+ warnflags = -Wextra -Wno-unused-parameter -Wno-parentheses -Wno-long-long -Wno-missing-field-initializers -Wpointer-arith -Wwrite-strings -Wdeclaration-after-statement -Wshorten-64-to-32 -Wimplicit-function-declaration
70
+ CFLAGS = -fno-common $(cflags) -fno-common -pipe $(ARCH_FLAG)
71
+ INCFLAGS = -I. -I$(arch_hdrdir) -I$(hdrdir)/ruby/backward -I$(hdrdir) -I$(srcdir)
72
+ DEFS =
73
+ CPPFLAGS = -I/Users/Kim/.rvm/usr/include -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE $(DEFS) $(cppflags)
74
+ CXXFLAGS = $(CFLAGS) $(cxxflags)
75
+ ldflags = -L.
76
+ dldflags = -Wl,-undefined,dynamic_lookup -Wl,-multiply_defined,suppress -Wl,-flat_namespace
77
+ ARCH_FLAG =
78
+ DLDFLAGS = $(ldflags) $(dldflags) $(ARCH_FLAG)
79
+ LDSHARED = $(CC) -dynamic -bundle
80
+ LDSHAREDXX = $(CXX) -dynamic -bundle
81
+ AR = ar
82
+ EXEEXT =
83
+
84
+ RUBY_BASE_NAME = ruby
85
+ RUBY_INSTALL_NAME = ruby
86
+ RUBY_SO_NAME = ruby.1.9.1
87
+ arch = x86_64-darwin11.3.0
88
+ sitearch = $(arch)
89
+ ruby_version = 1.9.1
90
+ ruby = /Users/Kim/.rvm/rubies/ruby-1.9.3-p125/bin/ruby
91
+ RUBY = $(ruby)
92
+ RM = rm -f
93
+ RM_RF = $(RUBY) -run -e rm -- -rf
94
+ RMDIRS = rmdir -p
95
+ MAKEDIRS = mkdir -p
96
+ INSTALL = /usr/bin/install -c
97
+ INSTALL_PROG = $(INSTALL) -m 0755
98
+ INSTALL_DATA = $(INSTALL) -m 644
99
+ COPY = cp
100
+
101
+ #### End of system configuration section. ####
102
+
103
+ preload =
104
+
105
+ libpath = . $(libdir) /Users/Kim/.rvm/usr/lib
106
+ LIBPATH = -L. -L$(libdir) -L/Users/Kim/.rvm/usr/lib
107
+ DEFFILE =
108
+
109
+ CLEANFILES = mkmf.log
110
+ DISTCLEANFILES =
111
+ DISTCLEANDIRS =
112
+
113
+ extout =
114
+ extout_prefix =
115
+ target_prefix =
116
+ LOCAL_LIBS =
117
+ LIBS = $(LIBRUBYARG_SHARED) -lpthread -ldl -lobjc
118
+ SRCS = rubyapi_ext.c
119
+ OBJS = rubyapi_ext.o
120
+ TARGET = rubyapi_ext
121
+ DLLIB = $(TARGET).bundle
122
+ EXTSTATIC =
123
+ STATIC_LIB =
124
+
125
+ BINDIR = $(bindir)
126
+ RUBYCOMMONDIR = $(sitedir)$(target_prefix)
127
+ RUBYLIBDIR = $(sitelibdir)$(target_prefix)
128
+ RUBYARCHDIR = $(sitearchdir)$(target_prefix)
129
+ HDRDIR = $(rubyhdrdir)/ruby$(target_prefix)
130
+ ARCHHDRDIR = $(rubyhdrdir)/$(arch)/ruby$(target_prefix)
131
+
132
+ TARGET_SO = $(DLLIB)
133
+ CLEANLIBS = $(TARGET).bundle
134
+ CLEANOBJS = *.o *.bak
135
+
136
+ all: $(DLLIB)
137
+ static: $(STATIC_LIB)
138
+ .PHONY: all install static install-so install-rb
139
+ .PHONY: clean clean-so clean-rb
140
+
141
+ clean-rb-default::
142
+ clean-rb::
143
+ clean-so::
144
+ clean: clean-so clean-rb-default clean-rb
145
+ @-$(RM) $(CLEANLIBS) $(CLEANOBJS) $(CLEANFILES)
146
+
147
+ distclean-rb-default::
148
+ distclean-rb::
149
+ distclean-so::
150
+ distclean: clean distclean-so distclean-rb-default distclean-rb
151
+ @-$(RM) Makefile $(RUBY_EXTCONF_H) conftest.* mkmf.log
152
+ @-$(RM) core ruby$(EXEEXT) *~ $(DISTCLEANFILES)
153
+ @-$(RMDIRS) $(DISTCLEANDIRS) 2> /dev/null || true
154
+
155
+ realclean: distclean
156
+ install: install-so install-rb
157
+
158
+ install-so: $(RUBYARCHDIR)
159
+ install-so: $(RUBYARCHDIR)/$(DLLIB)
160
+ $(RUBYARCHDIR)/$(DLLIB): $(DLLIB)
161
+ @-$(MAKEDIRS) $(@D)
162
+ $(INSTALL_PROG) $(DLLIB) $(@D)
163
+ install-rb: pre-install-rb install-rb-default
164
+ install-rb-default: pre-install-rb-default
165
+ pre-install-rb: Makefile
166
+ pre-install-rb-default: Makefile
167
+ pre-install-rb-default:
168
+ $(ECHO) installing default rubyapi_ext libraries
169
+ $(RUBYARCHDIR):
170
+ $(Q) $(MAKEDIRS) $@
171
+
172
+ site-install: site-install-so site-install-rb
173
+ site-install-so: install-so
174
+ site-install-rb: install-rb
175
+
176
+ .SUFFIXES: .c .m .cc .mm .cxx .cpp .C .o
177
+
178
+ .cc.o:
179
+ $(ECHO) compiling $(<)
180
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<
181
+
182
+ .mm.o:
183
+ $(ECHO) compiling $(<)
184
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<
185
+
186
+ .cxx.o:
187
+ $(ECHO) compiling $(<)
188
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<
189
+
190
+ .cpp.o:
191
+ $(ECHO) compiling $(<)
192
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<
193
+
194
+ .C.o:
195
+ $(ECHO) compiling $(<)
196
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<
197
+
198
+ .c.o:
199
+ $(ECHO) compiling $(<)
200
+ $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -c $<
201
+
202
+ .m.o:
203
+ $(ECHO) compiling $(<)
204
+ $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -c $<
205
+
206
+ $(DLLIB): $(OBJS) Makefile
207
+ $(ECHO) linking shared-object $(DLLIB)
208
+ @-$(RM) $(@)
209
+ $(Q) $(LDSHARED) -o $@ $(OBJS) $(LIBPATH) $(DLDFLAGS) $(LOCAL_LIBS) $(LIBS)
210
+
211
+
212
+
213
+ $(OBJS): $(hdrdir)/ruby.h $(hdrdir)/ruby/defines.h $(arch_hdrdir)/ruby/config.h
@@ -0,0 +1,32 @@
1
+ rubyapi
2
+ =======
3
+
4
+ Access help to the Ruby C library from FFI.
5
+
6
+ Installation
7
+ ------------
8
+
9
+ gem install rubyapi
10
+
11
+ Usage
12
+ -----
13
+
14
+ ```ruby
15
+
16
+ RubyAPI.attach_function :new_array, :rb_array_new, [], :object
17
+ RubyAPI.attach_function :inspect, :rb_p, [ :object ], :void
18
+
19
+ RubyAPI.inspect("Hello, world!")
20
+ RubyAPI.inspect(RubyAPI.new_array)
21
+
22
+ ```
23
+
24
+ WHY?
25
+ ----
26
+
27
+ :trollface:
28
+
29
+ License
30
+ -------
31
+
32
+ Simplified (2-clause) BSD license.
@@ -0,0 +1,23 @@
1
+ begin
2
+ require "bundler/gem_tasks"
3
+ rescue LoadError
4
+ end
5
+
6
+ task :compile do
7
+ sh 'ruby extconf.rb'
8
+ sh 'make'
9
+ end
10
+
11
+ task :console => :compile do
12
+ exec 'irb -I. -rrubyapi'
13
+ end
14
+
15
+ require 'rake/testtask'
16
+ Rake::TestTask.new(:test) do |t|
17
+ t.libs = ["."]
18
+ t.pattern = "*_spec.rb"
19
+ t.verbose = true
20
+ end
21
+ task :test => :compile
22
+
23
+ task :default => :test
@@ -0,0 +1,3 @@
1
+ require 'mkmf'
2
+
3
+ create_makefile 'rubyapi_ext'
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.name = "rubyapi"
5
+
6
+ gem.authors = ["Kim Burgestrand"]
7
+ gem.email = ["kim@burgestrand.se"]
8
+ gem.summary = %q{Access help to the Ruby C library from FFI}
9
+ gem.license = "Simplified BSD License"
10
+ gem.homepage = "https://github.com/Burgestrand/rubyapi"
11
+
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = ["rubyapi_spec.rb"]
14
+ gem.require_paths = ["."]
15
+ gem.extensions = ["extconf.rb"]
16
+ gem.version = "1.0.1"
17
+
18
+ gem.add_dependency 'ffi'
19
+ end
20
+
@@ -0,0 +1,30 @@
1
+ require 'ffi'
2
+ require 'rubyapi_ext'
3
+
4
+ module RubyAPI
5
+ extend FFI::Library
6
+ ffi_lib FFI::CURRENT_PROCESS
7
+
8
+ class VALUE
9
+ extend FFI::DataConverter
10
+ native_type FFI::Type::POINTER
11
+
12
+ def self.from_native(value, ctx)
13
+ RubyAPI.from_native(super.address)
14
+ end
15
+
16
+ def self.to_native(value, ctx)
17
+ FFI::Pointer.new(RubyAPI.to_native(value))
18
+ end
19
+ end
20
+
21
+ typedef VALUE, :object
22
+ end
23
+
24
+ class Object
25
+ # This is a bit of a dangerous hack, as FFI will now accept any
26
+ # object as a pointer argument anywhere. This renders the FFI type
27
+ # checking useless for pointers.
28
+ def to_ptr
29
+ end
30
+ end
@@ -0,0 +1,34 @@
1
+ #include <ruby.h>
2
+
3
+ static VALUE to_native(VALUE self, VALUE value)
4
+ {
5
+ #if SIZEOF_VALUE == SIZEOF_LONG_LONG
6
+ return LL2NUM(value);
7
+ #elif SIZEOF_VALUE == SIZEOF_LONG
8
+ return LONG2NUM(value);
9
+ #elif SIZEOF_VALUE == SIZEOF_INT
10
+ return INT2NUM(value);
11
+ #else
12
+ rb_raise(rb_eRuntimeError, "size of VALUE is %d", SIZEOF_VALUE);
13
+ #endif
14
+ }
15
+
16
+ static VALUE from_native(VALUE self, VALUE value)
17
+ {
18
+ #if SIZEOF_VALUE == SIZEOF_LONG_LONG
19
+ return (VALUE) NUM2LL(value);
20
+ #elif SIZEOF_VALUE == SIZEOF_LONG
21
+ return (VALUE) NUM2LONG(value);
22
+ #elif SIZEOF_VALUE == SIZEOF_INT
23
+ return (VALUE) NUM2INT(value);
24
+ #else
25
+ rb_raise(rb_eRuntimeError, "size of VALUE is %d", SIZEOF_VALUE);
26
+ #endif
27
+ }
28
+
29
+ void Init_rubyapi_ext()
30
+ {
31
+ VALUE rb_mCRuby = rb_define_module("RubyAPI");
32
+ rb_define_singleton_method(rb_mCRuby, "to_native", to_native, 1);
33
+ rb_define_singleton_method(rb_mCRuby, "from_native", from_native, 1);
34
+ }
@@ -0,0 +1,35 @@
1
+ $LOAD_PATH.unshift File.dirname(__FILE__)
2
+
3
+ require 'rubyapi'
4
+ require 'minitest/spec'
5
+ require 'minitest/autorun'
6
+ require 'stringio'
7
+
8
+ def capture_stdout
9
+ old_stdout, $stdout = $stdout, StringIO.new
10
+ yield
11
+ $stdout.rewind
12
+ $stdout.read
13
+ ensure
14
+ $stdout = old_stdout
15
+ end
16
+
17
+ describe RubyAPI do
18
+ it "allows binding ruby C API" do
19
+ RubyAPI.attach_function :new_array, :rb_ary_new, [], :object
20
+ RubyAPI.new_array.must_equal []
21
+ end
22
+
23
+ it "runs the example from the README" do
24
+ RubyAPI.attach_function :new_array, :rb_ary_new, [], :object
25
+ RubyAPI.attach_function :inspect, :rb_p, [ :object ], :void
26
+
27
+ stdout = capture_stdout do
28
+ RubyAPI.inspect("Hello, world!")
29
+ RubyAPI.inspect(RubyAPI.new_array)
30
+ end
31
+
32
+ stdout.must_match /Hello, world!/
33
+ stdout.must_match "[]"
34
+ end
35
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubyapi
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Kim Burgestrand
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: ffi
16
+ requirement: &70339498764720 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70339498764720
25
+ description:
26
+ email:
27
+ - kim@burgestrand.se
28
+ executables: []
29
+ extensions:
30
+ - extconf.rb
31
+ extra_rdoc_files: []
32
+ files:
33
+ - .gitignore
34
+ - LICENSE
35
+ - Makefile
36
+ - README.md
37
+ - Rakefile
38
+ - extconf.rb
39
+ - rubyapi.gemspec
40
+ - rubyapi.rb
41
+ - rubyapi_ext.c
42
+ - rubyapi_spec.rb
43
+ homepage: https://github.com/Burgestrand/rubyapi
44
+ licenses:
45
+ - Simplified BSD License
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - .
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubyforge_project:
64
+ rubygems_version: 1.8.17
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: Access help to the Ruby C library from FFI
68
+ test_files:
69
+ - rubyapi_spec.rb
70
+ has_rdoc: