I am still trying to understand wasm in general an...
# webassembly
a
I am still trying to understand wasm in general and how kotlin approaches it. So, do forgive me if these questions seem straight forward. • Why did the kotlin/wasm team introduce
JsAny
instead of just using the already existing
Any
? • Why do we need to use it as an upper bound to any external generic function? (i.e.)
Copy code
external Box<T> // fails with 'Type Any? cannot be used in type parameter upper bound'

external Box<T: JsAny?> // passes
But this now makes my common classes unusable. see code below
Copy code
class Person(val name: String)

val boxed: Box<Person> // fails with 'Exepcted JsAny found Person'
If I extend
JsAny
(which is dirty altogether)
Copy code
class Person(val name: String) : JsAny // fails with 'Non-external type extends external type JsAny'
• If we must introduce
JsAny
could we at least make it similar to
Any
? coz as of now,
JsAny
is an external interface while
Any
is an open class, which does prohibit expect/actual all together. Clearly I am missing something here. How do I go about defining external generics and using it from classes defined in my common code??
👀 1
1
I am stumbling across
toJsReference()
am I heading in the right direction?
I think I am slowly getting (typing here to be corrected if I am getting it all wrong). Wasm only holds a reference to the objects from javascript not the objects themselves. This is being reflected in the Kotlin/Wasm api with the JsReference, correct??
s
Hi! Kotlin Wasm supports very basic interop with JS at the moment. You can exchange individual numbers, String, and lambdas, and we are working on expanding on it to support a lot more useful common types like collections and classes. > Why did the kotlin/wasm team introduce
JsAny
instead of just using the already existing
Any
? Existing
Any
static type provides too little information for compiler to convert values between JS and Kotlin. For example, if you receive JS
number
, compiler won’t know if you want
Double
or
Int
to be put into your
Any
JsAny
is added as a general low-level way to reference any JS value. You can use it as a “handle” to JS world in Kotlin. Compiler doesn’t convert
JsAny
values to Kotlin automatically, so it can’t choose the wrong thing. You can model a richer JS type hierarchy with external types, like
external class C : JsAny { ... }
, and use
JsAny
in type parameters upper-bounds. > Why do we need to use it as an upper bound to any external generic function? We are thinking on how to relax this restriction. In general, type parameters in Kotlin are erased to its upper bound, so if we fully support
<T>
, we should support
Any?
as a default upper bound. > I am stumbling across
toJsReference()
am I heading in the right direction? JsReference is another low-level primitive, but used in opposite direction. It can provide JS with a reference to any Kotlin value. But, like
JsAny
, it is not converted, an you can’t do much with it directly in JS, but you can export Kotlin “helper methods” that accept
JsReference
as “this” parameter. All of this provide a very low-level, but functionally complete foundation to do any interop. The goal is for it to be useful in common code, and we need to add a lot more things in future versions!
🙏 2
r
a
@Svyatoslav Kuzmich [JB] Asking for a bit more clarification here with this statement
JsReference is another low-level primitive, but used in opposite direction. It can provide JS with a reference to any Kotlin value
If it provide's JS with a reference to any kotlin value, wouldn't it make more sense if the method was called,
toKotlinReference()
coz it provides a kotlin reference and not a js reference?
s
Agreed, the name is not perfect. But neither is
toKotlinReference
, you can say that
Any
is already a Kotlin reference 🙂 . Suggestions for a better name for
toJsValueRepresentingOpaqueKotlinReference
are more than welcome!
We used to have it named
toJsHandle
, but it was also confusing for some people.
a
hahahahahaa thats too long
But reading JsReference quickly tells my brain to expext a JsReference, but apparently its a kotlin reference
s
True, I prefer think about current name as a
JsReference<ToKotlinType>
a
Now it all makes sense. Apparently the full sentence was hidden by type inference all along 😃
Thanks for being such a cool instructor