Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 72493a7

Browse files
Anna GringauzeCommit Queue
Anna Gringauze
authored and
Commit Queue
committed
Make records inherit from core.Object
Towards: dart-lang/sdk#49717 Change-Id: Ie32704a21db51dd713a2f03ce78532ed67fe1792 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/270821 Commit-Queue: Anna Gringauze <[email protected]> Reviewed-by: Mark Zhou <[email protected]>
1 parent ff8caf9 commit 72493a7

File tree

4 files changed

+66
-62
lines changed

4 files changed

+66
-62
lines changed

pkg/dev_compiler/lib/src/kernel/target.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ class DevCompilerTarget extends Target {
117117
(uri.path == 'core' ||
118118
uri.path == 'typed_data' ||
119119
uri.path == '_interceptors' ||
120-
uri.path == '_native_typed_data');
120+
uri.path == '_native_typed_data' ||
121+
uri.path == '_runtime');
121122

122123
/// Returns [true] if [uri] represents a test script has been whitelisted to
123124
/// import private platform libraries.

sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/classes.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ bool isJsInterop(obj) {
341341
if (JS('!', '#[#] != null', obj, _extensionType)) return false;
342342

343343
// Exclude record types.
344-
if (JS('!', '#[#] != null', obj, _shape)) return false;
344+
if (_jsInstanceOf(obj, _RecordImpl)) return false;
345345
return !_jsInstanceOf(obj, Object);
346346
}
347347

sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/rtti.dart

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,12 @@ getFunctionType(obj) {
8484
}
8585

