Module: Jade::Codegen::Boundary::Specialized::Record
- Included in:
- Record
- Defined in:
- lib/jade/codegen/boundary/specialized/record.rb
Overview
Structs whose fields are all specializable. Each specializable struct gets a pair of ‘def self.decode_<name>` / `encode_<name>` helper methods emitted at module level; the wrapper body for `birthday(person: Person) -> Person` becomes `encode_person(Internal.birthday(decode_person(person)))`.
Cycle detection: a struct referencing itself (directly or mutually) falls back to the descriptor cache path. The ‘seen` set carries qnames of structs we’re currently inside; revisiting one means we’d loop, so we bail with nil.
Constant Summary
Constants included from Helpers
Instance Method Summary collapse
-
#collect_helpers(body, registry) ⇒ Object
All specializable structs reachable from any exposed function’s boundary signature, transitively (through nested struct fields and ‘List` / `Maybe` wrappers).
- #decode(type, input, registry) ⇒ Object
- #emit_helpers(structs, registry) ⇒ Object
- #encode(type, value_expr, registry) ⇒ Object
- #specializable?(type, registry, seen) ⇒ Boolean
- #struct_for(type, registry) ⇒ Object
-
#structs_in(type, registry) ⇒ Object
Returns specializable structs reachable from ‘type` through any depth of `List` / `Maybe` / nested struct fields.
Methods included from Helpers
data_define, dict_constraints, dict_synthetic_name, fn_constraints, fn_impl_synthetic_name, generate_many, generate_node, impl_synthetic_name, param_synthetic_name, resolve_callee_symbol, ruby_classes_for_type, to_qualified
Instance Method Details
#collect_helpers(body, registry) ⇒ Object
All specializable structs reachable from any exposed function’s boundary signature, transitively (through nested struct fields and ‘List` / `Maybe` wrappers). Each one needs `decode_<name>` / `encode_<name>` helper methods emitted.
37 38 39 40 41 42 43 |
# File 'lib/jade/codegen/boundary/specialized/record.rb', line 37 def collect_helpers(body, registry) body.expressions .filter { it.is_a?(AST::FunctionDeclaration) } .flat_map { fn_reachable_structs(it, registry) } .uniq .then { transitive_closure(it, registry) } end |
#decode(type, input, registry) ⇒ Object
19 20 21 22 |
# File 'lib/jade/codegen/boundary/specialized/record.rb', line 19 def decode(type, input, registry) struct = struct_for(type, registry) or return nil "#{decode_helper_name(struct)}(#{input})" end |
#emit_helpers(structs, registry) ⇒ Object
45 46 47 48 49 |
# File 'lib/jade/codegen/boundary/specialized/record.rb', line 45 def emit_helpers(structs, registry) structs.flat_map do [decode_helper(it, registry), encode_helper(it, registry)] end end |
#encode(type, value_expr, registry) ⇒ Object
24 25 26 27 |
# File 'lib/jade/codegen/boundary/specialized/record.rb', line 24 def encode(type, value_expr, registry) struct = struct_for(type, registry) or return nil "#{encode_helper_name(struct)}(#{value_expr})" end |
#specializable?(type, registry, seen) ⇒ Boolean
29 30 31 |
# File 'lib/jade/codegen/boundary/specialized/record.rb', line 29 def specializable?(type, registry, seen) specializable_struct(type, registry, seen) ? true : false end |
#struct_for(type, registry) ⇒ Object
51 52 53 |
# File 'lib/jade/codegen/boundary/specialized/record.rb', line 51 def struct_for(type, registry) specializable_struct(type, registry, ::Set.new) end |
#structs_in(type, registry) ⇒ Object
Returns specializable structs reachable from ‘type` through any depth of `List` / `Maybe` / nested struct fields. Used by `collect_helpers` to seed the closure walk.
58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/jade/codegen/boundary/specialized/record.rb', line 58 def structs_in(type, registry) if (struct = struct_for(type, registry)) [struct] elsif (inner = List.inner_of(type)) structs_in(inner, registry) elsif (inner = Maybe.inner_of(type)) structs_in(inner, registry) else [] end end |