Skip to content

Commit

Permalink
[dartdevc] Finishing migration of dart:_interceptors and dart:_runtime.
Browse files Browse the repository at this point in the history
Change-Id: I511e0bc701dea8fbccb135802ef79b3f678a3e52
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/126088
Commit-Queue: Mark Zhou <markzipan@google.com>
Reviewed-by: Nicholas Shahan <nshahan@google.com>
  • Loading branch information
Markzipan authored and commit-bot@chromium.org committed Dec 6, 2019
1 parent 84cc410 commit afdc8ca
Show file tree
Hide file tree
Showing 16 changed files with 580 additions and 620 deletions.
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

/// This library defines the operations that define and manipulate Dart
/// classes. Included in this are:
/// - Generics
Expand Down Expand Up @@ -68,7 +66,7 @@ void _copyMember(to, from, name) {
to,
name,
name);
JS('', '#.set = #.set', desc, getOwnPropertyDescriptor(obj, name));
JS<Object>('!', '#.set = #.set', desc, getOwnPropertyDescriptor(obj, name));
}
} else if (setter != null) {
if (getter == null) {
Expand All @@ -80,7 +78,7 @@ void _copyMember(to, from, name) {
to,
name,
name);
JS('', '#.get = #.get', desc, getOwnPropertyDescriptor(obj, name));
JS<Object>('!', '#.get = #.get', desc, getOwnPropertyDescriptor(obj, name));
}
}
defineProperty(to, name, desc);
Expand All @@ -107,7 +105,7 @@ final mixinOn = JS('', 'Symbol("mixinOn")');
@JSExportName('implements')
final implements_ = JS('', 'Symbol("implements")');

