dart-sdk/tests/compiler/dartdevc_native
Nate Bosch 113ff6c993 Enforce that allowInterop is used in DDC
Fixes #39074

DDC emits Dart code that can usually be called with the same semantics
as JS there is no guarantee that a function passed to JS and then
invoked successfully was wrapped with `allowInterop`. The wrapping is
always required in Dart2JS. To make DDC more strict, add interceptors
that check for the usage of `allowInterop`.

Whenever a JS interop function or setter is passed an argument which is
statically typed as a Function, but not wrapped with `allowInterop` at
the call site, wrap it with `assertInterop` which will check the
argument at call time and fail with a clear error if it was not wrapped.

Whenever a JS interop function is torn off, either at the top level or
from an instance, wrap it with a function that will also inject these
checks at runtime.

There are still holes where we can't catch the mistake:
- An argument which is statically dynamic and a Function at runtime
  won't be caught.
- A Function which is stored in a collection won't be caught.
- A JS interop definition where a getter returns a Function which takes
  a Function as an argument is not checked.
- A dynamic call through to javascript is not checked.

Changes:

- Refactor `_isJsLibrary` and add `isJsMember`, and `isAllowInterop`
  utilities to determine what needs wrapping.
- Update `assertInterop` to give a more clear error when it fails, and
  to ignore non function arguments.
- Add `tearoffInterop` to wrap a function an ensure that any function
  typed arguments are wrapped.
- Inject `assertInterop` around Function arguments passed to JS methods.
- Inject `assertInterop` around Function arguments passed to static or
  instance JS setters.
- Inject a runtime wrapper around static or instance Function tearoffs.
- Add a test covering all flavors of checks that are supported.
- Change the interop expando to an `Expando<dynamic>` in the NNBD SDK to work
  around a stricter type check. https://github.com/dart-lang/sdk/issues/39971

Potential improvements:

If the `tearoffInterop` turns out to be too heavy, we could loosen it so
that we only wrap methods if any of their argument types are statically
declared to be a Function.

Change-Id: Ibc92df5b54e1a041b4102a07b8398b774b6bd1d2
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/128462
Commit-Queue: Nate Bosch <nbosch@google.com>
Reviewed-by: Vijay Menon <vsm@google.com>
Reviewed-by: Nicholas Shahan <nshahan@google.com>
2020-01-02 20:24:26 +00:00
..
debugger Don't compare typeArguments for FunctionType(s) equality. 2019-06-20 06:07:37 +00:00
assertion_failure_message_test.dart [dartdevc] Fix crash when source files contain non-UTF8 characters 2019-11-08 23:10:59 +00:00
const_test.dart
dartdevc_native_dartdevc.status [ddk] Modify _isInterfaceSubtype to consider variance annotations during runtime. 2019-10-23 21:48:17 +00:00
hot_restart_test.dart [dartdevc] emit base types (void/dynamic) eagerly 2019-07-31 16:04:34 +00:00
hot_restart_timer_test.dart Stop timer callbacks after a hot reload 2019-10-23 04:16:06 +00:00
js_interop_test.dart Enforce that allowInterop is used in DDC 2020-01-02 20:24:26 +00:00
libraries_part.dart [dartdevc] encode part file metadata 2019-05-29 14:48:40 +00:00
libraries_test.dart [dartdevc] make ImmutableSet private 2019-05-29 20:04:24 +00:00
nnbd_basic_assignment.dart [dartdevc] Cleanup all temporary nnbd modular tests 2019-12-18 00:43:44 +00:00
nnbd_strong_subtype_test.dart Enable strong/weak strict checking in ddc runtime from the test runner 2019-12-13 19:11:53 +00:00
nnbd_type_normalization_test.dart [dartdevc] Setup NNBD weak mode test configuration 2019-10-23 20:58:47 +00:00
nnbd_weak_subtype_test.dart Enable strong/weak strict checking in ddc runtime from the test runner 2019-12-13 19:11:53 +00:00
no_such_method_errors_test.dart [dartdevc] Fix crash when source files contain non-UTF8 characters 2019-11-08 23:10:59 +00:00
runtime_utils.dart [dartdevc] Reorganize and add some NNBD subtype test cases 2019-10-25 23:51:02 +00:00
runtime_utils_nnbd.dart Enable strong/weak strict checking in ddc runtime from the test runner 2019-12-13 19:11:53 +00:00
subtype_test.dart [dartdevc] Move NNBD specific test helper methods to their own file 2019-10-22 21:55:26 +00:00
utils.dart [dartdevc] Fix crash when source files contain non-UTF8 characters 2019-11-08 23:10:59 +00:00
variance_subtype_test.dart [ddk] Modify _isInterfaceSubtype to consider variance annotations during runtime. 2019-10-23 21:48:17 +00:00
variance_test.dart [ddk] Emit variance modifiers for classes, mixins, mixin applications. 2019-10-19 01:03:41 +00:00