[JS IR BE] Minor fixes
* Global scope * Top-level properties * Refactoring
This commit is contained in:
committed by
Roman Artemev
parent
d6d0a7d804
commit
601f29b781
+1
-1
@@ -30,7 +30,7 @@ class IrDeclarationToJsTransformer : BaseIrElementToJsNodeTransformer<JsStatemen
|
|||||||
JsUnaryOperator.VOID,
|
JsUnaryOperator.VOID,
|
||||||
JsIntLiteral(1)
|
JsIntLiteral(1)
|
||||||
)
|
)
|
||||||
return jsAssignment(JsNameRef(fieldName, JsThisRef()), initExpression).makeStmt()
|
return JsVars(JsVars.JsVar(fieldName, initExpression))
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun visitVariable(declaration: IrVariable, context: JsGenerationContext): JsStatement {
|
override fun visitVariable(declaration: IrVariable, context: JsGenerationContext): JsStatement {
|
||||||
|
|||||||
+7
-7
@@ -20,22 +20,22 @@ class IrModuleToJsTransformer(val backendContext: JsIrBackendContext) : BaseIrEl
|
|||||||
}
|
}
|
||||||
|
|
||||||
// sort member forwarding code
|
// sort member forwarding code
|
||||||
addPostDeclarations(rootContext)
|
program.globalBlock.statements += addPostDeclarations(rootContext.staticContext.classModels)
|
||||||
|
|
||||||
return program
|
return program
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private fun addPostDeclarations(context: JsGenerationContext) {
|
private fun addPostDeclarations(classModels: Map<JsName, JsClassModel>): List<JsStatement> {
|
||||||
val staticContext = context.staticContext
|
|
||||||
val program = context.currentScope.program
|
|
||||||
val block = program.globalBlock
|
|
||||||
|
|
||||||
|
val statements = mutableListOf<JsStatement>()
|
||||||
val visited = mutableSetOf<JsName>()
|
val visited = mutableSetOf<JsName>()
|
||||||
|
|
||||||
for (name in staticContext.classModels.keys) {
|
for (name in classModels.keys) {
|
||||||
addPostDeclaration(name, visited, block.statements, staticContext.classModels)
|
addPostDeclaration(name, visited, statements, classModels)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return statements
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun addPostDeclaration(name: JsName, visited: MutableSet<JsName>, statements: MutableList<JsStatement>, classModels: Map<JsName, JsClassModel>) {
|
private fun addPostDeclaration(name: JsName, visited: MutableSet<JsName>, statements: MutableList<JsStatement>, classModels: Map<JsName, JsClassModel>) {
|
||||||
|
|||||||
+2
-2
@@ -29,7 +29,7 @@ class JsClassGenerator(private val irClass: IrClass, val context: JsGenerationCo
|
|||||||
private val baseClass = irClass.superClasses.firstOrNull { !it.owner.isInterface }
|
private val baseClass = irClass.superClasses.firstOrNull { !it.owner.isInterface }
|
||||||
private val baseClassName = baseClass?.let { context.getNameForSymbol(it) }
|
private val baseClassName = baseClass?.let { context.getNameForSymbol(it) }
|
||||||
private val classPrototypeRef = prototypeOf(classNameRef)
|
private val classPrototypeRef = prototypeOf(classNameRef)
|
||||||
private val classBlock = JsBlock()
|
private val classBlock = JsGlobalBlock()
|
||||||
private val classModel = JsClassModel(className, baseClassName)
|
private val classModel = JsClassModel(className, baseClassName)
|
||||||
|
|
||||||
fun generate(): JsStatement {
|
fun generate(): JsStatement {
|
||||||
@@ -73,7 +73,7 @@ class JsClassGenerator(private val irClass: IrClass, val context: JsGenerationCo
|
|||||||
val memberName = context.getNameForSymbol(declaration.symbol)
|
val memberName = context.getNameForSymbol(declaration.symbol)
|
||||||
val memberRef = JsNameRef(memberName, classPrototypeRef)
|
val memberRef = JsNameRef(memberName, classPrototypeRef)
|
||||||
|
|
||||||
translatedFunction?.let { return jsAssignment(memberRef, it).makeStmt() }
|
translatedFunction?.let { return jsAssignment(memberRef, it.apply { name = null }).makeStmt() }
|
||||||
|
|
||||||
// do not generate code like
|
// do not generate code like
|
||||||
// interface I { foo() = "OK" }
|
// interface I { foo() = "OK" }
|
||||||
|
|||||||
+1
-1
@@ -13,7 +13,7 @@ import org.jetbrains.kotlin.js.backend.ast.*
|
|||||||
|
|
||||||
class JsGenerationContext {
|
class JsGenerationContext {
|
||||||
fun newDeclaration(scope: JsScope, func: IrFunction? = null): JsGenerationContext {
|
fun newDeclaration(scope: JsScope, func: IrFunction? = null): JsGenerationContext {
|
||||||
return JsGenerationContext(this, JsBlock(), scope, func)
|
return JsGenerationContext(this, if (func != null) JsBlock() else JsGlobalBlock(), scope, func)
|
||||||
}
|
}
|
||||||
|
|
||||||
val currentBlock: JsBlock
|
val currentBlock: JsBlock
|
||||||
|
|||||||
Reference in New Issue
Block a user