ethanblake4 / dart_eval

Extensible Dart interpreter for Dart with full interop

Home Page:https://pub.dev/packages/dart_eval

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

how to declare static fields

Noobware1 opened this issue · comments

as the title says.

my current approach the static fields are unknown and hlsMaster

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

import 'package:meiyou_extenstions/src/constants/constants.dart';
import 'package:meiyou_extenstions/src/models/media/video/video_quailty.dart';

class $VideoQuality implements VideoQuality, $Instance {
  $VideoQuality.wrap(this.$value);

  static void configureForRuntime(Runtime runtime) {
    runtime.registerBridgeFunc(
        bridgeLibary, 'VideoQuality.', $VideoQuality.$new);

    runtime.registerBridgeFunc(bridgeLibary, 'VideoQuality.getFromString',
        $VideoQuality.$getFromString);
  }

  static const $type =
      BridgeTypeRef(BridgeTypeSpec(bridgeLibary, 'VideoQuality'));

  static const $declaration = BridgeClassDef(
    BridgeClassType($type),
    constructors: {
      '': BridgeConstructorDef(
        BridgeFunctionDef(returns: BridgeTypeAnnotation($type), params: [
          BridgeParameter(
              'height',
              BridgeTypeAnnotation(
                BridgeTypeRef(CoreTypes.int),
              ),
              false),
          BridgeParameter(
              'width',
              BridgeTypeAnnotation(
                BridgeTypeRef(CoreTypes.int),
              ),
              false),
        ]),
      )
    },
    fields: {
      'height': BridgeFieldDef(
        BridgeTypeAnnotation(
          BridgeTypeRef(CoreTypes.int),
        ),
      ),
      'width': BridgeFieldDef(
        BridgeTypeAnnotation(
          BridgeTypeRef(CoreTypes.int),
        ),
      ),
       'unknown': BridgeFieldDef(
          BridgeTypeAnnotation(
            $type,
          ),
          isStatic: true),
      'hlsMaster': BridgeFieldDef(
          BridgeTypeAnnotation(
            $type,
          ),
          isStatic: true),
    },
    methods: {
      'getFromString': BridgeMethodDef(
          BridgeFunctionDef(returns: BridgeTypeAnnotation($type), params: [
            BridgeParameter('str',
                BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.string)), false)
          ]),
          isStatic: true),
    },
    wrap: true,
  );

  @override
  $Value? $getProperty(Runtime runtime, String identifier) {
    switch (identifier) {
      case 'height':
        return $int($value.height);
      case 'width':
        return $int($value.width);
      case 'hlsMaster':
        return $VideoQuality.wrap(VideoQuality.hlsMaster);
      case 'unknown':
        return $VideoQuality.wrap(VideoQuality.unknown);

      default:
        return $null();
    }
  }

  static $Value $new(Runtime runtime, $Value? target, List<$Value?> args) {
    return $VideoQuality.wrap(VideoQuality(args[0]?.$value, args[1]?.$value));
  }

  static $Value $getFromString(
      Runtime runtime, $Value? target, List<$Value?> args) {
    return $VideoQuality.wrap(VideoQuality.getFromString(args[0]?.$value));
  }

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

  @override
  get $reified => $value;

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

  @override
  final VideoQuality $value;

  @override
  int get height => $value.height;

  @override
  int get width => $value.width;

  @override
  String toString() {
    return $value.toString();
  }
}

here's my error

Instance of '$ExtractorApi'
Instance of 'Future<void>'
Unhandled exception:
dart_eval runtime exception: UnimplementedError: Tried to invoke a nonexistent external function; did you forget to add it with registerBridgeFunc()?
#0      Runtime._fn (package:dart_eval/src/eval/runtime/runtime.dart:96:5)
#1      $Function.call (package:dart_eval/src/eval/runtime/function.dart:122:16)
#2      InvokeExternal.run (package:dart_eval/src/eval/runtime/ops/bridge.dart:107:44)
at GogoCDN.toVideoSource()
at <asynchronous gap>

RUNTIME STATE
=============
Program offset: 355
Stack sample: [L0: Instance of '$ExtractorApi', L1: Instance of '$Completer<dynamic>', L2: ExtractorLink(name: , url: https://goone.pro/streaming.php?id=MjE2NzQ2&title=Undead+Unluck+Episode+10, headers: null, referer: null, extra: null), L3: $"https://goone.pro/streaming.php?id=MjE2NzQ2&title=Undead+Unluck+Episode+10", L4: $"GET", L5: null, L6: Instance of '$Future<dynamic>', L7: Instance of '$OkHttpResponseObject', L8: Instance of '$DocumentObject', L9: $"script[data-name="episode"]"]
Args sample: []
Call stack: [0, -1]
TRACE:
349: InvokeDynamic (L0.C44)
350: PushReturnValue ()
351: Unbox (L9)
352: JumpIfFalse (@367 if L9 == false)
353: LoadGlobal (G27)

code for toVideoSource

 VideoSource toVideoSource(dynamic j, bool backup) {
    final fileLabel = StringUtils.valueToString(j['label']).toLowerCase();

    final url = j['file'];

    if (isHLS(fileLabel)) {
      return VideoSource(
        url: url,
        quality: VideoQuality.hlsMaster,
        format: VideoFormat.hls,
        isBackup: backup,
      );
    } else {
      return VideoSource(
        url: url,
        quality: VideoQuality.getFromString(fileLabel),
        format: VideoFormat.other,
        isBackup: backup,
      );
    }
  }

  bool isHLS(dynamic filelabel) {
    if (filelabel == 'hls p') {
      return true;
    } else if (filelabel == 'auto p') {
      return true;
    } else {
      return false;
    }
  }

For static fields you have to register the getter / setter of the field with registerBridgeFunc() using *g and/or *s postfixes respectfully, rather than using $getProperty / $setProperty. e.g. for hlsMaster you could do:

configureForRuntime(Runtime runtime) {
   // ...
   runtime.registerBridgeFunc(bridgeLibrary, 'VideoQuality.hlsMaster*g', $VideoQuality.$hlsMaster)
}

static $Value? $hlsMaster(Runtime runtime, $Value? target, List<$Value?> args) {
    return $VideoQuality.wrap(VideoQuality.hlsMaster);
}

thanks, it works!!