diff --git a/lib/src/eval/shared/stdlib/core/iterable.dart b/lib/src/eval/shared/stdlib/core/iterable.dart index e478853..c3a0405 100644 --- a/lib/src/eval/shared/stdlib/core/iterable.dart +++ b/lib/src/eval/shared/stdlib/core/iterable.dart @@ -17,6 +17,14 @@ class $Iterable implements Iterable, $Instance { BridgeParameter('toElement', BridgeTypeAnnotation(BridgeTypeRef.type(RuntimeTypes.functionType)), false), ], returns: BridgeTypeAnnotation(BridgeTypeRef(BridgeTypeSpec('dart:core', 'Iterable')))), isStatic: false), + 'toList': BridgeMethodDef( + BridgeFunctionDef( + params: [ + BridgeParameter('growable', BridgeTypeAnnotation(BridgeTypeRef.type(RuntimeTypes.boolType)), true), + ], + returns: BridgeTypeAnnotation( + BridgeTypeRef(CoreTypes.list, [BridgeTypeRef.type(RuntimeTypes.dynamicType)]))), + isStatic: false), }, getters: { 'iterator': BridgeMethodDef( @@ -51,6 +59,8 @@ class $Iterable implements Iterable, $Instance { return $Function(__map); case 'iterator': return $Iterator.wrap($value.iterator); + case 'toList': + return __toList; default: return _superclass.$getProperty(runtime, identifier); } @@ -75,6 +85,12 @@ class $Iterable implements Iterable, $Instance { return $Iterable.wrap((target!.$value as Iterable).map((e) => toElement.call(runtime, null, [e])!.$value)); } + static const $Function __toList = $Function(_toList); + + static $Value? _toList(Runtime runtime, $Value? target, List<$Value?> args) { + return $List.wrap((target!.$value as Iterable).toList(growable: args[0]?.$value ?? true)); + } + @override bool any(bool Function(E element) test) => $value.any(test); diff --git a/lib/src/eval/shared/stdlib/core/list.dart b/lib/src/eval/shared/stdlib/core/list.dart index 40c6d5f..55f784a 100644 --- a/lib/src/eval/shared/stdlib/core/list.dart +++ b/lib/src/eval/shared/stdlib/core/list.dart @@ -38,6 +38,9 @@ class $List implements List, $Instance { '[]': BridgeMethodDef(BridgeFunctionDef(params: [ BridgeParameter('index', BridgeTypeAnnotation(BridgeTypeRef.type(RuntimeTypes.intType)), false), ], returns: BridgeTypeAnnotation(BridgeTypeRef.ref('E')))), + 'contains': BridgeMethodDef(BridgeFunctionDef(params: [ + BridgeParameter('element', BridgeTypeAnnotation(BridgeTypeRef.ref('E')), false), + ], returns: BridgeTypeAnnotation(BridgeTypeRef.type(RuntimeTypes.boolType)))), '[]=': BridgeMethodDef(BridgeFunctionDef(params: [ BridgeParameter('index', BridgeTypeAnnotation(BridgeTypeRef.type(RuntimeTypes.intType)), false), BridgeParameter('value', BridgeTypeAnnotation(BridgeTypeRef.ref('E')), false), @@ -45,10 +48,163 @@ class $List implements List, $Instance { 'add': BridgeMethodDef(BridgeFunctionDef(params: [ BridgeParameter('value', BridgeTypeAnnotation(BridgeTypeRef.ref('E')), false), ], returns: BridgeTypeAnnotation(BridgeTypeRef.type(RuntimeTypes.voidType)))), + 'lastIndexOf': BridgeMethodDef(BridgeFunctionDef(params: [ + BridgeParameter('element', BridgeTypeAnnotation(BridgeTypeRef.ref('E')), false), + BridgeParameter( + 'start', BridgeTypeAnnotation(BridgeTypeRef.type(RuntimeTypes.intType), nullable: true), true), + ], returns: BridgeTypeAnnotation(BridgeTypeRef.type(RuntimeTypes.intType)))), + 'join': BridgeMethodDef( + BridgeFunctionDef(params: [ + BridgeParameter('separator', BridgeTypeAnnotation(BridgeTypeRef.type(RuntimeTypes.stringType)), true), + ], returns: BridgeTypeAnnotation(BridgeTypeRef.type(RuntimeTypes.stringType))), + isStatic: false), + 'indexOf': BridgeMethodDef(BridgeFunctionDef(params: [ + BridgeParameter('element', BridgeTypeAnnotation(BridgeTypeRef.ref('E')), false), + BridgeParameter( + 'start', BridgeTypeAnnotation(BridgeTypeRef.type(RuntimeTypes.intType), nullable: true), true), + ], returns: BridgeTypeAnnotation(BridgeTypeRef.type(RuntimeTypes.intType)))), + 'elementAt': BridgeMethodDef(BridgeFunctionDef(params: [ + BridgeParameter('index', BridgeTypeAnnotation(BridgeTypeRef.type(RuntimeTypes.intType)), false), + ], returns: BridgeTypeAnnotation(BridgeTypeRef.ref('E')))), + 'remove': BridgeMethodDef(BridgeFunctionDef(params: [ + BridgeParameter('value', BridgeTypeAnnotation(BridgeTypeRef.ref('E')), false), + ], returns: BridgeTypeAnnotation(BridgeTypeRef.type(RuntimeTypes.boolType)))), + 'removeAt': BridgeMethodDef(BridgeFunctionDef( + params: [ + BridgeParameter('index', BridgeTypeAnnotation(BridgeTypeRef.type(RuntimeTypes.intType)), false), + ], + returns: BridgeTypeAnnotation(BridgeTypeRef.ref('E')), + )), + 'removeLast': BridgeMethodDef(BridgeFunctionDef( + returns: BridgeTypeAnnotation(BridgeTypeRef.ref('E')), + )), + 'insert': BridgeMethodDef(BridgeFunctionDef(params: [ + BridgeParameter('index', BridgeTypeAnnotation(BridgeTypeRef.type(RuntimeTypes.intType)), false), + BridgeParameter('element', BridgeTypeAnnotation(BridgeTypeRef.ref('E')), false), + ], returns: BridgeTypeAnnotation(BridgeTypeRef.type(RuntimeTypes.voidType)))), + 'toSet': BridgeMethodDef(BridgeFunctionDef( + params: [], returns: BridgeTypeAnnotation(BridgeTypeRef(BridgeTypeSpec('dart:core', 'Iterable'))))), + 'map': BridgeMethodDef( + BridgeFunctionDef(params: [ + BridgeParameter('toElement', BridgeTypeAnnotation(BridgeTypeRef.type(RuntimeTypes.functionType)), false), + ], returns: BridgeTypeAnnotation(BridgeTypeRef(BridgeTypeSpec('dart:core', 'Iterable')))), + isStatic: false), + 'followedBy': BridgeMethodDef( + BridgeFunctionDef(params: [ + BridgeParameter( + 'other', BridgeTypeAnnotation(BridgeTypeRef(BridgeTypeSpec('dart:core', 'Iterable'))), false), + ], returns: BridgeTypeAnnotation(BridgeTypeRef(BridgeTypeSpec('dart:core', 'Iterable')))), + isStatic: false), + 'getRange': BridgeMethodDef( + BridgeFunctionDef(params: [ + BridgeParameter('start', BridgeTypeAnnotation(BridgeTypeRef.type(RuntimeTypes.intType)), false), + BridgeParameter('end', BridgeTypeAnnotation(BridgeTypeRef.type(RuntimeTypes.intType)), false), + ], returns: BridgeTypeAnnotation(BridgeTypeRef(BridgeTypeSpec('dart:core', 'Iterable')))), + isStatic: false), + 'where': BridgeMethodDef( + BridgeFunctionDef(params: [ + BridgeParameter('test', BridgeTypeAnnotation(BridgeTypeRef.type(RuntimeTypes.functionType)), false), + ], returns: BridgeTypeAnnotation(BridgeTypeRef(BridgeTypeSpec('dart:core', 'Iterable')))), + isStatic: false), + 'skipWhile': BridgeMethodDef( + BridgeFunctionDef(params: [ + BridgeParameter('test', BridgeTypeAnnotation(BridgeTypeRef.type(RuntimeTypes.functionType)), false), + ], returns: BridgeTypeAnnotation(BridgeTypeRef(BridgeTypeSpec('dart:core', 'Iterable')))), + isStatic: false), + 'takeWhile': BridgeMethodDef( + BridgeFunctionDef(params: [ + BridgeParameter('test', BridgeTypeAnnotation(BridgeTypeRef.type(RuntimeTypes.functionType)), false), + ], returns: BridgeTypeAnnotation(BridgeTypeRef(BridgeTypeSpec('dart:core', 'Iterable')))), + isStatic: false), + 'sort': BridgeMethodDef( + BridgeFunctionDef(params: [ + BridgeParameter('compare', BridgeTypeAnnotation(BridgeTypeRef.type(RuntimeTypes.functionType)), false), + ], returns: BridgeTypeAnnotation(BridgeTypeRef.type(RuntimeTypes.voidType))), + isStatic: false), + 'retainWhere': BridgeMethodDef( + BridgeFunctionDef(params: [ + BridgeParameter('test', BridgeTypeAnnotation(BridgeTypeRef.type(RuntimeTypes.functionType)), false), + ], returns: BridgeTypeAnnotation(BridgeTypeRef.type(RuntimeTypes.voidType))), + isStatic: false), + 'replaceRange': BridgeMethodDef( + BridgeFunctionDef(params: [ + BridgeParameter('start', BridgeTypeAnnotation(BridgeTypeRef.type(RuntimeTypes.intType)), false), + BridgeParameter('end', BridgeTypeAnnotation(BridgeTypeRef.type(RuntimeTypes.intType)), false), + BridgeParameter( + 'replacements', BridgeTypeAnnotation(BridgeTypeRef(BridgeTypeSpec('dart:core', 'Iterable'))), false), + ], returns: BridgeTypeAnnotation(BridgeTypeRef.type(RuntimeTypes.voidType))), + isStatic: false), + 'sublist': BridgeMethodDef( + BridgeFunctionDef( + params: [ + BridgeParameter('start', BridgeTypeAnnotation(BridgeTypeRef.type(RuntimeTypes.intType)), false), + BridgeParameter( + 'end', BridgeTypeAnnotation(BridgeTypeRef.type(RuntimeTypes.intType), nullable: true), true), + ], + returns: BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.list, [ + BridgeTypeRef.ref('E'), + ]))), + isStatic: false), + 'any': BridgeMethodDef( + BridgeFunctionDef(params: [ + BridgeParameter('test', BridgeTypeAnnotation(BridgeTypeRef.type(RuntimeTypes.functionType)), false), + ], returns: BridgeTypeAnnotation(BridgeTypeRef.type(RuntimeTypes.boolType))), + isStatic: false), + 'every': BridgeMethodDef( + BridgeFunctionDef(params: [ + BridgeParameter('test', BridgeTypeAnnotation(BridgeTypeRef.type(RuntimeTypes.functionType)), false), + ], returns: BridgeTypeAnnotation(BridgeTypeRef.type(RuntimeTypes.boolType))), + isStatic: false), + 'skip': BridgeMethodDef( + BridgeFunctionDef(params: [ + BridgeParameter('count', BridgeTypeAnnotation(BridgeTypeRef.type(RuntimeTypes.intType)), false), + ], returns: BridgeTypeAnnotation(BridgeTypeRef(BridgeTypeSpec('dart:core', 'Iterable')))), + isStatic: false), + 'take': BridgeMethodDef( + BridgeFunctionDef(params: [ + BridgeParameter('count', BridgeTypeAnnotation(BridgeTypeRef.type(RuntimeTypes.intType)), false), + ], returns: BridgeTypeAnnotation(BridgeTypeRef(BridgeTypeSpec('dart:core', 'Iterable')))), + isStatic: false), + 'asMap': BridgeMethodDef( + BridgeFunctionDef(returns: BridgeTypeAnnotation(BridgeTypeRef.type(RuntimeTypes.mapType))), + isStatic: false), + 'toString': BridgeMethodDef( + BridgeFunctionDef(returns: BridgeTypeAnnotation(BridgeTypeRef.type(RuntimeTypes.stringType))), + isStatic: false), + 'clear': BridgeMethodDef( + BridgeFunctionDef(returns: BridgeTypeAnnotation(BridgeTypeRef.type(RuntimeTypes.voidType))), + isStatic: false), + 'addAll': BridgeMethodDef(BridgeFunctionDef(params: [ + BridgeParameter( + 'iterable', BridgeTypeAnnotation(BridgeTypeRef(BridgeTypeSpec('dart:core', 'Iterable'))), false), + ], returns: BridgeTypeAnnotation(BridgeTypeRef.type(RuntimeTypes.voidType)))), + 'insertAll': BridgeMethodDef(BridgeFunctionDef(params: [ + BridgeParameter('index', BridgeTypeAnnotation(BridgeTypeRef.type(RuntimeTypes.intType)), false), + BridgeParameter( + 'iterable', BridgeTypeAnnotation(BridgeTypeRef(BridgeTypeSpec('dart:core', 'Iterable'))), false), + ], returns: BridgeTypeAnnotation(BridgeTypeRef.type(RuntimeTypes.voidType)))), }, getters: { 'length': BridgeMethodDef(BridgeFunctionDef(returns: BridgeTypeAnnotation(BridgeTypeRef.type(RuntimeTypes.intType)))), + 'hashCode': + BridgeMethodDef(BridgeFunctionDef(returns: BridgeTypeAnnotation(BridgeTypeRef.type(RuntimeTypes.intType)))), + 'iterator': BridgeMethodDef( + BridgeFunctionDef( + params: [], + returns: BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.iterator, [ + BridgeTypeRef.ref('E'), + ]))), + isStatic: false), + 'reversed': BridgeMethodDef( + BridgeFunctionDef(returns: BridgeTypeAnnotation(BridgeTypeRef(BridgeTypeSpec('dart:core', 'Iterable')))), + isStatic: false), + 'first': BridgeMethodDef(BridgeFunctionDef(returns: BridgeTypeAnnotation(BridgeTypeRef.ref('E')))), + 'last': BridgeMethodDef(BridgeFunctionDef(returns: BridgeTypeAnnotation(BridgeTypeRef.ref('E')))), + 'isEmpty': BridgeMethodDef( + BridgeFunctionDef(returns: BridgeTypeAnnotation(BridgeTypeRef.type(RuntimeTypes.boolType)))), + 'isNotEmpty': BridgeMethodDef( + BridgeFunctionDef(returns: BridgeTypeAnnotation(BridgeTypeRef.type(RuntimeTypes.boolType)))), }, setters: {}, fields: {}, @@ -74,6 +230,78 @@ class $List implements List, $Instance { return __add; case 'length': return $int($value.length); + case 'addAll': + return __addAll; + case 'first': + return $super.first; + case 'contains': + return __listContains; + case 'map': + return __map; + case 'where': + return __where; + case 'isEmpty': + return $bool($super.isEmpty); + case 'isNotEmpty': + return $bool($super.isNotEmpty); + case 'last': + return $super.last; + case 'reversed': + return $Iterable.wrap($value.reversed); + case 'toSet': + return __toSet; + case 'iterator': + return $Iterator.wrap($value.iterator); + case 'insert': + return __insert; + case 'insertAll': + return __insertAll; + case 'remove': + return __remove; + case 'any': + return __any; + case 'asMap': + return __asMap; + case 'hashCode': + return $int($value.hashCode); + case 'lastIndexOf': + return __lastIndexOf; + case 'indexOf': + return __indexOf; + case 'elementAt': + return __elementAt; + case 'every': + return __every; + case 'retainWhere': + return __retainWhere; + case 'replaceRange': + return __replaceRange; + case 'followedBy': + return __followedBy; + case 'getRange': + return __getRange; + case 'join': + return __join; + case 'sort': + return __sort; + case 'removeAt': + return __removeAt; + case 'removeLast': + return __removeLast; + case 'skip': + return __skip; + case 'skipWhile': + return __skipWhile; + case 'sublist': + return __sublist; + case 'take': + return __take; + case 'takeWhile': + return __takeWhile; + case 'toString': + return __toString; + case 'clear': + return __clear; } return $super.$getProperty(runtime, identifier); } @@ -83,6 +311,131 @@ class $List implements List, $Instance { throw EvalUnknownPropertyException(identifier); } + static const $Function __map = $Function(_map); + + static $Value? _map(Runtime runtime, $Value? target, List<$Value?> args) { + final toElement = args[0] as EvalCallable; + return $Iterable.wrap((target!.$value as Iterable).map((e) => toElement.call(runtime, null, [e])!.$value)); + } + + static const $Function __followedBy = $Function(_followedBy); + + static $Value? _followedBy(Runtime runtime, $Value? target, List<$Value?> args) { + return $Iterable.wrap((target!.$value as Iterable).followedBy(args[0]!.$value as Iterable)); + } + + static const $Function __getRange = $Function(_getRange); + + static $Value? _getRange(Runtime runtime, $Value? target, List<$Value?> args) { + return $Iterable.wrap((target!.$value as List).getRange(args[0]!.$value, args[1]!.$value)); + } + + 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!.$value as Iterable).where((e) => test.call(runtime, null, [e])!.$value as bool)); + } + + static const $Function __skipWhile = $Function(_skipWhile); + + static $Value? _skipWhile(Runtime runtime, $Value? target, List<$Value?> args) { + final test = args[0] as EvalCallable; + return $Iterable.wrap((target!.$value as List).skipWhile((e) => test.call(runtime, null, [e])!.$value as bool)); + } + + static const $Function __takeWhile = $Function(_takeWhile); + + static $Value? _takeWhile(Runtime runtime, $Value? target, List<$Value?> args) { + final test = args[0] as EvalCallable; + return $Iterable.wrap((target!.$value as List).takeWhile((e) => test.call(runtime, null, [e])!.$value as bool)); + } + + static const $Function __toString = $Function(_toString); + + static $Value? _toString(Runtime runtime, $Value? target, List<$Value?> args) { + return $String((target!.$value as List).toString()); + } + + static const $Function __clear = $Function(_clear); + + static $Value? _clear(Runtime runtime, $Value? target, List<$Value?> args) { + (target!.$value as List).clear(); + return null; + } + + static const $Function __retainWhere = $Function(_retainWhere); + + static $Value? _retainWhere(Runtime runtime, $Value? target, List<$Value?> args) { + final test = args[0] as EvalCallable; + (target!.$value as List).retainWhere((e) => test.call(runtime, null, [e])!.$value as bool); + return null; + } + + static const $Function __replaceRange = $Function(_replaceRange); + + static $Value? _replaceRange(Runtime runtime, $Value? target, List<$Value?> args) { + (target!.$value as List).replaceRange(args[0]!.$value, args[1]!.$value, args[2]!.$value as Iterable); + return null; + } + + static const $Function __lastIndexOf = $Function(_lastIndexOf); + + static $Value? _lastIndexOf(Runtime runtime, $Value? target, List<$Value?> args) { + return $int((target!.$value as List).lastIndexOf(args[0], args[1]?.$value)); + } + + static const $Function __indexOf = $Function(_indexOf); + + static $Value? _indexOf(Runtime runtime, $Value? target, List<$Value?> args) { + return $int((target!.$value as List).indexOf(args[0], args[1]?.$value ?? 0)); + } + + static const $Function __elementAt = $Function(_elementAt); + + static $Value? _elementAt(Runtime runtime, $Value? target, List<$Value?> args) { + return (target!.$value as List).elementAt(args[0]!.$value); + } + + static const $Function __any = $Function(_any); + + static $Value? _any(Runtime runtime, $Value? target, List<$Value?> args) { + final test = args[0] as EvalCallable; + return $bool((target!.$value as List).any((e) => test.call(runtime, null, [e])!.$value as bool)); + } + + static const $Function __every = $Function(_every); + + static $Value? _every(Runtime runtime, $Value? target, List<$Value?> args) { + final test = args[0] as EvalCallable; + return $bool((target!.$value as List).every((e) => test.call(runtime, null, [e])!.$value as bool)); + } + + static const $Function __sort = $Function(_sort); + + static $Value? _sort(Runtime runtime, $Value? target, List<$Value?> args) { + final compare = args[0] as EvalCallable; + + (target!.$value as List).sort( + (a, b) => compare.call(runtime, null, [a, b])?.$value, + ); + return null; + } + + static const $Function __addAll = $Function(_addAll); + + static $Value? _addAll(Runtime runtime, $Value? target, List<$Value?> args) { + (target!.$value as List).addAll(args[0]!.$value as Iterable); + return null; + } + + static const $Function __insertAll = $Function(_insertAll); + + static $Value? _insertAll(Runtime runtime, $Value? target, List<$Value?> args) { + (target!.$value as List).insertAll(args[0]!.$value, args[1]!.$value as Iterable); + return null; + } + static const $Function __indexGet = $Function(_indexGet); static $Value? _indexGet(Runtime runtime, $Value? target, List<$Value?> args) { @@ -90,6 +443,37 @@ class $List implements List, $Instance { return (target!.$value as List)[idx.$value]; } + static const $Function __join = $Function(_join); + + static $Value? _join(Runtime runtime, $Value? target, List<$Value?> args) { + final separator = args[0]?.$value ?? ''; + return $String((target!.$value as List).join(separator)); + } + + static const $Function __sublist = $Function(_sublist); + + static $Value? _sublist(Runtime runtime, $Value? target, List<$Value?> args) { + return $List.wrap((target!.$value as List).sublist(args[0]!.$value, args[1]?.$value)); + } + + static const $Function __skip = $Function(_skip); + + static $Value? _skip(Runtime runtime, $Value? target, List<$Value?> args) { + return $Iterable.wrap((target!.$value as List).skip(args[0]!.$value)); + } + + static const $Function __take = $Function(_take); + + static $Value? _take(Runtime runtime, $Value? target, List<$Value?> args) { + return $Iterable.wrap((target!.$value as List).take(args[0]!.$value)); + } + + static const $Function __listContains = $Function(_listContains); + + static $Value? _listContains(Runtime runtime, $Value? target, List<$Value?> args) { + return $bool((target!.$value as List).contains(args[0])); + } + static const $Function __indexSet = $Function(_indexSet); static $Value? _indexSet(Runtime runtime, $Value? target, List<$Value?> args) { @@ -98,6 +482,33 @@ class $List implements List, $Instance { return (target!.$value as List)[idx.$value] = value; } + static const $Function __insert = $Function(_insert); + + static $Value? _insert(Runtime runtime, $Value? target, List<$Value?> args) { + final idx = args[0]!; + final value = args[1]!; + (target!.$value as List).insert(idx.$value, value); + return null; + } + + static const $Function __remove = $Function(_remove); + + static $Value? _remove(Runtime runtime, $Value? target, List<$Value?> args) { + return $bool((target!.$value as List).remove(args[0]!)); + } + + static const $Function __removeAt = $Function(_removeAt); + + static $Value? _removeAt(Runtime runtime, $Value? target, List<$Value?> args) { + return (target!.$value as List).removeAt(args[0]!.$value) as $Value?; + } + + static const $Function __removeLast = $Function(_removeLast); + + static $Value? _removeLast(Runtime runtime, $Value? target, List<$Value?> args) { + return (target!.$value as List).removeLast() as $Value?; + } + static const $Function __add = $Function(_add); static $Value? _add(Runtime runtime, $Value? target, List<$Value?> args) { @@ -106,6 +517,18 @@ class $List implements List, $Instance { return null; } + static const $Function __asMap = $Function(_asMap); + + static $Value? _asMap(Runtime runtime, $Value? target, List<$Value?> args) { + return $Map.wrap((target!.$value as List).asMap()); + } + + static const $Function __toSet = $Function(_toSet); + + static $Value? _toSet(Runtime runtime, $Value? target, List<$Value?> args) { + return $List.wrap((target!.$value as List).toSet().toList()); + } + @override List get $reified => $value.map((e) => e is $Value ? e.$reified : e).toList();