[Wasm/WASI] Extract js-wasi mode to context property

This commit is contained in:
Igor Yakovlev
2024-01-03 16:09:14 +01:00
committed by Space Team
parent 1f0fb5a1a8
commit c80854eb7c
9 changed files with 16 additions and 16 deletions
@@ -29,6 +29,8 @@ import org.jetbrains.kotlin.ir.types.IrTypeSystemContextImpl
import org.jetbrains.kotlin.ir.util.SymbolTable
import org.jetbrains.kotlin.ir.util.addChild
import org.jetbrains.kotlin.ir.util.file
import org.jetbrains.kotlin.js.config.WasmTarget
import org.jetbrains.kotlin.js.config.wasmTarget
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
@@ -46,6 +48,8 @@ class WasmBackendContext(
override val scriptMode = false
override val irFactory: IrFactory = symbolTable.irFactory
val isWasmJsTarget: Boolean = configuration.wasmTarget == WasmTarget.JS
// Place to store declarations excluded from code generation
private val excludedDeclarations = mutableMapOf<FqName, IrPackageFragment>()
@@ -380,11 +380,7 @@ class WasmSymbols(
private val wasmExportClass = getIrClass(FqName("kotlin.wasm.WasmExport"))
val wasmExportConstructor by lazy { wasmExportClass.constructors.single() }
private val jsRelatedSymbolsIfNonWasi =
when (context.configuration.get(JSConfigurationKeys.WASM_TARGET, WasmTarget.JS) == WasmTarget.JS) {
true -> JsRelatedSymbols()
else -> null
}
private val jsRelatedSymbolsIfNonWasi = if (context.isWasmJsTarget) JsRelatedSymbols() else null
val jsRelatedSymbols get() = jsRelatedSymbolsIfNonWasi ?: error("Cannot access to js related std in wasi mode")
@@ -149,7 +149,7 @@ fun compileWasm(
val byteArray = os.toByteArray()
val jsUninstantiatedWrapper: String?
val jsWrapper: String
if (backendContext.configuration.get(JSConfigurationKeys.WASM_TARGET, WasmTarget.JS) == WasmTarget.JS) {
if (backendContext.isWasmJsTarget) {
jsUninstantiatedWrapper = compiledWasmModule.generateAsyncJsWrapper(
"./$baseFileName.wasm",
backendContext.jsModuleAndQualifierReferences
@@ -59,7 +59,7 @@ class BuiltInsLowering(val context: WasmBackendContext) : FileLoweringPass {
irBuiltins.eqeqeqSymbol -> {
fun callRefIsNull(expr: IrExpression): IrCall {
if (
context.configuration.get(JSConfigurationKeys.WASM_TARGET, WasmTarget.JS) == WasmTarget.WASI &&
!context.isWasmJsTarget &&
expr.type.erasedUpperBound?.isExternal == true
) {
error("Unexpected external refs in wasi mode")
@@ -23,7 +23,7 @@ class JsCodeCallsLowering(val context: WasmBackendContext) : FileLoweringPass {
private val jsRelatedSymbols get() = context.wasmSymbols.jsRelatedSymbols
override fun lower(irFile: IrFile) {
if (context.configuration.get(JSConfigurationKeys.WASM_TARGET, WasmTarget.JS) == WasmTarget.WASI) return
if (!context.isWasmJsTarget) return
irFile.transformDeclarationsFlat { declaration ->
when (declaration) {
is IrSimpleFunction -> transformFunction(declaration)
@@ -50,7 +50,7 @@ class JsInteropFunctionsLowering(val context: WasmBackendContext) : DeclarationT
lateinit var currentParent: IrDeclarationParent
override fun transformFlat(declaration: IrDeclaration): List<IrDeclaration>? {
if (context.configuration.get(JSConfigurationKeys.WASM_TARGET, WasmTarget.JS) == WasmTarget.WASI) return null
if (!context.isWasmJsTarget) return null
if (declaration.isFakeOverride) return null
if (declaration !is IrSimpleFunction) return null
@@ -923,7 +923,7 @@ internal fun StringBuilder.appendParameterList(size: Int, name: String = "p", is
*/
class JsInteropFunctionCallsLowering(val context: WasmBackendContext) : BodyLoweringPass {
override fun lower(irBody: IrBody, container: IrDeclaration) {
if (context.configuration.get(JSConfigurationKeys.WASM_TARGET, WasmTarget.JS) == WasmTarget.WASI) return
if (!context.isWasmJsTarget) return
irBody.transformChildrenVoid(object : IrElementTransformerVoid() {
override fun visitCall(expression: IrCall): IrExpression {
expression.transformChildrenVoid()
@@ -133,7 +133,7 @@ internal class UnhandledExceptionLowering(val context: WasmBackendContext) : Fil
}
override fun lower(irFile: IrFile) {
if (context.configuration.get(JSConfigurationKeys.WASM_TARGET, WasmTarget.JS) == WasmTarget.WASI) return
if (!context.isWasmJsTarget) return
for (declaration in irFile.declarations) {
if (declaration is IrFunction && declaration.isExported()) {
processExportFunction(declaration)
@@ -215,7 +215,7 @@ class WasmBaseTypeOperatorTransformer(val context: WasmBackendContext) : IrEleme
}
if (fromClass.isExternal && !toClass.isExternal) {
if (context.configuration.get(JSConfigurationKeys.WASM_TARGET, WasmTarget.JS) == WasmTarget.WASI) {
if (!context.isWasmJsTarget) {
TODO("Implement externalize adapter for wasi mode")
}
val narrowingToAny = builder.irCall(jsToKotlinAnyAdapter).also {
@@ -226,7 +226,7 @@ class WasmBaseTypeOperatorTransformer(val context: WasmBackendContext) : IrEleme
}
if (toClass.isExternal && !fromClass.isExternal) {
if (context.configuration.get(JSConfigurationKeys.WASM_TARGET, WasmTarget.JS) == WasmTarget.WASI) {
if (!context.isWasmJsTarget) {
TODO("Implement internalize adapter for wasi mode")
}
return builder.irCall(kotlinToJsAnyAdapter).also {
@@ -19,9 +19,9 @@ import org.jetbrains.kotlin.name.FqName
* Mark declarations from [exportedFqNames] with @JsExport annotation
*/
fun markExportedDeclarations(context: WasmBackendContext, irFile: IrFile, exportedFqNames: Set<FqName>) {
val exportConstructor = when (context.configuration.get(JSConfigurationKeys.WASM_TARGET, WasmTarget.JS)) {
WasmTarget.WASI -> context.wasmSymbols.wasmExportConstructor
else -> context.wasmSymbols.jsRelatedSymbols.jsExportConstructor
val exportConstructor = when (context.isWasmJsTarget) {
true -> context.wasmSymbols.jsRelatedSymbols.jsExportConstructor
else -> context.wasmSymbols.wasmExportConstructor
}
for (declaration in irFile.declarations) {