Module: Truffle::CExt

Defined in:
truffle/src/main/ruby/core/truffle/cext/cext.rb,
truffle/src/main/ruby/core/truffle/cext/mkmf.rb

Defined Under Namespace

Classes: MkMf

Class Method Summary (collapse)

Class Method Details

+ (nil) inline(headers, code, c_flags = [])

Load C source code from very simple inline code. Truffle::CExt.inline %{ #include <stdio.h> }, %{ printf("Hello, World!\n"); }

Returns:

  • (nil)


104
105
106
# File 'truffle/src/main/ruby/core/truffle/cext/cext.rb', line 104

def self.inline(headers, code, c_flags=[])
  load_string 'Init_inline', c_flags, "#include <ruby.h>\n #{headers}\n void Init_inline() { #{code} }\n"
end

+ (nil) load_extconf(path)

Load a C extension from the path of an extconf.rb file.

Example

Truffle::CExt::load_extconf 'foo/ext/foo/extconf.rb'

Returns:

  • (nil)


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'truffle/src/main/ruby/core/truffle/cext/cext.rb', line 76

def self.load_extconf(path)
  source = File.read(path)

  # Uses global variables - need to isolate somehow - maybe a
  # subprocess? Or some new Truffle-specific functionality for
  # mocking globals.

  $CFLAGS = []

  mkmf = MkMf.new(File.expand_path("..", path))
  mkmf.instance_eval(source)

  # Not sure we're handling $CFLAGS correctly - we get extra spaces in them
  cflags = $CFLAGS.map { |flag| flag.strip }

  load_files ["Init_#{mkmf.target_name}"], cflags, mkmf.c_files
end

+ (nil) load_files(init_functions, c_flags, files)

Load a set of C source code files as a C extension, specifying the name of the initialize functions and the C compiler flags. You can use this to load multiple C extensions by supplying multiple init functions.

Example

Truffle::CExt::load_files(['Init_foo', 'Init_bar'], ['-Wall'], ['foo.c', 'bar.c'])

Returns:

  • (nil)


29
30
31
32
# File 'truffle/src/main/ruby/core/truffle/cext/cext.rb', line 29

def self.load_files(init_functions, c_flags, files)
  raise 'C extensions not supported' unless supported?
  Truffle::Primitive.cext_load init_functions, c_flags, files
end

+ (nil) load_string(init_function, c_flags, string)

Load C source code from a string, specifying the name of the initialize function and the C compiler flags.

Example

Truffle::CExt::load_string('Init_foo', ['-Wall'], %{
  #include <ruby.h>

  VALUE add(VALUE self, VALUE a, VALUE b) {
    return INT2NUM(NUM2INT(a) + NUM2INT(b));
  }

  void Init_foo() {
    VALUE Foo = rb_define_module("Foo");
    rb_define_method(Foo, "add", add, 2);
  }
})

Returns:

  • (nil)


54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'truffle/src/main/ruby/core/truffle/cext/cext.rb', line 54

def self.load_string(init_function, c_flags, string)
  temp_file = 'temp.c'

  File.open(temp_file, 'w') do |file|
    file.write(string)
  end

  begin
    load_files [init_function], c_flags, [temp_file]
  ensure
    File.delete temp_file
  end
end

+ (Boolean) supported?

Are C extensions supported?

Returns:

  • (Boolean)


14
15
16
# File 'truffle/src/main/ruby/core/truffle/cext/cext.rb', line 14

def self.supported?
  Truffle::Primitive.cext_supported?
end