-
Notifications
You must be signed in to change notification settings - Fork 74
Un-restricting globals initialization #189
Comments
The pattern of 'build & freeze' is actually very common and not restricted to global variables. |
@fgmccabe what would that entail? |
Off the top of my head, some way to 'seal' a variable. One of the attractions, for me, of the maligned let instruction, was that it let me express this pattern pretty well. But that does not cover the 'build an object' use case. |
I've been wondering about this pattern. Related question for @jakobkummerow in order to start answering @tlively's question: are |
@RossTate : Currently no: in each RTT, we store its list of super RTTs, excluding itself. For casting, we special-case that case, so That said, I consider this an implementation detail that I don't want to make any long-term promises about. We implemented what seemed convenient, we may change it in the future (and have, in fact, already changed it in the past), other engines might implement it differently. (I also don't see why it would affect variable/object sealing.) On the larger topic: aside from immutability, another strategic benefit of allowing more different instructions in global initializers (which in turn allows more work to be done there, as opposed to the |
Thanks for the info! The reason I asked is that it's illustrative of the kinds of things language runtimes will have to implement within WebAssembly, which in turn guides how one would design support the "build and freeze" pattern. Many runtimes use immutable cyclic datastructures behind the scenes, and I've been wondering how we would be able to make WebAssembly support that pattern. The "build and freeze" pattern is important for separate compilation as well as for ensuring the memory safety of some efficient data structures (such as flattened interface tables) in multi-threaded settings. But I don't know the plans for supporting these features or use cases, so it'd be useful to hear what @rossberg has planned, rather than conjecture. |
Closely related is @sbc100's recent extended const proposal. Further extending the class of const expressions with some of the allocators in this proposal makes total sense to me. As for RTTs, I don't see any issue for now -- as is, That said, recursive initialisation is a well-known pain point. In the MVP, you'll have to use mutable vars/fields for that. Post-MVP we likely want the ability to turn fields readonly, e.g. via subsumption. That probably won't cover all use cases either, but there is no general solution really. |
To be clear, it won't cover any of the use cases I mentioned above.
Published works can already address these use cases (except for cyclic structures, which requires an extension), as they're used by major languages that have been compiled to typed assembly languages. |
We now have many more instructions marked constant, so I will close this issue. We can open new issues for specific new potential constant instructions based on the current state of the proposal. |
This proposal currently marks
i31.new
,rtt.canon
andrtt.sub
as constant instructions, which means they can be used to initialize globals. Notably, struct/array allocating instructions are not categorized as constant (whereasref.func
from the function-references proposal is). I got the following feedback from the J2CL team:Is there a particular reason to be so restrictive about which instructions are allowed in global initializers? Can we loosen the restrictions somewhat?
The text was updated successfully, but these errors were encountered: