source: webkit/trunk/JavaScriptCore/API/testapi.c@ 31350

Last change on this file since 31350 was 31350, checked in by Adam Roben, 17 years ago

Fix Bug 18060: Assertion failure (JSLock not held) beneath JSCallbackObject<Base>::toString

<http://bugs.webkit.org/show_bug.cgi?id=18060>

Reviewed by Geoff Garen.

Bug fix:

  • API/JSCallbackObjectFunctions.h: (KJS::JSCallbackObject<Base>::toString): Make the DropAllLocks instance only be in scope while calling convertToType.

Test:

  • API/testapi.c: (MyObject_convertToType): Implement type conversion to string.
  • API/testapi.js: Add a test for type conversion to string.
  • Property svn:eol-style set to native
File size: 37.5 KB
Line 
1// -*- mode: c++; c-basic-offset: 4 -*-
2/*
3 * Copyright (C) 2006 Apple Computer, Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "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
36static double nan(const char*)
37{
38 return std::numeric_limits<double>::quiet_NaN();
39}
40
41#endif
42
43static JSGlobalContextRef context = 0;
44
45static 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
51static 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
62static 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
82static 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
107static JSValueRef jsGlobalValue; // non-stack value for testing JSValueProtect()
108
109/* MyObject pseudo-class */
110
111static 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
127static 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
152static 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
165static 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
181static 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
197static 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
210static 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
221static 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
233static 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 case kJSTypeString:
242 {
243 JSStringRef string = JSStringCreateWithUTF8CString("MyObjectAsString");
244 JSValueRef result = JSValueMakeString(context, string);
245 JSStringRelease(string);
246 return result;
247 }
248 default:
249 break;
250 }
251
252 // string conversion -- forward to default object class
253 return NULL;
254}
255
256static JSStaticValue evilStaticValues[] = {
257 { "nullGetSet", 0, 0, kJSPropertyAttributeNone },
258 { 0, 0, 0, 0 }
259};
260
261static JSStaticFunction evilStaticFunctions[] = {
262 { "nullCall", 0, kJSPropertyAttributeNone },
263 { 0, 0, 0 }
264};
265
266JSClassDefinition 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
289static 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
300static 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
309static 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
319static 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
330static JSStaticFunction Base_staticFunctions[] = {
331 { "baseProtoDup", NULL, kJSPropertyAttributeNone },
332 { "baseProto", Base_callAsFunction, kJSPropertyAttributeNone },
333 { 0, 0, 0 }
334};
335
336static JSStaticValue Base_staticValues[] = {
337 { "baseDup", Base_get, Base_set, kJSPropertyAttributeNone },
338 { "baseOnly", Base_get, Base_set, kJSPropertyAttributeNone },
339 { 0, 0, 0, 0 }
340};
341
342static bool TestInitializeFinalize;
343static 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
353static unsigned Base_didFinalize;
354static 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
363static 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
379static 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
388static 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
399static 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
410static JSStaticFunction Derived_staticFunctions[] = {
411 { "protoOnly", Derived_callAsFunction, kJSPropertyAttributeNone },
412 { "protoDup", NULL, kJSPropertyAttributeNone },
413 { "baseProtoDup", Derived_callAsFunction, kJSPropertyAttributeNone },
414 { 0, 0, 0 }
415};
416
417static 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
424static 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
434static void Derived_finalize(JSObjectRef object)
435{
436 if (TestInitializeFinalize) {
437 ASSERT((void*)3 == JSObjectGetPrivate(object));
438 JSObjectSetPrivate(object, (void*)4);
439 }
440}
441
442static 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
457static 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);
466 char* stringUTF8 = (char*)malloc(sizeUTF8);
467 JSStringGetUTF8CString(string, stringUTF8, sizeUTF8);
468 printf("%s\n", stringUTF8);
469 free(stringUTF8);
470 JSStringRelease(string);
471 }
472
473 return JSValueMakeUndefined(context);
474}
475
476static 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
492static 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
512static 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
521static 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
531static 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
542static JSStaticValue globalObject_staticValues[] = {
543 { "globalStaticValue", globalObject_get, globalObject_set, kJSPropertyAttributeNone },
544 { 0, 0, 0, 0 }
545};
546
547static JSStaticFunction globalObject_staticFunctions[] = {
548 { "globalStaticFunction", globalObject_call, kJSPropertyAttributeNone },
549 { 0, 0, 0 }
550};
551
552static char* createStringWithContentsOfFile(const char* fileName);
553
554static 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
561int 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
569 context = JSGlobalContextCreate(NULL);
570 TestInitializeFinalize = true;
571 testInitializeFinalize();
572 JSGlobalContextRelease(context);
573 JSGarbageCollect(context);
574 TestInitializeFinalize = false;
575
576 ASSERT(Base_didFinalize);
577
578 JSClassDefinition globalObjectClassDefinition = kJSClassDefinitionEmpty;
579 globalObjectClassDefinition.initialize = globalObject_initialize;
580 globalObjectClassDefinition.staticValues = globalObject_staticValues;
581 globalObjectClassDefinition.staticFunctions = globalObject_staticFunctions;
582 globalObjectClassDefinition.attributes = kJSClassAttributeNoAutomaticPrototype;
583 JSClassRef globalObjectClass = JSClassCreate(&globalObjectClassDefinition);
584 context = JSGlobalContextCreate(globalObjectClass);
585
586 JSObjectRef globalObject = JSContextGetGlobalObject(context);
587 ASSERT(JSValueIsObject(context, globalObject));
588
589 JSValueRef jsUndefined = JSValueMakeUndefined(context);
590 JSValueRef jsNull = JSValueMakeNull(context);
591 JSValueRef jsTrue = JSValueMakeBoolean(context, true);
592 JSValueRef jsFalse = JSValueMakeBoolean(context, false);
593 JSValueRef jsZero = JSValueMakeNumber(context, 0);
594 JSValueRef jsOne = JSValueMakeNumber(context, 1);
595 JSValueRef jsOneThird = JSValueMakeNumber(context, 1.0 / 3.0);
596 JSObjectRef jsObjectNoProto = JSObjectMake(context, NULL, NULL);
597 JSObjectSetPrototype(context, jsObjectNoProto, JSValueMakeNull(context));
598
599 // FIXME: test funny utf8 characters
600 JSStringRef jsEmptyIString = JSStringCreateWithUTF8CString("");
601 JSValueRef jsEmptyString = JSValueMakeString(context, jsEmptyIString);
602
603 JSStringRef jsOneIString = JSStringCreateWithUTF8CString("1");
604 JSValueRef jsOneString = JSValueMakeString(context, jsOneIString);
605
606 UniChar singleUniChar = 65; // Capital A
607 CFMutableStringRef cfString =
608 CFStringCreateMutableWithExternalCharactersNoCopy(kCFAllocatorDefault,
609 &singleUniChar,
610 1,
611 1,
612 kCFAllocatorNull);
613
614 JSStringRef jsCFIString = JSStringCreateWithCFString(cfString);
615 JSValueRef jsCFString = JSValueMakeString(context, jsCFIString);
616
617 CFStringRef cfEmptyString = CFStringCreateWithCString(kCFAllocatorDefault, "", kCFStringEncodingUTF8);
618
619 JSStringRef jsCFEmptyIString = JSStringCreateWithCFString(cfEmptyString);
620 JSValueRef jsCFEmptyString = JSValueMakeString(context, jsCFEmptyIString);
621
622 CFIndex cfStringLength = CFStringGetLength(cfString);
623 UniChar* buffer = (UniChar*)malloc(cfStringLength * sizeof(UniChar));
624 CFStringGetCharacters(cfString,
625 CFRangeMake(0, cfStringLength),
626 buffer);
627 JSStringRef jsCFIStringWithCharacters = JSStringCreateWithCharacters((JSChar*)buffer, cfStringLength);
628 JSValueRef jsCFStringWithCharacters = JSValueMakeString(context, jsCFIStringWithCharacters);
629
630 JSStringRef jsCFEmptyIStringWithCharacters = JSStringCreateWithCharacters((JSChar*)buffer, CFStringGetLength(cfEmptyString));
631 free(buffer);
632 JSValueRef jsCFEmptyStringWithCharacters = JSValueMakeString(context, jsCFEmptyIStringWithCharacters);
633
634 ASSERT(JSValueGetType(context, jsUndefined) == kJSTypeUndefined);
635 ASSERT(JSValueGetType(context, jsNull) == kJSTypeNull);
636 ASSERT(JSValueGetType(context, jsTrue) == kJSTypeBoolean);
637 ASSERT(JSValueGetType(context, jsFalse) == kJSTypeBoolean);
638 ASSERT(JSValueGetType(context, jsZero) == kJSTypeNumber);
639 ASSERT(JSValueGetType(context, jsOne) == kJSTypeNumber);
640 ASSERT(JSValueGetType(context, jsOneThird) == kJSTypeNumber);
641 ASSERT(JSValueGetType(context, jsEmptyString) == kJSTypeString);
642 ASSERT(JSValueGetType(context, jsOneString) == kJSTypeString);
643 ASSERT(JSValueGetType(context, jsCFString) == kJSTypeString);
644 ASSERT(JSValueGetType(context, jsCFStringWithCharacters) == kJSTypeString);
645 ASSERT(JSValueGetType(context, jsCFEmptyString) == kJSTypeString);
646 ASSERT(JSValueGetType(context, jsCFEmptyStringWithCharacters) == kJSTypeString);
647
648 JSObjectRef myObject = JSObjectMake(context, MyObject_class(context), NULL);
649 JSStringRef myObjectIString = JSStringCreateWithUTF8CString("MyObject");
650 JSObjectSetProperty(context, globalObject, myObjectIString, myObject, kJSPropertyAttributeNone, NULL);
651 JSStringRelease(myObjectIString);
652
653 JSValueRef exception;
654
655 // Conversions that throw exceptions
656 exception = NULL;
657 ASSERT(NULL == JSValueToObject(context, jsNull, &exception));
658 ASSERT(exception);
659
660 exception = NULL;
661 // FIXME <rdar://4668451> - On i386 the isnan(double) macro tries to map to the isnan(float) function,
662 // causing a build break with -Wshorten-64-to-32 enabled. The issue is known by the appropriate team.
663 // After that's resolved, we can remove these casts
664 ASSERT(isnan((float)JSValueToNumber(context, jsObjectNoProto, &exception)));
665 ASSERT(exception);
666
667 exception = NULL;
668 ASSERT(!JSValueToStringCopy(context, jsObjectNoProto, &exception));
669 ASSERT(exception);
670
671 ASSERT(JSValueToBoolean(context, myObject));
672
673 exception = NULL;
674 ASSERT(!JSValueIsEqual(context, jsObjectNoProto, JSValueMakeNumber(context, 1), &exception));
675 ASSERT(exception);
676
677 exception = NULL;
678 JSObjectGetPropertyAtIndex(context, myObject, 0, &exception);
679 ASSERT(1 == JSValueToNumber(context, exception, NULL));
680
681 assertEqualsAsBoolean(jsUndefined, false);
682 assertEqualsAsBoolean(jsNull, false);
683 assertEqualsAsBoolean(jsTrue, true);
684 assertEqualsAsBoolean(jsFalse, false);
685 assertEqualsAsBoolean(jsZero, false);
686 assertEqualsAsBoolean(jsOne, true);
687 assertEqualsAsBoolean(jsOneThird, true);
688 assertEqualsAsBoolean(jsEmptyString, false);
689 assertEqualsAsBoolean(jsOneString, true);
690 assertEqualsAsBoolean(jsCFString, true);
691 assertEqualsAsBoolean(jsCFStringWithCharacters, true);
692 assertEqualsAsBoolean(jsCFEmptyString, false);
693 assertEqualsAsBoolean(jsCFEmptyStringWithCharacters, false);
694
695 assertEqualsAsNumber(jsUndefined, nan(""));
696 assertEqualsAsNumber(jsNull, 0);
697 assertEqualsAsNumber(jsTrue, 1);
698 assertEqualsAsNumber(jsFalse, 0);
699 assertEqualsAsNumber(jsZero, 0);
700 assertEqualsAsNumber(jsOne, 1);
701 assertEqualsAsNumber(jsOneThird, 1.0 / 3.0);
702 assertEqualsAsNumber(jsEmptyString, 0);
703 assertEqualsAsNumber(jsOneString, 1);
704 assertEqualsAsNumber(jsCFString, nan(""));
705 assertEqualsAsNumber(jsCFStringWithCharacters, nan(""));
706 assertEqualsAsNumber(jsCFEmptyString, 0);
707 assertEqualsAsNumber(jsCFEmptyStringWithCharacters, 0);
708 ASSERT(sizeof(JSChar) == sizeof(UniChar));
709
710 assertEqualsAsCharactersPtr(jsUndefined, "undefined");
711 assertEqualsAsCharactersPtr(jsNull, "null");
712 assertEqualsAsCharactersPtr(jsTrue, "true");
713 assertEqualsAsCharactersPtr(jsFalse, "false");
714 assertEqualsAsCharactersPtr(jsZero, "0");
715 assertEqualsAsCharactersPtr(jsOne, "1");
716 assertEqualsAsCharactersPtr(jsOneThird, "0.3333333333333333");
717 assertEqualsAsCharactersPtr(jsEmptyString, "");
718 assertEqualsAsCharactersPtr(jsOneString, "1");
719 assertEqualsAsCharactersPtr(jsCFString, "A");
720 assertEqualsAsCharactersPtr(jsCFStringWithCharacters, "A");
721 assertEqualsAsCharactersPtr(jsCFEmptyString, "");
722 assertEqualsAsCharactersPtr(jsCFEmptyStringWithCharacters, "");
723
724 assertEqualsAsUTF8String(jsUndefined, "undefined");
725 assertEqualsAsUTF8String(jsNull, "null");
726 assertEqualsAsUTF8String(jsTrue, "true");
727 assertEqualsAsUTF8String(jsFalse, "false");
728 assertEqualsAsUTF8String(jsZero, "0");
729 assertEqualsAsUTF8String(jsOne, "1");
730 assertEqualsAsUTF8String(jsOneThird, "0.3333333333333333");
731 assertEqualsAsUTF8String(jsEmptyString, "");
732 assertEqualsAsUTF8String(jsOneString, "1");
733 assertEqualsAsUTF8String(jsCFString, "A");
734 assertEqualsAsUTF8String(jsCFStringWithCharacters, "A");
735 assertEqualsAsUTF8String(jsCFEmptyString, "");
736 assertEqualsAsUTF8String(jsCFEmptyStringWithCharacters, "");
737
738 ASSERT(JSValueIsStrictEqual(context, jsTrue, jsTrue));
739 ASSERT(!JSValueIsStrictEqual(context, jsOne, jsOneString));
740
741 ASSERT(JSValueIsEqual(context, jsOne, jsOneString, NULL));
742 ASSERT(!JSValueIsEqual(context, jsTrue, jsFalse, NULL));
743
744 CFStringRef cfJSString = JSStringCopyCFString(kCFAllocatorDefault, jsCFIString);
745 CFStringRef cfJSEmptyString = JSStringCopyCFString(kCFAllocatorDefault, jsCFEmptyIString);
746 ASSERT(CFEqual(cfJSString, cfString));
747 ASSERT(CFEqual(cfJSEmptyString, cfEmptyString));
748 CFRelease(cfJSString);
749 CFRelease(cfJSEmptyString);
750
751 CFRelease(cfString);
752 CFRelease(cfEmptyString);
753
754 jsGlobalValue = JSObjectMake(context, NULL, NULL);
755 JSValueProtect(context, jsGlobalValue);
756 JSGarbageCollect(context);
757 ASSERT(JSValueIsObject(context, jsGlobalValue));
758 JSValueUnprotect(context, jsGlobalValue);
759
760 JSStringRef goodSyntax = JSStringCreateWithUTF8CString("x = 1;");
761 JSStringRef badSyntax = JSStringCreateWithUTF8CString("x := 1;");
762 ASSERT(JSCheckScriptSyntax(context, goodSyntax, NULL, 0, NULL));
763 ASSERT(!JSCheckScriptSyntax(context, badSyntax, NULL, 0, NULL));
764
765 JSValueRef result;
766 JSValueRef v;
767 JSObjectRef o;
768 JSStringRef string;
769
770 result = JSEvaluateScript(context, goodSyntax, NULL, NULL, 1, NULL);
771 ASSERT(result);
772 ASSERT(JSValueIsEqual(context, result, jsOne, NULL));
773
774 exception = NULL;
775 result = JSEvaluateScript(context, badSyntax, NULL, NULL, 1, &exception);
776 ASSERT(!result);
777 ASSERT(JSValueIsObject(context, exception));
778
779 JSStringRef array = JSStringCreateWithUTF8CString("Array");
780 JSObjectRef arrayConstructor = JSValueToObject(context, JSObjectGetProperty(context, globalObject, array, NULL), NULL);
781 JSStringRelease(array);
782 result = JSObjectCallAsConstructor(context, arrayConstructor, 0, NULL, NULL);
783 ASSERT(result);
784 ASSERT(JSValueIsObject(context, result));
785 ASSERT(JSValueIsInstanceOfConstructor(context, result, arrayConstructor, NULL));
786 ASSERT(!JSValueIsInstanceOfConstructor(context, JSValueMakeNull(context), arrayConstructor, NULL));
787
788 o = JSValueToObject(context, result, NULL);
789 exception = NULL;
790 ASSERT(JSValueIsUndefined(context, JSObjectGetPropertyAtIndex(context, o, 0, &exception)));
791 ASSERT(!exception);
792
793 JSObjectSetPropertyAtIndex(context, o, 0, JSValueMakeNumber(context, 1), &exception);
794 ASSERT(!exception);
795
796 exception = NULL;
797 ASSERT(1 == JSValueToNumber(context, JSObjectGetPropertyAtIndex(context, o, 0, &exception), &exception));
798 ASSERT(!exception);
799
800 JSStringRef functionBody;
801 JSObjectRef function;
802
803 exception = NULL;
804 functionBody = JSStringCreateWithUTF8CString("rreturn Array;");
805 JSStringRef line = JSStringCreateWithUTF8CString("line");
806 ASSERT(!JSObjectMakeFunction(context, NULL, 0, NULL, functionBody, NULL, 1, &exception));
807 ASSERT(JSValueIsObject(context, exception));
808 v = JSObjectGetProperty(context, JSValueToObject(context, exception, NULL), line, NULL);
809 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)
810 JSStringRelease(functionBody);
811 JSStringRelease(line);
812
813 exception = NULL;
814 functionBody = JSStringCreateWithUTF8CString("return Array;");
815 function = JSObjectMakeFunction(context, NULL, 0, NULL, functionBody, NULL, 1, &exception);
816 JSStringRelease(functionBody);
817 ASSERT(!exception);
818 ASSERT(JSObjectIsFunction(context, function));
819 v = JSObjectCallAsFunction(context, function, NULL, 0, NULL, NULL);
820 ASSERT(v);
821 ASSERT(JSValueIsEqual(context, v, arrayConstructor, NULL));
822
823 exception = NULL;
824 function = JSObjectMakeFunction(context, NULL, 0, NULL, jsEmptyIString, NULL, 0, &exception);
825 ASSERT(!exception);
826 v = JSObjectCallAsFunction(context, function, NULL, 0, NULL, &exception);
827 ASSERT(v && !exception);
828 ASSERT(JSValueIsUndefined(context, v));
829
830 exception = NULL;
831 v = NULL;
832 JSStringRef foo = JSStringCreateWithUTF8CString("foo");
833 JSStringRef argumentNames[] = { foo };
834 functionBody = JSStringCreateWithUTF8CString("return foo;");
835 function = JSObjectMakeFunction(context, foo, 1, argumentNames, functionBody, NULL, 1, &exception);
836 ASSERT(function && !exception);
837 JSValueRef arguments[] = { JSValueMakeNumber(context, 2) };
838 v = JSObjectCallAsFunction(context, function, NULL, 1, arguments, &exception);
839 JSStringRelease(foo);
840 JSStringRelease(functionBody);
841
842 string = JSValueToStringCopy(context, function, NULL);
843 assertEqualsAsUTF8String(JSValueMakeString(context, string), "function foo(foo) \n{\n return foo;\n}");
844 JSStringRelease(string);
845
846 JSStringRef print = JSStringCreateWithUTF8CString("print");
847 JSObjectRef printFunction = JSObjectMakeFunctionWithCallback(context, print, print_callAsFunction);
848 JSObjectSetProperty(context, globalObject, print, printFunction, kJSPropertyAttributeNone, NULL);
849 JSStringRelease(print);
850
851 ASSERT(!JSObjectSetPrivate(printFunction, (void*)1));
852 ASSERT(!JSObjectGetPrivate(printFunction));
853
854 JSStringRef myConstructorIString = JSStringCreateWithUTF8CString("MyConstructor");
855 JSObjectRef myConstructor = JSObjectMakeConstructor(context, NULL, myConstructor_callAsConstructor);
856 JSObjectSetProperty(context, globalObject, myConstructorIString, myConstructor, kJSPropertyAttributeNone, NULL);
857 JSStringRelease(myConstructorIString);
858
859 ASSERT(!JSObjectSetPrivate(myConstructor, (void*)1));
860 ASSERT(!JSObjectGetPrivate(myConstructor));
861
862 string = JSStringCreateWithUTF8CString("Derived");
863 JSObjectRef derivedConstructor = JSObjectMakeConstructor(context, Derived_class(context), NULL);
864 JSObjectSetProperty(context, globalObject, string, derivedConstructor, kJSPropertyAttributeNone, NULL);
865 JSStringRelease(string);
866
867 o = JSObjectMake(context, NULL, NULL);
868 JSObjectSetProperty(context, o, jsOneIString, JSValueMakeNumber(context, 1), kJSPropertyAttributeNone, NULL);
869 JSObjectSetProperty(context, o, jsCFIString, JSValueMakeNumber(context, 1), kJSPropertyAttributeDontEnum, NULL);
870 JSPropertyNameArrayRef nameArray = JSObjectCopyPropertyNames(context, o);
871 size_t expectedCount = JSPropertyNameArrayGetCount(nameArray);
872 size_t count;
873 for (count = 0; count < expectedCount; ++count)
874 JSPropertyNameArrayGetNameAtIndex(nameArray, count);
875 JSPropertyNameArrayRelease(nameArray);
876 ASSERT(count == 1); // jsCFString should not be enumerated
877
878 JSClassDefinition nullDefinition = kJSClassDefinitionEmpty;
879 nullDefinition.attributes = kJSClassAttributeNoAutomaticPrototype;
880 JSClassRef nullClass = JSClassCreate(&nullDefinition);
881 JSClassRelease(nullClass);
882
883 nullDefinition = kJSClassDefinitionEmpty;
884 nullClass = JSClassCreate(&nullDefinition);
885 JSClassRelease(nullClass);
886
887 functionBody = JSStringCreateWithUTF8CString("return this;");
888 function = JSObjectMakeFunction(context, NULL, 0, NULL, functionBody, NULL, 1, NULL);
889 JSStringRelease(functionBody);
890 v = JSObjectCallAsFunction(context, function, NULL, 0, NULL, NULL);
891 ASSERT(JSValueIsEqual(context, v, globalObject, NULL));
892 v = JSObjectCallAsFunction(context, function, o, 0, NULL, NULL);
893 ASSERT(JSValueIsEqual(context, v, o, NULL));
894
895 functionBody = JSStringCreateWithUTF8CString("return eval(\"this\");");
896 function = JSObjectMakeFunction(context, NULL, 0, NULL, functionBody, NULL, 1, NULL);
897 JSStringRelease(functionBody);
898 v = JSObjectCallAsFunction(context, function, NULL, 0, NULL, NULL);
899 ASSERT(JSValueIsEqual(context, v, globalObject, NULL));
900 v = JSObjectCallAsFunction(context, function, o, 0, NULL, NULL);
901 ASSERT(JSValueIsEqual(context, v, o, NULL));
902
903 JSStringRef script = JSStringCreateWithUTF8CString("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 script = JSStringCreateWithUTF8CString("eval(this);");
911 v = JSEvaluateScript(context, script, NULL, NULL, 0, NULL);
912 ASSERT(JSValueIsEqual(context, v, globalObject, NULL));
913 v = JSEvaluateScript(context, script, o, NULL, 0, NULL);
914 ASSERT(JSValueIsEqual(context, v, o, NULL));
915 JSStringRelease(script);
916
917 char* scriptUTF8 = createStringWithContentsOfFile(scriptPath);
918 if (!scriptUTF8)
919 printf("FAIL: Test script could not be loaded.\n");
920 else {
921 script = JSStringCreateWithUTF8CString(scriptUTF8);
922 result = JSEvaluateScript(context, script, NULL, NULL, 1, &exception);
923 if (JSValueIsUndefined(context, result))
924 printf("PASS: Test script executed successfully.\n");
925 else {
926 printf("FAIL: Test script returned unexpected value:\n");
927 JSStringRef exceptionIString = JSValueToStringCopy(context, exception, NULL);
928 CFStringRef exceptionCF = JSStringCopyCFString(kCFAllocatorDefault, exceptionIString);
929 CFShow(exceptionCF);
930 CFRelease(exceptionCF);
931 JSStringRelease(exceptionIString);
932 }
933 JSStringRelease(script);
934 free(scriptUTF8);
935 }
936
937 // Clear out local variables pointing at JSObjectRefs to allow their values to be collected
938 function = NULL;
939 v = NULL;
940 o = NULL;
941 globalObject = NULL;
942
943 JSStringRelease(jsEmptyIString);
944 JSStringRelease(jsOneIString);
945 JSStringRelease(jsCFIString);
946 JSStringRelease(jsCFEmptyIString);
947 JSStringRelease(jsCFIStringWithCharacters);
948 JSStringRelease(jsCFEmptyIStringWithCharacters);
949 JSStringRelease(goodSyntax);
950 JSStringRelease(badSyntax);
951
952 JSGlobalContextRelease(context);
953 JSGarbageCollect(context);
954 JSClassRelease(globalObjectClass);
955
956 printf("PASS: Program exited normally.\n");
957 return 0;
958}
959
960static char* createStringWithContentsOfFile(const char* fileName)
961{
962 char* buffer;
963
964 size_t buffer_size = 0;
965 size_t buffer_capacity = 1024;
966 buffer = (char*)malloc(buffer_capacity);
967
968 FILE* f = fopen(fileName, "r");
969 if (!f) {
970 fprintf(stderr, "Could not open file: %s\n", fileName);
971 return 0;
972 }
973
974 while (!feof(f) && !ferror(f)) {
975 buffer_size += fread(buffer + buffer_size, 1, buffer_capacity - buffer_size, f);
976 if (buffer_size == buffer_capacity) { // guarantees space for trailing '\0'
977 buffer_capacity *= 2;
978 buffer = (char*)realloc(buffer, buffer_capacity);
979 ASSERT(buffer);
980 }
981
982 ASSERT(buffer_size < buffer_capacity);
983 }
984 fclose(f);
985 buffer[buffer_size] = '\0';
986
987 return buffer;
988}
Note: See TracBrowser for help on using the repository browser.