extpp 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0a6f11af116da3bf20bffbb10008e43f8f03e41f
4
- data.tar.gz: adf0040c8f4c3e15aa60093b96a774530f9a09d2
3
+ metadata.gz: 5e8320433f1d3dfa3badfd3b28f46f9a3b73a9cd
4
+ data.tar.gz: b0c5a9aa30a8b40f568a1cd523310bc3e31e0063
5
5
  SHA512:
6
- metadata.gz: '0927493217502f10fc9f96a54ff1970a992ad4eec539c2ce8f5c03a35c22f917f20183c76ee3557ac245c9e033afc5289976a275267299abcb24983048473e94'
7
- data.tar.gz: c1b5f0e6710d8abcc2a97a5912176b9536fcd7652870d636b443949f408bbf14dd58711feb174630a84337379868190a6f820efd0e37c2bec26ed481dce2e392
6
+ metadata.gz: 9ea0d50417278ab94fe8297c846fc35dc69baae3667b21b149a5d615ecd9d0e5c47a3417113d96788111483aceabeb6fe75badbe89aaf1d27180518a389d68ea
7
+ data.tar.gz: 3c264c961f438c27bee7ab0020c471cc8c60f1badee85dee42ed916643e592c90ec4a21c1af1caf84a8ccee063e249be56ef7c8abe4963a42fda5ff6483c28c6
data/README.md CHANGED
@@ -16,14 +16,86 @@ can use C API directory from C++.
16
16
 
17
17
  ## Install
18
18
 
19
- TODO
19
+ ```console
20
+ % gem install extpp
21
+ ```
20
22
 
21
23
  ## How to use
22
24
 
23
- TODO
25
+ Here is an example to create `Hello` class.
26
+
27
+ Create `hello.cpp`:
28
+
29
+ ```c++
30
+ #include <ruby.hpp>
31
+
32
+ RB_BEGIN_DECLS
33
+
34
+ void
35
+ Init_hello(void)
36
+ {
37
+ rb::Class klass("Hello");
38
+ klass.define_method("initialize", [](VALUE rb_self, int argc, VALUE *argv) {
39
+ VALUE rb_name;
40
+ rb_scan_args(argc, argv, "1", &rb_name);
41
+ rb::Object self(rb_self);
42
+ self.ivar_set("@name", rb_name);
43
+ return Qnil;
44
+ });
45
+ klass.define_method("greet", [](VALUE rb_self) {
46
+ rb::Object self(rb_self);
47
+ rb::Object prefix(rb_str_new_cstr("Hello "));
48
+ auto message = prefix.send("+", {self.ivar_get("@name")});
49
+ return static_cast<VALUE>(message);
50
+ });
51
+ }
52
+
53
+ RB_END_DECLS
54
+ ```
55
+
56
+ This code equals to the following Ruby code:
57
+
58
+ ```ruby
59
+ class Hello
60
+ def initialize(name)
61
+ @name = name
62
+ end
63
+
64
+ def greet
65
+ "Hello " + @name
66
+ end
67
+ end
68
+ ```
69
+
70
+ Create `extconf.rb`:
71
+
72
+ ```ruby
73
+ require "extpp"
74
+
75
+ create_makefile("hello")
76
+ ```
77
+
78
+ Create `Makefile`:
79
+
80
+ ```console
81
+ % ruby extconf.rb
82
+ ```
83
+
84
+ Build this extension:
85
+
86
+ ```console
87
+ % make
88
+ ```
89
+
90
+ Now, you can use this extension:
91
+
92
+ ```console
93
+ % ruby -r ./hello -e 'p Hello.new("me").greet'
94
+ "Hello me"
95
+ ```
24
96
 
25
97
  ## License
26
98
 
27
- Copyright (C) 2017 Kouhei Sutou
99
+ Copyright (C) 2017-2018 Kouhei Sutou
28
100
 
29
101
  The 2-Clause BSD License. See [LICENSE.txt](LICENSE.txt) for details.
data/doc/text/news.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # News
2
2
 
3
+ ## 0.0.2 - 2018-02-16
4
+
5
+ ### Improvements
6
+
7
+ * `rb::Object::ivar_set()`: Added.
8
+
9
+ * `rb::Object::ivar_get()`: Added.
10
+
11
+ ### Fixes
12
+
13
+ * Added missing files to gem.
14
+
3
15
  ## 0.0.1 - 2018-02-16
4
16
 
5
17
  The first release!!!
@@ -0,0 +1,43 @@
1
+ #pragma once
2
+
3
+ #include <ruby.hpp>
4
+
5
+ namespace rb {
6
+ class Function {
7
+ public:
8
+ Function() = default;
9
+ virtual ~Function() = default;
10
+
11
+ virtual VALUE call(VALUE self, int argc, VALUE *argv) = 0;
12
+ };
13
+
14
+ class FunctionWithoutArgument : public Function {
15
+ public:
16
+ FunctionWithoutArgument(const MethodWithoutArguments &function);
17
+
18
+ VALUE call(VALUE self, int argc, VALUE *argv) override;
19
+
20
+ private:
21
+ MethodWithoutArguments function_;
22
+ };
23
+
24
+ class FunctionWithArguments : public Function {
25
+ public:
26
+ FunctionWithArguments(const MethodWithArguments &function);
27
+
28
+ VALUE call(VALUE self, int argc, VALUE *argv) override;
29
+
30
+ private:
31
+ MethodWithArguments function_;
32
+ };
33
+
34
+ class FunctionWithArgumentsCompatible : public Function {
35
+ public:
36
+ FunctionWithArgumentsCompatible(const MethodWithArgumentsCompatible &function);
37
+
38
+ VALUE call(VALUE self, int argc, VALUE *argv) override;
39
+
40
+ private:
41
+ MethodWithArgumentsCompatible function_;
42
+ };
43
+ }
@@ -89,6 +89,30 @@ namespace rb {
89
89
  return send(rb_intern_str(name), args, block);
90
90
  }
91
91
 
92
+ inline Object ivar_set(ID name_id, VALUE value) {
93
+ return Object(rb_ivar_set(rb_object_, name_id, value));
94
+ }
95
+
96
+ inline Object ivar_set(const char *name, VALUE value) {
97
+ return ivar_set(rb_intern(name), value);
98
+ }
99
+
100
+ inline Object ivar_set(Object name, VALUE value) {
101
+ return ivar_set(rb_intern_str(name), value);
102
+ }
103
+
104
+ inline Object ivar_get(ID name_id) {
105
+ return Object(rb_ivar_get(rb_object_, name_id));
106
+ }
107
+
108
+ inline Object ivar_get(const char *name) {
109
+ return ivar_get(rb_intern(name));
110
+ }
111
+
112
+ inline Object ivar_get(Object name) {
113
+ return ivar_get(rb_intern_str(name));
114
+ }
115
+
92
116
  private:
93
117
  VALUE rb_object_;
94
118
  bool is_gc_guarding_;
data/lib/extpp/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module ExtPP
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,13 @@
1
+ task :default => :build
2
+
3
+ desc "Build"
4
+ task :build => "hello.so"
5
+
6
+ file "Makefile" => "extconf.rb" do
7
+ ruby("extconf.rb")
8
+ sh("make", "clean")
9
+ end
10
+
11
+ file "hello.so" => ["Makefile", "hello.cpp"] do
12
+ sh("make")
13
+ end
@@ -0,0 +1,3 @@
1
+ require "extpp"
2
+
3
+ create_makefile("hello")
@@ -0,0 +1,24 @@
1
+ #include <ruby.hpp>
2
+
3
+ RB_BEGIN_DECLS
4
+
5
+ void
6
+ Init_hello(void)
7
+ {
8
+ rb::Class klass("Hello");
9
+ klass.define_method("initialize", [](VALUE rb_self, int argc, VALUE *argv) {
10
+ VALUE rb_name;
11
+ rb_scan_args(argc, argv, "1", &rb_name);
12
+ rb::Object self(rb_self);
13
+ self.ivar_set("@name", rb_name);
14
+ return Qnil;
15
+ });
16
+ klass.define_method("greet", [](VALUE rb_self) {
17
+ rb::Object self(rb_self);
18
+ rb::Object prefix(rb_str_new_cstr("Hello "));
19
+ auto message = prefix.send("+", {self.ivar_get("@name")});
20
+ return static_cast<VALUE>(message);
21
+ });
22
+ }
23
+
24
+ RB_END_DECLS
@@ -0,0 +1,264 @@
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
+ ECHO1 = $(V:1=@:)
9
+ ECHO = $(ECHO1:0=@echo)
10
+ NULLCMD = :
11
+
12
+ #### Start of system configuration section. ####
13
+
14
+ srcdir = .
15
+ topdir = /usr/include/ruby-2.3.0
16
+ hdrdir = $(topdir)
17
+ arch_hdrdir = /usr/include/x86_64-linux-gnu/ruby-2.3.0
18
+ PATH_SEPARATOR = :
19
+ VPATH = $(srcdir):$(arch_hdrdir)/ruby:$(hdrdir)/ruby
20
+ prefix = $(DESTDIR)/usr
21
+ rubysitearchprefix = $(sitearchlibdir)/$(RUBY_BASE_NAME)
22
+ rubyarchprefix = $(archlibdir)/$(RUBY_BASE_NAME)
23
+ rubylibprefix = $(libdir)/$(RUBY_BASE_NAME)
24
+ exec_prefix = $(prefix)
25
+ vendorarchhdrdir = $(sitearchincludedir)/$(RUBY_VERSION_NAME)/vendor_ruby
26
+ sitearchhdrdir = $(sitearchincludedir)/$(RUBY_VERSION_NAME)/site_ruby
27
+ rubyarchhdrdir = $(archincludedir)/$(RUBY_VERSION_NAME)
28
+ vendorhdrdir = $(rubyhdrdir)/vendor_ruby
29
+ sitehdrdir = $(rubyhdrdir)/site_ruby
30
+ rubyhdrdir = $(includedir)/$(RUBY_VERSION_NAME)
31
+ vendorarchdir = $(rubysitearchprefix)/vendor_ruby/$(ruby_version)
32
+ vendorlibdir = $(vendordir)/$(ruby_version)
33
+ vendordir = $(rubylibprefix)/vendor_ruby
34
+ sitearchdir = $(DESTDIR)/usr/local/lib/x86_64-linux-gnu/site_ruby
35
+ sitelibdir = $(sitedir)/$(ruby_version)
36
+ sitedir = $(DESTDIR)/usr/local/lib/site_ruby
37
+ rubyarchdir = $(rubyarchprefix)/$(ruby_version)
38
+ rubylibdir = $(rubylibprefix)/$(ruby_version)
39
+ sitearchincludedir = $(includedir)/$(sitearch)
40
+ archincludedir = $(includedir)/$(arch)
41
+ sitearchlibdir = $(libdir)/$(sitearch)
42
+ archlibdir = $(libdir)/$(arch)
43
+ ridir = $(datarootdir)/$(RI_BASE_NAME)
44
+ mandir = $(prefix)/share/man
45
+ localedir = $(datarootdir)/locale
46
+ libdir = $(exec_prefix)/lib
47
+ psdir = $(docdir)
48
+ pdfdir = $(docdir)
49
+ dvidir = $(docdir)
50
+ htmldir = $(docdir)
51
+ infodir = $(prefix)/share/info
52
+ docdir = $(datarootdir)/doc/$(PACKAGE)
53
+ oldincludedir = $(DESTDIR)/usr/include
54
+ includedir = $(prefix)/include
55
+ runstatedir = $(localstatedir)/run
56
+ localstatedir = $(DESTDIR)/var
57
+ sharedstatedir = $(prefix)/com
58
+ sysconfdir = $(DESTDIR)/etc
59
+ datadir = $(datarootdir)
60
+ datarootdir = $(prefix)/share
61
+ libexecdir = $(prefix)/lib/ruby2.3
62
+ sbindir = $(exec_prefix)/sbin
63
+ bindir = $(exec_prefix)/bin
64
+ archdir = $(rubyarchdir)
65
+
66
+
67
+ CC = gcc
68
+ CXX = g++
69
+ LIBRUBY = $(LIBRUBY_SO)
70
+ LIBRUBY_A = lib$(RUBY_SO_NAME)-static.a
71
+ LIBRUBYARG_SHARED = -l$(RUBY_SO_NAME)
72
+ LIBRUBYARG_STATIC = -l$(RUBY_SO_NAME)-static
73
+ empty =
74
+ OUTFLAG = -o $(empty)
75
+ COUTFLAG = -o $(empty)
76
+
77
+ RUBY_EXTCONF_H =
78
+ cflags = $(optflags) $(debugflags) $(warnflags)
79
+ cxxflags = $(optflags) $(debugflags) $(warnflags)
80
+ optflags = -O3 -fno-fast-math
81
+ debugflags = -ggdb3
82
+ warnflags = -Wall -Wextra -Wno-unused-parameter -Wno-parentheses -Wno-long-long -Wno-missing-field-initializers -Wunused-variable -Wpointer-arith -Wwrite-strings -Wdeclaration-after-statement -Wimplicit-function-declaration -Wdeprecated-declarations -Wno-packed-bitfield-compat
83
+ CCDLFLAGS = -fPIC
84
+ CFLAGS = $(CCDLFLAGS) -g -O2 -fdebug-prefix-map=/build/ruby2.3-KwDbD6/ruby2.3-2.3.6=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC $(ARCH_FLAG)
85
+ INCFLAGS = -I. -I$(arch_hdrdir) -I$(hdrdir)/ruby/backward -I$(hdrdir) -I$(srcdir) -I/home/kou/work/ruby/extpp/include
86
+ DEFS =
87
+ CPPFLAGS = -Wdate-time -D_FORTIFY_SOURCE=2 $(DEFS) $(cppflags)
88
+ CXXFLAGS = $(CCDLFLAGS) -g -O2 -fdebug-prefix-map=/build/ruby2.3-KwDbD6/ruby2.3-2.3.6=. -fstack-protector-strong -Wformat -Werror=format-security $(ARCH_FLAG)
89
+ ldflags = -L. -Wl,-z,relro -Wl,-z,now -fstack-protector -rdynamic -Wl,-export-dynamic
90
+ dldflags =
91
+ ARCH_FLAG =
92
+ DLDFLAGS = $(ldflags) $(dldflags) $(ARCH_FLAG)
93
+ LDSHARED = $(CC) -shared
94
+ LDSHAREDXX = $(CXX) -shared
95
+ AR = ar
96
+ EXEEXT =
97
+
98
+ RUBY_INSTALL_NAME = $(RUBY_BASE_NAME)2.3
99
+ RUBY_SO_NAME = ruby-2.3
100
+ RUBYW_INSTALL_NAME =
101
+ RUBY_VERSION_NAME = $(RUBY_BASE_NAME)-$(ruby_version)
102
+ RUBYW_BASE_NAME = rubyw
103
+ RUBY_BASE_NAME = ruby
104
+
105
+ arch = x86_64-linux-gnu
106
+ sitearch = $(arch)
107
+ ruby_version = 2.3.0
108
+ ruby = $(bindir)/$(RUBY_BASE_NAME)2.3
109
+ RUBY = $(ruby)
110
+ ruby_headers = $(hdrdir)/ruby.h $(hdrdir)/ruby/ruby.h $(hdrdir)/ruby/defines.h $(hdrdir)/ruby/missing.h $(hdrdir)/ruby/intern.h $(hdrdir)/ruby/st.h $(hdrdir)/ruby/subst.h $(arch_hdrdir)/ruby/config.h
111
+
112
+ RM = rm -f
113
+ RM_RF = $(RUBY) -run -e rm -- -rf
114
+ RMDIRS = rmdir --ignore-fail-on-non-empty -p
115
+ MAKEDIRS = /bin/mkdir -p
116
+ INSTALL = /usr/bin/install -c
117
+ INSTALL_PROG = $(INSTALL) -m 0755
118
+ INSTALL_DATA = $(INSTALL) -m 644
119
+ COPY = cp
120
+ TOUCH = exit >
121
+
122
+ #### End of system configuration section. ####
123
+
124
+ preload =
125
+
126
+ libpath = . $(archlibdir)
127
+ LIBPATH = -L. -L$(archlibdir)
128
+ DEFFILE =
129
+
130
+ CLEANFILES = mkmf.log
131
+ DISTCLEANFILES =
132
+ DISTCLEANDIRS =
133
+
134
+ extout =
135
+ extout_prefix =
136
+ target_prefix =
137
+ LOCAL_LIBS =
138
+ LIBS = $(LIBRUBYARG_SHARED) -lpthread -lgmp -ldl -lcrypt -lm -lc /home/kou/work/ruby/extpp/ext/extpp/libruby-extpp.so
139
+ ORIG_SRCS = cast.cpp
140
+ SRCS = $(ORIG_SRCS)
141
+ OBJS = cast.o
142
+ HDRS =
143
+ TARGET = cast
144
+ TARGET_NAME = cast
145
+ TARGET_ENTRY = Init_$(TARGET_NAME)
146
+ DLLIB = $(TARGET).so
147
+ EXTSTATIC =
148
+ STATIC_LIB =
149
+
150
+ TIMESTAMP_DIR = .
151
+ BINDIR = $(bindir)
152
+ RUBYCOMMONDIR = $(sitedir)$(target_prefix)
153
+ RUBYLIBDIR = $(sitelibdir)$(target_prefix)
154
+ RUBYARCHDIR = $(sitearchdir)$(target_prefix)
155
+ HDRDIR = $(rubyhdrdir)/ruby$(target_prefix)
156
+ ARCHHDRDIR = $(rubyhdrdir)/$(arch)/ruby$(target_prefix)
157
+
158
+ TARGET_SO = $(DLLIB)
159
+ CLEANLIBS = $(TARGET).so
160
+ CLEANOBJS = *.o *.bak
161
+
162
+ all: $(DLLIB)
163
+ static: $(STATIC_LIB)
164
+ .PHONY: all install static install-so install-rb
165
+ .PHONY: clean clean-so clean-static clean-rb
166
+
167
+ clean-static::
168
+ clean-rb-default::
169
+ clean-rb::
170
+ clean-so::
171
+ clean: clean-so clean-static clean-rb-default clean-rb
172
+ -$(Q)$(RM) $(CLEANLIBS) $(CLEANOBJS) $(CLEANFILES) .*.time
173
+
174
+ distclean-rb-default::
175
+ distclean-rb::
176
+ distclean-so::
177
+ distclean-static::
178
+ distclean: clean distclean-so distclean-static distclean-rb-default distclean-rb
179
+ -$(Q)$(RM) Makefile $(RUBY_EXTCONF_H) conftest.* mkmf.log
180
+ -$(Q)$(RM) core ruby$(EXEEXT) *~ $(DISTCLEANFILES)
181
+ -$(Q)$(RMDIRS) $(DISTCLEANDIRS) 2> /dev/null || true
182
+
183
+ realclean: distclean
184
+ install: install-so install-rb
185
+
186
+ install-so: $(DLLIB) $(TIMESTAMP_DIR)/.RUBYARCHDIR.time
187
+ $(INSTALL_PROG) $(DLLIB) $(RUBYARCHDIR)
188
+ clean-static::
189
+ -$(Q)$(RM) $(STATIC_LIB)
190
+ install-rb: pre-install-rb install-rb-default
191
+ install-rb-default: pre-install-rb-default
192
+ pre-install-rb: Makefile
193
+ pre-install-rb-default: Makefile
194
+ pre-install-rb-default:
195
+ @$(NULLCMD)
196
+ $(TIMESTAMP_DIR)/.RUBYARCHDIR.time:
197
+ $(Q) $(MAKEDIRS) $(@D) $(RUBYARCHDIR)
198
+ $(Q) $(TOUCH) $@
199
+
200
+ site-install: site-install-so site-install-rb
201
+ site-install-so: install-so
202
+ site-install-rb: install-rb
203
+
204
+ .SUFFIXES: .c .m .cc .mm .cxx .cpp .o .S
205
+
206
+ .cc.o:
207
+ $(ECHO) compiling $(<)
208
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<
209
+
210
+ .cc.S:
211
+ $(ECHO) translating $(<)
212
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -S $<
213
+
214
+ .mm.o:
215
+ $(ECHO) compiling $(<)
216
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<
217
+
218
+ .mm.S:
219
+ $(ECHO) translating $(<)
220
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -S $<
221
+
222
+ .cxx.o:
223
+ $(ECHO) compiling $(<)
224
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<
225
+
226
+ .cxx.S:
227
+ $(ECHO) translating $(<)
228
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -S $<
229
+
230
+ .cpp.o:
231
+ $(ECHO) compiling $(<)
232
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<
233
+
234
+ .cpp.S:
235
+ $(ECHO) translating $(<)
236
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -S $<
237
+
238
+ .c.o:
239
+ $(ECHO) compiling $(<)
240
+ $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -c $<
241
+
242
+ .c.S:
243
+ $(ECHO) translating $(<)
244
+ $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -S $<
245
+
246
+ .m.o:
247
+ $(ECHO) compiling $(<)
248
+ $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -c $<
249
+
250
+ .m.S:
251
+ $(ECHO) translating $(<)
252
+ $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -S $<
253
+
254
+ $(DLLIB): $(OBJS) Makefile
255
+ $(ECHO) linking shared-object $(DLLIB)
256
+ -$(Q)$(RM) $(@)
257
+ $(Q) $(LDSHAREDXX) -o $@ $(OBJS) $(LIBPATH) $(DLDFLAGS) $(LOCAL_LIBS) $(LIBS)
258
+
259
+
260
+
261
+ $(OBJS): $(HDRS) $(ruby_headers)
262
+
263
+ extpp_headers = /home/kou/work/ruby/extpp/include/ruby.hpp /home/kou/work/ruby/extpp/include/ruby/object.hpp /home/kou/work/ruby/extpp/include/ruby/type.hpp /home/kou/work/ruby/extpp/include/ruby/cast.hpp /home/kou/work/ruby/extpp/include/ruby/class.hpp
264
+ $(OBJS): $(extpp_headers)