Skip to content

Commit

Permalink
[nnbd] port dart:js_util to nnbd
Browse files Browse the repository at this point in the history
Change-Id: I5ec29e77dd747aaab28cf3b1b20fd47b21e2d129
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/132181
Reviewed-by: Sigmund Cherem <sigmund@google.com>
Commit-Queue: Vijay Menon <vsm@google.com>
  • Loading branch information
vsmenon authored and commit-bot@chromium.org committed Jan 17, 2020
1 parent c063f5d commit 9447686
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
1 change: 1 addition & 0 deletions pkg/dev_compiler/tool/check_nnbd_sdk.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ void main(List<String> argv) {
var emptyProgramUri = baseUri.resolve('empty_program.dart');
File.fromUri(emptyProgramUri).writeAsStringSync('''
import 'dart:js';
import 'dart:js_util';
main() {}
''');
Expand Down
29 changes: 15 additions & 14 deletions sdk_nnbd/lib/js_util/js_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// 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.

// @dart = 2.6

/// Utility methods to efficiently manipulate typed JSInterop objects in cases
/// where the name to call is not known at runtime. You should only use these
/// methods when the same effect cannot be achieved with @JS annotations.
Expand All @@ -28,17 +26,17 @@ import 'dart:_js_helper' show convertDartClosureToJS;
/// converted. Maps and Iterables are copied to a new JavaScript object.
/// Primitives and other transferable values are directly converted to their
/// JavaScript type, and all other objects are proxied.
jsify(object) {
Object jsify(Object object) {
if ((object is! Map) && (object is! Iterable)) {
throw ArgumentError("object must be a Map or Iterable");
}
return _convertDataTree(object);
}

_convertDataTree(data) {
Object _convertDataTree(Object data) {
var _convertedObjects = HashMap.identity();

_convert(o) {
Object? _convert(Object? o) {
if (_convertedObjects.containsKey(o)) {
return _convertedObjects[o];
}
Expand All @@ -59,23 +57,26 @@ _convertDataTree(data) {
}
}

return _convert(data);
return _convert(data)!;
}

newObject() => JS('=Object', '{}');
dynamic newObject() => JS('=Object', '{}');

bool hasProperty(o, name) => JS('bool', '# in #', name, o);
bool hasProperty(Object o, Object name) => JS('bool', '# in #', name, o);

getProperty(o, name) => JS('Object|Null', '#[#]', o, name);
dynamic getProperty(Object o, Object name) =>
JS('Object|Null', '#[#]', o, name);

setProperty(o, name, value) => JS('', '#[#]=#', o, name, value);
dynamic setProperty(Object o, Object name, Object? value) =>
JS('', '#[#]=#', o, name, value);

callMethod(o, String method, List args) =>
dynamic callMethod(Object o, String method, List<Object?> args) =>
JS('Object|Null', '#[#].apply(#, #)', o, method, o, args);

bool instanceof(o, Function type) => JS('bool', '# instanceof #', o, type);
bool instanceof(Object? o, Function type) =>
JS('bool', '# instanceof #', o, type);

callConstructor(Function constr, List arguments) {
dynamic callConstructor(Function constr, List<Object?> arguments) {
if (arguments == null) {
return JS('Object', 'new #()', constr);
}
Expand Down Expand Up @@ -144,7 +145,7 @@ callConstructor(Function constr, List arguments) {
///
/// final three = await threeFuture; // == 3
/// ```
Future<T> promiseToFuture<T>(jsPromise) {
Future<T> promiseToFuture<T>(Object jsPromise) {
final completer = Completer<T>();

final success = convertDartClosureToJS((r) => completer.complete(r), 1);
Expand Down

0 comments on commit 9447686

Please sign in to comment.