Skip to content
This repository was archived by the owner on Apr 25, 2025. It is now read-only.

Commit b3b3ed2

Browse files
[interpreter] Add event section
1 parent 176d5c3 commit b3b3ed2

File tree

16 files changed

+220
-18
lines changed

16 files changed

+220
-18
lines changed

interpreter/binary/decode.ml

+14-1
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,7 @@ let id s =
527527
| 10 -> `CodeSection
528528
| 11 -> `DataSection
529529
| 12 -> `DataCountSection
530+
| 13 -> `EventSection
530531
| _ -> error s (pos s) "malformed section id"
531532
) bo
532533

@@ -555,6 +556,7 @@ let import_desc s =
555556
| 0x01 -> TableImport (table_type s)
556557
| 0x02 -> MemoryImport (memory_type s)
557558
| 0x03 -> GlobalImport (global_type s)
559+
| 0x04 -> ignore (vu32 s); EventImport (at var s)
558560
| _ -> error s (pos s - 1) "malformed import kind"
559561

560562
let import s =
@@ -592,6 +594,13 @@ let memory s =
592594
let memory_section s =
593595
section `MemorySection (vec (at memory)) [] s
594596

597+
(* Event section *)
598+
599+
let event s =
600+
ignore (vu32 s); var s
601+
602+
let event_section s =
603+
section `EventSection (vec (at event)) [] s
595604

596605
(* Global section *)
597606

@@ -612,6 +621,7 @@ let export_desc s =
612621
| 0x01 -> TableExport (at var s)
613622
| 0x02 -> MemoryExport (at var s)
614623
| 0x03 -> GlobalExport (at var s)
624+
| 0x04 -> EventExport (at var s)
615625
| _ -> error s (pos s - 1) "malformed export kind"
616626

617627
let export s =
@@ -787,6 +797,8 @@ let module_ s =
787797
iterate custom_section s;
788798
let memories = memory_section s in
789799
iterate custom_section s;
800+
let events = event_section s in
801+
iterate custom_section s;
790802
let globals = global_section s in
791803
iterate custom_section s;
792804
let exports = export_section s in
@@ -812,7 +824,8 @@ let module_ s =
812824
let funcs =
813825
List.map2 Source.(fun t f -> {f.it with ftype = t} @@ f.at)
814826
func_types func_bodies
815-
in {types; tables; memories; globals; funcs; imports; exports; elems; datas; start}
827+
in {types; tables; memories; events; globals; funcs; imports; exports; elems;
828+
datas; start}
816829

817830

818831
let decode name bs = at module_ (stream name bs)

interpreter/binary/encode.ml

+9
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,7 @@ let encode m =
426426
| TableImport t -> u8 0x01; table_type t
427427
| MemoryImport t -> u8 0x02; memory_type t
428428
| GlobalImport t -> u8 0x03; global_type t
429+
| EventImport x -> u8 0x04; vu32 0x00l; var x
429430

430431
let import im =
431432
let {module_name; item_name; idesc} = im.it in
@@ -456,6 +457,12 @@ let encode m =
456457
let memory_section mems =
457458
section 5 (vec memory) mems (mems <> [])
458459

460+
(* Event section *)
461+
let event e = vu32 0x00l; var e
462+
463+
let event_section es =
464+
section 13 (vec event) es (es <> [])
465+
459466
(* Global section *)
460467
let global g =
461468
let {gtype; ginit} = g.it in
@@ -471,6 +478,7 @@ let encode m =
471478
| TableExport x -> u8 1; var x
472479
| MemoryExport x -> u8 2; var x
473480
| GlobalExport x -> u8 3; var x
481+
| EventExport x -> u8 4; var x
474482

475483
let export ex =
476484
let {name = n; edesc} = ex.it in
@@ -579,6 +587,7 @@ let encode m =
579587
func_section m.it.funcs;
580588
table_section m.it.tables;
581589
memory_section m.it.memories;
590+
event_section m.it.events;
582591
global_section m.it.globals;
583592
export_section m.it.exports;
584593
start_section m.it.start;

interpreter/exec/eval.ml

