1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 : *
3 : * ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
6 : * The contents of this file are subject to the Mozilla Public License Version
7 : * 1.1 (the "License"); you may not use this file except in compliance with
8 : * the License. You may obtain a copy of the License at
9 : * http://www.mozilla.org/MPL/
10 : *
11 : * Software distributed under the License is distributed on an "AS IS" basis,
12 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 : * for the specific language governing rights and limitations under the
14 : * License.
15 : *
16 : * The Original Code is Mozilla Communicator client code, released
17 : * March 31, 1998.
18 : *
19 : * The Initial Developer of the Original Code is
20 : * Netscape Communications Corporation.
21 : * Portions created by the Initial Developer are Copyright (C) 2001
22 : * the Initial Developer. All Rights Reserved.
23 : *
24 : * Contributor(s):
25 : * Mike Shaver <shaver@mozilla.org>
26 : *
27 : * Alternatively, the contents of this file may be used under the terms of
28 : * either of the GNU General Public License Version 2 or later (the "GPL"),
29 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 : * in which case the provisions of the GPL or the LGPL are applicable instead
31 : * of those above. If you wish to allow use of your version of this file only
32 : * under the terms of either the GPL or the LGPL, and not to allow others to
33 : * use your version of this file under the terms of the MPL, indicate your
34 : * decision by deleting the provisions above and replace them with the notice
35 : * and other provisions required by the GPL or the LGPL. If you do not delete
36 : * the provisions above, a recipient may use your version of this file under
37 : * the terms of any one of the MPL, the GPL or the LGPL.
38 : *
39 : * ***** END LICENSE BLOCK ***** */
40 :
41 : /*
42 : * Infrastructure for sharing DOMString data with JSStrings.
43 : *
44 : * Importing an nsAString into JS:
45 : * If possible (GetSharedBufferHandle works) use the external string support in
46 : * JS to create a JSString that points to the readable's buffer. We keep a
47 : * reference to the buffer handle until the JSString is finalized.
48 : *
49 : * Exporting a JSString as an nsAReadable:
50 : * Wrap the JSString with a root-holding XPCJSReadableStringWrapper, which roots
51 : * the string and exposes its buffer via the nsAString interface, as
52 : * well as providing refcounting support.
53 : */
54 :
55 : #include "xpcprivate.h"
56 : #include "nsStringBuffer.h"
57 :
58 : static void
59 103647 : FinalizeDOMString(const JSStringFinalizer *fin, jschar *chars)
60 : {
61 103647 : nsStringBuffer::FromData(chars)->Release();
62 103647 : }
63 :
64 : static const JSStringFinalizer sDOMStringFinalizer = { FinalizeDOMString };
65 :
66 :
67 : // convert a readable to a JSString, copying string data
68 : // static
69 : jsval
70 1372151 : XPCStringConvert::ReadableToJSVal(JSContext *cx,
71 : const nsAString &readable,
72 : nsStringBuffer** sharedBuffer)
73 : {
74 : JSString *str;
75 1372151 : *sharedBuffer = nsnull;
76 :
77 1372151 : PRUint32 length = readable.Length();
78 :
79 1372151 : if (length == 0)
80 8873 : return JS_GetEmptyStringValue(cx);
81 :
82 1363278 : nsStringBuffer *buf = nsStringBuffer::FromString(readable);
83 1363278 : if (buf) {
84 : // yay, we can share the string's buffer!
85 :
86 : str = JS_NewExternalString(cx,
87 103647 : reinterpret_cast<jschar *>(buf->Data()),
88 103647 : length, &sDOMStringFinalizer);
89 :
90 103647 : if (str) {
91 103647 : *sharedBuffer = buf;
92 : }
93 : } else {
94 : // blech, have to copy.
95 :
96 : jschar *chars = reinterpret_cast<jschar *>
97 : (JS_malloc(cx, (length + 1) *
98 1259631 : sizeof(jschar)));
99 1259631 : if (!chars)
100 0 : return JSVAL_NULL;
101 :
102 2519262 : if (length && !CopyUnicodeTo(readable, 0,
103 : reinterpret_cast<PRUnichar *>(chars),
104 1259631 : length)) {
105 0 : JS_free(cx, chars);
106 0 : return JSVAL_NULL;
107 : }
108 :
109 1259631 : chars[length] = 0;
110 :
111 1259631 : str = JS_NewUCString(cx, chars, length);
112 1259631 : if (!str)
113 0 : JS_free(cx, chars);
114 : }
115 1363278 : return str ? STRING_TO_JSVAL(str) : JSVAL_NULL;
116 : }
|