Skip to content

Commit

Permalink
Add make_unix_err_args to cleanup Unix_error arg construction
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Sep 24, 2021
1 parent 743db86 commit fad3108
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 39 deletions.
13 changes: 7 additions & 6 deletions runtime/fs_fake.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
//Requires: caml_bytes_of_array, caml_bytes_of_string, caml_bytes_of_jsbytes
//Requires: caml_is_ml_bytes, caml_is_ml_string
//Requires: caml_named_value, caml_raise_with_args, caml_named_values
//Requires: make_unix_err_args
function MlFakeDevice (root, f) {
this.content={};
this.root = root;
Expand Down Expand Up @@ -67,7 +68,7 @@ MlFakeDevice.prototype.mkdir = function(name,mode, raise_unix) {
var unix_error = raise_unix && caml_named_value('Unix.Unix_error');
if(this.exists(name)) {
if (unix_error) {
caml_raise_with_args(unix_error, [8, caml_string_of_jsstring("mkdir"), caml_string_of_jsstring(this.nm(name))]);
caml_raise_with_args(unix_error, make_unix_err_args("EEXIST", "mkdir", this.nm(name)));
}
else {
caml_raise_sys_error(name + ": File exists");
Expand All @@ -77,15 +78,15 @@ MlFakeDevice.prototype.mkdir = function(name,mode, raise_unix) {
parent = (parent && parent[1]) || '';
if(!this.exists(parent)){
if (unix_error) {
caml_raise_with_args(unix_error, [20, caml_string_of_jsstring("mkdir"), caml_string_of_jsstring(this.nm(parent))]);
caml_raise_with_args(unix_error, make_unix_err_args("ENOENT", "mkdir", this.nm(parent)));
}
else {
caml_raise_sys_error(parent + ": No such file or directory");
}
}
if(!this.is_dir(parent)){
if (unix_error) {
caml_raise_with_args(unix_error, [26, caml_string_of_jsstring("mkdir"), caml_string_of_jsstring(this.nm(parent))]);
caml_raise_with_args(unix_error, make_unix_err_args("ENOTDIR", "mkdir", this.nm(parent)));
}
else {
caml_raise_sys_error(parent + ": Not a directory");
Expand All @@ -99,15 +100,15 @@ MlFakeDevice.prototype.rmdir = function(name, raise_unix) {
var r = new RegExp("^" + name_slash + "([^/]+)");
if(!this.exists(name)) {
if (unix_error) {
caml_raise_with_args(unix_error, [20, caml_string_of_jsstring("rmdir"), caml_string_of_jsstring(this.nm(name))]);
caml_raise_with_args(unix_error, make_unix_err_args("ENOENT", "rmdir", this.nm(name)));
}
else {
caml_raise_sys_error(name + ": No such file or directory");
}
}
if(!this.is_dir(name)) {
if (unix_error) {
caml_raise_with_args(unix_error, [26, caml_string_of_jsstring("rmdir"), caml_string_of_jsstring(this.nm(name))]);
caml_raise_with_args(unix_error, make_unix_err_args("ENOTDIR", "rmdir", this.nm(name)));
}
else {
caml_raise_sys_error(name + ": Not a directory");
Expand All @@ -116,7 +117,7 @@ MlFakeDevice.prototype.rmdir = function(name, raise_unix) {
for(var n in this.content) {
if(n.match(r)) {
if (unix_error) {
caml_raise_with_args(unix_error, [27, caml_string_of_jsstring("rmdir"), caml_string_of_jsstring(this.nm(name))]);
caml_raise_with_args(unix_error, make_unix_err_args("ENOTEMPTY", "rmdir", this.nm(name)));
} else {
caml_raise_sys_error(this.nm(name) + ": Directory not empty");
}
Expand Down
36 changes: 3 additions & 33 deletions runtime/fs_node.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,11 @@ function fs_node_supported () {


//Provides: MlNodeDevice
//Requires: MlNodeFile, caml_raise_sys_error
//Requires: caml_raise_with_args, caml_named_value, caml_string_of_jsstring
//Requires: MlNodeFile, caml_raise_sys_error, caml_raise_with_args
//Requires: make_unix_err_args, caml_named_value, caml_string_of_jsstring
function MlNodeDevice(root) {
this.fs = require('fs');
this.root = root;

/* ===Unix.error===
*
* This array is in order of the variant in OCaml
*
* Attached to the MlNodeDevice to avoid allocating this array
* each time an error is caught
*/
this.unix_error = [
"E2BIG", "EACCES", "EAGAIN", "EBADF", "EBUSY", "ECHILD", "EDEADLK", "EDOM",
"EEXIST", "EFAULT", "EFBIG", "EINTR", "EINVAL", "EIO", "EISDIR", "EMFILE",
"EMLINK", "ENAMETOOLONG", "ENFILE", "ENODEV", "ENOENT", "ENOEXEC", "ENOLCK",
"ENOMEM", "ENOSPC", "ENOSYS", "ENOTDIR", "ENOTEMPTY", "ENOTTY", "ENXIO",
"EPERM", "EPIPE", "ERANGE", "EROFS", "ESPIPE", "ESRCH", "EXDEV", "EWOULDBLOCK",
"EINPROGRESS", "EALREADY", "ENOTSOCK", "EDESTADDRREQ", "EMSGSIZE",
"EPROTOTYPE", "ENOPROTOOPT", "EPROTONOSUPPORT", "ESOCKTNOSUPPORT",
"EOPNOTSUPP", "EPFNOSUPPORT", "EAFNOSUPPORT", "EADDRINUSE", "EADDRNOTAVAIL",
"ENETDOWN", "ENETUNREACH", "ENETRESET", "ECONNABORTED", "ECONNRESET", "ENOBUFS",
"EISCONN", "ENOTCONN", "ESHUTDOWN", "ETOOMANYREFS", "ETIMEDOUT", "ECONNREFUSED",
"EHOSTDOWN", "EHOSTUNREACH", "ELOOP", "EOVERFLOW"
];
}
MlNodeDevice.prototype.nm = function(name) {
return (this.root + name);
Expand Down Expand Up @@ -172,16 +151,7 @@ MlNodeDevice.prototype.readlink = function(name, raise_unix) {
MlNodeDevice.prototype.raise_nodejs_error = function(err, raise_unix) {
var unix_error = caml_named_value("Unix.Unix_error");
if (raise_unix && unix_error) {
var variant = this.unix_error.indexOf(err.code);
if (variant < 0) {
// If none of the above variants, fallback to EUNKNOWNERR(int)
variant = BLOCK(0, err.errno);
}
var args = [
variant,
caml_string_of_jsstring(err.syscall || ""),
caml_string_of_jsstring(err.path || "")
];
var args = make_unix_err_args(err.code, err.syscall, err.path, err.errno);
caml_raise_with_args(unix_error, args);
} else {
caml_raise_sys_error(err.toString());
Expand Down
37 changes: 37 additions & 0 deletions runtime/unix.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,43 @@ function unix_isatty(fileDescriptor) {
}
}

//Provides: make_unix_err_args
//Requires: caml_string_of_jsstring
var unix_error = [
/* ===Unix.error===
*
* This array is in order of the variant in OCaml
*/
"E2BIG", "EACCES", "EAGAIN", "EBADF", "EBUSY", "ECHILD", "EDEADLK", "EDOM",
"EEXIST", "EFAULT", "EFBIG", "EINTR", "EINVAL", "EIO", "EISDIR", "EMFILE",
"EMLINK", "ENAMETOOLONG", "ENFILE", "ENODEV", "ENOENT", "ENOEXEC", "ENOLCK",
"ENOMEM", "ENOSPC", "ENOSYS", "ENOTDIR", "ENOTEMPTY", "ENOTTY", "ENXIO",
"EPERM", "EPIPE", "ERANGE", "EROFS", "ESPIPE", "ESRCH", "EXDEV", "EWOULDBLOCK",
"EINPROGRESS", "EALREADY", "ENOTSOCK", "EDESTADDRREQ", "EMSGSIZE",
"EPROTOTYPE", "ENOPROTOOPT", "EPROTONOSUPPORT", "ESOCKTNOSUPPORT",
"EOPNOTSUPP", "EPFNOSUPPORT", "EAFNOSUPPORT", "EADDRINUSE", "EADDRNOTAVAIL",
"ENETDOWN", "ENETUNREACH", "ENETRESET", "ECONNABORTED", "ECONNRESET", "ENOBUFS",
"EISCONN", "ENOTCONN", "ESHUTDOWN", "ETOOMANYREFS", "ETIMEDOUT", "ECONNREFUSED",
"EHOSTDOWN", "EHOSTUNREACH", "ELOOP", "EOVERFLOW"
];
function make_unix_err_args(code, syscall, path, errno) {
var variant = unix_error.indexOf(code);
if (variant < 0) {
// Default if undefined
if (errno == null) {
errno = -9999
}
// If none of the above variants, fallback to EUNKNOWNERR(int)
variant = BLOCK(0, errno);
}
var args = [
variant,
caml_string_of_jsstring(syscall || ""),
caml_string_of_jsstring(path || "")
];
return args;
}

//Provides: unix_stat
//Requires: resolve_fs_device, caml_failwith
function unix_stat(name) {
Expand Down

0 comments on commit fad3108

Please sign in to comment.