[njs] Introduced njs_fs_path_arg().

Dmitry Volyntsev xeioex at nginx.com
Fri Jan 17 12:19:46 UTC 2020


details:   https://hg.nginx.org/njs/rev/c735708203e8
branches:  
changeset: 1305:c735708203e8
user:      Artem S. Povalyukhin <artem.povaluhin at gmail.com>
date:      Fri Jan 17 10:04:28 2020 +0300
description:
Introduced njs_fs_path_arg().

diffstat:

 src/njs_fs.c                    |  123 ++++++++++++++++-----------------------
 src/test/njs_interactive_test.c |    2 +-
 src/test/njs_unit_test.c        |   54 +++++++++++++---
 3 files changed, 96 insertions(+), 83 deletions(-)

diffs (338 lines):

diff -r ef8c8f65ac0e -r c735708203e8 src/njs_fs.c
--- a/src/njs_fs.c	Thu Jan 16 21:29:43 2020 +0300
+++ b/src/njs_fs.c	Fri Jan 17 10:04:28 2020 +0300
@@ -69,6 +69,24 @@ static njs_fs_entry_t njs_flags_table[] 
 };
 
 
+njs_inline njs_int_t
+njs_fs_path_arg(njs_vm_t *vm, const char **dst,
+    const njs_value_t* src, const njs_str_t *prop_name)
+{
+    if (njs_slow_path(!njs_is_string(src))) {
+        njs_type_error(vm, "\"%V\" must be a string", prop_name);
+        return NJS_ERROR;
+    }
+
+    *dst = njs_string_to_c_string(vm, njs_value_arg(src));
+    if (njs_slow_path(*dst == NULL)) {
+        return NJS_ERROR;
+    }
+
+    return NJS_OK;
+}
+
+
 static njs_int_t
 njs_fs_read_file(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
     njs_index_t unused)
