altavir
03/15/2019, 5:15 PMdynamic to be passed to JS library. What is the easiest way to do it?r4zzz4k
03/15/2019, 5:53 PMaltavir
03/15/2019, 5:59 PMaltavir
03/15/2019, 6:04 PMr4zzz4k
03/15/2019, 6:19 PMjs("{}") and fill it from map or any other source you have. Mapping an object tree to a Map can be done via Mapper (ooof) from kotlinx.coroutines.altavir
03/15/2019, 6:36 PMr4zzz4k
03/15/2019, 6:36 PMaltavir
03/15/2019, 6:37 PMjs directory seems to be the best solution.Svyatoslav Kuzmich [JB]
03/15/2019, 8:35 PMRobert Jaros
03/15/2019, 11:53 PMkotlin.js.JSON.parse(JSON.plain.stringify(serializer, data))Robert Jaros
03/15/2019, 11:54 PMaltavir
03/16/2019, 6:30 AMSvyatoslav Kuzmich [JB]
03/16/2019, 10:09 AMmy object is expected to be called byI assumed map uses String keys. Kotlin/JSobj.foo
Map can't be directly used for these cases. But there other zero copy approaches and they heavily depend on the context:
If set of keys is limited, you could use external interface/class where property correspond to entries:
external interface M {
var keyName1: ValueType?,
var keyName2: ValueType2?,
...
}
This will not fly since you use generic strings.
Other approach would be to use something similar to "native" JS map instead of Kotlin `Map`:
class NativeJsMap<V: Any> {
operator fun get(key: String): V? = this.asDynamic()[key]
operator fun set(key: String, value: V) {
this.asDynamic()[key] = value
}
}
This is useful for simple interop cases, but making it a full replacement of Map is hard, especially in multiplatform code.
From what I see, making copy is the way.altavir
03/16/2019, 1:56 PMget(String) method. Does JS automatically call A.get("foo") when asked for A.foo?Svyatoslav Kuzmich [JB]
03/16/2019, 4:33 PMA.get(...) when asked for property. get and set are purely for [] operator overloading on Kotlin level.
this.asDynamic()[key] does the trick, it accesses native JS property on the object. In Kotlin you can do A.asDynamic()["foo"] = fooValue; and then use it in javascript as A["foo"] (or equivalent A.foo)altavir
03/16/2019, 4:35 PMSvyatoslav Kuzmich [JB]
03/16/2019, 5:10 PMdynamic is the best term to describe what you mean. dynamic is a very general Kotlin type, literally anything can be stored in it.
I'm not sure how to name it, but is more like "object-based JS string-maps" 😃.altavir
03/16/2019, 6:30 PM