source: webkit/trunk/JavaScriptCore/API/JSObjectRef.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: 9.8 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 "JSValueRef.h"
29#include "JSObjectRef.h"
30#include "JSCallbackConstructor.h"
31#include "JSCallbackFunction.h"
32#include "JSCallbackObject.h"
33
34#include "identifier.h"
35#include "function.h"
36#include "nodes.h"
37#include "internal.h"
38#include "object.h"
39#include "reference_list.h"
40
41using namespace KJS;
42
43JSObjectRef JSObjectMake(JSContextRef context, JSClassRef jsClass, JSObjectRef prototype)
44{
45 JSLock lock;
46
47 ExecState* exec = toJS(context);
48 JSObject* jsPrototype = toJS(prototype);
49
50 if (!prototype)
51 jsPrototype = exec->lexicalInterpreter()->builtinObjectPrototype();
52
53 if (!jsClass)
54 return toRef(new JSObject(jsPrototype)); // slightly more efficient
55 else
56 return toRef(new JSCallbackObject(context, jsClass, jsPrototype));
57}
58
59JSObjectRef JSFunctionMake(JSContextRef context, JSCallAsFunctionCallback callAsFunction)
60{
61 JSLock lock;
62 ExecState* exec = toJS(context);
63 return toRef(new JSCallbackFunction(exec, callAsFunction));
64}
65
66JSObjectRef JSConstructorMake(JSContextRef context, JSCallAsConstructorCallback callAsConstructor)
67{
68 JSLock lock;
69 ExecState* exec = toJS(context);
70 return toRef(new JSCallbackConstructor(exec, callAsConstructor));
71}
72
73JSObjectRef JSFunctionMakeWithBody(JSContextRef context, JSInternalStringRef body, JSInternalStringRef sourceURL, int startingLineNumber, JSValueRef* exception)
74{
75 JSLock lock;
76
77 ExecState* exec = toJS(context);
78 UString::Rep* bodyRep = toJS(body);
79 UString jsSourceURL = UString(toJS(sourceURL));
80
81 if (!bodyRep)
82 bodyRep = &UString::Rep::null;
83
84 int sid;
85 int errLine;
86 UString errMsg;
87 RefPtr<FunctionBodyNode> bodyNode = Parser::parse(jsSourceURL, startingLineNumber, bodyRep->data(), bodyRep->size(), &sid, &errLine, &errMsg);
88 if (!bodyNode) {
89 if (exception)
90 *exception = Error::create(exec, SyntaxError, errMsg, errLine, sid, jsSourceURL);
91 return 0;
92 }
93
94 ScopeChain scopeChain;
95 scopeChain.push(exec->dynamicInterpreter()->globalObject());
96 return toRef(static_cast<JSObject*>(new DeclaredFunctionImp(exec, "anonymous", bodyNode.get(), scopeChain)));
97}
98
99JSInternalStringRef JSObjectGetDescription(JSObjectRef object)
100{
101 JSLock lock;
102 JSObject* jsObject = toJS(object);
103 return toRef(jsObject->className().rep());
104}
105
106JSValueRef JSObjectGetPrototype(JSObjectRef object)
107{
108 JSObject* jsObject = toJS(object);
109 return toRef(jsObject->prototype());
110}
111
112void JSObjectSetPrototype(JSObjectRef object, JSValueRef value)
113{
114 JSObject* jsObject = toJS(object);
115 JSValue* jsValue = toJS(value);
116
117 jsObject->setPrototype(jsValue);
118}
119
120bool JSObjectHasProperty(JSContextRef context, JSObjectRef object, JSInternalStringRef propertyName)
121{
122 JSLock lock;
123 ExecState* exec = toJS(context);
124 JSObject* jsObject = toJS(object);
125 UString::Rep* nameRep = toJS(propertyName);
126
127 return jsObject->hasProperty(exec, Identifier(nameRep));
128}
129
130JSValueRef JSObjectGetProperty(JSContextRef context, JSObjectRef object, JSInternalStringRef propertyName)
131{
132 JSLock lock;
133 ExecState* exec = toJS(context);
134 JSObject* jsObject = toJS(object);
135 UString::Rep* nameRep = toJS(propertyName);
136
137 JSValue* jsValue = jsObject->get(exec, Identifier(nameRep));
138 if (jsValue->isUndefined())
139 return 0;
140 return jsValue;
141}
142
143bool JSObjectSetProperty(JSContextRef context, JSObjectRef object, JSInternalStringRef propertyName, JSValueRef value, JSPropertyAttributes attributes)
144{
145 JSLock lock;
146 ExecState* exec = toJS(context);
147 JSObject* jsObject = toJS(object);
148 UString::Rep* nameRep = toJS(propertyName);
149 JSValue* jsValue = toJS(value);
150
151 Identifier jsNameID = Identifier(nameRep);
152 if (jsObject->canPut(exec, jsNameID)) {
153 jsObject->put(exec, jsNameID, jsValue, attributes);
154 return true;
155 } else
156 return false;
157}
158
159bool JSObjectDeleteProperty(JSContextRef context, JSObjectRef object, JSInternalStringRef propertyName)
160{
161 JSLock lock;
162 ExecState* exec = toJS(context);
163 JSObject* jsObject = toJS(object);
164 UString::Rep* nameRep = toJS(propertyName);
165
166 return jsObject->deleteProperty(exec, Identifier(nameRep));
167}
168
169void* JSObjectGetPrivate(JSObjectRef object)
170{
171 JSObject* jsObject = toJS(object);
172
173 if (jsObject->inherits(&JSCallbackObject::info))
174 return static_cast<JSCallbackObject*>(jsObject)->getPrivate();
175
176 if (jsObject->inherits(&JSCallbackFunction::info))
177 return static_cast<JSCallbackFunction*>(jsObject)->getPrivate();
178
179 if (jsObject->inherits(&JSCallbackConstructor::info))
180 return static_cast<JSCallbackConstructor*>(jsObject)->getPrivate();
181
182 return 0;
183}
184
185bool JSObjectSetPrivate(JSObjectRef object, void* data)
186{
187 JSObject* jsObject = toJS(object);
188
189 if (jsObject->inherits(&JSCallbackObject::info)) {
190 static_cast<JSCallbackObject*>(jsObject)->setPrivate(data);
191 return true;
192 }
193
194 if (jsObject->inherits(&JSCallbackFunction::info)) {
195 static_cast<JSCallbackFunction*>(jsObject)->setPrivate(data);
196 return true;
197 }
198
199 if (jsObject->inherits(&JSCallbackConstructor::info)) {
200 static_cast<JSCallbackConstructor*>(jsObject)->setPrivate(data);
201 return true;
202 }
203
204 return false;
205}
206
207bool JSObjectIsFunction(JSObjectRef object)
208{
209 JSObject* jsObject = toJS(object);
210 return jsObject->implementsCall();
211}
212
213JSValueRef JSObjectCallAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argc, JSValueRef argv[], JSValueRef* exception)
214{
215 JSLock lock;
216 ExecState* exec = toJS(context);
217 JSObject* jsObject = toJS(object);
218 JSObject* jsThisObject = toJS(thisObject);
219
220 if (!jsThisObject)
221 jsThisObject = exec->dynamicInterpreter()->globalObject();
222
223 List argList;
224 for (size_t i = 0; i < argc; i++)
225 argList.append(toJS(argv[i]));
226
227 JSValueRef result = toRef(jsObject->call(exec, jsThisObject, argList)); // returns NULL if object->implementsCall() is false
228 if (exec->hadException()) {
229 if (exception)
230 *exception = exec->exception();
231 exec->clearException();
232 result = 0;
233 }
234 return result;
235}
236
237bool JSObjectIsConstructor(JSObjectRef object)
238{
239 JSObject* jsObject = toJS(object);
240 return jsObject->implementsConstruct();
241}
242
243JSObjectRef JSObjectCallAsConstructor(JSContextRef context, JSObjectRef object, size_t argc, JSValueRef argv[], JSValueRef* exception)
244{
245 JSLock lock;
246 ExecState* exec = toJS(context);
247 JSObject* jsObject = toJS(object);
248
249 List argList;
250 for (size_t i = 0; i < argc; i++)
251 argList.append(toJS(argv[i]));
252
253 JSObjectRef result = toRef(jsObject->construct(exec, argList)); // returns NULL if object->implementsCall() is false
254 if (exec->hadException()) {
255 if (exception)
256 *exception = exec->exception();
257 exec->clearException();
258 result = 0;
259 }
260 return result;
261}
262
263struct __JSPropertyEnumerator
264{
265 __JSPropertyEnumerator() : refCount(0), iterator(list.end())
266 {
267 }
268
269 unsigned refCount;
270 ReferenceList list;
271 ReferenceListIterator iterator;
272};
273
274JSPropertyEnumeratorRef JSObjectCreatePropertyEnumerator(JSContextRef context, JSObjectRef object)
275{
276 JSLock lock;
277 ExecState* exec = toJS(context);
278 JSObject* jsObject = toJS(object);
279
280 JSPropertyEnumeratorRef enumerator = new __JSPropertyEnumerator();
281 jsObject->getPropertyList(exec, enumerator->list);
282 enumerator->iterator = enumerator->list.begin();
283
284 return JSPropertyEnumeratorRetain(enumerator);
285}
286
287JSInternalStringRef JSPropertyEnumeratorGetNext(JSPropertyEnumeratorRef enumerator)
288{
289 ReferenceListIterator& iterator = enumerator->iterator;
290 if (iterator != enumerator->list.end()) {
291 JSInternalStringRef result = toRef(iterator->getPropertyName().ustring().rep());
292 iterator++;
293 return result;
294 }
295 return 0;
296}
297
298JSPropertyEnumeratorRef JSPropertyEnumeratorRetain(JSPropertyEnumeratorRef enumerator)
299{
300 ++enumerator->refCount;
301 return enumerator;
302}
303
304void JSPropertyEnumeratorRelease(JSPropertyEnumeratorRef enumerator)
305{
306 if (--enumerator->refCount == 0)
307 delete enumerator;
308}
309
310void JSPropertyListAdd(JSPropertyListRef propertyList, JSObjectRef thisObject, JSInternalStringRef propertyName)
311{
312 JSLock lock;
313 ReferenceList* jsPropertyList = toJS(propertyList);
314 JSObject* jsObject = toJS(thisObject);
315 UString::Rep* rep = toJS(propertyName);
316
317 jsPropertyList->append(Reference(jsObject, Identifier(rep)));
318}
Note: See TracBrowser for help on using the repository browser.