Module: AutotypeNative

Defined in:
lib/autotype/native.rb,
ext/autotype/autotype.c

Defined Under Namespace

Classes: Solver

Class Method Summary collapse

Class Method Details

.available?Boolean

Returns:

  • (Boolean)


265
266
267
268
269
270
271
# File 'ext/autotype/autotype.c', line 265

def available?
  load_extension!
  const_get(:Solver)
  true
rescue LoadError, StandardError
  false
end

.load_extension!Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/autotype/native.rb', line 13

def load_extension!
  return if @extension_loaded

  ext_dir = File.expand_path("../../ext/autotype", __dir__)
  bundle = File.join(ext_dir, "autotype.#{RbConfig::CONFIG['DLEXT']}")
  native_sources = Dir[File.expand_path("../../native/autotype/src/*.c", __dir__)]
  extension_source = File.join(ext_dir, "autotype.c")
  sources = native_sources + [extension_source]

  stale =
    !File.exist?(bundle) ||
    sources.any? { |source| File.exist?(source) && File.mtime(source) > File.mtime(bundle) }

  if stale
    Dir.chdir(ext_dir) { system("ruby extconf.rb && make", exception: true) }
  end

  require bundle
  @extension_loaded = true
end

.solve_graph(graph) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'ext/autotype/autotype.c', line 197

static VALUE stc_solve_graph(VALUE self, VALUE graph) {
  (void)self;
  Check_Type(graph, T_HASH);

  stc_solver *solver = stc_solver_new();
  if (!solver) rb_raise(rb_eNoMemError, "stc_solver_new failed");

  VALUE capabilities = stc_hash_get(graph, "capabilities");
  VALUE bindings = stc_hash_get(graph, "bindings");
  Check_Type(capabilities, T_ARRAY);

  size_t cap_count = 0;
  stc_capability *caps = stc_load_capabilities(solver, capabilities, &cap_count);
  if (!caps) {
    stc_solver_free(solver);
    rb_raise(rb_eNoMemError, "capability allocation failed");
  }

  stc_solver_load_flat(solver, caps, cap_count);
  if (!NIL_P(bindings)) stc_load_bindings(solver, bindings);

  stc_solver_run(solver);

  VALUE result = rb_hash_new();
  rb_hash_aset(result, ID2SYM(rb_intern("bindings")), stc_export_bindings(solver));
  rb_hash_aset(result, ID2SYM(rb_intern("iterations")), INT2NUM(solver->iterations));
  rb_hash_aset(result, ID2SYM(rb_intern("converged")), solver->converged ? Qtrue : Qfalse);

  stc_solver_free(solver);
  return result;
}