-
Notifications
You must be signed in to change notification settings - Fork 314
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
Showing
77 changed files
with
2,462 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,12 @@ | ||
# fair_dart2js | ||
|
||
fair_compiler 的配套编译器,用于将 Dart 转为 JS。 | ||
fair_compiler 的配套编译器,用于将 Dart 转为 JS。 | ||
|
||
### 开发功能记录 | ||
* try、catch | ||
* 不支持 on 函数 例如 on Exception catch (e,s),此类写法将会抛弃处理,请直接写 catch (e) | ||
|
||
### 待开发 | ||
#### 原生函数转换 | ||
* Exception |
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,22 @@ | ||
import 'package:fair_dart2js/src/covert/convertFunction.dart'; | ||
/** | ||
* 正常测试 try catch | ||
* */ | ||
|
||
String a = convertFunction(''' | ||
void testTry() { | ||
try { | ||
int a=1; | ||
int b=2; | ||
int c=a+b; | ||
print("1"); | ||
throw FormatException('Expected at least 1 section'); | ||
} catch (e,s) { | ||
print('111'+e); | ||
} | ||
} | ||
'''); | ||
|
||
void main() { | ||
print(a); | ||
} |
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,25 @@ | ||
import 'package:fair_dart2js/src/covert/convertFunction.dart'; | ||
|
||
/** | ||
* 测试 on 函数将被丢弃 | ||
* */ | ||
|
||
String a = convertFunction(''' | ||
void testTry() { | ||
try { | ||
int a=1; | ||
int b=2; | ||
int c=a+b; | ||
print("1"); | ||
throw FormatException('Expected at least 1 section'); | ||
} on Exception catch (e,s) { | ||
print('111'+e); | ||
} catch (e) { | ||
print('222'+e); | ||
} | ||
} | ||
'''); | ||
|
||
void main() { | ||
print(a); | ||
} |
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,21 @@ | ||
import 'package:fair_dart2js/src/covert/convertFunction.dart'; | ||
|
||
/** | ||
* 测试 finally 处理 | ||
* */ | ||
|
||
String a = convertFunction(''' | ||
void testTry() { | ||
try { | ||
int a=1; | ||
} catch (e,s) { | ||
print('catch:'+e); | ||
} finally{ | ||
print('finally...'); | ||
} | ||
} | ||
'''); | ||
|
||
void main() { | ||
print(a); | ||
} |
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 |
---|---|---|
|
@@ -1754,3 +1754,5 @@ String uglify(String str) { | |
} | ||
return buf.toString(); | ||
} | ||
|
||
|
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,14 @@ | ||
export 'covert/convertClassString.dart'; | ||
export 'covert/convertExpression.dart'; | ||
export 'covert/convertFunction.dart'; | ||
export 'covert/convertArrayFuncExpression.dart'; | ||
export 'covert/convertBlock.dart'; | ||
export 'covert/convertFunctionExpression.dart'; | ||
export 'covert/convertStatements.dart'; | ||
export 'covert/convertWidgetStateFile.dart'; | ||
export 'covert/convertFunctionFromData.dart'; | ||
export 'declaration/ClassDeclarationData.dart'; | ||
export 'declaration/FieldDeclarationData.dart'; | ||
export 'declaration/MethodDeclarationData.dart'; | ||
|
||
|
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,21 @@ | ||
import 'package:analyzer/dart/ast/ast.dart'; | ||
|
||
import '../node/ArrowFunctionExpressionNode.dart'; | ||
import '../node/GenericStatementNode.dart'; | ||
import 'convertExpression.dart'; | ||
|
||
String convertArrayFuncExpression(FunctionExpression code) { | ||
var body = code.body as ExpressionFunctionBody; | ||
if (body.functionDefinition.toString() == '=>') { | ||
var gnNode = ArrowFunctionExpressionNode(); | ||
// TODO: 支持命名参数、可选参数 | ||
code.parameters?.parameters.forEach((element) { | ||
gnNode.argumentList.add([element.identifier.toString()]); | ||
}); | ||
gnNode.body.push( | ||
GenericStatementNode(convertExpression(body.expression.toString()))); | ||
return gnNode.toSource(); | ||
} else { | ||
throw 'error'; | ||
} | ||
} |
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,22 @@ | ||
import 'package:analyzer/dart/analysis/utilities.dart'; | ||
|
||
import '../visitor/SimpleFunctionGenerator.dart'; | ||
import '../funs/shouldErrorBeIgnored.dart'; | ||
|
||
String convertBlock(String code) { | ||
// print("[convertBlock]" + code); | ||
var res = parseString( | ||
content: '''dummy() async $code''', throwIfDiagnostics: false); | ||
|
||
if (res.errors.isNotEmpty) { | ||
if (shouldErrorBeIgnored(res.errors)) { | ||
// ignore | ||
} else { | ||
throw ArgumentError(); | ||
} | ||
} | ||
|
||
var generator = SimpleFunctionGenerator(); | ||
res.unit.visitChildren(generator); | ||
return generator.func?.body.toSource() ?? ''; | ||
} |
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,12 @@ | ||
import 'package:analyzer/dart/analysis/features.dart'; | ||
import 'package:analyzer/dart/analysis/utilities.dart'; | ||
|
||
import '../visitor/ClassDeclarationVisitor.dart'; | ||
|
||
String convertClassString(String content, [bool isDataBean = false]) { | ||
var result = | ||
parseString(content: content, featureSet: FeatureSet.fromEnableFlags([])); | ||
var visitor = ClassDeclarationVisitor(isDataBean); | ||
result.unit.visitChildren(visitor); | ||
return visitor.genJsCode(); | ||
} |
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,22 @@ | ||
import 'convertStatements.dart'; | ||
|
||
String convertExpression(String code) { | ||
// print("[convertExpression]" + code); | ||
var res = ''; | ||
var start = 0; | ||
try { | ||
res = convertStatements(code + ';'); | ||
res = res.trim(); | ||
} on ArgumentError { | ||
// 有些表达式直接变成语句会报错,例如字典字面量对象 | ||
var prefix = 'var __variable__ = '; | ||
res = convertStatements('''$prefix$code;'''); | ||
res = res.trim(); | ||
start = res.indexOf('=') + 1; | ||
} | ||
var end = res.length - 1; | ||
while (end >= 0 && RegExp(r'[\s\r\n;]', multiLine: false).hasMatch(res[end])) { | ||
end--; | ||
} | ||
return res.substring(start, end + 1); | ||
} |
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,16 @@ | ||
import 'package:analyzer/dart/analysis/utilities.dart'; | ||
|
||
import '../visitor/SimpleFunctionGenerator.dart'; | ||
|
||
String convertFunction(String code, | ||
{bool isArrow = false, | ||
bool isClassMethod = false, | ||
bool classHasStaticFields = false}) { | ||
var res = parseString(content: code); | ||
var generator = SimpleFunctionGenerator(isArrowFunc: isArrow); | ||
res.unit.visitChildren(generator); | ||
generator.func | ||
?..withContext = isClassMethod | ||
..classHasStaticFields = classHasStaticFields; | ||
return generator.func?.toSource() ?? ''; | ||
} |
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,6 @@ | ||
import 'convertFunction.dart'; | ||
|
||
String convertFunctionExpression(String code) { | ||
code = 'dummy' + code; | ||
return convertFunction(code); | ||
} |
Oops, something went wrong.