Skip to content

Commit 81f5fd9

Browse files
committed
fix wasmJs fs
1 parent 2938caa commit 81f5fd9

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

runtime/wasmJsMain/src/kotlinx/benchmark/NodeJsEngineSupport.kt

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,37 @@ package kotlinx.benchmark
33
import kotlin.time.DurationUnit
44
import kotlin.time.toDuration
55

6-
@JsFun("(path, text) => require('fs').writeFileSync(path, text, 'utf8')")
7-
private external fun nodeJsWriteFile(path: String, text: String)
6+
@JsModule("fs")
7+
external object fs {
8+
fun readFileSync(path: String, options: WriteFileOptions): String
9+
fun writeFileSync(path: String, data: String, options: WriteFileOptions)
10+
}
11+
12+
external interface WriteFileOptions : JsAny {
13+
var encoding: String?
14+
var mode: String?
15+
var flag: String?
16+
}
17+
18+
fun writeFileOptions(block: WriteFileOptions.() -> Unit): WriteFileOptions =
19+
js("{}").unsafeCast<WriteFileOptions>().apply(block)
820

9-
@JsFun("(path) => require('fs').readFileSync(path, 'utf8')")
10-
private external fun nodeJsReadFile(path: String): String
21+
//
22+
//@JsModule("fs")
23+
//private external fun writeFileSync(path: String, text: String, options: String)
24+
//
25+
//@JsModule("fs")
26+
//private external fun readFileSync(path: String, options: String): String
1127

1228
@JsFun("() => process.argv.slice(2).join(' ')")
1329
private external fun nodeJsArguments(): String
1430

1531
internal object NodeJsEngineSupport : JsEngineSupport() {
1632
override fun writeFile(path: String, text: String) =
17-
nodeJsWriteFile(path, text)
33+
fs.writeFileSync(path, text, writeFileOptions { encoding = "utf8" } )
1834

1935
override fun readFile(path: String): String =
20-
nodeJsReadFile(path)
36+
fs.readFileSync(path, writeFileOptions { encoding = "utf8" } )
2137

2238
override fun arguments(): Array<out String> =
2339
nodeJsArguments().split(' ').toTypedArray()

0 commit comments

Comments
 (0)