@@ -85,16 +103,17 @@ njs_fs_read_file(njs_vm_t *vm, njs_value
     njs_object_prop_t   *prop;
     njs_lvlhsh_query_t  lhq;
 
+    ret = njs_fs_path_arg(vm, &path, njs_arg(args, nargs, 1),
+                          &njs_str_value("path"));
+    if (njs_slow_path(ret != NJS_OK)) {
+        return ret;
+    }
+
     if (njs_slow_path(nargs < 3)) {
         njs_type_error(vm, "too few arguments");
         return NJS_ERROR;
     }
 
-    if (njs_slow_path(!njs_is_string(&args[1]))) {
-        njs_type_error(vm, "path must be a string");
-        return NJS_ERROR;
-    }
-
     flag.start = NULL;
     encoding.length = 0;
     encoding.start = NULL;
@@ -156,11 +175,6 @@ njs_fs_read_file(njs_vm_t *vm, njs_value
         return NJS_ERROR;
     }
 
-    path = njs_string_to_c_string(vm, &args[1]);
-    if (njs_slow_path(path == NULL)) {
-        return NJS_ERROR;
-    }
-
     if (encoding.length != 0
         && (encoding.length != 4 || memcmp(encoding.start, "utf8", 4) != 0))
     {
@@ -320,16 +334,17 @@ njs_fs_read_file_sync(njs_vm_t *vm, njs_
     njs_object_prop_t   *prop;
     njs_lvlhsh_query_t  lhq;
 
+    ret = njs_fs_path_arg(vm, &path, njs_arg(args, nargs, 1),
+                          &njs_str_value("path"));
+    if (njs_slow_path(ret != NJS_OK)) {
+        return ret;
+    }
+
     if (njs_slow_path(nargs < 2)) {
         njs_type_error(vm, "too few arguments");
         return NJS_ERROR;
     }
 
-    if (njs_slow_path(!njs_is_string(&args[1]))) {
-        njs_type_error(vm, "path must be a string");
-        return NJS_ERROR;
-    }
-
     flag.start = NULL;
     encoding.length = 0;
     encoding.start = NULL;
@@ -557,16 +572,17 @@ static njs_int_t njs_fs_write_file_inter
     njs_object_prop_t   *prop;
     njs_lvlhsh_query_t  lhq;
 
+    ret = njs_fs_path_arg(vm, &path, njs_arg(args, nargs, 1),
+                          &njs_str_value("path"));
+    if (njs_slow_path(ret != NJS_OK)) {
+        return ret;
+    }
+
     if (njs_slow_path(nargs < 4)) {
         njs_type_error(vm, "too few arguments");
         return NJS_ERROR;
     }
 
-    if (njs_slow_path(!njs_is_string(&args[1]))) {
-        njs_type_error(vm, "path must be a string");
-        return NJS_ERROR;
-    }
-
     if (njs_slow_path(!njs_is_string(&args[2]))) {
         njs_type_error(vm, "data must be a string");
         return NJS_ERROR;
@@ -654,11 +670,6 @@ static njs_int_t njs_fs_write_file_inter
         md = 0666;
     }
 
-    path = njs_string_to_c_string(vm, &args[1]);
-    if (njs_slow_path(path == NULL)) {
-        return NJS_ERROR;
-    }
-
     if (encoding.length != 0
         && (encoding.length != 4 || memcmp(encoding.start, "utf8", 4) != 0))
     {
@@ -745,16 +756,17 @@ njs_fs_write_file_sync_internal(njs_vm_t
     njs_object_prop_t   *prop;
     njs_lvlhsh_query_t  lhq;
 
+    ret = njs_fs_path_arg(vm, &path, njs_arg(args, nargs, 1),
+                          &njs_str_value("path"));
+    if (njs_slow_path(ret != NJS_OK)) {
+        return ret;
+    }
+
     if (njs_slow_path(nargs < 3)) {
         njs_type_error(vm, "too few arguments");
         return NJS_ERROR;
     }
 
-    if (njs_slow_path(!njs_is_string(&args[1]))) {
-        njs_type_error(vm, "path must be a string");
-        return NJS_ERROR;
-    }
-
     if (njs_slow_path(!njs_is_string(&args[2]))) {
         njs_type_error(vm, "data must be a string");
         return NJS_ERROR;
@@ -827,11 +839,6 @@ njs_fs_write_file_sync_internal(njs_vm_t
         md = 0666;
     }
 
-    path = njs_string_to_c_string(vm, &args[1]);
-    if (njs_slow_path(path == NULL)) {
-        return NJS_ERROR;
-    }
-
     if (encoding.length != 0
         && (encoding.length != 4 || memcmp(encoding.start, "utf8", 4) != 0))
     {
@@ -896,45 +903,19 @@ static njs_int_t
 njs_fs_rename_sync(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
     njs_index_t unused)
 {
-    int          ret;
-    const char   *old_path, *new_path;
-    njs_value_t  *old, *new;
+    njs_int_t   ret;
+    const char  *old_path, *new_path;
 
-    if (njs_slow_path(nargs < 3)) {
-        if (nargs < 2) {
-            njs_type_error(vm, "oldPath must be a string");
-            return NJS_ERROR;
-        }
-
-        njs_type_error(vm, "newPath must be a string");
-        return NJS_ERROR;
+    ret = njs_fs_path_arg(vm, &old_path, njs_arg(args, nargs, 1),
+                          &njs_str_value("oldPath"));
+    if (njs_slow_path(ret != NJS_OK)) {
+        return ret;
     }
 
-    old = njs_argument(args, 1);
-    new = njs_argument(args, 2);
-
-    if (njs_slow_path(!njs_is_string(old))) {
-        ret = njs_value_to_string(vm, old, old);
-        if (njs_slow_path(ret != NJS_OK)) {
-            return ret;
-        }
-    }
-
-    if (njs_slow_path(!njs_is_string(new))) {
-        ret = njs_value_to_string(vm, new, new);
-        if (njs_slow_path(ret != NJS_OK)) {
-            return ret;
-        }
-    }
-
-    old_path = njs_string_to_c_string(vm, old);
-    if (njs_slow_path(old_path == NULL)) {
-        return NJS_ERROR;
-    }
-
-    new_path = njs_string_to_c_string(vm, new);
-    if (njs_slow_path(new_path == NULL)) {
-        return NJS_ERROR;
+    ret = njs_fs_path_arg(vm, &new_path, njs_arg(args, nargs, 2),
+                          &njs_str_value("newPath"));
+    if (njs_slow_path(ret != NJS_OK)) {
+        return ret;
     }
 
     ret = rename(old_path, new_path);
diff -r ef8c8f65ac0e -r c735708203e8 src/test/njs_interactive_test.c
--- a/src/test/njs_interactive_test.c	Thu Jan 16 21:29:43 2020 +0300
+++ b/src/test/njs_interactive_test.c	Fri Jan 17 10:04:28 2020 +0300
@@ -233,7 +233,7 @@ static njs_interactive_test_t  njs_test[
                  "    at main (native)\n") },
 
     { njs_str("var fs = require('fs'); fs.readFile()" ENTER),
-      njs_str("TypeError: too few arguments\n"
+      njs_str("TypeError: \"path\" must be a string\n"
                  "    at fs.readFile (native)\n"
                  "    at main (native)\n") },
 
diff -r ef8c8f65ac0e -r c735708203e8 src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c	Thu Jan 16 21:29:43 2020 +0300
+++ b/src/test/njs_unit_test.c	Fri Jan 17 10:04:28 2020 +0300
@@ -15520,7 +15520,7 @@ static njs_unit_test_t  njs_test[] =
 
     { njs_str("var fs = require('fs');"
                  "fs.readFile()"),
-      njs_str("TypeError: too few arguments") },
+      njs_str("TypeError: \"path\" must be a string") },
 
     { njs_str("var fs = require('fs');"
                  "fs.readFile('/njs_unknown_path')"),
@@ -15550,11 +15550,11 @@ static njs_unit_test_t  njs_test[] =
 
     { njs_str("var fs = require('fs');"
                  "fs.readFileSync()"),
-      njs_str("TypeError: too few arguments") },
+      njs_str("TypeError: \"path\" must be a string") },
 
     { njs_str("var fs = require('fs');"
                  "fs.readFileSync({})"),
-      njs_str("TypeError: path must be a string") },
+      njs_str("TypeError: \"path\" must be a string") },
 
     { njs_str("var fs = require('fs');"
                  "fs.readFileSync('/njs_unknown_path', {flag:'xx'})"),
@@ -15577,7 +15577,7 @@ static njs_unit_test_t  njs_test[] =
 
     { njs_str("var fs = require('fs');"
                  "fs.writeFile()"),
-      njs_str("TypeError: too few arguments") },
+      njs_str("TypeError: \"path\" must be a string") },
 
     { njs_str("var fs = require('fs');"
                  "fs.writeFile('/njs_unknown_path')"),
@@ -15589,7 +15589,7 @@ static njs_unit_test_t  njs_test[] =
 
     { njs_str("var fs = require('fs');"
                  "fs.writeFile({}, '', function () {})"),
-      njs_str("TypeError: path must be a string") },
+      njs_str("TypeError: \"path\" must be a string") },
 
     { njs_str("var fs = require('fs');"
                  "fs.writeFile('/njs_unknown_path', '', 'utf8')"),
@@ -15615,7 +15615,7 @@ static njs_unit_test_t  njs_test[] =
 
     { njs_str("var fs = require('fs');"
                  "fs.writeFileSync()"),
-      njs_str("TypeError: too few arguments") },
+      njs_str("TypeError: \"path\" must be a string") },
 
     { njs_str("var fs = require('fs');"
                  "fs.writeFileSync('/njs_unknown_path')"),
@@ -15623,7 +15623,7 @@ static njs_unit_test_t  njs_test[] =
 
     { njs_str("var fs = require('fs');"
                  "fs.writeFileSync({}, '')"),
-      njs_str("TypeError: path must be a string") },
+      njs_str("TypeError: \"path\" must be a string") },
 
     { njs_str("var fs = require('fs');"
                  "fs.writeFileSync('/njs_unknown_path', '', {flag:'xx'})"),
@@ -15641,15 +15641,47 @@ static njs_unit_test_t  njs_test[] =
                  "fs.writeFileSync('/njs_unknown_path', '', true)"),
       njs_str("TypeError: Unknown options type (a string or object required)") },
 
-    /* require('fs').writeFileSync() */
+    /* require('fs').renameSync() */
 
     { njs_str("var fs = require('fs');"
               "fs.renameSync()"),
-      njs_str("TypeError: oldPath must be a string") },
+      njs_str("TypeError: \"oldPath\" must be a string") },
+
+    { njs_str("var fs = require('fs');"
+              "fs.renameSync('/njs_unknown_path')"),
+      njs_str("TypeError: \"newPath\" must be a string") },
+
+    { njs_str("var fs = require('fs');"
+              "[undefined, null, false, NaN, Symbol(), {}, Object('/njs_unknown_path')]"
+              ".map((x) => { try { fs.renameSync(x, '/njs_unknown_path'); } "
+              "              catch (e) { return (e instanceof TypeError); } })"
+              ".every((x) => x === true)"),
+      njs_str("true")},
 
     { njs_str("var fs = require('fs');"
-              "fs.renameSync({toString(){return '/path/1'}})"),
-      njs_str("TypeError: newPath must be a string") },
+              "[undefined, null, false, NaN, Symbol(), {}, Object('/njs_unknown_path')]"
+              ".map((x) => { try { fs.renameSync('/njs_unknown_path', x); } "
+              "              catch (e) { return (e instanceof TypeError); } })"
+              ".every((x) => x === true)"),
+      njs_str("true")},
+
+    { njs_str("var "
+              "fs = require('fs'),"
+              "func = ["
+                "'readFile',"
+                "'readFileSync',"
+                "'writeFile',"
+                "'writeFileSync',"
+                "'appendFile',"
+                "'appendFileSync',"
+              "],"
+              "test = (fname) =>"
+                "[undefined, null, false, NaN, Symbol(), {}, Object('/njs_unknown_path')]"
+                ".map((x) => { try { fs[fname](x); } "
+                "              catch (e) { return (e instanceof TypeError); } })"
+                ".every((x) => x === true);"
+              "func.map(test).every((x) => x)"),
+      njs_str("true")},
 
     /* require('crypto').createHash() */
 


More information about the nginx-devel mailing list