Skip to content

Commit d810e28

Browse files
committed
Unify module loading for js and wasmJs
1 parent 20b6bd7 commit d810e28

File tree

7 files changed

+41
-90
lines changed

7 files changed

+41
-90
lines changed

core/js/src/node/nodeModulesJs.kt

Lines changed: 0 additions & 39 deletions
This file was deleted.

core/nodeFilesystemShared/src/node/buffer.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
package kotlinx.io.node
77

8+
import kotlinx.io.withCaughtException
9+
810
internal external interface BufferModule {
911
val Buffer: BufferObj
1012
}
@@ -22,4 +24,8 @@ internal external interface Buffer {
2224
fun writeInt8(value: Byte, offset: Int)
2325
}
2426

25-
internal expect val buffer: BufferModule
27+
internal val buffer: BufferModule by lazy {
28+
loadModule("buffer", ::bufferInitializer)
29+
}
30+
31+
private fun bufferInitializer(): BufferModule? = js("eval('require')('buffer')")

core/nodeFilesystemShared/src/node/fs.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,8 @@ internal external interface realpathSync {
8686
fun native(path: String): String
8787
}
8888

89-
internal expect val fs: Fs
89+
internal val fs: Fs by lazy {
90+
loadModule("fs", ::fsInitializer)
91+
}
92+
93+
private fun fsInitializer(): Fs? = js("eval('require')('fs')")
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
3+
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE.txt file.
4+
*/
5+
6+
package kotlinx.io.node
7+
8+
import kotlinx.io.withCaughtException
9+
10+
internal fun <T> loadModule(name: String, initializer: () -> T?): T {
11+
var mod: T? = null
12+
val ex = withCaughtException {
13+
mod = initializer()
14+
}
15+
if (mod == null) {
16+
throw UnsupportedOperationException("Module '$name' could not be loaded", ex)
17+
}
18+
return mod!!
19+
}

core/nodeFilesystemShared/src/node/os.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,8 @@ internal external interface Os {
1818
fun platform(): String
1919
}
2020

21-
internal expect val os: Os
21+
internal val os: Os by lazy {
22+
loadModule("os", ::osInitializer)
23+
}
24+
25+
private fun osInitializer(): Os? = js("eval('require')('os')")

core/nodeFilesystemShared/src/node/path.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,8 @@ internal external interface Path {
1414
val sep: String
1515
}
1616

17-
internal expect val path: Path
17+
internal val path: Path by lazy {
18+
loadModule("path", ::pathInitializer)
19+
}
20+
21+
private fun pathInitializer(): Path? = js("eval('require')('path')")

core/wasmJs/src/node/nodeModulesWasmJs.kt

Lines changed: 0 additions & 47 deletions
This file was deleted.

0 commit comments

Comments
 (0)