[njs] Improved String.prototype.toString() for ordinary strings.

Dmitry Volyntsev xeioex at nginx.com
Fri Jul 24 16:25:20 UTC 2020


details:   https://hg.nginx.org/njs/rev/354318f9b00f
branches:  
changeset: 1479:354318f9b00f
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Fri Jul 24 14:02:04 2020 +0000
description:
Improved String.prototype.toString() for ordinary strings.

Allowing converting all strings to encodings 'hex', 'base64', 'base64url'.

diffstat:

 src/njs_string.c         |  9 ++-------
 src/test/njs_unit_test.c |  3 ---
 2 files changed, 2 insertions(+), 10 deletions(-)

diffs (40 lines):

diff -r 78f0887e5aa6 -r 354318f9b00f src/njs_string.c
--- a/src/njs_string.c	Thu Jul 23 13:24:10 2020 +0300
+++ b/src/njs_string.c	Fri Jul 24 14:02:04 2020 +0000
@@ -738,9 +738,9 @@ njs_string_prototype_value_of(njs_vm_t *
 
 
 /*
- * String.toString([encoding]).
+ * String.prototype.toString([encoding]).
  * Returns the string as is if no additional argument is provided,
- * otherwise converts a byte string into an encoded string: hex, base64,
+ * otherwise converts a string into an encoded string: hex, base64,
  * base64url.
  */
 
@@ -771,11 +771,6 @@ njs_string_prototype_to_string(njs_vm_t 
 
     (void) njs_string_prop(&string, &value);
 
-    if (njs_slow_path(string.length != 0)) {
-        njs_type_error(vm, "argument must be a byte string");
-        return NJS_ERROR;
-    }
-
     njs_string_get(&args[1], &enc);
 
     str.length = string.size;
diff -r 78f0887e5aa6 -r 354318f9b00f src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c	Thu Jul 23 13:24:10 2020 +0300
+++ b/src/test/njs_unit_test.c	Fri Jul 24 14:02:04 2020 +0000
@@ -895,9 +895,6 @@ static njs_unit_test_t  njs_test[] =
     { njs_str("'A'.toString()"),
       njs_str("A") },
 
-    { njs_str("'A'.toString('hex')"),
-      njs_str("TypeError: argument must be a byte string") },
-
     { njs_str("'A'.toBytes().toString('latin1')"),
       njs_str("TypeError: Unknown encoding: \"latin1\"") },
 


More information about the nginx-devel mailing list