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

CompileError: Unknown method dynamic.read at "ref.read(1)" #194

Open
HXiaoMing opened this issue Mar 21, 2024 · 0 comments
Open

CompileError: Unknown method dynamic.read at "ref.read(1)" #194

HXiaoMing opened this issue Mar 21, 2024 · 0 comments

Comments

@HXiaoMing
Copy link

HXiaoMing commented Mar 21, 2024

I want to write a wrapper for riverpod (https://pub-web.flutter-io.cn/packages/riverpod)
But occur many questions.
it is possible support generics type?
If dart eval support generics type will be better(many dart package use generics type).

Another question is like this:
test case:

import 'package:dart_eval/dart_eval.dart';
import 'package:dart_eval/dart_eval_bridge.dart';
import 'package:dart_eval/stdlib/core.dart';
import 'package:test/test.dart';


abstract class Ref {
  int read(int value);
}

class RefImpl implements Ref {
  @override
  int read(int value) {
    return value;
  }
}

typedef ProviderCreateFn = void Function(Ref ref);

class Provider {
  final ProviderCreateFn _createFn;

  Provider(this._createFn) {
    _createFn(RefImpl());
  }
}

void main() {
  group('Wrapper tests', () {
    late Compiler compiler;

    setUp(() {
      compiler = Compiler();
    });

    test('Using a Wrapper class', () {
      compiler.defineBridgeClasses([
        $Ref.$declaration,
        $RefImpl.$declaration,
        $Provider.$declaration,
      ]);

      final program = compiler.compile({
        'example': {
          'main.dart': '''
            import 'package:code_push_app/test_bind/test_data.dart';
            
            int main() {
            
              final provider = Provider((ref) {
                final value = ref.read(1);
                print('providerValue \$value');
              });
            
              return 11;
            }
          '''
        }
      });

      final runtime = Runtime.ofProgram(program);

      $Ref.configureForRuntime(runtime);
      $RefImpl.configureForRuntime(runtime);
      $Provider.configureForRuntime(runtime);

      final result = runtime.executeLib('package:example/main.dart', 'main');
      print('result $result');
    });
  });
}


/// dart_eval wrapper binding for [Ref]
class $Ref implements $Instance {
  /// generate static final fields

  /// Configure this class for use in a [Runtime]
  static void configureForRuntime(Runtime runtime) {}

  /// Compile-time type declaration of [$Ref]
  static const $type = BridgeTypeRef(
      BridgeTypeSpec('package:code_push_app/test_bind/test_data.dart', 'Ref'));

  /// Compile-time class declaration of [$Ref]
  static const $declaration = BridgeClassDef(
    BridgeClassType(
      $type,
      isAbstract: true,
    ),
    constructors: {},
    methods: {
      'read': BridgeMethodDef(
        BridgeFunctionDef(
          returns: BridgeTypeAnnotation(
            BridgeTypeRef(CoreTypes.int),
            nullable: false,
          ),
          namedParams: [],
          params: [
            BridgeParameter(
              'value',
              BridgeTypeAnnotation(
                BridgeTypeRef(CoreTypes.int),
                nullable: false,
              ),
              false,
            ),
          ],
        ),
      ),
    },
    getters: {},
    setters: {},
    fields: {},
    wrap: true,
  );

  late final _superclass = $Object($value);

  /// Wrap a [Ref] in a [$Ref]
  $Ref.wrap(this.$value);

  @override
  final Ref $value;

  @override
  Ref get $reified => $value;

  @override
  int $getRuntimeType(Runtime runtime) => runtime.lookupType($type.spec!);

  @override
  $Value? $getProperty(Runtime runtime, String identifier) {
    switch (identifier) {
      case 'read':
        return __read;
    }

    return _superclass.$getProperty(runtime, identifier);
  }

  late final $Function __read = $Function(_read);
  $Value? _read(Runtime runtime, $Value? target, List<$Value?> args) {
    final localInstance = (target?.$value as Ref?) ?? $value;

    final resultValue = localInstance.read(args[0]?.$value);
    return $int(resultValue);
  }

  @override
  void $setProperty(Runtime runtime, String identifier, $Value value) {
    return _superclass.$setProperty(runtime, identifier, value);
  }
}

/// dart_eval wrapper binding for [RefImpl]
class $RefImpl implements $Instance {
  /// generate static final fields

  /// Configure this class for use in a [Runtime]
  static void configureForRuntime(Runtime runtime) {
    runtime.registerBridgeFunc('package:code_push_app/test_bind/test_data.dart',
        'RefImpl.', $RefImpl.$new);
  }

  /// Compile-time type declaration of [$RefImpl]
  static const $type = BridgeTypeRef(BridgeTypeSpec(
      'package:code_push_app/test_bind/test_data.dart', 'RefImpl'));

  /// Compile-time class declaration of [$RefImpl]
  static const $declaration = BridgeClassDef(
    BridgeClassType(
      $type,
      isAbstract: false,
      $implements: [
        BridgeTypeRef(BridgeTypeSpec(
            'package:code_push_app/test_bind/test_data.dart', 'Ref'))
      ],
    ),
    constructors: {
      '': BridgeConstructorDef(
        BridgeFunctionDef(
          returns: BridgeTypeAnnotation($type),
          namedParams: [],
          params: [],
        ),
        isFactory: false,
      ),
    },
    methods: {
      'read': BridgeMethodDef(
        BridgeFunctionDef(
          returns: BridgeTypeAnnotation(
            BridgeTypeRef(CoreTypes.int),
            nullable: false,
          ),
          namedParams: [],
          params: [
            BridgeParameter(
              'value',
              BridgeTypeAnnotation(
                BridgeTypeRef(CoreTypes.int),
                nullable: false,
              ),
              false,
            ),
          ],
        ),
      ),
    },
    getters: {},
    setters: {},
    fields: {},
    wrap: true,
  );

  /// Wrapper for the [RefImpl.new] constructor
  static $Value? $new(Runtime runtime, $Value? thisValue, List<$Value?> args) {
    return $RefImpl.wrap(
      RefImpl(),
    );
  }

  late final _superclass = $Object($value);

  /// Wrap a [RefImpl] in a [$RefImpl]
  $RefImpl.wrap(this.$value);

  @override
  final RefImpl $value;

  @override
  RefImpl get $reified => $value;

  @override
  int $getRuntimeType(Runtime runtime) => runtime.lookupType($type.spec!);

  @override
  $Value? $getProperty(Runtime runtime, String identifier) {
    switch (identifier) {
      case 'read':
        return __read;
    }

    return _superclass.$getProperty(runtime, identifier);
  }

  late final $Function __read = $Function(_read);
  $Value? _read(Runtime runtime, $Value? target, List<$Value?> args) {
    final localInstance = (target?.$value as RefImpl?) ?? $value;

    final resultValue = localInstance.read(args[0]?.$value);
    return $int(resultValue);
  }

  @override
  void $setProperty(Runtime runtime, String identifier, $Value value) {
    return _superclass.$setProperty(runtime, identifier, value);
  }
}

/// dart_eval wrapper binding for [Provider]
class $Provider implements $Instance {
  /// generate static final fields

  /// Configure this class for use in a [Runtime]
  static void configureForRuntime(Runtime runtime) {
    runtime.registerBridgeFunc('package:code_push_app/test_bind/test_data.dart',
        'Provider.', $Provider.$new);
  }

  /// Compile-time type declaration of [$Provider]
  static const $type = BridgeTypeRef(BridgeTypeSpec(
      'package:code_push_app/test_bind/test_data.dart', 'Provider'));

  /// Compile-time class declaration of [$Provider]
  static const $declaration = BridgeClassDef(
    BridgeClassType(
      $type,
      isAbstract: false,
    ),
    constructors: {
      '': BridgeConstructorDef(
        BridgeFunctionDef(
          returns: BridgeTypeAnnotation($type),
          namedParams: [],
          params: [
            BridgeParameter(
              '_createFn',
              BridgeTypeAnnotation(
                BridgeTypeRef(CoreTypes.function),
                nullable: false,
              ),
              false,
            ),
          ],
        ),
        isFactory: false,
      ),
    },
    methods: {},
    getters: {},
    setters: {},
    fields: {
      '_createFn': BridgeFieldDef(
        BridgeTypeAnnotation(
          BridgeTypeRef(CoreTypes.function),
          nullable: false,
        ),
        isStatic: false,
      ),
    },
    wrap: true,
  );

  /// Wrapper for the [Provider.new] constructor
  static $Value? $new(Runtime runtime, $Value? thisValue, List<$Value?> args) {
    return $Provider.wrap(
      Provider((Ref ref) => (args[0] as EvalCallable)
          .call(runtime, null, [$Ref.wrap(ref)])?.$value),
    );
  }

  late final _superclass = $Object($value);

  /// Wrap a [Provider] in a [$Provider]
  $Provider.wrap(this.$value);

  @override
  final Provider $value;

  @override
  Provider get $reified => $value;

  @override
  int $getRuntimeType(Runtime runtime) => runtime.lookupType($type.spec!);

  @override
  $Value? $getProperty(Runtime runtime, String identifier) {
    return _superclass.$getProperty(runtime, identifier);
  }

  @override
  void $setProperty(Runtime runtime, String identifier, $Value value) {
    return _superclass.$setProperty(runtime, identifier, value);
  }
}

Error:
CompileError: Unknown method dynamic.read at "ref.read(1)" (file package:example/main.dart)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant