source: webkit/trunk/JavaScriptCore/API/JSStringRef.cpp@ 15404

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

Pleasing to Maciej.


  • Renamed JSEvaluate -> JSEvaluateScript, JSCheckSyntax -> JSCheckScriptSyntax
  • Added exception out parameters to JSValueTo* and JSValueIsEqual because they can throw
  • Removed JSObjectGetDescription because it's useless and vague, and JSValueToString/JSValueIsObjectOfClass do a better job, anyway
  • Clarified comments about "IsFunction/Constructor" to indicate that they are true of all functions/constructors, not just those created by JSObjectMake*
File size: 4.5 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 "JSStringRef.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 JSValueMakeString(JSStringRef string)
40{
41 JSLock lock;
42 UString::Rep* rep = toJS(string);
43 return toRef(jsString(UString(rep)));
44}
45
46JSStringRef JSStringCreateWithCharacters(const JSChar* chars, size_t numChars)
47{
48 JSLock lock;
49 return toRef(UString(reinterpret_cast<const UChar*>(chars), numChars).rep()->ref());
50}
51
52JSStringRef JSStringCreateWithUTF8CString(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
60JSStringRef JSStringRetain(JSStringRef string)
61{
62 UString::Rep* rep = toJS(string);
63 return toRef(rep->ref());
64}
65
66void JSStringRelease(JSStringRef string)
67{
68 JSLock lock;
69 UString::Rep* rep = toJS(string);
70 rep->deref();
71}
72
73size_t JSStringGetLength(JSStringRef string)
74{
75 UString::Rep* rep = toJS(string);
76 return rep->size();
77}
78
79const JSChar* JSStringGetCharactersPtr(JSStringRef string)
80{
81 UString::Rep* rep = toJS(string);
82 return reinterpret_cast<const JSChar*>(rep->data());
83}
84
85size_t JSStringGetMaximumUTF8CStringSize(JSStringRef string)
86{
87 UString::Rep* rep = toJS(string);
88
89 // Any UTF8 character > 3 bytes encodes as a UTF16 surrogate pair.
90 return rep->size() * 3 + 1; // + 1 for terminating '\0'
91}
92
93size_t JSStringGetUTF8CString(JSStringRef string, char* buffer, size_t bufferSize)
94{
95 JSLock lock;
96 UString::Rep* rep = toJS(string);
97 CString cString = UString(rep).UTF8String();
98
99 size_t length = std::min(bufferSize, cString.size() + 1); // + 1 for terminating '\0'
100 memcpy(buffer, cString.c_str(), length);
101 return length;
102}
103
104bool JSStringIsEqual(JSStringRef a, JSStringRef b)
105{
106 UString::Rep* aRep = toJS(a);
107 UString::Rep* bRep = toJS(b);
108
109 return UString(aRep) == UString(bRep);
110}
111
112bool JSStringIsEqualToUTF8CString(JSStringRef a, const char* b)
113{
114 JSStringRef bBuf = JSStringCreateWithUTF8CString(b);
115 bool result = JSStringIsEqual(a, bBuf);
116 JSStringRelease(bBuf);
117
118 return result;
119}
120
121#if defined(__APPLE__)
122JSStringRef JSStringCreateWithCFString(CFStringRef string)
123{
124 JSLock lock;
125 CFIndex length = CFStringGetLength(string);
126
127 // Optimized path for when CFString backing store is a UTF16 buffer
128 if (const UniChar* buffer = CFStringGetCharactersPtr(string)) {
129 UString::Rep* rep = UString(reinterpret_cast<const UChar*>(buffer), length).rep()->ref();
130 return toRef(rep);
131 }
132
133 UniChar* buffer = static_cast<UniChar*>(fastMalloc(sizeof(UniChar) * length));
134 CFStringGetCharacters(string, CFRangeMake(0, length), buffer);
135 UString::Rep* rep = UString(reinterpret_cast<UChar*>(buffer), length, false).rep()->ref();
136 return toRef(rep);
137}
138
139CFStringRef JSStringCopyCFString(CFAllocatorRef alloc, JSStringRef string)
140{
141 UString::Rep* rep = toJS(string);
142 return CFStringCreateWithCharacters(alloc, reinterpret_cast<const JSChar*>(rep->data()), rep->size());
143}
144
145#endif // __APPLE__
Note: See TracBrowser for help on using the repository browser.