Skip to content

Commit 65901fb

Browse files
committed
internal(fs): add js support
1 parent 6a8a49a commit 65901fb

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

fs_sync/fs_sync_js.mbt

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Copyright 2025 International Digital Economy Academy
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
///|
16+
extern "js" fn read_file_ffi(path : String) -> Int =
17+
#| function(path) {
18+
#| fs = require('fs');
19+
#| try {
20+
#| const content = fs.readFileSync(path);
21+
#| globalThis.fileContent = content;
22+
#| return 0;
23+
#| } catch (error) {
24+
#| globalThis.errorMessage = error.message;
25+
#| return -1;
26+
#| }
27+
#| }
28+
29+
///|
30+
extern "js" fn write_file_ffi(path : String, content : Bytes) -> Int =
31+
#| function(path, content) {
32+
#| fs = require('fs');
33+
#| try {
34+
#| fs.writeFileSync(path, Buffer.from(content));
35+
#| return 0;
36+
#| } catch (error) {
37+
#| globalThis.errorMessage = error.message;
38+
#| return -1;
39+
#| }
40+
#| }
41+
42+
///|
43+
extern "js" fn get_file_content_ffi() -> FixedArray[Byte] =
44+
#| function() {
45+
#| return globalThis.fileContent;
46+
#| }
47+
48+
///|
49+
extern "js" fn get_error_message_ffi() -> String =
50+
#| function() {
51+
#| return globalThis.errorMessage || '';
52+
#| }
53+
54+
///|
55+
fn read_file_to_bytes_internal(path : String) -> Bytes! {
56+
let res = read_file_ffi(path)
57+
if res == -1 {
58+
raise IOError(get_error_message_ffi())
59+
}
60+
let content = get_file_content_ffi()
61+
Bytes::from_iter(content.iter())
62+
}
63+
64+
fn read_file_to_string_internal(path : String, encoding~ : String = "utf8") -> String! {
65+
guard encoding == "utf8" else { raise IOError("Unsupported encoding: \{encoding}, only utf8 is supported for now") }
66+
let bytes = read_file_to_bytes_internal!(path)
67+
utf8_bytes_to_mbt_string(bytes)
68+
}
69+
70+
///|
71+
fn write_bytes_to_file_internal(path : String, content : Bytes) -> Unit! {
72+
let res = write_file_ffi(path, content)
73+
if res == -1 {
74+
raise IOError(get_error_message_ffi())
75+
}
76+
}
77+
78+
fn write_string_to_file_internal(path : String, content : String, encoding~ : String = "utf8") -> Unit! {
79+
guard encoding == "utf8" else { raise IOError("Unsupported encoding: \{encoding}, only utf8 is supported for now") }
80+
let bytes = mbt_string_to_utf8_bytes(content, false)
81+
write_bytes_to_file_internal!(path, bytes)
82+
}

0 commit comments

Comments
 (0)