source: webkit/trunk/JavaScriptCore/API/JSObjectRef.cpp@ 20310

Last change on this file since 20310 was 20310, checked in by mjs, 18 years ago

JavaScriptCore:

Reviewed by Mark.


  • avoid static construction (and global variable access) in a smarter, more portable way, to later enable MUTLI_THREAD mode to work on other platforms and compilers.


  • kjs/CommonIdentifiers.cpp: Added. New class to hold all the shared identifiers. (KJS::CommonIdentifiers::CommonIdentifiers): (KJS::CommonIdentifiers::shared):
  • kjs/CommonIdentifiers.h: Added.
  • kjs/ExecState.h: (KJS::ExecState::propertyNames): Hand the CommonIdentifiers instance here for easy access. (KJS::ExecState::ExecState):
  • API/JSObjectRef.cpp: (JSObjectMakeConstructor):
  • CMakeLists.txt:
  • JavaScriptCore.exp:
  • JavaScriptCore.pri:
  • JavaScriptCore.vcproj/JavaScriptCore/JavaScriptCore.vcproj:
  • JavaScriptCore.xcodeproj/project.pbxproj:
  • JavaScriptCoreSources.bkl:
  • bindings/runtime_array.cpp: (RuntimeArray::getOwnPropertySlot): (RuntimeArray::put):
  • bindings/runtime_method.cpp: (RuntimeMethod::getOwnPropertySlot):
  • kjs/array_object.cpp: (ArrayInstance::getOwnPropertySlot): (ArrayInstance::put): (ArrayInstance::deleteProperty): (ArrayProtoFunc::ArrayProtoFunc): (ArrayProtoFunc::callAsFunction): (ArrayObjectImp::ArrayObjectImp):
  • kjs/bool_object.cpp: (BooleanPrototype::BooleanPrototype): (BooleanProtoFunc::BooleanProtoFunc): (BooleanProtoFunc::callAsFunction): (BooleanObjectImp::BooleanObjectImp):
  • kjs/completion.h: (KJS::Completion::Completion):
  • kjs/date_object.cpp: (KJS::DateProtoFunc::DateProtoFunc): (KJS::DateObjectImp::DateObjectImp): (KJS::DateObjectFuncImp::DateObjectFuncImp):
  • kjs/error_object.cpp: (ErrorPrototype::ErrorPrototype): (ErrorProtoFunc::ErrorProtoFunc): (ErrorProtoFunc::callAsFunction): (ErrorObjectImp::ErrorObjectImp): (ErrorObjectImp::construct): (NativeErrorPrototype::NativeErrorPrototype): (NativeErrorImp::NativeErrorImp): (NativeErrorImp::construct): (NativeErrorImp::callAsFunction):
  • kjs/function.cpp: (KJS::FunctionImp::getOwnPropertySlot): (KJS::FunctionImp::put): (KJS::FunctionImp::deleteProperty): (KJS::FunctionImp::getParameterName): (KJS::DeclaredFunctionImp::construct): (KJS::IndexToNameMap::unMap): (KJS::Arguments::Arguments): (KJS::ActivationImp::getOwnPropertySlot): (KJS::ActivationImp::deleteProperty): (KJS::GlobalFuncImp::GlobalFuncImp):
  • kjs/function_object.cpp: (FunctionPrototype::FunctionPrototype): (FunctionProtoFunc::FunctionProtoFunc): (FunctionProtoFunc::callAsFunction): (FunctionObjectImp::FunctionObjectImp): (FunctionObjectImp::construct):
  • kjs/grammar.y:
  • kjs/identifier.cpp:
  • kjs/identifier.h:
  • kjs/interpreter.cpp: (KJS::Interpreter::init): (KJS::Interpreter::initGlobalObject):
  • kjs/interpreter.h:
  • kjs/lookup.h:
  • kjs/math_object.cpp: (MathFuncImp::MathFuncImp):
  • kjs/nodes.cpp: (ArrayNode::evaluate): (FuncDeclNode::processFuncDecl): (FuncExprNode::evaluate):
  • kjs/number_object.cpp: (NumberPrototype::NumberPrototype): (NumberProtoFunc::NumberProtoFunc): (NumberObjectImp::NumberObjectImp):
  • kjs/object.cpp: (KJS::JSObject::put): (KJS::JSObject::defaultValue): (KJS::JSObject::hasInstance):
  • kjs/object.h: (KJS::JSObject::getOwnPropertySlot):
  • kjs/object_object.cpp: (ObjectPrototype::ObjectPrototype): (ObjectProtoFunc::ObjectProtoFunc): (ObjectObjectImp::ObjectObjectImp):
  • kjs/regexp_object.cpp: (RegExpPrototype::RegExpPrototype): (RegExpProtoFunc::RegExpProtoFunc): (RegExpObjectImp::RegExpObjectImp):
  • kjs/string_object.cpp: (KJS::StringInstance::getOwnPropertySlot): (KJS::StringInstance::put): (KJS::StringInstance::deleteProperty): (KJS::StringPrototype::StringPrototype): (KJS::StringProtoFunc::StringProtoFunc): (KJS::StringProtoFunc::callAsFunction): (KJS::StringObjectImp::StringObjectImp): (KJS::StringObjectFuncImp::StringObjectFuncImp):
  • kjs/testkjs.cpp: (TestFunctionImp::TestFunctionImp):

WebCore:

Reviewed by Mark.

  • avoid static construction (and global variable access) in a smarter, more portable way, to later enable MUTLI_THREAD mode to work on other platforms and compilers.


  • bindings/js/JSCustomXPathNSResolver.cpp: (WebCore::JSCustomXPathNSResolver::lookupNamespaceURI):
  • bindings/js/JSHTMLInputElementBase.cpp: (WebCore::JSHTMLInputElementBaseFunction::JSHTMLInputElementBaseFunction):
  • bindings/js/JSHTMLOptionElementConstructor.cpp: (WebCore::JSHTMLOptionElementConstructor::JSHTMLOptionElementConstructor):
  • bindings/js/JSXMLHttpRequest.cpp: (KJS::JSXMLHttpRequestConstructorImp::JSXMLHttpRequestConstructorImp):
  • bindings/js/JSXSLTProcessor.cpp: (KJS::XSLTProcessorConstructorImp::XSLTProcessorConstructorImp):
  • bindings/js/kjs_dom.cpp: (KJS::DOMNamedNodeMap::getOwnPropertySlot): (KJS::DOMNamedNodesCollection::getOwnPropertySlot):
  • bindings/js/kjs_events.cpp: (KJS::JSAbstractEventListener::handleEvent):
  • bindings/js/kjs_html.cpp: (KJS::HTMLElementFunction::HTMLElementFunction): (KJS::JSHTMLCollection::getOwnPropertySlot):
  • bindings/js/kjs_window.cpp: (KJS::ScheduledAction::execute):
  • bindings/objc/WebScriptObject.mm:
  • bindings/scripts/CodeGeneratorJS.pm:
