https://kotlinlang.org logo
#javascript
Title
# javascript
n

natpryce

03/11/2023, 9:33 AM
Asking in #javascript because I didn’t get a response in #multiplatform… Does anyone have an example of a multiplatform JVM & Javascript project that uses the W3C DOM API? I’m not sure how to define the
expect
declarations because the DOM API is mapped to interfaces in the JVM platform and classes in the Javascript platform. The DOM objects are provided by factories in both platforms, so my code only needs to use the DOM objects, not instantiate the classes nor implement the interfaces. What should the expect declaration be?
t

turansky

03/11/2023, 3:02 PM
Which DOM class you want to use as alias?
Definetely works following example:
Copy code
// common
expect class Color

// JS
@Suppress("ACTUAL_WITHOUT_EXPECT")
actual typealias Color = csstype.Color
csstype.Color
- external interface
n

natpryce

03/11/2023, 4:46 PM
And what would the declaration be for the JVM platform?
I want to use the W3C XML DOM APIs in the org.w3c package – Node, Element, Attr etc.
For example, on the JVM platform, Element is declared as
public interface Element extends Node
while on the JS platform it is declared as
public abstract external class Element
.
On both platforms, the types are declared in org.w3c.dom, so I want to write my code that uses the DOM in the common module and declare an
expect
function that loads a document, implemented by platform-specific
actual
code in the JVM and JS modules.
But I’ve not been able to work out how to declare that
t

turansky

03/11/2023, 4:53 PM
Common = Like in JVM JS - with
Suppress
n

natpryce

03/11/2023, 4:55 PM
So the common module would have to declare `expect`s for all the types in the org.w3c package?
(I was hoping to avoid that because both platforms already have the org.w3c package)
t

turansky

03/11/2023, 4:56 PM
Looks like it can be generated 🙂
n

natpryce

03/11/2023, 4:56 PM
Yes… that’s looking like the best approach
Thanks
t

turansky

03/11/2023, 4:58 PM
FYI - latest Browser declarations you can find here
What is the final goal? If it’s not a secret
n

natpryce

03/11/2023, 5:04 PM
I have a library for fuzz-testing various structured data formats and encodings. It is compiled to JVM & JS with a Makefile and the command line Kotlin compiler. The multi-platform build uses a directory of cross-platform code and directories of platform-specific code, and each platform build compiles all the cross-platform code and platform-specific code as a single compilation unit. It works very well, and thanks to Make builds much faster than Gradle. However, there is so little support for the command-line tools that upgrading to 1.8 and the new IR is very difficult. So if I can get a low-maintenance project structure working with kotlin-multiplatform I’ll bite the bullet and move to Gradle.
t

turansky

03/12/2023, 10:26 PM
I expect desynchronization between JVM and JS implementations. How you will calculate common part between JVM and JS?
2 Views