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

Commit 3fc8553

Browse files
fixup! [interpreter] Add event section
1 parent b3b3ed2 commit 3fc8553

File tree

9 files changed

+43
-41
lines changed

9 files changed

+43
-41
lines changed

interpreter/binary/decode.ml

+8-8
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ let sized f s =
135135

136136
open Types
137137

138+
let zero s = expect 0x00 s "zero byte expected"
139+
let var s = vu32 s
140+
138141
let num_type s =
139142
match vs7 s with
140143
| -0x01 -> I32Type
@@ -189,17 +192,17 @@ let global_type s =
189192
let mut = mutability s in
190193
GlobalType (t, mut)
191194

195+
let event_type s =
196+
zero s; var s
197+
192198

193199
(* Decode instructions *)
194200

195201
open Ast
196202
open Operators
197203

198-
let var s = vu32 s
199-
200204
let op s = u8 s
201205
let end_ s = expect 0x0b s "END opcode expected"
202-
let zero s = expect 0x00 s "zero byte expected"
203206

204207
let memop s =
205208
let align = vu32 s in
@@ -556,7 +559,7 @@ let import_desc s =
556559
| 0x01 -> TableImport (table_type s)
557560
| 0x02 -> MemoryImport (memory_type s)
558561
| 0x03 -> GlobalImport (global_type s)
559-
| 0x04 -> ignore (vu32 s); EventImport (at var s)
562+
| 0x04 -> EventImport (at event_type s)
560563
| _ -> error s (pos s - 1) "malformed import kind"
561564

562565
let import s =
@@ -596,11 +599,8 @@ let memory_section s =
596599

597600
(* Event section *)
598601

599-
let event s =
600-
ignore (vu32 s); var s
601-
602602
let event_section s =
603-
section `EventSection (vec (at event)) [] s
603+
section `EventSection (vec (at event_type)) [] s
604604

605605
(* Global section *)
606606

interpreter/binary/encode.ml

+6-4
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ let encode m =
9090
(* Types *)
9191

9292
open Types
93+
open Source
94+
95+
let var x = vu32 x.it
9396

9497
let num_type = function
9598
| I32Type -> vs7 (-0x01)
@@ -118,6 +121,8 @@ let encode m =
118121
let memory_type = function
119122
| MemoryType lim -> limits vu32 lim
120123

124+
let event_type x = vu32 0x00l; var x
125+
121126
let mutability = function
122127
| Immutable -> u8 0
123128
| Mutable -> u8 1
@@ -127,7 +132,6 @@ let encode m =
127132

128133
(* Expressions *)
129134

130-
open Source
131135
open Ast
132136
open Values
133137

@@ -136,8 +140,6 @@ let encode m =
136140

137141
let memop {align; offset; _} = vu32 (Int32.of_int align); vu32 offset
138142

139-
let var x = vu32 x.it
140-
141143
let block_type = function
142144
| VarBlockType x -> vs33 x.it
143145
| ValBlockType None -> vs7 (-0x40)
@@ -426,7 +428,7 @@ let encode m =
426428
| TableImport t -> u8 0x01; table_type t
427429
| MemoryImport t -> u8 0x02; memory_type t
428430
| GlobalImport t -> u8 0x03; global_type t
429-
| EventImport x -> u8 0x04; vu32 0x00l; var x
431+
| EventImport x -> u8 0x04; event_type x
430432

431433
let import im =
432434
let {module_name; item_name; idesc} = im.it in

interpreter/exec/eval.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ let create_memory (inst : module_inst) (mem : memory) : memory_inst =
582582
Memory.alloc mtype
583583

584584
let create_event (inst : module_inst) (e : event) : event_inst =
585-
type_ inst e
585+
Event.alloc (type_ inst e)
586586

587587
let create_global (inst : module_inst) (glob : global) : global_inst =
588588
let {gtype; ginit} = glob.it in

interpreter/runtime/event.ml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
open Types
2+
3+
type event = {ty : func_type}
4+
type t = event
5+
6+
let alloc ty =
7+
{ty}
8+
9+
let type_of evt =
10+
evt.ty

interpreter/runtime/instance.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type module_inst =
1616
and func_inst = module_inst ref Func.t
1717
and table_inst = Table.t
1818
and memory_inst = Memory.t
19-
and event_inst = func_type
19+
and event_inst = Event.t
2020
and global_inst = Global.t
2121
and export_inst = Ast.name * extern
2222
and elem_inst = Values.ref_ list ref
@@ -58,7 +58,7 @@ let extern_type_of = function
5858
| ExternTable tab -> ExternTableType (Table.type_of tab)
5959
| ExternMemory mem -> ExternMemoryType (Memory.type_of mem)
6060
| ExternGlobal glob -> ExternGlobalType (Global.type_of glob)
61-
| ExternEvent event -> ExternEventType event
61+
| ExternEvent event -> ExternEventType (Event.type_of event)
6262

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

interpreter/syntax/types.ml

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,13 +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
13+
type event_type = EventType of int32
1414
type global_type = GlobalType of value_type * mutability
1515
type extern_type =
1616
| ExternFuncType of func_type
1717
| ExternTableType of table_type
1818
| ExternMemoryType of memory_type
19-
| ExternEventType of event_type
19+
| ExternEventType of func_type
2020
| ExternGlobalType of global_type
2121

2222
type pack_size = Pack8 | Pack16 | Pack32
@@ -51,10 +51,10 @@ let tables =
5151
Lib.List.map_filter (function ExternTableType t -> Some t | _ -> None)
5252
let memories =
5353
Lib.List.map_filter (function ExternMemoryType t -> Some t | _ -> None)
54-
let events =
55-
Lib.List.map_filter (function ExternEventType t -> Some t | _ -> None)
5654
let globals =
5755
Lib.List.map_filter (function ExternGlobalType t -> Some t | _ -> None)
56+
let events =
57+
Lib.List.map_filter (function ExternEventType t -> Some t | _ -> None)
5858

5959

6060
(* Subtyping *)
@@ -137,5 +137,5 @@ let string_of_extern_type = function
137137
| ExternFuncType ft -> "func " ^ string_of_func_type ft
138138
| ExternTableType tt -> "table " ^ string_of_table_type tt
139139
| ExternMemoryType mt -> "memory " ^ string_of_memory_type mt
140-
| ExternEventType et -> "event " ^ string_of_func_type et
141140
| ExternGlobalType gt -> "global " ^ string_of_global_type gt
141+
| ExternEventType ft -> "event " ^ string_of_func_type ft

interpreter/text/arrange.ml

+6-3
Original file line numberDiff line numberDiff line change
@@ -361,11 +361,14 @@ let data i seg =
361361

362362
(* Events *)
363363

364-
let event off i e =
365-
Node ("event",
364+
let event_with_name name e =
365+
Node ("event" ^ name,
366366
[Node ("type " ^ var e, [])]
367367
)
368368

369+
let event_with_index off i e =
370+
event_with_name (" $" ^ nat (off + i)) e
371+
369372

370373
(* Modules *)
371374

@@ -426,7 +429,7 @@ let module_with_var_opt x_opt m =
426429
imports @
427430
listi (table !tx) m.it.tables @
428431
listi (memory !mx) m.it.memories @
429-
listi (event !ex) m.it.events @
432+
listi (event_with_index !ex) m.it.events @
430433
listi (global !gx) m.it.globals @
431434
listi (func_with_index !fx) m.it.funcs @
432435
list export m.it.exports @

interpreter/text/parser.mly

+3-16
Original file line numberDiff line numberDiff line change
@@ -851,11 +851,11 @@ event :
851851
fun c -> let x = $3 c anon_event bind_event @@ at in fun () -> $4 c x at }
852852

853853
event_fields :
854-
| type_use event_types
854+
| type_use func_type
855855
{ fun c x at ->
856856
let y = inline_type_explicit c ($1 c type_) $2 at in
857857
[y], [], [] }
858-
| event_types /* Sugar */
858+
| func_type /* Sugar */
859859
{ fun c x at ->
860860
let y = inline_type c $1 at in
861861
[y], [], [] }
@@ -873,7 +873,7 @@ event_fields :
873873
idesc = EventImport y @@ at } @@ at ], [] }
874874
| inline_export event_fields /* Sugar */
875875
{ fun c x at ->
876-
let fns, ims, exs = $2 c x at in fns, ims, $1 (EventExport x) c :: exs }
876+
let evs, ims, exs = $2 c x at in evs, ims, $1 (EventExport x) c :: exs }
877877

878878
event_fields_import : /* Sugar */
879879
| event_fields_import_result { $1 }
@@ -887,19 +887,6 @@ event_fields_import_result : /* Sugar */
887887
| LPAR RESULT value_type_list RPAR event_fields_import_result
888888
{ let FuncType (ins, out) = $5 in FuncType (ins, $3 @ out) }
889889

890-
event_types :
891-
| event_result_types { $1 }
892-
| LPAR PARAM value_type_list RPAR event_types
893-
{ let FuncType (ins, out) = $5 in FuncType ($3 @ ins, out) }
894-
| LPAR PARAM bind_var value_type RPAR event_types /* Sugar */
895-
{ let FuncType (ins, out) = $6 in FuncType ($4 :: ins, out) }
896-
897-
event_result_types :
898-
| /* empty */ { FuncType ([], []) }
899-
| LPAR RESULT value_type_list RPAR event_result_types
900-
{ let FuncType (ins, out) = $5 in
901-
FuncType (ins, $3 @ out) }
902-
903890
global :
904891
| LPAR GLOBAL bind_var_opt global_fields RPAR
905892
{ let at = at () in

interpreter/valid/valid.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ let check_import (im : import) (c : context) : context =
575575
check_global_type gt idesc.at;
576576
{c with globals = gt :: c.globals}
577577
| EventImport x ->
578-
{c with events = type_ c x :: c.events}
578+
{c with events = EventType x.it :: c.events}
579579

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

@@ -608,7 +608,7 @@ let check_module (m : module_) =
608608
funcs = c0.funcs @ List.map (fun f -> type_ c0 f.it.ftype) funcs;
609609
tables = c0.tables @ List.map (fun tab -> tab.it.ttype) tables;
610610
memories = c0.memories @ List.map (fun mem -> mem.it.mtype) memories;
611-
events = c0.events @ List.map (fun event -> type_ c0 event) events;
611+
events = c0.events @ List.map (fun event -> EventType event.it) events;
612612
elems = List.map (fun elem -> elem.it.etype) elems;
613613
datas = List.map (fun _data -> ()) datas;
614614
}

0 commit comments

Comments
 (0)