-
Notifications
You must be signed in to change notification settings - Fork 259
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WASI proposal for dynamic typing (WASI-dyntype) #552
Comments
I think this sort of feature belongs in Core WebAssembly, not WASI, since what you're describing deeply influences how the code is compiled and executed (including what types and instructions are available in wasm function bodies). The wasm-gc proposal just reached Stage 4 (coincidentally, 10 minutes ago 🎉 ) and I think forms the basis of what you want (engine-provided GC). I expect wasm-gc is more statically-typed and low-level than you're looking for, but having seen a great deal of discussion of this over the years, I believe baking in high-level dynamic language semantics is a path fraught with peril as there are so many mutually-conflicting requirements. Instead, I think an interesting question to ask is: given now-standard wasm-gc, are there any other GC primitives that would enable dynamic languages to be compiled more-efficiently on top of wasm-gc than is possible today? To pursue this question, I'd suggest scanning through open issues in the wasm-gc and design repos for existing discussions on this topic and filing new issues for remaining questions/ideas. |
@lukewagner Thanks for the reply and suggestions, and it's really exciting to hear that wasm-gc reaches Stage 4 🎉
These APIs did influence how the code is compiled, but seems it's difficult to enter Core WebAssembly since supporting high level semantics such as dynamic typing is not a goal of WebAssembly. We are actually developing a compiler which can compile TypeScript to WebAssembly, we use wasm-gc to represent the statically typed code (e.g. number, boolean, class), but it's not possible to represent the dynamic types such as So that's why we are here: we abstract the APIs to access dynamic objects managed from external environment, it works as an escape hatch for dynamic typing, so we can still use wasm-gc to represent the statically typed objects in TypeScript and use these APIs to access dynamic objects, this avoids compiling a whole language runtime into WebAssembly. Previously most of the approaches to support dynamic language on WebAssembly is |
I think I agree with Luke (assuming I understand his suggestion) that the best way to achieve this kind of thing would be look at extensions to wasm-gc in order to support the kind of dynamic behavior your need. Where did you read that "supporting high level semantics such as dynamic typing is not a goal of WebAssembly"? My understanding is that the goal of WebAssembly is to support all types of languages as efficiently as possible, but that we just started out targeting certain types of languages with the initial version. |
Well, we think it would be better if this can be some wasm-gc extension, but according to some previous discussion, currently wasm-gc opcodes are "as low level as possible", so bring such dynamic typing into wasm-gc seems not compatible to the principle.
My previous description may be not very accurate, my understanding is that WebAssembly will not provide opcode to support dynamic type directly, currently dynamically typed languages already works well through compiling their runtime into WebAssembly, but this will introduce some footprint overhead. These APIs separate the processing of dynamic typing, so we can compile the static part to wasm-gc directly, without another garbage collector inside wasm module. |
Ah I see. My interpretation of that would be that in order for an addition of wasm-gc in support of dynamic languages to gain traction we would need to show that it would be much more efficient that building the same dynamic features on top of wasm-gc primitives. Presumably it is technically possible to build a dynamic object model top of the wasm-gc object model? (e.g. represent the dynamic fields in some kind of map data structure?). |
I believe that's why @xujuntwt95329 proposed a new WasmGC instruction to allow dynamic field access as in the link in the previous discussion. It's hard to do without a new instruction, really (an array can't mix different types, and a struct has fixed access only, and there are no maps). @xujuntwt95329 I sympathize with your position, since basically you went to the WasmGC people and got an unenthusiastic response, and so you came here but you got the same thing basically. With that said, I do agree with the concerns mentioned both here and there, even though I am very much in favor of good support for dynamic languages in wasm. My own position is still what I wrote at the end of one my comments there,
That is, data in linear memory will easily allow dynamic field access using the normal tricks, and reference access using a WasmGC array also allows dynamic access. You will have overhead (separate storage for data and references, and casts from the WasmGC array) but it might still be fast enough for dynamic objects (especially since your compiler doesn't implement all objects dynamically). I'd recommend experimenting with that first, as the other options appear to be more radical and would require some changing of minds. (This will need weak reference support, as mentioned before, but that is at least already planned, and can be polyfilled today on the Web using JS.) |
i think you can probably end up with a pretty efficient JS implementation using wasm GC by using the hidden classes and inline caching technique that V8 uses -- that doesn't usually use an arbitrary hashmap to represent objects. |
@kripken Thanks for your understanding 😀. The opcode we previously proposed to wasm-gc is much more low-level than these APIs, I believe that if we propose these dynamic features to wasm-gc, there will be a more direct reject. I personally understand all the concerns from both side, because there are already solutions to support dynamic typed language (as suggested by @sbc100 and @programmerjake, implement the object management based on WasmGC, or current solution: compile whole VM into WebAssembly), seems there isn't a strong necessity to introduce a new concept at the standard level. However, we are trying to, at least provide the opportunity to, avoid a runtime inside wasm module because there are many resource constraint devices, they may have very limited RAM and flash, remove the runtime from wasm module may allow these devices to install more applications. So that's why we want to use these APIs to separate the processing of dynamic types, it will have these benefits:
This gives us the flexibility to utilize different implementations on different environment, while don't need to introduce too many new concepts into WebAssembly. @kripken I like your idea that |
This issue is to introduce the idea of proposing
WASI-dyntype
APIs.Introduction
The type system of WebAssembly is entirely static in nature, precluding the direct compilation of dynamic programming languages into WebAssembly. Currently, the primary method of supporting dynamic languages involves compiling the language runtime into WebAssembly. This approach involves running virtual machines of individual languages within the WebAssembly environment (VM inside VM), which results in notable performance overhead and significant memory consumption.
Modern dynamic languages tend to incorporate more type annotations (such as TypeScript or type hints in Python). If these type annotations could be leveraged for static compilation, they would contribute to enhancing application performance. However, the aforementioned VM-inside-VM approach falls short of achieving this goal.
The objective of this proposal is to furnish a standardized set of public APIs designed for handling dynamically-typed objects. These APIs would facilitate the management and access of dynamic objects hosted by the external environment, thereby affording opportunities for statically compiling other objects with sufficient type information.
Strategy
Goal
Non-Goal
API walk-through
object creation and property accessing
This can be generated as:
runtime type checking
This can be generated as:
subtyping
This can be generated as:
exception
This can be generated as:
re-use existing built-in objects and methdos
This can be generated as:
Detailed design discussion
Given this code as an example:
This can be generated as such WebAssembly opcodes:
Accessing dynamic object
$a
will be slow because it involves invoking host APIs, but accessing the$num
and$a_inst
would be much faster than theVM-inside-VM
approach.The text was updated successfully, but these errors were encountered: