JS backend: generate better code for secondary constructor calls -- don't generate undefined when it possible

This commit is contained in:
Zalim Bashorov
2015-05-29 00:26:06 +03:00
parent 79b5652ce6
commit 779a0373c7
4 changed files with 8 additions and 6 deletions
@@ -38,7 +38,7 @@ public object KotlinJavascriptMetadataUtils {
* Matches string like <name>.kotlin_module_metadata(<abi version>, <module name>, <base64 data>)
*/
private val METADATA_PATTERN = "(?m)\\w+\\.$KOTLIN_JAVASCRIPT_METHOD_NAME\\((\\d+),\\s*(['\"])([^'\"]*)\\2,\\s*(['\"])([^'\"]*)\\4\\)".toRegex()
private val ABI_VERSION = 2
private val ABI_VERSION = 3
public fun replaceSuffix(filePath: String): String = filePath.substringBeforeLast(JS_EXT) + META_JS_SUFFIX
@@ -215,7 +215,7 @@ object ConstructorCallCase : FunctionCallCase {
return JsNew(functionRef, argumentsInfo.translateArguments)
}
else {
return JsInvocation(functionRef, listOf(JsLiteral.UNDEFINED) + argumentsInfo.translateArguments)
return JsInvocation(functionRef, argumentsInfo.translateArguments)
}
}
}
@@ -311,7 +311,7 @@ public class ClassTranslator private constructor(
val constructorInitializer = FunctionTranslator.newInstance(constructor, translationContext).translateAsMethod()
val constructorFunction = constructorInitializer.getValueExpr() as JsFunction
constructorFunction.getParameters().add(0, JsParameter(thisName))
constructorFunction.getParameters().add(JsParameter(thisName))
val referenceToClass = context.getQualifiedReference(classDescriptor)
@@ -64,15 +64,17 @@ fun JsExpression.toInvocationWith(thisExpr: JsExpression): JsExpression {
is JsNew -> {
qualifier = Namer.getFunctionCallRef(getConstructorExpression())
arguments = getArguments()
// `new A(a, b, c)` -> `A.call($this, a, b, c)`
return JsInvocation(qualifier, listOf(thisExpr) + arguments)
}
is JsInvocation -> {
qualifier = getQualifier()
arguments = with(getArguments()) { subList(1, size()) }
arguments = getArguments()
// `A(a, b, c)` -> `A(a, b, c, $this)`
return JsInvocation(qualifier, arguments + thisExpr)
}
else -> throw IllegalStateException("Unexpected node type: " + javaClass)
}
return JsInvocation(qualifier, listOf(thisExpr) + arguments)
}
public var JsNameRef.qualifier: JsExpression?