+8-1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ let type_ (inst : module_inst) x = lookup "type" inst.types x
8585
let func (inst : module_inst) x = lookup "function" inst.funcs x
8686
let table (inst : module_inst) x = lookup "table" inst.tables x
8787
let memory (inst : module_inst) x = lookup "memory" inst.memories x
88+
let event (inst : module_inst) x = lookup "event" inst.events x
8889
let global (inst : module_inst) x = lookup "global" inst.globals x
8990
let elem (inst : module_inst) x = lookup "element segment" inst.elems x
9091
let data (inst : module_inst) x = lookup "data segment" inst.datas x
@@ -580,6 +581,9 @@ let create_memory (inst : module_inst) (mem : memory) : memory_inst =
580581
let {mtype} = mem.it in
581582
Memory.alloc mtype
582583

584+
let create_event (inst : module_inst) (e : event) : event_inst =
585+
type_ inst e
586+
583587
let create_global (inst : module_inst) (glob : global) : global_inst =
584588
let {gtype; ginit} = glob.it in
585589
let v = eval_const inst ginit in
@@ -593,6 +597,7 @@ let create_export (inst : module_inst) (ex : export) : export_inst =
593597
| TableExport x -> ExternTable (table inst x)
594598
| MemoryExport x -> ExternMemory (memory inst x)
595599
| GlobalExport x -> ExternGlobal (global inst x)
600+
| EventExport x -> ExternEvent (event inst x)
596601
in (name, ext)
597602

598603
let create_elem (inst : module_inst) (seg : elem_segment) : elem_inst =
@@ -612,6 +617,7 @@ let add_import (m : module_) (ext : extern) (im : import) (inst : module_inst)
612617
| ExternFunc func -> {inst with funcs = func :: inst.funcs}
613618
| ExternTable tab -> {inst with tables = tab :: inst.tables}
614619
| ExternMemory mem -> {inst with memories = mem :: inst.memories}
620+
| ExternEvent event -> {inst with events = event :: inst.events}
615621
| ExternGlobal glob -> {inst with globals = glob :: inst.globals}
616622

617623
let init_func (inst : module_inst) (func : func_inst) =
@@ -654,7 +660,7 @@ let run_start start =
654660

655661
let init (m : module_) (exts : extern list) : module_inst =
656662
let
657-
{ imports; tables; memories; globals; funcs; types;
663+
{ imports; tables; memories; events; globals; funcs; types;
658664
exports; elems; datas; start
659665
} = m.it
660666
in
@@ -670,6 +676,7 @@ let init (m : module_) (exts : extern list) : module_inst =
670676
{ inst1 with
671677
tables = inst1.tables @ List.map (create_table inst1) tables;
672678
memories = inst1.memories @ List.map (create_memory inst1) memories;
679+
events = inst1.events @ List.map (create_event inst1) events;
673680
globals = inst1.globals @ List.map (create_global inst1) globals;
674681
}
675682
in

interpreter/runtime/instance.ml

+6-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ type module_inst =
66
funcs : func_inst list;
77
tables : table_inst list;
88
memories : memory_inst list;
9+
events : event_inst list;
910
globals : global_inst list;
1011
exports : export_inst list;
1112
elems : elem_inst list;
@@ -15,6 +16,7 @@ type module_inst =
1516
and func_inst = module_inst ref Func.t
1617
and table_inst = Table.t
1718
and memory_inst = Memory.t
19+
and event_inst = func_type
1820
and global_inst = Global.t
1921
and export_inst = Ast.name * extern
2022
and elem_inst = Values.ref_ list ref
@@ -24,6 +26,7 @@ and extern =
2426
| ExternFunc of func_inst
2527
| ExternTable of table_inst
2628
| ExternMemory of memory_inst
29+
| ExternEvent of event_inst
2730
| ExternGlobal of global_inst
2831

2932

@@ -47,14 +50,15 @@ let () =
4750
(* Auxiliary functions *)
4851

4952
let empty_module_inst =
50-
{ types = []; funcs = []; tables = []; memories = []; globals = [];
51-
exports = []; elems = []; datas = [] }
53+
{ types = []; funcs = []; tables = []; memories = []; events = [];
54+
globals = []; exports = []; elems = []; datas = [] }
5255

5356
let extern_type_of = function
5457
| ExternFunc func -> ExternFuncType (Func.type_of func)
5558
| ExternTable tab -> ExternTableType (Table.type_of tab)
5659
| ExternMemory mem -> ExternMemoryType (Memory.type_of mem)
5760
| ExternGlobal glob -> ExternGlobalType (Global.type_of glob)
61+
| ExternEvent event -> ExternEventType event
5862

5963
let export inst name =
6064
try Some (List.assoc name inst.exports) with Not_found -> None

interpreter/script/run.ml

+2
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ let print_import m im =
215215
| ExternFuncType t -> "func", string_of_func_type t
216216
| ExternTableType t -> "table", string_of_table_type t
217217
| ExternMemoryType t -> "memory", string_of_memory_type t
218+
| ExternEventType t -> "event", string_of_func_type t
218219
| ExternGlobalType t -> "global", string_of_global_type t
219220
in
220221
Printf.printf " import %s \"%s\" \"%s\" : %s\n"
@@ -228,6 +229,7 @@ let print_export m ex =
228229
| ExternFuncType t -> "func", string_of_func_type t
229230
| ExternTableType t -> "table", string_of_table_type t
230231
| ExternMemoryType t -> "memory", string_of_memory_type t
232+
| ExternEventType t -> "event", string_of_func_type t
231233
| ExternGlobalType t -> "global", string_of_global_type t
232234
in
233235
Printf.printf " export %s \"%s\" : %s\n"

interpreter/syntax/ast.ml

+11
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ and memory' =
150150
mtype : memory_type;
151151
}
152152

