@live-codes/ocaml 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +220 -0
- package/README.md +165 -2
- package/THIRD-PARTY-NOTICES.md +52 -0
- package/assets/compiler/compile_stdout.browser.js +27856 -0
- package/assets/compiler/jsoo.js +12631 -0
- package/assets/compiler/package.json +3 -0
- package/assets/compiler/runtime/bigarray.js +1072 -0
- package/assets/compiler/runtime/bigstring.js +137 -0
- package/assets/compiler/runtime/domain.js +393 -0
- package/assets/compiler/runtime/dynlink.js +91 -0
- package/assets/compiler/runtime/float32.js +486 -0
- package/assets/compiler/runtime/fs.js +416 -0
- package/assets/compiler/runtime/fs_fake.js +600 -0
- package/assets/compiler/runtime/fs_node.js +609 -0
- package/assets/compiler/runtime/fs_quickjs.js +532 -0
- package/assets/compiler/runtime/gc.js +201 -0
- package/assets/compiler/runtime/graphics.js +1053 -0
- package/assets/compiler/runtime/ieee_754.js +621 -0
- package/assets/compiler/runtime/int64.js +516 -0
- package/assets/compiler/runtime/io.js +739 -0
- package/assets/compiler/runtime/jslib.js +553 -0
- package/assets/compiler/runtime/jslib_js_of_ocaml.js +81 -0
- package/assets/compiler/runtime/lexing.js +240 -0
- package/assets/compiler/runtime/manifest.txt +31 -0
- package/assets/compiler/runtime/marshal.js +863 -0
- package/assets/compiler/runtime/md5.js +236 -0
- package/assets/compiler/runtime/mlBytes.js +1007 -0
- package/assets/compiler/runtime/nat.js +469 -0
- package/assets/compiler/runtime/parsing.js +317 -0
- package/assets/compiler/runtime/prng.js +84 -0
- package/assets/compiler/runtime/promise.js +45 -0
- package/assets/compiler/runtime/runtime.js +16688 -0
- package/assets/compiler/runtime/runtime_events.js +90 -0
- package/assets/compiler/runtime/stdlib.js +363 -0
- package/assets/compiler/runtime/sync.js +35 -0
- package/assets/compiler/runtime/toplevel.js +150 -0
- package/assets/compiler/runtime/unix.js +914 -0
- package/assets/compiler/runtime/weak.js +247 -0
- package/assets/compiler/stdlib.js +254 -0
- package/assets/compiler/worker.js +46 -0
- package/assets/interpreter/libs/greet.cma +0 -0
- package/assets/interpreter/libs/greet.cmi +0 -0
- package/assets/interpreter/libs/manifest.txt +1 -0
- package/assets/interpreter/package.json +3 -0
- package/assets/interpreter/stdlib/camlinternalFormat.cmi +0 -0
- package/assets/interpreter/stdlib/camlinternalFormatBasics.cmi +0 -0
- package/assets/interpreter/stdlib/camlinternalLazy.cmi +0 -0
- package/assets/interpreter/stdlib/camlinternalMod.cmi +0 -0
- package/assets/interpreter/stdlib/camlinternalOO.cmi +0 -0
- package/assets/interpreter/stdlib/std_exit.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Arg.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Array.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__ArrayLabels.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Atomic.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Bigarray.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Bool.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Buffer.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Bytes.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__BytesLabels.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Callback.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Char.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Complex.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Condition.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Digest.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Domain.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Dynarray.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Effect.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Either.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Ephemeron.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Filename.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Float.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Format.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Fun.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Gc.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Hashtbl.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Iarray.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__In_channel.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Int.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Int32.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Int64.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Lazy.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Lexing.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__List.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__ListLabels.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Map.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Marshal.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__MoreLabels.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Mutex.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Nativeint.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Obj.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Oo.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Option.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Out_channel.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Pair.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Parsing.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Pqueue.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Printexc.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Printf.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Queue.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Random.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Repr.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Result.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Scanf.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Semaphore.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Seq.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Set.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Stack.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__StdLabels.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__String.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__StringLabels.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Sys.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Type.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Uchar.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Unit.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Weak.cmi +0 -0
- package/assets/interpreter/stdlib/topdirs.cmi +0 -0
- package/assets/interpreter/stdlib/units.txt +75 -0
- package/assets/interpreter/stdlib/unix.cmi +0 -0
- package/assets/interpreter/stdlib/unixLabels.cmi +0 -0
- package/assets/interpreter/worker-entry.js +39 -0
- package/assets/interpreter/worker.js +42356 -0
- package/bin/copy-assets.mjs +75 -0
- package/package.json +41 -4
- package/src/compiler/compile.js +170 -0
- package/src/compiler/index.js +46 -0
- package/src/compiler/worker.js +46 -0
- package/src/index.js +12 -0
- package/src/interpreter/api.js +182 -0
- package/src/interpreter/index.js +5 -0
|
@@ -0,0 +1,416 @@
|
|
|
1
|
+
// Js_of_ocaml runtime support
|
|
2
|
+
// http://www.ocsigen.org/js_of_ocaml/
|
|
3
|
+
// Copyright (C) 2014 Jérôme Vouillon, Hugo Heuzard
|
|
4
|
+
// Laboratoire PPS - CNRS Université Paris Diderot
|
|
5
|
+
//
|
|
6
|
+
// This program is free software; you can redistribute it and/or modify
|
|
7
|
+
// it under the terms of the GNU Lesser General Public License as published by
|
|
8
|
+
// the Free Software Foundation, with linking exception;
|
|
9
|
+
// either version 2.1 of the License, or (at your option) any later version.
|
|
10
|
+
//
|
|
11
|
+
// This program is distributed in the hope that it will be useful,
|
|
12
|
+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13
|
+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14
|
+
// GNU Lesser General Public License for more details.
|
|
15
|
+
//
|
|
16
|
+
// You should have received a copy of the GNU Lesser General Public License
|
|
17
|
+
// along with this program; if not, write to the Free Software
|
|
18
|
+
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
19
|
+
|
|
20
|
+
///////////// Dummy filesystem
|
|
21
|
+
|
|
22
|
+
//Provides: caml_trailing_slash
|
|
23
|
+
function caml_trailing_slash(name) {
|
|
24
|
+
return name.slice(-1) !== "/" ? name + "/" : name;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
//Provides: caml_current_dir
|
|
28
|
+
//Requires: caml_trailing_slash, fs_node_supported, fs_quickjs_supported
|
|
29
|
+
var caml_current_dir = (function () {
|
|
30
|
+
if (fs_node_supported() && globalThis.process?.cwd)
|
|
31
|
+
return globalThis.process.cwd().replace(/\\/g, "/");
|
|
32
|
+
if (fs_quickjs_supported()) {
|
|
33
|
+
var r = globalThis.os.getcwd();
|
|
34
|
+
if (r[1] === 0) return r[0];
|
|
35
|
+
}
|
|
36
|
+
return "/static";
|
|
37
|
+
})();
|
|
38
|
+
caml_current_dir = caml_trailing_slash(caml_current_dir);
|
|
39
|
+
|
|
40
|
+
//Provides: caml_get_root
|
|
41
|
+
//Requires: path_is_absolute
|
|
42
|
+
function caml_get_root(path) {
|
|
43
|
+
var x = path_is_absolute(path);
|
|
44
|
+
if (!x) return;
|
|
45
|
+
return x[0] + "/";
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
//Provides: caml_root
|
|
49
|
+
//Requires: caml_get_root, caml_current_dir, caml_failwith
|
|
50
|
+
var caml_root =
|
|
51
|
+
caml_get_root(caml_current_dir) ||
|
|
52
|
+
caml_failwith("unable to compute caml_root");
|
|
53
|
+
|
|
54
|
+
//Provides: MlFile
|
|
55
|
+
function MlFile() {}
|
|
56
|
+
|
|
57
|
+
//Provides: path_is_absolute
|
|
58
|
+
//Requires: fs_node_supported
|
|
59
|
+
//Requires: jsoo_is_win32
|
|
60
|
+
function make_path_is_absolute() {
|
|
61
|
+
function posix(path) {
|
|
62
|
+
if (path.charAt(0) === "/") return ["", path.slice(1)];
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function win32(path) {
|
|
67
|
+
// https://github.com/nodejs/node/blob/b3fcc245fb25539909ef1d5eaa01dbf92e168633/lib/path.js#L56
|
|
68
|
+
var splitDeviceRe =
|
|
69
|
+
/^([a-zA-Z]:|[\\/]{2}[^\\/]+[\\/]+[^\\/]+)?([\\/])?([\s\S]*?)$/;
|
|
70
|
+
var result = splitDeviceRe.exec(path);
|
|
71
|
+
var device = result[1] || "";
|
|
72
|
+
var isUnc = device.length > 0 && device.charAt(1) !== ":";
|
|
73
|
+
|
|
74
|
+
// UNC paths are always absolute
|
|
75
|
+
if (result[2] || isUnc) {
|
|
76
|
+
var root = result[1] || "";
|
|
77
|
+
var sep = result[2] || "";
|
|
78
|
+
return [root, path.slice(root.length + sep.length)];
|
|
79
|
+
}
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
return jsoo_is_win32 ? win32 : posix;
|
|
83
|
+
}
|
|
84
|
+
var path_is_absolute = make_path_is_absolute();
|
|
85
|
+
|
|
86
|
+
//Provides: caml_make_path
|
|
87
|
+
//Requires: caml_current_dir
|
|
88
|
+
//Requires: caml_jsstring_of_string, path_is_absolute, caml_raise_system_error
|
|
89
|
+
function caml_make_path(name, raise_unix) {
|
|
90
|
+
name = caml_jsstring_of_string(name);
|
|
91
|
+
// An empty path is invalid: like native (and the wasm runtime), it must not
|
|
92
|
+
// resolve to the current directory but fail with ENOENT. [raise_unix]
|
|
93
|
+
// selects Unix_error (Unix callers) vs Sys_error (Sys callers).
|
|
94
|
+
if (name === "")
|
|
95
|
+
caml_raise_system_error(
|
|
96
|
+
raise_unix,
|
|
97
|
+
"ENOENT",
|
|
98
|
+
"",
|
|
99
|
+
"No such file or directory",
|
|
100
|
+
"",
|
|
101
|
+
);
|
|
102
|
+
if (!path_is_absolute(name)) name = caml_current_dir + name;
|
|
103
|
+
var comp0 = path_is_absolute(name);
|
|
104
|
+
var comp = comp0[1].split(/[/\\]/);
|
|
105
|
+
var ncomp = [];
|
|
106
|
+
for (var i = 0; i < comp.length; i++) {
|
|
107
|
+
switch (comp[i]) {
|
|
108
|
+
case "..":
|
|
109
|
+
ncomp.pop();
|
|
110
|
+
break;
|
|
111
|
+
case ".":
|
|
112
|
+
break;
|
|
113
|
+
case "":
|
|
114
|
+
break;
|
|
115
|
+
default:
|
|
116
|
+
ncomp.push(comp[i]);
|
|
117
|
+
break;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
ncomp.unshift(comp0[0]);
|
|
121
|
+
ncomp.orig = name;
|
|
122
|
+
return ncomp;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// QuickJS filesystem support can be added with by passing
|
|
126
|
+
// +fs_quickjs.js to the command line. It's not included by default to
|
|
127
|
+
// avoid bloat.
|
|
128
|
+
|
|
129
|
+
//Provides: MlQuickJSDevice
|
|
130
|
+
//Weakdef
|
|
131
|
+
class MlQuickJSDevice {}
|
|
132
|
+
|
|
133
|
+
//Provides: fs_quickjs_supported
|
|
134
|
+
//Weakdef
|
|
135
|
+
function fs_quickjs_supported() {
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
//Provides: caml_sys_open_for_quickjs
|
|
140
|
+
//Weakdef
|
|
141
|
+
function caml_sys_open_for_quickjs(_fd, _flags) {
|
|
142
|
+
return null;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
//Provides:jsoo_mount_point
|
|
146
|
+
//Requires: MlFakeDevice, MlNodeDevice, MlQuickJSDevice, caml_root, fs_node_supported, fs_quickjs_supported
|
|
147
|
+
var jsoo_mount_point = [];
|
|
148
|
+
if (fs_node_supported()) {
|
|
149
|
+
jsoo_mount_point.push({
|
|
150
|
+
path: caml_root,
|
|
151
|
+
device: new MlNodeDevice(caml_root),
|
|
152
|
+
});
|
|
153
|
+
} else if (fs_quickjs_supported()) {
|
|
154
|
+
jsoo_mount_point.push({
|
|
155
|
+
path: caml_root,
|
|
156
|
+
device: new MlQuickJSDevice(caml_root),
|
|
157
|
+
});
|
|
158
|
+
} else {
|
|
159
|
+
jsoo_mount_point.push({
|
|
160
|
+
path: caml_root,
|
|
161
|
+
device: new MlFakeDevice(caml_root),
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
jsoo_mount_point.push({
|
|
165
|
+
path: "/static/",
|
|
166
|
+
device: new MlFakeDevice("/static/"),
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
//Provides:caml_list_mount_point
|
|
170
|
+
//Requires: jsoo_mount_point, caml_string_of_jsstring
|
|
171
|
+
function caml_list_mount_point() {
|
|
172
|
+
var prev = 0;
|
|
173
|
+
for (var i = 0; i < jsoo_mount_point.length; i++) {
|
|
174
|
+
var old = prev;
|
|
175
|
+
prev = [0, caml_string_of_jsstring(jsoo_mount_point[i].path), old];
|
|
176
|
+
}
|
|
177
|
+
return prev;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
//Provides: resolve_fs_device
|
|
181
|
+
//Requires: caml_make_path, jsoo_mount_point, caml_raise_sys_error, caml_get_root, MlNodeDevice, caml_trailing_slash, fs_node_supported
|
|
182
|
+
function resolve_fs_device(name, raise_unix) {
|
|
183
|
+
var path = caml_make_path(name, raise_unix);
|
|
184
|
+
var name = path.join("/");
|
|
185
|
+
var name_slash = caml_trailing_slash(name);
|
|
186
|
+
var res;
|
|
187
|
+
for (var i = 0; i < jsoo_mount_point.length; i++) {
|
|
188
|
+
var m = jsoo_mount_point[i];
|
|
189
|
+
if (
|
|
190
|
+
name_slash.startsWith(m.path) &&
|
|
191
|
+
(!res || res.path.length < m.path.length)
|
|
192
|
+
)
|
|
193
|
+
res = {
|
|
194
|
+
path: m.path,
|
|
195
|
+
device: m.device,
|
|
196
|
+
rest: name.slice(m.path.length, name.length),
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
if (!res && fs_node_supported()) {
|
|
200
|
+
var root = caml_get_root(name);
|
|
201
|
+
if (root?.match(/^[a-zA-Z]:\/$/)) {
|
|
202
|
+
var m = { path: root, device: new MlNodeDevice(root) };
|
|
203
|
+
jsoo_mount_point.push(m);
|
|
204
|
+
res = {
|
|
205
|
+
path: m.path,
|
|
206
|
+
device: m.device,
|
|
207
|
+
rest: name.slice(m.path.length, name.length),
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
if (res) return res;
|
|
212
|
+
caml_raise_sys_error("no device found for " + name_slash);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
//Provides: caml_mount_autoload
|
|
216
|
+
//Requires: MlFakeDevice, caml_make_path, jsoo_mount_point, caml_trailing_slash
|
|
217
|
+
function caml_mount_autoload(name, f) {
|
|
218
|
+
var path = caml_make_path(name);
|
|
219
|
+
var name = caml_trailing_slash(path.join("/"));
|
|
220
|
+
jsoo_mount_point.push({ path: name, device: new MlFakeDevice(name, f) });
|
|
221
|
+
return 0;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
//Provides: caml_unmount
|
|
225
|
+
//Requires: jsoo_mount_point, caml_make_path, caml_trailing_slash
|
|
226
|
+
function caml_unmount(name) {
|
|
227
|
+
var path = caml_make_path(name);
|
|
228
|
+
var name = caml_trailing_slash(path.join("/"));
|
|
229
|
+
var idx = -1;
|
|
230
|
+
for (var i = 0; i < jsoo_mount_point.length; i++)
|
|
231
|
+
if (jsoo_mount_point[i].path === name) idx = i;
|
|
232
|
+
if (idx > -1) jsoo_mount_point.splice(idx, 1);
|
|
233
|
+
return 0;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
//Provides: caml_sys_getcwd
|
|
237
|
+
//Requires: caml_current_dir, caml_string_of_jsstring
|
|
238
|
+
//Alias: caml_unix_getcwd
|
|
239
|
+
//Alias: unix_getcwd
|
|
240
|
+
function caml_sys_getcwd() {
|
|
241
|
+
return caml_string_of_jsstring(caml_current_dir);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
//Provides: caml_sys_chdir
|
|
245
|
+
//Requires: caml_current_dir, caml_raise_no_such_file, resolve_fs_device, caml_trailing_slash, caml_jsstring_of_string, caml_raise_system_error
|
|
246
|
+
function caml_sys_chdir(dir, raise_unix) {
|
|
247
|
+
var root = resolve_fs_device(dir);
|
|
248
|
+
if (root.device.is_dir(root.rest)) {
|
|
249
|
+
if (root.rest)
|
|
250
|
+
caml_current_dir = caml_trailing_slash(root.path + root.rest);
|
|
251
|
+
else caml_current_dir = root.path;
|
|
252
|
+
return 0;
|
|
253
|
+
} else if (root.device.exists(root.rest)) {
|
|
254
|
+
caml_raise_system_error(
|
|
255
|
+
raise_unix,
|
|
256
|
+
"ENOTDIR",
|
|
257
|
+
"chdir",
|
|
258
|
+
"not a directory",
|
|
259
|
+
caml_jsstring_of_string(dir),
|
|
260
|
+
);
|
|
261
|
+
} else {
|
|
262
|
+
caml_raise_no_such_file(caml_jsstring_of_string(dir), raise_unix, "chdir");
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
//Provides: caml_raise_no_such_file
|
|
267
|
+
//Requires: caml_raise_system_error, caml_raise_sys_error
|
|
268
|
+
function caml_raise_no_such_file(name, raise_unix, cmd) {
|
|
269
|
+
if (raise_unix)
|
|
270
|
+
caml_raise_system_error(
|
|
271
|
+
raise_unix,
|
|
272
|
+
"ENOENT",
|
|
273
|
+
cmd || "open",
|
|
274
|
+
"no such file or directory",
|
|
275
|
+
name,
|
|
276
|
+
);
|
|
277
|
+
// match the native Sys_error message
|
|
278
|
+
caml_raise_sys_error(name + ": No such file or directory");
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
//Provides: caml_sys_file_exists
|
|
282
|
+
//Requires: resolve_fs_device, caml_jsstring_of_string
|
|
283
|
+
function caml_sys_file_exists(name) {
|
|
284
|
+
// An empty path exists nowhere; like native, return false rather than
|
|
285
|
+
// resolving "" to the current directory (which always exists).
|
|
286
|
+
if (caml_jsstring_of_string(name) === "") return 0;
|
|
287
|
+
var root = resolve_fs_device(name);
|
|
288
|
+
return root.device.exists(root.rest);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
//Provides: caml_sys_read_directory
|
|
292
|
+
//Requires: caml_string_of_jsstring
|
|
293
|
+
//Requires: resolve_fs_device
|
|
294
|
+
function caml_sys_read_directory(name) {
|
|
295
|
+
var root = resolve_fs_device(name);
|
|
296
|
+
var a = root.device.readdir(root.rest);
|
|
297
|
+
var l = new Array(a.length + 1);
|
|
298
|
+
l[0] = 0;
|
|
299
|
+
for (var i = 0; i < a.length; i++) l[i + 1] = caml_string_of_jsstring(a[i]);
|
|
300
|
+
return l;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
//Provides: caml_sys_remove
|
|
304
|
+
//Requires: resolve_fs_device
|
|
305
|
+
function caml_sys_remove(name) {
|
|
306
|
+
var root = resolve_fs_device(name);
|
|
307
|
+
return root.device.unlink(root.rest);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
//Provides: caml_sys_is_directory
|
|
311
|
+
//Requires: resolve_fs_device
|
|
312
|
+
function caml_sys_is_directory(name) {
|
|
313
|
+
var root = resolve_fs_device(name);
|
|
314
|
+
var a = root.device.is_dir(root.rest);
|
|
315
|
+
return a ? 1 : 0;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
//Provides: caml_sys_rename
|
|
319
|
+
//Requires: caml_failwith, resolve_fs_device, caml_raise_sys_error
|
|
320
|
+
function caml_sys_rename(o, n) {
|
|
321
|
+
var o_root = resolve_fs_device(o);
|
|
322
|
+
var n_root = resolve_fs_device(n);
|
|
323
|
+
if (o_root.device !== n_root.device)
|
|
324
|
+
// native raises Sys_error (strerror of EXDEV)
|
|
325
|
+
caml_raise_sys_error("Invalid cross-device link");
|
|
326
|
+
if (!o_root.device.rename) caml_failwith("caml_sys_rename: not implemented");
|
|
327
|
+
o_root.device.rename(o_root.rest, n_root.rest);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
//Provides: caml_sys_mkdir
|
|
331
|
+
//Requires: resolve_fs_device
|
|
332
|
+
function caml_sys_mkdir(name, perm) {
|
|
333
|
+
var root = resolve_fs_device(name);
|
|
334
|
+
root.device.mkdir(root.rest, perm);
|
|
335
|
+
return 0;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
//Provides: caml_sys_rmdir
|
|
339
|
+
//Requires: resolve_fs_device
|
|
340
|
+
function caml_sys_rmdir(name) {
|
|
341
|
+
var root = resolve_fs_device(name);
|
|
342
|
+
root.device.rmdir(root.rest);
|
|
343
|
+
return 0;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
//Provides: caml_ba_map_file
|
|
347
|
+
//Requires: caml_failwith
|
|
348
|
+
function caml_ba_map_file(_vfd, _kind, _layout, _shared, _dims, _pos) {
|
|
349
|
+
// var data = caml_sys_fds[vfd];
|
|
350
|
+
caml_failwith("caml_ba_map_file not implemented");
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
//Provides: caml_ba_map_file_bytecode
|
|
354
|
+
//Requires: caml_ba_map_file
|
|
355
|
+
function caml_ba_map_file_bytecode(argv, _argn) {
|
|
356
|
+
// argn === 6
|
|
357
|
+
return caml_ba_map_file(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5]);
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
//Provides: jsoo_create_file_extern
|
|
361
|
+
function jsoo_create_file_extern(name, content) {
|
|
362
|
+
if (globalThis.jsoo_create_file) globalThis.jsoo_create_file(name, content);
|
|
363
|
+
else {
|
|
364
|
+
if (!globalThis.jsoo_fs_tmp) globalThis.jsoo_fs_tmp = [];
|
|
365
|
+
globalThis.jsoo_fs_tmp.push({ name: name, content: content });
|
|
366
|
+
}
|
|
367
|
+
return 0;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
//Provides: caml_fs_init
|
|
371
|
+
//Requires: jsoo_create_file
|
|
372
|
+
function caml_fs_init() {
|
|
373
|
+
var tmp = globalThis.jsoo_fs_tmp;
|
|
374
|
+
if (tmp) {
|
|
375
|
+
for (var i = 0; i < tmp.length; i++) {
|
|
376
|
+
jsoo_create_file(tmp[i].name, tmp[i].content);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
globalThis.jsoo_create_file = jsoo_create_file;
|
|
380
|
+
globalThis.jsoo_fs_tmp = [];
|
|
381
|
+
return 0;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
//Provides: caml_create_file
|
|
385
|
+
//Requires: caml_failwith, resolve_fs_device
|
|
386
|
+
function caml_create_file(name, content) {
|
|
387
|
+
var root = resolve_fs_device(name);
|
|
388
|
+
if (!root.device.register) caml_failwith("cannot register file");
|
|
389
|
+
root.device.register(root.rest, content);
|
|
390
|
+
return 0;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
//Provides: jsoo_create_file
|
|
394
|
+
//Requires: caml_create_file, caml_string_of_jsbytes, caml_string_of_jsstring
|
|
395
|
+
function jsoo_create_file(name, content) {
|
|
396
|
+
var name = caml_string_of_jsstring(name);
|
|
397
|
+
var content = caml_string_of_jsbytes(content);
|
|
398
|
+
return caml_create_file(name, content);
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
//Provides: caml_read_file_content
|
|
402
|
+
//Requires: resolve_fs_device, caml_raise_no_such_file, caml_string_of_uint8_array
|
|
403
|
+
//Requires: caml_string_of_jsstring, caml_jsstring_of_string
|
|
404
|
+
function caml_read_file_content(name) {
|
|
405
|
+
var name = typeof name === "string" ? caml_string_of_jsstring(name) : name;
|
|
406
|
+
var root = resolve_fs_device(name);
|
|
407
|
+
if (root.device.exists(root.rest)) {
|
|
408
|
+
var file = root.device.open(root.rest, { rdonly: 1 });
|
|
409
|
+
var len = file.length();
|
|
410
|
+
var buf = new Uint8Array(len);
|
|
411
|
+
file.read(buf, 0, len, false);
|
|
412
|
+
file.close();
|
|
413
|
+
return caml_string_of_uint8_array(buf);
|
|
414
|
+
}
|
|
415
|
+
caml_raise_no_such_file(caml_jsstring_of_string(name));
|
|
416
|
+
}
|