Skip to content

Commit 3e570db

Browse files
committed
android JavaScriptEngine improve; add JsType.value property
1 parent 09b8bab commit 3e570db

File tree

3 files changed

+45
-14
lines changed

3 files changed

+45
-14
lines changed

javascript/build.gradle.kts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ android {
1212
defaultConfig {
1313
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
1414
}
15+
16+
packagingOptions {
17+
exclude("META-INF/*.kotlin_module")
18+
exclude("META-INF/*.kotlin_module")
19+
exclude("META-INF/AL2.0")
20+
exclude("META-INF/LGPL2.1")
21+
22+
}
1523
}
1624

1725
dependencies {

javascript/src/androidMain/kotlin/dev/icerock/moko/javascript/JavaScriptEngine.kt

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import app.cash.quickjs.QuickJs
88
import app.cash.quickjs.QuickJsException
99
import kotlinx.serialization.SerializationException
1010
import kotlinx.serialization.json.Json
11-
import kotlinx.serialization.json.encodeToJsonElement
11+
import kotlinx.serialization.json.JsonObject
1212

13-
actual class JavaScriptEngine actual constructor(){
13+
actual class JavaScriptEngine actual constructor() {
1414
private val quickJs: QuickJs = QuickJs.create()
1515
private val json: Json = Json.Default
1616

@@ -27,7 +27,7 @@ actual class JavaScriptEngine actual constructor(){
2727
return try {
2828
internalEvaluate(context, script)
2929
} catch (exception: QuickJsException) {
30-
throw JavaScriptEvaluationException(exception)
30+
throw JavaScriptEvaluationException(exception, exception.message)
3131
}
3232
}
3333

@@ -41,22 +41,33 @@ actual class JavaScriptEngine actual constructor(){
4141
context: Map<String, JsType>,
4242
script: String
4343
): JsType {
44-
context.forEach { pair ->
45-
quickJs.set(pair.key, pair.value.javaClass, pair.value)
46-
}
47-
val result = quickJs.evaluate(script)
44+
val scriptWithContext = convertContextMapToJsScript(context) + script
45+
val result = quickJs.evaluate(scriptWithContext)
4846
return handleQuickJsResult(result)
4947
}
5048

49+
private fun convertContextMapToJsScript(context: Map<String, JsType>): String {
50+
if (context.isEmpty()) return ""
51+
52+
return context.mapNotNull { pair ->
53+
pair.value.value?.let { "var ${pair.key} = $it;" }
54+
}.joinToString(separator = "")
55+
}
56+
5157
private fun handleQuickJsResult(result: Any?): JsType {
52-
return when (result) {
58+
return when {
5359
result == null -> JsType.Null
54-
is Boolean -> JsType.Bool(result)
55-
is Int -> JsType.IntNum(result)
56-
is Double -> JsType.DoubleNum(result)
57-
is Float -> JsType.DoubleNum(result.toDouble())
58-
is String -> try {
59-
JsType.Json(json.encodeToJsonElement(result))
60+
result is Boolean -> JsType.Bool(result)
61+
result is Int -> JsType.IntNum(result)
62+
result is Double -> JsType.DoubleNum(result)
63+
result is Float -> JsType.DoubleNum(result.toDouble())
64+
result is String -> try {
65+
val serializeResult = json.parseToJsonElement(result)
66+
if (serializeResult is JsonObject) {
67+
JsType.Json(serializeResult)
68+
} else {
69+
JsType.Str(result)
70+
}
6071
} catch (ex: SerializationException) {
6172
JsType.Str(result)
6273
}

javascript/src/commonMain/kotlin/dev/icerock/moko/javascript/JsType.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@ sealed class JsType {
1919
object Null : JsType()
2020
}
2121

22+
val JsType.value: Any?
23+
get() {
24+
return when (this) {
25+
is JsType.Bool -> value
26+
is JsType.Str -> value
27+
is JsType.IntNum -> value
28+
is JsType.DoubleNum -> value
29+
is JsType.Json -> value.toString()
30+
is JsType.Null -> null
31+
}
32+
}
33+
2234
fun JsType.boolValue(): Boolean = (this as JsType.Bool).value
2335
fun JsType.stringValue(): String = (this as JsType.Str).value
2436
fun JsType.intValue(): Int = (this as JsType.IntNum).value

0 commit comments

Comments
 (0)