@@ -586,7 +586,7 @@ let rec find_fun_export (name : name) (exports : export list) : var option =
586
586
else
587
587
find_fun_export name exports
588
588
589
- let rec find_idx p lst : int option =
589
+ let find_idx p lst : int option =
590
590
let rec go idx = function
591
591
| [] -> None
592
592
| x :: xs -> if p x then Some idx else go (idx + 1 ) xs
@@ -830,24 +830,54 @@ let link (em1 : extended_module) libname (em2 : extended_module) =
830
830
for the imported functions *)
831
831
let dm2 =
832
832
Hashtbl. fold (fun import_idx (global_idx , func_idx ) dm2 ->
833
- (* - Find the function in the table (add it if it's not in the table)
834
- - Remove the import
835
- - Add a global at `global_idx` as i32 of the table index *)
833
+
834
+ Printf. printf " import_idx=%d, global_idx=%d, func_idx=%d\n "
835
+ (Int32. to_int import_idx)
836
+ (Int32. to_int global_idx)
837
+ (Int32. to_int func_idx);
838
+
839
+ (* Find the function in the table (add it if it's not in the table) *)
836
840
let (tbl_idx, dm2) = match find_table_idx func_idx dm2.elems with
837
841
| Some tbl_idx -> (tbl_idx, dm2)
838
842
| None ->
839
- (* Function not in elem section, add it *)
843
+ (* Function not in elem section, add it to the last elem section in the
844
+ RTS module. *)
840
845
let last_elem_section = (List. nth dm2.elems (List. length dm2.elems - 1 )).it in
846
+ (* 'offset' field of the elem section gives us the index of the first
847
+ element in the section. *)
841
848
let offset = get_i32_const last_elem_section.offset in
849
+ (* Elements in the section *)
842
850
let var_list: var list = last_elem_section.init in
843
-
851
+ (* Table index of the function we're adding to the section *)
844
852
let tbl_idx = Int32. (add offset (of_int (List. length var_list))) in
845
- (* TODO: Append last elem section with the function index *)
846
- let dm2 = { dm2 with elems = List. append (Lib.List. take (List. length var_list - 1 ) dm2.elems) [last_elem_section @@ no_region] } in
853
+ (* Append the function to the last elem section *)
854
+ let last_elem_section =
855
+ { last_elem_section with
856
+ init = List. append last_elem_section.init [func_idx @@ no_region] }
857
+ in
858
+ (* Update the module *)
859
+ let final_elem_section =
860
+ List. append (Lib.List. init dm2.elems) [last_elem_section @@ no_region]
861
+ in
862
+ let dm2 = { dm2 with elems = final_elem_section } in
847
863
848
864
(tbl_idx, dm2)
849
865
in
850
- (* TODO *)
866
+
867
+ (* Turn import into global (remove import, add global) *)
868
+ let global =
869
+ { gtype = Wasm.Types. GlobalType (Wasm.Types. I32Type , Wasm.Types. Mutable );
870
+ value = [Const (Wasm.Values. I32 tbl_idx @@ no_region) @@ no_region] @@ no_region }
871
+ in
872
+ let dm2 =
873
+ { dm2 with
874
+ (* FIXME: Lots of other imports removed already so the index here is
875
+ not right. Maybe remove this with others above? *)
876
+ (* imports = Lib.List.remove (Int32.to_int import_idx) dm2.imports; *)
877
+ globals = Lib.List. insert (Int32. to_int global_idx) (global @@ no_region) dm2.globals;
878
+ }
879
+ in
880
+
851
881
dm2
852
882
) got_func_imports dm2
853
883
in
0 commit comments