Module Wire.ParamSource

Formal parameters for codecs.

A parameter lets a codec depend on a value that is not in the buffer. An input parameter is supplied by the caller before decoding -- use it in constraints or size expressions. An output parameter is written by an action during decoding -- read it back afterwards to extract a computed result.

Both kinds carry a wire type (uint8 ... uint64, bool, enum) so the same definition can be projected to EverParse 3D.

let max_len = Param.input "max_len" uint16be
let out_len = Param.output "out_len" uint16be
let f_length = Field.v "Length" uint16be

let f_data =
  Field.v "Data"
    ~action:
      (Action.on_success [ Action.assign out_len (Field.ref f_length) ])
    (byte_slice ~size:(Field.ref f_length))

let codec =
  let open Codec in
  v "Bounded"
    ~where:Expr.(Field.ref f_length <= Param.expr max_len)
    (fun len data -> { len; data })
    [ (f_length $ fun r -> r.len); (f_data $ fun r -> r.data) ]

let env = Codec.env codec |> Param.bind max_len 1024
let _ = Codec.decode_with codec env buf 0
let len = Param.get env out_len

Do not share an env across concurrent decodes.

Sourcetype input = Wire__.Types.param_input

Phantom kind for input parameters.

Sourcetype output = Wire__.Types.param_output

Phantom kind for output parameters.

Sourcetype ('a, 'k) t = ('a, 'k) Wire__.Types.param_handle

Typed handle for one formal parameter.

Sourceval input : string -> 'a typ -> ('a, input) t

input name typ declares an input parameter.

Sourceval output : string -> 'a typ -> ('a, output) t

output name typ declares an output parameter.

Sourceval decl : ('a, 'k) t -> param

Project to an untyped formal declaration (for 3D rendering).

Sourceval name : ('a, 'k) t -> string
Sourcetype env = Wire__.Types.param_env

Parameter environment. Create with Codec.env, bind inputs with bind, read outputs with get.

Sourceval bind : ('a, input) t -> 'a -> env -> env

bind p v env returns an environment with input p set to v.

Sourceval get : env -> ('a, 'k) t -> 'a

get env p reads param p. For outputs, call after decode.

Sourceval expr : ('a, 'k) t -> int expr

expr p returns the expression referencing this param.