Skip to content

Commit

Permalink
1. fix equal call bug
Browse files Browse the repository at this point in the history
2. change testcase struct,add testcase2
3. some ops modified to single instance
  • Loading branch information
lancexin committed Jan 14, 2025
1 parent 624b0fc commit e876dd0
Show file tree
Hide file tree
Showing 208 changed files with 2,277 additions and 19,215 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

[中文](README_cn.md)

# **This project is still in the development stage and there are still many bugs. It is not recommended to use it in the project. Currently, it only serves as a learning reference**

# Introduce
Expand Down
2 changes: 1 addition & 1 deletion README_cn.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

[English](README.md)
# **这个项目仍然在开发阶段,bug还很多,不建议在项目中用,目前只有学习参考意义**

# 介绍
Expand Down
1 change: 1 addition & 0 deletions examples/animations/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,4 @@
!**/ios/**/default.pbxuser
!**/ios/**/default.perspectivev3
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages
pubspec.lock
1 change: 1 addition & 0 deletions examples/dart_example_1/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# https://dart.dev/guides/libraries/private-files
# Created by `dart pub`
.dart_tool/
pubspec.lock
1 change: 1 addition & 0 deletions examples/flutter_animations/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ app.*.map.json
/android/app/debug
/android/app/profile
/android/app/release
pubspec.lock
1 change: 1 addition & 0 deletions examples/flutter_animations_1/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ app.*.map.json
/android/app/debug
/android/app/profile
/android/app/release
pubspec.lock
1 change: 1 addition & 0 deletions examples/flutter_animations_2/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ app.*.map.json
/android/app/debug
/android/app/profile
/android/app/release
pubspec.lock
1 change: 1 addition & 0 deletions examples/flutter_example/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ app.*.map.json
/android/app/debug
/android/app/profile
/android/app/release
pubspec.lock
1 change: 1 addition & 0 deletions examples/flutter_example_1/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ app.*.map.json
/android/app/debug
/android/app/profile
/android/app/release
pubspec.lock
1 change: 1 addition & 0 deletions examples/flutter_example_2/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ app.*.map.json
/android/app/debug
/android/app/profile
/android/app/release
pubspec.lock
1 change: 1 addition & 0 deletions examples/flutter_example_gallery/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ app.*.map.json
/android/app/debug
/android/app/profile
/android/app/release
pubspec.lock
1 change: 1 addition & 0 deletions examples/flutter_example_gallery_1/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ app.*.map.json
/android/app/debug
/android/app/profile
/android/app/release
pubspec.lock
1 change: 1 addition & 0 deletions examples/flutter_example_gallery_2/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ app.*.map.json
/android/app/debug
/android/app/profile
/android/app/release
pubspec.lock
1 change: 1 addition & 0 deletions examples/flutter_plugin_1/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ migrate_working_dir/
.dart_tool/
.packages
build/
pubspec.lock
1 change: 1 addition & 0 deletions examples/flutter_plugin_2/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ migrate_working_dir/
.dart_tool/
.packages
build/
pubspec.lock
1 change: 1 addition & 0 deletions examples/flutter_plugin_gallery/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ migrate_working_dir/
.dart_tool/
.packages
build/
pubspec.lock
Binary file modified micro_dart.dart.snapshot
Binary file not shown.
10 changes: 10 additions & 0 deletions micro_dart_compiler/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,13 @@

# Conventional directory for build output.
build/

.flutter-plugins
.flutter-plugins-dependencies

pubspec.lock

test/testcases/**/_*.txt
test/testcases/**/_*.json
test/testcases2/**/_*.txt
test/testcases2/**/_*.json
52 changes: 42 additions & 10 deletions micro_dart_compiler/bin/micro_dart_compiler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,25 @@ import 'package:micro_dart_compiler/micro_dart_compiler.dart';
import 'package:pubspec_parse/pubspec_parse.dart';

final ArgParser argParser = ArgParser(allowTrailingOptions: true)
..addOption('out', help: 'data out path default is ./YourPackageName.data')
..addOption('json-ast-out', help: 'json ast out path')
..addOption('ast-out', help: 'ast out path')
..addOption('op-out', help: 'op out path')
..addOption('external-methods-out', help: 'external methods out path ')
..addOption('out', help: 'data out path,default is ./YourPackageName.data')
..addOption('json-ast-out', help: 'json ast out path,it is optional ')
..addOption('ast-out', help: 'ast out path,it is optional')
..addOption('op-out', help: 'op out path,it is optional')
..addOption('constants-out', help: 'constant out path,it is optional')
..addOption('external-methods-out',
help: 'external methods out path,it is optional ')
..addOption('types-out', help: 'types out path,it is optional')
..addOption('declarations-out', help: 'declarations out path,it is optional')
..addFlag('verbose',
help: 'Enables verbose output from the compiler.', defaultsTo: false);
help: 'verbose output from the compiler', defaultsTo: false);

String usage = '''
Usage: micro_dart [--out|--json-ast-out|--ast-out|--external-methods-out] [--verbose] [lib/main.dart]
Usage: micro_dart [--out|--json-ast-out|--ast-out|--external-methods-out|--op-out|--constants-out|--types-out|--declarations-out] [--verbose] [lib/main.dart]
Example1: dart run micro_dart.dart.snapshot --out micro_dart.data examples/flutter_plugin_2/lib/plugin_2.dart
Example2: dart run micro_dart.dart.snapshot --out micro_dart.data --json-ast-out json-ast-out.txt --ast-out ast-out.txt --external-methods-out external-methods-out.txt --op-out op-out.txt --constants-out constants-out.txt --types-out types-out.txt --declarations-out declarations-out.txt --verbose examples/flutter_plugin_2/lib/plugin_2.dart
Example: dart run micro_dart.dart.snapshot --out examples/flutter_plugin_2/assets/micro_dart.data --verbose examples/flutter_plugin_2/lib/plugin_2.dart
Options:
${argParser.usage}
''';
Expand Down Expand Up @@ -62,6 +69,9 @@ Future<int> main(List<String> args) async {
String? astOut = options["ast-out"];
String? externalMethodsOut = options["external-methods-out"];
String? opOut = options["op-out"];
String? constantOut = options["constants-out"];
String? typesOut = options["types-out"];
String? declarationsOut = options["declarations-out"];

RegExp regExp = RegExp(compileRegex);

Expand All @@ -82,6 +92,8 @@ Future<int> main(List<String> args) async {
print("ast-out: $astOut");
print("external-methods-out: $externalMethodsOut");
print("op-out: $opOut");
print("types-out: $typesOut");
print("declarations-out: $declarationsOut");
}

String sdkSummaryPath = sdkSummary.toFilePath(windows: Platform.isWindows);
Expand Down Expand Up @@ -111,15 +123,35 @@ Future<int> main(List<String> args) async {
if (astOut != null) {
writeComponentToText(program.component!, path: astOut);
}

if (externalMethodsOut != null) {
File(externalMethodsOut)
.writeAsStringSync(program.getExternalCallMethods());
}

if (opOut != null) {
if (opOut != null ||
constantOut != null ||
typesOut != null ||
declarationsOut != null) {
var engine = MicroDartEngine.fromData(bytes);
File(opOut).writeAsStringSync(engine.getOpcodes());

if (opOut != null) {
File(opOut).writeAsStringSync(engine.getOpcodes());
}

if (constantOut != null) {
File(constantOut).writeAsStringSync(engine.getConstants());
}

if (typesOut != null) {
File(typesOut).writeAsStringSync(engine.getTypes());
}

if (declarationsOut != null) {
File(declarationsOut).writeAsStringSync(engine.getDeclarations());
}
}

return 0;
}

Expand Down
6 changes: 3 additions & 3 deletions micro_dart_compiler/lib/compiler/ast/arguments.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ void compileArguments(
OpPushConstant.make(context.constantPool.addOrGet(element.name)));
});
context.pushOp(OpPushConstantInt.make(pLength));
int argmentLength =
int argumentLength =
arguments.positional.length + arguments.named.length * 2 + 2;
if (!isStatic) {
argmentLength++;
argumentLength++;
}
context.pushOp(OpPushArgments.make(argmentLength));
context.pushOp(OpPushArguments.make(argumentLength));
}
3 changes: 2 additions & 1 deletion micro_dart_compiler/lib/compiler/ast/ast.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import 'package:kernel/kernel.dart';
import 'package:kernel/ast.dart';
import 'package:micro_dart_compiler/util.dart';

import 'package:micro_dart_runtime/micro_dart_runtime.dart';

import '../offset_tracker.dart';
import '../context.dart';

