Discrepancies Between `configureAll` and ES6 Classes
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 25/100
- Issue type
- Feature
- Clarity
- Mostly clear
- Activity status
- Active
- Tech stack
- javascript, wasm
- Domain
- backend-api-design, web-dev
Research direction
Read the configureAll configuration API in proposals/custom-descriptors/Overview.md, then trace how parentidx and constructorconfig are encoded and applied. The issue leaves constructor inheritance as an open design question and notes that changing the encoding would be breaking. Done requires a decided behavior for constructor prototype inheritance and static methods, getters, and setters that is reflected in the API and its tests.
Written by the indexing model from the issue text.
Description
Background
configureAll is designed to declaratively initialize JavaScript prototypes, constructors, methods, and accessors for WebAssembly struct hierarchies so that they behave like JavaScript classes from the perspective of JS consumers.
Currently, the property descriptors (writable, enumerable, configurable) installed by configureAll on the constructors namespace, constructor functions, and prototype objects match ES6 classes. To match ES6 classes, instance method/getter/setter wrappers should additionally be non-constructible (i.e. not callable with new). They should not have a .prototype property, but they should have .length and .name properties, e.g. "m", "get p", and "set p".
Even with all that, several behavioral differences remain between configureAll outputs and ES6 class hierarchies.
Differences from ES6 Classes
Given a two-level class hierarchy (Base and Sub extends Base):
| Aspect | ES6 Classes (class Base / class Sub extends Base) |
configureAll (wasm:js-prototypes) |
Category |
|---|---|---|---|
1. Constructor inheritance (Object.getPrototypeOf(Sub)) |
Object.getPrototypeOf(Sub) === Base (Sub.sMethod === Base.sMethod) |
Object.getPrototypeOf(Sub) === Function.prototype (Sub.sMethod === undefined) |
Open design question |
2. Static method / getter / setter .name and identity |
Fresh function objects with .name set to "sMethod", "get sProp", "set sProp" |
Unwrapped WebAssembly Exported Functions with .name set to the Wasm function index ("2", "3", "4") |
Open design question |
3. Static method this receiver |
this inside static sMethod() is the receiver (Base or Sub) |
this is ignored and not passed to the Wasm function |
Consequence of (2) |
4. Calling constructor without new |
Base() throws TypeError |
Base() invokes the Wasm constructor function directly |
Intentional |
5. Function.prototype.toString() |
"class Base { ... }" / "method(...) { ... }" |
"function Base() { [native code] }" |
Inherent (built-in functions) |
| 6. Instance extensibility & JS subclass fields | new Base() is an extensible ordinary object; class JSChild extends Base { x = 1; } can define own properties on this |
new Base() returns a frozen Wasm GC object (Object.isExtensible(inst) === false); class JSChild extends Base inherits methods, but adding own JS fields to this throws TypeError |
Inherent (Wasm GC storage) |
1. Constructor Inheritance (Object.getPrototypeOf(Sub) === Base)
Current Behavior vs. ES6 Classes
In ES6, class Sub extends Base {} establishes two parallel [[Prototype]] links:
- Instance prototype chain:
Object.getPrototypeOf(Sub.prototype) === Base.prototype, so instances ofSubinherit instance methods and accessors fromBase.prototype. - Constructor prototype chain:
Object.getPrototypeOf(Sub) === Base, so theSubconstructor itself inherits static methods and static accessors fromBase(Sub.sMethod === Base.sMethod).
In configureAll today, parentidx only establishes the first link. Every configured constructor has Function.prototype as its [[Prototype]], so static methods and accessors defined on Base are not accessible on Sub.
Constructor Inheritance in configureAll
In configureAll, each protoconfig configuring a prototype has an optional constructorconfig:
protoconfig ::= vec(constructorconfig) (with size <= 1)
vec(methodconfig)
parentidx
If we want to support constructor inheritance, we have two cases to consider:
- The current and parent prototypes configure constructors
- In this case we can make the parent constructor the prototype of the current constructor.
- The current prototype configures a constructor, but the parent prototype does not.
- A) We could just set the current constructor's prototype to
%Function.prototype%. - B) We could search the parent prototype configuration chain to find the first
constructorconfigand set that constructor to be the prototype of the current constructor (falling back to%Function.prototype%). - C) We could search the parent prototype chain to find the first
.constructorproperty and set that constructor to be the prototype of the current constructor (again falling back to%Function.prototype%). We probably wouldn't want to search all the way up to%Object.prototype%and find its.constructor === Objectin the case of plain{}as the parent constructor, though. We would also have to decide what to do with non-Object.constructorvalues.
- A) We could just set the current constructor's prototype to
Option (A) is the simplest, and might be sufficient. Option (C) would let Wasm objects be configured to have the same prototype as a JS class, then have constructors of Wasm subtypes automatically get that JS class's constructor as their own prototypes. This sounds cool, but I'm not sure if there is any practical use case. I don't think option (B) is very attractive; if we were to do that, then we might as well do option (C), which subsumes it.
If we want to support constructor inheritance, it would also be useful to put the parentidx before the constructorconfig in the encoding of protoconfig. That way the parent constructor would already be known when we create the new constructor for the current prototype. This would be a breaking change to the configureAll binary format.
2. Static Method, Getter, and Setter .name (and this Receiver)
In the current design (Overview.md):
For each
methodconfiginside aconstructorconfig, the current function is installed with the given name and type (method, getter, or setter) on the current constructor. The function is not wrapped or modified in any way, and in particular it does not receive a method receiver as its first parameter.
Because static methods, getters, and setters are installed directly as unwrapped WebAssembly exported Functions:
- Their
.nameproperty is the stringified WebAssembly function index (e.g."2","3","4") rather than"sMethod","get sProp","set sProp", and if the same Wasm function is also exported from the module,Ctor.sMethod === instance.exports.sMethod. - They do not pass the JS
thisvalue (Ctor) as a first argument to the Wasm function.
While not passing this to static methods is usually desirable for Wasm toolchains (where static functions do not expect a receiver argument), the .name discrepancy is visible to reflection and stack traces. If we keep static methods as unwrapped Exported Functions, this .name difference is expected and should be noted as intentional.
My opinion is that the .name property probably doesn't matter much, so keeping the static methods unwrapped is fine.
- Dominant language
- WebAssembly
- Stars
- 13
- Forks
- 3
- Avg merge
- 3d 3h
- Merged PRs (30d)
- 17
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from WebAssembly/custom-descriptors
-
Difficulty 5/5 Over a week Newbie friendliness 15/100
WebAssembly/custom-descriptors#125 · 2 comments ·
Maintainers usually reply within 1 day
-
Difficulty 5/5 Over a week Newbie friendliness 35/100
WebAssembly/custom-descriptors#107 · 1 comment · 1 reaction ·
Maintainers usually reply within 1 day
-
Difficulty 4/5 3-5 days Newbie friendliness 30/100
WebAssembly/custom-descriptors#100 ·
Maintainers usually reply within 1 day
-
Difficulty 5/5 Over a week Newbie friendliness 35/100
WebAssembly/custom-descriptors#98 · 2 reactions ·
Maintainers usually reply within 1 day
-
Difficulty 5/5 Over a week Newbie friendliness 25/100
WebAssembly/custom-descriptors#95 · 1 comment ·
Maintainers usually reply within 1 day
All issues in WebAssembly/custom-descriptors
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
389ds/389-ds-base#8152 ·
Maintainers usually reply within 2 days
-
Bug feat:Syntax
Difficulty 2/5 1-3 hours Newbie friendliness 80/100
ignatov/intellij-erlang#1342 ·
Maintainers usually reply within 1 day
-
intcomma returns exponential notation for large floatsPossibly taken @Omarinho12345 claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
python-humanize/humanize#521 ·
Maintainers usually reply within 1 day
-
area/providers type/feature version/v2
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
anomalyco/opencode#64845 · 1 comment ·
Maintainers usually reply within 1 day
-
LUCJ pass manager raises NoEdgeBetweenNodes when an edge is bad for two two-qubit gatesPossibly taken @hat-im claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Maintainers usually reply within 1 day