-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[pkg:js] Add tests for allowInterop idempotency
Closes #56897 Adds a few missing tests to make sure we handle already converted functions, Dart functions that were wrapped by a previous call, and JS functions correctly in allowInterop and allowInteropCaptureThis. Change-Id: Iaa6f34652717787fad8141ad941c47e944e70a5e Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/390400 Auto-Submit: Srujan Gaddam <srujzs@google.com> Commit-Queue: Srujan Gaddam <srujzs@google.com> Reviewed-by: Nicholas Shahan <nshahan@google.com>
- Loading branch information
Showing
2 changed files
with
59 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright (c) 2024, 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. | ||
|
||
// Test that `allowInterop` and `allowInteropCaptureThis` should be idempotent | ||
// when given wrapped functions, JS functions, or Dart functions that were | ||
// wrapped by a previous call. | ||
|
||
import 'package:expect/expect.dart'; | ||
import 'package:js/js.dart'; | ||
|
||
const isDart2JS = const bool.fromEnvironment('dart.tool.dart2js'); | ||
|
||
@JS() | ||
external void eval(String code); | ||
|
||
@JS() | ||
external Function get jsFunction; | ||
|
||
void main() { | ||
eval(''' | ||
self.jsFunction = function() {}; | ||
'''); | ||
final dartFunction = (_) {}; | ||
final wrappedFunction = allowInterop(dartFunction); | ||
final wrappedFunctionCaptureThis = allowInteropCaptureThis(dartFunction); | ||
// Should add a wrapper that's unique to the function. | ||
Expect.notEquals(dartFunction, wrappedFunction); | ||
Expect.notEquals(dartFunction, wrappedFunctionCaptureThis); | ||
Expect.notEquals(wrappedFunction, wrappedFunctionCaptureThis); | ||
// Should return the same value if the value is already a wrapped function. | ||
Expect.equals(wrappedFunction, allowInterop(wrappedFunction)); | ||
Expect.equals( | ||
wrappedFunctionCaptureThis, allowInterop(wrappedFunctionCaptureThis)); | ||
// Passing a non-Dart function to `allowInteropCaptureThis` throws in dart2js, | ||
// so for now, capture the existing semantics. We may want to update DDC to | ||
// match this. | ||
if (isDart2JS) { | ||
Expect.throws(() => allowInteropCaptureThis(wrappedFunction)); | ||
Expect.throws(() => allowInteropCaptureThis(wrappedFunctionCaptureThis)); | ||
} else { | ||
Expect.equals(wrappedFunction, allowInteropCaptureThis(wrappedFunction)); | ||
Expect.equals(wrappedFunctionCaptureThis, | ||
allowInteropCaptureThis(wrappedFunctionCaptureThis)); | ||
} | ||
// Should directly return JS functions without doing anything. | ||
Expect.equals(jsFunction, allowInterop(jsFunction)); | ||
if (isDart2JS) { | ||
Expect.throws(() => allowInteropCaptureThis(jsFunction)); | ||
} else { | ||
Expect.equals(jsFunction, allowInteropCaptureThis(jsFunction)); | ||
} | ||
// If `allowInterop`/`allowInteropCaptureThis` is called again on the same | ||
// Dart value, should return the previously wrapped function. | ||
Expect.equals(wrappedFunction, allowInterop(dartFunction)); | ||
Expect.equals( | ||
wrappedFunctionCaptureThis, allowInteropCaptureThis(dartFunction)); | ||
} |
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