File tree Expand file tree Collapse file tree 7 files changed +41
-90
lines changed
nodeFilesystemShared/src/node Expand file tree Collapse file tree 7 files changed +41
-90
lines changed Load Diff This file was deleted.
Original file line number Diff line number Diff line change 5
5
6
6
package kotlinx.io.node
7
7
8
+ import kotlinx.io.withCaughtException
9
+
8
10
internal external interface BufferModule {
9
11
val Buffer : BufferObj
10
12
}
@@ -22,4 +24,8 @@ internal external interface Buffer {
22
24
fun writeInt8 (value : Byte , offset : Int )
23
25
}
24
26
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')" )
Original file line number Diff line number Diff line change @@ -86,4 +86,8 @@ internal external interface realpathSync {
86
86
fun native (path : String ): String
87
87
}
88
88
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')" )
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -18,4 +18,8 @@ internal external interface Os {
18
18
fun platform (): String
19
19
}
20
20
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')" )
Original file line number Diff line number Diff line change @@ -14,4 +14,8 @@ internal external interface Path {
14
14
val sep: String
15
15
}
16
16
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')" )
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments