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

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

Approved by Maciej, Darin.


Renamed JSStringBufferRef to JSInternalStringRef. "Internal string" means the
JavaScript engine's internal string representation, which is the most
low-level and efficient representation to use when interfacing with JavaScript.

  • API/APICast.h: (toJS): (toRef):
  • API/JSBase.h:
  • API/JSCallbackObject.cpp: (KJS::JSCallbackObject::getOwnPropertySlot): (KJS::JSCallbackObject::put): (KJS::JSCallbackObject::deleteProperty): (KJS::JSCallbackObject::staticValueGetter): (KJS::JSCallbackObject::callbackGetter):
  • API/JSContextRef.cpp: (JSEvaluate): (JSCheckSyntax):
  • API/JSContextRef.h:
  • API/JSInternalStringRef.cpp: Added. (JSStringMake): (JSInternalStringCreate): (JSInternalStringCreateUTF8): (JSInternalStringRetain): (JSInternalStringRelease): (JSValueCopyStringValue): (JSInternalStringGetLength): (JSInternalStringGetCharactersPtr): (JSInternalStringGetCharacters): (JSInternalStringGetMaxLengthUTF8): (JSInternalStringGetCharactersUTF8): (JSInternalStringIsEqual): (JSInternalStringIsEqualUTF8): (JSInternalStringCreateCF): (CFStringCreateWithJSInternalString):
  • API/JSInternalStringRef.h: Added.
  • API/JSNode.c: (JSNodePrototype_appendChild): (JSNode_getNodeType): (JSNode_getChildNodes): (JSNode_getFirstChild):
  • API/JSNodeList.c: (JSNodeList_length): (JSNodeList_getProperty):
  • API/JSObjectRef.cpp: (JSFunctionMakeWithBody): (JSObjectGetDescription): (JSObjectHasProperty): (JSObjectGetProperty): (JSObjectSetProperty): (JSObjectDeleteProperty): (JSPropertyEnumeratorGetNext): (JSPropertyListAdd):
  • API/JSObjectRef.h:
  • API/JSStringBufferRef.cpp: Removed.
  • API/JSStringBufferRef.h: Removed.
  • API/JSValueRef.h:
  • API/JavaScriptCore.h:
  • API/minidom.c: (main): (print):
  • API/testapi.c: (assertEqualsAsUTF8String): (assertEqualsAsCharactersPtr): (assertEqualsAsCharacters): (MyObject_hasProperty): (MyObject_getProperty): (MyObject_setProperty): (MyObject_deleteProperty): (MyObject_getPropertyList): (print_callAsFunction): (myConstructor_callAsConstructor): (main):
  • JavaScriptCore.exp:
  • JavaScriptCore.xcodeproj/project.pbxproj:
File size: 5.4 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 buffer)
40{
41 JSLock lock;
42 UString::Rep* rep = toJS(buffer);
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 buffer)
61{
62 UString::Rep* rep = toJS(buffer);
63 return toRef(rep->ref());
64}
65
66void JSInternalStringRelease(JSInternalStringRef buffer)
67{
68 JSLock lock;
69 UString::Rep* rep = toJS(buffer);
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 charBufferRef = toRef(jsValue->toString(exec).rep()->ref());
80 if (exec->hadException())
81 exec->clearException();
82 return charBufferRef;
83}
84
85size_t JSInternalStringGetLength(JSInternalStringRef buffer)
86{
87 UString::Rep* rep = toJS(buffer);
88 return rep->size();
89}
90
91const JSChar* JSInternalStringGetCharactersPtr(JSInternalStringRef buffer)
92{
93 UString::Rep* rep = toJS(buffer);
94 return reinterpret_cast<const JSChar*>(rep->data());
95}
96
97void JSInternalStringGetCharacters(JSInternalStringRef inBuffer, JSChar* outBuffer, size_t numChars)
98{
99 UString::Rep* rep = toJS(inBuffer);
100 const JSChar* data = reinterpret_cast<const JSChar*>(rep->data());
101
102 memcpy(outBuffer, data, numChars * sizeof(JSChar));
103}
104
105size_t JSInternalStringGetMaxLengthUTF8(JSInternalStringRef buffer)
106{
107 UString::Rep* rep = toJS(buffer);
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 inBuffer, char* outBuffer, size_t bufferSize)
114{
115 JSLock lock;
116 UString::Rep* rep = toJS(inBuffer);
117 CString cString = UString(rep).UTF8String();
118
119 size_t length = std::min(bufferSize, cString.size() + 1); // + 1 for terminating '\0'
120 memcpy(outBuffer, 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 buffer)
160{
161 UString::Rep* rep = toJS(buffer);
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.