Skip to content
This repository was archived by the owner on Jul 9, 2025. It is now read-only.

Commit ffd93ba

Browse files
committed
Bug 1033916 - Move JSAutoByteString out of jsapi.h into js/public/AutoByteString.h, incidentally breaking the jsfriendapi.h -> jsapi.h dependency. r=jandem
--HG-- extra : rebase_source : d85baf9b28e632db5669aa3d056cc9744686f5c8
1 parent 3e90595 commit ffd93ba

55 files changed

Lines changed: 196 additions & 83 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

dom/base/ChromeUtils.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "ChromeUtils.h"
88

9+
#include "js/AutoByteString.h"
910
#include "js/SavedFrameAPI.h"
1011
#include "jsfriendapi.h"
1112
#include "WrapperFactory.h"

dom/base/nsJSUtils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "GeckoProfiler.h"
2020
#include "jsapi.h"
2121
#include "jsfriendapi.h"
22+
#include "js/AutoByteString.h"
2223
#include "js/Conversions.h"
2324
#include "js/StableStringChars.h"
2425
#include "nsString.h"

dom/bindings/BindingUtils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#define mozilla_dom_BindingUtils_h__
99

1010
#include "jsfriendapi.h"
11+
#include "js/AutoByteString.h"
1112
#include "js/Wrapper.h"
1213
#include "js/Conversions.h"
1314
#include "mozilla/ArrayUtils.h"

dom/bindings/DOMJSClass.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#ifndef mozilla_dom_DOMJSClass_h
88
#define mozilla_dom_DOMJSClass_h
99

10+
#include "jsapi.h"
1011
#include "jsfriendapi.h"
1112
#include "js/Wrapper.h"
1213
#include "mozilla/Assertions.h"

ipc/testshell/XPCShellEnvironment.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "base/basictypes.h"
1717

1818
#include "jsapi.h"
19+
#include "js/AutoByteString.h"
1920

2021
#include "xpcpublic.h"
2122

js/public/AutoByteString.h

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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 */

js/src/builtin/TestingFunctions.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "jit/BaselineJIT.h"
4242
#include "jit/InlinableNatives.h"
4343
#include "jit/JitRealm.h"
44+
#include "js/AutoByteString.h"
4445
#include "js/Debug.h"
4546
#include "js/HashTable.h"
4647
#include "js/StableStringChars.h"

js/src/builtin/intl/Collator.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "builtin/intl/ScopedICUObject.h"
1818
#include "builtin/intl/SharedIntlData.h"
1919
#include "gc/FreeOp.h"
20+
#include "js/AutoByteString.h"
2021
#include "js/StableStringChars.h"
2122
#include "js/TypeDecls.h"
2223
#include "vm/GlobalObject.h"

js/src/builtin/intl/DateTimeFormat.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "builtin/intl/SharedIntlData.h"
2020
#include "builtin/intl/TimeZoneDataGenerated.h"
2121
#include "gc/FreeOp.h"
22+
#include "js/AutoByteString.h"
2223
#include "js/StableStringChars.h"
2324
#include "vm/DateTime.h"
2425
#include "vm/GlobalObject.h"

js/src/builtin/intl/IntlObject.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "builtin/intl/NumberFormat.h"
2222
#include "builtin/intl/PluralRules.h"
2323
#include "builtin/intl/ScopedICUObject.h"
24+
#include "js/AutoByteString.h"
2425
#include "js/Class.h"
2526
#include "js/StableStringChars.h"
2627
#include "vm/GlobalObject.h"

0 commit comments

Comments
 (0)