Module: Autotype::NativeBridge
- Defined in:
- lib/autotype/native_bridge.rb
Class Method Summary collapse
- .apply_result!(inferencer, result) ⇒ Object
- .enabled? ⇒ Boolean
- .export_graph(inferencer) ⇒ Object
- .export_type(type) ⇒ Object
- .import_type(hash) ⇒ Object
- .native_available? ⇒ Boolean
- .prime!(inferencer) ⇒ Object
Class Method Details
.apply_result!(inferencer, result) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/autotype/native_bridge.rb', line 91 def apply_result!(inferencer, result) result = result.transform_keys(&:to_s) if result.is_a?(Hash) var_map = {} inferencer.instance_variable_get(:@methods).each do |method| method.capabilities.each do |capability| [capability.receiver, capability.result, *capability.arguments].each do |node| var_map[node.object_id] = node if node.is_a?(Autotype::TypeVariable) end end method.parameters.each { |parameter| var_map[parameter.type.object_id] = parameter.type if parameter.type.is_a?(Autotype::TypeVariable) } method.ivars.each_value { |type| var_map[type.object_id] = type if type.is_a?(Autotype::TypeVariable) } end subs = inferencer.instance_variable_get(:@substitutions) result.fetch("bindings").each do |object_id, type_hash| variable = var_map[object_id.to_i] next unless variable subs[variable] = import_type(type_hash) end end |
.enabled? ⇒ Boolean
7 8 9 |
# File 'lib/autotype/native_bridge.rb', line 7 def enabled? ENV["AUTOTYPE_NATIVE"] == "1" && native_available? end |
.export_graph(inferencer) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/autotype/native_bridge.rb', line 46 def export_graph(inferencer) resolve = inferencer.method(:resolve) var_objects = {} capabilities = [] inferencer.instance_variable_get(:@methods).each do |method| method.capabilities.each do |capability| next if capability.arguments.any? { |argument| argument.is_a?(Autotype::Function) } args = capability.arguments.reject { |argument| argument.is_a?(Autotype::Function) } result = capability.result next unless result.is_a?(Autotype::TypeVariable) var_objects[result.object_id] = result receiver = capability.receiver var_objects[receiver.object_id] = receiver if receiver.is_a?(Autotype::TypeVariable) args.each do |argument| var_objects[argument.object_id] = argument if argument.is_a?(Autotype::TypeVariable) end capabilities << { "message" => capability..to_s, "receiver" => export_type(receiver), "result" => result.object_id, "args" => args.map { export_type(_1) }, "line" => capability.line } end end bindings = inferencer.instance_variable_get(:@substitutions).each_with_object({}) do |(variable, type), hash| resolved = resolve.call(type) next if resolved.is_a?(Autotype::TypeVariable) hash[variable.object_id] = export_type(resolved) end { "capabilities" => capabilities, "bindings" => bindings, "var_count" => var_objects.size } end |
.export_type(type) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/autotype/native_bridge.rb', line 18 def export_type(type) case type when Autotype::Named { "k" => "named", "name" => type.name } when Autotype::Generic { "k" => "generic", "name" => type.name, "args" => type.arguments.map { export_type(_1) } } when Autotype::Union { "k" => "union", "members" => type.members.map { export_type(_1) } } when Autotype::TypeVariable { "k" => "var", "id" => type.object_id, "hint" => type.hint.to_s } else { "k" => "named", "name" => "Object" } end end |
.import_type(hash) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/autotype/native_bridge.rb', line 33 def import_type(hash) entry = hash.transform_keys(&:to_s) case entry["k"] when "named" then Autotype::Named.new(entry["name"]) when "generic" Autotype::Generic.new(entry["name"], entry["args"].map { import_type(_1) }) when "union" Autotype::Union.new(entry["members"].map { import_type(_1) }) else Autotype::Named.new("Object") end end |
.native_available? ⇒ Boolean
11 12 13 14 15 16 |
# File 'lib/autotype/native_bridge.rb', line 11 def native_available? require_relative "native" AutotypeNative.available? rescue LoadError, StandardError false end |
.prime!(inferencer) ⇒ Object
113 114 115 116 117 118 119 |
# File 'lib/autotype/native_bridge.rb', line 113 def prime!(inferencer) native_available? graph = export_graph(inferencer) result = AutotypeNative.solve_graph(graph) apply_result!(inferencer, result) result end |