|
| 1 | +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 5 | + |
| 6 | +/* |
| 7 | + * DEPRECATED functions and classes for heap-allocating copies of a JSString's |
| 8 | + * data. |
| 9 | + */ |
| 10 | + |
| 11 | +#ifndef js_AutoByteString_h |
| 12 | +#define js_AutoByteString_h |
| 13 | + |
| 14 | +#include "mozilla/Assertions.h" // MOZ_ASSERT |
| 15 | +#include "mozilla/Attributes.h" // MOZ_RAII, MOZ_GUARD* |
| 16 | + |
| 17 | +#include <string.h> // strlen |
| 18 | + |
| 19 | +#include "jstypes.h" // JS_PUBLIC_API |
| 20 | + |
| 21 | +#include "js/MemoryFunctions.h" // JS_free |
| 22 | +#include "js/RootingAPI.h" // JS::Handle |
| 23 | +#include "js/TypeDecls.h" // JSContext, JSString |
| 24 | +#include "js/Utility.h" // js_free, JS::UniqueChars |
| 25 | + |
| 26 | +/** |
| 27 | + * DEPRECATED |
| 28 | + * |
| 29 | + * Allocate memory sufficient to contain the characters of |str| truncated to |
| 30 | + * Latin-1 and a trailing null terminator, fill the memory with the characters |
| 31 | + * interpreted in that manner plus the null terminator, and return a pointer to |
| 32 | + * the memory. The memory must be freed using JS_free to avoid leaking. |
| 33 | + * |
| 34 | + * This function *loses information* when it copies the characters of |str| if |
| 35 | + * |str| contains code units greater than 0xFF. Additionally, users that |
| 36 | + * depend on null-termination will misinterpret the copied characters if |str| |
| 37 | + * contains any nulls. Avoid using this function if possible, because it will |
| 38 | + * eventually be removed. |
| 39 | + */ |
| 40 | +extern JS_PUBLIC_API(char*) |
| 41 | +JS_EncodeString(JSContext* cx, JSString* str); |
| 42 | + |
| 43 | +/** |
| 44 | + * DEPRECATED |
| 45 | + * |
| 46 | + * Same behavior as JS_EncodeString(), but encode into a UTF-8 string. |
| 47 | + * |
| 48 | + * This function *loses information* when it copies the characters of |str| if |
| 49 | + * |str| contains invalid UTF-16: U+FFFD REPLACEMENT CHARACTER will be copied |
| 50 | + * instead. |
| 51 | + * |
| 52 | + * The returned string is also subject to misinterpretation if |str| contains |
| 53 | + * any nulls (which are faithfully transcribed into the returned string, but |
| 54 | + * which will implicitly truncate the string if it's passed to functions that |
| 55 | + * expect null-terminated strings). |
| 56 | + * |
| 57 | + * Avoid using this function if possible, because we'll remove it once we can |
| 58 | + * devise a better API for the task. |
| 59 | + */ |
| 60 | +extern JS_PUBLIC_API(char*) |
| 61 | +JS_EncodeStringToUTF8(JSContext* cx, JS::Handle<JSString*> str); |
| 62 | + |
| 63 | +/** |
| 64 | + * DEPRECATED |
| 65 | + * |
| 66 | + * A lightweight RAII helper class around the various JS_Encode* functions |
| 67 | + * above, subject to the same pitfalls noted above. Avoid using this class if |
| 68 | + * possible, because as with the functions above, it too needs to be replaced |
| 69 | + * with a better, safer API. |
| 70 | + */ |
| 71 | +class MOZ_RAII JSAutoByteString final |
| 72 | +{ |
| 73 | + private: |
| 74 | + char* mBytes; |
| 75 | + MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER |
| 76 | + |
| 77 | + private: |
| 78 | + JSAutoByteString(const JSAutoByteString& another) = delete; |
| 79 | + void operator=(const JSAutoByteString& another) = delete; |
| 80 | + |
| 81 | + public: |
| 82 | + JSAutoByteString(JSContext* cx, JSString* str |
| 83 | + MOZ_GUARD_OBJECT_NOTIFIER_PARAM) |
| 84 | + : mBytes(JS_EncodeString(cx, str)) |
| 85 | + { |
| 86 | + MOZ_ASSERT(cx); |
| 87 | + MOZ_GUARD_OBJECT_NOTIFIER_INIT; |
| 88 | + } |
| 89 | + |
| 90 | + explicit JSAutoByteString(MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM) |
| 91 | + : mBytes(nullptr) |
| 92 | + { |
| 93 | + MOZ_GUARD_OBJECT_NOTIFIER_INIT; |
| 94 | + } |
| 95 | + |
| 96 | + ~JSAutoByteString() { |
| 97 | + JS_free(nullptr, mBytes); |
| 98 | + } |
| 99 | + |
| 100 | + /* Take ownership of the given byte array. */ |
| 101 | + void initBytes(JS::UniqueChars&& bytes) { |
| 102 | + MOZ_ASSERT(!mBytes); |
| 103 | + mBytes = bytes.release(); |
| 104 | + } |
| 105 | + |
| 106 | + char* encodeLatin1(JSContext* cx, JSString* str) { |
| 107 | + MOZ_ASSERT(!mBytes); |
| 108 | + MOZ_ASSERT(cx); |
| 109 | + mBytes = JS_EncodeString(cx, str); |
| 110 | + return mBytes; |
| 111 | + } |
| 112 | + |
| 113 | + char* encodeUtf8(JSContext* cx, JS::Handle<JSString*> str) { |
| 114 | + MOZ_ASSERT(!mBytes); |
| 115 | + MOZ_ASSERT(cx); |
| 116 | + mBytes = JS_EncodeStringToUTF8(cx, str); |
| 117 | + return mBytes; |
| 118 | + } |
| 119 | + |
| 120 | + void clear() { |
| 121 | + js_free(mBytes); |
| 122 | + mBytes = nullptr; |
| 123 | + } |
| 124 | + |
| 125 | + char* ptr() const { |
| 126 | + return mBytes; |
| 127 | + } |
| 128 | + |
| 129 | + bool operator!() const { |
| 130 | + return !mBytes; |
| 131 | + } |
| 132 | + |
| 133 | + size_t length() const { |
| 134 | + if (!mBytes) |
| 135 | + return 0; |
| 136 | + return strlen(mBytes); |
| 137 | + } |
| 138 | +}; |
| 139 | + |
| 140 | +#endif /* js_AutoByteString_h */ |
0 commit comments