44
55package dev.icerock.moko.javascript
66
7+ import kotlinx.serialization.SerializationException
8+ import kotlinx.serialization.json.Json
79import kotlinx.serialization.json.JsonArray
810import kotlinx.serialization.json.JsonElement
911import kotlinx.serialization.json.JsonNull
@@ -29,6 +31,16 @@ actual class JavaScriptEngine actual constructor() {
2931 val message = " \" context = $exceptionContext , exception = $exception \" "
3032 throw JavaScriptEvaluationException (cause = null , message = message)
3133 }
34+
35+ this .evaluateScript(
36+ """
37+ function mokoJavaScriptProcessResult(result) {
38+ if (typeof result === 'object') return JSON.stringify(result);
39+ else if (typeof result === 'array') return JSON.stringify(result);
40+ else return result;
41+ }
42+ """ .trimIndent()
43+ )
3244 }
3345
3446 actual fun setContextObjects (vararg context : Pair <String , JsType >) {
@@ -58,23 +70,24 @@ actual class JavaScriptEngine actual constructor() {
5870 )
5971 }
6072
61- return result?.toMokoJSType() ? : JsType .Null
73+ val resultKey = " evaluationResult"
74+ jsContext.setObject(
75+ `object ` = result,
76+ forKeyedSubscript = NSString .create(string = resultKey)
77+ )
78+ val formattedResult = jsContext.evaluateScript(" mokoJavaScriptProcessResult($resultKey )" )
79+ jsContext.setObject(
80+ `object ` = null ,
81+ forKeyedSubscript = NSString .create(string = resultKey)
82+ )
83+
84+ return formattedResult?.toMokoJSType() ? : JsType .Null
6285 }
6386
6487 actual fun close () {
6588 // Nothing to do here
6689 }
6790
68- // actual fun objectToJsonString(value: JsType): String? {
69- // val localContext = JSContext()
70- // localContext.setObject(
71- // `object` = prepareValueForJsContext(value),
72- // forKeyedSubscript = NSString.create(string = "objectValue")
73- // )
74- //
75- // return localContext.evaluateScript("JSON.stringify(objectValue);")?.toString_()
76- // }
77-
7891 private fun prepareValueForJsContext (valueWrapper : JsType ): Any? {
7992 return if (valueWrapper is JsType .Json ) valueWrapper.value.getValue()
8093 else valueWrapper.value
@@ -108,16 +121,19 @@ private fun JsonElement.getValue(): Any? {
108121}
109122
110123private fun JSValue.toMokoJSType (): JsType {
124+ val json = Json .Default
111125 return when {
112126 isBoolean -> JsType .Bool (toBool())
113- isString -> JsType .Str (toString_().orEmpty())
127+ isString -> try {
128+ val jsonElement: JsonElement = json.parseToJsonElement(toString_().orEmpty())
129+ if (jsonElement is JsonObject || jsonElement is JsonArray ) JsType .Json (jsonElement)
130+ else JsType .Str (toString_().orEmpty())
131+ } catch (ex: SerializationException ) {
132+ JsType .Str (toString_().orEmpty())
133+ } catch (ex: IllegalStateException ) {
134+ JsType .Str (toString_().orEmpty())
135+ }
114136 isNumber -> JsType .DoubleNum (toDouble())
115- isObject -> this .toDictionary()!!
116- .toJson()
117- .let { JsType .Json (it) }
118- isArray -> this .toArray()!!
119- .map { it.toJsonElement() }
120- .let { JsType .Json (JsonArray (it)) }
121137 isNull || isUndefined -> JsType .Null
122138 else -> throw IllegalArgumentException (" unknown JSValue type $this " )
123139 }
0 commit comments