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 |
|
---|
43 | using namespace KJS;
|
---|
44 |
|
---|
45 | JSClassRef 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 |
|
---|
55 | JSClassRef JSClassRetain(JSClassRef jsClass)
|
---|
56 | {
|
---|
57 | JSLock lock;
|
---|
58 | ++jsClass->refCount;
|
---|
59 | return jsClass;
|
---|
60 | }
|
---|
61 |
|
---|
62 | void JSClassRelease(JSClassRef jsClass)
|
---|
63 | {
|
---|
64 | JSLock lock;
|
---|
65 | if (--jsClass->refCount == 0)
|
---|
66 | delete jsClass;
|
---|
67 | }
|
---|
68 |
|
---|
69 | JSObjectRef 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 |
|
---|
84 | JSObjectRef 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 |
|
---|
93 | JSObjectRef 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 |
|
---|
107 | JSObjectRef 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 |
|
---|
132 | JSValueRef JSObjectGetPrototype(JSContextRef, JSObjectRef object)
|
---|
133 | {
|
---|
134 | JSObject* jsObject = toJS(object);
|
---|
135 | return toRef(jsObject->prototype());
|
---|
136 | }
|
---|
137 |
|
---|
138 | void JSObjectSetPrototype(JSContextRef, JSObjectRef object, JSValueRef value)
|
---|
139 | {
|
---|
140 | JSObject* jsObject = toJS(object);
|
---|
141 | JSValue* jsValue = toJS(value);
|
---|
142 |
|
---|
143 | jsObject->setPrototype(jsValue);
|
---|
144 | }
|
---|
145 |
|
---|
146 | bool 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 |
|
---|
156 | JSValueRef 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 |
|
---|
172 | void 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 |
|
---|
188 | JSValueRef 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 |
|
---|
204 | void 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 |
|
---|
219 | bool 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 |
|
---|
235 | void* 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 |
|
---|
245 | bool 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 |
|
---|
257 | bool JSObjectIsFunction(JSContextRef, JSObjectRef object)
|
---|
258 | {
|
---|
259 | JSObject* jsObject = toJS(object);
|
---|
260 | return jsObject->implementsCall();
|
---|
261 | }
|
---|
262 |
|
---|
263 | JSValueRef 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 |
|
---|
287 | bool JSObjectIsConstructor(JSContextRef, JSObjectRef object)
|
---|
288 | {
|
---|
289 | JSObject* jsObject = toJS(object);
|
---|
290 | return jsObject->implementsConstruct();
|
---|
291 | }
|
---|
292 |
|
---|
293 | JSObjectRef 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 |
|
---|
313 | struct OpaqueJSPropertyNameArray
|
---|
314 | {
|
---|
315 | OpaqueJSPropertyNameArray() : refCount(0)
|
---|
316 | {
|
---|
317 | }
|
---|
318 |
|
---|
319 | unsigned refCount;
|
---|
320 | PropertyNameArray array;
|
---|
321 | };
|
---|
322 |
|
---|
323 | JSPropertyNameArrayRef 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 |
|
---|
335 | JSPropertyNameArrayRef JSPropertyNameArrayRetain(JSPropertyNameArrayRef array)
|
---|
336 | {
|
---|
337 | JSLock lock;
|
---|
338 | ++array->refCount;
|
---|
339 | return array;
|
---|
340 | }
|
---|
341 |
|
---|
342 | void JSPropertyNameArrayRelease(JSPropertyNameArrayRef array)
|
---|
343 | {
|
---|
344 | JSLock lock;
|
---|
345 | if (--array->refCount == 0)
|
---|
346 | delete array;
|
---|
347 | }
|
---|
348 |
|
---|
349 | size_t JSPropertyNameArrayGetCount(JSPropertyNameArrayRef array)
|
---|
350 | {
|
---|
351 | return array->array.size();
|
---|
352 | }
|
---|
353 |
|
---|
354 | JSStringRef JSPropertyNameArrayGetNameAtIndex(JSPropertyNameArrayRef array, size_t index)
|
---|
355 | {
|
---|
356 | return toRef(array->array[static_cast<unsigned>(index)].ustring().rep());
|
---|
357 | }
|
---|
358 |
|
---|
359 | void 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 | }
|
---|