Module: Jade::Codegen::Boundary::Cache

Extended by:
Cache, Helpers
Included in:
Cache
Defined in:
lib/jade/codegen/boundary/cache.rb

Overview

Per-module cache mapping each type a boundary wrapper needs to decode/encode to a const name (‘BOUNDARY_DEC_0`, etc.) that holds the value once at module load. `collect` walks the body to build the map; `decoder_for` / `encoder_for` consult it from wrapper codegen, falling back to the raw `Boundary` spec when uncached (e.g. when emission runs outside a Module).

Constant Summary

Constants included from Helpers

Helpers::NATIVE_RUBY_CLASSES

Instance Method Summary collapse

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

#cache_map(types, tag, &specialized) ⇒ Object

Types with a specialized inline emission don’t need a cached constant — the wrapper emits the validation directly.



47
48
49
50
51
52
53
54
# File 'lib/jade/codegen/boundary/cache.rb', line 47

def cache_map(types, tag, &specialized)
  types
    .reject(&specialized)
    .uniq
    .each_with_index
    .map { |t, i| [t, "BOUNDARY_#{tag}_#{i}"] }
    .to_h
end

#collect(body, registry) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/jade/codegen/boundary/cache.rb', line 27

def collect(body, registry)
  per_fn = body
    .expressions
    .filter_map do
      boundary_types(it, registry) if it.is_a?(AST::FunctionDeclaration)
    end

  {
    decoders: cache_map(per_fn.flat_map { it[:decoders] }, 'DEC') { |t|
      Boundary::Specialized.decode_expr(t, '_', registry)
    },
    encoders: cache_map(per_fn.flat_map { it[:encoders] }, 'ENC') { |t|
      Boundary::Specialized.identity_encoder?(t) ||
        Boundary::Specialized.encode_expr(t, '_', registry)
    },
  }
end

#constants(cache, registry) ⇒ Object



56
57
58
59
# File 'lib/jade/codegen/boundary/cache.rb', line 56

def constants(cache, registry)
  cache[:decoders].map { |type, name| "#{name} = #{Boundary.decoder_for(type, registry)}" } +
    cache[:encoders].map { |type, name| "#{name} = #{Boundary.encoder_for(type, registry)}" }
end

#decoder_for(type, registry) ⇒ Object



14
15
16
# File 'lib/jade/codegen/boundary/cache.rb', line 14

def decoder_for(type, registry)
  Codegen.boundary_cache[:decoders][type] || Boundary.decoder_for(type, registry)
end

#encoder_for(type, registry) ⇒ Object



18
19
20
# File 'lib/jade/codegen/boundary/cache.rb', line 18

def encoder_for(type, registry)
  Codegen.boundary_cache[:encoders][type] || Boundary.encoder_for(type, registry)
end

#task_arms(task_type, registry) ⇒ Object



22
23
24
25
# File 'lib/jade/codegen/boundary/cache.rb', line 22

def task_arms(task_type, registry)
  task_type => Type::Application(args: [ok_t, err_t])
  [encoder_for(ok_t, registry), encoder_for(err_t, registry)]
end