8686
// Compute and cache reified record type.
87-
RecordType getRecordType(obj) {
87+
RecordType getRecordType(_RecordImpl obj) {
8888
var type = JS<RecordType?>('', '#[#]', obj, _runtimeType);
8989
if (type == null) {
90-
var shape = JS<Shape>('', '#[#]', obj, _shape);
91-
var named = JS<List<String>?>('', '#.named', shape);
92-
var positionals = JS<int>('', '#.positionals', shape);
90+
var shape = obj.shape;
91+
var named = shape.named;
92+
var positionals = shape.positionals;
9393
var types = [];
9494
var count = 0;
9595
while (count < positionals) {
@@ -151,11 +151,7 @@ getReifiedType(obj) {
151151
switch (JS<String>('!', 'typeof #', obj)) {
152152
case "object":
153153
if (obj == null) return JS('', '#', Null);
154-
// TODO(annagrin): Replace the check with
155-
//` _jsInstanceOf(obj, _RecordImpl)`
156-
// after _RecordImpl inherits from Object.
157-
var shape = JS('', '#[#]', obj, _shape);
158-
if (shape != null) {
154+
if (_jsInstanceOf(obj, _RecordImpl)) {
159155
return getRecordType(obj);
160156
}
161157
if (_jsInstanceOf(obj, Object)) {

sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/types.dart

Lines changed: 58 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2264,56 +2264,61 @@ class Shape {
22642264
}
22652265
}
22662266

2267-
final _shape = JS('', 'Symbol("shape")');
2268-
2269-
final Object _RecordImpl = JS(
2270-
'!',
2271-
'''
2272-
class _RecordImpl {
2273-
constructor(values) {
2274-
this.values = values;
2275-
}
2276-
2277-
[#](other) {
2278-
if (!(other instanceof #)) return false;
2279-
if (this.# !== other.#) return false;
2280-
if (this.values.length !== other.values.length) return false;
2281-
for (let i = 0; i < this.values.length; i++)
2282-
if (!#(this.values[i], other.values[i]))
2267+
/// Internal base class for all concrete records.
2268+
class _RecordImpl implements Record {
2269+
Shape shape;
2270+
List values;
2271+
2272+
int? _hashCode;
2273+
String? _printed;
2274+
2275+
_RecordImpl(this.shape, this.values);
2276+
2277+
@override
2278+
bool operator ==(Object? other) {
2279+
if (!(other is _RecordImpl)) return false;
2280+
if (shape != other.shape) return false;
2281+
if (values.length != other.values.length) {
2282+
return false;
2283+
}
2284+
for (var i = 0; i < values.length; i++) {
2285+
if (values[i] != other.values[i]) {
22832286
return false;
2287+
}
2288+
}
22842289
return true;
22852290
}
22862291

2287-
get [#]() {
2288-
return #([this.#].concat(this.values));
2292+
@override
2293+
int get hashCode {
2294+
if (_hashCode == null) {
2295+
_hashCode = Object.hashAll([shape, ...values]);
2296+
}
2297+
return _hashCode!;
22892298
}
22902299

2291-
[#]() {
2292-
let shape = this.#;
2293-
let s = '(';
2294-
for (let i = 0; i < this.values.length; i++) {
2295-
if (i >= shape.positionals) {
2296-
s += shape.named[i - shape.positionals] + ': '
2300+
@override
2301+
String toString() {
2302+
if (_printed == null) {
2303+
var buffer = StringBuffer();
2304+
var posCount = shape.positionals;
2305+
var count = values.length;
2306+
2307+
buffer.write('(');
2308+
for (var i = 0; i < count; i++) {
2309+
if (i >= posCount) {
2310+
buffer.write('${shape.named![i - posCount]}');
2311+
buffer.write(': ');
2312+
}
2313+
buffer.write('${values[i]}');
2314+
if (i < count - 1) buffer.write(', ');
22972315
}
2298-
s += #(this.values[i]);
2299-
if (i < this.values.length - 1) s += ', ';
2316+
buffer.write(')');
2317+
_printed = buffer.toString();
23002318
}
2301-
s += ')';
2302-
return s;
2319+
return _printed!;
23032320
}
23042321
}
2305-
''',
2306-
extensionSymbol('_equals'),
2307-
_RecordImpl,
2308-
_shape,
2309-
_shape,
2310-
equals,
2311-
extensionSymbol('hashCode'),
2312-
Object.hashAll,
2313-
_shape,
2314-
extensionSymbol('toString'),
2315-
_shape,
2316-
_toString);
23172322

23182323
/// Cache for Record shapes. These are keyed by a distinct shape recipe,
23192324
/// which consists of an integer followed by space-separated named labels.
@@ -2339,20 +2344,21 @@ Object registerRecord(@notNull String shapeRecipe, @notNull int positionals,
23392344
return cached;
23402345
}
23412346

2342-
Object recordClass = JS(
2347+
Object recordClass = JS('!', 'class _Record extends # {}', _RecordImpl);
2348+
// Add a 'new' function to be used instead of a constructor
2349+
// (which is disallowed on dart objects).
2350+
Object newRecord = JS(
23432351
'!',
23442352
'''
2345-
class _Record extends # {
2346-
constructor(values) {
2347-
super(values);
2353+
#.new = function (shape, values) {
2354+
#.__proto__.new.call(this, shape, values);
23482355
}
2349-
}
23502356
''',
2351-
_RecordImpl);
2357+
recordClass,
2358+
recordClass);
23522359

2353-
var shape = registerShape(shapeRecipe, positionals, named);
2360+
JS('!', '#.prototype = #.prototype', newRecord, recordClass);
23542361
var recordPrototype = JS('', '#.prototype', recordClass);
2355-
JS('', '#[#] = #', recordPrototype, _shape, shape);
23562362

23572363
_recordGet(@notNull int index) =>
23582364
JS('!', 'function recordGet() {return this.values[#];}', index);
@@ -2373,8 +2379,8 @@ Object registerRecord(@notNull String shapeRecipe, @notNull int positionals,
23732379
}
23742380
}
23752381

2376-
JS('', '#.set(#, #)', _records, shapeRecipe, recordClass);
2377-
return recordClass;
2382+
JS('', '#.set(#, #)', _records, shapeRecipe, newRecord);
2383+
return newRecord;
23782384
}
23792385

23802386
/// Creates a record type.
@@ -2388,8 +2394,9 @@ RecordType recordType(@notNull Shape shape, @notNull List types) =>
23882394
/// named element in sorted order.
23892395
Object recordLiteral(@notNull String shapeRecipe, @notNull int positionals,
23902396
List<String>? named, @notNull List values) {
2397+
var shape = registerShape(shapeRecipe, positionals, named);
23912398
var record = registerRecord(shapeRecipe, positionals, named);
2392-
return JS('!', 'new #(#)', record, values);
2399+
return JS('!', 'new #(#, #)', record, shape, values);
23932400
}
23942401

23952402
/// Creates a shape and binds it to [types].

0 commit comments

Comments
 (0)