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

Fixed bridge subclass issue and Iterable functions #206

Open
wants to merge 7 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
4 changes: 2 additions & 2 deletions lib/src/eval/compiler/compiler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ class Compiler implements BridgeDeclarationRegistry, EvalPluginRegistry {
// Iterate over bridge libraries
for (final bridgeLibrary in _bridgeDeclarations.keys) {
// Wrap bridge declarations in this library as [DeclarationOrBridge]s

final bridgeLibDeclarations = [
for (final bridgeDeclaration in _bridgeDeclarations[bridgeLibrary]!)
DeclarationOrBridge(-1, bridge: bridgeDeclaration)
Expand Down Expand Up @@ -412,7 +413,6 @@ class Compiler implements BridgeDeclarationRegistry, EvalPluginRegistry {
_ctx.instanceDeclarationsMap = _instanceDeclarationsMap;
_ctx.visibleDeclarations = visibleDeclarationsByIndex;
_ctx.visibleTypes = visibleTypesByIndex;

unboxedAcrossFunctionBoundaries = {
CoreTypes.int.ref(_ctx),
CoreTypes.double.ref(_ctx),
Expand All @@ -422,6 +422,7 @@ class Compiler implements BridgeDeclarationRegistry, EvalPluginRegistry {

for (final library in reachableLibraries) {
final libraryIndex = libraryIndexMap[library]!;
// print(library.uri.toString());
for (final dec in library.declarations) {
if (dec.isBridge) {
final bridge = dec.bridge;
Expand Down Expand Up @@ -560,7 +561,6 @@ class Compiler implements BridgeDeclarationRegistry, EvalPluginRegistry {
if (!_instanceDeclarationsMap.containsKey(libraryIndex)) {
_instanceDeclarationsMap[libraryIndex] = {};
}

if (declarationOrBridge.isBridge) {
final bridge = declarationOrBridge.bridge!;
if (bridge is BridgeClassDef) {
Expand Down
72 changes: 70 additions & 2 deletions lib/src/eval/compiler/declaration/constructor.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:analyzer/dart/ast/ast.dart';
import 'package:dart_eval/dart_eval_bridge.dart';
import 'package:dart_eval/src/eval/bridge/declaration.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';
Expand Down Expand Up @@ -183,7 +184,7 @@ void compileConstructorDeclaration(
return;
}

final $extends = parent is EnumDeclaration
var $extends = parent is EnumDeclaration
? null
: (parent as ClassDeclaration).extendsClause;
Variable $super;
Expand All @@ -193,6 +194,7 @@ void compileConstructorDeclaration(
final namedArgTypes = <String, TypeRef?>{};

var constructorName = $superInitializer?.constructorName?.name ?? '';
bool doesExtendBridge = false;

if ($extends == null) {
$super = BuiltinValue().push(ctx);
Expand All @@ -201,6 +203,14 @@ void compileConstructorDeclaration(
.visibleDeclarations[ctx.library]![$extends.superclass.name2.lexeme]!;

final decl = extendsWhat.declaration!;
if (!decl.isBridge) {
final checkResults = _doesExtendBridge(decl, ctx);
if (checkResults.extendsBridge) {
doesExtendBridge = true;
extendsWhat = checkResults.declaration;
$extends = checkResults.extendsClause;
}
}

if (decl.isBridge) {
ctx.pushOp(PushBridgeSuperShim.make(), PushBridgeSuperShim.length);
Expand Down Expand Up @@ -346,8 +356,13 @@ void compileConstructorDeclaration(
ctx.bridgeStaticFunctionIndices[decl.sourceLib]![
'${$extends.superclass.name2.value()}.$constructorName']!);
ctx.pushOp(op, BridgeInstantiate.len(op));

final bridgeInst = Variable.alloc(ctx, CoreTypes.dynamic.ref(ctx));

if (doesExtendBridge) {
ctx.pushOp(PushBridgeSuperShim.make(), PushBridgeSuperShim.length);
$super = Variable.alloc(ctx, CoreTypes.dynamic.ref(ctx));
}
ctx.pushOp(
ParentBridgeSuperShim.make(
$super.scopeFrameOffset, bridgeInst.scopeFrameOffset),
Expand Down Expand Up @@ -376,11 +391,12 @@ void compileDefaultConstructor(CompilerContext ctx,
final fieldIndices = _getFieldIndices(fields);
final fieldIdx = fieldIndices.length;

final $extends = parent is EnumDeclaration
var $extends = parent is EnumDeclaration
? null
: (parent as ClassDeclaration).extendsClause;
Variable $super;
DeclarationOrPrefix? extendsWhat;
bool doesExtendBridge = false;

final argTypes = <TypeRef?>[];
final namedArgTypes = <String, TypeRef?>{};
Expand All @@ -395,6 +411,15 @@ void compileDefaultConstructor(CompilerContext ctx,

final decl = extendsWhat.declaration!;

if (!decl.isBridge) {
final checkResults = _doesExtendBridge(decl, ctx);
if (checkResults.extendsBridge) {
doesExtendBridge = true;
extendsWhat = checkResults.declaration;
$extends = checkResults.extendsClause;
}
}

if (decl.isBridge) {
ctx.pushOp(PushBridgeSuperShim.make(), PushBridgeSuperShim.length);
$super = Variable.alloc(ctx, CoreTypes.dynamic.ref(ctx));
Expand Down Expand Up @@ -472,6 +497,11 @@ void compileDefaultConstructor(CompilerContext ctx,
ctx.pushOp(op, BridgeInstantiate.len(op));
final bridgeInst = Variable.alloc(ctx, CoreTypes.dynamic.ref(ctx));

if (doesExtendBridge) {
ctx.pushOp(PushBridgeSuperShim.make(), PushBridgeSuperShim.length);
$super = Variable.alloc(ctx, CoreTypes.dynamic.ref(ctx));
}

ctx.pushOp(
ParentBridgeSuperShim.make(
$super.scopeFrameOffset, bridgeInst.scopeFrameOffset),
Expand Down Expand Up @@ -518,3 +548,41 @@ void _compileUnusedFields(CompilerContext ctx, List<FieldDeclaration> fields,
}
}
}

_BridgeCheckResult _doesExtendBridge(
DeclarationOrBridge? decl, CompilerContext ctx) {
if (decl != null && decl.declaration is ClassDeclaration) {
var classDecl = decl.declaration as ClassDeclaration;
var superClassName = classDecl.extendsClause?.superclass.name2.lexeme;
var superClassDecl =
ctx.visibleDeclarations[decl.sourceLib]?[superClassName];

if (superClassDecl != null && superClassDecl.declaration!.isBridge) {
return _BridgeCheckResult(
declaration: superClassDecl,
extendsClause: classDecl.extendsClause,
);
} else if (superClassDecl == null || superClassDecl.declaration == null) {
return _BridgeCheckResult.fail();
}

return _doesExtendBridge(superClassDecl.declaration!, ctx);
}
return _BridgeCheckResult.fail();
}

class _BridgeCheckResult {
final bool extendsBridge;
final DeclarationOrPrefix? declaration;
final ExtendsClause? extendsClause;

const _BridgeCheckResult.fail()
: extendsBridge = false,
declaration = null,
extendsClause = null;

const _BridgeCheckResult({
required this.declaration,
required this.extendsClause,
}) : extendsBridge = true;
}
3 changes: 1 addition & 2 deletions lib/src/eval/runtime/ops/bridge.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class BridgeInstantiate implements EvcOp {
@override
void run(Runtime runtime) {
final $subclass = runtime.frame[_subclass] as $Instance?;

final _args = runtime.args;
final _argsLen = _args.length;

Expand All @@ -32,9 +31,9 @@ class BridgeInstantiate implements EvcOp {
final instance =
runtime._bridgeFunctions[_constructor](runtime, null, _mappedArgs)
as $Instance;

Runtime.bridgeData[instance] =
BridgeData(runtime, $runtimeType, $subclass ?? BridgeDelegatingShim());

runtime.frame[runtime.frameOffset++] = instance;
}

Expand Down
1 change: 0 additions & 1 deletion lib/src/eval/runtime/ops/objects.dart
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ class CreateClass implements EvcOp {
void run(Runtime runtime) {
final $super = runtime.frame[_super] as $Instance?;
final $cls = runtime.declaredClasses[_library]![_name]!;

final instance = $InstanceImpl($cls, $super, List.filled(_valuesLen, null));
runtime.frame[runtime.frameOffset++] = instance;
}
Expand Down
3 changes: 1 addition & 2 deletions lib/src/eval/shared/stdlib/core/base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import 'package:dart_eval/src/eval/runtime/exception.dart';
import 'package:dart_eval/src/eval/shared/stdlib/core/collection.dart';
import 'package:dart_eval/src/eval/shared/stdlib/core/object.dart';
import 'package:dart_eval/src/eval/shared/stdlib/core/pattern.dart';
import 'package:dart_eval/src/eval/utils/wap_helper.dart';
import 'num.dart';

const $dynamicCls = BridgeClassDef(
Expand Down Expand Up @@ -236,7 +235,7 @@ class $String implements $Instance {
case 'codeUnitAt':
return __codeUnitAt;
case 'codeUnits':
return wrapList<int>($value.codeUnits, (e) => $int(e));
return $List.wrap($value.codeUnits.map((e) => $int(e)).toList());
case 'compareTo':
return __compareTo;
case 'contains':
Expand Down
Loading
Loading