Skip to content

js.constructorOf[SomeJSClass] to retrieve the JS constructor #1870

@sjrd

Description

@sjrd

Add the following primitive method in the package object js:

def constructorOf[C <: js.Any]: js.Dynamic = sys.error("stub")

The compiler will check at compile-time that C refers to a class (not a trait nor module class, nor any other complex type), similarly to classOf[C]. C can be native or not.

The method returns the JS constructor of the class C. In IR terms, this is JSLoadConstructor(ClassType(C)).

This is particularly important for Scala.js-defined JS classes, as it allows to obtain the JS constructor of a JS class without exporting it (which allows dce) nor instantiating it.

Implementation note: since C will be erased by erasure, PrepJSInterop must rewrite calls to this method to some runtime method runtime.constructorOf(classOf[C]). The backend will then see the ClassConstant(C) as a parameter of runtime.constructorOf; it is of course invalid to call runtime.constructorOf(cls) with a cls which is not a ClassConstant.

Metadata

Metadata

Assignees

Labels

enhancementFeature request (that does not concern language semantics, see "language")

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions