DDC / dart2js inconsistencies around undefined and null #36116
Labels
area-web
Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop.
web-dart2js
web-dev-compiler
There are inconsistencies between how DDC and dart2js use JavaScript's
undefined
value. The compilers are mostly self-consistent, but this occasionally causes problems, often at the js-interop boundary.Is there something we can do to make DDC and dart2js more consistent?
A summary of the differences:
DDC tries to always use JavaScript
null
for Dartnull
.This enables
undefined
to be used as a token that signifies an omitted positional optional argument.This has the nice property that this is how ES6 optional arguments are defined.
The cost is that places that naturally return
undefined
need to be coerced tonull
, e.g. values from ES6 Maps need to be coerced viaOne of the most expensive coercions is that Arrays need to be filled, i.e.
Occasionally an
undefined
value leaks out (e.g. #36052). Also see example at end.dart2js uses both JavaScript
undefined
andnull
for Dartnull
.The calling convention uses the method name (aka a selector, foo$1 vs foo$2) to encode the optional argument, so there is no sentinel value that can accidentally make a passed argument be defaulted.
Arrays do not need to be filled, and are a major source of
undefined
values.dart2js is an optimizing compiler and has many code-size reducing transformations enabled by allowing
undefined
. E.g.These optimizations, taken as a whole, are responsible for a substantial code size reduction, but they make it hard to be sure what exactly is being passed to JavaScript.
Problems in pushing to convergence:
The following prints
x: null
on VM and dart2js andx: hello
on DDC.(The pragmas stop dart2js constant-folding the whole program to
print("x: null")
.)The text was updated successfully, but these errors were encountered: