├── .gitignore
├── .travis.yml
├── LICENSE
├── README.md
├── Rakefile
├── ext
└── lose_kanone
│ ├── extconf.rb
│ └── lose_kanone.c
├── lib
└── unsafe
│ └── unsafe.rb
├── test
└── test_unsafe.rb
└── unsafe.gemspec
/.gitignore:
--------------------------------------------------------------------------------
1 | *.gem
2 | *.rbc
3 | /.config
4 | /coverage/
5 | /InstalledFiles
6 | /pkg/
7 | /spec/reports/
8 | /test/tmp/
9 | /test/version_tmp/
10 | /tmp/
11 |
12 | ## Specific to RubyMotion:
13 | .dat*
14 | .repl_history
15 | build/
16 |
17 | ## Documentation cache and generated files:
18 | /.yardoc/
19 | /_yardoc/
20 | /doc/
21 | /rdoc/
22 |
23 | ## Environment normalisation:
24 | /.bundle/
25 | /vendor/bundle
26 | /lib/bundler/man/
27 |
28 | # for a library or gem, you might want to ignore these files since the code is
29 | # intended to run in multiple environments; otherwise, check them in:
30 | # Gemfile.lock
31 | # .ruby-version
32 | # .ruby-gemset
33 |
34 | # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
35 | .rvmrc
36 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: ruby
2 | rvm:
3 | - 2.2.1
4 | install:
5 | - cd ext/lose_kanone/
6 | - ruby extconf.rb
7 | - make
8 | env:
9 | - RUBYLIB=ext
10 | deploy:
11 | provider: rubygems
12 | skip_cleanup: true
13 | api_key:
14 | secure: FhV6uOvDY3+4D8WR9M3Wi+CRvV1/pprrH36EOICzmPbnGW25No5maBcaQ3q/cCNB5x7AQnh2egCl0wJjGq2ny7xjYRgtLz9tTg9MoAoO+DUmghY/LHJot9UKRXp73LNy+6M0kpY0XUz+7C5dQxkIwW1SzM6AV2HelgH/F6GOrofp6KwqGNeA0qg5IXVZwuxXAAoX8XUtqGqde/ug1mzIXdVN2RGdR0TTDajYpTAHe7hykQdg9RbRvUJqGtw0TwKjvyX3d93YZ7uHmjzpWkQoHwG9r7cZsEx2B/IvO4cbLOBF+VKeIisyC7iePXy6Vj8YLxNqs2fuYJGjZpKp+0Ev5RYbN5mVq69POwtLhoCbxaRFcC1tiC0wQDNY+mAW3/qrP9Un1bzSOJcW/Dlk7YK/QLwGqS94ahf5k85pn59YbLpaDaqtzBfoUHaqwJKdh74Xxn64EtMrfsubm7fuCq/RzH0USj7hJymn4IQ4WhKi31hRzHs3dB42a0i7riXcUcP7iXkqCNQ91KkV3X/9ZgKOTdG4b3ZvkdTb8L+sQi4Xvg3xQU6vUG/jOzkcjzcxDJpNWihMSowGqPt+nE56P2s6GA9rwe6pTTgVuL0kHWiHylQYxAlhWevkp7OAdGBFS/Pnr0c8fSMKM6lr93ERTzr/BEKGlymEoAS6P+PUqoSj23U=
15 | gem: unsafe
16 | on:
17 | tags: true
18 | repo: bascht/unsafe
19 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU LESSER GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 |
9 | This version of the GNU Lesser General Public License incorporates
10 | the terms and conditions of version 3 of the GNU General Public
11 | License, supplemented by the additional permissions listed below.
12 |
13 | 0. Additional Definitions.
14 |
15 | As used herein, "this License" refers to version 3 of the GNU Lesser
16 | General Public License, and the "GNU GPL" refers to version 3 of the GNU
17 | General Public License.
18 |
19 | "The Library" refers to a covered work governed by this License,
20 | other than an Application or a Combined Work as defined below.
21 |
22 | An "Application" is any work that makes use of an interface provided
23 | by the Library, but which is not otherwise based on the Library.
24 | Defining a subclass of a class defined by the Library is deemed a mode
25 | of using an interface provided by the Library.
26 |
27 | A "Combined Work" is a work produced by combining or linking an
28 | Application with the Library. The particular version of the Library
29 | with which the Combined Work was made is also called the "Linked
30 | Version".
31 |
32 | The "Minimal Corresponding Source" for a Combined Work means the
33 | Corresponding Source for the Combined Work, excluding any source code
34 | for portions of the Combined Work that, considered in isolation, are
35 | based on the Application, and not on the Linked Version.
36 |
37 | The "Corresponding Application Code" for a Combined Work means the
38 | object code and/or source code for the Application, including any data
39 | and utility programs needed for reproducing the Combined Work from the
40 | Application, but excluding the System Libraries of the Combined Work.
41 |
42 | 1. Exception to Section 3 of the GNU GPL.
43 |
44 | You may convey a covered work under sections 3 and 4 of this License
45 | without being bound by section 3 of the GNU GPL.
46 |
47 | 2. Conveying Modified Versions.
48 |
49 | If you modify a copy of the Library, and, in your modifications, a
50 | facility refers to a function or data to be supplied by an Application
51 | that uses the facility (other than as an argument passed when the
52 | facility is invoked), then you may convey a copy of the modified
53 | version:
54 |
55 | a) under this License, provided that you make a good faith effort to
56 | ensure that, in the event an Application does not supply the
57 | function or data, the facility still operates, and performs
58 | whatever part of its purpose remains meaningful, or
59 |
60 | b) under the GNU GPL, with none of the additional permissions of
61 | this License applicable to that copy.
62 |
63 | 3. Object Code Incorporating Material from Library Header Files.
64 |
65 | The object code form of an Application may incorporate material from
66 | a header file that is part of the Library. You may convey such object
67 | code under terms of your choice, provided that, if the incorporated
68 | material is not limited to numerical parameters, data structure
69 | layouts and accessors, or small macros, inline functions and templates
70 | (ten or fewer lines in length), you do both of the following:
71 |
72 | a) Give prominent notice with each copy of the object code that the
73 | Library is used in it and that the Library and its use are
74 | covered by this License.
75 |
76 | b) Accompany the object code with a copy of the GNU GPL and this license
77 | document.
78 |
79 | 4. Combined Works.
80 |
81 | You may convey a Combined Work under terms of your choice that,
82 | taken together, effectively do not restrict modification of the
83 | portions of the Library contained in the Combined Work and reverse
84 | engineering for debugging such modifications, if you also do each of
85 | the following:
86 |
87 | a) Give prominent notice with each copy of the Combined Work that
88 | the Library is used in it and that the Library and its use are
89 | covered by this License.
90 |
91 | b) Accompany the Combined Work with a copy of the GNU GPL and this license
92 | document.
93 |
94 | c) For a Combined Work that displays copyright notices during
95 | execution, include the copyright notice for the Library among
96 | these notices, as well as a reference directing the user to the
97 | copies of the GNU GPL and this license document.
98 |
99 | d) Do one of the following:
100 |
101 | 0) Convey the Minimal Corresponding Source under the terms of this
102 | License, and the Corresponding Application Code in a form
103 | suitable for, and under terms that permit, the user to
104 | recombine or relink the Application with a modified version of
105 | the Linked Version to produce a modified Combined Work, in the
106 | manner specified by section 6 of the GNU GPL for conveying
107 | Corresponding Source.
108 |
109 | 1) Use a suitable shared library mechanism for linking with the
110 | Library. A suitable mechanism is one that (a) uses at run time
111 | a copy of the Library already present on the user's computer
112 | system, and (b) will operate properly with a modified version
113 | of the Library that is interface-compatible with the Linked
114 | Version.
115 |
116 | e) Provide Installation Information, but only if you would otherwise
117 | be required to provide such information under section 6 of the
118 | GNU GPL, and only to the extent that such information is
119 | necessary to install and execute a modified version of the
120 | Combined Work produced by recombining or relinking the
121 | Application with a modified version of the Linked Version. (If
122 | you use option 4d0, the Installation Information must accompany
123 | the Minimal Corresponding Source and Corresponding Application
124 | Code. If you use option 4d1, you must provide the Installation
125 | Information in the manner specified by section 6 of the GNU GPL
126 | for conveying Corresponding Source.)
127 |
128 | 5. Combined Libraries.
129 |
130 | You may place library facilities that are a work based on the
131 | Library side by side in a single library together with other library
132 | facilities that are not Applications and are not covered by this
133 | License, and convey such a combined library under terms of your
134 | choice, if you do both of the following:
135 |
136 | a) Accompany the combined library with a copy of the same work based
137 | on the Library, uncombined with any other library facilities,
138 | conveyed under the terms of this License.
139 |
140 | b) Give prominent notice with the combined library that part of it
141 | is a work based on the Library, and explaining where to find the
142 | accompanying uncombined form of the same work.
143 |
144 | 6. Revised Versions of the GNU Lesser General Public License.
145 |
146 | The Free Software Foundation may publish revised and/or new versions
147 | of the GNU Lesser General Public License from time to time. Such new
148 | versions will be similar in spirit to the present version, but may
149 | differ in detail to address new problems or concerns.
150 |
151 | Each version is given a distinguishing version number. If the
152 | Library as you received it specifies that a certain numbered version
153 | of the GNU Lesser General Public License "or any later version"
154 | applies to it, you have the option of following the terms and
155 | conditions either of that published version or of any later version
156 | published by the Free Software Foundation. If the Library as you
157 | received it does not specify a version number of the GNU Lesser
158 | General Public License, you may choose any version of the GNU Lesser
159 | General Public License ever published by the Free Software Foundation.
160 |
161 | If the Library as you received it specifies that a proxy can decide
162 | whether future versions of the GNU Lesser General Public License shall
163 | apply, that proxy's public statement of acceptance of any version is
164 | permanent authorization for you to choose that version for the
165 | Library.
166 |
167 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # This Gem is unsafe [](https://travis-ci.org/bascht/unsafe)
2 |
3 | Want to try Rust [unsafe{}](https://doc.rust-lang.org/book/unsafe.html)?
4 | Still stuck with Ruby?
5 |
6 | This is for you.
7 |
8 | ## All the goodness in one Gem
9 |
10 | * No Garbage collector
11 | * Nullpointers
12 | * Uncertainty.
13 |
14 | ### Usage
15 |
16 | ```ruby
17 | #!/usr/bin/env ruby
18 | # -- encoding: utf-8 --
19 |
20 | require 'unsafe/unsafe'
21 |
22 | CHANCE_OF_DOOM=0.5
23 |
24 | unsafe {
25 | (1..10).each do |i|
26 | puts "I'm fine."
27 | sleep 1
28 | end
29 | }
30 | ```
31 | Voila:
32 |
33 | ```shell
34 | $ unsafe
35 | I'm fine.
36 | I'm fine.
37 | I'm fine.
38 | I'm fine.
39 | I'm fine.
40 | I'm fine.
41 | /home/bascht/.rvm/gems/ruby-2.2.1/gems/unsafe-0.1-x86_64-linux/lib/unsafe/unsafe.rb:10: [BUG] Segmentation fault a
42 | t 0x007f3e1ccaf805
43 | ruby 2.2.1p85 (2015-02-26 revision 49769) [x86_64-linux]
44 |
45 | -- Control frame information -----------------------------------------------
46 | c:0003 p:---- s:0007 e:000006 CFUNC :fire
47 | c:0002 p:0044 s:0004 e:000003 BLOCK /home/bascht/.rvm/gems/ruby-2.2.1/gems/unsafe-0.1-x86_64-linux/lib/unsafe/uns
48 | afe.rb:10 [FINISH]
49 | c:0001 p:---- s:0002 e:000001 TOP [FINISH]
50 |
51 | -- Ruby level backtrace information ----------------------------------------
52 | /home/bascht/.rvm/gems/ruby-2.2.1/gems/unsafe-0.1-x86_64-linux/lib/unsafe/unsafe.rb:10:in `block in unsafe'
53 | /home/bascht/.rvm/gems/ruby-2.2.1/gems/unsafe-0.1-x86_64-linux/lib/unsafe/unsafe.rb:10:in `fire'
54 |
55 | -- Machine register context ------------------------------------------------
56 | RIP: 0x00007f3e1ccaf7c0 RBP: 0x0000000000000000 RSP: 0x00007f3e1cbac368
57 | RAX: 0x0000000000000000 RBX: 0x00000000011d45b0 RCX: 0x00007f3e1cbae038
58 | RDX: 0x00007f3e1ccaf7c0 RDI: 0x000000000120c1e0 RSI: 0x000000000120c1e0
59 | R8: 0x00007f3e24e9f240 R9: 0x000000000120c1e0 R10: 0x0000000000000000
60 | R11: 0x000000000120c190 R12: 0x0000000000000003 R13: 0x00000000012e0830
61 | R14: 0x00007f3e1ccadf70 R15: 0x0000000001239ee0 EFL: 0x0000000000010246
62 |
63 | -- C level backtrace information -------------------------------------------
64 | /home/bascht/.rvm/rubies/ruby-2.2.1/lib/libruby.so.2.2(rb_vm_bugreport+0x4ea) [0x7f3e24f6cdba] vm_dump.c:693
65 | /home/bascht/.rvm/rubies/ruby-2.2.1/lib/libruby.so.2.2(rb_bug_context+0xcb) [0x7f3e24e065eb] error.c:425
66 | /home/bascht/.rvm/rubies/ruby-2.2.1/lib/libruby.so.2.2(sigsegv+0x3e) [0x7f3e24ee367e] signal.c:879
67 | /lib64/libpthread.so.0 [0x7f3e24b7e430]
68 |
69 | ```
70 |
--------------------------------------------------------------------------------
/Rakefile:
--------------------------------------------------------------------------------
1 | require 'rake'
2 | require 'rake/testtask'
3 |
4 | Rake::TestTask.new do |t|
5 | t.test_files = Dir.glob('test/test_*.rb')
6 | end
7 |
8 | task(default: :test)
9 |
--------------------------------------------------------------------------------
/ext/lose_kanone/extconf.rb:
--------------------------------------------------------------------------------
1 | require 'mkmf'
2 | create_makefile('lose_kanone/lose_kanone')
3 |
--------------------------------------------------------------------------------
/ext/lose_kanone/lose_kanone.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | static void fire(VALUE klass)
4 | {
5 | char *this = "is the end";
6 | *this = 'my friend';
7 | }
8 |
9 | void Init_lose_kanone()
10 | {
11 | VALUE mKanone = rb_define_module("Kanone");
12 | rb_define_singleton_method(mKanone, "fire", fire, 0);
13 | }
14 |
--------------------------------------------------------------------------------
/lib/unsafe/unsafe.rb:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env ruby
2 | # -- encoding: utf-8 --
3 |
4 | require 'lose_kanone/lose_kanone'
5 |
6 | def unsafe
7 | GC.disable
8 | Thread.new do
9 | sleep rand(10) # keep it interesting
10 | Kanone.fire if rand < (CHANCE_OF_DOOM || 0.1)
11 | end
12 | yield
13 | GC.enable
14 | end
15 |
--------------------------------------------------------------------------------
/test/test_unsafe.rb:
--------------------------------------------------------------------------------
1 | require 'minitest/autorun'
2 |
3 | require 'unsafe/unsafe'
4 |
5 | # I've got to come up with a nice segfault
6 | # test for the crashy bits of this lib.
7 | CHANCE_OF_DOOM=0
8 |
9 | class TestSafe < MiniTest::Unit::TestCase
10 | def test_that_this_wont_crash
11 | result = unsafe { sleep 3; true }
12 | assert_equal result, true
13 | end
14 | end
15 |
--------------------------------------------------------------------------------
/unsafe.gemspec:
--------------------------------------------------------------------------------
1 | Gem::Specification.new do |spec|
2 | spec.name = 'unsafe'
3 | spec.version = '0.1'
4 | spec.summary = 'unsafe{} goodness in Ruby.'
5 | spec.description = 'Stuck with Ruby but want to give Rust a try?'
6 | spec.email = 'rubygems.org@bascht.com'
7 | spec.homepage = 'http://bascht.com'
8 | spec.author = 'bascht'
9 | spec.bindir = 'bin'
10 | spec.executable = 'unsafe'
11 | spec.files = Dir['lib/**/*.rb'] + Dir['bin/*'] + Dir['ext/**/*.so']
12 | spec.platform = Gem::Platform::CURRENT
13 | spec.require_paths = [ 'lib', 'ext' ]
14 | spec.license = 'LGPL'
15 | end
16 |
--------------------------------------------------------------------------------