Skip to content

Commit

Permalink
[dartdevc] Migrating dart:_js_helper and dart:_interceptors to NNBD.
Browse files Browse the repository at this point in the history
Subsumes the reland of https://dart-review.googlesource.com/c/sdk/+/125994

Change-Id: Ie12c8d473303d0ce7379828caf7523b26733ab75
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/126610
Reviewed-by: Leaf Petersen <leafp@google.com>
Reviewed-by: Sigmund Cherem <sigmund@google.com>
Commit-Queue: Mark Zhou <markzipan@google.com>
  • Loading branch information
Markzipan authored and commit-bot@chromium.org committed Dec 4, 2019
1 parent 2bb6076 commit a4bac1f
Show file tree
Hide file tree
Showing 13 changed files with 107 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class JsPeerInterface {
/// Used for classes where Dart subclasses should be callable from JavaScript
/// matching the JavaScript calling conventions.
final String name;
const JsPeerInterface({this.name});
const JsPeerInterface({required this.name});
}

/// A Dart interface may only be implemented by a native JavaScript object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@
part of dart._js_helper;

class CustomKeyHashMap<K, V> extends CustomHashMap<K, V> {
final _Predicate<Object> _validKey;
final _Predicate<Object?> _validKey;
CustomKeyHashMap(_Equality<K> equals, _Hasher<K> hashCode, this._validKey)
: super(equals, hashCode);

@override
@notNull
bool containsKey(Object key) {
bool containsKey(Object? key) {
if (!_validKey(key)) return false;
return super.containsKey(key);
}

@override
V operator [](Object key) {
V? operator [](Object? key) {
if (!_validKey(key)) return null;
return super[key];
}

@override
V remove(Object key) {
V? remove(Object? key) {
if (!_validKey(key)) return null;
return super.remove(key);
}
Expand Down Expand Up @@ -67,7 +67,7 @@ class CustomHashMap<K, V> extends InternalMap<K, V> {
Iterable<V> get values => _JSMapIterable<V>(this, false);

@notNull
bool containsKey(Object key) {
bool containsKey(Object? key) {
if (key is K) {
var buckets = JS('', '#.get(# & 0x3ffffff)', _keyMap, _hashCode(key));
if (buckets != null) {
Expand All @@ -81,7 +81,7 @@ class CustomHashMap<K, V> extends InternalMap<K, V> {
return false;
}

bool containsValue(Object value) {
bool containsValue(Object? value) {
for (var v in JS('', '#.values()', _map)) {
if (value == v) return true;
}
Expand All @@ -94,7 +94,7 @@ class CustomHashMap<K, V> extends InternalMap<K, V> {
});
}

V operator [](Object key) {
V? operator [](Object? key) {
if (key is K) {
var buckets = JS('', '#.get(# & 0x3ffffff)', _keyMap, _hashCode(key));
if (buckets != null) {
Expand Down Expand Up @@ -150,13 +150,13 @@ class CustomHashMap<K, V> extends InternalMap<K, V> {
JS('', '#.push(#)', buckets, key);
}
V value = ifAbsent();
if (value == null) value = null; // coerce undefined to null.
if (value == null) JS('', '# = null', value); // coerce undefined to null.
JS('', '#.set(#, #)', _map, key, value);
_modifications = (_modifications + 1) & 0x3ffffff;
return value;
}

V remove(Object key) {
V? remove(Object? key) {
if (key is K) {
int hash = JS('!', '# & 0x3ffffff', _hashCode(key));
var keyMap = _keyMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ class IdentityMap<K, V> extends InternalMap<K, V> {
Iterable<K> get keys => _JSMapIterable<K>(this, true);
Iterable<V> get values => _JSMapIterable<V>(this, false);

bool containsKey(Object key) {
bool containsKey(Object? key) {
return JS<bool>('!', '#.has(#)', _map, key);
}

bool containsValue(Object value) {
bool containsValue(Object? value) {
for (var v in JS('', '#.values()', _map)) {
if (v == value) return true;
}
Expand All @@ -54,7 +54,7 @@ class IdentityMap<K, V> extends InternalMap<K, V> {
}
}

V operator [](Object key) {
V? operator [](Object? key) {
V value = JS('', '#.get(#)', _map, key);
return value == null ? null : value; // coerce undefined to null.
}
Expand All @@ -73,13 +73,13 @@ class IdentityMap<K, V> extends InternalMap<K, V> {
return JS('', '#.get(#)', _map, key);
}
V value = ifAbsent();
if (value == null) value = null; // coerce undefined to null.
if (value == null) JS('', '# = null', value);
JS('', '#.set(#, #)', _map, key, value);
_modifications = (_modifications + 1) & 0x3ffffff;
return value;
}

V remove(Object key) {
V? remove(Object? key) {
V value = JS('', '#.get(#)', _map, key);
if (JS<bool>('!', '#.delete(#)', _map, key)) {
_modifications = (_modifications + 1) & 0x3ffffff;
Expand Down Expand Up @@ -128,10 +128,10 @@ class _JSMapIterable<E> extends EfficientLengthIterable<E> {

Iterator<E> get iterator => DartIterator<E>(_jsIterator());

bool contains(Object element) =>
bool contains(Object? element) =>
_isKeys ? _map.containsKey(element) : _map.containsValue(element);

void forEach(void f(E element)) {
void forEach(void Function(E) f) {
for (var entry in this) f(entry);
}
}
10 changes: 4 additions & 6 deletions sdk_nnbd/lib/_internal/js_dev_runtime/private/interceptors.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.5

library dart._interceptors;

import 'dart:collection';
Expand Down Expand Up @@ -122,15 +120,15 @@ class JSNoSuchMethodError extends NativeError implements NoSuchMethodError {
static final _extensionName = RegExp(r"^Symbol\(dartx\.(.+)\)$");
static final _privateName = RegExp(r"^Symbol\((_.+)\)$");

String _fieldName(String message) {
String? _fieldName(String message) {
var match = _nullError.firstMatch(message);
if (match == null) return null;
var name = match[1];
match = _extensionName.firstMatch(name) ?? _privateName.firstMatch(name);
return match != null ? match[1] : name;
}

String _functionCallTarget(String message) {
String? _functionCallTarget(String message) {
var match = _notAFunction.firstMatch(message);
return match != null ? match[1] : null;
}
Expand Down Expand Up @@ -180,7 +178,7 @@ class JSFunction extends Interceptor {
// TODO(jmesserly): remove these once we canonicalize tearoffs.
operator ==(other) {
if (other == null) return false;
var boundObj = JS<Object>('', '#._boundObject', this);
var boundObj = JS<Object?>('', '#._boundObject', this);
if (boundObj == null) return JS<bool>('!', '# === #', this, other);
return JS(
'bool',
Expand All @@ -192,7 +190,7 @@ class JSFunction extends Interceptor {
}

get hashCode {
var boundObj = JS<Object>('', '#._boundObject', this);
var boundObj = JS<Object?>('', '#._boundObject', this);
if (boundObj == null) return identityHashCode(this);

var boundMethod = JS<Object>('!', '#._boundMethod', this);
Expand Down
60 changes: 31 additions & 29 deletions sdk_nnbd/lib/_internal/js_dev_runtime/private/js_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ const _Patch patch = _Patch();
// https://github.com/dart-lang/sdk/issues/28320
class DartIterator<E> implements Iterator<E> {
final _jsIterator;
E _current;
E? _current;

DartIterator(this._jsIterator);

E get current => _current;
E get current => _current as E;

bool moveNext() {
final ret = JS('', '#.next()', _jsIterator);
Expand All @@ -69,17 +69,18 @@ class SyncIterable<E> extends IterableBase<E> {

class Primitives {
@NoInline()
static int? _parseIntError(String source, int? handleError(String source)?) {
static int? _parseIntError(
String source, int? Function(String)? handleError) {
if (handleError == null) throw FormatException(source);
return handleError(source);
}

static int? parseInt(
@nullCheck String source, int? _radix, int? handleError(String source)?) {
static int? parseInt(@nullCheck String source, int? _radix,
int? Function(String)? handleError) {
var re = JS('', r'/^\s*[+-]?((0x[a-f0-9]+)|(\d+)|([a-z0-9]+))\s*$/i');
// TODO(jmesserly): this isn't reified List<String>, but it's safe to use as
// long as we use it locally and don't expose it to user code.
List<String> match = JS('', '#.exec(#)', re, source);
List<String>? match = JS('', '#.exec(#)', re, source);
int digitsIndex = 1;
int hexIndex = 2;
int decimalIndex = 3;
Expand All @@ -89,7 +90,7 @@ class Primitives {
// again.
return _parseIntError(source, handleError);
}
String decimalMatch = match[decimalIndex];
String? decimalMatch = match[decimalIndex];
if (_radix == null) {
if (decimalMatch != null) {
// Cannot fail because we know that the digits are all decimal.
Expand Down Expand Up @@ -147,15 +148,15 @@ class Primitives {

@NoInline()
static double? _parseDoubleError(
String source, double? handleError(String source)?) {
String source, double? Function(String)? handleError) {
if (handleError == null) {
throw FormatException('Invalid double', source);
}
return handleError(source);
}

static double? parseDouble(
@nullCheck String source, double? handleError(String source)?) {
@nullCheck String source, double? Function(String)? handleError) {
// Notice that JS parseFloat accepts garbage at the end of the string.
// Accept only:
// - [+/-]NaN
Expand All @@ -169,7 +170,7 @@ class Primitives {
source)) {
return _parseDoubleError(source, handleError);
}
num result = JS('!', r'parseFloat(#)', source);
double result = JS('!', r'parseFloat(#)', source);
if (result.isNaN) {
var trimmed = source.trim();
if (trimmed == 'NaN' || trimmed == '+NaN' || trimmed == '-NaN') {
Expand All @@ -186,7 +187,7 @@ class Primitives {
static int dateNow() => JS<int>('!', r'Date.now()');

static void initTicker() {
if (timerFrequency != null) return;
if (timerFrequency != 0) return;
// Start with low-resolution. We overwrite the fields if we find better.
timerFrequency = 1000;
timerTicks = dateNow;
Expand All @@ -200,8 +201,9 @@ class Primitives {
timerTicks = () => (1000 * JS<num>('!', '#.now()', performance)).floor();
}

static int timerFrequency;
static int Function() timerTicks;
/// 0 frequency indicates the default uninitialized state.
static int timerFrequency = 0;
static late int Function() timerTicks;

static bool get isD8 {
return JS(
Expand Down Expand Up @@ -266,7 +268,7 @@ class Primitives {
}

@notNull
static String stringFromCharCodes(List<int> charCodes) {
static String stringFromCharCodes(JSArray<int> charCodes) {
for (@nullCheck var i in charCodes) {
if (i < 0) throw argumentErrorValue(i);
if (i > 0xffff) return stringFromCodePoints(charCodes);
Expand Down Expand Up @@ -325,7 +327,7 @@ class Primitives {
// Example: "Wed May 16 2012 21:13:00 GMT+0200 (CEST)".
// We extract this name using a regexp.
var d = lazyAsJsDate(receiver);
List match = JS('JSArray|Null', r'/\((.*)\)/.exec(#.toString())', d);
List? match = JS('JSArray|Null', r'/\((.*)\)/.exec(#.toString())', d);
if (match != null) return match[1];

// Internet Explorer 10+ emits the zone name without parenthesis:
Expand Down Expand Up @@ -360,7 +362,7 @@ class Primitives {
return -JS<int>('!', r'#.getTimezoneOffset()', lazyAsJsDate(receiver));
}

static int valueFromDecomposedDate(
static int? valueFromDecomposedDate(
@nullCheck int years,
@nullCheck int month,
@nullCheck int day,
Expand All @@ -371,7 +373,7 @@ class Primitives {
@nullCheck bool isUtc) {
final int MAX_MILLISECONDS_SINCE_EPOCH = 8640000000000000;
var jsMonth = month - 1;
num value;
int value;
if (isUtc) {
value = JS<int>('!', r'Date.UTC(#, #, #, #, #, #, #)', years, jsMonth,
day, hours, minutes, seconds, milliseconds);
Expand Down Expand Up @@ -504,7 +506,7 @@ Error diagnoseIndexError(indexable, int index) {
* describes the problem.
*/
@NoInline()
Error diagnoseRangeError(int start, int end, int length) {
Error diagnoseRangeError(int? start, int? end, int length) {
if (start == null) {
return ArgumentError.value(start, 'start');
}
Expand Down Expand Up @@ -553,9 +555,9 @@ throwConcurrentModificationError(collection) {
}

class JsNoSuchMethodError extends Error implements NoSuchMethodError {
final String _message;
final String _method;
final String _receiver;
final String? _message;
final String? _method;
final String? _receiver;

JsNoSuchMethodError(this._message, match)
: _method = match == null ? null : JS('String|Null', '#.method', match),
Expand Down Expand Up @@ -597,11 +599,11 @@ fillLiteralMap(keyValuePairs, Map result) {
return result;
}

bool jsHasOwnProperty(var jsObject, String property) {
bool jsHasOwnProperty(jsObject, String property) {
return JS<bool>('!', r'#.hasOwnProperty(#)', jsObject, property);
}

jsPropertyAccess(var jsObject, String property) {
jsPropertyAccess(jsObject, String property) {
return JS('var', r'#[#]', jsObject, property);
}

Expand Down Expand Up @@ -738,12 +740,12 @@ class RuntimeError extends Error {

/// Error thrown by DDC when an `assert()` fails (with or without a message).
class AssertionErrorImpl extends AssertionError {
final String _fileUri;
final int _line;
final int _column;
final String _conditionSource;
final String? _fileUri;
final int? _line;
final int? _column;
final String? _conditionSource;

AssertionErrorImpl(Object message,
AssertionErrorImpl(Object? message,
[this._fileUri, this._line, this._column, this._conditionSource])
: super(message);

Expand Down Expand Up @@ -809,7 +811,7 @@ class PrivateSymbol implements Symbol {

static String getName(Symbol symbol) => (symbol as PrivateSymbol)._name;

static Object getNativeSymbol(Symbol symbol) {
static Object? getNativeSymbol(Symbol symbol) {
if (symbol is PrivateSymbol) return symbol._nativeSymbol;
return null;
}
Expand Down
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.5

/// dart2js "primitives", that is, features that cannot be implemented without
/// access to JavaScript features.
library dart2js._js_primitives;
Expand Down
Loading

0 comments on commit a4bac1f

Please sign in to comment.