List Function() getImplements(clazz) => JS(
List? Function() getImplements(clazz) => JS(
'',
'Object.hasOwnProperty.call(#, #) ? #[#] : null',
clazz,
Expand Down Expand Up @@ -178,11 +176,11 @@ generic(typeConstructor, setBaseClass) => JS('', '''(() => {

getGenericClass(type) => safeGetOwnProperty(type, _originalDeclaration);

List getGenericArgs(type) =>
JS('List', '#', safeGetOwnProperty(type, _typeArguments));
List? getGenericArgs(type) =>
JS<List?>('', '#', safeGetOwnProperty(type, _typeArguments));

List getGenericArgVariances(type) =>
JS('List', '#', safeGetOwnProperty(type, _variances));
List? getGenericArgVariances(type) =>
JS<List?>('', '#', safeGetOwnProperty(type, _variances));

void setGenericArgVariances(f, variances) =>
JS('', '#[#] = #', f, _variances, variances);
Expand Down Expand Up @@ -340,7 +338,7 @@ void _installProperties(jsProto, dartType, installedParent) {
}
// If the extension methods of the parent have been installed on the parent
// of [jsProto], the methods will be available via prototype inheritance.
var dartSupertype = JS('', '#.__proto__', dartType);
var dartSupertype = JS<Object>('!', '#.__proto__', dartType);
if (JS('!', '# !== #', dartSupertype, installedParent)) {
_installProperties(jsProto, dartSupertype, installedParent);
}
Expand All @@ -352,7 +350,7 @@ void _installProperties(jsProto, dartType, installedParent) {
void _installPropertiesForObject(jsProto) {
// core.Object members need to be copied from the non-symbol name to the
// symbol name.
var coreObjProto = JS('', '#.prototype', Object);
var coreObjProto = JS<Object>('!', '#.prototype', Object);
var names = getOwnPropertyNames(coreObjProto);
for (int i = 0, n = JS('!', '#.length', names); i < n; ++i) {
var name = JS<String>('!', '#[#]', names, i);
Expand All @@ -371,7 +369,7 @@ void _installPropertiesForGlobalObject(jsProto) {

final _extensionMap = JS('', 'new Map()');

_applyExtension(jsType, dartExtType) {
void _applyExtension(jsType, dartExtType) {
// TODO(vsm): Not all registered js types are real.
if (jsType == null) return;
var jsProto = JS('', '#.prototype', jsType);
Expand Down Expand Up @@ -446,13 +444,13 @@ defineExtensionMethods(type, Iterable memberNames) {
}

/// Like [defineExtensionMethods], but for getter/setter pairs.
defineExtensionAccessors(type, Iterable memberNames) {
var proto = JS('', '#.prototype', type);
void defineExtensionAccessors(type, Iterable memberNames) {
var proto = JS<Object>('!', '#.prototype', type);
for (var name in memberNames) {
// Find the member. It should always exist (or we have a compiler bug).
var member;
var p = proto;
for (;; p = JS('', '#.__proto__', p)) {
for (;; p = JS<Object>('!', '#.__proto__', p)) {
member = getOwnPropertyDescriptor(p, name);
if (member != null) break;
}
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

part of dart._runtime;

// We need to set these properties while the sdk is only partially initialized
Expand All @@ -21,12 +19,12 @@ throwUnimplementedError(String message) {

// TODO(nshahan) Cleanup embeded strings and extract file location at runtime
// from the stacktrace.
assertFailed(String message,
[String fileUri, int line, int column, String conditionSource]) {
assertFailed(String? message,
[String? fileUri, int? line, int? column, String? conditionSource]) {
throw AssertionErrorImpl(message, fileUri, line, column, conditionSource);
}

throwCyclicInitializationError([Object field]) {
throwCyclicInitializationError([String? field]) {
throw CyclicInitializationError(field);
}

Expand Down Expand Up @@ -80,7 +78,7 @@ final Object _jsError = JS('', 'Symbol("_jsError")');
///
/// If the throw originated in JavaScript, then there is not a corresponding
/// Dart value, so we just return the error object.
Object getThrown(Object error) {
Object? getThrown(Object? error) {
if (error != null) {
// Get the Dart thrown value, if any.
var value = JS('', '#[#]', error, _thrownValue);
Expand Down Expand Up @@ -112,7 +110,7 @@ StackTrace stackTrace(Object error) {
}

// If we've already created the Dart stack trace object, return it.
StackTrace trace = JS('', '#[#]', error, _stackTrace);
StackTrace? trace = JS('', '#[#]', error, _stackTrace);
if (trace != null) return trace;

// Otherwise create the Dart stack trace (by parsing the JS stack), and
Expand Down Expand Up @@ -189,7 +187,7 @@ final Object RethrownDartError = JS(
/// Implements `throw` of [exception], allowing for throw in an expression
/// context, and capturing the current stack trace.
@JSExportName('throw')
void throw_(Object exception) {
void throw_(Object? exception) {
/// Wrap the object so we capture a new stack trace, and so it will print
/// nicely from JS, as if it were a normal JS error.
JS('', 'throw new #(#)', DartError, exception);
Expand All @@ -212,7 +210,7 @@ void throw_(Object exception) {
/// If the stack trace is null, this will preserve the original stack trace
/// on the exception, if available, otherwise it will capture the current stack
/// trace.
Object createErrorWithStack(Object exception, StackTrace trace) {
Object? createErrorWithStack(Object exception, StackTrace? trace) {
if (trace == null) {
var error = JS('', '#[#]', exception, _jsError);
return error != null ? error : JS('', 'new #(#)', DartError, exception);
Expand All @@ -236,18 +234,18 @@ void stackPrint(Object error) {
}

class _StackTrace implements StackTrace {
final Object _jsError;
final Object _jsObjectMissingTrace;
String _trace;
final Object? _jsError;
final Object? _jsObjectMissingTrace;
String? _trace;

_StackTrace(this._jsError) : _jsObjectMissingTrace = null;

_StackTrace.missing(Object caughtObj)
_StackTrace.missing(Object? caughtObj)
: _jsObjectMissingTrace = caughtObj != null ? caughtObj : 'null',
_jsError = JS('', 'Error()');

String toString() {
if (_trace != null) return _trace;
if (_trace != null) return _trace!;

var e = _jsError;
String trace = '';
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

/// This library defines runtime operations on objects used by the code
/// generator.
part of dart._runtime;
Expand All @@ -19,9 +17,9 @@ class InvocationImpl extends Invocation {
final bool isSetter;
final String failureMessage;

InvocationImpl(memberName, List<Object> positionalArguments,
InvocationImpl(memberName, List<Object?> positionalArguments,
{namedArguments,
List typeArguments,
List typeArguments = [],
this.isMethod = false,
this.isGetter = false,
this.isSetter = false,
Expand All @@ -30,9 +28,7 @@ class InvocationImpl extends Invocation {
isSetter ? _setterSymbol(memberName) : _dartSymbol(memberName),
positionalArguments = List.unmodifiable(positionalArguments),
namedArguments = _namedArgsToSymbols(namedArguments),
typeArguments = typeArguments == null
? const []
: List.unmodifiable(typeArguments.map(wrapType));
typeArguments = List.unmodifiable(typeArguments.map(wrapType));

static Map<Symbol, dynamic> _namedArgsToSymbols(namedArgs) {
if (namedArgs == null) return const {};
Expand Down Expand Up @@ -164,7 +160,7 @@ dput(obj, field, value, [@undefined mirrors]) {
/// [actuals] and [namedActuals].
///
/// Returns `null` if all checks pass.
String _argumentErrors(FunctionType type, List actuals, namedActuals) {
String? _argumentErrors(FunctionType type, List actuals, namedActuals) {
// Check for too few required arguments.
int actualsCount = JS('!', '#.length', actuals);
var required = type.args;
Expand All @@ -183,13 +179,13 @@ String _argumentErrors(FunctionType type, List actuals, namedActuals) {
}

// Check if we have invalid named arguments.
Iterable names;
Iterable? names;
var named = type.named;
var requiredNamed = type.requiredNamed;
if (namedActuals != null) {
names = getOwnPropertyNames(namedActuals);
for (var name in names) {
if (!JS('!', '(#.hasOwnProperty(#) || #.hasOwnProperty(#))', named, name,
for (var name in names!) {
if (!JS<bool>('!', '(#.hasOwnProperty(#) || #.hasOwnProperty(#))', named, name,
requiredNamed, name)) {
return "Dynamic call with unexpected named argument '$name'.";
}
Expand All @@ -198,7 +194,7 @@ String _argumentErrors(FunctionType type, List actuals, namedActuals) {
// Verify that all required named parameters are provided an argument.
Iterable requiredNames = getOwnPropertyNames(requiredNamed);
for (var name in requiredNames) {
if (!JS('!', '#.hasOwnProperty(#)', namedActuals, name)) {
if (!JS<bool>('!', '#.hasOwnProperty(#)', namedActuals, name)) {
return "Dynamic call with missing required named argument '$name'.";
}
}
Expand Down Expand Up @@ -436,9 +432,9 @@ cast(obj, type, @notNull bool isImplicit) {
return castError(obj, type, isImplicit);
}

bool test(bool obj) {
bool test(bool? obj) {
if (obj == null) _throwBooleanConversionError();
return obj;
return obj!;
}

bool dtest(obj) {
Expand Down Expand Up @@ -476,7 +472,7 @@ _notNull(x) {
}

/// The global constant map table.
final constantMaps = JS('', 'new Map()');
final constantMaps = JS<Object>('!', 'new Map()');

// TODO(leafp): This table gets quite large in apps.
// Keeping the paths is probably expensive. It would probably
Expand All @@ -486,7 +482,7 @@ Object _lookupNonTerminal(Object map, Object key) {
var result = JS('', '#.get(#)', map, key);
if (result != null) return result;
JS('', '#.set(#, # = new Map())', map, key, result);
return result;
return result!;
}

Map<K, V> constMap<K, V>(JSArray elements) {
Expand All @@ -496,14 +492,14 @@ Map<K, V> constMap<K, V>(JSArray elements) {
map = _lookupNonTerminal(map, JS('', '#[#]', elements, i));
}
map = _lookupNonTerminal(map, K);
var result = JS('', '#.get(#)', map, V);
Map<K, V>? result = JS('', '#.get(#)', map, V);
if (result != null) return result;
result = ImmutableMap<K, V>.from(elements);
JS('', '#.set(#, #)', map, V, result);
return result;
}

final constantSets = JS('', 'new Map()');
final constantSets = JS<Object>('!', 'new Map()');
var _immutableSetConstructor;

// We cannot invoke private class constructors directly in Dart.
Expand All @@ -519,7 +515,7 @@ Set<E> constSet<E>(JSArray<E> elements) {
for (var i = 0; i < count; i++) {
map = _lookupNonTerminal(map, JS('', '#[#]', elements, i));
}
var result = JS('', '#.get(#)', map, E);
Set<E>? result = JS('', '#.get(#)', map, E);
if (result != null) return result;
result = _createImmutableSet<E>(elements);
JS('', '#.set(#, #)', map, E, result);
Expand Down Expand Up @@ -570,7 +566,7 @@ multiKeyPutIfAbsent(map, keys, valueFn) => JS('', '''(() => {
/// and value of the field. The final map is
/// indexed by runtime type, and contains the canonical
/// version of the object.
final constants = JS('', 'new Map()');
final constants = JS('!', 'new Map()');

///
/// Canonicalize a constant object.
Expand Down Expand Up @@ -689,7 +685,7 @@ runtimeType(obj) {
return obj == null ? Null : JS('', '#[dartx.runtimeType]', obj);
}

final identityHashCode_ = JS('', 'Symbol("_identityHashCode")');
final identityHashCode_ = JS<Object>('!', 'Symbol("_identityHashCode")');

/// Adapts a Dart `get iterator` into a JS `[Symbol.iterator]`.
// TODO(jmesserly): instead of an adaptor, we could compile Dart iterators
Expand Down
Loading

0 comments on commit afdc8ca

Please sign in to comment.