-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[dart:js_util] Add checks for functions in js_util
Closes #44145 Introduces assertions in js_util for `setProperty`, `callMethod`, and `callConstructor` for when the provided values are Functions but not wrapped with `allowInterop`. Adds a test to check for assertions in each of those methods. Change-Id: I7681cf81170d7975abc7e7dd4172f2c5673f203b Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/176740 Reviewed-by: Sigmund Cherem <sigmund@google.com> Commit-Queue: Srujan Gaddam <srujzs@google.com>
- Loading branch information
Showing
5 changed files
with
145 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
// dart2jsOptions=--enable-asserts | ||
|
||
// Tests that `js_util` methods correctly check that function arguments allow | ||
// interop. | ||
|
||
@JS() | ||
library assert_function_interop_test; | ||
|
||
import 'package:js/js.dart'; | ||
import 'package:js/js_util.dart' as js_util; | ||
import 'package:expect/expect.dart'; | ||
|
||
@JS() | ||
external void eval(String code); | ||
|
||
@JS() | ||
class JSClass { | ||
external JSClass(); | ||
} | ||
|
||
@JS() | ||
external get JSClassWithFuncArgs; | ||
|
||
@JS() | ||
external void Function() get jsFunction; | ||
|
||
void dartFunction() {} | ||
|
||
@pragma('dart2js:noInline') | ||
@pragma('dart2js:assumeDynamic') | ||
confuse(x) => x; | ||
|
||
void main() { | ||
eval(r""" | ||
function JSClass() { | ||
this.functionProperty = function() {}; | ||
this.methodWithFuncArgs = function(f1, f2) { | ||
f1(); | ||
if (arguments.length == 2) f2(); | ||
}; | ||
} | ||
function JSClassWithFuncArgs(f1, f2) { | ||
f1(); | ||
if (arguments.length == 2) f2(); | ||
} | ||
function jsFunction() {} | ||
"""); | ||
|
||
var jsClass = JSClass(); | ||
dynamic d = confuse(dartFunction); | ||
var interopFunction = allowInterop(dartFunction); | ||
var interopDynamic = confuse(allowInterop(dartFunction)); | ||
|
||
// Functions that aren't wrapped with `allowInterop` should throw. | ||
Expect.throws( | ||
() => js_util.setProperty(jsClass, 'functionProperty', dartFunction)); | ||
Expect.throws(() => js_util.setProperty(jsClass, 'functionProperty', d)); | ||
// Correctly wrapped functions should not throw. | ||
js_util.setProperty(jsClass, 'functionProperty', interopFunction); | ||
js_util.setProperty(jsClass, 'functionProperty', interopDynamic); | ||
|
||
// Using a JS function should not throw. | ||
js_util.setProperty(jsClass, 'functionProperty', jsFunction); | ||
|
||
Expect.throws( | ||
() => js_util.callMethod(jsClass, 'methodWithFuncArgs', [dartFunction])); | ||
Expect.throws(() => js_util.callMethod(jsClass, 'methodWithFuncArgs', [d])); | ||
js_util.callMethod(jsClass, 'methodWithFuncArgs', [interopFunction]); | ||
js_util.callMethod(jsClass, 'methodWithFuncArgs', [interopDynamic]); | ||
// Check to see that all arguments are checked. | ||
Expect.throws(() => js_util.callMethod( | ||
jsClass, 'methodWithFuncArgs', [interopFunction, dartFunction])); | ||
js_util.callMethod( | ||
jsClass, 'methodWithFuncArgs', [interopFunction, interopFunction]); | ||
|
||
js_util.callMethod(jsClass, 'methodWithFuncArgs', [jsFunction]); | ||
|
||
Expect.throws( | ||
() => js_util.callConstructor(JSClassWithFuncArgs, [dartFunction])); | ||
Expect.throws(() => js_util.callConstructor(JSClassWithFuncArgs, [d])); | ||
js_util.callConstructor(JSClassWithFuncArgs, [interopFunction]); | ||
js_util.callConstructor(JSClassWithFuncArgs, [interopDynamic]); | ||
Expect.throws(() => js_util | ||
.callConstructor(JSClassWithFuncArgs, [interopFunction, dartFunction])); | ||
js_util | ||
.callConstructor(JSClassWithFuncArgs, [interopFunction, interopFunction]); | ||
|
||
js_util.callConstructor(JSClassWithFuncArgs, [jsFunction]); | ||
} |