All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jetbrains.kotlin.backend.wasm.ir2wasm.JsHelpers.kt Maven / Gradle / Ivy

There is a newer version: 2.1.0-Beta1
Show newest version
/*
 * Copyright 2010-2019 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 org.jetbrains.kotlin.backend.wasm.ir2wasm

import org.jetbrains.kotlin.js.backend.JsToStringGenerationVisitor
import java.util.Base64

fun String.toJsStringLiteral(): CharSequence =
    JsToStringGenerationVisitor.javaScriptString(this)

data class JsModuleAndQualifierReference(
    val module: String?,
    val qualifier: String?,
) {
    private val moduleBase64 = module?.let { encode(it) }.orEmpty()

    private val qualifierBase64 = qualifier?.let { encode(it) }.orEmpty()

    val jsVariableName = "_ref_${moduleBase64}_$qualifierBase64"

    val importVariableName = "_import_${moduleBase64}_$qualifierBase64"

    companion object {
        // Encode variable name as base64 to have a valid unique JS identifier
        private val encoder = Base64.getEncoder().withoutPadding()

        fun encode(value: String): String {
            return encoder.encodeToString(value.encodeToByteArray())
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy