Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Bad state: NoSuchMethodError: The getter 'length' was called on n… #9

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions lib/src/executable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,32 @@ abstract class Executable<T> {
final SendPort _sendPort;

U instanceOf<U>(String typeName,
{List positionalArguments: const [], Map<Symbol, dynamic> namedArguments, Symbol constructorName}) {
ClassMirror typeMirror = currentMirrorSystem().isolate.rootLibrary.declarations[new Symbol(typeName)];
{List positionalArguments: const [],
Map<Symbol, dynamic> namedArguments = const <Symbol, dynamic>{},
Symbol constructorName}) {
ClassMirror typeMirror = currentMirrorSystem()
.isolate
.rootLibrary
.declarations[new Symbol(typeName)];
if (typeMirror == null) {
typeMirror = currentMirrorSystem()
.libraries
.values
.where((lib) => lib.uri.scheme == "package" || lib.uri.scheme == "file")
.where(
(lib) => lib.uri.scheme == "package" || lib.uri.scheme == "file")
.expand((lib) => lib.declarations.values)
.firstWhere((decl) => decl is ClassMirror && MirrorSystem.getName(decl.simpleName) == typeName,
orElse: () => throw new ArgumentError("Unknown type '$typeName'. Did you forget to import it?"));
.firstWhere(
(decl) =>
decl is ClassMirror &&
MirrorSystem.getName(decl.simpleName) == typeName,
orElse: () => throw new ArgumentError(
"Unknown type '$typeName'. Did you forget to import it?"));
}

return typeMirror.newInstance(constructorName ?? const Symbol(""), positionalArguments, namedArguments).reflectee
as U;
return typeMirror
.newInstance(constructorName ?? const Symbol(""), positionalArguments,
namedArguments)
.reflectee as U;
}

void send(dynamic message) {
Expand Down
21 changes: 15 additions & 6 deletions lib/src/source_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@ import 'dart:isolate';
import 'dart:mirrors';
import 'dart:async';

import 'package:analyzer/analyzer.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/analysis/features.dart';
import 'package:analyzer/dart/analysis/utilities.dart';
import 'package:isolate_executor/src/executable.dart';
import 'package:pub_semver/pub_semver.dart';

class SourceGenerator {
SourceGenerator(this.executableType, {this.imports, this.additionalContents, this.additionalTypes});
SourceGenerator(this.executableType,
{this.imports, this.additionalContents, this.additionalTypes});

Type executableType;

String get typeName => MirrorSystem.getName(reflectType(executableType).simpleName);
String get typeName =>
MirrorSystem.getName(reflectType(executableType).simpleName);
final List<String> imports;
final String additionalContents;
final List<Type> additionalTypes;
Expand Down Expand Up @@ -50,11 +55,15 @@ Future main (List<String> args, Map<String, dynamic> message) async {
}

static Future<ClassDeclaration> _getClass(Type type) async {
final uri = await Isolate.resolvePackageUri(reflectClass(type).location.sourceUri);
final fileUnit = parseDartFile(uri.toFilePath(windows: Platform.isWindows));
final uri =
await Isolate.resolvePackageUri(reflectClass(type).location.sourceUri);
final fileUnit = parseFile(
path: uri.toFilePath(windows: Platform.isWindows),
featureSet: FeatureSet.fromEnableFlags2(
flags: [], sdkLanguageVersion: Version(2, 8, 0)));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the significance of using version 2.8.0?

final typeName = MirrorSystem.getName(reflectClass(type).simpleName);

return fileUnit.declarations
return fileUnit.unit.declarations
.where((u) => u is ClassDeclaration)
.map((cu) => cu as ClassDeclaration)
.firstWhere((classDecl) => classDecl.name.name == typeName);
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ environment:
sdk: ">=2.0.0 <3.0.0"

dependencies:
analyzer: '>=0.32.0 <0.50.0'
analyzer: '>=0.40.6 <0.41.0'
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be more permissive

Suggested change
analyzer: '>=0.40.6 <0.41.0'
analyzer: '>=0.40.1 <0.42.0'


dev_dependencies:
test: ^1.3.0
Expand Down