Skip to content

Commit e848b7d

Browse files
committed
Implement compact imports in the spec interpreter
Adds binary and text parsing support for the compact import section proposal. Also fixes a small section-sizing bug in the binary tests.
1 parent 9a54f93 commit e848b7d

2 files changed

Lines changed: 87 additions & 29 deletions

File tree

interpreter/binary/decode.ml

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,14 +1028,37 @@ let type_section s =
10281028

10291029
(* Import section *)
10301030

1031-
let import s =
1031+
let imports s =
1032+
let left = pos s in
10321033
let module_name = name s in
10331034
let item_name = name s in
1034-
let xt = externtype s in
1035-
Import (module_name, item_name, xt)
1035+
if item_name = [] then
1036+
match peek s with
1037+
| Some 0x7f ->
1038+
skip 1 s;
1039+
vec (fun s ->
1040+
let l = pos s in
1041+
let nm = name s in
1042+
let xt = externtype s in
1043+
Import (module_name, nm, xt) @@ region s l (pos s)
1044+
) s
1045+
| Some 0x7e ->
1046+
skip 1 s;
1047+
let xt = externtype s in
1048+
vec (fun s ->
1049+
let l = pos s in
1050+
let nm = name s in
1051+
Import (module_name, nm, xt) @@ region s l (pos s)
1052+
) s
1053+
| _ ->
1054+
let xt = externtype s in
1055+
[Import (module_name, item_name, xt) @@ region s left (pos s)]
1056+
else
1057+
let xt = externtype s in
1058+
[Import (module_name, item_name, xt) @@ region s left (pos s)]
10361059

10371060
let import_section s =
1038-
section Custom.Import (vec (at import)) [] s
1061+
section Custom.Import (fun s -> List.concat (vec imports s)) [] s
10391062

10401063

10411064
(* Function section *)

interpreter/text/parser.mly

Lines changed: 60 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,32 +1227,67 @@ table_fields :
12271227
/* Imports & Exports */
12281228

