Skip to content

Commit

Permalink
Merge pull request #157 from Noobware1/master
Browse files Browse the repository at this point in the history
Added List.of, List.from and removeWhere to list
  • Loading branch information
ethanblake4 authored Dec 18, 2023
2 parents 3889842 + e58fced commit 480ce14
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 1 deletion.
79 changes: 78 additions & 1 deletion lib/src/eval/shared/stdlib/core/list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class $List<E> implements List<E>, $Instance {
runtime.registerBridgeFunc('dart:core', 'List.filled', _$List_filled.call);
runtime.registerBridgeFunc(
'dart:core', 'List.generate', _$List_generate.call);
runtime.registerBridgeFunc('dart:core', 'List.of', _$List_of.call);
runtime.registerBridgeFunc('dart:core', 'List.from', _$List_from.call);
}

/// Bridge class declaration for [$List]
Expand Down Expand Up @@ -47,6 +49,36 @@ class $List<E> implements List<E>, $Instance {
returns: BridgeTypeAnnotation(
BridgeTypeRef(CoreTypes.list, [BridgeTypeRef.ref('E')])),
generics: {'E': BridgeGenericParam()})),
'of': BridgeConstructorDef(BridgeFunctionDef(
params: [
BridgeParameter(
'elements',
BridgeTypeAnnotation(BridgeTypeRef(
CoreTypes.iterable, [BridgeTypeRef.ref('E')])),
false),
],
namedParams: [
BridgeParameter('growable',
BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.bool)), true),
],
returns: BridgeTypeAnnotation(
BridgeTypeRef(CoreTypes.list, [BridgeTypeRef.ref('E')])),
generics: {'E': BridgeGenericParam()})),
'from': BridgeConstructorDef(BridgeFunctionDef(
params: [
BridgeParameter(
'elements',
BridgeTypeAnnotation(BridgeTypeRef(
CoreTypes.iterable, [BridgeTypeRef(CoreTypes.dynamic)])),
false),
],
namedParams: [
BridgeParameter('growable',
BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.bool)), true),
],
returns: BridgeTypeAnnotation(
BridgeTypeRef(CoreTypes.list, [BridgeTypeRef.ref('E')])),
generics: {'E': BridgeGenericParam()})),
},
methods: {
'[]': BridgeMethodDef(BridgeFunctionDef(params: [
Expand Down Expand Up @@ -232,6 +264,17 @@ class $List<E> implements List<E>, $Instance {
returns:
BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.voidType))),
isStatic: false),
'removeWhere': BridgeMethodDef(
BridgeFunctionDef(
params: [
BridgeParameter(
'test',
BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.function)),
false),
],
returns:
BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.voidType))),
isStatic: false),
'sublist': BridgeMethodDef(
BridgeFunctionDef(
params: [
Expand Down Expand Up @@ -409,6 +452,8 @@ class $List<E> implements List<E>, $Instance {
return __indexOf;
case 'retainWhere':
return __retainWhere;
case 'removeWhere':
return __removeWhere;
case 'replaceRange':
return __replaceRange;
case 'getRange':
Expand Down Expand Up @@ -478,6 +523,16 @@ class $List<E> implements List<E>, $Instance {
return null;
}

static const $Function __removeWhere = $Function(_removeWhere);

static $Value? _removeWhere(
Runtime runtime, $Value? target, List<$Value?> args) {
final test = args[0] as EvalCallable;
(target!.$value as List)
.removeWhere((e) => test.call(runtime, null, [e])!.$value as bool);
return null;
}

static const $Function __replaceRange = $Function(_replaceRange);

static $Value? _replaceRange(
Expand Down Expand Up @@ -599,7 +654,9 @@ class $List<E> implements List<E>, $Instance {
static const $Function __asMap = $Function(_asMap);

static $Value? _asMap(Runtime runtime, $Value? target, List<$Value?> args) {
return $Map.wrap((target!.$value as List).asMap().map((key, value) => MapEntry($int(key), value)));
return $Map.wrap((target!.$value as List)
.asMap()
.map((key, value) => MapEntry($int(key), value)));
}

static const $Function __cast = $Function(_cast);
Expand Down Expand Up @@ -831,3 +888,23 @@ $Value? _List_generate(Runtime runtime, $Value? target, List<$Value?> args) {
growable: args[2]?.$value ?? true),
);
}

$Function get$List_of(Runtime _) => _$List_of;

const _$List_of = $Function(_List_of);

$Value? _List_of(Runtime runtime, $Value? target, List<$Value?> args) {
return $List.wrap(
List.of(args[0]!.$value, growable: args[1]?.$value ?? true),
);
}

$Function get$List_from(Runtime _) => _$List_from;

const _$List_from = $Function(_List_from);

$Value? _List_from(Runtime runtime, $Value? target, List<$Value?> args) {
return $List.wrap(
List.from(args[0]!.$value, growable: args[1]?.$value ?? true),
);
}
30 changes: 30 additions & 0 deletions test/stdlib_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,36 @@ void main() {
}, prints('[0, 1, 2]\n'));
});

test('List.of', () {
final runtime = compiler.compileWriteAndLoad({
'example': {
'main.dart': '''
void main() {
print(List.of([0, 1, 2], growable: true));
}
''',
}
});
expect(() {
runtime.executeLib('package:example/main.dart', 'main');
}, prints('[0, 1, 2]\n'));
});

test('List.from', () {
final runtime = compiler.compileWriteAndLoad({
'example': {
'main.dart': '''
void main() {
print(List<int>.from([0, 1, 2], growable: true));
}
''',
}
});
expect(() {
runtime.executeLib('package:example/main.dart', 'main');
}, prints('[0, 1, 2]\n'));
});

test('Object.hash', () {
final runtime = compiler.compileWriteAndLoad({
'example': {
Expand Down

0 comments on commit 480ce14

Please sign in to comment.