fastruby 0.0.6 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG CHANGED
@@ -1,3 +1,11 @@
1
+ 0.0.7 Normalized random class names on specs to maximize the profit of the cache when running specs
2
+
3
+ Implemented dump of classes into literals using rb_define_class. This improves the cache loads
4
+
5
+ Added fastruby VERSION to cache hash
6
+
7
+ Support for class variables
8
+
1
9
  0.0.6 Implemented cache of native libraries to speedup init, see README for more info
2
10
 
3
11
  0.0.5 Support for case..when..end
data/README CHANGED
@@ -58,6 +58,8 @@ I will stabilize the API and document it for next releases. I promise
58
58
  == Known Limitations & Issues
59
59
 
60
60
  * monkey patching does not work with fastruby methods
61
+ * callcc is not supported, it works but using it may result in unexpected behaviour
62
+ * recursive methods will be slow since the optimization of method calls only works with already defined methods
61
63
  * calls with blocks to ruby or cruby methods are almost as slow as normal ruby (if the called method is defined by fastruby, the call it's pretty fast)
62
64
  * does not support methods with variable number of arguments
63
65
 
data/Rakefile CHANGED
@@ -7,7 +7,7 @@ require "rspec/core/rake_task"
7
7
 
8
8
  spec = Gem::Specification.new do |s|
9
9
  s.name = 'fastruby'
10
- s.version = '0.0.6'
10
+ s.version = '0.0.7'
11
11
  s.author = 'Dario Seminara'
12
12
  s.email = 'robertodarioseminara@gmail.com'
13
13
  s.platform = Gem::Platform::RUBY
@@ -134,7 +134,7 @@ module FastRuby
134
134
  c_code = context.to_c_method(tree)
135
135
 
136
136
  unless options[:main]
137
- context.define_method_at_init(@method_name, args_tree.size+1, signature)
137
+ context.define_method_at_init(@owner,@method_name, args_tree.size+1, signature)
138
138
  end
139
139
 
140
140
  so_name = nil
@@ -22,6 +22,7 @@ require "fastruby/builder"
22
22
  require "fastruby/getlocals"
23
23
  require "fastruby/method_extension"
24
24
  require "fastruby/cache/cache"
25
+ require "fastruby"
25
26
 
26
27
  # clean rubyinline cache
27
28
  system("rm -fr #{ENV["HOME"]}/.ruby_inline/*")
@@ -58,7 +59,7 @@ class Object
58
59
  objs = Array.new
59
60
 
60
61
  unless options_hash[:no_cache]
61
- snippet_hash = FastRuby.cache.hash_snippet(argument,options_hash[:validate_lvar_types].to_s)
62
+ snippet_hash = FastRuby.cache.hash_snippet(argument,options_hash[:validate_lvar_types].to_s + "^" + FastRuby::VERSION)
62
63
  objs = FastRuby.cache.retrieve(snippet_hash)
63
64
  end
64
65
 
@@ -95,7 +96,8 @@ class Object
95
96
  FastRuby.cache.register_proc(obj, $last_obj_proc)
96
97
  end
97
98
  FastRuby.cache.execute(obj, self)
98
- rescue
99
+ rescue Exception => e
100
+ p e
99
101
  end
100
102
  end
101
103
  end
@@ -91,6 +91,12 @@ module FastRuby
91
91
  }
92
92
  "
93
93
 
94
+ extra_code << "static VALUE __rb_cvar_set(VALUE recv,ID idvar, VALUE value, int warn) {
95
+ rb_cvar_set(recv,idvar,value,warn);
96
+ return value;
97
+ }
98
+ "
99
+
94
100
  extra_code << "static VALUE _lvar_assing(VALUE* destination,VALUE value) {
95
101
  *destination = value;
96
102
  return value;
@@ -505,6 +511,14 @@ module FastRuby
505
511
  inline_block str
506
512
  end
507
513
 
514
+ def to_c_cvar(tree)
515
+ "rb_cvar_get(CLASS_OF(plocals->self) != rb_cClass ? CLASS_OF(plocals->self) : plocals->self,#{intern_num tree[1]})"
516
+ end
517
+
518
+ def to_c_cvasgn(tree)
519
+ "__rb_cvar_set(CLASS_OF(plocals->self) != rb_cClass ? CLASS_OF(plocals->self) : plocals->self,#{intern_num tree[1]},#{to_c tree[2]},Qfalse)"
520
+ end
521
+
508
522
  def to_c_return(tree)
509
523
  "pframe->target_frame = ((typeof(pframe))plocals->pframe); plocals->return_value = #{to_c(tree[1])}; longjmp(pframe->jmp, 1); return Qnil;\n"
510
524
  end
@@ -1011,7 +1025,7 @@ module FastRuby
1011
1025
  end
1012
1026
  end
1013
1027
 
1014
- def define_method_at_init(method_name, size, signature)
1028
+ def define_method_at_init(klass,method_name, size, signature)
1015
1029
  init_extra << "
1016
1030
  {
1017
1031
  VALUE method_name = rb_funcall(
@@ -1022,7 +1036,7 @@ module FastRuby
1022
1036
  #{literal_value signature}
1023
1037
  );
1024
1038
 
1025
- rb_define_method(#{to_c s(:gvar, :$class_self)}, RSTRING(method_name)->ptr, #{alt_method_name}, #{size});
1039
+ rb_define_method(#{literal_value klass}, RSTRING(method_name)->ptr, #{alt_method_name}, #{size});
1026
1040
  }
1027
1041
  "
1028
1042
  end
@@ -1758,14 +1772,46 @@ module FastRuby
1758
1772
  name = self.add_global_name("VALUE", "Qnil");
1759
1773
 
1760
1774
  begin
1775
+
1761
1776
  str = Marshal.dump(value)
1762
1777
 
1763
- init_extra << "
1764
- #{name} = rb_marshal_load(rb_str_new(#{c_escape str}, #{str.size}));
1765
- rb_funcall(#{name},#{intern_num :gc_register_object},0);
1766
1778
 
1767
- "
1768
- rescue TypeError
1779
+ if value.instance_of? Module
1780
+
1781
+ container_str = value.to_s.split("::")[0..-2].join("::")
1782
+
1783
+ init_extra << "
1784
+ #{name} = rb_define_module_under(
1785
+ #{container_str == "" ? "rb_cObject" : literal_value(eval(container_str))}
1786
+ ,\"#{value.to_s.split("::").last}\");
1787
+
1788
+ rb_funcall(#{name},#{intern_num :gc_register_object},0);
1789
+ "
1790
+ elsif value.instance_of? Class
1791
+ container_str = value.to_s.split("::")[0..-2].join("::")
1792
+
1793
+ init_extra << "
1794
+ #{name} = rb_define_class_under(
1795
+ #{container_str == "" ? "rb_cObject" : literal_value(eval(container_str))}
1796
+ ,\"#{value.to_s.split("::").last}\"
1797
+ ,#{value.superclass == Object ? "rb_cObject" : literal_value(value.superclass)});
1798
+
1799
+ rb_funcall(#{name},#{intern_num :gc_register_object},0);
1800
+ "
1801
+ elsif value.instance_of? Array
1802
+ init_extra << "
1803
+ #{name} = rb_ary_new3(#{value.size}, #{value.map{|x| literal_value x}.join(",")} );
1804
+ rb_funcall(#{name},#{intern_num :gc_register_object},0);
1805
+ "
1806
+ else
1807
+
1808
+ init_extra << "
1809
+ #{name} = rb_marshal_load(rb_str_new(#{c_escape str}, #{str.size}));
1810
+ rb_funcall(#{name},#{intern_num :gc_register_object},0);
1811
+
1812
+ "
1813
+ end
1814
+ rescue TypeError => e
1769
1815
  @no_cache = true
1770
1816
  FastRuby.logger.info "#{value} disabling cache for extension"
1771
1817
  init_extra << "
data/lib/fastruby.rb CHANGED
@@ -33,6 +33,6 @@ module FastRuby
33
33
  FastRuby.fastruby_script_path = File.expand_path(__FILE__)
34
34
  FastRuby.fastruby_load_path = File.expand_path(File.dirname(__FILE__))
35
35
 
36
- VERSION = "0.0.6" unless defined? VERSION
36
+ VERSION = "0.0.7" unless defined? FastRuby::VERSION
37
37
  end
38
38
 
@@ -87,12 +87,12 @@ describe FastRuby, "fastruby" do
87
87
  class BahException < Exception
88
88
  end
89
89
 
90
- def self.basic_unhandled_exception(*exception_names)
90
+ def self.basic_unhandled_exception(class_name_suffix, *exception_names)
91
91
 
92
92
  exception_names.each do |exception_name|
93
93
  it "should raise basic exception #{exception_name}" do
94
94
 
95
- random_name = "::L5_" + rand(10000).to_s
95
+ random_name = "::L5_" + class_name_suffix
96
96
 
97
97
  fastruby "
98
98
  class #{random_name}
@@ -111,7 +111,7 @@ describe FastRuby, "fastruby" do
111
111
 
112
112
  it "should not raise basic exception #{exception_name} if rescued" do
113
113
 
114
- random_name = "::L6_" + rand(10000).to_s
114
+ random_name = "::L6_" + class_name_suffix
115
115
 
116
116
  fastruby "
117
117
  class #{random_name}
@@ -133,7 +133,7 @@ describe FastRuby, "fastruby" do
133
133
 
134
134
  it "should raise basic exception #{exception_name} even if rescued when the rescue is for another exception" do
135
135
 
136
- random_name = "::L7_" + rand(10000).to_s
136
+ random_name = "::L7_" + class_name_suffix
137
137
 
138
138
  fastruby "
139
139
  class #{random_name}
@@ -155,8 +155,8 @@ describe FastRuby, "fastruby" do
155
155
 
156
156
  it "should rescue basic exception #{exception_name} when raised from rubycode called from fastruby code" do
157
157
 
158
- random_name = "::L8_" + rand(10000).to_s
159
- random_name_2 = "::L8_" + rand(10000).to_s
158
+ random_name = "::L8_" + class_name_suffix
159
+ random_name_2 = "::L8_" + class_name_suffix + "_"
160
160
 
161
161
  eval "
162
162
  class #{random_name_2}
@@ -183,8 +183,8 @@ describe FastRuby, "fastruby" do
183
183
 
184
184
  it "should rescue basic exception #{exception_name} from fastruby code when raised from rubycode" do
185
185
 
186
- random_name = "::L9_" + rand(10000).to_s
187
- random_name_2 = "::L9_" + rand(10000).to_s
186
+ random_name = "::L9_" + class_name_suffix
187
+ random_name_2 = "::L9_" + class_name_suffix + "_"
188
188
 
189
189
  eval "
190
190
  class #{random_name_2}
@@ -217,7 +217,7 @@ describe FastRuby, "fastruby" do
217
217
 
218
218
  it "should raise basic exception #{exception_name} from singleton method" do
219
219
 
220
- random_name = "::L5_" + rand(10000).to_s
220
+ random_name = "::L10_" + class_name_suffix
221
221
 
222
222
  fastruby "
223
223
  class #{random_name}
@@ -240,7 +240,7 @@ describe FastRuby, "fastruby" do
240
240
 
241
241
  it "should not raise basic exception #{exception_name} if rescued from singleton method" do
242
242
 
243
- random_name = "::L6_" + rand(10000).to_s
243
+ random_name = "::L11_" + class_name_suffix
244
244
 
245
245
  fastruby "
246
246
  class #{random_name}
@@ -266,7 +266,7 @@ describe FastRuby, "fastruby" do
266
266
 
267
267
  it "should raise basic exception #{exception_name} even if rescued when the rescue is for another exception from singleton method" do
268
268
 
269
- random_name = "::L7_" + rand(10000).to_s
269
+ random_name = "::L12_" + class_name_suffix
270
270
 
271
271
  fastruby "
272
272
  class #{random_name}
@@ -292,8 +292,8 @@ describe FastRuby, "fastruby" do
292
292
 
293
293
  it "should rescue basic exception #{exception_name} when raised from rubycode called from fastruby code from singleton method" do
294
294
 
295
- random_name = "::L8_" + rand(10000).to_s
296
- random_name_2 = "::L8_" + rand(10000).to_s
295
+ random_name = "::L13_" + class_name_suffix
296
+ random_name_2 = "::L13_" + class_name_suffix + "_"
297
297
 
298
298
  eval "
299
299
  class #{random_name_2}
@@ -324,8 +324,8 @@ describe FastRuby, "fastruby" do
324
324
 
325
325
  it "should rescue basic exception #{exception_name} from fastruby code when raised from rubycode from singleton methods" do
326
326
 
327
- random_name = "::L9_" + rand(10000).to_s
328
- random_name_2 = "::L9_" + rand(10000).to_s
327
+ random_name = "::L14_" + class_name_suffix
328
+ random_name_2 = "::L14_" + class_name_suffix + "_"
329
329
 
330
330
  eval "
331
331
  class #{random_name_2}
@@ -370,14 +370,14 @@ describe FastRuby, "fastruby" do
370
370
  end
371
371
  end
372
372
 
373
- basic_unhandled_exception("Exception")
374
- basic_unhandled_exception("RuntimeError")
375
- basic_unhandled_exception("StandardError")
376
- basic_unhandled_exception("Errno::ENOENT")
373
+ basic_unhandled_exception("2", "Exception")
374
+ basic_unhandled_exception("3", "RuntimeError")
375
+ basic_unhandled_exception("4", "StandardError")
376
+ basic_unhandled_exception("5", "Errno::ENOENT")
377
377
 
378
378
  it "should accept else with rescue" do
379
379
 
380
- random_name = "::L11_" + rand(10000).to_s
380
+ random_name = "::L11_1"
381
381
  fastruby "
382
382
  class #{random_name}
383
383
  def foo
@@ -398,7 +398,7 @@ describe FastRuby, "fastruby" do
398
398
 
399
399
  it "should accept else with rescue, when no exception is raised" do
400
400
 
401
- random_name = "::L12_" + rand(10000).to_s
401
+ random_name = "::L12_1"
402
402
  fastruby "
403
403
  class #{random_name}
404
404
  def foo
@@ -418,7 +418,7 @@ describe FastRuby, "fastruby" do
418
418
 
419
419
  it "should accept else with rescue, when no exception is raised and begin has body" do
420
420
 
421
- random_name = "::L13_" + rand(10000).to_s
421
+ random_name = "::L13_1"
422
422
  fastruby "
423
423
  class #{random_name}
424
424
  def foo
@@ -191,10 +191,12 @@ describe FastRuby, "fastruby" do
191
191
  ::U16.new.foo.should be == nil
192
192
  end
193
193
 
194
+ $u_class_number = 30
194
195
  def self.test_defined(code,title,defined_name)
195
196
  it "should call defined? to defined #{title} and return '#{defined_name}" do
196
- classname = "::U"+rand(1000000).to_s
197
+ classname = "::U"+$u_class_number.to_s
197
198
 
199
+ $u_class_number = $u_class_number + 1
198
200
  code = "def foo; #{code}; end"
199
201
  eval("class #{classname}
200
202
  fastruby #{code.inspect}
@@ -205,8 +207,9 @@ describe FastRuby, "fastruby" do
205
207
 
206
208
  def self.test_defined_block(code,title,defined_name)
207
209
  it "should call defined? to defined #{title} and return '#{defined_name}'" do
208
- classname = "::U"+rand(1000000).to_s
210
+ classname = "::U"+$u_class_number.to_s
209
211
 
212
+ $u_class_number = $u_class_number + 1
210
213
  code = "def foo; #{code}; end"
211
214
  eval("class #{classname}
212
215
  fastruby #{code.inspect}
@@ -233,4 +236,39 @@ describe FastRuby, "fastruby" do
233
236
  test_defined "defined? #{var}=0", "#{var} assignment", "assignment"
234
237
  end
235
238
 
239
+ it "should read class variable" do
240
+ class ::U17
241
+
242
+ def self.foo(a)
243
+ @@a = a
244
+ end
245
+
246
+ fastruby "
247
+ def foo
248
+ @@a
249
+ end
250
+ "
251
+ end
252
+ ::U17.foo(31)
253
+ ::U17.new.foo.should be === 31
254
+ end
255
+
256
+ it "should write class variable" do
257
+ class ::U18
258
+
259
+ def self.foo
260
+ @@a
261
+ end
262
+
263
+ fastruby "
264
+ def foo(a)
265
+ @@a = a
266
+ end
267
+ "
268
+ end
269
+ ::U18.new.foo(71)
270
+ ::U18.foo.should be == 71
271
+ end
272
+
273
+
236
274
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fastruby
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 17
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 6
10
- version: 0.0.6
9
+ - 7
10
+ version: 0.0.7
11
11
  platform: ruby
12
12
  authors:
13
13
  - Dario Seminara
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-08-27 00:00:00 -03:00
18
+ date: 2011-09-02 00:00:00 -03:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency