https://kotlinlang.org logo
#webassembly
Title
# webassembly
d

Darryl Miles

03/12/2024, 4:51 PM
Taking a fetch API example, is there a standard idiom to use for generating an object map, like the "headers" field:
Copy code
// This snippet is from Kotlin/JS  not WasmJS
window.fetch(
        url, RequestInit(
            method = method,
            body = body,
            headers = json(   //  THIS PART: Is there a WASM equivalent to kotlin.js.json
                "Content-Type" to "application/json",
                "Accept" to "application/json",
                "pragma" to "no-cache"
            )
        )
    )
Is it possible to perform something like:
Copy code
@JsFun("(value) => JSON.stringify(value)")
external fun jsonStringify(value: JsAny?): JsString = definedExternally
How about:
Copy code
@JsFun("(value, replacer, space) => JSON.stringify(value, replacer, space)")
external fun jsonStringify(value: JsAny?, replacer: (JsString, JsAny?) -> JsAny, space: JsAny): JsString = definedExternally
a

Artem Kobzar

03/12/2024, 5:14 PM
d

Darryl Miles

03/12/2024, 5:26 PM
Wow that snippet worked, thanks, is there a reason such a basic concept as JsObject is not in the standard setup ? or maybe it will be available on future ?
https://pl.kotl.in/4DmieoXqE trying to get a WASM handle on UNDEFINED use of
Unit.toJsReference()
results in JSON object having
{}
not the key being removed. The
Unit.toJsReference()
is is documentation somewhere.
Copy code
private val UNDEFINED: JsAny? = js("undefined")

fun main() {
    val result = json(
        "Content-Type".toJsString() to "application/json".toJsString(),
        "Accept".toJsString() to "application/json".toJsString(),
        "pragma".toJsString() to "no-cache".toJsString(),
        "bf".toJsString() to true.toJsBoolean(),
        "int".toJsString() to (-1).toJsNumber(),
        "double".toJsString() to (42.0).toJsNumber(),
        "null".toJsString() to null,
        "undef1".toJsString() to Unit.toJsReference(),  // results in {}
        "undef2".toJsString() to UNDEFINED
    )
    println(JSON.stringify(result))
}
Like the original I there is error on website
Unhandled JavaScript exception: wasm validation error: at offset 87: bad type
Maybe it is FF115 ESR I use on this machine
Copy code
@JsFun("(o,k) => { return delete o[k] }")  // need JS keyword access
external fun DELETE(o: JsAny, k: JsString): JsBoolean = definedExternally

fun JsObject.delete(key: JsString): JsBoolean {
   return DELETE(this, key)
}

fun JsObject.delete(key: String): Boolean {
   return delete(key.toJsString()).toBoolean()
}
Maybe I can mitigate the lack of
JsAny.undefined
as a reference to a thing, with the above.
7 Views