Module: FFI::Inline

Defined in:
lib/ffi/inline/inline.rb,
lib/ffi/inline/version.rb,
lib/ffi/inline/builders.rb,
lib/ffi/inline/compilers.rb,
lib/ffi/inline/builders/c.rb,
lib/ffi/inline/builders/cpp.rb,
lib/ffi/inline/compilers/gcc.rb,
lib/ffi/inline/compilers/gxx.rb,
lib/ffi/inline/compilers/tcc.rb

Defined Under Namespace

Classes: Builder, Compiler, Signature

Constant Summary collapse

VERSION =
'0.0.4.1'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.directoryObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ffi/inline/inline.rb', line 16

def self.directory
	if ENV['FFI_INLINER_PATH'] && !ENV['FFI_INLINER_PATH'].empty?
		@directory = ENV['FFI_INLINER_PATH']
	else
		require 'tmpdir'
		@directory ||= File.expand_path(File.join(Dir.tmpdir, ".ffi-inline-#{Process.uid}"))
	end

	if File.exists?(@directory) && !File.directory?(@directory)
		raise 'the FFI_INLINER_PATH exists and is not a directory'
	end

	if !File.exists?(@directory)
		FileUtils.mkdir(@directory)
	end

	@directory
end

Instance Method Details

#inline(*args, &block) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/ffi/inline/inline.rb', line 35

def inline (*args, &block)
	if self.class == Class
		instance_inline(*args, &block)
	else
		singleton_inline(*args, &block)
	end
end

#instance_inline(*args) {|builder| ... } ⇒ Object

Yields:

  • (builder)


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ffi/inline/inline.rb', line 61

def instance_inline (*args)
	options = args.last.is_a?(Hash) ? args.pop : {}

	language, code = if args.length == 2
		args
	else
		block_given? ? [args.shift || :c, ''] : [:c, args.shift || '']
	end

	builder = Builder[language].new(code, options)
	yield builder if block_given?
	mod = builder.build

	builder.symbols.each {|sym|
		define_method sym, &mod.method(sym)
	}
end

#singleton_inline(*args) {|builder| ... } ⇒ Object

Yields:

  • (builder)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ffi/inline/inline.rb', line 43

def singleton_inline (*args)
	options = args.last.is_a?(Hash) ? args.pop : {}

	language, code = if args.length == 2
		args
	else
		block_given? ? [args.shift || :c, ''] : [:c, args.shift || '']
	end

	builder = Builder[language].new(code, options)
	yield builder if block_given?
	mod = builder.build

	builder.symbols.each {|sym|
		define_singleton_method sym, &mod.method(sym)
	}
end