diff --git a/example/dart_eval_example.dart b/example/dart_eval_example.dart index 6795936..9af9544 100644 --- a/example/dart_eval_example.dart +++ b/example/dart_eval_example.dart @@ -62,31 +62,39 @@ void main(List args) { // Compile the source code into a Program containing metadata and bytecode. // In a real app, you could also compile the Eval code separately and output // it to a file using program.write(). - final program = compiler.compile({ - 'example': {'main.dart': source} - }); + try { + final program = compiler.compile({ + 'example': {'main.dart': source} + }); - // Create a runtime from the compiled program, and register bridge functions - // for all static methods and constructors. Default constructors use - // "ClassName." syntax. - final runtime = Runtime.ofProgram(program) - ..registerBridgeFunc('package:example/bridge.dart', 'TimestampedTime.', - $TimestampedTime.$new) - ..registerBridgeFunc('package:example/bridge.dart', 'WorldTimeTracker.', - $WorldTimeTracker$bridge.$new, - isBridge: true); + // Create a runtime from the compiled program, and register bridge functions + // for all static methods and constructors. Default constructors use + // "ClassName." syntax. - // Call the function and cast the result to the desired type - final timeTracker = runtime.executeLib( - 'package:example/main.dart', - 'fn', - // Wrap args in $Value wrappers except int, double, bool, and List - [$String('USA')], - ) as WorldTimeTracker; + final runtime = Runtime.ofProgram(program) + ..registerBridgeFunc('package:example/bridge.dart', 'TimestampedTime.', + $TimestampedTime.$new) + ..registerBridgeFunc('package:example/bridge.dart', 'WorldTimeTracker.', + $WorldTimeTracker$bridge.$new, + isBridge: true); - // We can now utilize the returned bridge class - print('UK timezone offset: ${timeTracker.getTimeFor('UK').timezoneOffset}' - ' (from outside Eval!)'); + // Call the function and cast the result to the desired type + final timeTracker = runtime.executeLib( + 'package:example/main.dart', + 'fn', + // Wrap args in $Value wrappers except int, double, bool, and List + [$String('USA')], + ) as WorldTimeTracker; + + // We can now utilize the returned bridge class + print('UK timezone offset: ${timeTracker.getTimeFor('UK').timezoneOffset}' + ' (from outside Eval!)'); + } catch (e) { + if (e is CompileError) { + print(e.message); + } + print(e.toString()); + } } /// Create a wrapper for [TimestampedTime]. A wrapper is a performant interop diff --git a/lib/dart_eval.dart b/lib/dart_eval.dart index b873b8f..482e3b4 100644 --- a/lib/dart_eval.dart +++ b/lib/dart_eval.dart @@ -5,4 +5,5 @@ export 'src/eval/eval.dart'; export 'src/eval/runtime/runtime.dart' show Runtime; export 'src/eval/compiler/compiler.dart'; export 'src/eval/compiler/program.dart'; +export 'src/eval/compiler/errors.dart' hide NotReferencableError; export 'src/eval/runtime/override.dart' hide runtimeOverride; diff --git a/lib/src/eval/runtime/runtime.dart b/lib/src/eval/runtime/runtime.dart index 6f77880..5cdd7e2 100644 --- a/lib/src/eval/runtime/runtime.dart +++ b/lib/src/eval/runtime/runtime.dart @@ -9,7 +9,9 @@ import 'package:dart_eval/dart_eval_security.dart'; import 'package:dart_eval/src/eval/bridge/runtime_bridge.dart'; import 'package:dart_eval/src/eval/compiler/model/override_spec.dart'; import 'package:dart_eval/src/eval/runtime/class.dart'; +import 'package:dart_eval/src/eval/runtime/continuation.dart'; import 'package:dart_eval/src/eval/runtime/function.dart'; +import 'package:dart_eval/src/eval/runtime/type.dart'; import 'package:dart_eval/src/eval/shared/stdlib/async.dart'; import 'package:dart_eval/src/eval/shared/stdlib/collection.dart'; import 'package:dart_eval/src/eval/shared/stdlib/convert.dart'; @@ -19,21 +21,15 @@ import 'package:dart_eval/src/eval/shared/stdlib/io.dart'; import 'package:dart_eval/src/eval/shared/stdlib/math.dart'; import 'package:dart_eval/src/eval/shared/stdlib/typed_data.dart'; import 'package:dart_eval/stdlib/core.dart'; -import 'package:dart_eval/src/eval/runtime/continuation.dart'; -import 'package:dart_eval/src/eval/runtime/type.dart'; import 'exception.dart'; import 'ops/all_ops.dart'; -part 'ops/primitives.dart'; - -part 'ops/memory.dart'; - +part 'ops/bridge.dart'; part 'ops/flow.dart'; - +part 'ops/memory.dart'; part 'ops/objects.dart'; - -part 'ops/bridge.dart'; +part 'ops/primitives.dart'; typedef TypeAutowrapper = $Value? Function(dynamic); @@ -61,6 +57,7 @@ class _UnloadedBridgeFunction { class _UnloadedEnumValues { const _UnloadedEnumValues(this.library, this.name, this.values); + final String library; final String name; final Map values; @@ -103,6 +100,7 @@ class Runtime { /// Create a [Runtime] from a [Program]. This constructor should be preferred /// where possible as it avoids overhead of loading bytecode. + ///CompileError will be be thrown when there's an error Runtime.ofProgram(Program program) : id = _id++, _fromEvc = false, @@ -808,6 +806,7 @@ class Runtime { var declarations = >{}; final declaredClasses = >{}; + //late final List typeNames; late final List> typeTypes; late final Map> typeIds;