Skip to content

Commit

Permalink
Readded $List.view
Browse files Browse the repository at this point in the history
  • Loading branch information
Noobware1 committed Mar 28, 2024
1 parent 9a2b339 commit 5176212
Show file tree
Hide file tree
Showing 3 changed files with 250 additions and 5 deletions.
1 change: 1 addition & 0 deletions lib/src/eval/shared/stdlib/core.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:dart_eval/dart_eval.dart';
import 'package:dart_eval/dart_eval_bridge.dart';
import 'package:dart_eval/src/eval/shared/stdlib/async/stream.dart';
import 'package:dart_eval/src/eval/shared/stdlib/core/base.dart';
Expand Down
7 changes: 3 additions & 4 deletions lib/src/eval/shared/stdlib/core/iterable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -929,12 +929,11 @@ class $Iterable<E> implements Iterable<E>, $Instance {
final $this = target?.$value as Iterable;
final test = args[0] as EvalCallable;
final orElse = args[1] as EvalCallable?;
print(orElse?.call(runtime, null, []));
final $result = $this.firstWhere(
(element) => test.call(runtime, null, [element])!.$value as bool,
orElse: orElse == null
? null
: () => orElse.call(runtime, null, []) ?? $null(),
: () => orElse.call(runtime, null, [])!,
);
return $result;
}
Expand All @@ -952,7 +951,7 @@ class $Iterable<E> implements Iterable<E>, $Instance {
(element) => test.call(runtime, null, [element])!.$value as bool,
orElse: orElse == null
? null
: () => orElse.call(runtime, null, []) ?? $null(),
: () => orElse.call(runtime, null, [])!,
);
return $result;
}
Expand All @@ -970,7 +969,7 @@ class $Iterable<E> implements Iterable<E>, $Instance {
(element) => test.call(runtime, null, [element])!.$value as bool,
orElse: orElse == null
? null
: () => orElse.call(runtime, null, []) ?? $null(),
: () => orElse.call(runtime, null, [])!,
);
return $result;
}
Expand Down
247 changes: 246 additions & 1 deletion lib/src/eval/shared/stdlib/core/list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class $List<E> implements List<E>, $Instance {
isBridge: false);
}

late final $Instance _superclass = $Iterable.wrap($value);
late final $Iterable _superclass = $Iterable.wrap($value);

static const $type = BridgeTypeRef(CoreTypes.list);

