Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 6 additions & 18 deletions lib/codegen.ml
Original file line number Diff line number Diff line change
Expand Up @@ -157,24 +157,12 @@ let ownership_kind_byte = function
u8* param_kind (one per param, see kind encoding above)
u8 return_kind *)
let build_ownership_section (annots : (int * ownership_kind list * ownership_kind) list) : bytes =
if annots = [] then Bytes.empty
else
let buf = Buffer.create 64 in
let write_u32_le n =
Buffer.add_char buf (Char.chr (n land 0xff));
Buffer.add_char buf (Char.chr ((n lsr 8) land 0xff));
Buffer.add_char buf (Char.chr ((n lsr 16) land 0xff));
Buffer.add_char buf (Char.chr ((n lsr 24) land 0xff))
in
let write_u8 n = Buffer.add_char buf (Char.chr (n land 0xff)) in
write_u32_le (List.length annots);
List.iter (fun (func_idx, param_kinds, ret_kind) ->
write_u32_le func_idx;
write_u8 (List.length param_kinds);
List.iter (fun k -> write_u8 (ownership_kind_byte k)) param_kinds;
write_u8 (ownership_kind_byte ret_kind)
) annots;
Buffer.to_bytes buf
Tw_section.Encode.ownership
(List.map (fun (func_idx, param_kinds, ret_kind) ->
(func_idx,
List.map ownership_kind_byte param_kinds,
ownership_kind_byte ret_kind))
annots)

(** Map AffineScript type to WASM value type *)
let type_to_wasm (ty : type_expr) : value_type result =
Expand Down
1 change: 1 addition & 0 deletions lib/dune
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
token
trait
tw_interface
tw_section
tw_verify
typecheck
types
Expand Down
96 changes: 0 additions & 96 deletions lib/tw_ownership_section.ml

This file was deleted.

25 changes: 25 additions & 0 deletions lib/tw_section.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
(* SPDX-License-Identifier: MPL-2.0 *)
(* SPDX-FileCopyrightText: 2026 hyperpolymath *)

module Encode = struct
let write_u32_le buf n =
Buffer.add_char buf (Char.chr (n land 0xff));
Buffer.add_char buf (Char.chr ((n lsr 8) land 0xff));
Buffer.add_char buf (Char.chr ((n lsr 16) land 0xff));
Buffer.add_char buf (Char.chr ((n lsr 24) land 0xff))

let write_u8 buf n = Buffer.add_char buf (Char.chr (n land 0xff))

let ownership (annots : (int * int list * int) list) : bytes =
if annots = [] then Bytes.empty
else
let buf = Buffer.create 64 in
write_u32_le buf (List.length annots);
List.iter (fun (func_idx, param_kind_bytes, ret_kind_byte) ->
write_u32_le buf func_idx;
write_u8 buf (List.length param_kind_bytes);
List.iter (fun b -> write_u8 buf b) param_kind_bytes;
write_u8 buf ret_kind_byte
) annots;
Buffer.to_bytes buf
end
36 changes: 36 additions & 0 deletions lib/tw_section.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
(* SPDX-License-Identifier: MPL-2.0 *)
(* SPDX-FileCopyrightText: 2026 hyperpolymath *)

(** Canonical home for typed-wasm custom-section encoders.

Wire encoders operate on pre-byte-encoded triples to keep this
module free of any dependency on producer-side ownership types
(avoiding a cycle with [Codegen]). Callers map their domain
types to bytes before invoking.

Forward-compatibility slot for typed-wasm proposal 0001 (issue
hyperpolymath/typed-wasm#34): [Encode.regions] and
[Encode.capabilities] land here when the proposal promotes to
[accepted]. Sharing the internal LE writers (u8 / u32le / leb128)
inside this module prevents the 3-sections × 2-copies = 6-encoder
fan-out flagged by hyperpolymath/affinescript#444. *)

module Encode : sig
(** v1 wire format for [typedwasm.ownership]:
u32le entry_count
per entry:
u32le func_index
u8 param_count
u8* param_kind (one per param)
u8 return_kind

Caller responsibilities:
- Map domain ownership type → byte before calling
(see [Codegen.ownership_kind_byte]).
- Empty input → [Bytes.empty]; caller omits the section.

Producer-emit is v1 per ADR-021. Parser-side v2 support lives in
[Tw_verify.parse_ownership_section_payload]; emit-side flip is
cross-repo-coordinated, not local to this function. *)
val ownership : (int * int list * int) list -> bytes
end
2 changes: 1 addition & 1 deletion lib/tw_verify.ml
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ let verify_module

Per ADR-021's coordinated landing protocol, this verifier ships
parse-support FIRST. AffineScript's emitter ([Codegen.build_ownership_
section] / [Tw_ownership_section.build_section]) stays on v1 until
section] / [Tw_section.Encode.ownership]) stays on v1 until
[hyperpolymath/typed-wasm]'s Rust verifier + [hyperpolymath/ephapax]'s
OCaml verifier also ship parse-support; then all producers flip to v2
emit together. See [docs/specs/TYPED-WASM-COORDINATION-LEDGER.adoc]
Expand Down
Loading