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.



8
9
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
# File 'lib/ruby2cext/plugins/require_include.rb', line 8

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 <<-EOC
						static NODE * find_top_cref(NODE *cref) {
							while (cref && cref->nd_next) cref = cref->nd_next;
							return cref;
						}
					EOC
					c_scope {
						l "NODE *top_cref = find_top_cref(#{get_cref});"
						l "static int done = 0;"
						c_if("!done") {
							l "done = 1;"
							compiler.rb_file_to_toplevel_functions(IO.read(file), file).each { |tlfn|
								l "#{tlfn}(org_ruby_top_self, top_cref);"
							}
						}
					}
				}
			end
			"Qtrue"
		else
			node
		end
	}
end

Instance Attribute Details

#include_pathsObject (readonly)

Returns the value of attribute include_paths.



6
7
8
# File 'lib/ruby2cext/plugins/require_include.rb', line 6

def include_paths
  @include_paths
end

Instance Method Details

#search_file(req_str) ⇒ Object



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

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