5 val to_bytes : t -> BytesOut.t list
8 module Make(Inst : Inst) = struct
16 HList.concat_map f xs in
17 (u30 (List.length xs))::ys
21 { int=[]; uint=[]; double=[]; string=[]; namespace=[]; namespace_set=[]; multiname=[]}
25 HList.concat_map f xs in
31 array ~f:(fun c -> [u8 (Char.code c)]) @@ ExtString.String.explode str
37 | PackageNamespace name ->
39 | PackageInternalNamespace name ->
41 | ProtectedNamespace name ->
43 | ExplicitNamespace name ->
45 | StaticProtectedNamespace name ->
47 | PrivateNamespace name ->
51 array ~f:(fun ns->[u30 ns])
56 [u8 0x07;u30 ns; u30 name]
58 [u8 0x0D;u30 ns; u30 name]
67 | Multiname (name,ns_set) ->
68 [u8 0x09; u30 name; u30 ns_set]
69 | MultinameA (name,ns_set) ->
70 [u8 0x0E; u30 name; u30 ns_set]
79 cpool_map (fun x->[s32 x]) cpool.int;
80 cpool_map (fun x->[u32 x]) cpool.uint;
81 cpool_map (fun x->[d64 x]) cpool.double;
82 cpool_map of_string cpool.string;
83 cpool_map of_ns cpool.namespace;
84 cpool_map of_ns_set cpool.namespace_set;
85 cpool_map of_multiname cpool.multiname;
92 List.fold_left (lor) 0 @@ List.map to_bit xs
95 flags [ATTR_Final ,0x01;
98 (* kind field contains two four-bit fields. The lower four bits determine the kind of this trait.
99 The upper four bits comprise a bit vector providing attributes of the trait. *)
101 u8 @@ ((of_trait_attrs attr) lsl 4) lor kind
105 SlotTrait (slot_id,type_name,vindex,vkind) ->
107 [u8 0;u30 slot_id; u30 type_name;u30 0]
109 [u8 0;u30 slot_id; u30 type_name;u30 vindex;u8 vkind]
110 | MethodTrait (disp_id,meth,attrs) ->
111 [kind attrs 1;u30 disp_id; u30 meth]
112 | GetterTrait (disp_id,meth,attrs) ->
113 [kind attrs 2;u30 disp_id; u30 meth]
114 | SetterTrait (disp_id,meth,attrs) ->
115 [kind attrs 3;u30 disp_id; u30 meth]
116 | ClassTrait (slot_id,classi) ->
117 [u8 4; u30 slot_id; u30 classi]
118 | FunctionTrait (slot_id,func) ->
119 [u8 5;u30 slot_id; u30 func]
120 | ConstTrait (slot_id,type_name,vindex,vkind) ->
122 [u8 6;u30 slot_id; u30 type_name;u30 0]
124 [u8 6;u30 slot_id; u30 type_name;u30 vindex;u8 vkind]
126 let of_trait {trait_name=name; data=data} =
127 List.concat [[u30 name];
131 let of_option_info xs =
132 let detail value kind =
133 [u30 value; u8 kind] in
134 array xs ~f:begin function
153 | PackageNamespaceVal n ->
155 | PackageInternalNamespaceVal n ->
157 | ProtectedNamespaceVal n ->
159 | ExplicitNamespaceVal n ->
161 | StaticProtectedNamespaceVal n ->
163 | PrivateNamespaceVal n ->
167 let of_method_flags xs =
168 let (flags, option, names) =
169 StdLabels.List.fold_left xs ~init:(0,[],[]) ~f:begin fun (flags,option,names) x ->
172 (0x01 lor flags, option, names)
174 (0x02 lor flags, option, names)
176 (0x04 lor flags, option, names)
178 (0x40 lor flags, option, names)
180 (0x08 lor flags, of_option_info xs, names)
181 | HasParamNames xs ->
182 (0x80 lor flags, option, List.map u30 xs)
184 List.concat [ [u8 flags];
188 let of_method_info info =
189 List.concat [[u30 (List.length info.params);
191 List.map u30 info.params;
192 [u30 info.method_name];
193 of_method_flags info.method_flags]
195 let of_script {init=init; script_traits=traits} =
196 (u30 init)::array ~f:of_trait traits
198 let of_method_body body =
202 [ u30 body.method_sig;
204 u30 body.local_count;
205 u30 body.init_scope_depth;
206 u30 body.max_scope_depth];
207 [backpatch 0 (fun addr map -> to_int_list [u30 (find map t - addr)])];
208 HList.concat_map Inst.to_bytes body.code;
210 dummy body.exceptions;
211 array ~f:of_trait body.method_traits]
213 let of_class {cinit=init; class_traits=traits} =
216 array ~f:of_trait traits]
218 let of_instance {instance_name = name;
220 instance_flags = flags;
223 instance_traits = traits} =
229 | ProtectedNs _ -> 0x08 in
231 List.fold_left (fun x y -> x lor (flag y)) 0 flags in
234 +> HList.concat_map begin function
235 ProtectedNs ns -> [u30 ns]
236 | Sealed | Final | Interface -> []
238 +> function [] -> [] | x::_ -> [x] in
244 array ~f:(fun x -> [u30 x]) inf;
246 array ~f:of_trait traits]
248 let to_bytes { cpool;
254 method_bodies=bodies; } =
258 array ~f:of_method_info info;
260 array ~f:of_instance instances;
261 HList.concat_map of_class classes;
262 array ~f:of_script scripts;
263 array ~f:of_method_body bodies