Skip to content

Support compact imports in the spec interpreter#18

Open
bvisness wants to merge 1 commit into
mainfrom
cimp-spec-interpreter
Open

Support compact imports in the spec interpreter#18
bvisness wants to merge 1 commit into
mainfrom
cimp-spec-interpreter

Conversation

@bvisness
Copy link
Copy Markdown
Collaborator

@bvisness bvisness commented Mar 25, 2026

Adds binary and text parsing support for the compact import section proposal.

The text parser is a bit crazy because we need to disallow identifiers when using the second compact encoding form. The actual binding of identifiers has been sort of proxied up to the "caller" as a result. I'm open to suggestions for other ways to structure this, but duplicating all the externtype parsing seemed bad, and I'm no good with Menhir.

@bvisness bvisness requested a review from rossberg March 25, 2026 17:42
@bvisness bvisness force-pushed the cimp-spec-interpreter branch from 13139c4 to 1196239 Compare April 27, 2026 22:00
@bvisness bvisness mentioned this pull request Apr 27, 2026
5 tasks
Adds binary and text parsing support for the compact import section
proposal. Also fixes a small section-sizing bug in the binary tests.
@bvisness bvisness force-pushed the cimp-spec-interpreter branch from 1196239 to e848b7d Compare April 27, 2026 22:21
Copy link
Copy Markdown
Member

@rossberg rossberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I have somehow managed to miss this PR entirely!

This looks correct, I only have suggestions for simplifying the code somewhat.

The one thing I wonder is whether the encoder should also make use of the short-hands, similar to how it tries to compactify locals. But we can discuss that separately.

Comment on lines +1057 to +1058
let xt = externtype s in
[Import (module_name, item_name, xt) @@ region s left (pos s)]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can avoid duplicating this case by replacing the outer if with guards on the cases, like

    match peek s with
    | Some 0x7f when item_name = [] -> ...
    | Some 0x7e when item_name = [] -> ...
    | _ -> ...

{ fun c ->
let (items, xt_fn) = $4 in
let (id, anon, _bind, df) = xt_fn c in
(match id with Some x -> error x.at "identifier not allowed" | None -> ());
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(match id with Some x -> error x.at "identifier not allowed" | None -> ());
Option.iter (fun x -> error x.at "identifier not allowed") id;

Comment on lines +1231 to +1232
{ fun c -> ($3, anon_func, bind_func,
fun () -> ExternFuncT (Idx ($4 c).it)) }
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this somewhat complicated deferred application and checking can be avoided by passing in a Boolean attribute b that controls whether an id is allowed. Like:

    { fun c b -> ignore ($3 c b anon_func (bind_if b bind_func));
      fun () -> ExternFuncT (Idx ($4 c).it) }

where

let bind_if b f = if b then f else fun _c x -> error x.at "identifier not allowed"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants