[JS IR BE] Set Throwable subtype name in extendThrowable function

This commit is contained in:
Svyatoslav Kuzmich
2019-07-04 19:05:41 +03:00
parent 4379780ff8
commit 675f01ee80
2 changed files with 6 additions and 28 deletions
@@ -27,12 +27,9 @@ import org.jetbrains.kotlin.ir.visitors.IrElementTransformer
class ThrowableLowering(val context: JsIrBackendContext) : FileLoweringPass {
private val unitType get() = context.irBuiltIns.unitType
private val nothingNType get() = context.irBuiltIns.nothingNType
private val stringType get() = context.irBuiltIns.stringType
private val throwableConstructors = context.throwableConstructors
private val newThrowableFunction = context.newThrowableSymbol
private val extendThrowableFunction = context.extendThrowableSymbol
@@ -84,30 +81,6 @@ class ThrowableLowering(val context: JsIrBackendContext) : FileLoweringPass {
}
}
override fun visitConstructor(declaration: IrConstructor, data: IrDeclarationParent): IrStatement {
val klass = data as IrClass
if (klass.defaultType.isThrowableTypeOrSubtype() && declaration.callsSuper(context.irBuiltIns)) {
val body = declaration.body as IrBlockBody
body.statements += IrDynamicOperatorExpressionImpl(
UNDEFINED_OFFSET, UNDEFINED_OFFSET,
unitType,
IrDynamicOperator.EQ
).also {
it.receiver = IrDynamicMemberExpressionImpl(
UNDEFINED_OFFSET, UNDEFINED_OFFSET,
context.dynamicType,
"name",
JsIrBuilder.buildGetValue(klass.thisReceiver!!.symbol)
)
it.arguments.add(JsIrBuilder.buildString(stringType, klass.name.asString()))
}
}
declaration.transformChildren(this, data)
return declaration
}
override fun visitDelegatingConstructorCall(expression: IrDelegatingConstructorCall, data: IrDeclarationParent): IrExpression {
expression.transformChildren(this, data)
if (expression.symbol !in throwableConstructors) return expression
@@ -97,11 +97,16 @@ internal fun extendThrowable(this_: dynamic, message: String?, cause: Throwable?
js("Error").call(this_)
this_.message = message ?: cause?.toString() ?: undefined
this_.cause = cause
this_.name = JsObject.getPrototypeOf(this_).constructor.name
captureStack(this_)
}
@JsName("Object")
internal external class JsObject
internal external class JsObject {
companion object {
fun getPrototypeOf(obj: Any?): dynamic
}
}
internal fun <T, R> boxIntrinsic(x: T): R = error("Should be lowered")
internal fun <T, R> unboxIntrinsic(x: T): R = error("Should be lowered")