You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by da...@apache.org on 2020/04/16 20:20:12 UTC
[couchdb] 01/07: Fix UTF-8 transcoding functions
This is an automated email from the ASF dual-hosted git repository.
davisp pushed a commit to branch fix-couchjs-utf8-conversions-take2
in repository https://gitbox.apache.org/repos/asf/couchdb.git
commit e85c5d5d1c4c847afcde5baa8ba5f4983ab5c134
Author: Paul J. Davis <pa...@gmail.com>
AuthorDate: Tue Apr 14 13:22:28 2020 -0500
Fix UTF-8 transcoding functions
This fixes our string transcoding between UTF-8 and UTF-16. Once
everything is updated to use these new adapters we can remove the
bespoke UTF-8 transcoding implementation in `utf8.{h,c}`.
---
src/couch/priv/couch_js/60/util.cpp | 48 ++++++++++++++++++++++++++++++-------
src/couch/priv/couch_js/60/util.h | 1 +
2 files changed, 41 insertions(+), 8 deletions(-)
diff --git a/src/couch/priv/couch_js/60/util.cpp b/src/couch/priv/couch_js/60/util.cpp
index ad68f26..3945114 100644
--- a/src/couch/priv/couch_js/60/util.cpp
+++ b/src/couch/priv/couch_js/60/util.cpp
@@ -15,7 +15,9 @@
#include <jsapi.h>
#include <js/Initialization.h>
+#include <js/CharacterEncoding.h>
#include <js/Conversions.h>
+#include <mozilla/Unused.h>
#include "help.h"
#include "util.h"
@@ -30,23 +32,53 @@ js_to_string(JSContext* cx, JS::HandleValue val)
JS::UniqueChars chars(JS_EncodeStringToUTF8(cx, sval));
if(!chars) {
JS_ClearPendingException(cx);
- fprintf(stderr, "Error converting value to string.\n");
- exit(3);
+ return std::string();
}
return chars.get();
}
+bool
+js_to_string(JSContext* cx, JS::HandleValue val, std::string& str)
+{
+ if(!val.isString()) {
+ return false;
+ }
+
+ if(JS_GetStringLength(val.toString()) == 0) {
+ str = "";
+ return true;
+ }
+
+ std::string conv = js_to_string(cx, val);
+ if(!conv.size()) {
+ return false;
+ }
+
+ str = conv;
+ return true;
+}
+
JSString*
-string_to_js(JSContext* cx, const std::string& s)
+string_to_js(JSContext* cx, const std::string& raw)
{
- JSString* ret = JS_NewStringCopyN(cx, s.c_str(), s.size());
- if(ret != nullptr) {
- return ret;
+ JS::UTF8Chars utf8(raw.c_str(), raw.size());
+ JS::UniqueTwoByteChars utf16;
+ size_t len;
+
+ utf16.reset(JS::UTF8CharsToNewTwoByteCharsZ(cx, utf8, &len).get());
+ if(!utf16) {
+ return nullptr;
+ }
+
+ JSString* ret = JS_NewUCString(cx, utf16.get(), len);
+
+ if(ret) {
+ // JS_NewUCString took ownership on succses
+ mozilla::Unused << utf16.release();
}
- fprintf(stderr, "Unable to allocate string object.\n");
- exit(3);
+ return ret;
}
size_t
diff --git a/src/couch/priv/couch_js/60/util.h b/src/couch/priv/couch_js/60/util.h
index 0c9f0f8..07efc47 100644
--- a/src/couch/priv/couch_js/60/util.h
+++ b/src/couch/priv/couch_js/60/util.h
@@ -26,6 +26,7 @@ typedef struct {
} couch_args;
std::string js_to_string(JSContext* cx, JS::HandleValue val);
+bool js_to_string(JSContext* cx, JS::HandleValue val, std::string& str);
JSString* string_to_js(JSContext* cx, const std::string& s);
couch_args* couch_parse_args(int argc, const char* argv[]);