153+
type event = int32 Source.phrase
154+
153155
type segment_mode = segment_mode' Source.phrase
154156
and segment_mode' =
155157
| Passive
@@ -182,6 +184,7 @@ and export_desc' =
182184
| TableExport of var
183185
| MemoryExport of var
184186
| GlobalExport of var
187+
| EventExport of var
185188

186189
type export = export' Source.phrase
187190
and export' =
@@ -196,6 +199,7 @@ and import_desc' =
196199
| TableImport of table_type
197200
| MemoryImport of memory_type
198201
| GlobalImport of global_type
202+
| EventImport of var
199203

200204
type import = import' Source.phrase
201205
and import' =
@@ -212,6 +216,7 @@ and module_' =
212216
globals : global list;
213217
tables : table list;
214218
memories : memory list;
219+
events : event list;
215220
funcs : func list;
216221
start : var option;
217222
elems : elem_segment list;
@@ -229,6 +234,7 @@ let empty_module =
229234
globals = [];
230235
tables = [];
231236
memories = [];
237+
events = [];
232238
funcs = [];
233239
start = None;
234240
elems = [];
@@ -248,6 +254,7 @@ let import_type (m : module_) (im : import) : extern_type =
248254
| FuncImport x -> ExternFuncType (func_type_for m x)
249255
| TableImport t -> ExternTableType t
250256
| MemoryImport t -> ExternMemoryType t
257+
| EventImport x -> ExternEventType (func_type_for m x)
251258
| GlobalImport t -> ExternGlobalType t
252259

253260
let export_type (m : module_) (ex : export) : extern_type =
@@ -268,6 +275,10 @@ let export_type (m : module_) (ex : export) : extern_type =
268275
| GlobalExport x ->
269276
let gts = globals its @ List.map (fun g -> g.it.gtype) m.it.globals in
270277
ExternGlobalType (nth gts x.it)
278+
| EventExport x ->
279+
let ets =
280+
events its @ List.map (fun e -> func_type_for m e) m.it.events
281+
in ExternEventType (nth ets x.it)
271282

272283
let string_of_name n =
273284
let b = Buffer.create 16 in

interpreter/syntax/free.ml

+7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ type t =
99
globals : Set.t;
1010
tables : Set.t;
1111
memories : Set.t;
12+
events : Set.t;
1213
funcs : Set.t;
1314
elems : Set.t;
1415
datas : Set.t;
@@ -22,6 +23,7 @@ let empty : t =
2223
globals = Set.empty;
2324
tables = Set.empty;
2425
memories = Set.empty;
26+
events = Set.empty;
2527
funcs = Set.empty;
2628
elems = Set.empty;
2729
datas = Set.empty;
@@ -35,6 +37,7 @@ let union (s1 : t) (s2 : t) : t =
3537
globals = Set.union s1.globals s2.globals;
3638
tables = Set.union s1.tables s2.tables;
3739
memories = Set.union s1.memories s2.memories;
40+
events = Set.union s1.events s2.events;
3841
funcs = Set.union s1.funcs s2.funcs;
3942
elems = Set.union s1.elems s2.elems;
4043
datas = Set.union s1.datas s2.datas;
@@ -46,6 +49,7 @@ let types s = {empty with types = s}
4649
let globals s = {empty with globals = s}
4750
let tables s = {empty with tables = s}
4851
let memories s = {empty with memories = s}
52+
let events s = {empty with events = s}
4953
let funcs s = {empty with funcs = s}
5054
let elems s = {empty with elems = s}
5155
let datas s = {empty with datas = s}
@@ -93,6 +97,7 @@ let global (g : global) = const g.it.ginit
9397
let func (f : func) = {(block f.it.body) with locals = Set.empty}
9498
let table (t : table) = empty
9599
let memory (m : memory) = empty
100+
let event (e : event) = empty
96101

97102
let segment_mode f (m : segment_mode) =
98103
match m.it with
@@ -112,13 +117,15 @@ let export_desc (d : export_desc) =
112117
| FuncExport x -> funcs (var x)
113118
| TableExport x -> tables (var x)
114119
| MemoryExport x -> memories (var x)
120+
| EventExport x -> events (var x)
115121
| GlobalExport x -> globals (var x)
116122

117123
let import_desc (d : import_desc) =
118124
match d.it with
119125
| FuncImport x -> types (var x)
120126
| TableImport tt -> empty
121127
| MemoryImport mt -> empty
128+
| EventImport x -> types (var x)
122129
| GlobalImport gt -> empty
123130

124131
let export (e : export) = export_desc e.it.edesc

interpreter/syntax/free.mli

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ type t =
66
globals : Set.t;
77
tables : Set.t;
88
memories : Set.t;
9+
events : Set.t;
910
funcs : Set.t;
1011
elems : Set.t;
1112
datas : Set.t;
@@ -25,6 +26,7 @@ val global : Ast.global -> t
2526
val func : Ast.func -> t
2627
val table : Ast.table -> t
2728
val memory : Ast.memory -> t
29+
val event : Ast.event -> t
2830
val elem : Ast.elem_segment -> t
2931
val data : Ast.data_segment -> t
3032
val export : Ast.export -> t

interpreter/syntax/types.ml

+7-1
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ type 'a limits = {min : 'a; max : 'a option}
1010
type mutability = Immutable | Mutable
1111
type table_type = TableType of Int32.t limits * ref_type
1212
type memory_type = MemoryType of Int32.t limits
13+
type event_type = func_type
1314
type global_type = GlobalType of value_type * mutability
1415
type extern_type =
1516
| ExternFuncType of func_type
1617
| ExternTableType of table_type
1718
| ExternMemoryType of memory_type
19+
| ExternEventType of event_type
1820
| ExternGlobalType of global_type
1921

2022
type pack_size = Pack8 | Pack16 | Pack32
@@ -49,6 +51,8 @@ let tables =
4951
Lib.List.map_filter (function ExternTableType t -> Some t | _ -> None)
5052
let memories =
5153
Lib.List.map_filter (function ExternMemoryType t -> Some t | _ -> None)
54+
let events =
55+
Lib.List.map_filter (function ExternEventType t -> Some t | _ -> None)
5256
let globals =
5357
Lib.List.map_filter (function ExternGlobalType t -> Some t | _ -> None)
5458

@@ -76,7 +80,8 @@ let match_global_type gt1 gt2 =
7680

7781
let match_extern_type et1 et2 =
7882
match et1, et2 with
79-
| ExternFuncType ft1, ExternFuncType ft2 -> match_func_type ft1 ft2
83+
| ExternFuncType ft1, ExternFuncType ft2
84+
| ExternEventType ft1, ExternEventType ft2 -> match_func_type ft1 ft2
8085
| ExternTableType tt1, ExternTableType tt2 -> match_table_type tt1 tt2
8186
| ExternMemoryType mt1, ExternMemoryType mt2 -> match_memory_type mt1 mt2
8287
| ExternGlobalType gt1, ExternGlobalType gt2 -> match_global_type gt1 gt2
@@ -132,4 +137,5 @@ let string_of_extern_type = function
132137
| ExternFuncType ft -> "func " ^ string_of_func_type ft
133138
| ExternTableType tt -> "table " ^ string_of_table_type tt
134139
| ExternMemoryType mt -> "memory " ^ string_of_memory_type mt
140+
| ExternEventType et -> "event " ^ string_of_func_type et
135141
| ExternGlobalType gt -> "global " ^ string_of_global_type gt

0 commit comments

Comments
 (0)