Skip to content

Web compilers should align on strict mode #61513

@srujzs

Description

@srujzs

Today, compiled code in each of the compilers (testing with test_runner) behaves the following way:

dart2wasm: strict mode (@osa1 mentions not intentionally with "use strict", it looks like the imported generated JS code when instantiating the module uses strict mode implicitly)
dart2js: non-strict mode
ddc: strict mode with "use strict" (both AMD and DDC library bundle formats)

This is an issue when it comes to JS interop. For example, if we have a Dart function that's passed to JS and called, the semantics of the function will depend on the compiler. One example is if we use toJSCaptureThis and call it with .call('abc') in JS. With strict mode, the this value in the function will stay a string, but with non-strict mode, it will be converted to an object: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call#thisarg.

Another issue is that functions and files can opt into strict mode using "use strict" headers, but cannot necessarily opt out, so that may influence which direction we go with this. That being said, one can't just put "use strict" in the toJS'd Dart function as it may get tree-shaken/the compiled code may look different, so opting into strict mode may not be easy either.

We could make strict mode configurable, but that might be more effort/testing than it's worth. I'd prioritize consistency first.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-dart2wasmIssues for the dart2wasm compiler.area-web-jsIssues related to JavaScript support for Dart Web, including DDC, dart2js, and JS interop.web-dart2jsweb-dev-compilerweb-js-interopIssues that impact all js interop

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions