sdk icon indicating copy to clipboard operation
sdk copied to clipboard

[js_util] Add constructor function for Arrays

Open rakudrama opened this issue 3 years ago • 0 comments

Usually the corelib List constructors can be used to create a JavaScript Array for js-interop purposes.

Occasionally this is not sufficient. Sometimes it is required to create a JavaScript Array that is free of the additional properties that are placed on the Array instances to fully implement Dart Lists. The exact details may change, but the reified type is stored on a property with a symbol name, and a fixed$length property is used to mark the Array as a fixed-length List, and another property is used to mark the list as unmodifiable.

One way around this is to create the Array via js-interop, e.g. js_util.callConstructor<List>(...). The problem with using js-interop to create the Array is that the code looks like any other code to create an object via a JavaScript constructor and dart2js no longer understands that the array is in fact a JavaScript Array. Therefore operations on the array end up going via interceptor convention calls.

js_util should provide methods for creating Array instances. The methods would be written in a manner that ensures that dart2js understands the result to be a JavaScript Array, and possibly more (e.g. the initial length).

The implementation might look something like

external List newArray([int length]);

A call would be compiled to either [] or new Array(length) depending on whether the argument was provided.

rakudrama avatar Sep 20 '22 06:09 rakudrama