Skip to content

Commit

Permalink
Fix example
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanblake4 committed May 18, 2022
1 parent fefc715 commit 06dda6a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 28 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void main() {

## Compiling to a file

For most use-cases, it's recommended to pre-compile your Dart code to DBC bytecode,
For most use-cases, it's recommended to pre-compile your Dart code to EVC bytecode,
to avoid runtime compilation overhead. (This is still runtime code execution, it's
just executing a more efficient code format.)

Expand All @@ -72,7 +72,7 @@ void main() {
final bytecode = program.write();
final file = File('program.dbc');
final file = File('program.evc');
file.writeAsBytesSync(bytecode);
}
```
Expand All @@ -84,7 +84,7 @@ import 'dart:io';
import 'package:dart_eval/dart_eval.dart';
void main() {
final file = File('program.dbc');
final file = File('program.evc');
final bytecode = file
.readAsBytesSync()
.buffer
Expand Down Expand Up @@ -171,7 +171,7 @@ Then, the compiler looks at each of the declarations in turn, and recursively co
to a linear bytecode format.

For evaluation dart_eval uses Dart's optimized dynamic dispatch. This means each bytecode
is actually a class implementing `DbcOp` and we call its `run()` method to execute it.
is actually a class implementing `EvcOp` and we call its `run()` method to execute it.
Bytecodes can do things like push and pop values on the stack, add numbers, and jump to
other places in the program, as well as more complex Dart-specific operations like
create a class.
Expand Down
60 changes: 36 additions & 24 deletions example/dart_eval_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ void main(List<String> args) {
// 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);
..registerBridgeFunc('package:example/bridge.dart', 'WorldTimeTracker.', $WorldTimeTracker$bridge.$new,
isBridge: true);

// Call runtime.setup() after registering all bridge functions
runtime.setup();
Expand Down Expand Up @@ -104,19 +105,25 @@ class $TimestampedTime implements TimestampedTime, $Instance {
static const $type = BridgeTypeRef.spec(BridgeTypeSpec('package:example/bridge.dart', 'TimestampedTime'));

/// Define the compile-time class declaration and map out all the fields and methods for the compiler.
static const $declaration = BridgeClassDef(BridgeClassType($type), constructors: {
// Define the default constructor with an empty string
'': BridgeConstructorDef(BridgeFunctionDef(returns: BridgeTypeAnnotation($type), params: [
// Parameters using built-in types can use [RuntimeTypes] for the most common types. Others, like
// Future, may need to use a type spec for 'dart:core'.
BridgeParameter('utcTime', BridgeTypeAnnotation(BridgeTypeRef.type(RuntimeTypes.intType)), false)
], namedParams: [
BridgeParameter('timezoneOffset', BridgeTypeAnnotation(BridgeTypeRef.type(RuntimeTypes.intType)), true)
]))
}, methods: {}, getters: {}, setters: {}, fields: {
'utcTime': BridgeFieldDef(BridgeTypeAnnotation(BridgeTypeRef.type(RuntimeTypes.intType))),
'timezoneOffset': BridgeFieldDef(BridgeTypeAnnotation(BridgeTypeRef.type(RuntimeTypes.intType)))
});
static const $declaration = BridgeClassDef(BridgeClassType($type),
constructors: {
// Define the default constructor with an empty string
'': BridgeConstructorDef(BridgeFunctionDef(returns: BridgeTypeAnnotation($type), params: [
// Parameters using built-in types can use [RuntimeTypes] for the most common types. Others, like
// Future, may need to use a type spec for 'dart:core'.
BridgeParameter('utcTime', BridgeTypeAnnotation(BridgeTypeRef.type(RuntimeTypes.intType)), false)
], namedParams: [
BridgeParameter('timezoneOffset', BridgeTypeAnnotation(BridgeTypeRef.type(RuntimeTypes.intType)), true)
]))
},
methods: {},
getters: {},
setters: {},
fields: {
'utcTime': BridgeFieldDef(BridgeTypeAnnotation(BridgeTypeRef.type(RuntimeTypes.intType))),
'timezoneOffset': BridgeFieldDef(BridgeTypeAnnotation(BridgeTypeRef.type(RuntimeTypes.intType)))
},
wrap: true);

/// Define static [EvalCallableFunc] functions for all static methods and constructors. This is for the
/// default constructor and is what the runtime will use to create an instance of this class.
Expand Down Expand Up @@ -184,16 +191,21 @@ class $WorldTimeTracker$bridge with $Bridge<WorldTimeTracker> implements WorldTi
static const _$type = BridgeTypeRef.spec(BridgeTypeSpec('package:example/bridge.dart', 'WorldTimeTracker'));

/// Define the compile-time class declaration and map out all the fields and methods for the compiler.
static const $declaration = BridgeClassDef(BridgeClassType(_$type, isAbstract: true), constructors: {
// Even though this class is abstract, we currently need to define the default constructor anyway. This
// may change in the future.
'': BridgeConstructorDef(BridgeFunctionDef(returns: BridgeTypeAnnotation(_$type), params: [], namedParams: []))
}, methods: {
'getTimeFor': BridgeMethodDef(BridgeFunctionDef(
returns: BridgeTypeAnnotation($TimestampedTime.$type),
params: [BridgeParameter('country', BridgeTypeAnnotation(BridgeTypeRef.type(RuntimeTypes.stringType)), false)],
namedParams: []))
}, getters: {}, setters: {}, fields: {});
static const $declaration = BridgeClassDef(BridgeClassType(_$type, isAbstract: true),
constructors: {
// Even though this class is abstract, we currently need to define the default constructor anyway. This
// may change in the future.
'': BridgeConstructorDef(BridgeFunctionDef(returns: BridgeTypeAnnotation(_$type), params: [], namedParams: []))
},
methods: {
'getTimeFor': BridgeMethodDef(BridgeFunctionDef(returns: BridgeTypeAnnotation($TimestampedTime.$type), params: [
BridgeParameter('country', BridgeTypeAnnotation(BridgeTypeRef.type(RuntimeTypes.stringType)), false)
], namedParams: []))
},
getters: {},
setters: {},
fields: {},
bridge: true);

/// Define static [EvalCallableFunc] functions for all static methods and constructors. This is for the
/// default constructor and is what the runtime will use to create an instance of this class.
Expand Down

0 comments on commit 06dda6a

Please sign in to comment.