WebAssembly.Memory: length, initial, maximum#1027
Conversation
As suggested in #1024. I'll follow-up with a similar suggestion for WebAssembly.Instance, which needs its own discussion about encapsulation as well as multiple memories. WebAssembly.Memory is simpler: there's only one Memory and it's not encapsulated.
|
One thing that remains outstanding is how to identify that these are post-MVP things. We use 🦄 for future features, maybe we could use another marker for post-MVP?
|
domenic
left a comment
There was a problem hiding this comment.
This needs to initialize those slots, not just state they exist. (Or, maybe grab them from the internal [[Memory]].
I don't think adding a shortcut for .buffer.length is a good idea. Objects with .length properties in JavaScript are uniformly indexed-access objects. The only reasonable name is .bufferLength, but then, why not .buffer.length?
Ah yes! Done.
That was discussed in #1024. I'm OK with any name that's consistent.
|
Ah, I didn't realize the ArrayBuffer would be created lazily by implementations. (The spec creates it at creation time, but that's the spec; it's observably equivalent to create it lazily.) More accurately you'd be creating it, and never throwing it away, since you require If it is indeed anticipated that people will want to know the buffer length but not ever access the buffer itself, then yeah, adding a |
Until we detach due to
I was writing code to massage some Emscripten output and it would be useful to have. People aren't clamoring for it.
|
There was a problem hiding this comment.
No strong opinion on the length name; just trying to figure out what is the most consistent. bufferByteLength is kinda verbose and I think, for wasm, the Memory is the real owner of the memory--the ArrayBuffer just gets to alias it--and so byteLength seems like the right name if length is wrong.
Probably good to get @rossberg-chromium 's opinion too; he's out on PTO for a bit of time, though.
|
Given that conceptual model, |
|
I've renamed to |
domenic
left a comment
There was a problem hiding this comment.
LGTM, thanks for taking my comments and walking me through things.
|
As for names, I would prefer either |
It's symmetrical with the
If we want to be consistent I'd go with one of:
Going with the corresponding JS API seems more consistent IMO, but I'm happy painting the bikeshed any length.
|
|
@jfbastien, re consistency: the right feature to look at would be neither of the above IMO but the "resizable limits" in the import/export types of memories/tables, because that's pretty much exactly what the current/maximum methods would be reflecting on a memory/table object and what is relevant for linking. Fair point about the different units, but that doesn't need to affect naming. The constructors and Not convinced by the symmetry argument regarding |
|
I agree with @rossberg-chromium that including I think a way to access the initial and maximum size of a memory import on a Modle is definitely useful too (possibly more so). Should we consider that? |
Yeah let's do it as part of #1046 in Module. Does this sound good?
Same. We're inconsistent with one thing or another.
Yes, #1046. |
|
Updated, PTAL. |
JS.md
Outdated
| If `this` is not a `WebAssembly.Memory`, a [`TypeError`](https://tc39.github.io/ecma262/#sec-native-error-types-used-in-this-standard-typeerror) | ||
| is thrown. Otherwise return `M.[[BufferObject]]`. | ||
|
|
||
| ### `WebAssembly.Memory.prototype.length` |
There was a problem hiding this comment.
I'm still quite opposed to length for things without indexed properties 0, 1, 2, ..., length - 1, in JavaScript APIs. If you're indifferent between length and byteLength, please pick byteLength :)
There was a problem hiding this comment.
Sounds good. That's what I had but @rossberg-chromium disliked it. I'll put it back!
|
@domenic, we need a name that works consistently for both memories and tables, and unfortunately @jfbastien, how about |
Why do we need this property?
If we indeed need that property then |
|
@jfbastien, because tables share the exact same limits properties, so should expose them in a consistent manner. If |
Can you clarify what limit property you mean? I don't understand what the similarity is. |
|
Tables can grow in the same way as memories, and they also have an initial size, a current size, and an optional maximum size (see e.g. the use of |
Ah OK, so IIUC your point is: presence of initial and maximum plus growth, regardless of unit, is what should guide naming. Unit is irrelevant. Is this accurate? Two questions I think will help resolve this:
|
|
Well, in the API, we already have the same symmetry in the Table vs Memory constructor arguments (both taking an object with |
True. Your argument is then: we've ignored unit before, and we should therefore continue ignoring it. Right? Seems sensible. @domenic thoughts? |
|
Table already has |
|
Oh... if we've already decided to break the rule on .length only being for indexed objects, then I guess consistency argues we should continue to do so... it's a shame that wasn't caught earlier :( |
littledan
left a comment
There was a problem hiding this comment.
What's the current state of this proposal? Should I add it to the JS API specification?
| Return a new `WebAssembly.Memory` instance with | ||
| [[Memory]] set to `m`, | ||
| [[BufferObject]] set to `buffer`, and | ||
| [[Maximum]] set to `undefined` if `maximum` is `None` or `maximum` otherwise. |
There was a problem hiding this comment.
What's the motivation for storing the maximum here, rather than looking it up in the store?
There was a problem hiding this comment.
What do you mean "in the store"?
There was a problem hiding this comment.
He means that the memory instance m (living in the Wasm abstract store) already has this information, so the internal slot is redundant.
| Let `T` be the `this` value. If `T` is not a `WebAssembly.Memory`, a [`TypeError`](https://tc39.github.io/ecma262/#sec-native-error-types-used-in-this-standard-typeerror) | ||
| is thrown. | ||
|
|
||
| Return `T.[[BufferObject]].[[ArrayBufferByteLength]]`. |
There was a problem hiding this comment.
What is the motivation for this feature, when a user can do memory.buffer.byteLength? Is it more optimizable if you never access the ArrayBuffer, or something like that?
There was a problem hiding this comment.
Yeah creating the array buffer is expensive-ish. We can optimize it away, but that's being extra silly IMO.
That would be great, though I'd advise bringing it up at a CG meeting (either in-person or VC). |
|
We deliberately chose to make this post-MVP, so we should not rush it into the MVP spec now all of a sudden. There is a bit of a design space around how to provide this information and fit it nicely with #1046, which would expose equivalent information in other places. I think these features need to be designed together. |
|
If anyone wishes to pursue this feature, please make a new proposal to the CG. |
As suggested in #1024.
I'll follow-up with a similar suggestion for WebAssembly.Instance, which needs its own discussion about encapsulation as well as multiple memories. WebAssembly.Memory is simpler: there's only one Memory and it's not encapsulated.