Class: Ruby2CExtension::Plugins::RequireInclude

Inherits:
Ruby2CExtension::Plugin show all
Defined in:
lib/ruby2cext/plugins/require_include.rb

Instance Attribute Summary collapse

Attributes inherited from Ruby2CExtension::Plugin

#compiler

Instance Method Summary collapse

Methods inherited from Ruby2CExtension::Plugin

#global_c_code, #init_c_code

Constructor Details

#initialize(compiler, include_paths, ignore_files = nil) ⇒ RequireInclude

Returns a new instance of RequireInclude.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ruby2cext/plugins/require_include.rb', line 10

def initialize(compiler, include_paths, ignore_files = nil)
  super(compiler)
  @include_paths = include_paths
  done = {}
  if ignore_files
    ignore_files.each { |file|
      done[File.expand_path(file)] = true
    }
  end
  compiler.add_preprocessor(:fcall) { |cfun, node|
    hash = node.last
    if hash[:mid] == :require &&
        (args = hash[:args]) &&
        args.first == :array &&
        (args = args.last).size == 1 &&
        Array === args[0] &&
        args[0].first == :str &&
        (file = search_file(args[0].last[:lit]))
      unless done[File.expand_path(file)]
        done[File.expand_path(file)] = true
        cfun.compiler.log "including require'd file: #{file}"
        cfun.instance_eval {
          add_helper "            static NODE * find_top_cref(NODE *cref) {\n              while (cref && cref->nd_next) cref = cref->nd_next;\n              return cref;\n            }\n          EOC\n          c_scope {\n            l \"NODE *top_cref = find_top_cref(\#{get_cref});\"\n            l \"static int done = 0;\"\n            c_if(\"!done\") {\n              l \"done = 1;\"\n              compiler.rb_file_to_toplevel_functions(IO.read(file), file).each { |tlfn|\n                l \"\#{tlfn}(org_ruby_top_self, top_cref);\"\n              }\n            }\n          }\n        }\n      end\n      \"Qtrue\"\n    else\n      node\n    end\n  }\nend\n"

Instance Attribute Details

#include_pathsObject (readonly)

Returns the value of attribute include_paths.



8
9
10
# File 'lib/ruby2cext/plugins/require_include.rb', line 8

def include_paths
  @include_paths
end

Instance Method Details

#search_file(req_str) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ruby2cext/plugins/require_include.rb', line 57

def search_file(req_str)
  req_str = req_str.dup
  unless req_str =~ /\.rb\z/
    req_str << ".rb"
  end
  res = false
  include_paths.map { |path|
    File.join(path, req_str)
  }.find { |file|
    File.exists? file
  }
end