# webassembly

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
        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

Artem Kobzar

03/12/2024, 5:14 PM

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 ? trying to get a WASM handle on UNDEFINED use of
results in JSON object having
not the key being removed. The
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
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
as a reference to a thing, with the above.