Creating a bridge for class with static methods. #46
-
So I have a class i'm looking to bridge. It contains all static methods. What i'd like to know is how can I define these methods in the bridge boiler plate code? Can someone please give me some assistance on how to properly do this. Thanks in advance. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 18 replies
-
I've been going through it piece by piece and this is what I have so far. import 'package:dart_eval/dart_eval_bridge.dart';
import 'package:dart_eval/src/eval/runtime/runtime.dart';
import 'package:dart_eval/stdlib/core.dart';
import 'package:flutter_auto_gui/flutter_auto_gui.dart';
class $FlutterAutoGUI implements FlutterAutoGUI, $Instance {
/// Define the compile-time type descriptor as an unresolved type
static const $type = BridgeTypeRef.spec(
BridgeTypeSpec('package:example/bridge.dart', 'FlutterAutoGUI'));
@override
final FlutterAutoGUI $value;
$FlutterAutoGUI.wrap(this.$value) : _superclass = $Object($value);
final $Instance _superclass;
static const $declaration = BridgeClassDef(
BridgeClassType($type),
constructors: {},
methods: {},
getters: {},
setters: {},
fields: {},
wrap: true,
);
static $Value? $new(Runtime runtime, $Value? target, List<$Value?> args) {
return $FlutterAutoGUI.wrap(FlutterAutoGUI());
}
@override
$Value? $getProperty(Runtime runtime, String identifier) {
throw UnimplementedError();
}
@override
// TODO: implement $reified
get $reified => throw UnimplementedError();
@override
// TODO: implement $runtimeType
int get $runtimeType => throw UnimplementedError();
@override
void $setProperty(Runtime runtime, String identifier, $Value value) {
// TODO: implement $setProperty
}
}
|
Beta Was this translation helpful? Give feedback.
-
Hey, nice work! You're definitely on the right track :) So, a few things. Next, dart_eval doesn't have standard library bindings for Point, Curve, or Size yet. Luckily that's really easy to do so I'll add those in the next few days and publish a new point release. You'll also probably want to replace 'package:example/bridge.dart` with your actual package and file name so the import makes sense. If you want to be technical about it and specify an internal URI with /src/, you'll also have to do compiler.addSource and put your top-level library file with the export '...' directives. After that, all that's left is creating the runtime bindings which for you should be entirely with registerBridgeFunc(). The notable thing there is that you'll have to provide a type mapper when wrapping your returned Futures in $Future, so that it can convert the internal type (e.g. Point to $Point). That's just a parameter on the $Future constructor, where you can specify a closure that does One final thing - you'll want to remove that |
Beta Was this translation helpful? Give feedback.
-
Nope, you're good.
No, those are instance runtime bindings. You can see examples of static runtime bindings here (which references this) and here (which references this). Constructors and static methods are defined basically the same way at runtime. (Edit: I did notice you already started doing this in your global class)
No.
I think you can remove
I'd recommend making |
Beta Was this translation helpful? Give feedback.
-
@ethanblake4 Sorry but I'm back again haha. So. This is what I've managed so far. I think everything here should be fine (focusing more on import 'package:dart_eval/dart_eval.dart';
import 'package:dart_eval/dart_eval_bridge.dart';
import 'package:dart_eval/stdlib/core.dart';
import 'package:dart_eval/stdlib/math.dart';
import 'package:flutter_auto_gui/flutter_auto_gui.dart';
const _flutterAutoGUISource = '''
export 'package:flutter_auto_gui/flutter_auto_gui.dart' show FlutterAutoGUI;
''';
class $FlutterAutoGUI extends $Instance {
static const $type = BridgeTypeRef.spec(
BridgeTypeSpec(
'package:flutter_auto_gui/flutter_auto_gui.dart',
'FlutterAutoGUI',
),
);
@override
final FlutterAutoGUI $value;
/// Configure this class for compilation in a [Compiler].
static void configureForCompile(Compiler compiler) {
compiler.defineBridgeClass($declaration);
compiler.addSource(DartSource(
'package:flutter_auto_gui/flutter_auto_gui.dart',
_flutterAutoGUISource));
}
/// Configure this class for runtime in a [Runtime].
static void configureForRuntime(Runtime runtime) {
runtime.registerBridgeFunc(
'package:flutter_auto_gui/flutter_auto_gui.dart',
'FlutterAutoGUI.position',
$FlutterAutoGUI.$position,
);
}
$FlutterAutoGUI.wrap(this.$value);
static const $declaration = BridgeClassDef(
BridgeClassType($type),
constructors: {},
methods: {
'position': BridgeMethodDef(
BridgeFunctionDef(
returns: BridgeTypeAnnotation(
BridgeTypeRef.spec(
BridgeTypeSpec('dart:core', 'Future'),
),
),
),
isStatic: true,
),
},
getters: {},
setters: {},
fields: {},
wrap: true,
);
static $Future $position(
Runtime runtime, $Value? target, List<$Value?> args) {
return $Future.wrap(
FlutterAutoGUI.position(),
(value) => $Point.wrap(value),
);
}
@override
$Value? $getProperty(Runtime runtime, String identifier) {
throw UnimplementedError();
}
@override
get $reified => throw UnimplementedError();
@override
int get $runtimeType => throw UnimplementedError();
@override
void $setProperty(Runtime runtime, String identifier, $Value value) =>
throw UnimplementedError();
} I've went ahead and created static function for the compile and runtime set ups like I've seen you done in flutter_eval (i like this approach) |
Beta Was this translation helpful? Give feedback.
Nope, you're good.
No, those are instance runtime bindings. You can see examples of static runtime bindings here (which references this) and here (which references this). Constructors and static methods are defined basically the same way at runtime. (Edit: I did notice you already started doing this in your global class)
No.
I think you can remove
nullable: true
with the Future type reference.