Skip to content

Commit da60f10

Browse files
committed
Abstract script runner over engine
1 parent 4e314da commit da60f10

File tree

15 files changed

+836
-702
lines changed

15 files changed

+836
-702
lines changed

interpreter/exec/eval.ml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,15 @@ open Instance
99
(* Errors *)
1010

1111
module Link = Error.Make ()
12-
module Exception = Error.Make ()
1312
module Trap = Error.Make ()
1413
module Crash = Error.Make ()
1514
module Exhaustion = Error.Make ()
1615

1716
exception Link = Link.Error
18-
exception Exception = Exception.Error
1917
exception Trap = Trap.Error
2018
exception Crash = Crash.Error (* failure that cannot happen in valid code *)
2119
exception Exhaustion = Exhaustion.Error
20+
exception Exception of region * Exn.t
2221

2322
let table_error at = function
2423
| Table.Bounds -> "out of bounds table access"
@@ -1146,8 +1145,7 @@ let rec eval (c : config) : value stack =
11461145
Trap.error at msg
11471146

11481147
| vs, {it = Throwing (a, args); at} :: _ ->
1149-
let msg = "uncaught exception with args (" ^ string_of_values args ^ ")" in
1150-
Exception.error at msg
1148+
raise (Exception (at, Exn.Exn (a, args)))
11511149

11521150
| vs, es ->
11531151
eval (step c)
@@ -1183,7 +1181,7 @@ let init_type (inst : moduleinst) (type_ : type_) : moduleinst =
11831181
let x = Lib.List32.length inst.types in
11841182
{inst with types = inst.types @ roll_deftypes x rt}
11851183

1186-
let init_import (inst : moduleinst) (ex : extern) (im : import) : moduleinst =
1184+
let init_import (inst : moduleinst) (ex : externinst) (im : import) : moduleinst =
11871185
let Import (module_name, item_name, xt) = im.it in
11881186
let xt = subst_externtype (subst_of inst) xt in
11891187
let xt' = externtype_of inst.types ex in
@@ -1305,7 +1303,7 @@ let init_list f xs (inst : moduleinst) : moduleinst =
13051303
let init_list2 f xs ys (inst : moduleinst) : moduleinst =
13061304
List.fold_left2 f inst xs ys
13071305

1308-
let init (m : module_) (exts : extern list) : moduleinst =
1306+
let init (m : module_) (exts : externinst list) : moduleinst =
13091307
if List.length exts <> List.length m.it.imports then
13101308
Link.error m.at "wrong number of imports provided for initialisation";
13111309
let inst =

interpreter/exec/eval.mli

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ open Instance
33

44
exception Link of Source.region * string
55
exception Trap of Source.region * string
6-
exception Exception of Source.region * string
76
exception Crash of Source.region * string
87
exception Exhaustion of Source.region * string
8+
exception Exception of Source.region * Exn.t
99

10-
val init : Ast.module_ -> extern list -> moduleinst (* raises Link, Trap *)
11-
val invoke : funcinst -> value list -> value list (* raises Trap *)
10+
val init : Ast.module_ -> externinst list -> moduleinst (* raises Link, Trap, Exception *)
11+
val invoke : funcinst -> value list -> value list (* raises Trap, Exception *)

interpreter/host/env.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ let exit vs =
4343
let lookup name et =
4444
match Utf8.encode name, et with
4545
| "abort", ExternFuncT ut ->
46-
ExternFunc (Func.alloc_host (deftype_of_typeuse ut) abort)
46+
Some (ExternFunc (Func.alloc_host (deftype_of_typeuse ut) abort))
4747
| "exit", ExternFuncT ut ->
48-
ExternFunc (Func.alloc_host (deftype_of_typeuse ut) exit)
49-
| _ -> raise Not_found
48+
Some (ExternFunc (Func.alloc_host (deftype_of_typeuse ut) exit))
49+
| _ -> None

interpreter/host/spectest.ml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,23 @@ let global (GlobalT (_, t) as gt) =
1717
| VecT V128T -> Vec (V128 (V128.I32x4.of_lanes [666l; 666l; 666l; 666l]))
1818
| RefT (_, t) -> Ref (NullRef t)
1919
| BotT -> assert false
20-
in ExternGlobal (Global.alloc gt v)
20+
in Some (ExternGlobal (Global.alloc gt v))
2121

2222
let table =
2323
let tt = TableT (I32AT, {min = 10L; max = Some 20L}, (Null, FuncHT)) in
24-
ExternTable (Table.alloc tt (NullRef FuncHT))
24+
Some (ExternTable (Table.alloc tt (NullRef FuncHT)))
2525

