WebAssembly / WebAssembly/spec

Should importing or exporting a function count as declaring it for ref.func?

Open
#2,072 17 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
WebAssembly
Stars
3.5k
Forks
539
Avg merge
2d 1h
Merged PRs (30d)
13

Description

A function must be 'declared' by use outside of the function section in order to be allowed to be used with ref.func.

I recently discovered that just importing a function doesn't count:

(module
  (import "" "" (func $f))
  (func
    ref.func $f
    drop
  )
)

This code fails to validate in V8, SM, and JSC complaining that $f wasn't pre-declared.

This seems odd to me, and I can't think of any reason we shouldn't consider it valid. But maybe I'm missing something?

I then double checked the spec here, and I believe this is the relevant rule in module validation:
Image
The refs field on C is the set of valid targets for ref.func. If I'm reading it right, that means that both exports and imports aren't included in refs.

SM and V8 treat exporting a function as declaring it, while it looks like JSC does not do that. So there's inconsistent handling on exports.

I personally think that exporting/importing a function should count as declaring it. I'm pretty sure imports/exports and declaring was discussed a long time ago, but can't find anything relevant.

cc @tlively @kmiller68 @rossberg

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the module validation rule shown in the issue, especially the refs field, and compare the stated V8, SM, and JSC behavior for imported and exported functions. Done means resolving the semantic question and updating the specification and corresponding reference-interpreter or test-suite behavior consistently.

Written by the indexing model from the issue text.

Assessment

Tech stack
wasm
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.