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

Expose CompileError #219

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 30 additions & 22 deletions example/dart_eval_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,31 +62,39 @@ void main(List<String> 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
Expand Down
1 change: 1 addition & 0 deletions lib/dart_eval.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
17 changes: 8 additions & 9 deletions lib/src/eval/runtime/runtime.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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);

Expand Down Expand Up @@ -61,6 +57,7 @@ class _UnloadedBridgeFunction {

class _UnloadedEnumValues {
const _UnloadedEnumValues(this.library, this.name, this.values);

final String library;
final String name;
final Map<String, $Value> values;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -808,6 +806,7 @@ class Runtime {

var declarations = <int, Map<String, int>>{};
final declaredClasses = <int, Map<String, EvalClass>>{};

//late final List<String> typeNames;
late final List<Set<int>> typeTypes;
late final Map<int, Map<String, int>> typeIds;
Expand Down
Loading