12291229
externtype :
1230-
| LPAR FUNC bindidx_opt typeuse RPAR
1231-
{ fun c -> ignore ($3 c anon_func bind_func);
1232-
fun () -> ExternFuncT (Idx ($4 c).it) }
1233-
| LPAR TAG bindidx_opt typeuse RPAR
1234-
{ fun c -> ignore ($3 c anon_tag bind_tag);
1235-
fun () -> ExternTagT (TagT (Idx ($4 c).it)) }
1236-
| LPAR TAG bindidx_opt functype RPAR /* Sugar */
1237-
{ fun c -> ignore ($3 c anon_tag bind_tag);
1238-
fun () -> ExternTagT (TagT (Idx (inline_functype c ($4 c) $loc($4)).it)) }
1239-
| LPAR GLOBAL bindidx_opt globaltype RPAR
1240-
{ fun c -> ignore ($3 c anon_global bind_global);
1241-
fun () -> ExternGlobalT ($4 c) }
1242-
| LPAR MEMORY bindidx_opt memorytype RPAR
1243-
{ fun c -> ignore ($3 c anon_memory bind_memory);
1244-
fun () -> ExternMemoryT ($4 c) }
1245-
| LPAR TABLE bindidx_opt tabletype RPAR
1246-
{ fun c -> ignore ($3 c anon_table bind_table);
1247-
fun () -> ExternTableT ($4 c) }
1248-
| LPAR FUNC bindidx_opt functype RPAR /* Sugar */
1249-
{ fun c -> ignore ($3 c anon_func bind_func);
1250-
fun () -> ExternFuncT (Idx (inline_functype c ($4 c) $loc($4)).it) }
1230+
| LPAR FUNC option(bindidx) typeuse RPAR
1231+
{ fun c -> ($3, anon_func, bind_func,
1232+
fun () -> ExternFuncT (Idx ($4 c).it)) }
1233+
| LPAR TAG option(bindidx) typeuse RPAR
1234+
{ fun c -> ($3, anon_tag, bind_tag,
1235+
fun () -> ExternTagT (TagT (Idx ($4 c).it))) }
1236+
| LPAR TAG option(bindidx) functype RPAR /* Sugar */
1237+
{ fun c -> ($3, anon_tag, bind_tag,
1238+
fun () -> ExternTagT (TagT (Idx (inline_functype c ($4 c) $loc($4)).it))) }
1239+
| LPAR GLOBAL option(bindidx) globaltype RPAR
1240+
{ fun c -> ($3, anon_global, bind_global,
1241+
fun () -> ExternGlobalT ($4 c)) }
1242+
| LPAR MEMORY option(bindidx) memorytype RPAR
1243+
{ fun c -> ($3, anon_memory, bind_memory,
1244+
fun () -> ExternMemoryT ($4 c)) }
1245+
| LPAR TABLE option(bindidx) tabletype RPAR
1246+
{ fun c -> ($3, anon_table, bind_table,
1247+
fun () -> ExternTableT ($4 c)) }
1248+
| LPAR FUNC option(bindidx) functype RPAR /* Sugar */
1249+
{ fun c -> ($3, anon_func, bind_func,
1250+
fun () -> ExternFuncT (Idx (inline_functype c ($4 c) $loc($4)).it)) }
1251+
1252+
compact_item1 :
1253+
| LPAR ITEM name externtype RPAR
1254+
{ fun c -> let (id, anon, bind, df) = $4 c in
1255+
ignore (match id with None -> anon c $loc($4) | Some x -> bind c x);
1256+
fun () -> ($3, df ()) }
1257+
1258+
compact_item1_list :
1259+
| compact_item1
1260+
{ fun c -> let f = $1 c in
1261+
fun () -> [f ()] }
1262+
| compact_item1 compact_item1_list
1263+
{ fun c -> let f = $1 c in let fs = $2 c in
1264+
fun () -> f () :: fs () }
1265+
1266+
compact_item2_list :
1267+
| LPAR ITEM name RPAR compact_item2_list
1268+
{ let (items, xt_fn) = $5 in ($3 :: items, xt_fn) }
1269+
| externtype
1270+
{ ([], $1) }
12511271

12521272
import :
12531273
| LPAR IMPORT name name externtype RPAR
1254-
{ fun c -> let df = $5 c in
1255-
fun () -> Import ($3, $4, df ()) @@ $sloc }
1274+
{ fun c -> let (id, anon, bind, df) = $5 c in
1275+
ignore (match id with None -> anon c $loc($5) | Some x -> bind c x);
1276+
fun () -> [Import ($3, $4, df ()) @@ $sloc] }
1277+
| LPAR IMPORT name compact_item1_list RPAR
1278+
{ fun c -> let items = $4 c in
1279+
fun () ->
1280+
List.map (fun (item_name, xt) -> Import ($3, item_name, xt) @@ $sloc)
1281+
(items ()) }
1282+
| LPAR IMPORT name compact_item2_list RPAR
1283+
{ fun c ->
1284+
let (items, xt_fn) = $4 in
1285+
let (id, anon, _bind, df) = xt_fn c in
1286+
(match id with Some x -> error x.at "identifier not allowed" | None -> ());
1287+
List.iter (fun _ -> ignore (anon c $sloc)) items;
1288+
fun () ->
1289+
let xt = df () in
1290+
List.map (fun item_name -> Import ($3, item_name, xt) @@ $sloc) items }
12561291

12571292
inline_import :
12581293
| LPAR IMPORT name name RPAR { $3, $4 }
@@ -1377,8 +1412,8 @@ module_fields1 :
13771412
| import module_fields
13781413
{ fun c -> let imf = $1 c in let mff = $2 c in
13791414
fun () -> let mf = mff () in
1380-
fun () -> let im = imf () in let m = mf () in
1381-
{m with imports = im :: m.imports} }
1415+
fun () -> let ims = imf () in let m = mf () in
1416+
{m with imports = ims @ m.imports} }
13821417
| export module_fields
13831418
{ fun c -> let mff = $2 c in
13841419
fun () -> let mf = mff () in

0 commit comments

Comments
 (0)