Skip to content

Commit e434c34

Browse files
committed
Vaidation returns module type
1 parent d499afc commit e434c34

File tree

7 files changed

+57
-43
lines changed

7 files changed

+57
-43
lines changed

interpreter/script/import.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ let registry = ref Registry.empty
1010

1111
let register name lookup = registry := Registry.add name lookup !registry
1212

13-
let lookup (ImportT (et, module_name, item_name)) at : Instance.extern =
14-
try Registry.find module_name !registry item_name et with Not_found ->
13+
let lookup (ImportT (module_name, item_name, xt)) at : Instance.extern =
14+
try Registry.find module_name !registry item_name xt with Not_found ->
1515
Unknown.error at
1616
("unknown import \"" ^ Types.string_of_name module_name ^
1717
"\".\"" ^ Types.string_of_name item_name ^ "\"")

interpreter/script/js.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ type env =
226226

227227
let exports m : exports =
228228
let ModuleT (_, ets) = moduletype_of m in
229-
List.fold_left (fun map (ExportT (et, name)) -> NameMap.add name et map)
229+
List.fold_left (fun map (ExportT (name, xt)) -> NameMap.add name xt map)
230230
NameMap.empty ets
231231

232232
let env () : env =
@@ -622,7 +622,7 @@ let wrap item_name wrap_action wrap_assertion at =
622622
let funcs = [Func (0l @@ at, locals, body) @@ at] in
623623
let m = {empty_module with types; funcs; imports; exports} @@ at in
624624
(try
625-
Valid.check_module m; (* sanity check *)
625+
ignore (Valid.check_module m); (* sanity check *)
626626
with Valid.Invalid _ as exn ->
627627
prerr_endline (string_of_region at ^
628628
": internal error in JS converter, invalid wrapper module generated:");

interpreter/script/run.ml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,10 @@ let indent s =
220220
let lines = List.filter ((<>) "") (String.split_on_char '\n' s) in
221221
String.concat "\n" (List.map ((^) " ") lines) ^ "\n"
222222

223-
let print_module x_opt m =
223+
let print_moduletype x_opt mt =
224224
Printf.printf "module%s :\n%s%!"
225225
(match x_opt with None -> "" | Some x -> " " ^ x.it)
226-
(indent (Types.string_of_moduletype (Ast.moduletype_of m)))
226+
(indent (Types.string_of_moduletype mt))
227227

228228
let print_values vs =
229229
let ts = List.map Value.type_of_value vs in
@@ -469,7 +469,7 @@ let run_assertion ass =
469469
trace "Asserting invalid...";
470470
(match
471471
let m, cs = run_definition def in
472-
Valid.check_module_with_custom (m, cs)
472+
ignore (Valid.check_module_with_custom (m, cs))
473473
with
474474
| exception Valid.Invalid (_, msg) ->
475475
assert_message ass.at "validation" msg re
@@ -480,7 +480,7 @@ let run_assertion ass =
480480
trace "Asserting invalid custom...";
481481
(match
482482
let m, cs = run_definition def in
483-
Valid.check_module_with_custom (m, cs)
483+
ignore (Valid.check_module_with_custom (m, cs))
484484
with
485485
| exception Custom.Invalid (_, msg) ->
486486
assert_message ass.at "custom validation" msg re
@@ -490,7 +490,7 @@ let run_assertion ass =
490490
| AssertUnlinkable (x_opt, re) ->
491491
trace "Asserting unlinkable...";
492492
let m, cs = lookup_module x_opt ass.at in
493-
if not !Flags.unchecked then Valid.check_module_with_custom (m, cs);
493+
if not !Flags.unchecked then ignore (Valid.check_module_with_custom (m, cs));
494494
(match
495495
let imports = Import.link m in
496496
ignore (Eval.init m imports)
@@ -503,7 +503,7 @@ let run_assertion ass =
503503
| AssertUninstantiable (x_opt, re) ->
504504
trace "Asserting trap...";
505505
let m, cs = lookup_module x_opt ass.at in
506-
if not !Flags.unchecked then Valid.check_module_with_custom (m, cs);
506+
if not !Flags.unchecked then ignore (Valid.check_module_with_custom (m, cs));
507507
(match
508508
let imports = Import.link m in
509509
ignore (Eval.init m imports)
@@ -547,10 +547,10 @@ let rec run_command cmd =
547547
let m, cs = run_definition def in
548548
if not !Flags.unchecked then begin
549549
trace "Checking...";
550-
Valid.check_module_with_custom (m, cs);
550+
let mt = Valid.check_module_with_custom (m, cs) in
551551
if !Flags.print_sig then begin
552552
trace "Signature:";
553-
print_module x_opt m
553+
print_moduletype x_opt mt
554554
end
555555
end;
556556
bind "module" modules x_opt (m, cs);

interpreter/syntax/ast.ml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -345,8 +345,6 @@ and module_' =
345345
}
346346

347347

348-
(* Auxiliary functions *)
349-
350348
let empty_module =
351349
{
352350
types = [];
@@ -362,6 +360,9 @@ let empty_module =
362360
exports = [];
363361
}
364362

363+
364+
(* Extract module type (unchecked) *)
365+
365366
open Source
366367

367368
let deftypes_of (m : module_) : deftype list =
@@ -374,7 +375,7 @@ let deftypes_of (m : module_) : deftype list =
374375
let importtype_of (m : module_) (im : import) : importtype =
375376
let Import (module_name, item_name, xt) = im.it in
376377
let dts = deftypes_of m in
377-
ImportT (subst_externtype (subst_of dts) xt, module_name, item_name)
378+
ImportT (module_name, item_name, subst_externtype (subst_of dts) xt)
378379

379380
let exporttype_of (m : module_) (ex : export) : exporttype =
380381
let Export (name, xx) = ex.it in
@@ -403,7 +404,7 @@ let exporttype_of (m : module_) (ex : export) : exporttype =
403404
let tts = tags xts @ List.map (fun t ->
404405
let Tag tt = t.it in tt) m.it.tags in
405406
ExternTagT (Lib.List32.nth tts x.it)
406-
in ExportT (subst_externtype (subst_of dts) xt, name)
407+
in ExportT (name, subst_externtype (subst_of dts) xt)
407408

408409
let moduletype_of (m : module_) : moduletype =
409410
let its = List.map (importtype_of m) m.it.imports in

interpreter/syntax/types.ml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ type externtype =
5757
| ExternGlobalT of globaltype
5858
| ExternTagT of tagtype
5959

60-
type exporttype = ExportT of externtype * name
61-
type importtype = ImportT of externtype * name * name
60+
type exporttype = ExportT of name * externtype
61+
type importtype = ImportT of name * name * externtype
6262
type moduletype = ModuleT of importtype list * exporttype list
6363

6464

@@ -135,8 +135,8 @@ let structtype_of_comptype = function StructCT st -> st | _ -> assert false
135135
let arraytype_of_comptype = function ArrayCT at -> at | _ -> assert false
136136
let functype_of_comptype = function FuncCT ft -> ft | _ -> assert false
137137

138-
let externtype_of_importtype = function ImportT (et, _, _) -> et
139-
let externtype_of_exporttype = function ExportT (et, _) -> et
138+
let externtype_of_importtype = function ImportT (_, _, xt) -> xt
139+
let externtype_of_exporttype = function ExportT (_, xt) -> xt
140140

141141

142142
(* Filters *)
@@ -247,11 +247,11 @@ let subst_externtype s = function
247247

248248

249249
let subst_exporttype s = function
250-
| ExportT (xt, name) -> ExportT (subst_externtype s xt, name)
250+
| ExportT (name, xt) -> ExportT (name, subst_externtype s xt)
251251

252252
let subst_importtype s = function
253-
| ImportT (xt, module_name, name) ->
254-
ImportT (subst_externtype s xt, module_name, name)
253+
| ImportT (module_name, name, xt) ->
254+
ImportT (module_name, name, subst_externtype s xt)
255255

256256
let subst_moduletype s = function
257257
| ModuleT (its, ets) ->
@@ -447,11 +447,11 @@ let string_of_externtype = function
447447

448448

449449
let string_of_exporttype = function
450-
| ExportT (xt, name) ->
450+
| ExportT (name, xt) ->
451451
"\"" ^ string_of_name name ^ "\" : " ^ string_of_externtype xt
452452

453453
let string_of_importtype = function
454-
| ImportT (xt, module_name, name) ->
454+
| ImportT (module_name, name, xt) ->
455455
"\"" ^ string_of_name module_name ^ "\" \"" ^
456456
string_of_name name ^ "\" : " ^ string_of_externtype xt
457457

interpreter/valid/valid.ml

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,23 +1125,30 @@ let check_import (c : context) (im : import) : context =
11251125

11261126
module NameSet = Set.Make(struct type t = Ast.name let compare = compare end)
11271127

1128-
let check_export (c : context) (set : NameSet.t) (ex : export) : NameSet.t =
1128+
let check_export (c : context) (ex : export) : exporttype =
11291129
let Export (name, xx) = ex.it in
1130-
(match xx.it with
1131-
| FuncX x -> ignore (func c x)
1132-
| GlobalX x -> ignore (global c x)
1133-
| TableX x -> ignore (table c x)
1134-
| MemoryX x -> ignore (memory c x)
1135-
| TagX x -> ignore (tag c x)
1136-
);
1137-
require (not (NameSet.mem name set)) ex.at "duplicate export name";
1138-
NameSet.add name set
1139-
1130+
let xt =
1131+
match xx.it with
1132+
| FuncX x -> ExternFuncT (Def (func c x))
1133+
| GlobalX x -> ExternGlobalT (global c x)
1134+
| TableX x -> ExternTableT (table c x)
1135+
| MemoryX x -> ExternMemoryT (memory c x)
1136+
| TagX x -> ExternTagT (tag c x)
1137+
in ExportT (name, xt)
11401138

11411139
let check_list f xs (c : context) : context =
11421140
List.fold_left f c xs
11431141

1144-
let check_module (m : module_) =
1142+
let check_names (names : name list) at =
1143+
ignore (
1144+
List.fold_left (fun set name ->
1145+
require (not (NameSet.mem name set)) at
1146+
("duplicate export name \"" ^ string_of_name name ^ "\"");
1147+
NameSet.add name set
1148+
) NameSet.empty names
1149+
)
1150+
1151+
let check_module (m : module_) : moduletype =
11451152
let refs = Free.module_ ({m.it with funcs = []; start = None} @@ m.at) in
11461153
let c =
11471154
{empty_context with refs}
@@ -1157,8 +1164,14 @@ let check_module (m : module_) =
11571164
in
11581165
List.iter (check_func_body c) m.it.funcs;
11591166
Option.iter (check_start c) m.it.start;
1160-
ignore (List.fold_left (check_export c) NameSet.empty m.it.exports)
1167+
let its = List.map (fun {it = Import (mnm, nm, xt); _} -> ImportT (mnm, nm, xt)) m.it.imports in
1168+
let ets = List.map (check_export c) m.it.exports in
1169+
check_names (List.map (fun (ExportT (nm, _xt)) -> nm) ets) m.at;
1170+
subst_moduletype (subst_of c.types) (ModuleT (its, ets))
1171+
11611172

1162-
let check_module_with_custom ((m : module_), (cs : Custom.section list)) =
1163-
check_module m;
1164-
List.iter (fun (module S : Custom.Section) -> S.Handler.check m S.it) cs
1173+
let check_module_with_custom ((m : module_), (cs : Custom.section list))
1174+
: moduletype =
1175+
let mt = check_module m in
1176+
List.iter (fun (module S : Custom.Section) -> S.Handler.check m S.it) cs;
1177+
mt

interpreter/valid/valid.mli

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
exception Invalid of Source.region * string
22

3-
val check_module : Ast.module_ -> unit (* raises Invalid *)
4-
val check_module_with_custom : Ast.module_ * Custom.section list -> unit (* raises Invalid, Custom.Check *)
3+
val check_module : Ast.module_ -> Types.moduletype (* raises Invalid *)
4+
val check_module_with_custom : Ast.module_ * Custom.section list -> Types.moduletype (* raises Invalid, Custom.Check *)

0 commit comments

Comments
 (0)