Skip to content

add support for exported classesΒ #102

@mathe42

Description

@mathe42

I currently look at how we can support classes.

Example:

Assemblyscript

export class example {
  constructor(private s: string) {}

  public getString() { return this.s }
}

In JS:

const e = new exports.example('answer: 42')
console.log(e.getString()) // "answer: 42"

This in combination with #64 would allow some cool things!

To do that I would wrap the full class in a JS-Class that calls the correspondig function by runtime and loader.

FinalizationRegistry

I want to do the following to get full garbage collection (in a wrapper function for the classes):

const classPtr = new exports.Foo()
exports.__pin(classPtr)
const foo = Foo.wrap(classPtr)

// We only need one of these for all classes
const registry = new FinalizationRegistry(ptr => {
   exports.__unpin(ptr)
});

registry.register(foo, classPtr);

This would allow full GarbageCollection for Browsers that support FinalizationRegistry (see https://caniuse.com/mdn-javascript_builtins_finalizationregistry). Without FinalizationRegistry I see no way to add this wrapping without memory leaks (any ideas?).

But in that case the developer can allways call __unpin so I think there is nothing to worry about.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions