From 05739627950567885293f42fc4d4661be5e1ac04 Mon Sep 17 00:00:00 2001 From: nixo Date: Tue, 24 Nov 2020 10:27:55 +0100 Subject: [PATCH] Replace unsupported '...' and 'if' in lists --- .../lib/src/analyzer/code_generator.dart | 27 +-- .../lib/src/compiler/shared_compiler.dart | 9 +- pkg/dev_compiler/lib/src/kernel/compiler.dart | 169 ++++++++++-------- 3 files changed, 121 insertions(+), 84 deletions(-) diff --git a/pkg/dev_compiler/lib/src/analyzer/code_generator.dart b/pkg/dev_compiler/lib/src/analyzer/code_generator.dart index 3ff97b0df18..8ab1afa6855 100644 --- a/pkg/dev_compiler/lib/src/analyzer/code_generator.dart +++ b/pkg/dev_compiler/lib/src/analyzer/code_generator.dart @@ -4161,19 +4161,24 @@ class CodeGenerator extends Object } var location = _getLocation(condition.offset); - return js.statement(' if (!#) #.assertFailed(#, #, #, #, #);', [ + var newvar = [ jsCondition, runtimeModule, - if (message == null) - JS.LiteralNull() - else - _visitExpression(message), - js.escapedString(location.sourceUrl.toString()), - // Lines and columns are typically printed with 1 based indexing. - js.number(location.line + 1), - js.number(location.column + 1), - js.escapedString(condition.toSource()), - ]); + ]; + + if (message == null) { + newvar.add(JS.LiteralNull()); + } else { + newvar.add(_visitExpression(message)); + } + newvar.addAll([ + js.escapedString(location.sourceUrl.toString()), + // Lines and columns are typically printed with 1 based indexing. + js.number(location.line + 1), + js.number(location.column + 1), + js.escapedString(condition.toSource()),]); + + return js.statement(' if (!#) #.assertFailed(#, #, #, #, #);', newvar); } @override diff --git a/pkg/dev_compiler/lib/src/compiler/shared_compiler.dart b/pkg/dev_compiler/lib/src/compiler/shared_compiler.dart index 6a3182d0607..86741493a69 100644 --- a/pkg/dev_compiler/lib/src/compiler/shared_compiler.dart +++ b/pkg/dev_compiler/lib/src/compiler/shared_compiler.dart @@ -213,8 +213,13 @@ abstract class SharedCompiler { /// dart.asInt() /// @protected - JS.Expression runtimeCall(String code, [List args]) => - js.call('#.$code', [runtimeModule, ...?args]); + JS.Expression runtimeCall(String code, [List args]) { + var obj = [runtimeModule]; + if (args != null) { + obj.addAll(args); + } + return js.call('#.$code', obj); + } /// Calls [runtimeCall] and uses `toStatement()` to convert the resulting /// expression into a statement. diff --git a/pkg/dev_compiler/lib/src/kernel/compiler.dart b/pkg/dev_compiler/lib/src/kernel/compiler.dart index 531ca405cff..81424212e4c 100644 --- a/pkg/dev_compiler/lib/src/kernel/compiler.dart +++ b/pkg/dev_compiler/lib/src/kernel/compiler.dart @@ -554,9 +554,10 @@ class ProgramCompiler extends Object var genericArgs = [ typeConstructor, - if (deferredBaseClass != null && deferredBaseClass.isNotEmpty) - js.call('(#) => { #; }', [jsFormals, deferredBaseClass]), ]; + if (deferredBaseClass != null && deferredBaseClass.isNotEmpty) { + genericArgs.add(js.call('(#) => { #; }', [jsFormals, deferredBaseClass])); + } var genericCall = runtimeCall('generic(#)', [genericArgs]); @@ -726,11 +727,14 @@ class ProgramCompiler extends Object var jsParams = _emitParameters(ctor.function); _currentUri = savedUri; var name = ctor.name.name; - var ctorBody = [ - if (mixinCtor != null) mixinCtor, - if (name != '' || hasUnnamedSuper) - _emitSuperConstructorCall(className, name, jsParams), + var ctorBody = [ ]; + if (mixinCtor != null) { + ctorBody.add(mixinCtor); + } + if (name != '' || hasUnnamedSuper) { + ctorBody.add(_emitSuperConstructorCall(className, name, jsParams)); + } body.add(_addConstructorToClass( c, className, name, JS.Fun(jsParams, JS.Block(ctorBody)))); } @@ -1294,10 +1298,10 @@ class ProgramCompiler extends Object if (emitMetadata) { var constructors = []; - var allConstructors = [ - ...c.constructors, - ...c.procedures.where((p) => p.isFactory), - ]; + var allConstructors = [ ]; + allConstructors.addAll(c.constructors); + allConstructors.addAll(c.procedures.where((p) => p.isFactory)); + for (var ctor in allConstructors) { var memberName = _constructorName(ctor.name.name); var type = _emitAnnotatedFunctionType( @@ -3032,10 +3036,13 @@ class ProgramCompiler extends Object // (sync*/async/async*). Our code generator assumes it can emit names for // named argument initialization, and sync* functions also emit locally // modified parameters into the function's scope. - var parameterNames = { - for (var p in f.positionalParameters) p.name, - for (var p in f.namedParameters) p.name, - }; + var parameterNames = Set(); + for (var p in f.positionalParameters) { + parameterNames.add(p.name); + } + for (var p in f.namedParameters) { + parameterNames.add(p.name); + } return jsBody.toScopedBlock(parameterNames); } @@ -3205,23 +3212,27 @@ class ProgramCompiler extends Object } var encodedConditionSource = node - .enclosingComponent.uriToSource[node.location.file].source - .sublist(node.conditionStartOffset, node.conditionEndOffset); + .enclosingComponent.uriToSource[node.location.file].source + .sublist(node.conditionStartOffset, node.conditionEndOffset); var conditionSource = utf8.decode(encodedConditionSource); var location = _getLocation(node.conditionStartOffset); - return js.statement(' if (!#) #.assertFailed(#, #, #, #, #);', [ + var newvar = [ jsCondition, runtimeModule, - if (node.message == null) - JS.LiteralNull() - else - _visitExpression(node.message), - js.escapedString(location.sourceUrl.toString()), - // Lines and columns are typically printed with 1 based indexing. - js.number(location.line + 1), - js.number(location.column + 1), - js.escapedString(conditionSource), - ]); + ]; + if (node.message == null) { + newvar.add(JS.LiteralNull()); + } else { + newvar.add(_visitExpression(node.message)); + } + newvar.addAll([ + js.escapedString(location.sourceUrl.toString()), + // Lines and columns are typically printed with 1 based indexing. + js.number(location.line + 1), + js.number(location.column + 1), + js.escapedString(conditionSource)]); + + return js.statement(' if (!#) #.assertFailed(#, #, #, #, #);', newvar); } static bool isBreakable(Statement stmt) { @@ -3624,15 +3635,17 @@ class ProgramCompiler extends Object _emitVariableDef(exceptionParameter), runtimeModule, _emitVariableRef(caughtError) - ]), - if (stackTraceParameter != null) - js.statement('let # = #.stackTrace(#)', [ + ]) ]; + + if (stackTraceParameter != null) { + catchStatements.add(js.statement('let # = #.stackTrace(#)', [ _emitVariableDef(stackTraceParameter), runtimeModule, _emitVariableRef(caughtError) - ]), - catchBody, - ]; + ])); + } + catchStatements.add(catchBody); + _rethrowParameter = savedRethrow; return JS.Catch(_emitVariableDef(caughtError), JS.Block(catchStatements)); } @@ -4425,12 +4438,14 @@ class ProgramCompiler extends Object isGetter: !setter, isSetter: setter); } else { var function = member.function; - var params = [ - ..._emitTypeFormals(function.typeParameters), - for (var param in function.positionalParameters) - JS.Identifier(param.name), - if (function.namedParameters.isNotEmpty) namedArgumentTemp, - ]; + var params = [ ]..addAll(_emitTypeFormals(function.typeParameters)); + for (var param in function.positionalParameters) { + params.add(JS.Identifier(param.name)); + } + + if (function.namedParameters.isNotEmpty) { + params.add(namedArgumentTemp); + } var fn = js.fun( 'function(#) { return super[#](#); }', [params, jsName, params]); @@ -4543,18 +4558,24 @@ class ProgramCompiler extends Object List _emitArgumentList(Arguments node, {bool types = true, Member target}) { types = types && _reifyGenericFunction(target); - return [ - if (types) for (var typeArg in node.types) _emitType(typeArg), - for (var arg in node.positional) - if (arg is StaticInvocation && - isJSSpreadInvocation(arg.target) && - arg.arguments.positional.length == 1) - JS.Spread(_visitExpression(arg.arguments.positional[0])) - else - _visitExpression(arg), - if (node.named.isNotEmpty) - JS.ObjectInitializer(node.named.map(_emitNamedExpression).toList()), - ]; + var newvar = []; + if (types) { + for (var typeArg in node.types) { + newvar.add(_emitType(typeArg)); + } + } + for (var arg in node.positional) { + if (arg is StaticInvocation && isJSSpreadInvocation(arg.target) && + arg.arguments.positional.length == 1) { + newvar.add(JS.Spread(_visitExpression(arg.arguments.positional[0]))); + } else { + newvar.add(_visitExpression(arg)); + } + } + if (node.named.isNotEmpty) { + newvar.add(JS.ObjectInitializer(node.named.map(_emitNamedExpression).toList())); + } + return newvar; } JS.Property _emitNamedExpression(NamedExpression arg) { @@ -5052,12 +5073,14 @@ class ProgramCompiler extends Object @override JS.Expression visitMapLiteral(MapLiteral node) { - var entries = [ - for (var e in node.entries) ...[ - _visitExpression(e.key), - _visitExpression(e.value), - ], - ]; + var entries = [ ]; + + for (var e in node.entries) { + entries.addAll([ + _visitExpression(e.key), + _visitExpression(e.value), + ]); + } // TODO(markzipan): remove const check when we use front-end const eval if (!node.isConst) { @@ -5152,10 +5175,12 @@ class ProgramCompiler extends Object @override JS.Expression visitBlockExpression(BlockExpression node) { var jsExpr = _visitExpression(node.value); - var jsStmts = [ - for (var s in node.body.statements) _visitStatement(s), - JS.Return(jsExpr), - ]; + var jsStmts = [ ]; + for (var s in node.body.statements) { + jsStmts.add(_visitStatement(s)); + } + jsStmts.add(JS.Return(jsExpr)); + var jsBlock = JS.Block(jsStmts); // BlockExpressions with async operations must be constructed // with a generator instead of a lambda. @@ -5277,12 +5302,14 @@ class ProgramCompiler extends Object @override JS.Expression visitMapConstant(MapConstant node) { - var entries = [ - for (var e in node.entries) ...[ - visitConstant(e.key), - visitConstant(e.value), - ], - ]; + var entries = [ ]; + for (var e in node.entries) { + entries.addAll([ + visitConstant(e.key), + visitConstant(e.value), + ]); + } + return _emitConstMap(node.keyType, node.valueType, entries); } @@ -5305,10 +5332,10 @@ class ProgramCompiler extends Object var type = visitInterfaceType(node.getType(types) as InterfaceType); var prototype = js.call("#.prototype", [type]); - var properties = [ - JS.Property(propertyName("__proto__"), prototype), - for (var e in node.fieldValues.entries) entryToProperty(e), - ]; + var properties = [ JS.Property(propertyName("__proto__"), prototype) ]; + for (var e in node.fieldValues.entries) { + properties.add(entryToProperty(e)); + } return canonicalizeConstObject( JS.ObjectInitializer(properties, multiline: true)); } -- 2.29.2