-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d2ff728
commit 18999d0
Showing
13 changed files
with
193 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,61 @@ | ||
import 'package:analyzer/dart/ast/ast.dart'; | ||
import 'package:dart_eval/src/eval/compiler/builtins.dart'; | ||
import 'package:dart_eval/src/eval/compiler/context.dart'; | ||
import 'package:dart_eval/src/eval/compiler/errors.dart'; | ||
import 'package:dart_eval/src/eval/compiler/expression/expression.dart'; | ||
import 'package:dart_eval/src/eval/compiler/scope.dart'; | ||
import 'package:dart_eval/src/eval/compiler/type.dart'; | ||
import 'package:dart_eval/src/eval/runtime/runtime.dart'; | ||
|
||
void compileFieldDeclaration(int fieldIndex, FieldDeclaration d, CompilerContext ctx, ClassDeclaration parent) { | ||
var _fieldIndex = fieldIndex; | ||
for (final field in d.fields.variables) { | ||
final pos = beginMethod(ctx, d, d.offset, parent.name.name + '.' + field.name.name + ' (get)'); | ||
ctx.pushOp(PushObjectPropertyImpl.make(0, _fieldIndex), PushObjectPropertyImpl.LEN); | ||
ctx.pushOp(Return.make(1), Return.LEN); | ||
ctx.instanceDeclarationPositions[ctx.library]![parent.name.name]![0][field.name.name] = pos; | ||
|
||
if (!(field.isFinal || field.isConst)) { | ||
final setterPos = beginMethod(ctx, d, d.offset, parent.name.name + '.' + field.name.name + ' (set)'); | ||
ctx.pushOp(SetObjectPropertyImpl.make(0, _fieldIndex, 1), SetObjectPropertyImpl.LEN); | ||
if (d.isStatic) { | ||
final initializer = field.initializer; | ||
if (initializer != null) { | ||
final pos = beginMethod(ctx, field, field.offset, parent.name.name + '.' + field.name.name + ' (init)'); | ||
var V = compileExpression(initializer, ctx); | ||
TypeRef type; | ||
final specifiedType = d.fields.type; | ||
if (specifiedType != null) { | ||
type = TypeRef.fromAnnotation(ctx, ctx.library, specifiedType); | ||
if (!V.type.isAssignableTo(ctx, type)) { | ||
throw CompileError( | ||
'Static field ${parent.name.name}.${field.name.name} of inferred type ${V.type} ' | ||
'does not conform to type $type'); | ||
} | ||
} else { | ||
type = V.type; | ||
} | ||
if (!unboxedAcrossFunctionBoundaries.contains(type)) { | ||
V = V.boxIfNeeded(ctx); | ||
type = type.copyWith(boxed: true); | ||
} else { | ||
V = V.unboxIfNeeded(ctx); | ||
type = type.copyWith(boxed: false); | ||
} | ||
final _name = '${parent.name.name}.${field.name.name}'; | ||
final _index = ctx.topLevelGlobalIndices[ctx.library]![_name]!; | ||
ctx.pushOp(SetGlobal.make(_index, V.scopeFrameOffset), SetGlobal.LEN); | ||
ctx.topLevelVariableInferredTypes[ctx.library]![_name] = type; | ||
ctx.topLevelGlobalInitializers[ctx.library]![_name] = pos; | ||
ctx.runtimeGlobalInitializerMap[_index] = pos; | ||
ctx.pushOp(Return.make(V.scopeFrameOffset), Return.LEN); | ||
} | ||
} else { | ||
final pos = beginMethod(ctx, d, d.offset, parent.name.name + '.' + field.name.name + ' (get)'); | ||
ctx.pushOp(PushObjectPropertyImpl.make(0, _fieldIndex), PushObjectPropertyImpl.LEN); | ||
ctx.pushOp(Return.make(1), Return.LEN); | ||
ctx.instanceDeclarationPositions[ctx.library]![parent.name.name]![1][field.name.name] = setterPos; | ||
} | ||
ctx.instanceDeclarationPositions[ctx.library]![parent.name.name]![0][field.name.name] = pos; | ||
|
||
_fieldIndex++; | ||
if (!(field.isFinal || field.isConst)) { | ||
final setterPos = beginMethod(ctx, d, d.offset, parent.name.name + '.' + field.name.name + ' (set)'); | ||
ctx.pushOp(SetObjectPropertyImpl.make(0, _fieldIndex, 1), SetObjectPropertyImpl.LEN); | ||
ctx.pushOp(Return.make(1), Return.LEN); | ||
ctx.instanceDeclarationPositions[ctx.library]![parent.name.name]![1][field.name.name] = setterPos; | ||
} | ||
|
||
_fieldIndex++; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
part of 'collection.dart'; | ||
|
||
class $Map<K, V> implements $Instance { | ||
$Map(String id, Map<K, V> value) : $value = runtimeOverride(id) as Map<K, V>? ?? value; | ||
|
||
$Map.wrap(this.$value); | ||
|
||
@override | ||
final Map<K, V> $value; | ||
|
||
late final $Object $super = $Object($value); | ||
|
||
@override | ||
$Value? $getProperty(Runtime runtime, String identifier) { | ||
switch (identifier) { | ||
case '[]': | ||
return __indexGet; | ||
case '[]=': | ||
return __indexSet; | ||
case 'length': | ||
return $int($value.length); | ||
} | ||
return $super.$getProperty(runtime, identifier); | ||
} | ||
|
||
@override | ||
void $setProperty(Runtime runtime, String identifier, $Value value) { | ||
throw EvalUnknownPropertyException(identifier); | ||
} | ||
|
||
static const $Function __indexGet = $Function(_indexGet); | ||
|
||
static $Value? _indexGet(Runtime runtime, $Value? target, List<$Value?> args) { | ||
final idx = args[0]!; | ||
return (target!.$value as Map)[idx.$value]; | ||
} | ||
|
||
static const $Function __indexSet = $Function(_indexSet); | ||
|
||
static $Value? _indexSet(Runtime runtime, $Value? target, List<$Value?> args) { | ||
final idx = args[0]!; | ||
final value = args[1]!; | ||
return (target!.$value as Map)[idx.$value] = value; | ||
} | ||
|
||
@override | ||
Map get $reified => $value.map((k, v) => MapEntry(k is $Value ? k.$reified : k, v is $Value ? v.$reified : v)); | ||
|
||
@override | ||
int get $runtimeType => RuntimeTypes.mapType; | ||
} |