]>
Commit | Line | Data |
---|---|---|
b37bf2e1 A |
1 | /* |
2 | * Copyright (C) 2006 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * Redistribution and use in source and binary forms, with or without | |
5 | * modification, are permitted provided that the following conditions | |
6 | * are met: | |
7 | * 1. Redistributions of source code must retain the above copyright | |
8 | * notice, this list of conditions and the following disclaimer. | |
9 | * 2. Redistributions in binary form must reproduce the above copyright | |
10 | * notice, this list of conditions and the following disclaimer in the | |
11 | * documentation and/or other materials provided with the distribution. | |
12 | * | |
13 | * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY | |
14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR | |
17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | |
21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
24 | */ | |
25 | ||
26 | #include "JavaScriptCore.h" | |
9dae56ea | 27 | #include "JSBasePrivate.h" |
b37bf2e1 A |
28 | #include <math.h> |
29 | #include <wtf/Assertions.h> | |
30 | #include <wtf/UnusedParam.h> | |
31 | ||
9dae56ea A |
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 | ||
b37bf2e1 A |
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); | |
9dae56ea | 67 | char* jsBuffer = (char*)malloc(jsSize); |
b37bf2e1 A |
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 | ||
9dae56ea | 78 | free(jsBuffer); |
b37bf2e1 A |
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); | |
9dae56ea | 93 | UniChar* cfBuffer = (UniChar*)malloc(cfLength * sizeof(UniChar)); |
b37bf2e1 A |
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 | ||
9dae56ea | 103 | free(cfBuffer); |
b37bf2e1 A |
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); | |
9dae56ea A |
241 | case kJSTypeString: |
242 | { | |
243 | JSStringRef string = JSStringCreateWithUTF8CString("MyObjectAsString"); | |
244 | JSValueRef result = JSValueMakeString(context, string); | |
245 | JSStringRelease(string); | |
246 | return result; | |
247 | } | |
b37bf2e1 A |
248 | default: |
249 | break; | |
250 | } | |
251 | ||
252 | // string conversion -- forward to default object class | |
253 | return NULL; | |
254 | } | |
255 | ||
256 | static JSStaticValue evilStaticValues[] = { | |
257 | { "nullGetSet", 0, 0, kJSPropertyAttributeNone }, | |
258 | { 0, 0, 0, 0 } | |
259 | }; | |
260 | ||
261 | static JSStaticFunction evilStaticFunctions[] = { | |
262 | { "nullCall", 0, kJSPropertyAttributeNone }, | |
263 | { 0, 0, 0 } | |
264 | }; | |
265 | ||
266 | JSClassDefinition MyObject_definition = { | |
267 | 0, | |
268 | kJSClassAttributeNone, | |
269 | ||
270 | "MyObject", | |
271 | NULL, | |
272 | ||
273 | evilStaticValues, | |
274 | evilStaticFunctions, | |
275 | ||
276 | NULL, | |
277 | NULL, | |
278 | MyObject_hasProperty, | |
279 | MyObject_getProperty, | |
280 | MyObject_setProperty, | |
281 | MyObject_deleteProperty, | |
282 | MyObject_getPropertyNames, | |
283 | MyObject_callAsFunction, | |
284 | MyObject_callAsConstructor, | |
285 | MyObject_hasInstance, | |
286 | MyObject_convertToType, | |
287 | }; | |
288 | ||
289 | static JSClassRef MyObject_class(JSContextRef context) | |
290 | { | |
291 | UNUSED_PARAM(context); | |
292 | ||
293 | static JSClassRef jsClass; | |
294 | if (!jsClass) | |
295 | jsClass = JSClassCreate(&MyObject_definition); | |
296 | ||
297 | return jsClass; | |
298 | } | |
299 | ||
300 | static JSValueRef Base_get(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception) | |
301 | { | |
302 | UNUSED_PARAM(object); | |
303 | UNUSED_PARAM(propertyName); | |
304 | UNUSED_PARAM(exception); | |
305 | ||
306 | return JSValueMakeNumber(ctx, 1); // distinguish base get form derived get | |
307 | } | |
308 | ||
309 | static bool Base_set(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef value, JSValueRef* exception) | |
310 | { | |
311 | UNUSED_PARAM(object); | |
312 | UNUSED_PARAM(propertyName); | |
313 | UNUSED_PARAM(value); | |
314 | ||
315 | *exception = JSValueMakeNumber(ctx, 1); // distinguish base set from derived set | |
316 | return true; | |
317 | } | |
318 | ||
319 | static JSValueRef Base_callAsFunction(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) | |
320 | { | |
321 | UNUSED_PARAM(function); | |
322 | UNUSED_PARAM(thisObject); | |
323 | UNUSED_PARAM(argumentCount); | |
324 | UNUSED_PARAM(arguments); | |
325 | UNUSED_PARAM(exception); | |
326 | ||
327 | return JSValueMakeNumber(ctx, 1); // distinguish base call from derived call | |
328 | } | |
329 | ||
330 | static JSStaticFunction Base_staticFunctions[] = { | |
331 | { "baseProtoDup", NULL, kJSPropertyAttributeNone }, | |
332 | { "baseProto", Base_callAsFunction, kJSPropertyAttributeNone }, | |
333 | { 0, 0, 0 } | |
334 | }; | |
335 | ||
336 | static JSStaticValue Base_staticValues[] = { | |
337 | { "baseDup", Base_get, Base_set, kJSPropertyAttributeNone }, | |
338 | { "baseOnly", Base_get, Base_set, kJSPropertyAttributeNone }, | |
339 | { 0, 0, 0, 0 } | |
340 | }; | |
341 | ||
342 | static bool TestInitializeFinalize; | |
343 | static void Base_initialize(JSContextRef context, JSObjectRef object) | |
344 | { | |
345 | UNUSED_PARAM(context); | |
346 | ||
347 | if (TestInitializeFinalize) { | |
348 | ASSERT((void*)1 == JSObjectGetPrivate(object)); | |
349 | JSObjectSetPrivate(object, (void*)2); | |
350 | } | |
351 | } | |
352 | ||
353 | static unsigned Base_didFinalize; | |
354 | static void Base_finalize(JSObjectRef object) | |
355 | { | |
356 | UNUSED_PARAM(object); | |
357 | if (TestInitializeFinalize) { | |
358 | ASSERT((void*)4 == JSObjectGetPrivate(object)); | |
359 | Base_didFinalize = true; | |
360 | } | |
361 | } | |
362 | ||
363 | static JSClassRef Base_class(JSContextRef context) | |
364 | { | |
365 | UNUSED_PARAM(context); | |
366 | ||
367 | static JSClassRef jsClass; | |
368 | if (!jsClass) { | |
369 | JSClassDefinition definition = kJSClassDefinitionEmpty; | |
370 | definition.staticValues = Base_staticValues; | |
371 | definition.staticFunctions = Base_staticFunctions; | |
372 | definition.initialize = Base_initialize; | |
373 | definition.finalize = Base_finalize; | |
374 | jsClass = JSClassCreate(&definition); | |
375 | } | |
376 | return jsClass; | |
377 | } | |
378 | ||
379 | static JSValueRef Derived_get(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception) | |
380 | { | |
381 | UNUSED_PARAM(object); | |
382 | UNUSED_PARAM(propertyName); | |
383 | UNUSED_PARAM(exception); | |
384 | ||
385 | return JSValueMakeNumber(ctx, 2); // distinguish base get form derived get | |
386 | } | |
387 | ||
388 | static bool Derived_set(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef value, JSValueRef* exception) | |
389 | { | |
390 | UNUSED_PARAM(ctx); | |
391 | UNUSED_PARAM(object); | |
392 | UNUSED_PARAM(propertyName); | |
393 | UNUSED_PARAM(value); | |
394 | ||
395 | *exception = JSValueMakeNumber(ctx, 2); // distinguish base set from derived set | |
396 | return true; | |
397 | } | |
398 | ||
399 | static JSValueRef Derived_callAsFunction(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) | |
400 | { | |
401 | UNUSED_PARAM(function); | |
402 | UNUSED_PARAM(thisObject); | |
403 | UNUSED_PARAM(argumentCount); | |
404 | UNUSED_PARAM(arguments); | |
405 | UNUSED_PARAM(exception); | |
406 | ||
407 | return JSValueMakeNumber(ctx, 2); // distinguish base call from derived call | |
408 | } | |
409 | ||
410 | static JSStaticFunction Derived_staticFunctions[] = { | |
411 | { "protoOnly", Derived_callAsFunction, kJSPropertyAttributeNone }, | |
412 | { "protoDup", NULL, kJSPropertyAttributeNone }, | |
413 | { "baseProtoDup", Derived_callAsFunction, kJSPropertyAttributeNone }, | |
414 | { 0, 0, 0 } | |
415 | }; | |
416 | ||
417 | static JSStaticValue Derived_staticValues[] = { | |
418 | { "derivedOnly", Derived_get, Derived_set, kJSPropertyAttributeNone }, | |
419 | { "protoDup", Derived_get, Derived_set, kJSPropertyAttributeNone }, | |
420 | { "baseDup", Derived_get, Derived_set, kJSPropertyAttributeNone }, | |
421 | { 0, 0, 0, 0 } | |
422 | }; | |
423 | ||
424 | static void Derived_initialize(JSContextRef context, JSObjectRef object) | |
425 | { | |
426 | UNUSED_PARAM(context); | |
427 | ||
428 | if (TestInitializeFinalize) { | |
429 | ASSERT((void*)2 == JSObjectGetPrivate(object)); | |
430 | JSObjectSetPrivate(object, (void*)3); | |
431 | } | |
432 | } | |
433 | ||
434 | static void Derived_finalize(JSObjectRef object) | |
435 | { | |
436 | if (TestInitializeFinalize) { | |
437 | ASSERT((void*)3 == JSObjectGetPrivate(object)); | |
438 | JSObjectSetPrivate(object, (void*)4); | |
439 | } | |
440 | } | |
441 | ||
442 | static JSClassRef Derived_class(JSContextRef context) | |
443 | { | |
444 | static JSClassRef jsClass; | |
445 | if (!jsClass) { | |
446 | JSClassDefinition definition = kJSClassDefinitionEmpty; | |
447 | definition.parentClass = Base_class(context); | |
448 | definition.staticValues = Derived_staticValues; | |
449 | definition.staticFunctions = Derived_staticFunctions; | |
450 | definition.initialize = Derived_initialize; | |
451 | definition.finalize = Derived_finalize; | |
452 | jsClass = JSClassCreate(&definition); | |
453 | } | |
454 | return jsClass; | |
455 | } | |
456 | ||
457 | static JSValueRef print_callAsFunction(JSContextRef context, JSObjectRef functionObject, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) | |
458 | { | |
459 | UNUSED_PARAM(functionObject); | |
460 | UNUSED_PARAM(thisObject); | |
461 | UNUSED_PARAM(exception); | |
462 | ||
463 | if (argumentCount > 0) { | |
464 | JSStringRef string = JSValueToStringCopy(context, arguments[0], NULL); | |
465 | size_t sizeUTF8 = JSStringGetMaximumUTF8CStringSize(string); | |
9dae56ea | 466 | char* stringUTF8 = (char*)malloc(sizeUTF8); |
b37bf2e1 A |
467 | JSStringGetUTF8CString(string, stringUTF8, sizeUTF8); |
468 | printf("%s\n", stringUTF8); | |
9dae56ea | 469 | free(stringUTF8); |
b37bf2e1 A |
470 | JSStringRelease(string); |
471 | } | |
472 | ||
473 | return JSValueMakeUndefined(context); | |
474 | } | |
475 | ||
476 | static JSObjectRef myConstructor_callAsConstructor(JSContextRef context, JSObjectRef constructorObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) | |
477 | { | |
478 | UNUSED_PARAM(constructorObject); | |
479 | UNUSED_PARAM(exception); | |
480 | ||
481 | JSObjectRef result = JSObjectMake(context, NULL, NULL); | |
482 | if (argumentCount > 0) { | |
483 | JSStringRef value = JSStringCreateWithUTF8CString("value"); | |
484 | JSObjectSetProperty(context, result, value, arguments[0], kJSPropertyAttributeNone, NULL); | |
485 | JSStringRelease(value); | |
486 | } | |
487 | ||
488 | return result; | |
489 | } | |
490 | ||
491 | ||
492 | static void globalObject_initialize(JSContextRef context, JSObjectRef object) | |
493 | { | |
494 | UNUSED_PARAM(object); | |
495 | // Ensure that an execution context is passed in | |
496 | ASSERT(context); | |
497 | ||
498 | // Ensure that the global object is set to the object that we were passed | |
499 | JSObjectRef globalObject = JSContextGetGlobalObject(context); | |
500 | ASSERT(globalObject); | |
501 | ASSERT(object == globalObject); | |
502 | ||
503 | // Ensure that the standard global properties have been set on the global object | |
504 | JSStringRef array = JSStringCreateWithUTF8CString("Array"); | |
505 | JSObjectRef arrayConstructor = JSValueToObject(context, JSObjectGetProperty(context, globalObject, array, NULL), NULL); | |
506 | JSStringRelease(array); | |
507 | ||
508 | UNUSED_PARAM(arrayConstructor); | |
509 | ASSERT(arrayConstructor); | |
510 | } | |
511 | ||
512 | static JSValueRef globalObject_get(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception) | |
513 | { | |
514 | UNUSED_PARAM(object); | |
515 | UNUSED_PARAM(propertyName); | |
516 | UNUSED_PARAM(exception); | |
517 | ||
518 | return JSValueMakeNumber(ctx, 3); | |
519 | } | |
520 | ||
521 | static bool globalObject_set(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef value, JSValueRef* exception) | |
522 | { | |
523 | UNUSED_PARAM(object); | |
524 | UNUSED_PARAM(propertyName); | |
525 | UNUSED_PARAM(value); | |
526 | ||
527 | *exception = JSValueMakeNumber(ctx, 3); | |
528 | return true; | |
529 | } | |
530 | ||
531 | static JSValueRef globalObject_call(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) | |
532 | { | |
533 | UNUSED_PARAM(function); | |
534 | UNUSED_PARAM(thisObject); | |
535 | UNUSED_PARAM(argumentCount); | |
536 | UNUSED_PARAM(arguments); | |
537 | UNUSED_PARAM(exception); | |
538 | ||
539 | return JSValueMakeNumber(ctx, 3); | |
540 | } | |
541 | ||
542 | static JSStaticValue globalObject_staticValues[] = { | |
543 | { "globalStaticValue", globalObject_get, globalObject_set, kJSPropertyAttributeNone }, | |
544 | { 0, 0, 0, 0 } | |
545 | }; | |
546 | ||
547 | static JSStaticFunction globalObject_staticFunctions[] = { | |
548 | { "globalStaticFunction", globalObject_call, kJSPropertyAttributeNone }, | |
549 | { 0, 0, 0 } | |
550 | }; | |
551 | ||
552 | static char* createStringWithContentsOfFile(const char* fileName); | |
553 | ||
554 | static void testInitializeFinalize() | |
555 | { | |
556 | JSObjectRef o = JSObjectMake(context, Derived_class(context), (void*)1); | |
557 | UNUSED_PARAM(o); | |
558 | ASSERT(JSObjectGetPrivate(o) == (void*)3); | |
559 | } | |
560 | ||
561 | int main(int argc, char* argv[]) | |
562 | { | |
563 | const char *scriptPath = "testapi.js"; | |
564 | if (argc > 1) { | |
565 | scriptPath = argv[1]; | |
566 | } | |
567 | ||
568 | // Test garbage collection with a fresh context | |
9dae56ea | 569 | context = JSGlobalContextCreateInGroup(NULL, NULL); |
b37bf2e1 A |
570 | TestInitializeFinalize = true; |
571 | testInitializeFinalize(); | |
572 | JSGlobalContextRelease(context); | |
b37bf2e1 A |
573 | TestInitializeFinalize = false; |
574 | ||
575 | ASSERT(Base_didFinalize); | |
576 | ||
577 | JSClassDefinition globalObjectClassDefinition = kJSClassDefinitionEmpty; | |
578 | globalObjectClassDefinition.initialize = globalObject_initialize; | |
579 | globalObjectClassDefinition.staticValues = globalObject_staticValues; | |
580 | globalObjectClassDefinition.staticFunctions = globalObject_staticFunctions; | |
581 | globalObjectClassDefinition.attributes = kJSClassAttributeNoAutomaticPrototype; | |
582 | JSClassRef globalObjectClass = JSClassCreate(&globalObjectClassDefinition); | |
9dae56ea A |
583 | context = JSGlobalContextCreateInGroup(NULL, globalObjectClass); |
584 | ||
585 | JSGlobalContextRetain(context); | |
586 | JSGlobalContextRelease(context); | |
b37bf2e1 | 587 | |
9dae56ea A |
588 | JSReportExtraMemoryCost(context, 0); |
589 | JSReportExtraMemoryCost(context, 1); | |
590 | JSReportExtraMemoryCost(context, 1024); | |
591 | ||
b37bf2e1 A |
592 | JSObjectRef globalObject = JSContextGetGlobalObject(context); |
593 | ASSERT(JSValueIsObject(context, globalObject)); | |
594 | ||
595 | JSValueRef jsUndefined = JSValueMakeUndefined(context); | |
596 | JSValueRef jsNull = JSValueMakeNull(context); | |
597 | JSValueRef jsTrue = JSValueMakeBoolean(context, true); | |
598 | JSValueRef jsFalse = JSValueMakeBoolean(context, false); | |
599 | JSValueRef jsZero = JSValueMakeNumber(context, 0); | |
600 | JSValueRef jsOne = JSValueMakeNumber(context, 1); | |
601 | JSValueRef jsOneThird = JSValueMakeNumber(context, 1.0 / 3.0); | |
602 | JSObjectRef jsObjectNoProto = JSObjectMake(context, NULL, NULL); | |
603 | JSObjectSetPrototype(context, jsObjectNoProto, JSValueMakeNull(context)); | |
604 | ||
605 | // FIXME: test funny utf8 characters | |
606 | JSStringRef jsEmptyIString = JSStringCreateWithUTF8CString(""); | |
607 | JSValueRef jsEmptyString = JSValueMakeString(context, jsEmptyIString); | |
608 | ||
609 | JSStringRef jsOneIString = JSStringCreateWithUTF8CString("1"); | |
610 | JSValueRef jsOneString = JSValueMakeString(context, jsOneIString); | |
611 | ||
612 | UniChar singleUniChar = 65; // Capital A | |
613 | CFMutableStringRef cfString = | |
614 | CFStringCreateMutableWithExternalCharactersNoCopy(kCFAllocatorDefault, | |
615 | &singleUniChar, | |
616 | 1, | |
617 | 1, | |
618 | kCFAllocatorNull); | |
619 | ||
620 | JSStringRef jsCFIString = JSStringCreateWithCFString(cfString); | |
621 | JSValueRef jsCFString = JSValueMakeString(context, jsCFIString); | |
622 | ||
623 | CFStringRef cfEmptyString = CFStringCreateWithCString(kCFAllocatorDefault, "", kCFStringEncodingUTF8); | |
624 | ||
625 | JSStringRef jsCFEmptyIString = JSStringCreateWithCFString(cfEmptyString); | |
626 | JSValueRef jsCFEmptyString = JSValueMakeString(context, jsCFEmptyIString); | |
627 | ||
628 | CFIndex cfStringLength = CFStringGetLength(cfString); | |
9dae56ea | 629 | UniChar* buffer = (UniChar*)malloc(cfStringLength * sizeof(UniChar)); |
b37bf2e1 A |
630 | CFStringGetCharacters(cfString, |
631 | CFRangeMake(0, cfStringLength), | |
632 | buffer); | |
9dae56ea | 633 | JSStringRef jsCFIStringWithCharacters = JSStringCreateWithCharacters((JSChar*)buffer, cfStringLength); |
b37bf2e1 A |
634 | JSValueRef jsCFStringWithCharacters = JSValueMakeString(context, jsCFIStringWithCharacters); |
635 | ||
9dae56ea A |
636 | JSStringRef jsCFEmptyIStringWithCharacters = JSStringCreateWithCharacters((JSChar*)buffer, CFStringGetLength(cfEmptyString)); |
637 | free(buffer); | |
b37bf2e1 A |
638 | JSValueRef jsCFEmptyStringWithCharacters = JSValueMakeString(context, jsCFEmptyIStringWithCharacters); |
639 | ||
640 | ASSERT(JSValueGetType(context, jsUndefined) == kJSTypeUndefined); | |
641 | ASSERT(JSValueGetType(context, jsNull) == kJSTypeNull); | |
642 | ASSERT(JSValueGetType(context, jsTrue) == kJSTypeBoolean); | |
643 | ASSERT(JSValueGetType(context, jsFalse) == kJSTypeBoolean); | |
644 | ASSERT(JSValueGetType(context, jsZero) == kJSTypeNumber); | |
645 | ASSERT(JSValueGetType(context, jsOne) == kJSTypeNumber); | |
646 | ASSERT(JSValueGetType(context, jsOneThird) == kJSTypeNumber); | |
647 | ASSERT(JSValueGetType(context, jsEmptyString) == kJSTypeString); | |
648 | ASSERT(JSValueGetType(context, jsOneString) == kJSTypeString); | |
649 | ASSERT(JSValueGetType(context, jsCFString) == kJSTypeString); | |
650 | ASSERT(JSValueGetType(context, jsCFStringWithCharacters) == kJSTypeString); | |
651 | ASSERT(JSValueGetType(context, jsCFEmptyString) == kJSTypeString); | |
652 | ASSERT(JSValueGetType(context, jsCFEmptyStringWithCharacters) == kJSTypeString); | |
653 | ||
654 | JSObjectRef myObject = JSObjectMake(context, MyObject_class(context), NULL); | |
655 | JSStringRef myObjectIString = JSStringCreateWithUTF8CString("MyObject"); | |
656 | JSObjectSetProperty(context, globalObject, myObjectIString, myObject, kJSPropertyAttributeNone, NULL); | |
657 | JSStringRelease(myObjectIString); | |
658 | ||
659 | JSValueRef exception; | |
660 | ||
661 | // Conversions that throw exceptions | |
662 | exception = NULL; | |
663 | ASSERT(NULL == JSValueToObject(context, jsNull, &exception)); | |
664 | ASSERT(exception); | |
665 | ||
666 | exception = NULL; | |
667 | // FIXME <rdar://4668451> - On i386 the isnan(double) macro tries to map to the isnan(float) function, | |
668 | // causing a build break with -Wshorten-64-to-32 enabled. The issue is known by the appropriate team. | |
669 | // After that's resolved, we can remove these casts | |
670 | ASSERT(isnan((float)JSValueToNumber(context, jsObjectNoProto, &exception))); | |
671 | ASSERT(exception); | |
672 | ||
673 | exception = NULL; | |
674 | ASSERT(!JSValueToStringCopy(context, jsObjectNoProto, &exception)); | |
675 | ASSERT(exception); | |
676 | ||
677 | ASSERT(JSValueToBoolean(context, myObject)); | |
678 | ||
679 | exception = NULL; | |
680 | ASSERT(!JSValueIsEqual(context, jsObjectNoProto, JSValueMakeNumber(context, 1), &exception)); | |
681 | ASSERT(exception); | |
682 | ||
683 | exception = NULL; | |
684 | JSObjectGetPropertyAtIndex(context, myObject, 0, &exception); | |
685 | ASSERT(1 == JSValueToNumber(context, exception, NULL)); | |
686 | ||
687 | assertEqualsAsBoolean(jsUndefined, false); | |
688 | assertEqualsAsBoolean(jsNull, false); | |
689 | assertEqualsAsBoolean(jsTrue, true); | |
690 | assertEqualsAsBoolean(jsFalse, false); | |
691 | assertEqualsAsBoolean(jsZero, false); | |
692 | assertEqualsAsBoolean(jsOne, true); | |
693 | assertEqualsAsBoolean(jsOneThird, true); | |
694 | assertEqualsAsBoolean(jsEmptyString, false); | |
695 | assertEqualsAsBoolean(jsOneString, true); | |
696 | assertEqualsAsBoolean(jsCFString, true); | |
697 | assertEqualsAsBoolean(jsCFStringWithCharacters, true); | |
698 | assertEqualsAsBoolean(jsCFEmptyString, false); | |
699 | assertEqualsAsBoolean(jsCFEmptyStringWithCharacters, false); | |
700 | ||
701 | assertEqualsAsNumber(jsUndefined, nan("")); | |
702 | assertEqualsAsNumber(jsNull, 0); | |
703 | assertEqualsAsNumber(jsTrue, 1); | |
704 | assertEqualsAsNumber(jsFalse, 0); | |
705 | assertEqualsAsNumber(jsZero, 0); | |
706 | assertEqualsAsNumber(jsOne, 1); | |
707 | assertEqualsAsNumber(jsOneThird, 1.0 / 3.0); | |
708 | assertEqualsAsNumber(jsEmptyString, 0); | |
709 | assertEqualsAsNumber(jsOneString, 1); | |
710 | assertEqualsAsNumber(jsCFString, nan("")); | |
711 | assertEqualsAsNumber(jsCFStringWithCharacters, nan("")); | |
712 | assertEqualsAsNumber(jsCFEmptyString, 0); | |
713 | assertEqualsAsNumber(jsCFEmptyStringWithCharacters, 0); | |
714 | ASSERT(sizeof(JSChar) == sizeof(UniChar)); | |
715 | ||
716 | assertEqualsAsCharactersPtr(jsUndefined, "undefined"); | |
717 | assertEqualsAsCharactersPtr(jsNull, "null"); | |
718 | assertEqualsAsCharactersPtr(jsTrue, "true"); | |
719 | assertEqualsAsCharactersPtr(jsFalse, "false"); | |
720 | assertEqualsAsCharactersPtr(jsZero, "0"); | |
721 | assertEqualsAsCharactersPtr(jsOne, "1"); | |
722 | assertEqualsAsCharactersPtr(jsOneThird, "0.3333333333333333"); | |
723 | assertEqualsAsCharactersPtr(jsEmptyString, ""); | |
724 | assertEqualsAsCharactersPtr(jsOneString, "1"); | |
725 | assertEqualsAsCharactersPtr(jsCFString, "A"); | |
726 | assertEqualsAsCharactersPtr(jsCFStringWithCharacters, "A"); | |
727 | assertEqualsAsCharactersPtr(jsCFEmptyString, ""); | |
728 | assertEqualsAsCharactersPtr(jsCFEmptyStringWithCharacters, ""); | |
729 | ||
730 | assertEqualsAsUTF8String(jsUndefined, "undefined"); | |
731 | assertEqualsAsUTF8String(jsNull, "null"); | |
732 | assertEqualsAsUTF8String(jsTrue, "true"); | |
733 | assertEqualsAsUTF8String(jsFalse, "false"); | |
734 | assertEqualsAsUTF8String(jsZero, "0"); | |
735 | assertEqualsAsUTF8String(jsOne, "1"); | |
736 | assertEqualsAsUTF8String(jsOneThird, "0.3333333333333333"); | |
737 | assertEqualsAsUTF8String(jsEmptyString, ""); | |
738 | assertEqualsAsUTF8String(jsOneString, "1"); | |
739 | assertEqualsAsUTF8String(jsCFString, "A"); | |
740 | assertEqualsAsUTF8String(jsCFStringWithCharacters, "A"); | |
741 | assertEqualsAsUTF8String(jsCFEmptyString, ""); | |
742 | assertEqualsAsUTF8String(jsCFEmptyStringWithCharacters, ""); | |
743 | ||
744 | ASSERT(JSValueIsStrictEqual(context, jsTrue, jsTrue)); | |
745 | ASSERT(!JSValueIsStrictEqual(context, jsOne, jsOneString)); | |
746 | ||
747 | ASSERT(JSValueIsEqual(context, jsOne, jsOneString, NULL)); | |
748 | ASSERT(!JSValueIsEqual(context, jsTrue, jsFalse, NULL)); | |
749 | ||
750 | CFStringRef cfJSString = JSStringCopyCFString(kCFAllocatorDefault, jsCFIString); | |
751 | CFStringRef cfJSEmptyString = JSStringCopyCFString(kCFAllocatorDefault, jsCFEmptyIString); | |
752 | ASSERT(CFEqual(cfJSString, cfString)); | |
753 | ASSERT(CFEqual(cfJSEmptyString, cfEmptyString)); | |
754 | CFRelease(cfJSString); | |
755 | CFRelease(cfJSEmptyString); | |
756 | ||
757 | CFRelease(cfString); | |
758 | CFRelease(cfEmptyString); | |
759 | ||
760 | jsGlobalValue = JSObjectMake(context, NULL, NULL); | |
761 | JSValueProtect(context, jsGlobalValue); | |
762 | JSGarbageCollect(context); | |
763 | ASSERT(JSValueIsObject(context, jsGlobalValue)); | |
764 | JSValueUnprotect(context, jsGlobalValue); | |
765 | ||
766 | JSStringRef goodSyntax = JSStringCreateWithUTF8CString("x = 1;"); | |
767 | JSStringRef badSyntax = JSStringCreateWithUTF8CString("x := 1;"); | |
768 | ASSERT(JSCheckScriptSyntax(context, goodSyntax, NULL, 0, NULL)); | |
769 | ASSERT(!JSCheckScriptSyntax(context, badSyntax, NULL, 0, NULL)); | |
770 | ||
771 | JSValueRef result; | |
772 | JSValueRef v; | |
773 | JSObjectRef o; | |
774 | JSStringRef string; | |
775 | ||
776 | result = JSEvaluateScript(context, goodSyntax, NULL, NULL, 1, NULL); | |
777 | ASSERT(result); | |
778 | ASSERT(JSValueIsEqual(context, result, jsOne, NULL)); | |
779 | ||
780 | exception = NULL; | |
781 | result = JSEvaluateScript(context, badSyntax, NULL, NULL, 1, &exception); | |
782 | ASSERT(!result); | |
783 | ASSERT(JSValueIsObject(context, exception)); | |
784 | ||
785 | JSStringRef array = JSStringCreateWithUTF8CString("Array"); | |
786 | JSObjectRef arrayConstructor = JSValueToObject(context, JSObjectGetProperty(context, globalObject, array, NULL), NULL); | |
787 | JSStringRelease(array); | |
788 | result = JSObjectCallAsConstructor(context, arrayConstructor, 0, NULL, NULL); | |
789 | ASSERT(result); | |
790 | ASSERT(JSValueIsObject(context, result)); | |
791 | ASSERT(JSValueIsInstanceOfConstructor(context, result, arrayConstructor, NULL)); | |
792 | ASSERT(!JSValueIsInstanceOfConstructor(context, JSValueMakeNull(context), arrayConstructor, NULL)); | |
793 | ||
794 | o = JSValueToObject(context, result, NULL); | |
795 | exception = NULL; | |
796 | ASSERT(JSValueIsUndefined(context, JSObjectGetPropertyAtIndex(context, o, 0, &exception))); | |
797 | ASSERT(!exception); | |
798 | ||
799 | JSObjectSetPropertyAtIndex(context, o, 0, JSValueMakeNumber(context, 1), &exception); | |
800 | ASSERT(!exception); | |
801 | ||
802 | exception = NULL; | |
803 | ASSERT(1 == JSValueToNumber(context, JSObjectGetPropertyAtIndex(context, o, 0, &exception), &exception)); | |
804 | ASSERT(!exception); | |
805 | ||
806 | JSStringRef functionBody; | |
807 | JSObjectRef function; | |
808 | ||
809 | exception = NULL; | |
810 | functionBody = JSStringCreateWithUTF8CString("rreturn Array;"); | |
811 | JSStringRef line = JSStringCreateWithUTF8CString("line"); | |
812 | ASSERT(!JSObjectMakeFunction(context, NULL, 0, NULL, functionBody, NULL, 1, &exception)); | |
813 | ASSERT(JSValueIsObject(context, exception)); | |
814 | v = JSObjectGetProperty(context, JSValueToObject(context, exception, NULL), line, NULL); | |
9dae56ea | 815 | assertEqualsAsNumber(v, 1); |
b37bf2e1 A |
816 | JSStringRelease(functionBody); |
817 | JSStringRelease(line); | |
818 | ||
819 | exception = NULL; | |
820 | functionBody = JSStringCreateWithUTF8CString("return Array;"); | |
821 | function = JSObjectMakeFunction(context, NULL, 0, NULL, functionBody, NULL, 1, &exception); | |
822 | JSStringRelease(functionBody); | |
823 | ASSERT(!exception); | |
824 | ASSERT(JSObjectIsFunction(context, function)); | |
825 | v = JSObjectCallAsFunction(context, function, NULL, 0, NULL, NULL); | |
826 | ASSERT(v); | |
827 | ASSERT(JSValueIsEqual(context, v, arrayConstructor, NULL)); | |
828 | ||
829 | exception = NULL; | |
830 | function = JSObjectMakeFunction(context, NULL, 0, NULL, jsEmptyIString, NULL, 0, &exception); | |
831 | ASSERT(!exception); | |
832 | v = JSObjectCallAsFunction(context, function, NULL, 0, NULL, &exception); | |
833 | ASSERT(v && !exception); | |
834 | ASSERT(JSValueIsUndefined(context, v)); | |
835 | ||
836 | exception = NULL; | |
837 | v = NULL; | |
838 | JSStringRef foo = JSStringCreateWithUTF8CString("foo"); | |
839 | JSStringRef argumentNames[] = { foo }; | |
840 | functionBody = JSStringCreateWithUTF8CString("return foo;"); | |
841 | function = JSObjectMakeFunction(context, foo, 1, argumentNames, functionBody, NULL, 1, &exception); | |
842 | ASSERT(function && !exception); | |
843 | JSValueRef arguments[] = { JSValueMakeNumber(context, 2) }; | |
844 | v = JSObjectCallAsFunction(context, function, NULL, 1, arguments, &exception); | |
845 | JSStringRelease(foo); | |
846 | JSStringRelease(functionBody); | |
847 | ||
848 | string = JSValueToStringCopy(context, function, NULL); | |
9dae56ea | 849 | assertEqualsAsUTF8String(JSValueMakeString(context, string), "function foo(foo) {return foo;}"); |
b37bf2e1 A |
850 | JSStringRelease(string); |
851 | ||
852 | JSStringRef print = JSStringCreateWithUTF8CString("print"); | |
853 | JSObjectRef printFunction = JSObjectMakeFunctionWithCallback(context, print, print_callAsFunction); | |
854 | JSObjectSetProperty(context, globalObject, print, printFunction, kJSPropertyAttributeNone, NULL); | |
855 | JSStringRelease(print); | |
856 | ||
857 | ASSERT(!JSObjectSetPrivate(printFunction, (void*)1)); | |
858 | ASSERT(!JSObjectGetPrivate(printFunction)); | |
859 | ||
860 | JSStringRef myConstructorIString = JSStringCreateWithUTF8CString("MyConstructor"); | |
861 | JSObjectRef myConstructor = JSObjectMakeConstructor(context, NULL, myConstructor_callAsConstructor); | |
862 | JSObjectSetProperty(context, globalObject, myConstructorIString, myConstructor, kJSPropertyAttributeNone, NULL); | |
863 | JSStringRelease(myConstructorIString); | |
864 | ||
865 | ASSERT(!JSObjectSetPrivate(myConstructor, (void*)1)); | |
866 | ASSERT(!JSObjectGetPrivate(myConstructor)); | |
867 | ||
868 | string = JSStringCreateWithUTF8CString("Derived"); | |
869 | JSObjectRef derivedConstructor = JSObjectMakeConstructor(context, Derived_class(context), NULL); | |
870 | JSObjectSetProperty(context, globalObject, string, derivedConstructor, kJSPropertyAttributeNone, NULL); | |
871 | JSStringRelease(string); | |
872 | ||
873 | o = JSObjectMake(context, NULL, NULL); | |
874 | JSObjectSetProperty(context, o, jsOneIString, JSValueMakeNumber(context, 1), kJSPropertyAttributeNone, NULL); | |
875 | JSObjectSetProperty(context, o, jsCFIString, JSValueMakeNumber(context, 1), kJSPropertyAttributeDontEnum, NULL); | |
876 | JSPropertyNameArrayRef nameArray = JSObjectCopyPropertyNames(context, o); | |
877 | size_t expectedCount = JSPropertyNameArrayGetCount(nameArray); | |
878 | size_t count; | |
879 | for (count = 0; count < expectedCount; ++count) | |
880 | JSPropertyNameArrayGetNameAtIndex(nameArray, count); | |
881 | JSPropertyNameArrayRelease(nameArray); | |
882 | ASSERT(count == 1); // jsCFString should not be enumerated | |
883 | ||
9dae56ea A |
884 | JSValueRef argumentsArrayValues[] = { JSValueMakeNumber(context, 10), JSValueMakeNumber(context, 20) }; |
885 | o = JSObjectMakeArray(context, sizeof(argumentsArrayValues) / sizeof(JSValueRef), argumentsArrayValues, NULL); | |
886 | string = JSStringCreateWithUTF8CString("length"); | |
887 | v = JSObjectGetProperty(context, o, string, NULL); | |
888 | assertEqualsAsNumber(v, 2); | |
889 | v = JSObjectGetPropertyAtIndex(context, o, 0, NULL); | |
890 | assertEqualsAsNumber(v, 10); | |
891 | v = JSObjectGetPropertyAtIndex(context, o, 1, NULL); | |
892 | assertEqualsAsNumber(v, 20); | |
893 | ||
894 | o = JSObjectMakeArray(context, 0, NULL, NULL); | |
895 | v = JSObjectGetProperty(context, o, string, NULL); | |
896 | assertEqualsAsNumber(v, 0); | |
897 | JSStringRelease(string); | |
898 | ||
899 | JSValueRef argumentsDateValues[] = { JSValueMakeNumber(context, 0) }; | |
900 | o = JSObjectMakeDate(context, 1, argumentsDateValues, NULL); | |
901 | assertEqualsAsUTF8String(o, "Wed Dec 31 1969 16:00:00 GMT-0800 (PST)"); | |
902 | ||
903 | string = JSStringCreateWithUTF8CString("an error message"); | |
904 | JSValueRef argumentsErrorValues[] = { JSValueMakeString(context, string) }; | |
905 | o = JSObjectMakeError(context, 1, argumentsErrorValues, NULL); | |
906 | assertEqualsAsUTF8String(o, "Error: an error message"); | |
907 | JSStringRelease(string); | |
908 | ||
909 | string = JSStringCreateWithUTF8CString("foo"); | |
910 | JSStringRef string2 = JSStringCreateWithUTF8CString("gi"); | |
911 | JSValueRef argumentsRegExpValues[] = { JSValueMakeString(context, string), JSValueMakeString(context, string2) }; | |
912 | o = JSObjectMakeRegExp(context, 2, argumentsRegExpValues, NULL); | |
913 | assertEqualsAsUTF8String(o, "/foo/gi"); | |
914 | JSStringRelease(string); | |
915 | JSStringRelease(string2); | |
916 | ||
b37bf2e1 A |
917 | JSClassDefinition nullDefinition = kJSClassDefinitionEmpty; |
918 | nullDefinition.attributes = kJSClassAttributeNoAutomaticPrototype; | |
919 | JSClassRef nullClass = JSClassCreate(&nullDefinition); | |
920 | JSClassRelease(nullClass); | |
921 | ||
922 | nullDefinition = kJSClassDefinitionEmpty; | |
923 | nullClass = JSClassCreate(&nullDefinition); | |
924 | JSClassRelease(nullClass); | |
925 | ||
926 | functionBody = JSStringCreateWithUTF8CString("return this;"); | |
927 | function = JSObjectMakeFunction(context, NULL, 0, NULL, functionBody, NULL, 1, NULL); | |
928 | JSStringRelease(functionBody); | |
929 | v = JSObjectCallAsFunction(context, function, NULL, 0, NULL, NULL); | |
930 | ASSERT(JSValueIsEqual(context, v, globalObject, NULL)); | |
931 | v = JSObjectCallAsFunction(context, function, o, 0, NULL, NULL); | |
932 | ASSERT(JSValueIsEqual(context, v, o, NULL)); | |
9dae56ea A |
933 | |
934 | functionBody = JSStringCreateWithUTF8CString("return eval(\"this\");"); | |
935 | function = JSObjectMakeFunction(context, NULL, 0, NULL, functionBody, NULL, 1, NULL); | |
936 | JSStringRelease(functionBody); | |
937 | v = JSObjectCallAsFunction(context, function, NULL, 0, NULL, NULL); | |
938 | ASSERT(JSValueIsEqual(context, v, globalObject, NULL)); | |
939 | v = JSObjectCallAsFunction(context, function, o, 0, NULL, NULL); | |
940 | ASSERT(JSValueIsEqual(context, v, o, NULL)); | |
941 | ||
942 | JSStringRef script = JSStringCreateWithUTF8CString("this;"); | |
943 | v = JSEvaluateScript(context, script, NULL, NULL, 1, NULL); | |
944 | ASSERT(JSValueIsEqual(context, v, globalObject, NULL)); | |
945 | v = JSEvaluateScript(context, script, o, NULL, 1, NULL); | |
946 | ASSERT(JSValueIsEqual(context, v, o, NULL)); | |
947 | JSStringRelease(script); | |
948 | ||
949 | script = JSStringCreateWithUTF8CString("eval(this);"); | |
950 | v = JSEvaluateScript(context, script, NULL, NULL, 1, NULL); | |
951 | ASSERT(JSValueIsEqual(context, v, globalObject, NULL)); | |
952 | v = JSEvaluateScript(context, script, o, NULL, 1, NULL); | |
953 | ASSERT(JSValueIsEqual(context, v, o, NULL)); | |
954 | JSStringRelease(script); | |
955 | ||
956 | char* scriptUTF8 = createStringWithContentsOfFile(scriptPath); | |
b37bf2e1 A |
957 | if (!scriptUTF8) |
958 | printf("FAIL: Test script could not be loaded.\n"); | |
959 | else { | |
9dae56ea | 960 | script = JSStringCreateWithUTF8CString(scriptUTF8); |
b37bf2e1 A |
961 | result = JSEvaluateScript(context, script, NULL, NULL, 1, &exception); |
962 | if (JSValueIsUndefined(context, result)) | |
963 | printf("PASS: Test script executed successfully.\n"); | |
964 | else { | |
965 | printf("FAIL: Test script returned unexpected value:\n"); | |
966 | JSStringRef exceptionIString = JSValueToStringCopy(context, exception, NULL); | |
967 | CFStringRef exceptionCF = JSStringCopyCFString(kCFAllocatorDefault, exceptionIString); | |
968 | CFShow(exceptionCF); | |
969 | CFRelease(exceptionCF); | |
970 | JSStringRelease(exceptionIString); | |
971 | } | |
972 | JSStringRelease(script); | |
973 | free(scriptUTF8); | |
974 | } | |
975 | ||
976 | // Clear out local variables pointing at JSObjectRefs to allow their values to be collected | |
977 | function = NULL; | |
978 | v = NULL; | |
979 | o = NULL; | |
980 | globalObject = NULL; | |
981 | ||
982 | JSStringRelease(jsEmptyIString); | |
983 | JSStringRelease(jsOneIString); | |
984 | JSStringRelease(jsCFIString); | |
985 | JSStringRelease(jsCFEmptyIString); | |
986 | JSStringRelease(jsCFIStringWithCharacters); | |
987 | JSStringRelease(jsCFEmptyIStringWithCharacters); | |
988 | JSStringRelease(goodSyntax); | |
989 | JSStringRelease(badSyntax); | |
990 | ||
991 | JSGlobalContextRelease(context); | |
b37bf2e1 A |
992 | JSClassRelease(globalObjectClass); |
993 | ||
994 | printf("PASS: Program exited normally.\n"); | |
995 | return 0; | |
996 | } | |
997 | ||
998 | static char* createStringWithContentsOfFile(const char* fileName) | |
999 | { | |
1000 | char* buffer; | |
1001 | ||
1002 | size_t buffer_size = 0; | |
1003 | size_t buffer_capacity = 1024; | |
1004 | buffer = (char*)malloc(buffer_capacity); | |
1005 | ||
1006 | FILE* f = fopen(fileName, "r"); | |
1007 | if (!f) { | |
1008 | fprintf(stderr, "Could not open file: %s\n", fileName); | |
1009 | return 0; | |
1010 | } | |
1011 | ||
1012 | while (!feof(f) && !ferror(f)) { | |
1013 | buffer_size += fread(buffer + buffer_size, 1, buffer_capacity - buffer_size, f); | |
1014 | if (buffer_size == buffer_capacity) { // guarantees space for trailing '\0' | |
1015 | buffer_capacity *= 2; | |
1016 | buffer = (char*)realloc(buffer, buffer_capacity); | |
1017 | ASSERT(buffer); | |
1018 | } | |
1019 | ||
1020 | ASSERT(buffer_size < buffer_capacity); | |
1021 | } | |
1022 | fclose(f); | |
1023 | buffer[buffer_size] = '\0'; | |
1024 | ||
1025 | return buffer; | |
1026 | } |