-
Notifications
You must be signed in to change notification settings - Fork 40
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
[Bug] The readme example can't work. #216
Comments
import 'package:dart_eval/dart_eval.dart';
void main() {
final compiler = Compiler();
final program = compiler.compile({
'my_package': {
'main.dart': '''
import 'package:my_package/finder.dart';
void main() {
final parentheses = findParentheses('Hello (world)');
if (parentheses.isNotEmpty) print(parentheses);
}
''',
'finder.dart': r'''
List<int> findParentheses(string) {
final regex = RegExp(r'\((.*?)\)');
final matches = regex.allMatches(string);
List<int> result = [];
for (var match in matches) {
result.add(match.start);
}
return result;
}
'''
}
});
final runtime = Runtime.ofProgram(program);
print(runtime.executeLib(
'package:my_package/main.dart', 'main')); // prints '[6]'
} This works fine. It seems the error occurs in |
And the readme example import 'package:dart_eval/dart_eval.dart';
import 'package:dart_eval/dart_eval_bridge.dart';
import 'package:dart_eval/dart_eval_extensions.dart';
import 'package:dart_eval/stdlib/core.dart';
/// An example class we want to wrap
class Book {
Book(this.pages);
final List<String> pages;
String getPage(int index) => pages[index];
}
/// This is our wrapper class
class $Book implements $Instance {
/// Create a type specification for the dart_eval compiler
static final $type = BridgeTypeSpec('package:hello/book.dart', 'Book').ref;
/// Create a class declaration for the dart_eval compiler
static final $declaration = BridgeClassDef(BridgeClassType($type),
constructors: {
// Define the default constructor with an empty string
'': BridgeFunctionDef(
returns: $type.annotate,
params: ['pages'.param(CoreTypes.string.ref.annotate)])
.asConstructor
},
methods: {
'getPage': BridgeFunctionDef(
returns: CoreTypes.string.ref.annotate,
params: ['index'.param(CoreTypes.int.ref.annotate)],
).asMethod,
},
wrap: true);
/// Override $value and $reified to return the value
@override
final Book $value;
@override
get $reified => $value;
/// Create a constructor that wraps the Book class
$Book.wrap(this.$value);
static $Value? $new(Runtime runtime, $Value? target, List<$Value?> args) {
return $Book.wrap(Book(args[0]!.$value));
}
/// Create a wrapper for property and method getters
@override
$Value? $getProperty(Runtime runtime, String identifier) {
if (identifier == 'getPage') {
return $Function((_, target, args) {
return $String($value.getPage(args[0]!.$value));
});
}
return $Object(this).$getProperty(runtime, identifier);
}
/// Create a wrapper for property setters
@override
void $setProperty(Runtime runtime, String identifier, $Value value) {
return $Object(this).$setProperty(runtime, identifier, value);
}
/// Allow runtime type lookup
@override
int $getRuntimeType(Runtime runtime) => runtime.lookupType($type.spec!);
}
/// Now we can use it in dart_eval!
void main() {
final compiler = Compiler();
compiler.defineBridgeClass($Book.$declaration);
final program = compiler.compile({
'hello': {
'main.dart': '''
import 'book.dart';
void main() {
final book = Book(['Hello world!', 'Hello again!']);
print(book.getPage(1));
}
'''
}
});
final runtime = Runtime.ofProgram(program);
// Register static methods and constructors with the runtime
runtime.registerBridgeFunc('package:hello/book.dart', 'Book.', $Book.$new);
runtime.executeLib('package:hello/main.dart', 'main'); // -> 'Hello again!'
} Error log:
|
================= I am having the same issue with $String() at compile step If I remove the $String() and put just the string compilation works but I get this error Failed to evaluate program: dart_eval runtime exception: TypeError: "Hi!": type 'String' is not a subtype of type '$Instance' RUNTIME STATEProgram offset: 83 |
Fixed issue with the readme Wrapper interop example. As far as the initial issue, I can't reproduce, this example is actually a test in dart_eval and it is working fine. |
The readme example below can't work with dart_eval: ^0.7.9.
Below is the error log:
The text was updated successfully, but these errors were encountered: