[njs] FS: module is rewritten using public API.

Dmitry Volyntsev xeioex at nginx.com
Wed May 3 04:13:33 UTC 2023


details:   https://hg.nginx.org/njs/rev/18385a4a90ad
branches:  
changeset: 2102:18385a4a90ad
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Tue May 02 20:50:55 2023 -0700
description:
FS: module is rewritten using public API.

diffstat:

 external/njs_fs_module.c   |  1485 ++++++++++++++++++++++---------------------
 src/njs.h                  |    11 +
 src/njs_value.c            |    31 +
 src/njs_value_conversion.h |    17 -
 src/njs_vm.c               |    56 +
 src/test/njs_unit_test.c   |    74 +-
 test/fs/methods.t.js       |    16 +-
 test/fs/promises_07.t.js   |     2 +-
 8 files changed, 898 insertions(+), 794 deletions(-)

diffs (truncated from 3111 to 1000 lines):

diff -r fd956d2a25a3 -r 18385a4a90ad external/njs_fs_module.c
--- a/external/njs_fs_module.c	Tue May 02 20:50:55 2023 -0700
+++ b/external/njs_fs_module.c	Tue May 02 20:50:55 2023 -0700
@@ -5,9 +5,11 @@
  */
 
 
-#include <njs_main.h>
-
+#include <njs.h>
+#include <njs_utils.h>
+#include <njs_buffer.h>
 #include <dirent.h>
+#include <njs_unix.h>
 
 #if (NJS_SOLARIS)
 
@@ -133,8 +135,8 @@ typedef struct {
 
 
 typedef struct {
-    njs_int_t       bytes;
-    njs_value_t     buffer;
+    njs_int_t           bytes;
+    njs_opaque_value_t  buffer;
 } njs_bytes_struct_t;
 
 
@@ -173,10 +175,8 @@ static njs_int_t njs_fs_write(njs_vm_t *
 static njs_int_t njs_fs_write_file(njs_vm_t *vm, njs_value_t *args,
     njs_uint_t nargs, njs_index_t calltype, njs_value_t *retval);
 
-static njs_int_t njs_fs_constants(njs_vm_t *vm, njs_object_prop_t *prop,
-    njs_value_t *value, njs_value_t *unused, njs_value_t *retval);
-static njs_int_t njs_fs_promises(njs_vm_t *vm, njs_object_prop_t *prop,
-    njs_value_t *value, njs_value_t *unused, njs_value_t *retval);
+static njs_int_t njs_fs_constant(njs_vm_t *vm, njs_object_prop_t *prop,
+    njs_value_t *value, njs_value_t *setval, njs_value_t *retval);
 
 static njs_int_t njs_fs_dirent_constructor(njs_vm_t *vm, njs_value_t *args,
     njs_uint_t nargs, njs_index_t unused, njs_value_t *retval);
@@ -195,18 +195,18 @@ static njs_int_t njs_fs_filehandle_close
 static njs_int_t njs_fs_filehandle_value_of(njs_vm_t *vm, njs_value_t *args,
     njs_uint_t nargs, njs_index_t unused, njs_value_t *retval);
 static njs_int_t njs_fs_filehandle_create(njs_vm_t *vm, int fd,
-    njs_bool_t shadow, njs_value_t *retval);
+    njs_bool_t shadow, njs_opaque_value_t *retval);
 
 static njs_int_t njs_fs_bytes_read_create(njs_vm_t *vm, int bytes,
-    njs_value_t *buffer, njs_value_t *retval);
+    njs_value_t *buffer, njs_opaque_value_t *retval);
 static njs_int_t njs_fs_bytes_written_create(njs_vm_t *vm, int bytes,
-    njs_value_t *buffer, njs_value_t *retval);
+    njs_value_t *buffer, njs_opaque_value_t *retval);
 
 static njs_int_t njs_fs_fd_read(njs_vm_t *vm, int fd, njs_str_t *data);
 
 static njs_int_t njs_fs_error(njs_vm_t *vm, const char *syscall,
-    const char *desc, const char *path, int errn, njs_value_t *result);
-static njs_int_t njs_fs_result(njs_vm_t *vm, njs_value_t *result,
+    const char *desc, const char *path, int errn, njs_opaque_value_t *result);
+static njs_int_t njs_fs_result(njs_vm_t *vm, njs_opaque_value_t *result,
     njs_index_t calltype, const njs_value_t* callback, njs_uint_t nargs,
     njs_value_t *retval);
 
@@ -214,19 +214,16 @@ static njs_int_t njs_file_tree_walk(cons
     njs_file_tree_walk_cb_t cb, int fd_limit, njs_ftw_flags_t flags);
 
 static njs_int_t njs_fs_make_path(njs_vm_t *vm, char *path, mode_t md,
-    njs_bool_t recursive, njs_value_t *retval);
+    njs_bool_t recursive, njs_opaque_value_t *retval);
 static njs_int_t njs_fs_rmtree(njs_vm_t *vm, const char *path,
-    njs_bool_t recursive, njs_value_t *retval);
+    njs_bool_t recursive, njs_opaque_value_t *retval);
 
 static const char *njs_fs_path(njs_vm_t *vm, char storage[NJS_MAX_PATH + 1],
-    const njs_value_t *src, const char *prop_name);
+    njs_value_t *src, const char *prop_name);
 static int njs_fs_flags(njs_vm_t *vm, njs_value_t *value, int default_flags);
 static mode_t njs_fs_mode(njs_vm_t *vm, njs_value_t *value,
     mode_t default_mode);
 
-static njs_int_t njs_fs_add_event(njs_vm_t *vm, const njs_value_t *callback,
-    const njs_value_t *args, njs_uint_t nargs);
-
 static njs_int_t njs_fs_dirent_create(njs_vm_t *vm, njs_value_t *name,
     njs_value_t *type, njs_value_t *retval);
 
@@ -234,11 +231,11 @@ static njs_int_t njs_fs_dirent_create(nj
 static njs_int_t njs_fs_init(njs_vm_t *vm);
 
 
-static const njs_value_t  string_flag = njs_string("flag");
-static const njs_value_t  string_mode = njs_string("mode");
-static const njs_value_t  string_buffer = njs_string("buffer");
-static const njs_value_t  string_encoding = njs_string("encoding");
-static const njs_value_t  string_recursive = njs_string("recursive");
+static const njs_str_t  string_flag = njs_str("flag");
+static const njs_str_t  string_mode = njs_str("mode");
+static const njs_str_t  string_buffer = njs_str("buffer");
+static const njs_str_t  string_encoding = njs_str("encoding");
+static const njs_str_t  string_recursive = njs_str("recursive");
 
 
 static njs_fs_entry_t njs_flags_table[] = {
@@ -259,6 +256,243 @@ static njs_fs_entry_t njs_flags_table[] 
 };
 
 
+static njs_external_t  njs_ext_fs_constants[] = {
+
+    {
+        .flags = NJS_EXTERN_PROPERTY,
+        .name.string = njs_str("F_OK"),
+        .enumerable = 1,
+        .u.property = {
+            .handler = njs_fs_constant,
+            .magic32 = F_OK,
+        }
+    },
+
+    {
+        .flags = NJS_EXTERN_PROPERTY,
+        .name.string = njs_str("R_OK"),
+        .enumerable = 1,
+        .u.property = {
+            .handler = njs_fs_constant,
+            .magic32 = R_OK,
+        }
+    },
+
+    {
+        .flags = NJS_EXTERN_PROPERTY,
+        .name.string = njs_str("W_OK"),
+        .enumerable = 1,
+        .u.property = {
+            .handler = njs_fs_constant,
+            .magic32 = W_OK,
+        }
+    },
+
+    {
+        .flags = NJS_EXTERN_PROPERTY,
+        .name.string = njs_str("X_OK"),
+        .enumerable = 1,
+        .u.property = {
+            .handler = njs_fs_constant,
+            .magic32 = X_OK,
+        }
+    },
+
+};
+
+
+static njs_external_t  njs_ext_fs_promises[] = {
+
+    {
+        .flags = NJS_EXTERN_METHOD,
+        .name.string = njs_str("access"),
+        .writable = 1,
+        .configurable = 1,
+        .u.method = {
+            .native = njs_fs_access,
+            .magic8 = NJS_FS_PROMISE,
+        }
+    },
+
+    {
+        .flags = NJS_EXTERN_METHOD,
+        .name.string = njs_str("appendFile"),
+        .writable = 1,
+        .configurable = 1,
+        .u.method = {
+            .native = njs_fs_write_file,
+            .magic8 = njs_fs_magic(NJS_FS_PROMISE, NJS_FS_APPEND),
+        }
+    },
+
+    {
+        .flags = NJS_EXTERN_METHOD,
+        .name.string = njs_str("close"),
+        .writable = 1,
+        .configurable = 1,
+        .u.method = {
+            .native = njs_fs_close,
+            .magic8 = NJS_FS_PROMISE,
+        }
+    },
+
+    {
+        .flags = NJS_EXTERN_METHOD,
+        .name.string = njs_str("fstat"),
+        .writable = 1,
+        .configurable = 1,
+        .u.method = {
+            .native = njs_fs_stat,
+            .magic8 = njs_fs_magic(NJS_FS_PROMISE, NJS_FS_FSTAT),
+        }
+    },
+
+    {
+        .flags = NJS_EXTERN_METHOD,
+        .name.string = njs_str("mkdir"),
+        .writable = 1,
+        .configurable = 1,
+        .u.method = {
+            .native = njs_fs_mkdir,
+            .magic8 = NJS_FS_PROMISE,
+        }
+    },
+
+    {
+        .flags = NJS_EXTERN_METHOD,
+        .name.string = njs_str("lstat"),
+        .writable = 1,
+        .configurable = 1,
+        .u.method = {
+            .native = njs_fs_stat,
+            .magic8 = njs_fs_magic(NJS_FS_PROMISE, NJS_FS_LSTAT),
+        }
+    },
+
+    {
+        .flags = NJS_EXTERN_METHOD,
+        .name.string = njs_str("open"),
+        .writable = 1,
+        .configurable = 1,
+        .u.method = {
+            .native = njs_fs_open,
+            .magic8 = NJS_FS_PROMISE,
+        }
+    },
+
+    {
+        .flags = NJS_EXTERN_METHOD,
+        .name.string = njs_str("readFile"),
+        .writable = 1,
+        .configurable = 1,
+        .u.method = {
+            .native = njs_fs_read_file,
+            .magic8 = NJS_FS_PROMISE,
+        }
+    },
+
+    {
+        .flags = NJS_EXTERN_METHOD,
+        .name.string = njs_str("readSync"),
+        .writable = 1,
+        .configurable = 1,
+        .u.method = {
+            .native = njs_fs_read,
+            .magic8 = NJS_FS_PROMISE,
+        }
+    },
+
+    {
+        .flags = NJS_EXTERN_METHOD,
+        .name.string = njs_str("readdir"),
+        .writable = 1,
+        .configurable = 1,
+        .u.method = {
+            .native = njs_fs_readdir,
+            .magic8 = NJS_FS_PROMISE,
+        }
+    },
+
+    {
+        .flags = NJS_EXTERN_METHOD,
+        .name.string = njs_str("realpath"),
+        .writable = 1,
+        .configurable = 1,
+        .u.method = {
+            .native = njs_fs_realpath,
+            .magic8 = NJS_FS_PROMISE,
+        }
+    },
+
+    {
+        .flags = NJS_EXTERN_METHOD,
+        .name.string = njs_str("rename"),
+        .writable = 1,
+        .configurable = 1,
+        .u.method = {
+            .native = njs_fs_rename,
+            .magic8 = NJS_FS_PROMISE,
+        }
+    },
+
+    {
+        .flags = NJS_EXTERN_METHOD,
+        .name.string = njs_str("rmdir"),
+        .writable = 1,
+        .configurable = 1,
+        .u.method = {
+            .native = njs_fs_rmdir,
+            .magic8 = NJS_FS_PROMISE,
+        }
+    },
+
+    {
+        .flags = NJS_EXTERN_METHOD,
+        .name.string = njs_str("stat"),
+        .writable = 1,
+        .configurable = 1,
+        .u.method = {
+            .native = njs_fs_stat,
+            .magic8 = njs_fs_magic(NJS_FS_PROMISE, NJS_FS_STAT),
+        }
+    },
+
+    {
+        .flags = NJS_EXTERN_METHOD,
+        .name.string = njs_str("symlink"),
+        .writable = 1,
+        .configurable = 1,
+        .u.method = {
+            .native = njs_fs_symlink,
+            .magic8 = NJS_FS_PROMISE,
+        }
+    },
+
+    {
+        .flags = NJS_EXTERN_METHOD,
+        .name.string = njs_str("unlink"),
+        .writable = 1,
+        .configurable = 1,
+        .u.method = {
+            .native = njs_fs_unlink,
+            .magic8 = NJS_FS_PROMISE,
+        }
+    },
+
+    {
+        .flags = NJS_EXTERN_METHOD,
+        .name.string = njs_str("writeFile"),
+        .writable = 1,
+        .configurable = 1,
+        .u.method = {
+            .native = njs_fs_write_file,
+            .magic8 = njs_fs_magic(NJS_FS_PROMISE, NJS_FS_TRUNC),
+        }
+    },
+
+};
+
+
 static njs_external_t  njs_ext_fs[] = {
 
     {
@@ -325,11 +559,14 @@ static njs_external_t  njs_ext_fs[] = {
     },
 
     {
-        .flags = NJS_EXTERN_PROPERTY,
+        .flags = NJS_EXTERN_OBJECT,
         .name.string = njs_str("constants"),
+        .writable = 1,
         .enumerable = 1,
-        .u.property = {
-            .handler = njs_fs_constants,
+        .configurable = 1,
+        .u.object = {
+            .properties = njs_ext_fs_constants,
+            .nproperties = njs_nitems(njs_ext_fs_constants),
         }
     },
 
@@ -411,11 +648,14 @@ static njs_external_t  njs_ext_fs[] = {
     },
 
     {
-        .flags = NJS_EXTERN_PROPERTY,
+        .flags = NJS_EXTERN_OBJECT,
         .name.string = njs_str("promises"),
+        .writable = 1,
         .enumerable = 1,
-        .u.property = {
-            .handler = njs_fs_promises,
+        .configurable = 1,
+        .u.object = {
+            .properties = njs_ext_fs_promises,
+            .nproperties = njs_nitems(njs_ext_fs_promises),
         }
     },
 
@@ -757,7 +997,7 @@ static njs_external_t  njs_ext_stats[] =
         .enumerable = 1,
         .u.property = {
             .handler = njs_fs_stats_prop,
-            .magic32 = njs_fs_magic2(NJS_FS_STAT_ATIME, NJS_DATE),
+            .magic32 = njs_fs_magic2(NJS_FS_STAT_ATIME, 1),
         }
     },
 
@@ -767,7 +1007,7 @@ static njs_external_t  njs_ext_stats[] =
         .enumerable = 1,
         .u.property = {
             .handler = njs_fs_stats_prop,
-            .magic32 = njs_fs_magic2(NJS_FS_STAT_ATIME, NJS_NUMBER),
+            .magic32 = njs_fs_magic2(NJS_FS_STAT_ATIME, 0),
         }
     },
 
@@ -777,7 +1017,7 @@ static njs_external_t  njs_ext_stats[] =
         .enumerable = 1,
         .u.property = {
             .handler = njs_fs_stats_prop,
-            .magic32 = njs_fs_magic2(NJS_FS_STAT_BIRTHTIME, NJS_DATE),
+            .magic32 = njs_fs_magic2(NJS_FS_STAT_BIRTHTIME, 1),
         }
     },
 
@@ -787,7 +1027,7 @@ static njs_external_t  njs_ext_stats[] =
         .enumerable = 1,
         .u.property = {
             .handler = njs_fs_stats_prop,
-            .magic32 = njs_fs_magic2(NJS_FS_STAT_BIRTHTIME, NJS_NUMBER),
+            .magic32 = njs_fs_magic2(NJS_FS_STAT_BIRTHTIME, 0),
         }
     },
 
@@ -797,7 +1037,7 @@ static njs_external_t  njs_ext_stats[] =
         .enumerable = 1,
         .u.property = {
             .handler = njs_fs_stats_prop,
-            .magic32 = njs_fs_magic2(NJS_FS_STAT_CTIME, NJS_DATE),
+            .magic32 = njs_fs_magic2(NJS_FS_STAT_CTIME, 1),
         }
     },
 
@@ -807,7 +1047,7 @@ static njs_external_t  njs_ext_stats[] =
         .enumerable = 1,
         .u.property = {
             .handler = njs_fs_stats_prop,
-            .magic32 = njs_fs_magic2(NJS_FS_STAT_CTIME, NJS_NUMBER),
+            .magic32 = njs_fs_magic2(NJS_FS_STAT_CTIME, 0),
         }
     },
 
@@ -817,7 +1057,7 @@ static njs_external_t  njs_ext_stats[] =
         .enumerable = 1,
         .u.property = {
             .handler = njs_fs_stats_prop,
-            .magic32 = njs_fs_magic2(NJS_FS_STAT_BLKSIZE, NJS_NUMBER),
+            .magic32 = njs_fs_magic2(NJS_FS_STAT_BLKSIZE, 0),
         }
     },
 
@@ -827,7 +1067,7 @@ static njs_external_t  njs_ext_stats[] =
         .enumerable = 1,
         .u.property = {
             .handler = njs_fs_stats_prop,
-            .magic32 = njs_fs_magic2(NJS_FS_STAT_BLOCKS, NJS_NUMBER),
+            .magic32 = njs_fs_magic2(NJS_FS_STAT_BLOCKS, 0),
         }
     },
 
@@ -837,7 +1077,7 @@ static njs_external_t  njs_ext_stats[] =
         .enumerable = 1,
         .u.property = {
             .handler = njs_fs_stats_prop,
-            .magic32 = njs_fs_magic2(NJS_FS_STAT_DEV, NJS_NUMBER),
+            .magic32 = njs_fs_magic2(NJS_FS_STAT_DEV, 0),
         }
     },
 
@@ -847,7 +1087,7 @@ static njs_external_t  njs_ext_stats[] =
         .enumerable = 1,
         .u.property = {
             .handler = njs_fs_stats_prop,
-            .magic32 = njs_fs_magic2(NJS_FS_STAT_GID, NJS_NUMBER),
+            .magic32 = njs_fs_magic2(NJS_FS_STAT_GID, 0),
         }
     },
 
@@ -857,7 +1097,7 @@ static njs_external_t  njs_ext_stats[] =
         .enumerable = 1,
         .u.property = {
             .handler = njs_fs_stats_prop,
-            .magic32 = njs_fs_magic2(NJS_FS_STAT_INO, NJS_NUMBER),
+            .magic32 = njs_fs_magic2(NJS_FS_STAT_INO, 0),
         }
     },
 
@@ -867,7 +1107,7 @@ static njs_external_t  njs_ext_stats[] =
         .enumerable = 1,
         .u.property = {
             .handler = njs_fs_stats_prop,
-            .magic32 = njs_fs_magic2(NJS_FS_STAT_MODE, NJS_NUMBER),
+            .magic32 = njs_fs_magic2(NJS_FS_STAT_MODE, 0),
         }
     },
 
@@ -877,7 +1117,7 @@ static njs_external_t  njs_ext_stats[] =
         .enumerable = 1,
         .u.property = {
             .handler = njs_fs_stats_prop,
-            .magic32 = njs_fs_magic2(NJS_FS_STAT_MTIME, NJS_DATE),
+            .magic32 = njs_fs_magic2(NJS_FS_STAT_MTIME, 1),
         }
     },
 
@@ -887,7 +1127,7 @@ static njs_external_t  njs_ext_stats[] =
         .enumerable = 1,
         .u.property = {
             .handler = njs_fs_stats_prop,
-            .magic32 = njs_fs_magic2(NJS_FS_STAT_MTIME, NJS_NUMBER),
+            .magic32 = njs_fs_magic2(NJS_FS_STAT_MTIME, 0),
         }
     },
 
@@ -897,7 +1137,7 @@ static njs_external_t  njs_ext_stats[] =
         .enumerable = 1,
         .u.property = {
             .handler = njs_fs_stats_prop,
-            .magic32 = njs_fs_magic2(NJS_FS_STAT_NLINK, NJS_NUMBER),
+            .magic32 = njs_fs_magic2(NJS_FS_STAT_NLINK, 0),
         }
     },
 
@@ -907,7 +1147,7 @@ static njs_external_t  njs_ext_stats[] =
         .enumerable = 1,
         .u.property = {
             .handler = njs_fs_stats_prop,
-            .magic32 = njs_fs_magic2(NJS_FS_STAT_RDEV, NJS_NUMBER),
+            .magic32 = njs_fs_magic2(NJS_FS_STAT_RDEV, 0),
         }
     },
 
@@ -917,7 +1157,7 @@ static njs_external_t  njs_ext_stats[] =
         .enumerable = 1,
         .u.property = {
             .handler = njs_fs_stats_prop,
-            .magic32 = njs_fs_magic2(NJS_FS_STAT_SIZE, NJS_NUMBER),
+            .magic32 = njs_fs_magic2(NJS_FS_STAT_SIZE, 0),
         }
     },
 
@@ -927,7 +1167,7 @@ static njs_external_t  njs_ext_stats[] =
         .enumerable = 1,
         .u.property = {
             .handler = njs_fs_stats_prop,
-            .magic32 = njs_fs_magic2(NJS_FS_STAT_UID, NJS_NUMBER),
+            .magic32 = njs_fs_magic2(NJS_FS_STAT_UID, 0),
         }
     },
 
@@ -1175,11 +1415,12 @@ static njs_int_t
 njs_fs_access(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
     njs_index_t calltype, njs_value_t *retval)
 {
-    int          md;
-    njs_int_t    ret;
-    const char  *path;
-    njs_value_t  result, *callback, *mode;
-    char         path_buf[NJS_MAX_PATH + 1];
+    int                 md;
+    njs_int_t           ret;
+    const char          *path;
+    njs_value_t         *callback, *mode;
+    njs_opaque_value_t  result;
+    char                path_buf[NJS_MAX_PATH + 1];
 
     path = njs_fs_path(vm, path_buf, njs_arg(args, nargs, 1), "path");
     if (njs_slow_path(path == NULL)) {
@@ -1191,8 +1432,8 @@ njs_fs_access(njs_vm_t *vm, njs_value_t 
 
     if (calltype == NJS_FS_CALLBACK) {
         callback = njs_arg(args, nargs, njs_min(nargs - 1, 3));
-        if (!njs_is_function(callback)) {
-            njs_type_error(vm, "\"callback\" must be a function");
+        if (!njs_value_is_function(callback)) {
+            njs_vm_error(vm, "\"callback\" must be a function");
             return NJS_ERROR;
         }
 
@@ -1201,21 +1442,18 @@ njs_fs_access(njs_vm_t *vm, njs_value_t 
         }
     }
 
-    switch (mode->type) {
-    case NJS_UNDEFINED:
+    if (njs_value_is_number(mode)) {
+        md = njs_value_number(mode);
+
+    } else if (njs_value_is_undefined(mode)) {
         md = F_OK;
-        break;
-
-    case NJS_NUMBER:
-        md = njs_number(mode);
-        break;
-
-    default:
-        njs_type_error(vm, "\"mode\" must be a number");
+
+    } else {
+        njs_vm_error(vm, "\"mode\" must be a number");
         return NJS_ERROR;
     }
 
-    njs_set_undefined(&result);
+    njs_value_undefined_set(njs_value_arg(&result));
 
     ret = access(path, md);
     if (njs_slow_path(ret != 0)) {
@@ -1234,12 +1472,13 @@ static njs_int_t
 njs_fs_open(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
     njs_index_t calltype, njs_value_t *retval)
 {
-    int          fd, flags;
-    mode_t       md;
-    njs_int_t    ret;
-    const char   *path;
-    njs_value_t  result, *value;
-    char         path_buf[NJS_MAX_PATH + 1];
+    int                 fd, flags;
+    mode_t              md;
+    njs_int_t           ret;
+    const char          *path;
+    njs_value_t         *value;
+    njs_opaque_value_t  result;
+    char                path_buf[NJS_MAX_PATH + 1];
 
     path = njs_fs_path(vm, path_buf, njs_arg(args, nargs, 1), "path");
     if (njs_slow_path(path == NULL)) {
@@ -1247,7 +1486,7 @@ njs_fs_open(njs_vm_t *vm, njs_value_t *a
     }
 
     value = njs_arg(args, nargs, 2);
-    if (njs_is_function(value)) {
+    if (njs_value_is_function(value)) {
         value = njs_value_arg(&njs_value_undefined);
     }
 
@@ -1257,7 +1496,7 @@ njs_fs_open(njs_vm_t *vm, njs_value_t *a
     }
 
     value = njs_arg(args, nargs, 3);
-    if (njs_is_function(value)) {
+    if (njs_value_is_function(value)) {
         value = njs_value_arg(&njs_value_undefined);
     }
 
@@ -1278,7 +1517,7 @@ njs_fs_open(njs_vm_t *vm, njs_value_t *a
     }
 
     if (calltype == NJS_FS_DIRECT) {
-        njs_value_number_set(&result, fd);
+        njs_value_number_set(njs_value_arg(&result), fd);
     }
 
 done:
@@ -1299,9 +1538,10 @@ static njs_int_t
 njs_fs_close(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
     njs_index_t calltype, njs_value_t *retval)
 {
-    int64_t      fd;
-    njs_int_t    ret;
-    njs_value_t  result, *fh;
+    int64_t             fd;
+    njs_int_t           ret;
+    njs_value_t         *fh;
+    njs_opaque_value_t  result;
 
     fh = njs_arg(args, nargs, 1);
 
@@ -1310,7 +1550,7 @@ njs_fs_close(njs_vm_t *vm, njs_value_t *
         return ret;
     }
 
-    njs_set_undefined(&result);
+    njs_value_undefined_set(njs_value_arg(&result));
 
     ret = close((int) fd);
     if (njs_slow_path(ret != 0)) {
@@ -1329,11 +1569,12 @@ static njs_int_t
 njs_fs_mkdir(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
     njs_index_t calltype, njs_value_t *retval)
 {
-    char         *path;
-    mode_t       md;
-    njs_int_t    ret;
-    njs_value_t  mode, recursive, result, *callback, *options;
-    char         path_buf[NJS_MAX_PATH + 1];
+    char                *path;
+    mode_t              md;
+    njs_int_t           ret;
+    njs_value_t         *callback, *options;
+    njs_opaque_value_t  mode, recursive, result;
+    char                path_buf[NJS_MAX_PATH + 1];
 
     path = (char *) njs_fs_path(vm, path_buf, njs_arg(args, nargs, 1), "path");
     if (njs_slow_path(path == NULL)) {
@@ -1345,53 +1586,41 @@ njs_fs_mkdir(njs_vm_t *vm, njs_value_t *
 
     if (njs_slow_path(calltype == NJS_FS_CALLBACK)) {
         callback = njs_arg(args, nargs, njs_min(nargs - 1, 3));
-        if (!njs_is_function(callback)) {
-            njs_type_error(vm, "\"callback\" must be a function");
+        if (!njs_value_is_function(callback)) {
+            njs_vm_error(vm, "\"callback\" must be a function");
             return NJS_ERROR;
         }
+
         if (options == callback) {
             options = njs_value_arg(&njs_value_undefined);
         }
     }
 
-    njs_set_undefined(&mode);
-    njs_set_false(&recursive);
-
-    switch (options->type) {
-    case NJS_NUMBER:
-        mode = *options;
-        break;
-
-    case NJS_UNDEFINED:
-        break;
-
-    default:
-        if (!njs_is_object(options)) {
-            njs_type_error(vm, "Unknown options type: \"%s\" "
-                           "(a number or object required)",
-                           njs_type_string(options->type));
+    njs_value_undefined_set(njs_value_arg(&mode));
+    njs_value_boolean_set(njs_value_arg(&recursive), 0);
+
+    if (njs_value_is_number(options)) {
+        njs_value_assign(&mode, options);
+
+    } else if (!njs_value_is_undefined(options)) {
+        if (!njs_value_is_object(options)) {
+            njs_vm_error(vm, "Unknown options type"
+                         "(a number or object required)");
             return NJS_ERROR;
         }
 
-        ret = njs_value_property(vm, options, njs_value_arg(&string_mode),
-                                 &mode);
-        if (njs_slow_path(ret == NJS_ERROR)) {
-            return ret;
-        }
-
-        ret = njs_value_property(vm, options, njs_value_arg(&string_recursive),
-                                 &recursive);
-        if (njs_slow_path(ret == NJS_ERROR)) {
-            return ret;
-        }
+        (void) njs_vm_object_prop(vm, options, &string_recursive, &recursive);
+
+        (void) njs_vm_object_prop(vm, options, &string_mode, &mode);
     }
 
-    md = njs_fs_mode(vm, &mode, 0777);
+    md = njs_fs_mode(vm, njs_value_arg(&mode), 0777);
     if (njs_slow_path(md == (mode_t) -1)) {
         return NJS_ERROR;
     }
 
-    ret = njs_fs_make_path(vm, path, md, njs_is_true(&recursive), &result);
+    ret = njs_fs_make_path(vm, path, md,
+                           njs_value_bool(njs_value_arg(&recursive)), &result);
 
     if (ret == NJS_OK) {
         return njs_fs_result(vm, &result, calltype, callback, 1, retval);
@@ -1405,12 +1634,13 @@ static njs_int_t
 njs_fs_read(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
     njs_index_t calltype, njs_value_t *retval)
 {
-    int64_t      fd, length, pos, offset;
-    ssize_t      n;
-    njs_int_t    ret;
-    njs_str_t    data;
-    njs_uint_t   fd_offset;
-    njs_value_t  result, *buffer, *value;
+    int64_t             fd, length, pos, offset;
+    ssize_t             n;
+    njs_int_t           ret;
+    njs_str_t           data;
+    njs_uint_t          fd_offset;
+    njs_value_t         *buffer, *value;
+    njs_opaque_value_t  result;
 
     fd_offset = !!(calltype == NJS_FS_DIRECT);
 
@@ -1439,8 +1669,7 @@ njs_fs_read(njs_vm_t *vm, njs_value_t *a
     }
 
     if (njs_slow_path(offset < 0 || (size_t) offset > data.length)) {
-        njs_range_error(vm, "offset is out of range (must be <= %z)",
-                        data.length);
+        njs_vm_error(vm, "offset is out of range (must be <= %z)", data.length);
         return NJS_ERROR;
     }
 
@@ -1449,15 +1678,15 @@ njs_fs_read(njs_vm_t *vm, njs_value_t *a
 
     value = njs_arg(args, nargs, fd_offset + 3);
 
-    if (njs_is_defined(value)) {
+    if (!njs_value_is_undefined(value)) {
         ret = njs_value_to_integer(vm, value, &length);
         if (njs_slow_path(ret != NJS_OK)) {
             return ret;
         }
 
         if (njs_slow_path(length < 0 || (size_t) length > data.length)) {
-            njs_range_error(vm, "length is out of range (must be <= %z)",
-                            data.length);
+            njs_vm_error(vm, "length is out of range (must be <= %z)",
+                         data.length);
             return NJS_ERROR;
         }
 
@@ -1466,7 +1695,7 @@ njs_fs_read(njs_vm_t *vm, njs_value_t *a
 
     value = njs_arg(args, nargs, fd_offset + 4);
 
-    if (!njs_is_null_or_undefined(value)) {
+    if (!njs_value_is_null_or_undefined(value)) {
         ret = njs_value_to_integer(vm, value, &pos);
         if (njs_slow_path(ret != NJS_OK)) {
             return ret;
@@ -1492,7 +1721,7 @@ njs_fs_read(njs_vm_t *vm, njs_value_t *a
         }
 
     } else {
-        njs_value_number_set(&result, n);
+        njs_value_number_set(njs_value_arg(&result), n);
     }
 
 done:
@@ -1513,8 +1742,9 @@ njs_fs_read_file(njs_vm_t *vm, njs_value
     njs_str_t                    data;
     njs_int_t                    ret;
     const char                   *path;
-    njs_value_t                  flag, encode, result, *callback, *options;
+    njs_value_t                  *callback, *options;
     struct stat                  sb;
+    njs_opaque_value_t           flag, result, encode;
     const njs_buffer_encoding_t  *encoding;
     char                         path_buf[NJS_MAX_PATH + 1];
 
@@ -1529,8 +1759,8 @@ njs_fs_read_file(njs_vm_t *vm, njs_value
 
     if (calltype == NJS_FS_CALLBACK) {
         callback = njs_arg(args, nargs, njs_min(nargs - 1, 3));
-        if (!njs_is_function(callback)) {
-            njs_type_error(vm, "\"callback\" must be a function");
+        if (!njs_value_is_function(callback)) {
+            njs_vm_error(vm, "\"callback\" must be a function");
             return NJS_ERROR;
         }
 
@@ -1539,46 +1769,32 @@ njs_fs_read_file(njs_vm_t *vm, njs_value
         }
     }
 
-    njs_set_undefined(&flag);
-    njs_set_undefined(&encode);
-
-    switch (options->type) {
-    case NJS_STRING:
-        encode = *options;
-        break;
-
-    case NJS_UNDEFINED:
-        break;
-
-    default:
-        if (!njs_is_object(options)) {
-            njs_type_error(vm, "Unknown options type: \"%s\" "
-                           "(a string or object required)",
-                           njs_type_string(options->type));
+    njs_value_undefined_set(njs_value_arg(&flag));
+    njs_value_undefined_set(njs_value_arg(&encode));
+
+    if (njs_value_is_string(options)) {
+        njs_value_assign(&encode, options);
+
+    } else if (!njs_value_is_undefined(options)) {
+        if (!njs_value_is_object(options)) {
+            njs_vm_error(vm, "Unknown options type "
+                         "(a string or object required)");
             return NJS_ERROR;
         }
 
-        ret = njs_value_property(vm, options, njs_value_arg(&string_flag),
-                                 &flag);
-        if (njs_slow_path(ret == NJS_ERROR)) {
-            return ret;
-        }
-
-        ret = njs_value_property(vm, options, njs_value_arg(&string_encoding),
-                                 &encode);
-        if (njs_slow_path(ret == NJS_ERROR)) {
-            return ret;
-        }
+        (void) njs_vm_object_prop(vm, options, &string_flag, &flag);
+
+        (void) njs_vm_object_prop(vm, options, &string_encoding, &encode);
     }
 
-    flags = njs_fs_flags(vm, &flag, O_RDONLY);
+    flags = njs_fs_flags(vm, njs_value_arg(&flag), O_RDONLY);
     if (njs_slow_path(flags == -1)) {
         return NJS_ERROR;
     }
 
     encoding = NULL;
-    if (njs_is_defined(&encode)) {
-        encoding = njs_buffer_encoding(vm, &encode, 1);
+    if (!njs_value_is_undefined(njs_value_arg(&encode))) {
+        encoding = njs_buffer_encoding(vm, njs_value_arg(&encode), 1);
         if (njs_slow_path(encoding == NULL)) {
             return NJS_ERROR;
         }
@@ -1615,11 +1831,12 @@ njs_fs_read_file(njs_vm_t *vm, njs_value
     }
 
     if (encoding == NULL) {
-        ret = njs_buffer_set(vm, &result, data.start, data.length);
+        ret = njs_buffer_set(vm, njs_value_arg(&result), data.start,
+                             data.length);
 
     } else {
-        ret = encoding->encode(vm, &result, &data);
-        njs_mp_free(vm->mem_pool, data.start);
+        ret = encoding->encode(vm, njs_value_arg(&result), &data);
+        njs_mp_free(njs_vm_memory_pool(vm), data.start);
     }
 
 done:
@@ -1644,14 +1861,13 @@ njs_fs_readdir(njs_vm_t *vm, njs_value_t
     njs_str_t                    s;
     njs_int_t                    ret;
     const char                   *path;
-    njs_value_t                  encode, types, ename, etype, result,
-                                 *callback, *options, *value;
-    njs_array_t                  *results;
+    njs_value_t                  *callback, *options, *value;
     struct dirent                *entry;
+    njs_opaque_value_t           encode, types, ename, etype, result;
     const njs_buffer_encoding_t  *encoding;
     char                         path_buf[NJS_MAX_PATH + 1];
 
-    static const njs_value_t  string_types = njs_string("withFileTypes");
+    static const njs_str_t  string_types = njs_str("withFileTypes");
 
     path = njs_fs_path(vm, path_buf, njs_arg(args, nargs, 1), "path");
     if (njs_slow_path(path == NULL)) {
@@ -1663,8 +1879,8 @@ njs_fs_readdir(njs_vm_t *vm, njs_value_t
 
     if (njs_slow_path(calltype == NJS_FS_CALLBACK)) {
         callback = njs_arg(args, nargs, njs_min(nargs - 1, 3));
-        if (!njs_is_function(callback)) {
-            njs_type_error(vm, "\"callback\" must be a function");
+        if (!njs_value_is_function(callback)) {
+            njs_vm_error(vm, "\"callback\" must be a function");
             return NJS_ERROR;
         }
         if (options == callback) {
@@ -1672,53 +1888,42 @@ njs_fs_readdir(njs_vm_t *vm, njs_value_t
         }
     }
 
-    njs_set_false(&types);
-    njs_set_undefined(&encode);
-
-    switch (options->type) {
-    case NJS_STRING:
-        encode = *options;
-        break;
-
-    case NJS_UNDEFINED:
-        break;
-
-    default:
-        if (!njs_is_object(options)) {
-            njs_type_error(vm, "Unknown options type: \"%s\" "
-                           "(a string or object required)",
-                           njs_type_string(options->type));
+    njs_value_boolean_set(njs_value_arg(&types), 0);
+    njs_value_undefined_set(njs_value_arg(&encode));
+
+    if (njs_value_is_string(options)) {
+        njs_value_assign(&encode, options);
+
+    } else if (!njs_value_is_undefined(options)) {
+        if (!njs_value_is_object(options)) {
+            njs_vm_error(vm, "Unknown options type "
+                         "(a string or object required)");
             return NJS_ERROR;
         }
 
-        ret = njs_value_property(vm, options, njs_value_arg(&string_encoding),
-                                 &encode);
-        if (njs_slow_path(ret == NJS_ERROR)) {


More information about the nginx-devel mailing list