File size: 11.2 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 <wtf/Platform.h>
28#include "APICast.h"
29#include "JSValueRef.h"
30#include "JSObjectRef.h"
31#include "JSCallbackConstructor.h"
32#include "JSCallbackFunction.h"
33#include "JSCallbackObject.h"
34#include "JSClassRef.h"
35
36#include "identifier.h"
37#include "function.h"
38#include "nodes.h"
39#include "internal.h"
40#include "object.h"
41#include "PropertyNameArray.h"
42
43using namespace KJS;
44
45JSClassRef JSClassCreate(JSClassDefinition* definition)
46{
47 JSLock lock;
48 JSClassRef jsClass = (definition->attributes & kJSClassAttributeNoAutomaticPrototype)
49 ? OpaqueJSClass::createNoAutomaticPrototype(definition)
50 : OpaqueJSClass::create(definition);
51
52 return JSClassRetain(jsClass);
53}
54
55JSClassRef JSClassRetain(JSClassRef jsClass)
56{
57 JSLock lock;
58 ++jsClass->refCount;
59 return jsClass;
60}
61
62void JSClassRelease(JSClassRef jsClass)
63{
64 JSLock lock;
65 if (--jsClass->refCount == 0)
66 delete jsClass;
67}
68
69JSObjectRef JSObjectMake(JSContextRef ctx, JSClassRef jsClass, void* data)
70{
71 JSLock lock;
72 ExecState* exec = toJS(ctx);
73
74 if (!jsClass)
75 return toRef(new JSObject(exec->lexicalInterpreter()->builtinObjectPrototype())); // slightly more efficient
76
77 JSValue* jsPrototype = jsClass->prototype(ctx);
78 if (!jsPrototype)
79 jsPrototype = exec->lexicalInterpreter()->builtinObjectPrototype();
80
81 return toRef(new JSCallbackObject(exec, jsClass, jsPrototype, data));
82}
83
84JSObjectRef JSObjectMakeFunctionWithCallback(JSContextRef ctx, JSStringRef name, JSObjectCallAsFunctionCallback callAsFunction)
85{
86 JSLock lock;
87 ExecState* exec = toJS(ctx);
88 Identifier nameID = name ? Identifier(toJS(name)) : Identifier("anonymous");
89
90 return toRef(new JSCallbackFunction(exec, callAsFunction, nameID));
91}
92
93JSObjectRef JSObjectMakeConstructor(JSContextRef ctx, JSClassRef jsClass, JSObjectCallAsConstructorCallback callAsConstructor)
94{
95 JSLock lock;
96 ExecState* exec = toJS(ctx);
97
98 JSValue* jsPrototype = jsClass
99 ? jsClass->prototype(ctx)
100 : exec->dynamicInterpreter()->builtinObjectPrototype();
101
102 JSObject* constructor = new JSCallbackConstructor(exec, jsClass, callAsConstructor);
103 constructor->put(exec, exec->propertyNames().prototype, jsPrototype, DontEnum|DontDelete|ReadOnly);
104 return toRef(constructor);
105}
106
107JSObjectRef JSObjectMakeFunction(JSContextRef ctx, JSStringRef name, unsigned parameterCount, const JSStringRef parameterNames[], JSStringRef body, JSStringRef sourceURL, int startingLineNumber, JSValueRef* exception)
108{
109 JSLock lock;
110
111 ExecState* exec = toJS(ctx);
112 UString::Rep* bodyRep = toJS(body);
113 UString::Rep* sourceURLRep = sourceURL ? toJS(sourceURL) : &UString::Rep::null;
114
115 Identifier nameID = name ? Identifier(toJS(name)) : Identifier("anonymous");
116
117 List args;
118 for (unsigned i = 0; i < parameterCount; i++)
119 args.append(jsString(UString(toJS(parameterNames[i]))));
120 args.append(jsString(UString(bodyRep)));
121
122 JSObject* result = exec->dynamicInterpreter()->builtinFunction()->construct(exec, args, nameID, UString(sourceURLRep), startingLineNumber);
123 if (exec->hadException()) {
124 if (exception)
125 *exception = toRef(exec->exception());
126 exec->clearException();
127 result = 0;
128 }
129 return toRef(result);
130}
131
132JSValueRef JSObjectGetPrototype(JSContextRef, JSObjectRef object)
133{
134 JSObject* jsObject = toJS(object);
135 return toRef(jsObject->prototype());
136}
137
138void JSObjectSetPrototype(JSContextRef, JSObjectRef object, JSValueRef value)
139{
140 JSObject* jsObject = toJS(object);
141 JSValue* jsValue = toJS(value);
142
143 jsObject->setPrototype(jsValue);
144}
145
146bool JSObjectHasProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName)
147{
148 JSLock lock;
149 ExecState* exec = toJS(ctx);
150 JSObject* jsObject = toJS(object);
151 UString::Rep* nameRep = toJS(propertyName);
152
153 return jsObject->hasProperty(exec, Identifier(nameRep));
154}
155
156JSValueRef JSObjectGetProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
157{
158 JSLock lock;
159 ExecState* exec = toJS(ctx);
160 JSObject* jsObject = toJS(object);
161 UString::Rep* nameRep = toJS(propertyName);
162
163 JSValue* jsValue = jsObject->get(exec, Identifier(nameRep));
164 if (exec->hadException()) {
165 if (exception)
166 *exception = toRef(exec->exception());
167 exec->clearException();
168 }
169 return toRef(jsValue);
170}
171
172void JSObjectSetProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef value, JSPropertyAttributes attributes, JSValueRef* exception)
173{
174 JSLock lock;
175 ExecState* exec = toJS(ctx);
176 JSObject* jsObject = toJS(object);
177 UString::Rep* nameRep = toJS(propertyName);
178 JSValue* jsValue = toJS(value);
179
180 jsObject->put(exec, Identifier(nameRep), jsValue, attributes);
181 if (exec->hadException()) {
182 if (exception)
183 *exception = toRef(exec->exception());
184 exec->clearException();
185 }
186}
187
188JSValueRef JSObjectGetPropertyAtIndex(JSContextRef ctx, JSObjectRef object, unsigned propertyIndex, JSValueRef* exception)
189{
190 JSLock lock;
191 ExecState* exec = toJS(ctx);
192 JSObject* jsObject = toJS(object);
193
194 JSValue* jsValue = jsObject->get(exec, propertyIndex);
195 if (exec->hadException()) {
196 if (exception)
197 *exception = toRef(exec->exception());
198 exec->clearException();
199 }
200 return toRef(jsValue);
201}
202
203
204void JSObjectSetPropertyAtIndex(JSContextRef ctx, JSObjectRef object, unsigned propertyIndex, JSValueRef value, JSValueRef* exception)
205{
206 JSLock lock;
207 ExecState* exec = toJS(ctx);
208 JSObject* jsObject = toJS(object);
209 JSValue* jsValue = toJS(value);
210
211 jsObject->put(exec, propertyIndex, jsValue);
212 if (exec->hadException()) {
213 if (exception)
214 *exception = toRef(exec->exception());
215 exec->clearException();
216 }
217}
218
219bool JSObjectDeleteProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
220{
221 JSLock lock;
222 ExecState* exec = toJS(ctx);
223 JSObject* jsObject = toJS(object);
224 UString::Rep* nameRep = toJS(propertyName);
225
226 bool result = jsObject->deleteProperty(exec, Identifier(nameRep));
227 if (exec->hadException()) {
228 if (exception)
229 *exception = toRef(exec->exception());
230 exec->clearException();
231 }
232 return result;
233}
234
235void* JSObjectGetPrivate(JSObjectRef object)
236{
237 JSObject* jsObject = toJS(object);
238
239 if (jsObject->inherits(&JSCallbackObject::info))
240 return static_cast<JSCallbackObject*>(jsObject)->getPrivate();
241
242 return 0;
243}
244
245bool JSObjectSetPrivate(JSObjectRef object, void* data)
246{
247 JSObject* jsObject = toJS(object);
248
249 if (jsObject->inherits(&JSCallbackObject::info)) {
250 static_cast<JSCallbackObject*>(jsObject)->setPrivate(data);
251 return true;
252 }
253
254 return false;
255}
256
257bool JSObjectIsFunction(JSContextRef, JSObjectRef object)
258{
259 JSObject* jsObject = toJS(object);
260 return jsObject->implementsCall();
261}
262
263JSValueRef JSObjectCallAsFunction(JSContextRef ctx, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
264{
265 JSLock lock;
266 ExecState* exec = toJS(ctx);
267 JSObject* jsObject = toJS(object);
268 JSObject* jsThisObject = toJS(thisObject);
269
270 if (!jsThisObject)
271 jsThisObject = exec->dynamicInterpreter()->globalObject();
272
273 List argList;
274 for (size_t i = 0; i < argumentCount; i++)
275 argList.append(toJS(arguments[i]));
276
277 JSValueRef result = toRef(jsObject->call(exec, jsThisObject, argList)); // returns NULL if object->implementsCall() is false
278 if (exec->hadException()) {
279 if (exception)
280 *exception = toRef(exec->exception());
281 exec->clearException();
282 result = 0;
283 }
284 return result;
285}
286
287bool JSObjectIsConstructor(JSContextRef, JSObjectRef object)
288{
289 JSObject* jsObject = toJS(object);
290 return jsObject->implementsConstruct();
291}
292
293JSObjectRef JSObjectCallAsConstructor(JSContextRef ctx, JSObjectRef object, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
294{
295 JSLock lock;
296 ExecState* exec = toJS(ctx);
297 JSObject* jsObject = toJS(object);
298
299 List argList;
300 for (size_t i = 0; i < argumentCount; i++)
301 argList.append(toJS(arguments[i]));
302
303 JSObjectRef result = toRef(jsObject->construct(exec, argList)); // returns NULL if object->implementsCall() is false
304 if (exec->hadException()) {
305 if (exception)
306 *exception = toRef(exec->exception());
307 exec->clearException();
308 result = 0;
309 }
310 return result;
311}
312
313struct OpaqueJSPropertyNameArray
314{
315 OpaqueJSPropertyNameArray() : refCount(0)
316 {
317 }
318
319 unsigned refCount;
320 PropertyNameArray array;
321};
322
323JSPropertyNameArrayRef JSObjectCopyPropertyNames(JSContextRef ctx, JSObjectRef object)
324{
325 JSLock lock;
326 JSObject* jsObject = toJS(object);
327 ExecState* exec = toJS(ctx);
328
329 JSPropertyNameArrayRef propertyNames = new OpaqueJSPropertyNameArray();
330 jsObject->getPropertyNames(exec, propertyNames->array);
331
332 return JSPropertyNameArrayRetain(propertyNames);
333}
334
335JSPropertyNameArrayRef JSPropertyNameArrayRetain(JSPropertyNameArrayRef array)
336{
337 JSLock lock;
338 ++array->refCount;
339 return array;
340}
341
342void JSPropertyNameArrayRelease(JSPropertyNameArrayRef array)
343{
344 JSLock lock;
345 if (--array->refCount == 0)
346 delete array;
347}
348
349size_t JSPropertyNameArrayGetCount(JSPropertyNameArrayRef array)
350{
351 return array->array.size();
352}
353
354JSStringRef JSPropertyNameArrayGetNameAtIndex(JSPropertyNameArrayRef array, size_t index)
355{
356 return toRef(array->array[static_cast<unsigned>(index)].ustring().rep());
357}
358
359void JSPropertyNameAccumulatorAddName(JSPropertyNameAccumulatorRef array, JSStringRef propertyName)
360{
361 JSLock lock;
362 PropertyNameArray* propertyNames = toJS(array);
363 UString::Rep* rep = toJS(propertyName);
364
365 propertyNames->add(Identifier(rep));
366}
Note: See TracBrowser for help on using the repository browser.