Hello, I am trying to use in Android's WebView a ...
# javascript
t
Hello, I am trying to use in Android's WebView a project using Kotlin/JS. I am getting error
method can't be invoked on a non-injected object
Looking on Google, I found this: https://bugs.chromium.org/p/chromium/issues/detail?id=514628 So I looked into the code generated, and indeed, we are not doing what they recommend. The code generated gives:
Copy code
var tmp0_safe_receiver = Native.adOver;
return tmp0_safe_receiver == null ? null : tmp0_safe_receiver();
For
Copy code
external object Native {
    val adOver: (() -> Unit)?
}

Native.adOver?.invoke()
According to the report, we should generate:
Copy code
var tmp0_safe_receiver = Native.adOver.bind(Native);
Has it been reported yet? Is there a workaround (other than just use
js("Native.adOver?.()")
)
e
I think what Kotlin generates matches Kotlin semantics
d
Yeah, but it is an external declaration, so Kotlin semantics are irrelvant. The problem here is really that the
invoke
call is fiction, in reality you are invoking
adOver
as a function, if it is present. The correct code to preserve the correct receiver would be
Copy code
var temp = Native.adOver;
return temp == null ? null : temp.call(Native)
Or Kotlin could just target modern Javascript (please...):
Native.adOver?.()
e
that being said, if this is for a native bridge via webview: - consider using createWebMessageChannel() instead of addJavascriptInterface() - does it even need to be nullable?
external object Native { fun adOver() }; Native.adOver()
should generate the right code
t
Thanks for your advice! I'll take a look to createWebMessageChannel. I was blindly following this https://developer.android.com/guide/webapps/webview
d
This should still be a YouTrack issue, as this is a bug
e
it does feel strange to give
Copy code
Native.adOver?.invoke()
Native.adOver?.let { it.invoke() }
different behavior in Kotlin, though, even if it is an external object
it feels like the real interface is
Copy code
external object Native {
    val adOver: (Native.() -> Unit)?
}
but that's not (currently) legal
d
That is a good point... but the 2nd syntax has a different meaning
115 Views