Expand Down Expand Up @@ -829,6 +829,10 @@ class $List<E> implements List<E>, $Instance {
/// Wrap an [List] in an [$List]
$List.wrap(this.$value);

/// Create a view of a [List] as a [$List] (supports writeback)
factory $List.view(List<E> value, $Value Function(E value) mapper) =
_$List$view;

@override
$Value? $getProperty(Runtime runtime, String identifier) {
switch (identifier) {
Expand Down Expand Up @@ -1453,3 +1457,244 @@ class $List<E> implements List<E>, $Instance {
@override
Iterable<T> whereType<T>() => $value.whereType<T>();
}

/// Writeback-capable wrapper for [List] with type mapping function
class _$List$view<E> extends $List<E> {
_$List$view(List<E> $value, this.mapper) : super.wrap($value);

final $Value Function(E) mapper;

$Value $map(E value) {
if (value == null) return $null();
return mapper(value);
}

@override
$Value? $getProperty(Runtime runtime, String identifier) {
switch (identifier) {
case '[]':
return __indexGet;
case '[]=':
return __indexSet;
case 'add':
return __add;
case 'addAll':
return __addAll;
case 'first':
return $map(_superclass.first);
case 'contains':
return __listContains;
case 'where':
return __where;
case 'last':
return $map(_superclass.last);
case 'reversed':
return $Iterable.wrap($value.reversed.map($map));
case 'iterator':
return $Iterator.wrap($value.map($map).iterator);
case 'insert':
return __insert;
case 'insertAll':
return __insertAll;
case 'remove':
return __remove;
case 'asMap':
return __asMap;
case 'lastIndexOf':
return __lastIndexOf;
case 'indexOf':
return __indexOf;
case 'retainWhere':
return __retainWhere;
case 'removeWhere':
return __removeWhere;
case 'replaceRange':
return __replaceRange;
case 'getRange':
return __getRange;
case 'sort':
return __sort;
case 'removeAt':
return __removeAt;
case 'sublist':
return __sublist;
case 'takeWhile':
return __takeWhile;
}
return super.$getProperty(runtime, identifier);
}

static const $Function __add = $Function(_add);

static $Value? _add(Runtime runtime, $Value? target, List<$Value?> args) {
final value = args[0]!;
(target! as _$List$view).add(value.$value);
return null;
}

static const $Function __addAll = $Function(_addAll);

static $Value? _addAll(Runtime runtime, $Value? target, List<$Value?> args) {
(target! as _$List$view).addAll(args[0]!.$reified);
return null;
}

static const $Function __insertAll = $Function(_insertAll);

static $Value? _insertAll(
Runtime runtime, $Value? target, List<$Value?> args) {
(target! as _$List$view).insertAll(args[0]!.$value, args[1]!.$reified);
return null;
}

static const $Function __insert = $Function(_insert);

static $Value? _insert(Runtime runtime, $Value? target, List<$Value?> args) {
(target! as _$List$view).insert(args[0]!.$value, args[1]!.$reified);
return null;
}

static const $Function __remove = $Function(_remove);

static $Value? _remove(Runtime runtime, $Value? target, List<$Value?> args) {
return $bool((target! as _$List$view).remove(args[0]!.$reified));
}

static const $Function __removeAt = $Function(_removeAt);

static $Value? _removeAt(
Runtime runtime, $Value? target, List<$Value?> args) {
return (target! as _$List$view).removeAt(args[0]!.$value);
}

static const $Function __lastIndexOf = $Function(_lastIndexOf);

static $Value? _lastIndexOf(
Runtime runtime, $Value? target, List<$Value?> args) {
return $int((target! as _$List$view).lastIndexOf(args[0]!.$reified));
}

static const $Function __indexOf = $Function(_indexOf);

static $Value? _indexOf(Runtime runtime, $Value? target, List<$Value?> args) {
return $int((target! as _$List$view).indexOf(args[0]!.$reified));
}

static const $Function __indexGet = $Function(_indexGet);

static $Value? _indexGet(
Runtime runtime, $Value? target, List<$Value?> args) {
final idx = args[0]!;
final view = (target! as _$List$view);
return view.$map(view.$value[idx.$value]);
}

static const $Function __indexSet = $Function(_indexSet);

static $Value? _indexSet(
Runtime runtime, $Value? target, List<$Value?> args) {
final idx = args[0]!;
final value = args[1]!;
(target! as _$List$view).$value[idx.$value] = value.$value;
return value;
}

static const $Function __sublist = $Function(_sublist);

static $Value? _sublist(Runtime runtime, $Value? target, List<$Value?> args) {
return $List.wrap((target! as _$List$view)
.$value
.sublist(args[0]!.$value, args[1]?.$value));
}

static const $Function __listContains = $Function(_listContains);

static $Value? _listContains(
Runtime runtime, $Value? target, List<$Value?> args) {
return $bool((target! as _$List$view).contains(args[0]!.$reified));
}

static const $Function __where = $Function(_where);

static $Value? _where(Runtime runtime, $Value? target, List<$Value?> args) {
final test = args[0] as EvalCallable;
return $Iterable.wrap((target! as _$List$view)
.$value
.where((e) => test.call(runtime, null, [e])!.$value as bool)
.map((e) => (target as _$List$view).$map(e)));
}

static const $Function __asMap = $Function(_asMap);

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

static const $Function __retainWhere = $Function(_retainWhere);

static $Value? _retainWhere(
Runtime runtime, $Value? target, List<$Value?> args) {
final test = args[0] as EvalCallable;
final view = (target! as _$List$view);

view.retainWhere(
(e) => test.call(runtime, null, [view.$map(e)])!.$value as bool);
return null;
}

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

static $Value? _removeWhere(
Runtime runtime, $Value? target, List<$Value?> args) {
final test = args[0] as EvalCallable;
final view = (target! as _$List$view);

view.removeWhere(
(e) => test.call(runtime, null, [view.$map(e)])!.$value as bool);
return null;
}

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

static $Value? _replaceRange(
Runtime runtime, $Value? target, List<$Value?> args) {
final view = (target! as _$List$view);
view.$value
.replaceRange(args[0]!.$value, args[1]!.$value, args[2]!.$reified);
return null;
}

static const $Function __getRange = $Function(_getRange);

static $Value? _getRange(
Runtime runtime, $Value? target, List<$Value?> args) {
final view = (target! as _$List$view);
return $Iterable.wrap(
view.$value.getRange(args[0]!.$value, args[1]!.$value).map(view.$map));
}

static const $Function __sort = $Function(_sort);

static $Value? _sort(Runtime runtime, $Value? target, List<$Value?> args) {
final compare = args[0] as EvalCallable;
final view = (target! as _$List$view);

view.$value.sort((a, b) =>
compare.call(runtime, null, [view.$map(a), view.$map(b)])!.$value);
return null;
}

static const $Function __takeWhile = $Function(_takeWhile);

static $Value? _takeWhile(
Runtime runtime, $Value? target, List<$Value?> args) {
final test = args[0] as EvalCallable;
final view = (target! as _$List$view);

return $Iterable.wrap(view.$value
.takeWhile((e) => test.call(runtime, null, [view.$map(e)])!.$value));
}
}

0 comments on commit 5176212

Please sign in to comment.