part 'namednode.dart';
part 'named_node.dart';
part 'statement.dart';
part 'expression.dart';
part 'arguments.dart';
Expand Down
2 changes: 1 addition & 1 deletion micro_dart_compiler/lib/compiler/ast/ast_to_json.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ void astToJson(String fileName, RegExp pluginUri, Component? component) {
var map = _Visitor(pluginUri).visitComponent(component);
StringBuffer buffer = StringBuffer();
buffer.write(jsonEncode(map));
File("$fileName.json").writeAsStringSync('${buffer.toString()}');
File("$fileName").writeAsStringSync('${buffer.toString()}');
}

List<R>? visitList<R>(List<Node>? nodes, Visitor<R> visitor) {
Expand Down
16 changes: 8 additions & 8 deletions micro_dart_compiler/lib/compiler/ast/constructor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ int compileConstructor(MicroCompilerContext context, Constructor node) {
context.startCompileNode(node);
var ref = node.getCallRef();

if (context.rumtimeDeclarationOpIndexes[ref] != null) {
return context.rumtimeDeclarationOpIndexes[ref]!;
if (context.runtimeDeclarationOpIndexes[ref] != null) {
return context.runtimeDeclarationOpIndexes[ref]!;
}
//开启一个作用域
int pos = context.callStart(ref);
context.rumtimeDeclarationOpIndexes[ref] = pos;
List<String> posationalNames = [];
context.runtimeDeclarationOpIndexes[ref] = pos;
List<String> positionalNames = [];
//参数初始化
node.function.positionalParameters.forEach((element) {
compileVariableDeclaration(context, element);
posationalNames.add(element.name!);
positionalNames.add(element.name!);
});
node.function.namedParameters.forEach((element) {
compileVariableDeclaration(context, element);
});
context.pushOp(OpPopArgments.make(posationalNames, false, true));
context.pushOp(OpPopArguments.make(positionalNames, false, true));
context.pushOp(OpCreateInstance.make(
ClassRef(node.stringLibraryUri, node.stringClassName!)));
context.pushOp(OpSetScopeParam.make("#this"));
Expand Down Expand Up @@ -98,7 +98,7 @@ int compileCallConstructor(MicroCompilerContext context, Arguments arguments,
//将参数压入当前作用域
compileArguments(context, arguments, true);

var classtypeStirngs = arguments.types
var classTypeStrings = arguments.types
.map((e) => compileDartType(context, e))
.map((e) => e?.ref.className ?? "")
.toList();
Expand All @@ -108,7 +108,7 @@ int compileCallConstructor(MicroCompilerContext context, Arguments arguments,
op = OpCallDynamic.make(ref, true, false, false, false, true);
} else {
context.externalCallMethods.add(ref);
op = OpCallExternal.make(ref, true, [], classtypeStirngs);
op = OpCallExternal.make(ref, true, [], classTypeStrings);
}

return context.pushOp(op);
Expand Down
23 changes: 14 additions & 9 deletions micro_dart_compiler/lib/compiler/ast/expression.dart
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ int compileSuperPropertySet(
compileExpression(context, node.value);
context.pushOp(OpPushConstantInt.make(1));
context.pushOp(OpPushConstantInt.make(0));
context.pushOp(OpPushArgments.make(4));
context.pushOp(OpPushArguments.make(4));
if (target is Procedure) {
bool isAsync = (target.function.asyncMarker == AsyncMarker.Async);
if (isAsync) {
Expand Down Expand Up @@ -213,7 +213,7 @@ int compileConstructorTearOff(

int compileStaticTearOff(MicroCompilerContext context, StaticTearOff node) {
var ref = node.target.getCallRef();
int opOffset = context.rumtimeDeclarationOpIndexes[ref] ?? -1;
int opOffset = context.runtimeDeclarationOpIndexes[ref] ?? -1;
bool isAsync = (node.target.function.asyncMarker == AsyncMarker.Async);
int pos = context
.pushOp(OpPushPointer.make(opOffset, node.target.isStatic, isAsync));
Expand Down Expand Up @@ -391,8 +391,12 @@ int compileFunctionTearOff(MicroCompilerContext context, FunctionTearOff node) {

int compileEqualsCall(MicroCompilerContext context, EqualsCall node) {
compileExpression(context, node.left);
compileExpression(context, node.right);
return context.pushOp(OpEquals.make());
//compileExpression(context, node.right);

return compileCallProcedure(
context, Arguments([node.right]), node.interfaceTarget, false);

//return context.pushOp(OpEquals.make());
}

int compileLet(MicroCompilerContext context, Let node) {
Expand All @@ -406,7 +410,7 @@ int compileInstanceTearOff(MicroCompilerContext context, InstanceTearOff node) {
if (context.compileDeclarations.contains(node.interfaceTarget)) {
compileExpression(context, node.receiver);
var ref = node.interfaceTarget.getCallRef();
int opOffset = context.rumtimeDeclarationOpIndexes[ref] ?? -1;
int opOffset = context.runtimeDeclarationOpIndexes[ref] ?? -1;
bool isAsync =
(node.interfaceTarget.function.asyncMarker == AsyncMarker.Async);
int pos =
Expand Down Expand Up @@ -460,7 +464,7 @@ int compileAsExpression(MicroCompilerContext context, AsExpression node) {
//这里表示它是一个外部类
context.pushOp(OpPushConstantInt.make(0));
context.pushOp(OpPushConstantInt.make(0));
context.pushOp(OpPushArgments.make(3));
context.pushOp(OpPushArguments.make(3));

var asRef = CallRef(type.classNode.stringLibraryUri,
type.classNode.stringClassName!, "#as", false, false);
Expand Down Expand Up @@ -545,7 +549,8 @@ int compileSuperPropertyGet(
if (target is Procedure) {
context.pushOp(OpPushConstantInt.make(0));
context.pushOp(OpPushConstantInt.make(0));
context.pushOp(OpPushArgments.make(3));
context.pushOp(OpPushArguments.make(3));

late String className;
//print("compiling node: ${context.compilingNode!.parent}");
bool isMixinDeclaration = false;
Expand Down Expand Up @@ -646,7 +651,7 @@ int compileInstanceGet(MicroCompilerContext context, InstanceGet node) {
} else if (target is Field) {
compileExpression(context, node.receiver);
return compileCallFieldGet(context, target);
//int opOffset = context.rumtimeDeclarationOpIndexes[target.getNamedName()]!;
//int opOffset = context.runtimeDeclarationOpIndexes[target.getNamedName()]!;
// if (opOffset == -1) {
// throw Exception("object ${target.getNamedName()} not found ");
// }
Expand Down Expand Up @@ -732,7 +737,7 @@ int compileConstant(MicroCompilerContext context, Constant constant) {
} else if (constant is StaticTearOffConstant) {
if (context.compileDeclarations.contains(constant.target)) {
var ref = constant.target.getCallRef();
int opOffset = context.rumtimeDeclarationOpIndexes[ref]!;
int opOffset = context.runtimeDeclarationOpIndexes[ref]!;
bool isAsync =
(constant.target.function.asyncMarker == AsyncMarker.Async);
int pos = context.pushOp(OpPushPointer.make(opOffset, true, isAsync));
Expand Down
12 changes: 6 additions & 6 deletions micro_dart_compiler/lib/compiler/ast/field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ int compileField(MicroCompilerContext context, Field node) {
compileDartType(context, node.type);
var ref = node.getCallRef();

if (context.rumtimeDeclarationOpIndexes[ref] != null) {
return context.rumtimeDeclarationOpIndexes[ref]!;
if (context.runtimeDeclarationOpIndexes[ref] != null) {
return context.runtimeDeclarationOpIndexes[ref]!;
}

int pos = context.callStart(ref);
context.rumtimeDeclarationOpIndexes[ref] = pos;
context.runtimeDeclarationOpIndexes[ref] = pos;

if (node.initializer != null) {
compileExpression(context, node.initializer!);
Expand All @@ -27,7 +27,7 @@ int compileCallFieldGet(MicroCompilerContext context, Field field) {

Op? op;
if (context.compileDeclarationIndexes.containsKey(ref)) {
int opOffset = context.rumtimeDeclarationOpIndexes[ref] ?? -1;
int opOffset = context.runtimeDeclarationOpIndexes[ref] ?? -1;
if (field.isStatic) {
int pos = context.pushOp(OpGetGlobalParam.make(ref, opOffset));
if (opOffset == -1) {
Expand All @@ -46,9 +46,9 @@ int compileCallFieldGet(MicroCompilerContext context, Field field) {
context.pushOp(OpPushConstantInt.make(0));
context.pushOp(OpPushConstantInt.make(0));
if (field.isStatic) {
context.pushOp(OpPushArgments.make(2));
context.pushOp(OpPushArguments.make(2));
} else {
context.pushOp(OpPushArgments.make(3));
context.pushOp(OpPushArguments.make(3));
}
context.externalCallMethods.add(ref);
op = OpCallExternal.make(ref, true, [], []);
Expand Down
Loading

0 comments on commit e876dd0

Please sign in to comment.