source: webkit/trunk/JavaScriptCore/API/JSInternalStringRef.cpp@ 15328

Last change on this file since 15328 was 15328, checked in by ggaren, 19 years ago

Reviewed by Darin.


  • Changed public header includes to the <JavaScriptCore/ style.
  • Changed instances of 'buffer' to 'string' since we decided on JSInternalString instead of JSStringBuffer.
  • API/JSContextRef.h:
  • API/JSInternalStringRef.cpp: (JSStringMake): (JSInternalStringRetain): (JSInternalStringRelease): (JSValueCopyStringValue): (JSInternalStringGetLength): (JSInternalStringGetCharactersPtr): (JSInternalStringGetCharacters): (JSInternalStringGetMaxLengthUTF8): (JSInternalStringGetCharactersUTF8): (CFStringCreateWithJSInternalString):
  • API/JSInternalStringRef.h:
  • API/JSNode.c: (JSNodePrototype_appendChild): (JSNode_getNodeType):
  • API/JSObjectRef.cpp: (JSObjectCallAsConstructor):
  • API/JSValueRef.h:
  • API/JavaScriptCore.h:
  • API/minidom.c: (main): (print):
  • API/testapi.c: (MyObject_getPropertyList): (myConstructor_callAsConstructor): (main): I noticed that we were prematurely releasing some string buffers, so I moved their release calls to the end of main(). I got rid of 'Buf' in *Buf (sometimes changing to 'IString', when necessary to differentiate a variable) to match the buffer->string change.
File size: 5.3 KB
Line 
1// -*- mode: c++; c-basic-offset: 4 -*-
2/*
3 * Copyright (C) 2006 Apple Computer, Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "APICast.h"
28#include "JSInternalStringRef.h"
29
30#include <kjs/JSLock.h>
31#include <kjs/JSType.h>
32#include <kjs/internal.h>
33#include <kjs/operations.h>
34#include <kjs/ustring.h>
35#include <kjs/value.h>
36
37using namespace KJS;
38
39JSValueRef JSStringMake(JSInternalStringRef string)
40{
41 JSLock lock;
42 UString::Rep* rep = toJS(string);
43 return toRef(jsString(UString(rep)));
44}
45
46JSInternalStringRef JSInternalStringCreate(const JSChar* chars, size_t numChars)
47{
48 JSLock lock;
49 return toRef(UString(reinterpret_cast<const UChar*>(chars), numChars).rep()->ref());
50}
51
52JSInternalStringRef JSInternalStringCreateUTF8(const char* string)
53{
54 JSLock lock;
55 // FIXME: Only works with ASCII
56 // Use decodeUTF8Sequence or http://www.unicode.org/Public/PROGRAMS/CVTUTF/ instead
57 return toRef(UString(string).rep()->ref());
58}
59
60JSInternalStringRef JSInternalStringRetain(JSInternalStringRef string)
61{
62 UString::Rep* rep = toJS(string);
63 return toRef(rep->ref());
64}
65
66void JSInternalStringRelease(JSInternalStringRef string)
67{
68 JSLock lock;
69 UString::Rep* rep = toJS(string);
70 rep->deref();
71}
72
73JSInternalStringRef JSValueCopyStringValue(JSContextRef context, JSValueRef value)
74{
75 JSLock lock;
76 JSValue* jsValue = toJS(value);
77 ExecState* exec = toJS(context);
78
79 JSInternalStringRef stringRef = toRef(jsValue->toString(exec).rep()->ref());
80 if (exec->hadException())
81 exec->clearException();
82 return stringRef;
83}
84
85size_t JSInternalStringGetLength(JSInternalStringRef string)
86{
87 UString::Rep* rep = toJS(string);
88 return rep->size();
89}
90
91const JSChar* JSInternalStringGetCharactersPtr(JSInternalStringRef string)
92{
93 UString::Rep* rep = toJS(string);
94 return reinterpret_cast<const JSChar*>(rep->data());
95}
96
97void JSInternalStringGetCharacters(JSInternalStringRef string, JSChar* buffer, size_t numChars)
98{
99 UString::Rep* rep = toJS(string);
100 const JSChar* data = reinterpret_cast<const JSChar*>(rep->data());
101
102 memcpy(buffer, data, numChars * sizeof(JSChar));
103}
104
105size_t JSInternalStringGetMaxLengthUTF8(JSInternalStringRef string)
106{
107 UString::Rep* rep = toJS(string);
108
109 // Any UTF8 character > 3 bytes encodes as a UTF16 surrogate pair.
110 return rep->size() * 3 + 1; // + 1 for terminating '\0'
111}
112
113size_t JSInternalStringGetCharactersUTF8(JSInternalStringRef string, char* buffer, size_t bufferSize)
114{
115 JSLock lock;
116 UString::Rep* rep = toJS(string);
117 CString cString = UString(rep).UTF8String();
118
119 size_t length = std::min(bufferSize, cString.size() + 1); // + 1 for terminating '\0'
120 memcpy(buffer, cString.c_str(), length);
121 return length;
122}
123
124bool JSInternalStringIsEqual(JSInternalStringRef a, JSInternalStringRef b)
125{
126 UString::Rep* aRep = toJS(a);
127 UString::Rep* bRep = toJS(b);
128
129 return UString(aRep) == UString(bRep);
130}
131
132bool JSInternalStringIsEqualUTF8(JSInternalStringRef a, const char* b)
133{
134 JSInternalStringRef bBuf = JSInternalStringCreateUTF8(b);
135 bool result = JSInternalStringIsEqual(a, bBuf);
136 JSInternalStringRelease(bBuf);
137
138 return result;
139}
140
141#if defined(__APPLE__)
142JSInternalStringRef JSInternalStringCreateCF(CFStringRef string)
143{
144 JSLock lock;
145 CFIndex length = CFStringGetLength(string);
146
147 // Optimized path for when CFString backing store is a UTF16 buffer
148 if (const UniChar* buffer = CFStringGetCharactersPtr(string)) {
149 UString::Rep* rep = UString(reinterpret_cast<const UChar*>(buffer), length).rep()->ref();
150 return toRef(rep);
151 }
152
153 UniChar* buffer = static_cast<UniChar*>(fastMalloc(sizeof(UniChar) * length));
154 CFStringGetCharacters(string, CFRangeMake(0, length), buffer);
155 UString::Rep* rep = UString(reinterpret_cast<UChar*>(buffer), length, false).rep()->ref();
156 return toRef(rep);
157}
158
159CFStringRef CFStringCreateWithJSInternalString(CFAllocatorRef alloc, JSInternalStringRef string)
160{
161 UString::Rep* rep = toJS(string);
162 return CFStringCreateWithCharacters(alloc, reinterpret_cast<const JSChar*>(rep->data()), rep->size());
163}
164
165#endif // __APPLE__
Note: See TracBrowser for help on using the repository browser.