Skip to content

Commit 342a58c

Browse files
committed
fix problems
1 parent eea8f0a commit 342a58c

File tree

4 files changed

+50
-16
lines changed

4 files changed

+50
-16
lines changed

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@ actual class JavaScriptEngine actual constructor() {
1616
private val quickJs: QuickJs = QuickJs.create()
1717
private val json: Json = Json.Default
1818

19+
private var context: Map<String, JsType> = mapOf()
20+
actual fun setContextObjects(context: Map<String, JsType>) {
21+
this.context = context
22+
}
23+
1924
@Volatile
2025
var isClosed = false
2126
private set
2227

23-
actual fun evaluate(
24-
context: Map<String, JsType>,
25-
script: String
26-
): JsType {
28+
actual fun evaluate(script: String): JsType {
2729
if (isClosed) throw JavaScriptEvaluationException(message = "Engine already closed")
2830

2931
return try {
@@ -39,6 +41,12 @@ actual class JavaScriptEngine actual constructor() {
3941
isClosed = true
4042
}
4143

44+
actual fun objectToJsonString(value: JsType): String? {
45+
val script = "JSON.stringify(objectValue);"
46+
val scriptWithContext = convertContextMapToJsScript(mapOf("objectValue" to value)) + script + "\n"
47+
return quickJs.evaluate(scriptWithContext) as String
48+
}
49+
4250
private fun internalEvaluate(
4351
context: Map<String, JsType>,
4452
script: String
@@ -74,6 +82,7 @@ actual class JavaScriptEngine actual constructor() {
7482
"\"$it\""
7583
}
7684
JsType.Null -> null
85+
is JsType.AnyValue -> null
7786
}
7887
}
7988

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,27 @@ package dev.icerock.moko.javascript
66

77
expect class JavaScriptEngine() {
88
/**
9-
* Evaluate some [script] with external [context].
9+
* Evaluate some [script].
1010
*
1111
* @throws JavaScriptEvaluationException in case of an error in the engine evaluation or if the
1212
* engine has already been closed.
1313
*/
14-
fun evaluate(context: Map<String, JsType>, script: String): JsType
14+
fun evaluate(script: String): JsType
15+
16+
/**
17+
* Set some [context] with external [context].
18+
*/
19+
fun setContextObjects(context: Map<String, JsType>)
20+
21+
/**
22+
* Turns some [JsType] to JsonString.
23+
*/
24+
fun objectToJsonString(value: JsType): String?
1525

1626
/**
1727
* Closes the engine and releases the allocated memory.
1828
*/
1929
fun close()
30+
31+
2032
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ sealed class JsType {
1111
data class Str(override val value: String) : JsType()
1212
data class DoubleNum(override val value: Double) : JsType()
1313
data class Json(override val value: JsonElement) : JsType()
14+
data class AnyValue(override val value: Any) : JsType()
1415

1516
/**
1617
* For "undefined" and "null".

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

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,24 @@ import platform.JavaScriptCore.JSValue
2222
import platform.JavaScriptCore.setObject
2323

2424
actual class JavaScriptEngine actual constructor() {
25-
actual fun evaluate(context: Map<String, JsType>, script: String): JsType {
2625

27-
val jsContext = JSContext()
28-
29-
jsContext.exceptionHandler = { exceptionContext, exception ->
30-
val message = "\"context = $exceptionContext, exception = $exception\""
31-
throw JavaScriptEvaluationException(cause = null, message = message)
32-
}
26+
private val jsContext = JSContext()
3327

28+
actual fun setContextObjects(context: Map<String, JsType>) {
3429
context.forEach {
3530
jsContext.setObject(
3631
`object` = prepareValueForJsContext(it.value),
3732
forKeyedSubscript = NSString.create(string = it.key)
3833
)
3934
}
35+
}
36+
37+
actual fun evaluate(script: String): JsType {
38+
39+
jsContext.exceptionHandler = { exceptionContext, exception ->
40+
val message = "\"context = $exceptionContext, exception = $exception\""
41+
throw JavaScriptEvaluationException(cause = null, message = message)
42+
}
4043

4144
val result = jsContext.evaluateScript(script)
4245

@@ -47,6 +50,16 @@ actual class JavaScriptEngine actual constructor() {
4750
// Nothing to do here
4851
}
4952

53+
actual fun objectToJsonString(value: JsType): String? {
54+
val localContext = JSContext()
55+
localContext.setObject(
56+
`object` = prepareValueForJsContext(value),
57+
forKeyedSubscript = NSString.create(string = "objectValue")
58+
)
59+
60+
return localContext.evaluateScript("JSON.stringify(objectValue);")?.toString_()
61+
}
62+
5063
private fun prepareValueForJsContext(valueWrapper: JsType): Any? {
5164
return if (valueWrapper is JsType.Json) valueWrapper.value.getValue()
5265
else valueWrapper.value
@@ -84,10 +97,9 @@ private fun JSValue.toMokoJSType(): JsType {
8497
isBoolean -> JsType.Bool(toBool())
8598
isString -> JsType.Str(toString_().orEmpty())
8699
isNumber -> JsType.DoubleNum(toDouble())
87-
isObject -> JsType.Json(Json.encodeToJsonElement(toDictionary()))
100+
isObject -> JsType.AnyValue(toObject()!!)
88101
isArray -> JsType.Json(Json.encodeToJsonElement(toArray()))
89-
isUndefined -> JsType.Null
90102
isNull -> JsType.Null
91-
else -> JsType.Null
103+
else -> JsType.AnyValue(this)
92104
}
93105
}

0 commit comments

Comments
 (0)