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 "JavaScriptCore.h"
|
---|
28 | #include <math.h>
|
---|
29 | #include <wtf/Assertions.h>
|
---|
30 | #include <wtf/UnusedParam.h>
|
---|
31 |
|
---|
32 | #if COMPILER(MSVC)
|
---|
33 |
|
---|
34 | #include <wtf/MathExtras.h>
|
---|
35 |
|
---|
36 | static double nan(const char*)
|
---|
37 | {
|
---|
38 | return std::numeric_limits<double>::quiet_NaN();
|
---|
39 | }
|
---|
40 |
|
---|
41 | #endif
|
---|
42 |
|
---|
43 | static JSGlobalContextRef context = 0;
|
---|
44 |
|
---|
45 | static void assertEqualsAsBoolean(JSValueRef value, bool expectedValue)
|
---|
46 | {
|
---|
47 | if (JSValueToBoolean(context, value) != expectedValue)
|
---|
48 | fprintf(stderr, "assertEqualsAsBoolean failed: %p, %d\n", value, expectedValue);
|
---|
49 | }
|
---|
50 |
|
---|
51 | static void assertEqualsAsNumber(JSValueRef value, double expectedValue)
|
---|
52 | {
|
---|
53 | double number = JSValueToNumber(context, value, NULL);
|
---|
54 |
|
---|
55 | // FIXME <rdar://4668451> - On i386 the isnan(double) macro tries to map to the isnan(float) function,
|
---|
56 | // causing a build break with -Wshorten-64-to-32 enabled. The issue is known by the appropriate team.
|
---|
57 | // After that's resolved, we can remove these casts
|
---|
58 | if (number != expectedValue && !(isnan((float)number) && isnan((float)expectedValue)))
|
---|
59 | fprintf(stderr, "assertEqualsAsNumber failed: %p, %lf\n", value, expectedValue);
|
---|
60 | }
|
---|
61 |
|
---|
62 | static void assertEqualsAsUTF8String(JSValueRef value, const char* expectedValue)
|
---|
63 | {
|
---|
64 | JSStringRef valueAsString = JSValueToStringCopy(context, value, NULL);
|
---|
65 |
|
---|
66 | size_t jsSize = JSStringGetMaximumUTF8CStringSize(valueAsString);
|
---|
67 | char* jsBuffer = (char*)malloc(jsSize);
|
---|
68 | JSStringGetUTF8CString(valueAsString, jsBuffer, jsSize);
|
---|
69 |
|
---|
70 | unsigned i;
|
---|
71 | for (i = 0; jsBuffer[i]; i++)
|
---|
72 | if (jsBuffer[i] != expectedValue[i])
|
---|
73 | fprintf(stderr, "assertEqualsAsUTF8String failed at character %d: %c(%d) != %c(%d)\n", i, jsBuffer[i], jsBuffer[i], expectedValue[i], expectedValue[i]);
|
---|
74 |
|
---|
75 | if (jsSize < strlen(jsBuffer) + 1)
|
---|
76 | fprintf(stderr, "assertEqualsAsUTF8String failed: jsSize was too small\n");
|
---|
77 |
|
---|
78 | free(jsBuffer);
|
---|
79 | JSStringRelease(valueAsString);
|
---|
80 | }
|
---|
81 |
|
---|
82 | static void assertEqualsAsCharactersPtr(JSValueRef value, const char* expectedValue)
|
---|
83 | {
|
---|
84 | JSStringRef valueAsString = JSValueToStringCopy(context, value, NULL);
|
---|
85 |
|
---|
86 | size_t jsLength = JSStringGetLength(valueAsString);
|
---|
87 | const JSChar* jsBuffer = JSStringGetCharactersPtr(valueAsString);
|
---|
88 |
|
---|
89 | CFStringRef expectedValueAsCFString = CFStringCreateWithCString(kCFAllocatorDefault,
|
---|
90 | expectedValue,
|
---|
91 | kCFStringEncodingUTF8);
|
---|
92 | CFIndex cfLength = CFStringGetLength(expectedValueAsCFString);
|
---|
93 | UniChar* cfBuffer = (UniChar*)malloc(cfLength * sizeof(UniChar));
|
---|
94 | CFStringGetCharacters(expectedValueAsCFString, CFRangeMake(0, cfLength), cfBuffer);
|
---|
95 | CFRelease(expectedValueAsCFString);
|
---|
96 |
|
---|
97 | if (memcmp(jsBuffer, cfBuffer, cfLength * sizeof(UniChar)) != 0)
|
---|
98 | fprintf(stderr, "assertEqualsAsCharactersPtr failed: jsBuffer != cfBuffer\n");
|
---|
99 |
|
---|
100 | if (jsLength != (size_t)cfLength)
|
---|
101 | fprintf(stderr, "assertEqualsAsCharactersPtr failed: jsLength(%ld) != cfLength(%ld)\n", jsLength, cfLength);
|
---|
102 |
|
---|
103 | free(cfBuffer);
|
---|
104 | JSStringRelease(valueAsString);
|
---|
105 | }
|
---|
106 |
|
---|
107 | static JSValueRef jsGlobalValue; // non-stack value for testing JSValueProtect()
|
---|
108 |
|
---|
109 | /* MyObject pseudo-class */
|
---|
110 |
|
---|
111 | static bool MyObject_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName)
|
---|
112 | {
|
---|
113 | UNUSED_PARAM(context);
|
---|
114 | UNUSED_PARAM(object);
|
---|
115 |
|
---|
116 | if (JSStringIsEqualToUTF8CString(propertyName, "alwaysOne")
|
---|
117 | || JSStringIsEqualToUTF8CString(propertyName, "cantFind")
|
---|
118 | || JSStringIsEqualToUTF8CString(propertyName, "myPropertyName")
|
---|
119 | || JSStringIsEqualToUTF8CString(propertyName, "hasPropertyLie")
|
---|
120 | || JSStringIsEqualToUTF8CString(propertyName, "0")) {
|
---|
121 | return true;
|
---|
122 | }
|
---|
123 |
|
---|
124 | return false;
|
---|
125 | }
|
---|
126 |
|
---|
127 | static JSValueRef MyObject_getProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
|
---|
128 | {
|
---|
129 | UNUSED_PARAM(context);
|
---|
130 | UNUSED_PARAM(object);
|
---|
131 |
|
---|
132 | if (JSStringIsEqualToUTF8CString(propertyName, "alwaysOne")) {
|
---|
133 | return JSValueMakeNumber(context, 1);
|
---|
134 | }
|
---|
135 |
|
---|
136 | if (JSStringIsEqualToUTF8CString(propertyName, "myPropertyName")) {
|
---|
137 | return JSValueMakeNumber(context, 1);
|
---|
138 | }
|
---|
139 |
|
---|
140 | if (JSStringIsEqualToUTF8CString(propertyName, "cantFind")) {
|
---|
141 | return JSValueMakeUndefined(context);
|
---|
142 | }
|
---|
143 |
|
---|
144 | if (JSStringIsEqualToUTF8CString(propertyName, "0")) {
|
---|
145 | *exception = JSValueMakeNumber(context, 1);
|
---|
146 | return JSValueMakeNumber(context, 1);
|
---|
147 | }
|
---|
148 |
|
---|
149 | return NULL;
|
---|
150 | }
|
---|
151 |
|
---|
152 | static bool MyObject_setProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef value, JSValueRef* exception)
|
---|
153 | {
|
---|
154 | UNUSED_PARAM(context);
|
---|
155 | UNUSED_PARAM(object);
|
---|
156 | UNUSED_PARAM(value);
|
---|
157 | UNUSED_PARAM(exception);
|
---|
158 |
|
---|
159 | if (JSStringIsEqualToUTF8CString(propertyName, "cantSet"))
|
---|
160 | return true; // pretend we set the property in order to swallow it
|
---|
161 |
|
---|
162 | return false;
|
---|
163 | }
|
---|
164 |
|
---|
165 | static bool MyObject_deleteProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
|
---|
166 | {
|
---|
167 | UNUSED_PARAM(context);
|
---|
168 | UNUSED_PARAM(object);
|
---|
169 |
|
---|
170 | if (JSStringIsEqualToUTF8CString(propertyName, "cantDelete"))
|
---|
171 | return true;
|
---|
172 |
|
---|
173 | if (JSStringIsEqualToUTF8CString(propertyName, "throwOnDelete")) {
|
---|
174 | *exception = JSValueMakeNumber(context, 2);
|
---|
175 | return false;
|
---|
176 | }
|
---|
177 |
|
---|
178 | return false;
|
---|
179 | }
|
---|
180 |
|
---|
181 | static void MyObject_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef propertyNames)
|
---|
182 | {
|
---|
183 | UNUSED_PARAM(context);
|
---|
184 | UNUSED_PARAM(object);
|
---|
185 |
|
---|
186 | JSStringRef propertyName;
|
---|
187 |
|
---|
188 | propertyName = JSStringCreateWithUTF8CString("alwaysOne");
|
---|
189 | JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
|
---|
190 | JSStringRelease(propertyName);
|
---|
191 |
|
---|
192 | propertyName = JSStringCreateWithUTF8CString("myPropertyName");
|
---|
193 | JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
|
---|
194 | JSStringRelease(propertyName);
|
---|
195 | }
|
---|
196 |
|
---|
197 | static JSValueRef MyObject_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
|
---|
198 | {
|
---|
199 | UNUSED_PARAM(context);
|
---|
200 | UNUSED_PARAM(object);
|
---|
201 | UNUSED_PARAM(thisObject);
|
---|
202 | UNUSED_PARAM(exception);
|
---|
203 |
|
---|
204 | if (argumentCount > 0 && JSValueIsStrictEqual(context, arguments[0], JSValueMakeNumber(context, 0)))
|
---|
205 | return JSValueMakeNumber(context, 1);
|
---|
206 |
|
---|
207 | return JSValueMakeUndefined(context);
|
---|
208 | }
|
---|
209 |
|
---|
210 | static JSObjectRef MyObject_callAsConstructor(JSContextRef context, JSObjectRef object, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
|
---|
211 | {
|
---|
212 | UNUSED_PARAM(context);
|
---|
213 | UNUSED_PARAM(object);
|
---|
214 |
|
---|
215 | if (argumentCount > 0 && JSValueIsStrictEqual(context, arguments[0], JSValueMakeNumber(context, 0)))
|
---|
216 | return JSValueToObject(context, JSValueMakeNumber(context, 1), exception);
|
---|
217 |
|
---|
218 | return JSValueToObject(context, JSValueMakeNumber(context, 0), exception);
|
---|
219 | }
|
---|
220 |
|
---|
221 | static bool MyObject_hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef possibleValue, JSValueRef* exception)
|
---|
222 | {
|
---|
223 | UNUSED_PARAM(context);
|
---|
224 | UNUSED_PARAM(constructor);
|
---|
225 |
|
---|
226 | JSStringRef numberString = JSStringCreateWithUTF8CString("Number");
|
---|
227 | JSObjectRef numberConstructor = JSValueToObject(context, JSObjectGetProperty(context, JSContextGetGlobalObject(context), numberString, exception), exception);
|
---|
228 | JSStringRelease(numberString);
|
---|
229 |
|
---|
230 | return JSValueIsInstanceOfConstructor(context, possibleValue, numberConstructor, exception);
|
---|
231 | }
|
---|
232 |
|
---|
233 | static JSValueRef MyObject_convertToType(JSContextRef context, JSObjectRef object, JSType type, JSValueRef* exception)
|
---|
234 | {
|
---|
235 | UNUSED_PARAM(object);
|
---|
236 | UNUSED_PARAM(exception);
|
---|
237 |
|
---|
238 | switch (type) {
|
---|
239 | case kJSTypeNumber:
|
---|
240 | return JSValueMakeNumber(context, 1);
|
---|
241 | default:
|
---|
242 | break;
|
---|
243 | }
|
---|
244 |
|
---|
245 | // string conversion -- forward to default object class
|
---|
246 | return NULL;
|
---|
247 | }
|
---|
248 |
|
---|
249 | static JSStaticValue evilStaticValues[] = {
|
---|
250 | { "nullGetSet", 0, 0, kJSPropertyAttributeNone },
|
---|
251 | { 0, 0, 0, 0 }
|
---|
252 | };
|
---|
253 |
|
---|
254 | static JSStaticFunction evilStaticFunctions[] = {
|
---|
255 | { "nullCall", 0, kJSPropertyAttributeNone },
|
---|
256 | { 0, 0, 0 }
|
---|
257 | };
|
---|
258 |
|
---|
259 | JSClassDefinition MyObject_definition = {
|
---|
260 | 0,
|
---|
261 | kJSClassAttributeNone,
|
---|
262 |
|
---|
263 | "MyObject",
|
---|
264 | NULL,
|
---|
265 |
|
---|
266 | evilStaticValues,
|
---|
267 | evilStaticFunctions,
|
---|
268 |
|
---|
269 | NULL,
|
---|
270 | NULL,
|
---|
271 | MyObject_hasProperty,
|
---|
272 | MyObject_getProperty,
|
---|
273 | MyObject_setProperty,
|
---|
274 | MyObject_deleteProperty,
|
---|
275 | MyObject_getPropertyNames,
|
---|
276 | MyObject_callAsFunction,
|
---|
277 | MyObject_callAsConstructor,
|
---|
278 | MyObject_hasInstance,
|
---|
279 | MyObject_convertToType,
|
---|
280 | };
|
---|
281 |
|
---|
282 | static JSClassRef MyObject_class(JSContextRef context)
|
---|
283 | {
|
---|
284 | UNUSED_PARAM(context);
|
---|
285 |
|
---|
286 | static JSClassRef jsClass;
|
---|
287 | if (!jsClass)
|
---|
288 | jsClass = JSClassCreate(&MyObject_definition);
|
---|
289 |
|
---|
290 | return jsClass;
|
---|
291 | }
|
---|
292 |
|
---|
293 | static JSValueRef Base_get(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
|
---|
294 | {
|
---|
295 | UNUSED_PARAM(object);
|
---|
296 | UNUSED_PARAM(propertyName);
|
---|
297 | UNUSED_PARAM(exception);
|
---|
298 |
|
---|
299 | return JSValueMakeNumber(ctx, 1); // distinguish base get form derived get
|
---|
300 | }
|
---|
301 |
|
---|
302 | static bool Base_set(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef value, JSValueRef* exception)
|
---|
303 | {
|
---|
304 | UNUSED_PARAM(object);
|
---|
305 | UNUSED_PARAM(propertyName);
|
---|
306 | UNUSED_PARAM(value);
|
---|
307 |
|
---|
308 | *exception = JSValueMakeNumber(ctx, 1); // distinguish base set from derived set
|
---|
309 | return true;
|
---|
310 | }
|
---|
311 |
|
---|
312 | static JSValueRef Base_callAsFunction(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
|
---|
313 | {
|
---|
314 | UNUSED_PARAM(function);
|
---|
315 | UNUSED_PARAM(thisObject);
|
---|
316 | UNUSED_PARAM(argumentCount);
|
---|
317 | UNUSED_PARAM(arguments);
|
---|
318 | UNUSED_PARAM(exception);
|
---|
319 |
|
---|
320 | return JSValueMakeNumber(ctx, 1); // distinguish base call from derived call
|
---|
321 | }
|
---|
322 |
|
---|
323 | static JSStaticFunction Base_staticFunctions[] = {
|
---|
324 | { "baseProtoDup", NULL, kJSPropertyAttributeNone },
|
---|
325 | { "baseProto", Base_callAsFunction, kJSPropertyAttributeNone },
|
---|
326 | { 0, 0, 0 }
|
---|
327 | };
|
---|
328 |
|
---|
329 | static JSStaticValue Base_staticValues[] = {
|
---|
330 | { "baseDup", Base_get, Base_set, kJSPropertyAttributeNone },
|
---|
331 | { "baseOnly", Base_get, Base_set, kJSPropertyAttributeNone },
|
---|
332 | { 0, 0, 0, 0 }
|
---|
333 | };
|
---|
334 |
|
---|
335 | static bool TestInitializeFinalize;
|
---|
336 | static void Base_initialize(JSContextRef context, JSObjectRef object)
|
---|
337 | {
|
---|
338 | UNUSED_PARAM(context);
|
---|
339 |
|
---|
340 | if (TestInitializeFinalize) {
|
---|
341 | ASSERT((void*)1 == JSObjectGetPrivate(object));
|
---|
342 | JSObjectSetPrivate(object, (void*)2);
|
---|
343 | }
|
---|
344 | }
|
---|
345 |
|
---|
346 | static unsigned Base_didFinalize;
|
---|
347 | static void Base_finalize(JSObjectRef object)
|
---|
348 | {
|
---|
349 | UNUSED_PARAM(object);
|
---|
350 | if (TestInitializeFinalize) {
|
---|
351 | ASSERT((void*)4 == JSObjectGetPrivate(object));
|
---|
352 | Base_didFinalize = true;
|
---|
353 | }
|
---|
354 | }
|
---|
355 |
|
---|
356 | static JSClassRef Base_class(JSContextRef context)
|
---|
357 | {
|
---|
358 | UNUSED_PARAM(context);
|
---|
359 |
|
---|
360 | static JSClassRef jsClass;
|
---|
361 | if (!jsClass) {
|
---|
362 | JSClassDefinition definition = kJSClassDefinitionEmpty;
|
---|
363 | definition.staticValues = Base_staticValues;
|
---|
364 | definition.staticFunctions = Base_staticFunctions;
|
---|
365 | definition.initialize = Base_initialize;
|
---|
366 | definition.finalize = Base_finalize;
|
---|
367 | jsClass = JSClassCreate(&definition);
|
---|
368 | }
|
---|
369 | return jsClass;
|
---|
370 | }
|
---|
371 |
|
---|
372 | static JSValueRef Derived_get(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
|
---|
373 | {
|
---|
374 | UNUSED_PARAM(object);
|
---|
375 | UNUSED_PARAM(propertyName);
|
---|
376 | UNUSED_PARAM(exception);
|
---|
377 |
|
---|
378 | return JSValueMakeNumber(ctx, 2); // distinguish base get form derived get
|
---|
379 | }
|
---|
380 |
|
---|
381 | static bool Derived_set(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef value, JSValueRef* exception)
|
---|
382 | {
|
---|
383 | UNUSED_PARAM(ctx);
|
---|
384 | UNUSED_PARAM(object);
|
---|
385 | UNUSED_PARAM(propertyName);
|
---|
386 | UNUSED_PARAM(value);
|
---|
387 |
|
---|
388 | *exception = JSValueMakeNumber(ctx, 2); // distinguish base set from derived set
|
---|
389 | return true;
|
---|
390 | }
|
---|
391 |
|
---|
392 | static JSValueRef Derived_callAsFunction(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
|
---|
393 | {
|
---|
394 | UNUSED_PARAM(function);
|
---|
395 | UNUSED_PARAM(thisObject);
|
---|
396 | UNUSED_PARAM(argumentCount);
|
---|
397 | UNUSED_PARAM(arguments);
|
---|
398 | UNUSED_PARAM(exception);
|
---|
399 |
|
---|
400 | return JSValueMakeNumber(ctx, 2); // distinguish base call from derived call
|
---|
401 | }
|
---|
402 |
|
---|
403 | static JSStaticFunction Derived_staticFunctions[] = {
|
---|
404 | { "protoOnly", Derived_callAsFunction, kJSPropertyAttributeNone },
|
---|
405 | { "protoDup", NULL, kJSPropertyAttributeNone },
|
---|
406 | { "baseProtoDup", Derived_callAsFunction, kJSPropertyAttributeNone },
|
---|
407 | { 0, 0, 0 }
|
---|
408 | };
|
---|
409 |
|
---|
410 | static JSStaticValue Derived_staticValues[] = {
|
---|
411 | { "derivedOnly", Derived_get, Derived_set, kJSPropertyAttributeNone },
|
---|
412 | { "protoDup", Derived_get, Derived_set, kJSPropertyAttributeNone },
|
---|
413 | { "baseDup", Derived_get, Derived_set, kJSPropertyAttributeNone },
|
---|
414 | { 0, 0, 0, 0 }
|
---|
415 | };
|
---|
416 |
|
---|
417 | static void Derived_initialize(JSContextRef context, JSObjectRef object)
|
---|
418 | {
|
---|
419 | UNUSED_PARAM(context);
|
---|
420 |
|
---|
421 | if (TestInitializeFinalize) {
|
---|
422 | ASSERT((void*)2 == JSObjectGetPrivate(object));
|
---|
423 | JSObjectSetPrivate(object, (void*)3);
|
---|
424 | }
|
---|
425 | }
|
---|
426 |
|
---|
427 | static void Derived_finalize(JSObjectRef object)
|
---|
428 | {
|
---|
429 | if (TestInitializeFinalize) {
|
---|
430 | ASSERT((void*)3 == JSObjectGetPrivate(object));
|
---|
431 | JSObjectSetPrivate(object, (void*)4);
|
---|
432 | }
|
---|
433 | }
|
---|
434 |
|
---|
435 | static JSClassRef Derived_class(JSContextRef context)
|
---|
436 | {
|
---|
437 | static JSClassRef jsClass;
|
---|
438 | if (!jsClass) {
|
---|
439 | JSClassDefinition definition = kJSClassDefinitionEmpty;
|
---|
440 | definition.parentClass = Base_class(context);
|
---|
441 | definition.staticValues = Derived_staticValues;
|
---|
442 | definition.staticFunctions = Derived_staticFunctions;
|
---|
443 | definition.initialize = Derived_initialize;
|
---|
444 | definition.finalize = Derived_finalize;
|
---|
445 | jsClass = JSClassCreate(&definition);
|
---|
446 | }
|
---|
447 | return jsClass;
|
---|
448 | }
|
---|
449 |
|
---|
450 | static JSValueRef print_callAsFunction(JSContextRef context, JSObjectRef functionObject, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
|
---|
451 | {
|
---|
452 | UNUSED_PARAM(functionObject);
|
---|
453 | UNUSED_PARAM(thisObject);
|
---|
454 | UNUSED_PARAM(exception);
|
---|
455 |
|
---|
456 | if (argumentCount > 0) {
|
---|
457 | JSStringRef string = JSValueToStringCopy(context, arguments[0], NULL);
|
---|
458 | size_t sizeUTF8 = JSStringGetMaximumUTF8CStringSize(string);
|
---|
459 | char* stringUTF8 = (char*)malloc(sizeUTF8);
|
---|
460 | JSStringGetUTF8CString(string, stringUTF8, sizeUTF8);
|
---|
461 | printf("%s\n", stringUTF8);
|
---|
462 | free(stringUTF8);
|
---|
463 | JSStringRelease(string);
|
---|
464 | }
|
---|
465 |
|
---|
466 | return JSValueMakeUndefined(context);
|
---|
467 | }
|
---|
468 |
|
---|
469 | static JSObjectRef myConstructor_callAsConstructor(JSContextRef context, JSObjectRef constructorObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
|
---|
470 | {
|
---|
471 | UNUSED_PARAM(constructorObject);
|
---|
472 | UNUSED_PARAM(exception);
|
---|
473 |
|
---|
474 | JSObjectRef result = JSObjectMake(context, NULL, NULL);
|
---|
475 | if (argumentCount > 0) {
|
---|
476 | JSStringRef value = JSStringCreateWithUTF8CString("value");
|
---|
477 | JSObjectSetProperty(context, result, value, arguments[0], kJSPropertyAttributeNone, NULL);
|
---|
478 | JSStringRelease(value);
|
---|
479 | }
|
---|
480 |
|
---|
481 | return result;
|
---|
482 | }
|
---|
483 |
|
---|
484 |
|
---|
485 | static void globalObject_initialize(JSContextRef context, JSObjectRef object)
|
---|
486 | {
|
---|
487 | UNUSED_PARAM(object);
|
---|
488 | // Ensure that an execution context is passed in
|
---|
489 | ASSERT(context);
|
---|
490 |
|
---|
491 | // Ensure that the global object is set to the object that we were passed
|
---|
492 | JSObjectRef globalObject = JSContextGetGlobalObject(context);
|
---|
493 | ASSERT(globalObject);
|
---|
494 | ASSERT(object == globalObject);
|
---|
495 |
|
---|
496 | // Ensure that the standard global properties have been set on the global object
|
---|
497 | JSStringRef array = JSStringCreateWithUTF8CString("Array");
|
---|
498 | JSObjectRef arrayConstructor = JSValueToObject(context, JSObjectGetProperty(context, globalObject, array, NULL), NULL);
|
---|
499 | JSStringRelease(array);
|
---|
500 |
|
---|
501 | UNUSED_PARAM(arrayConstructor);
|
---|
502 | ASSERT(arrayConstructor);
|
---|
503 | }
|
---|
504 |
|
---|
505 | static JSValueRef globalObject_get(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
|
---|
506 | {
|
---|
507 | UNUSED_PARAM(object);
|
---|
508 | UNUSED_PARAM(propertyName);
|
---|
509 | UNUSED_PARAM(exception);
|
---|
510 |
|
---|
511 | return JSValueMakeNumber(ctx, 3);
|
---|
512 | }
|
---|
513 |
|
---|
514 | static bool globalObject_set(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef value, JSValueRef* exception)
|
---|
515 | {
|
---|
516 | UNUSED_PARAM(object);
|
---|
517 | UNUSED_PARAM(propertyName);
|
---|
518 | UNUSED_PARAM(value);
|
---|
519 |
|
---|
520 | *exception = JSValueMakeNumber(ctx, 3);
|
---|
521 | return true;
|
---|
522 | }
|
---|
523 |
|
---|
524 | static JSValueRef globalObject_call(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
|
---|
525 | {
|
---|
526 | UNUSED_PARAM(function);
|
---|
527 | UNUSED_PARAM(thisObject);
|
---|
528 | UNUSED_PARAM(argumentCount);
|
---|
529 | UNUSED_PARAM(arguments);
|
---|
530 | UNUSED_PARAM(exception);
|
---|
531 |
|
---|
532 | return JSValueMakeNumber(ctx, 3);
|
---|
533 | }
|
---|
534 |
|
---|
535 | static JSStaticValue globalObject_staticValues[] = {
|
---|
536 | { "globalStaticValue", globalObject_get, globalObject_set, kJSPropertyAttributeNone },
|
---|
537 | { 0, 0, 0, 0 }
|
---|
538 | };
|
---|
539 |
|
---|
540 | static JSStaticFunction globalObject_staticFunctions[] = {
|
---|
541 | { "globalStaticFunction", globalObject_call, kJSPropertyAttributeNone },
|
---|
542 | { 0, 0, 0 }
|
---|
543 | };
|
---|
544 |
|
---|
545 | static char* createStringWithContentsOfFile(const char* fileName);
|
---|
546 |
|
---|
547 | static void testInitializeFinalize()
|
---|
548 | {
|
---|
549 | JSObjectRef o = JSObjectMake(context, Derived_class(context), (void*)1);
|
---|
550 | UNUSED_PARAM(o);
|
---|
551 | ASSERT(JSObjectGetPrivate(o) == (void*)3);
|
---|
552 | }
|
---|
553 |
|
---|
554 | int main(int argc, char* argv[])
|
---|
555 | {
|
---|
556 | const char *scriptPath = "testapi.js";
|
---|
557 | if (argc > 1) {
|
---|
558 | scriptPath = argv[1];
|
---|
559 | }
|
---|
560 |
|
---|
561 | // Test garbage collection with a fresh context
|
---|
562 | context = JSGlobalContextCreate(NULL);
|
---|
563 | TestInitializeFinalize = true;
|
---|
564 | testInitializeFinalize();
|
---|
565 | JSGlobalContextRelease(context);
|
---|
566 | JSGarbageCollect(context);
|
---|
567 | TestInitializeFinalize = false;
|
---|
568 |
|
---|
569 | ASSERT(Base_didFinalize);
|
---|
570 |
|
---|
571 | JSClassDefinition globalObjectClassDefinition = kJSClassDefinitionEmpty;
|
---|
572 | globalObjectClassDefinition.initialize = globalObject_initialize;
|
---|
573 | globalObjectClassDefinition.staticValues = globalObject_staticValues;
|
---|
574 | globalObjectClassDefinition.staticFunctions = globalObject_staticFunctions;
|
---|
575 | globalObjectClassDefinition.attributes = kJSClassAttributeNoAutomaticPrototype;
|
---|
576 | JSClassRef globalObjectClass = JSClassCreate(&globalObjectClassDefinition);
|
---|
577 | context = JSGlobalContextCreate(globalObjectClass);
|
---|
578 |
|
---|
579 | JSObjectRef globalObject = JSContextGetGlobalObject(context);
|
---|
580 | ASSERT(JSValueIsObject(context, globalObject));
|
---|
581 |
|
---|
582 | JSValueRef jsUndefined = JSValueMakeUndefined(context);
|
---|
583 | JSValueRef jsNull = JSValueMakeNull(context);
|
---|
584 | JSValueRef jsTrue = JSValueMakeBoolean(context, true);
|
---|
585 | JSValueRef jsFalse = JSValueMakeBoolean(context, false);
|
---|
586 | JSValueRef jsZero = JSValueMakeNumber(context, 0);
|
---|
587 | JSValueRef jsOne = JSValueMakeNumber(context, 1);
|
---|
588 | JSValueRef jsOneThird = JSValueMakeNumber(context, 1.0 / 3.0);
|
---|
589 | JSObjectRef jsObjectNoProto = JSObjectMake(context, NULL, NULL);
|
---|
590 | JSObjectSetPrototype(context, jsObjectNoProto, JSValueMakeNull(context));
|
---|
591 |
|
---|
592 | // FIXME: test funny utf8 characters
|
---|
593 | JSStringRef jsEmptyIString = JSStringCreateWithUTF8CString("");
|
---|
594 | JSValueRef jsEmptyString = JSValueMakeString(context, jsEmptyIString);
|
---|
595 |
|
---|
596 | JSStringRef jsOneIString = JSStringCreateWithUTF8CString("1");
|
---|
597 | JSValueRef jsOneString = JSValueMakeString(context, jsOneIString);
|
---|
598 |
|
---|
599 | UniChar singleUniChar = 65; // Capital A
|
---|
600 | CFMutableStringRef cfString =
|
---|
601 | CFStringCreateMutableWithExternalCharactersNoCopy(kCFAllocatorDefault,
|
---|
602 | &singleUniChar,
|
---|
603 | 1,
|
---|
604 | 1,
|
---|
605 | kCFAllocatorNull);
|
---|
606 |
|
---|
607 | JSStringRef jsCFIString = JSStringCreateWithCFString(cfString);
|
---|
608 | JSValueRef jsCFString = JSValueMakeString(context, jsCFIString);
|
---|
609 |
|
---|
610 | CFStringRef cfEmptyString = CFStringCreateWithCString(kCFAllocatorDefault, "", kCFStringEncodingUTF8);
|
---|
611 |
|
---|
612 | JSStringRef jsCFEmptyIString = JSStringCreateWithCFString(cfEmptyString);
|
---|
613 | JSValueRef jsCFEmptyString = JSValueMakeString(context, jsCFEmptyIString);
|
---|
614 |
|
---|
615 | CFIndex cfStringLength = CFStringGetLength(cfString);
|
---|
616 | UniChar* buffer = (UniChar*)malloc(cfStringLength * sizeof(UniChar));
|
---|
617 | CFStringGetCharacters(cfString,
|
---|
618 | CFRangeMake(0, cfStringLength),
|
---|
619 | buffer);
|
---|
620 | JSStringRef jsCFIStringWithCharacters = JSStringCreateWithCharacters((JSChar*)buffer, cfStringLength);
|
---|
621 | JSValueRef jsCFStringWithCharacters = JSValueMakeString(context, jsCFIStringWithCharacters);
|
---|
622 |
|
---|
623 | JSStringRef jsCFEmptyIStringWithCharacters = JSStringCreateWithCharacters((JSChar*)buffer, CFStringGetLength(cfEmptyString));
|
---|
624 | free(buffer);
|
---|
625 | JSValueRef jsCFEmptyStringWithCharacters = JSValueMakeString(context, jsCFEmptyIStringWithCharacters);
|
---|
626 |
|
---|
627 | ASSERT(JSValueGetType(context, jsUndefined) == kJSTypeUndefined);
|
---|
628 | ASSERT(JSValueGetType(context, jsNull) == kJSTypeNull);
|
---|
629 | ASSERT(JSValueGetType(context, jsTrue) == kJSTypeBoolean);
|
---|
630 | ASSERT(JSValueGetType(context, jsFalse) == kJSTypeBoolean);
|
---|
631 | ASSERT(JSValueGetType(context, jsZero) == kJSTypeNumber);
|
---|
632 | ASSERT(JSValueGetType(context, jsOne) == kJSTypeNumber);
|
---|
633 | ASSERT(JSValueGetType(context, jsOneThird) == kJSTypeNumber);
|
---|
634 | ASSERT(JSValueGetType(context, jsEmptyString) == kJSTypeString);
|
---|
635 | ASSERT(JSValueGetType(context, jsOneString) == kJSTypeString);
|
---|
636 | ASSERT(JSValueGetType(context, jsCFString) == kJSTypeString);
|
---|
637 | ASSERT(JSValueGetType(context, jsCFStringWithCharacters) == kJSTypeString);
|
---|
638 | ASSERT(JSValueGetType(context, jsCFEmptyString) == kJSTypeString);
|
---|
639 | ASSERT(JSValueGetType(context, jsCFEmptyStringWithCharacters) == kJSTypeString);
|
---|
640 |
|
---|
641 | JSObjectRef myObject = JSObjectMake(context, MyObject_class(context), NULL);
|
---|
642 | JSStringRef myObjectIString = JSStringCreateWithUTF8CString("MyObject");
|
---|
643 | JSObjectSetProperty(context, globalObject, myObjectIString, myObject, kJSPropertyAttributeNone, NULL);
|
---|
644 | JSStringRelease(myObjectIString);
|
---|
645 |
|
---|
646 | JSValueRef exception;
|
---|
647 |
|
---|
648 | // Conversions that throw exceptions
|
---|
649 | exception = NULL;
|
---|
650 | ASSERT(NULL == JSValueToObject(context, jsNull, &exception));
|
---|
651 | ASSERT(exception);
|
---|
652 |
|
---|
653 | exception = NULL;
|
---|
654 | // FIXME <rdar://4668451> - On i386 the isnan(double) macro tries to map to the isnan(float) function,
|
---|
655 | // causing a build break with -Wshorten-64-to-32 enabled. The issue is known by the appropriate team.
|
---|
656 | // After that's resolved, we can remove these casts
|
---|
657 | ASSERT(isnan((float)JSValueToNumber(context, jsObjectNoProto, &exception)));
|
---|
658 | ASSERT(exception);
|
---|
659 |
|
---|
660 | exception = NULL;
|
---|
661 | ASSERT(!JSValueToStringCopy(context, jsObjectNoProto, &exception));
|
---|
662 | ASSERT(exception);
|
---|
663 |
|
---|
664 | ASSERT(JSValueToBoolean(context, myObject));
|
---|
665 |
|
---|
666 | exception = NULL;
|
---|
667 | ASSERT(!JSValueIsEqual(context, jsObjectNoProto, JSValueMakeNumber(context, 1), &exception));
|
---|
668 | ASSERT(exception);
|
---|
669 |
|
---|
670 | exception = NULL;
|
---|
671 | JSObjectGetPropertyAtIndex(context, myObject, 0, &exception);
|
---|
672 | ASSERT(1 == JSValueToNumber(context, exception, NULL));
|
---|
673 |
|
---|
674 | assertEqualsAsBoolean(jsUndefined, false);
|
---|
675 | assertEqualsAsBoolean(jsNull, false);
|
---|
676 | assertEqualsAsBoolean(jsTrue, true);
|
---|
677 | assertEqualsAsBoolean(jsFalse, false);
|
---|
678 | assertEqualsAsBoolean(jsZero, false);
|
---|
679 | assertEqualsAsBoolean(jsOne, true);
|
---|
680 | assertEqualsAsBoolean(jsOneThird, true);
|
---|
681 | assertEqualsAsBoolean(jsEmptyString, false);
|
---|
682 | assertEqualsAsBoolean(jsOneString, true);
|
---|
683 | assertEqualsAsBoolean(jsCFString, true);
|
---|
684 | assertEqualsAsBoolean(jsCFStringWithCharacters, true);
|
---|
685 | assertEqualsAsBoolean(jsCFEmptyString, false);
|
---|
686 | assertEqualsAsBoolean(jsCFEmptyStringWithCharacters, false);
|
---|
687 |
|
---|
688 | assertEqualsAsNumber(jsUndefined, nan(""));
|
---|
689 | assertEqualsAsNumber(jsNull, 0);
|
---|
690 | assertEqualsAsNumber(jsTrue, 1);
|
---|
691 | assertEqualsAsNumber(jsFalse, 0);
|
---|
692 | assertEqualsAsNumber(jsZero, 0);
|
---|
693 | assertEqualsAsNumber(jsOne, 1);
|
---|
694 | assertEqualsAsNumber(jsOneThird, 1.0 / 3.0);
|
---|
695 | assertEqualsAsNumber(jsEmptyString, 0);
|
---|
696 | assertEqualsAsNumber(jsOneString, 1);
|
---|
697 | assertEqualsAsNumber(jsCFString, nan(""));
|
---|
698 | assertEqualsAsNumber(jsCFStringWithCharacters, nan(""));
|
---|
699 | assertEqualsAsNumber(jsCFEmptyString, 0);
|
---|
700 | assertEqualsAsNumber(jsCFEmptyStringWithCharacters, 0);
|
---|
701 | ASSERT(sizeof(JSChar) == sizeof(UniChar));
|
---|
702 |
|
---|
703 | assertEqualsAsCharactersPtr(jsUndefined, "undefined");
|
---|
704 | assertEqualsAsCharactersPtr(jsNull, "null");
|
---|
705 | assertEqualsAsCharactersPtr(jsTrue, "true");
|
---|
706 | assertEqualsAsCharactersPtr(jsFalse, "false");
|
---|
707 | assertEqualsAsCharactersPtr(jsZero, "0");
|
---|
708 | assertEqualsAsCharactersPtr(jsOne, "1");
|
---|
709 | assertEqualsAsCharactersPtr(jsOneThird, "0.3333333333333333");
|
---|
710 | assertEqualsAsCharactersPtr(jsEmptyString, "");
|
---|
711 | assertEqualsAsCharactersPtr(jsOneString, "1");
|
---|
712 | assertEqualsAsCharactersPtr(jsCFString, "A");
|
---|
713 | assertEqualsAsCharactersPtr(jsCFStringWithCharacters, "A");
|
---|
714 | assertEqualsAsCharactersPtr(jsCFEmptyString, "");
|
---|
715 | assertEqualsAsCharactersPtr(jsCFEmptyStringWithCharacters, "");
|
---|
716 |
|
---|
717 | assertEqualsAsUTF8String(jsUndefined, "undefined");
|
---|
718 | assertEqualsAsUTF8String(jsNull, "null");
|
---|
719 | assertEqualsAsUTF8String(jsTrue, "true");
|
---|
720 | assertEqualsAsUTF8String(jsFalse, "false");
|
---|
721 | assertEqualsAsUTF8String(jsZero, "0");
|
---|
722 | assertEqualsAsUTF8String(jsOne, "1");
|
---|
723 | assertEqualsAsUTF8String(jsOneThird, "0.3333333333333333");
|
---|
724 | assertEqualsAsUTF8String(jsEmptyString, "");
|
---|
725 | assertEqualsAsUTF8String(jsOneString, "1");
|
---|
726 | assertEqualsAsUTF8String(jsCFString, "A");
|
---|
727 | assertEqualsAsUTF8String(jsCFStringWithCharacters, "A");
|
---|
728 | assertEqualsAsUTF8String(jsCFEmptyString, "");
|
---|
729 | assertEqualsAsUTF8String(jsCFEmptyStringWithCharacters, "");
|
---|
730 |
|
---|
731 | ASSERT(JSValueIsStrictEqual(context, jsTrue, jsTrue));
|
---|
732 | ASSERT(!JSValueIsStrictEqual(context, jsOne, jsOneString));
|
---|
733 |
|
---|
734 | ASSERT(JSValueIsEqual(context, jsOne, jsOneString, NULL));
|
---|
735 | ASSERT(!JSValueIsEqual(context, jsTrue, jsFalse, NULL));
|
---|
736 |
|
---|
737 | CFStringRef cfJSString = JSStringCopyCFString(kCFAllocatorDefault, jsCFIString);
|
---|
738 | CFStringRef cfJSEmptyString = JSStringCopyCFString(kCFAllocatorDefault, jsCFEmptyIString);
|
---|
739 | ASSERT(CFEqual(cfJSString, cfString));
|
---|
740 | ASSERT(CFEqual(cfJSEmptyString, cfEmptyString));
|
---|
741 | CFRelease(cfJSString);
|
---|
742 | CFRelease(cfJSEmptyString);
|
---|
743 |
|
---|
744 | CFRelease(cfString);
|
---|
745 | CFRelease(cfEmptyString);
|
---|
746 |
|
---|
747 | jsGlobalValue = JSObjectMake(context, NULL, NULL);
|
---|
748 | JSValueProtect(context, jsGlobalValue);
|
---|
749 | JSGarbageCollect(context);
|
---|
750 | ASSERT(JSValueIsObject(context, jsGlobalValue));
|
---|
751 | JSValueUnprotect(context, jsGlobalValue);
|
---|
752 |
|
---|
753 | JSStringRef goodSyntax = JSStringCreateWithUTF8CString("x = 1;");
|
---|
754 | JSStringRef badSyntax = JSStringCreateWithUTF8CString("x := 1;");
|
---|
755 | ASSERT(JSCheckScriptSyntax(context, goodSyntax, NULL, 0, NULL));
|
---|
756 | ASSERT(!JSCheckScriptSyntax(context, badSyntax, NULL, 0, NULL));
|
---|
757 |
|
---|
758 | JSValueRef result;
|
---|
759 | JSValueRef v;
|
---|
760 | JSObjectRef o;
|
---|
761 | JSStringRef string;
|
---|
762 |
|
---|
763 | result = JSEvaluateScript(context, goodSyntax, NULL, NULL, 1, NULL);
|
---|
764 | ASSERT(result);
|
---|
765 | ASSERT(JSValueIsEqual(context, result, jsOne, NULL));
|
---|
766 |
|
---|
767 | exception = NULL;
|
---|
768 | result = JSEvaluateScript(context, badSyntax, NULL, NULL, 1, &exception);
|
---|
769 | ASSERT(!result);
|
---|
770 | ASSERT(JSValueIsObject(context, exception));
|
---|
771 |
|
---|
772 | JSStringRef array = JSStringCreateWithUTF8CString("Array");
|
---|
773 | JSObjectRef arrayConstructor = JSValueToObject(context, JSObjectGetProperty(context, globalObject, array, NULL), NULL);
|
---|
774 | JSStringRelease(array);
|
---|
775 | result = JSObjectCallAsConstructor(context, arrayConstructor, 0, NULL, NULL);
|
---|
776 | ASSERT(result);
|
---|
777 | ASSERT(JSValueIsObject(context, result));
|
---|
778 | ASSERT(JSValueIsInstanceOfConstructor(context, result, arrayConstructor, NULL));
|
---|
779 | ASSERT(!JSValueIsInstanceOfConstructor(context, JSValueMakeNull(context), arrayConstructor, NULL));
|
---|
780 |
|
---|
781 | o = JSValueToObject(context, result, NULL);
|
---|
782 | exception = NULL;
|
---|
783 | ASSERT(JSValueIsUndefined(context, JSObjectGetPropertyAtIndex(context, o, 0, &exception)));
|
---|
784 | ASSERT(!exception);
|
---|
785 |
|
---|
786 | JSObjectSetPropertyAtIndex(context, o, 0, JSValueMakeNumber(context, 1), &exception);
|
---|
787 | ASSERT(!exception);
|
---|
788 |
|
---|
789 | exception = NULL;
|
---|
790 | ASSERT(1 == JSValueToNumber(context, JSObjectGetPropertyAtIndex(context, o, 0, &exception), &exception));
|
---|
791 | ASSERT(!exception);
|
---|
792 |
|
---|
793 | JSStringRef functionBody;
|
---|
794 | JSObjectRef function;
|
---|
795 |
|
---|
796 | exception = NULL;
|
---|
797 | functionBody = JSStringCreateWithUTF8CString("rreturn Array;");
|
---|
798 | JSStringRef line = JSStringCreateWithUTF8CString("line");
|
---|
799 | ASSERT(!JSObjectMakeFunction(context, NULL, 0, NULL, functionBody, NULL, 1, &exception));
|
---|
800 | ASSERT(JSValueIsObject(context, exception));
|
---|
801 | v = JSObjectGetProperty(context, JSValueToObject(context, exception, NULL), line, NULL);
|
---|
802 | assertEqualsAsNumber(v, 2); // FIXME: Lexer::setCode bumps startingLineNumber by 1 -- we need to change internal callers so that it doesn't have to (saying '0' to mean '1' in the API would be really confusing -- it's really confusing internally, in fact)
|
---|
803 | JSStringRelease(functionBody);
|
---|
804 | JSStringRelease(line);
|
---|
805 |
|
---|
806 | exception = NULL;
|
---|
807 | functionBody = JSStringCreateWithUTF8CString("return Array;");
|
---|
808 | function = JSObjectMakeFunction(context, NULL, 0, NULL, functionBody, NULL, 1, &exception);
|
---|
809 | JSStringRelease(functionBody);
|
---|
810 | ASSERT(!exception);
|
---|
811 | ASSERT(JSObjectIsFunction(context, function));
|
---|
812 | v = JSObjectCallAsFunction(context, function, NULL, 0, NULL, NULL);
|
---|
813 | ASSERT(v);
|
---|
814 | ASSERT(JSValueIsEqual(context, v, arrayConstructor, NULL));
|
---|
815 |
|
---|
816 | exception = NULL;
|
---|
817 | function = JSObjectMakeFunction(context, NULL, 0, NULL, jsEmptyIString, NULL, 0, &exception);
|
---|
818 | ASSERT(!exception);
|
---|
819 | v = JSObjectCallAsFunction(context, function, NULL, 0, NULL, &exception);
|
---|
820 | ASSERT(v && !exception);
|
---|
821 | ASSERT(JSValueIsUndefined(context, v));
|
---|
822 |
|
---|
823 | exception = NULL;
|
---|
824 | v = NULL;
|
---|
825 | JSStringRef foo = JSStringCreateWithUTF8CString("foo");
|
---|
826 | JSStringRef argumentNames[] = { foo };
|
---|
827 | functionBody = JSStringCreateWithUTF8CString("return foo;");
|
---|
828 | function = JSObjectMakeFunction(context, foo, 1, argumentNames, functionBody, NULL, 1, &exception);
|
---|
829 | ASSERT(function && !exception);
|
---|
830 | JSValueRef arguments[] = { JSValueMakeNumber(context, 2) };
|
---|
831 | v = JSObjectCallAsFunction(context, function, NULL, 1, arguments, &exception);
|
---|
832 | JSStringRelease(foo);
|
---|
833 | JSStringRelease(functionBody);
|
---|
834 |
|
---|
835 | string = JSValueToStringCopy(context, function, NULL);
|
---|
836 | assertEqualsAsUTF8String(JSValueMakeString(context, string), "function foo(foo) \n{\n return foo;\n}");
|
---|
837 | JSStringRelease(string);
|
---|
838 |
|
---|
839 | JSStringRef print = JSStringCreateWithUTF8CString("print");
|
---|
840 | JSObjectRef printFunction = JSObjectMakeFunctionWithCallback(context, print, print_callAsFunction);
|
---|
841 | JSObjectSetProperty(context, globalObject, print, printFunction, kJSPropertyAttributeNone, NULL);
|
---|
842 | JSStringRelease(print);
|
---|
843 |
|
---|
844 | ASSERT(!JSObjectSetPrivate(printFunction, (void*)1));
|
---|
845 | ASSERT(!JSObjectGetPrivate(printFunction));
|
---|
846 |
|
---|
847 | JSStringRef myConstructorIString = JSStringCreateWithUTF8CString("MyConstructor");
|
---|
848 | JSObjectRef myConstructor = JSObjectMakeConstructor(context, NULL, myConstructor_callAsConstructor);
|
---|
849 | JSObjectSetProperty(context, globalObject, myConstructorIString, myConstructor, kJSPropertyAttributeNone, NULL);
|
---|
850 | JSStringRelease(myConstructorIString);
|
---|
851 |
|
---|
852 | ASSERT(!JSObjectSetPrivate(myConstructor, (void*)1));
|
---|
853 | ASSERT(!JSObjectGetPrivate(myConstructor));
|
---|
854 |
|
---|
855 | string = JSStringCreateWithUTF8CString("Derived");
|
---|
856 | JSObjectRef derivedConstructor = JSObjectMakeConstructor(context, Derived_class(context), NULL);
|
---|
857 | JSObjectSetProperty(context, globalObject, string, derivedConstructor, kJSPropertyAttributeNone, NULL);
|
---|
858 | JSStringRelease(string);
|
---|
859 |
|
---|
860 | o = JSObjectMake(context, NULL, NULL);
|
---|
861 | JSObjectSetProperty(context, o, jsOneIString, JSValueMakeNumber(context, 1), kJSPropertyAttributeNone, NULL);
|
---|
862 | JSObjectSetProperty(context, o, jsCFIString, JSValueMakeNumber(context, 1), kJSPropertyAttributeDontEnum, NULL);
|
---|
863 | JSPropertyNameArrayRef nameArray = JSObjectCopyPropertyNames(context, o);
|
---|
864 | size_t expectedCount = JSPropertyNameArrayGetCount(nameArray);
|
---|
865 | size_t count;
|
---|
866 | for (count = 0; count < expectedCount; ++count)
|
---|
867 | JSPropertyNameArrayGetNameAtIndex(nameArray, count);
|
---|
868 | JSPropertyNameArrayRelease(nameArray);
|
---|
869 | ASSERT(count == 1); // jsCFString should not be enumerated
|
---|
870 |
|
---|
871 | JSClassDefinition nullDefinition = kJSClassDefinitionEmpty;
|
---|
872 | nullDefinition.attributes = kJSClassAttributeNoAutomaticPrototype;
|
---|
873 | JSClassRef nullClass = JSClassCreate(&nullDefinition);
|
---|
874 | JSClassRelease(nullClass);
|
---|
875 |
|
---|
876 | nullDefinition = kJSClassDefinitionEmpty;
|
---|
877 | nullClass = JSClassCreate(&nullDefinition);
|
---|
878 | JSClassRelease(nullClass);
|
---|
879 |
|
---|
880 | functionBody = JSStringCreateWithUTF8CString("return this;");
|
---|
881 | function = JSObjectMakeFunction(context, NULL, 0, NULL, functionBody, NULL, 1, NULL);
|
---|
882 | JSStringRelease(functionBody);
|
---|
883 | v = JSObjectCallAsFunction(context, function, NULL, 0, NULL, NULL);
|
---|
884 | ASSERT(JSValueIsEqual(context, v, globalObject, NULL));
|
---|
885 | v = JSObjectCallAsFunction(context, function, o, 0, NULL, NULL);
|
---|
886 | ASSERT(JSValueIsEqual(context, v, o, NULL));
|
---|
887 |
|
---|
888 | functionBody = JSStringCreateWithUTF8CString("return eval(\"this\");");
|
---|
889 | function = JSObjectMakeFunction(context, NULL, 0, NULL, functionBody, NULL, 1, NULL);
|
---|
890 | JSStringRelease(functionBody);
|
---|
891 | v = JSObjectCallAsFunction(context, function, NULL, 0, NULL, NULL);
|
---|
892 | ASSERT(JSValueIsEqual(context, v, globalObject, NULL));
|
---|
893 | v = JSObjectCallAsFunction(context, function, o, 0, NULL, NULL);
|
---|
894 | ASSERT(JSValueIsEqual(context, v, o, NULL));
|
---|
895 |
|
---|
896 | JSStringRef script = JSStringCreateWithUTF8CString("this;");
|
---|
897 | v = JSEvaluateScript(context, script, NULL, NULL, 0, NULL);
|
---|
898 | ASSERT(JSValueIsEqual(context, v, globalObject, NULL));
|
---|
899 | v = JSEvaluateScript(context, script, o, NULL, 0, NULL);
|
---|
900 | ASSERT(JSValueIsEqual(context, v, o, NULL));
|
---|
901 | JSStringRelease(script);
|
---|
902 |
|
---|
903 | script = JSStringCreateWithUTF8CString("eval(this);");
|
---|
904 | v = JSEvaluateScript(context, script, NULL, NULL, 0, NULL);
|
---|
905 | ASSERT(JSValueIsEqual(context, v, globalObject, NULL));
|
---|
906 | v = JSEvaluateScript(context, script, o, NULL, 0, NULL);
|
---|
907 | ASSERT(JSValueIsEqual(context, v, o, NULL));
|
---|
908 | JSStringRelease(script);
|
---|
909 |
|
---|
910 | char* scriptUTF8 = createStringWithContentsOfFile(scriptPath);
|
---|
911 | if (!scriptUTF8)
|
---|
912 | printf("FAIL: Test script could not be loaded.\n");
|
---|
913 | else {
|
---|
914 | script = JSStringCreateWithUTF8CString(scriptUTF8);
|
---|
915 | result = JSEvaluateScript(context, script, NULL, NULL, 1, &exception);
|
---|
916 | if (JSValueIsUndefined(context, result))
|
---|
917 | printf("PASS: Test script executed successfully.\n");
|
---|
918 | else {
|
---|
919 | printf("FAIL: Test script returned unexpected value:\n");
|
---|
920 | JSStringRef exceptionIString = JSValueToStringCopy(context, exception, NULL);
|
---|
921 | CFStringRef exceptionCF = JSStringCopyCFString(kCFAllocatorDefault, exceptionIString);
|
---|
922 | CFShow(exceptionCF);
|
---|
923 | CFRelease(exceptionCF);
|
---|
924 | JSStringRelease(exceptionIString);
|
---|
925 | }
|
---|
926 | JSStringRelease(script);
|
---|
927 | free(scriptUTF8);
|
---|
928 | }
|
---|
929 |
|
---|
930 | // Clear out local variables pointing at JSObjectRefs to allow their values to be collected
|
---|
931 | function = NULL;
|
---|
932 | v = NULL;
|
---|
933 | o = NULL;
|
---|
934 | globalObject = NULL;
|
---|
935 |
|
---|
936 | JSStringRelease(jsEmptyIString);
|
---|
937 | JSStringRelease(jsOneIString);
|
---|
938 | JSStringRelease(jsCFIString);
|
---|
939 | JSStringRelease(jsCFEmptyIString);
|
---|
940 | JSStringRelease(jsCFIStringWithCharacters);
|
---|
941 | JSStringRelease(jsCFEmptyIStringWithCharacters);
|
---|
942 | JSStringRelease(goodSyntax);
|
---|
943 | JSStringRelease(badSyntax);
|
---|
944 |
|
---|
945 | JSGlobalContextRelease(context);
|
---|
946 | JSGarbageCollect(context);
|
---|
947 | JSClassRelease(globalObjectClass);
|
---|
948 |
|
---|
949 | printf("PASS: Program exited normally.\n");
|
---|
950 | return 0;
|
---|
951 | }
|
---|
952 |
|
---|
953 | static char* createStringWithContentsOfFile(const char* fileName)
|
---|
954 | {
|
---|
955 | char* buffer;
|
---|
956 |
|
---|
957 | size_t buffer_size = 0;
|
---|
958 | size_t buffer_capacity = 1024;
|
---|
959 | buffer = (char*)malloc(buffer_capacity);
|
---|
960 |
|
---|
961 | FILE* f = fopen(fileName, "r");
|
---|
962 | if (!f) {
|
---|
963 | fprintf(stderr, "Could not open file: %s\n", fileName);
|
---|
964 | return 0;
|
---|
965 | }
|
---|
966 |
|
---|
967 | while (!feof(f) && !ferror(f)) {
|
---|
968 | buffer_size += fread(buffer + buffer_size, 1, buffer_capacity - buffer_size, f);
|
---|
969 | if (buffer_size == buffer_capacity) { // guarantees space for trailing '\0'
|
---|
970 | buffer_capacity *= 2;
|
---|
971 | buffer = (char*)realloc(buffer, buffer_capacity);
|
---|
972 | ASSERT(buffer);
|
---|
973 | }
|
---|
974 |
|
---|
975 | ASSERT(buffer_size < buffer_capacity);
|
---|
976 | }
|
---|
977 | fclose(f);
|
---|
978 | buffer[buffer_size] = '\0';
|
---|
979 |
|
---|
980 | return buffer;
|
---|
981 | }
|
---|