2626
let table64 =
2727
let tt = TableT (I64AT, {min = 10L; max = Some 20L}, (Null, FuncHT)) in
28-
ExternTable (Table.alloc tt (NullRef FuncHT))
28+
Some (ExternTable (Table.alloc tt (NullRef FuncHT)))
2929

3030
let memory =
3131
let mt = MemoryT (I32AT, {min = 1L; max = Some 2L}) in
32-
ExternMemory (Memory.alloc mt)
32+
Some (ExternMemory (Memory.alloc mt))
3333

3434
let func f ts1 ts2 =
3535
let dt = DefT (RecT [SubT (Final, [], FuncT (ts1, ts2))], 0l) in
36-
ExternFunc (Func.alloc_host dt (f ts1 ts2))
36+
Some (ExternFunc (Func.alloc_host dt (f ts1 ts2)))
3737

3838
let print_value v =
3939
Printf.printf "%s : %s\n"
@@ -61,4 +61,4 @@ let lookup name t =
6161
| "table", _ -> table
6262
| "table64", _ -> table64
6363
| "memory", _ -> memory
64-
| _ -> raise Not_found
64+
| _ -> None

interpreter/main/main.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ let all_handlers = [
88
]
99

1010
let configure custom_handlers =
11-
Import.register (Utf8.decode "spectest") Spectest.lookup;
12-
Import.register (Utf8.decode "env") Env.lookup;
11+
Run.register_virtual (Utf8.decode "spectest") Spectest.lookup;
12+
Run.register_virtual (Utf8.decode "env") Env.lookup;
1313
List.iter Custom.register custom_handlers
1414

1515
let banner () =

interpreter/runtime/exn.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
open Types
22
open Value
33

4-
type exn_ = Exn of Tag.t * value list
4+
type t = exn_
5+
and exn_ = Exn of Tag.t * value list
56

67
type ref_ += ExnRef of exn_
78

interpreter/runtime/exn.mli

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
open Types
22
open Value
33

4-
type exn_ = Exn of Tag.t * value list
4+
type t = exn_
5+
and exn_ = Exn of Tag.t * value list
56

67
type ref_ += ExnRef of exn_
78

interpreter/runtime/instance.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ and tableinst = Table.t
2121
and funcinst = moduleinst Lib.Promise.t Func.t
2222
and datainst = Data.t
2323
and eleminst = Elem.t
24-
and exportinst = Ast.name * extern
24+
and exportinst = Ast.name * externinst
2525

26-
and extern =
26+
and externinst =
2727
| ExternTag of taginst
2828
| ExternGlobal of globalinst
2929
| ExternMemory of memoryinst

interpreter/script/engine.ml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
type module_ = Ast.module_
2+
type value = Value.value
3+
type ref_ = Value.ref_
4+
5+
module type Engine =
6+
sig
7+
type moduleinst
8+
type taginst
9+
type globalinst
10+
type memoryinst
11+
type tableinst
12+
type funcinst
13+
14+
type externinst =
15+
| ExternTag of taginst
16+
| ExternGlobal of globalinst
17+
| ExternMemory of memoryinst
18+
| ExternTable of tableinst
19+
| ExternFunc of funcinst
20+
21+
type error = Source.region * string
22+
type 'a return =
23+
| Return of 'a
24+
| Exn of Source.region * taginst * value list
25+
| Trap of error
26+
| Exhaustion of error
27+
28+
val validate : module_ -> (Types.moduletype, error) result
29+
val validate_with_custom :
30+
module_ * Custom.section list -> (Types.moduletype, error) result
31+
val instantiate :
32+
module_ -> externinst list -> (moduleinst return, error) result
33+
34+
val module_export : moduleinst -> Ast.name -> externinst option
35+
36+
val tag_type : taginst -> Types.tagtype
37+
38+
val global_type : globalinst -> Types.globaltype
39+
val global_get : globalinst -> value
40+
val global_set : globalinst -> value -> unit
41+
42+
val memory_type : memoryinst -> Types.memorytype
43+
val memory_size : memoryinst -> int64
44+
val memory_grow : memoryinst -> int64 -> unit option
45+
val memory_load_byte : memoryinst -> int64 -> int option
46+
val memory_store_byte : memoryinst -> int64 -> int -> unit option
47+
48+
val table_type : tableinst -> Types.tabletype
49+
val table_size : tableinst -> int64
50+
val table_grow : tableinst -> int64 -> ref_ -> unit option
51+
val table_get : tableinst -> int64 -> ref_ option
52+
val table_set : tableinst -> int64 -> ref_ -> unit option
53+
54+
val func_type : funcinst -> Types.deftype
55+
val func_call : funcinst -> value list -> value list return
56+
end

interpreter/script/import.ml

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)