[gjs/ewlsh/text-encoding] temp - parse_call_args stuff




commit a0e997aa6dac8e213bc5621fbf94849e31cea0b7
Author: Philip Chimento <philip chimento gmail com>
Date:   Fri Jul 2 14:16:31 2021 -0700

    temp - parse_call_args stuff

 gjs/byteArray.cpp     | 30 ++++--------------------------
 gjs/text-encoding.cpp | 33 ++++++++-------------------------
 2 files changed, 12 insertions(+), 51 deletions(-)
---
diff --git a/gjs/byteArray.cpp b/gjs/byteArray.cpp
index acc6cb64..b210e77b 100644
--- a/gjs/byteArray.cpp
+++ b/gjs/byteArray.cpp
@@ -12,7 +12,6 @@
 
 #include <js/ArrayBuffer.h>
 #include <js/CallArgs.h>
-#include <js/CharacterEncoding.h>
 #include <js/PropertySpec.h>
 #include <js/RootingAPI.h>
 #include <js/TypeDecls.h>
@@ -96,34 +95,13 @@ static bool define_legacy_tostring(JSContext* cx, JS::HandleObject array) {
 GJS_JSAPI_RETURN_CONVENTION
 static bool from_string_func(JSContext* cx, unsigned argc, JS::Value* vp) {
     JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
-
-    if (!args.requireAtLeast(cx, "fromString", 1))
-        return false;
-
-    if (!args[0].isString()) {
-        gjs_throw(cx, "First argument for fromString() should be a string.");
-        return false;
-    }
-
-    // Only check the argument type if there is more than one argument.
-    if (args.length() > 1 && !args[1].isString() && !args[1].isUndefined()) {
-        gjs_throw(cx,
-                  "Second argument for fromString() should be a string or "
-                  "undefined.");
+    JS::RootedString str(cx);
+    JS::UniqueChars encoding;
+    if (!gjs_parse_call_args(cx, "fromString", args, "S|s", "string", &str,
+                             "encoding", &encoding))
         return false;
-    }
-
-    GjsAutoChar encoding = nullptr;
-    // If more than one argument is passed and it is a string,
-    // convert it to an encoding label.
-    if (args.length() > 1 && args[1].isString()) {
-        JS::RootedString encoding_str(cx, args[1].toString());
-        JS::UniqueChars chars(JS_EncodeStringToUTF8(cx, encoding_str));
-        encoding = g_strdup(chars.get());
-    }
 
     const char* actual_encoding = encoding ? encoding.get() : "utf-8";
-    JS::RootedString str(cx, args[0].toString());
     JS::RootedObject uint8array(
         cx, gjs_encode_to_uint8array(cx, str, actual_encoding,
                                      GjsStringTermination::ZERO_TERMINATED));
diff --git a/gjs/text-encoding.cpp b/gjs/text-encoding.cpp
index 13f2ae4d..11bbd53d 100644
--- a/gjs/text-encoding.cpp
+++ b/gjs/text-encoding.cpp
@@ -494,17 +494,10 @@ static bool gjs_encode_into_uint8array(JSContext* cx, JS::HandleString str,
 GJS_JSAPI_RETURN_CONVENTION
 static bool gjs_encode(JSContext* cx, unsigned argc, JS::Value* vp) {
     JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
-    if (!args.requireAtLeast(cx, "encode", 2))
-        return false;
-
-    if (!args[0].isString()) {
-        gjs_throw(cx, "First argument for encode() should be a string.");
-        return false;
-    }
-
-    JS::RootedString str(cx, args[0].toString());
-    JS::UniqueChars encoding = gjs_string_to_utf8(cx, args[1]);
-    if (!encoding)
+    JS::RootedString str(cx);
+    JS::UniqueChars encoding;
+    if (!gjs_parse_call_args(cx, "encode", args, "Ss", "string", &str,
+                             "encoding", &encoding))
         return false;
 
     JS::RootedObject uint8array(
@@ -520,22 +513,12 @@ static bool gjs_encode(JSContext* cx, unsigned argc, JS::Value* vp) {
 GJS_JSAPI_RETURN_CONVENTION
 static bool gjs_encode_into(JSContext* cx, unsigned argc, JS::Value* vp) {
     JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
-    if (!args.requireAtLeast(cx, "encodeInto", 2))
+    JS::RootedString str(cx);
+    JS::RootedObject uint8array(cx);
+    if (!gjs_parse_call_args(cx, "encodeInto", args, "So", "string", &str,
+                             "byteArray", &uint8array))
         return false;
 
-    if (!args[0].isString()) {
-        gjs_throw(cx, "First argument for encodeInto() should be a string.");
-        return false;
-    }
-
-    if (!args[1].isObject()) {
-        gjs_throw(cx, "Second argument for encodeInto() should be an object.");
-        return false;
-    }
-
-    JS::RootedString str(cx, args[0].toString());
-    JS::RootedObject uint8array(cx, &args[1].toObject());
-
     return gjs_encode_into_uint8array(cx, str, uint8array, args.rval());
 }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]