Discrepancies Between `configureAll` and ES6 Classes

Open
#132 0 comments 0 reactions 0 assignees View on GitHub

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

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:

  1. Instance prototype chain: Object.getPrototypeOf(Sub.prototype) === Base.prototype, so instances of Sub inherit instance methods and accessors from Base.prototype.
  2. Constructor prototype chain: Object.getPrototypeOf(Sub) === Base, so the Sub constructor itself inherits static methods and static accessors from Base (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:

  1. The current and parent prototypes configure constructors
    • In this case we can make the parent constructor the prototype of the current constructor.
  2. 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 constructorconfig and 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 .constructor property 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 === Object in the case of plain {} as the parent constructor, though. We would also have to decide what to do with non-Object .constructor values.

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 methodconfig inside a constructorconfig, 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:

  1. Their .name property 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.
  2. They do not pass the JS this value (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

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from WebAssembly/custom-descriptors

All issues in WebAssembly/custom-descriptors

Similar issues

More Backend & API Design issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.