Class: Ruby2CExtension::Eval2C
Constant Summary collapse
- DEFAULT_PATH =
File.join(File.(ENV["HOME"] || ENV["USERPROFILE"] || ENV["HOMEPATH"] || "."), ".ruby2cext")
Instance Attribute Summary collapse
-
#force_recompile ⇒ Object
readonly
Returns the value of attribute force_recompile.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#plugins ⇒ Object
readonly
Returns the value of attribute plugins.
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
Instance Method Summary collapse
- #compile_methods(mod, *methods) ⇒ Object
- #compile_to_proc(code_str) ⇒ Object
-
#initialize(options = {}) ⇒ Eval2C
constructor
A new instance of Eval2C.
- #instance_eval(object, code_str) ⇒ Object
- #module_eval(mod, code_str) ⇒ Object (also: #class_eval)
- #toplevel_eval(code_str) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Eval2C
Returns a new instance of Eval2C.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/ruby2cext/eval2c.rb', line 13 def initialize( = {}) unless (@path = [:path]) @path = DEFAULT_PATH Dir.mkdir(@path, 0700) unless File.exists?(@path) end @path = File.(@path) unless File.directory?(@path) raise Ruby2CExtError, "#{@path} is no directory" end unless File.stat(@path).mode & 022 == 0 # no writing for group and others warn "Ruby2CExtension::Eval2C warning: #{@path} is insecure" end @prefix = [:prefix] || "eval2c" @plugins = [:plugins] || {:optimizations => :all} @logger = [:logger] @force_recompile = [:force_recompile] @done = {} @digest_extra = Ruby2CExtension::FULL_VERSION_STRING + @plugins.inspect.split(//).sort.join("") end |
Instance Attribute Details
#force_recompile ⇒ Object (readonly)
Returns the value of attribute force_recompile.
9 10 11 |
# File 'lib/ruby2cext/eval2c.rb', line 9 def force_recompile @force_recompile end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
9 10 11 |
# File 'lib/ruby2cext/eval2c.rb', line 9 def logger @logger end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
9 10 11 |
# File 'lib/ruby2cext/eval2c.rb', line 9 def path @path end |
#plugins ⇒ Object (readonly)
Returns the value of attribute plugins.
9 10 11 |
# File 'lib/ruby2cext/eval2c.rb', line 9 def plugins @plugins end |
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
9 10 11 |
# File 'lib/ruby2cext/eval2c.rb', line 9 def prefix @prefix end |
Instance Method Details
#compile_methods(mod, *methods) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/ruby2cext/eval2c.rb', line 75 def compile_methods(mod, *methods) methods = methods.map { |m| m.to_sym }.uniq defs = methods.map { |m| bnode = mod.instance_method(m).body_node unless bnode.type == :scope raise Ruby2CExtError, "the method #{m} cannot be compiled, only methods defined using def can be compiled" end [:defn, {:mid => m, :defn => bnode.transform(Compiler::NODE_TRANSFORM_OPTIONS)}] } node_tree = [:scope, {:next => [:gasgn, {:vid => :$test, :value => [:iter, { :var => false, :iter => [:fcall, {:args => false, :mid => :proc}], :body => [:block, defs] }] }]}] # save current visibility of the methods publ_methods = mod.public_instance_methods.map { |m| m.to_sym } & methods prot_methods = mod.protected_instance_methods.map { |m| m.to_sym } & methods priv_methods = mod.private_instance_methods.map { |m| m.to_sym } & methods # compile to test if the methods don't need a cref and to get a string for the hash c = Compiler.new("test") c.add_toplevel(c.compile_toplevel_function(node_tree)) test_code = c.to_c_code(nil) # no time_stamp # don't allow methods that need a cref, because the compiled version would get a different cref if test_code =~ /^static void def_only_once/ # a bit hackish ... raise Ruby2CExtError, "the method(s) cannot be compiled, because at least one needs a cref" end # compile the proc def_proc = compile_helper(test_code) { |compiler, name, gvar_name| node_tree.last[:next].last[:vid] = gvar_name.to_sym compiler.add_toplevel(compiler.compile_toplevel_function(node_tree)) } # try to remove all the methods mod.module_eval { methods.each { |m| remove_method(m) rescue nil } } # add the compiled methods mod.module_eval &def_proc # restore original visibility mod.module_eval { public(*publ_methods) unless publ_methods.empty? protected(*prot_methods) unless prot_methods.empty? private(*priv_methods) unless priv_methods.empty? } end |
#compile_to_proc(code_str) ⇒ Object
56 57 58 59 60 |
# File 'lib/ruby2cext/eval2c.rb', line 56 def compile_to_proc(code_str) compile_helper(code_str) { |compiler, name, gvar_name| compiler.add_rb_file("#{gvar_name} = proc { #{code_str} }", name) } end |
#instance_eval(object, code_str) ⇒ Object
67 68 69 |
# File 'lib/ruby2cext/eval2c.rb', line 67 def instance_eval(object, code_str) object.instance_eval(&compile_to_proc(code_str)) end |
#module_eval(mod, code_str) ⇒ Object Also known as: class_eval
62 63 64 |
# File 'lib/ruby2cext/eval2c.rb', line 62 def module_eval(mod, code_str) mod.module_eval(&compile_to_proc(code_str)) end |
#toplevel_eval(code_str) ⇒ Object
71 72 73 |
# File 'lib/ruby2cext/eval2c.rb', line 71 def toplevel_eval(code_str) compile_to_proc(code_str).call end |