[JS BE] add runtime-function and intrinsics for es6-classes
This commit is contained in:
@@ -151,6 +151,11 @@ class JsIntrinsics(private val irBuiltIns: IrBuiltIns, val context: JsIrBackendC
|
|||||||
val jsAnyToString = getInternalFunction("anyToString")
|
val jsAnyToString = getInternalFunction("anyToString")
|
||||||
val jsCompareTo = getInternalFunction("compareTo")
|
val jsCompareTo = getInternalFunction("compareTo")
|
||||||
val jsEquals = getInternalFunction("equals")
|
val jsEquals = getInternalFunction("equals")
|
||||||
|
val jsConstruct = getInternalFunction("construct")
|
||||||
|
val jsNewTarget = unOp("jsNewTarget")
|
||||||
|
val jsEmptyObject = unOp("emptyObject")
|
||||||
|
val jsOpenInitializerBox = binOp("openInitializerBox")
|
||||||
|
val es6DefaultType = defineEs6DefaultTypeIntrinsic().symbol
|
||||||
|
|
||||||
val jsImul = getInternalFunction("imul")
|
val jsImul = getInternalFunction("imul")
|
||||||
|
|
||||||
@@ -324,6 +329,14 @@ class JsIntrinsics(private val irBuiltIns: IrBuiltIns, val context: JsIrBackendC
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun defineEs6DefaultTypeIntrinsic(): IrSimpleFunction {
|
||||||
|
return externalPackageFragment.addFunction {
|
||||||
|
name = Name.identifier("DefaultType")
|
||||||
|
}.apply {
|
||||||
|
returnType = addTypeParameter("T", irBuiltIns.anyType).defaultType
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun defineJsBindIntrinsic(): IrSimpleFunction {
|
private fun defineJsBindIntrinsic(): IrSimpleFunction {
|
||||||
return externalPackageFragment.addFunction {
|
return externalPackageFragment.addFunction {
|
||||||
name = Name.identifier("\$jsBind\$")
|
name = Name.identifier("\$jsBind\$")
|
||||||
|
|||||||
+30
@@ -101,6 +101,36 @@ class JsIntrinsicTransformers(backendContext: JsIrBackendContext) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
add(intrinsics.jsNewTarget) { _, _ ->
|
||||||
|
JsNameRef(JsName("target"), JsNameRef(JsName("new")))
|
||||||
|
}
|
||||||
|
|
||||||
|
add(intrinsics.jsOpenInitializerBox) { call, context ->
|
||||||
|
val arguments = translateCallArguments(call, context)
|
||||||
|
|
||||||
|
JsInvocation(
|
||||||
|
JsNameRef("Object.assign"),
|
||||||
|
arguments
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
add(intrinsics.jsEmptyObject) { _, _ ->
|
||||||
|
JsObjectLiteral()
|
||||||
|
}
|
||||||
|
|
||||||
|
add(intrinsics.es6DefaultType) { call, context ->
|
||||||
|
val classifier: IrClassifierSymbol = call.getTypeArgument(0)!!.classifierOrFail
|
||||||
|
val owner = classifier.owner
|
||||||
|
|
||||||
|
when {
|
||||||
|
owner is IrClass && owner.isEffectivelyExternal() ->
|
||||||
|
context.getRefForExternalClass(owner)
|
||||||
|
|
||||||
|
else ->
|
||||||
|
context.getNameForStaticDeclaration(owner as IrDeclarationWithName).makeRef()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
addIfNotNull(intrinsics.jsCode) { _, _ -> error("Should not be called") }
|
addIfNotNull(intrinsics.jsCode) { _, _ -> error("Should not be called") }
|
||||||
|
|
||||||
add(intrinsics.jsGetContinuation) { _, context: JsGenerationContext ->
|
add(intrinsics.jsGetContinuation) { _, context: JsGenerationContext ->
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||||
|
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package kotlin.js
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param CT is return type of calling constructor (uses in DCE)
|
||||||
|
*/
|
||||||
|
fun <CT> construct(constructorType: dynamic, resultType: dynamic, vararg args: Any?): Any {
|
||||||
|
return js("Reflect").construct(constructorType, args, resultType)
|
||||||
|
}
|
||||||
@@ -99,6 +99,10 @@ internal fun newThrowable(message: String?, cause: Throwable?): Throwable {
|
|||||||
|
|
||||||
internal fun extendThrowable(this_: dynamic, message: String?, cause: Throwable?) {
|
internal fun extendThrowable(this_: dynamic, message: String?, cause: Throwable?) {
|
||||||
js("Error").call(this_)
|
js("Error").call(this_)
|
||||||
|
setPropertiesToThrowableInstance(this_, message, cause)
|
||||||
|
}
|
||||||
|
|
||||||
|
internal fun setPropertiesToThrowableInstance(this_: dynamic, message: String?, cause: Throwable?) {
|
||||||
if (!hasOwnPrototypeProperty(this_, "message")) {
|
if (!hasOwnPrototypeProperty(this_, "message")) {
|
||||||
this_.message = message ?: cause?.toString() ?: undefined
|
this_.message = message ?: cause?.toString() ?: undefined
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user