I am attempting to use an NPM dependency from the ...
# javascript
j
I am attempting to use an NPM dependency from the JS portion of an MPP project. Specifically, I am trying to construct an object of a class from the NPM. Specifically, the nvk NPM. I have added a file,
nvk.kt
which contains:
Copy code
@file:JsModule("nvk")
@file:JsNonModule
package nvk

external class VulkanWindow
And in the JS source set, I am trying to define an
actual
for an
expected
as seen here:
Copy code
actual class VkWindow {

    val window: VulkanWindow

    actual constructor(width: Int, height: Int, title: String) {
        window = VulkanWindow::class.js.createInstance()
        window.asDynamic()["width"] = width
        window.asDynamic()["height"] = height
        window.asDynamic()["title"] = title
    }
}
Where
createInstance()
is defined following the discussion at https://discuss.kotlinlang.org/t/is-there-a-way-to-use-the-new-operator-with-arguments-on-a-dynamic-variable-in-kotlin-javascript/6126/4 as:
Copy code
fun <T : Any> JsClass<T>.createInstance(vararg args: dynamic): T {
    @Suppress("UNUSED_VARIABLE")
    val ctor = this

    @Suppress("UNUSED_VARIABLE")
    val argsArray = (listOf(null) + args).toTypedArray()

    //language=JavaScript 1.6
    return js("new (Function.prototype.bind.apply(ctor, argsArray))").unsafeCast<T>()
}
This all compiles fine but when I try and run it via
jsNodeRun
I get the following error:
return js("new (Function.prototype.bind.apply(ctor, argsArray))").unsafeCast<T>()
^
Error: Argument 1 must be of type 'Object'
To be honest, I don't have a particular preference on how I instantiate objects from this module, though I would like to be able to eventually do it with arguments. Any guidance would be greatly appreciated.
t
Do you need browser API in your project?
j
not strictly, no. I was hopeful I could eventually get there but i would settle for node
d
nvk only works on nodejs anyway.
j
so that answers that, node only
its all i have been trying so far anyway