spec icon indicating copy to clipboard operation
spec copied to clipboard

[js-api] Exposing Wasm-specific internal structure

Open ShinWonho opened this issue 4 months ago • 2 comments

Functions defined in embedding section provide abstraction of Wasm's mechanics to embedders. However, I found a case in JS API specification that breaks this abstraction: exposing Wasm-specific internal structure. I'm curious whether these are inevitable or intentional.


Some embedding functions abstract over the internal structure of the Wasm store. Fig.1 provides a representative example. It takes a store S and an address a, and returns S.funcs[a].type. When the embedder tries to retrieve the function type, the unnecessary details about the Wasm store can be abstracted away using the embedding function.

Image

Fig.1: Embedding function func_type

However, Fig.2 is the algorithm that breaks this abstraction layer. It accesses the store directly to get a function instance (line 2).

Not only that, this algorithm checks whether the function instance is a host function (line 3) and accesses its fields (line 3.1). This also undermines the abstraction, because, as I understand it, the internal structure of Wasm-specific structures are intended to be exposed only for error, values(val, externval), or types(externtype, memtype, globaltype, valtype).

Image

Fig.2: JS API algorithm for the name of the WebAssembly function

The abstraction over the internal structure of the store and the function instance should be enforced by accessing them only through embedding functions; however, this abstraction is currently not maintained. These exposures might result from accessing Wasm's functionality beyond what the embedding functions provide. I'd like to ask whether this exposure is intentional, and whether it would be possible to add new embedding functions to eliminate it.

ShinWonho avatar Aug 18 '25 04:08 ShinWonho

Adding new embedding functions to fix this violation of the embedder API abstraction sounds good to me.

tlively avatar Aug 21 '25 00:08 tlively

I agree that the embedder interface should have lookup functions for each sort of address. Should be straightforward to add, would anybody volunteer? :)

rossberg avatar Sep 02 '25 09:09 rossberg