A nicer workaround for JS compiler wrong function name bug

This commit is contained in:
Ugljesa Jovanovic 2020-08-08 22:05:46 +02:00 committed by Ugljesa Jovanovic
parent 5d61858f81
commit e997c18d1d
No known key found for this signature in database
GPG Key ID: 178E6DFCECCB0E0F
8 changed files with 38 additions and 28 deletions

View File

@ -18,9 +18,6 @@ fun ClassDefinition.defineHashFunctions() {
"crypto_hash_sha256_state" "crypto_hash_sha256_state"
) )
+funcDef( +funcDef(
"crypto_hash_sha256_init_spec",
"crypto_hash_sha256_init",
"crypto_hash_sha256_init",
"crypto_hash_sha256_init", "crypto_hash_sha256_init",
CustomTypeDefinition(ClassName(packageName, "Sha256State")), CustomTypeDefinition(ClassName(packageName, "Sha256State")),
dynamicJsReturn = true, dynamicJsReturn = true,

View File

@ -85,8 +85,13 @@ object JsLibsodiumGenerator {
constructJsCall.append(paramsToString(methodDefinition)) constructJsCall.append(paramsToString(methodDefinition))
} }
is CustomTypeDefinition -> { is CustomTypeDefinition -> {
if (methodDefinition.parameterList.filter { it.isStateType.not() }.size > 0) {
constructJsCall.append("return getSodium().${methodDefinition.jsName}") constructJsCall.append("return getSodium().${methodDefinition.jsName}")
constructJsCall.append(paramsToString(methodDefinition)) constructJsCall.append(paramsToString(methodDefinition))
} else {
constructJsCall.append("val result = js(\"getSodium().${methodDefinition.jsName}()\")")
constructJsCall.append("\nreturn result")
}
} }
} }
methodBuilder.addStatement(constructJsCall.toString()) methodBuilder.addStatement(constructJsCall.toString())

View File

@ -566,12 +566,12 @@ tasks {
// } // }
// } // }
// val jsIrBrowserTest by getting(KotlinJsTest::class) { val jsBrowserTest by getting(KotlinJsTest::class) {
// testLogging { testLogging {
// events("PASSED", "FAILED", "SKIPPED") events("PASSED", "FAILED", "SKIPPED")
// showStandardStreams = true showStandardStreams = true
// } }
// } }
} }
if (getHostOsName() == "windows") { if (getHostOsName() == "windows") {

View File

@ -10,7 +10,7 @@ expect class Sha512State
expect class GenericHashState expect class GenericHashState
expect class Crypto internal constructor() { expect class Crypto internal constructor() {
fun crypto_hash_sha256_init_spec(): Sha256State fun crypto_hash_sha256_init(): Sha256State
fun crypto_hash_sha256_update(state: Sha256State, input: UByteArray) fun crypto_hash_sha256_update(state: Sha256State, input: UByteArray)

View File

@ -1,5 +1,6 @@
package com.ionspin.kotlin.crypto package com.ionspin.kotlin.crypto
import com.ionspin.kotlin.bignum.integer.BigInteger
import com.ionspin.kotlin.crypto.hash.encodeToUByteArray import com.ionspin.kotlin.crypto.hash.encodeToUByteArray
import com.ionspin.kotlin.crypto.util.testBlocking import com.ionspin.kotlin.crypto.util.testBlocking
import com.ionspin.kotlin.crypto.util.toHexString import com.ionspin.kotlin.crypto.util.toHexString
@ -13,18 +14,23 @@ import kotlin.test.assertTrue
* on 08-Aug-2020 * on 08-Aug-2020
*/ */
class SmokeTest { class SmokeTest {
//TODO Browser ignores our testBlocking, node works fine though
@Test @Test
fun testIfLibraryIsNotOnFire() = testBlocking { fun testIfLibraryIsNotOnFire() {
testBlocking {
Initializer.initialize() Initializer.initialize()
val crypto = Crypto() val crypto = Crypto()
val state256 = crypto.crypto_hash_sha256_init_spec() //TODO seems to be a bug in JS compiler, if we have the same method name in crypto an in JsSodiumInterface, method tries to call wrong method name (unneeded suffix _0) //TODO seems to be a bug in JS compiler, if we have the same method name in crypto an in JsSodiumInterface, method tries to call wrong method name (unneeded suffix _0)
//I've workaround this by making state functions with 1 parameter execute call with js("") wrap, but still might sail somewhere else
val state256 = crypto.crypto_hash_sha256_init()
crypto.crypto_hash_sha256_update(state256, "Hello".encodeToUByteArray()) crypto.crypto_hash_sha256_update(state256, "Hello".encodeToUByteArray())
val result = crypto.crypto_hash_sha256_final(state256).toHexString() val result = crypto.crypto_hash_sha256_final(state256)
println("Result: $result") val resultString = result.toHexString()
println("Result: $resultString")
assertTrue { assertTrue {
"185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969" == result "185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969" == resultString
} }
}
} }
} }

View File

@ -14,9 +14,10 @@ actual typealias Sha512State = Any
actual typealias GenericHashState = Any actual typealias GenericHashState = Any
actual class Crypto internal actual constructor() { actual class Crypto internal actual constructor() {
actual fun crypto_hash_sha256_init_spec(): dynamic { actual fun crypto_hash_sha256_init(): dynamic {
println("Debug") println("Debug")
return getSodium().crypto_hash_sha256_init() val result = js("getSodium().crypto_hash_sha256_init()")
return result
} }
actual fun crypto_hash_sha256_update(state: Sha256State, input: UByteArray) { actual fun crypto_hash_sha256_update(state: Sha256State, input: UByteArray) {
@ -31,7 +32,8 @@ actual class Crypto internal actual constructor() {
actual fun crypto_hash_sha512_init(): dynamic { actual fun crypto_hash_sha512_init(): dynamic {
println("Debug") println("Debug")
return getSodium().crypto_hash_sha512_init() val result = js("getSodium().crypto_hash_sha512_init()")
return result
} }
actual fun crypto_hash_sha512_update(state: Sha512State, input: UByteArray) { actual fun crypto_hash_sha512_update(state: Sha512State, input: UByteArray) {

View File

@ -15,7 +15,7 @@ actual typealias Sha512State = Hash.State512
actual typealias GenericHashState = ByteArray actual typealias GenericHashState = ByteArray
actual class Crypto internal actual constructor() { actual class Crypto internal actual constructor() {
actual fun crypto_hash_sha256_init_spec(): Sha256State { actual fun crypto_hash_sha256_init(): Sha256State {
val state = debug.test.Sha256State() val state = debug.test.Sha256State()
println("Debug") println("Debug")
sodium.crypto_hash_sha256_init(state) sodium.crypto_hash_sha256_init(state)

View File

@ -27,7 +27,7 @@ actual class Crypto internal actual constructor() {
val _emitByteArray: ByteArray = ByteArray(0) val _emitByteArray: ByteArray = ByteArray(0)
actual fun crypto_hash_sha256_init_spec(): Sha256State { actual fun crypto_hash_sha256_init(): Sha256State {
val allocated = sodium_malloc(debug.test.Sha256State.size.convert())!! val allocated = sodium_malloc(debug.test.Sha256State.size.convert())!!
val state = allocated.reinterpret<debug.test.Sha256State>().pointed val state = allocated.reinterpret<debug.test.Sha256State>().pointed
libsodium.crypto_hash_sha256_init(state.ptr) libsodium.crypto_hash_sha256_init(state.ptr)