-
Notifications
You must be signed in to change notification settings - Fork 168
Expand file tree
/
Copy pathtestapi.c
More file actions
2074 lines (1734 loc) · 78 KB
/
testapi.c
File metadata and controls
2074 lines (1734 loc) · 78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2006 Apple Computer, Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "JavaScriptCore.h"
#include "JSBasePrivate.h"
#include "JSContextRefPrivate.h"
#include "JSObjectRefPrivate.h"
#include "JSScriptRefPrivate.h"
#include "JSStringRefPrivate.h"
#include <math.h>
#define ASSERT_DISABLED 0
#include <wtf/Assertions.h>
#if PLATFORM(MAC) || PLATFORM(IOS)
#include <mach/mach.h>
#include <mach/mach_time.h>
#include <sys/time.h>
#endif
#if OS(WINDOWS)
#include <windows.h>
#endif
#if JSC_OBJC_API_ENABLED
void testObjectiveCAPI(void);
#endif
extern void JSSynchronousGarbageCollectForDebugging(JSContextRef);
static JSGlobalContextRef context;
int failed;
static void assertEqualsAsBoolean(JSValueRef value, bool expectedValue)
{
if (JSValueToBoolean(context, value) != expectedValue) {
fprintf(stderr, "assertEqualsAsBoolean failed: %p, %d\n", value, expectedValue);
failed = 1;
}
}
static void assertEqualsAsNumber(JSValueRef value, double expectedValue)
{
double number = JSValueToNumber(context, value, NULL);
// FIXME <rdar://4668451> - On i386 the isnan(double) macro tries to map to the isnan(float) function,
// causing a build break with -Wshorten-64-to-32 enabled. The issue is known by the appropriate team.
// After that's resolved, we can remove these casts
if (number != expectedValue && !(isnan((float)number) && isnan((float)expectedValue))) {
fprintf(stderr, "assertEqualsAsNumber failed: %p, %lf\n", value, expectedValue);
failed = 1;
}
}
static void assertEqualsAsUTF8String(JSValueRef value, const char* expectedValue)
{
JSStringRef valueAsString = JSValueToStringCopy(context, value, NULL);
size_t jsSize = JSStringGetMaximumUTF8CStringSize(valueAsString);
char* jsBuffer = (char*)malloc(jsSize);
JSStringGetUTF8CString(valueAsString, jsBuffer, jsSize);
unsigned i;
for (i = 0; jsBuffer[i]; i++) {
if (jsBuffer[i] != expectedValue[i]) {
fprintf(stderr, "assertEqualsAsUTF8String failed at character %d: %c(%d) != %c(%d)\n", i, jsBuffer[i], jsBuffer[i], expectedValue[i], expectedValue[i]);
failed = 1;
}
}
if (jsSize < strlen(jsBuffer) + 1) {
fprintf(stderr, "assertEqualsAsUTF8String failed: jsSize was too small\n");
failed = 1;
}
free(jsBuffer);
JSStringRelease(valueAsString);
}
static void assertEqualsAsCharactersPtr(JSValueRef value, const char* expectedValue)
{
JSStringRef valueAsString = JSValueToStringCopy(context, value, NULL);
size_t jsLength = JSStringGetLength(valueAsString);
const JSChar* jsBuffer = JSStringGetCharactersPtr(valueAsString);
CFStringRef expectedValueAsCFString = CFStringCreateWithCString(kCFAllocatorDefault,
expectedValue,
kCFStringEncodingUTF8);
CFIndex cfLength = CFStringGetLength(expectedValueAsCFString);
UniChar* cfBuffer = (UniChar*)malloc(cfLength * sizeof(UniChar));
CFStringGetCharacters(expectedValueAsCFString, CFRangeMake(0, cfLength), cfBuffer);
CFRelease(expectedValueAsCFString);
if (memcmp(jsBuffer, cfBuffer, cfLength * sizeof(UniChar)) != 0) {
fprintf(stderr, "assertEqualsAsCharactersPtr failed: jsBuffer != cfBuffer\n");
failed = 1;
}
if (jsLength != (size_t)cfLength) {
fprintf(stderr, "assertEqualsAsCharactersPtr failed: jsLength(%ld) != cfLength(%ld)\n", jsLength, cfLength);
failed = 1;
}
free(cfBuffer);
JSStringRelease(valueAsString);
}
static bool timeZoneIsPST()
{
char timeZoneName[70];
struct tm gtm;
memset(>m, 0, sizeof(gtm));
strftime(timeZoneName, sizeof(timeZoneName), "%Z", >m);
return 0 == strcmp("PST", timeZoneName);
}
static JSValueRef jsGlobalValue; // non-stack value for testing JSValueProtect()
/* MyObject pseudo-class */
static bool MyObject_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName)
{
UNUSED_PARAM(context);
UNUSED_PARAM(object);
if (JSStringIsEqualToUTF8CString(propertyName, "alwaysOne")
|| JSStringIsEqualToUTF8CString(propertyName, "cantFind")
|| JSStringIsEqualToUTF8CString(propertyName, "throwOnGet")
|| JSStringIsEqualToUTF8CString(propertyName, "myPropertyName")
|| JSStringIsEqualToUTF8CString(propertyName, "hasPropertyLie")
|| JSStringIsEqualToUTF8CString(propertyName, "0")) {
return true;
}
return false;
}
static JSValueRef MyObject_getProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
{
UNUSED_PARAM(context);
UNUSED_PARAM(object);
if (JSStringIsEqualToUTF8CString(propertyName, "alwaysOne")) {
return JSValueMakeNumber(context, 1);
}
if (JSStringIsEqualToUTF8CString(propertyName, "myPropertyName")) {
return JSValueMakeNumber(context, 1);
}
if (JSStringIsEqualToUTF8CString(propertyName, "cantFind")) {
return JSValueMakeUndefined(context);
}
if (JSStringIsEqualToUTF8CString(propertyName, "hasPropertyLie")) {
return 0;
}
if (JSStringIsEqualToUTF8CString(propertyName, "throwOnGet")) {
return JSEvaluateScript(context, JSStringCreateWithUTF8CString("throw 'an exception'"), object, JSStringCreateWithUTF8CString("test script"), 1, exception);
}
if (JSStringIsEqualToUTF8CString(propertyName, "0")) {
*exception = JSValueMakeNumber(context, 1);
return JSValueMakeNumber(context, 1);
}
return JSValueMakeNull(context);
}
static bool MyObject_setProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef value, JSValueRef* exception)
{
UNUSED_PARAM(context);
UNUSED_PARAM(object);
UNUSED_PARAM(value);
UNUSED_PARAM(exception);
if (JSStringIsEqualToUTF8CString(propertyName, "cantSet"))
return true; // pretend we set the property in order to swallow it
if (JSStringIsEqualToUTF8CString(propertyName, "throwOnSet")) {
JSEvaluateScript(context, JSStringCreateWithUTF8CString("throw 'an exception'"), object, JSStringCreateWithUTF8CString("test script"), 1, exception);
}
return false;
}
static bool MyObject_deleteProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
{
UNUSED_PARAM(context);
UNUSED_PARAM(object);
if (JSStringIsEqualToUTF8CString(propertyName, "cantDelete"))
return true;
if (JSStringIsEqualToUTF8CString(propertyName, "throwOnDelete")) {
JSEvaluateScript(context, JSStringCreateWithUTF8CString("throw 'an exception'"), object, JSStringCreateWithUTF8CString("test script"), 1, exception);
return false;
}
return false;
}
static void MyObject_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef propertyNames)
{
UNUSED_PARAM(context);
UNUSED_PARAM(object);
JSStringRef propertyName;
propertyName = JSStringCreateWithUTF8CString("alwaysOne");
JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
JSStringRelease(propertyName);
propertyName = JSStringCreateWithUTF8CString("myPropertyName");
JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
JSStringRelease(propertyName);
}
static JSValueRef MyObject_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
{
UNUSED_PARAM(context);
UNUSED_PARAM(object);
UNUSED_PARAM(thisObject);
UNUSED_PARAM(exception);
if (argumentCount > 0 && JSValueIsString(context, arguments[0]) && JSStringIsEqualToUTF8CString(JSValueToStringCopy(context, arguments[0], 0), "throwOnCall")) {
JSEvaluateScript(context, JSStringCreateWithUTF8CString("throw 'an exception'"), object, JSStringCreateWithUTF8CString("test script"), 1, exception);
return JSValueMakeUndefined(context);
}
if (argumentCount > 0 && JSValueIsStrictEqual(context, arguments[0], JSValueMakeNumber(context, 0)))
return JSValueMakeNumber(context, 1);
return JSValueMakeUndefined(context);
}
static JSObjectRef MyObject_callAsConstructor(JSContextRef context, JSObjectRef object, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
{
UNUSED_PARAM(context);
UNUSED_PARAM(object);
if (argumentCount > 0 && JSValueIsString(context, arguments[0]) && JSStringIsEqualToUTF8CString(JSValueToStringCopy(context, arguments[0], 0), "throwOnConstruct")) {
JSEvaluateScript(context, JSStringCreateWithUTF8CString("throw 'an exception'"), object, JSStringCreateWithUTF8CString("test script"), 1, exception);
return object;
}
if (argumentCount > 0 && JSValueIsStrictEqual(context, arguments[0], JSValueMakeNumber(context, 0)))
return JSValueToObject(context, JSValueMakeNumber(context, 1), exception);
return JSValueToObject(context, JSValueMakeNumber(context, 0), exception);
}
static bool MyObject_hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef possibleValue, JSValueRef* exception)
{
UNUSED_PARAM(context);
UNUSED_PARAM(constructor);
if (JSValueIsString(context, possibleValue) && JSStringIsEqualToUTF8CString(JSValueToStringCopy(context, possibleValue, 0), "throwOnHasInstance")) {
JSEvaluateScript(context, JSStringCreateWithUTF8CString("throw 'an exception'"), constructor, JSStringCreateWithUTF8CString("test script"), 1, exception);
return false;
}
JSStringRef numberString = JSStringCreateWithUTF8CString("Number");
JSObjectRef numberConstructor = JSValueToObject(context, JSObjectGetProperty(context, JSContextGetGlobalObject(context), numberString, exception), exception);
JSStringRelease(numberString);
return JSValueIsInstanceOfConstructor(context, possibleValue, numberConstructor, exception);
}
static JSValueRef MyObject_convertToType(JSContextRef context, JSObjectRef object, JSType type, JSValueRef* exception)
{
UNUSED_PARAM(object);
UNUSED_PARAM(exception);
switch (type) {
case kJSTypeNumber:
return JSValueMakeNumber(context, 1);
case kJSTypeString:
{
JSStringRef string = JSStringCreateWithUTF8CString("MyObjectAsString");
JSValueRef result = JSValueMakeString(context, string);
JSStringRelease(string);
return result;
}
default:
break;
}
// string conversion -- forward to default object class
return JSValueMakeNull(context);
}
static JSValueRef MyObject_convertToTypeWrapper(JSContextRef context, JSObjectRef object, JSType type, JSValueRef* exception)
{
UNUSED_PARAM(context);
UNUSED_PARAM(object);
UNUSED_PARAM(type);
UNUSED_PARAM(exception);
// Forward to default object class
return 0;
}
static bool MyObject_set_nullGetForwardSet(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef value, JSValueRef* exception)
{
UNUSED_PARAM(ctx);
UNUSED_PARAM(object);
UNUSED_PARAM(propertyName);
UNUSED_PARAM(value);
UNUSED_PARAM(exception);
return false; // Forward to parent class.
}
static JSStaticValue evilStaticValues[] = {
{ "nullGetSet", 0, 0, kJSPropertyAttributeNone },
{ "nullGetForwardSet", 0, MyObject_set_nullGetForwardSet, kJSPropertyAttributeNone },
{ 0, 0, 0, 0 }
};
static JSStaticFunction evilStaticFunctions[] = {
{ "nullCall", 0, kJSPropertyAttributeNone },
{ 0, 0, 0 }
};
JSClassDefinition MyObject_definition = {
0,
kJSClassAttributeNone,
"MyObject",
NULL,
evilStaticValues,
evilStaticFunctions,
NULL,
NULL,
MyObject_hasProperty,
MyObject_getProperty,
MyObject_setProperty,
MyObject_deleteProperty,
MyObject_getPropertyNames,
MyObject_callAsFunction,
MyObject_callAsConstructor,
MyObject_hasInstance,
MyObject_convertToType,
};
JSClassDefinition MyObject_convertToTypeWrapperDefinition = {
0,
kJSClassAttributeNone,
"MyObject",
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
MyObject_convertToTypeWrapper,
};
JSClassDefinition MyObject_nullWrapperDefinition = {
0,
kJSClassAttributeNone,
"MyObject",
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
};
static JSClassRef MyObject_class(JSContextRef context)
{
UNUSED_PARAM(context);
static JSClassRef jsClass;
if (!jsClass) {
JSClassRef baseClass = JSClassCreate(&MyObject_definition);
MyObject_convertToTypeWrapperDefinition.parentClass = baseClass;
JSClassRef wrapperClass = JSClassCreate(&MyObject_convertToTypeWrapperDefinition);
MyObject_nullWrapperDefinition.parentClass = wrapperClass;
jsClass = JSClassCreate(&MyObject_nullWrapperDefinition);
}
return jsClass;
}
static JSValueRef PropertyCatchalls_getProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
{
UNUSED_PARAM(context);
UNUSED_PARAM(object);
UNUSED_PARAM(propertyName);
UNUSED_PARAM(exception);
if (JSStringIsEqualToUTF8CString(propertyName, "x")) {
static size_t count;
if (count++ < 5)
return NULL;
// Swallow all .x gets after 5, returning null.
return JSValueMakeNull(context);
}
if (JSStringIsEqualToUTF8CString(propertyName, "y")) {
static size_t count;
if (count++ < 5)
return NULL;
// Swallow all .y gets after 5, returning null.
return JSValueMakeNull(context);
}
if (JSStringIsEqualToUTF8CString(propertyName, "z")) {
static size_t count;
if (count++ < 5)
return NULL;
// Swallow all .y gets after 5, returning null.
return JSValueMakeNull(context);
}
return NULL;
}
static bool PropertyCatchalls_setProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef value, JSValueRef* exception)
{
UNUSED_PARAM(context);
UNUSED_PARAM(object);
UNUSED_PARAM(propertyName);
UNUSED_PARAM(value);
UNUSED_PARAM(exception);
if (JSStringIsEqualToUTF8CString(propertyName, "x")) {
static size_t count;
if (count++ < 5)
return false;
// Swallow all .x sets after 4.
return true;
}
if (JSStringIsEqualToUTF8CString(propertyName, "make_throw") || JSStringIsEqualToUTF8CString(propertyName, "0")) {
*exception = JSValueMakeNumber(context, 5);
return true;
}
return false;
}
static void PropertyCatchalls_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef propertyNames)
{
UNUSED_PARAM(context);
UNUSED_PARAM(object);
static size_t count;
static const char* numbers[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
// Provide a property of a different name every time.
JSStringRef propertyName = JSStringCreateWithUTF8CString(numbers[count++ % 10]);
JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
JSStringRelease(propertyName);
}
JSClassDefinition PropertyCatchalls_definition = {
0,
kJSClassAttributeNone,
"PropertyCatchalls",
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
PropertyCatchalls_getProperty,
PropertyCatchalls_setProperty,
NULL,
PropertyCatchalls_getPropertyNames,
NULL,
NULL,
NULL,
NULL,
};
static JSClassRef PropertyCatchalls_class(JSContextRef context)
{
UNUSED_PARAM(context);
static JSClassRef jsClass;
if (!jsClass)
jsClass = JSClassCreate(&PropertyCatchalls_definition);
return jsClass;
}
static bool EvilExceptionObject_hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef possibleValue, JSValueRef* exception)
{
UNUSED_PARAM(context);
UNUSED_PARAM(constructor);
JSStringRef hasInstanceName = JSStringCreateWithUTF8CString("hasInstance");
JSValueRef hasInstance = JSObjectGetProperty(context, constructor, hasInstanceName, exception);
JSStringRelease(hasInstanceName);
if (!hasInstance)
return false;
JSObjectRef function = JSValueToObject(context, hasInstance, exception);
JSValueRef result = JSObjectCallAsFunction(context, function, constructor, 1, &possibleValue, exception);
return result && JSValueToBoolean(context, result);
}
static JSValueRef EvilExceptionObject_convertToType(JSContextRef context, JSObjectRef object, JSType type, JSValueRef* exception)
{
UNUSED_PARAM(object);
UNUSED_PARAM(exception);
JSStringRef funcName;
switch (type) {
case kJSTypeNumber:
funcName = JSStringCreateWithUTF8CString("toNumber");
break;
case kJSTypeString:
funcName = JSStringCreateWithUTF8CString("toStringExplicit");
break;
default:
return JSValueMakeNull(context);
break;
}
JSValueRef func = JSObjectGetProperty(context, object, funcName, exception);
JSStringRelease(funcName);
JSObjectRef function = JSValueToObject(context, func, exception);
if (!function)
return JSValueMakeNull(context);
JSValueRef value = JSObjectCallAsFunction(context, function, object, 0, NULL, exception);
if (!value) {
JSStringRef errorString = JSStringCreateWithUTF8CString("convertToType failed");
JSValueRef errorStringRef = JSValueMakeString(context, errorString);
JSStringRelease(errorString);
return errorStringRef;
}
return value;
}
JSClassDefinition EvilExceptionObject_definition = {
0,
kJSClassAttributeNone,
"EvilExceptionObject",
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
EvilExceptionObject_hasInstance,
EvilExceptionObject_convertToType,
};
static JSClassRef EvilExceptionObject_class(JSContextRef context)
{
UNUSED_PARAM(context);
static JSClassRef jsClass;
if (!jsClass)
jsClass = JSClassCreate(&EvilExceptionObject_definition);
return jsClass;
}
JSClassDefinition EmptyObject_definition = {
0,
kJSClassAttributeNone,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
};
static JSClassRef EmptyObject_class(JSContextRef context)
{
UNUSED_PARAM(context);
static JSClassRef jsClass;
if (!jsClass)
jsClass = JSClassCreate(&EmptyObject_definition);
return jsClass;
}
static JSValueRef Base_get(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
{
UNUSED_PARAM(object);
UNUSED_PARAM(propertyName);
UNUSED_PARAM(exception);
return JSValueMakeNumber(ctx, 1); // distinguish base get form derived get
}
static bool Base_set(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef value, JSValueRef* exception)
{
UNUSED_PARAM(object);
UNUSED_PARAM(propertyName);
UNUSED_PARAM(value);
*exception = JSValueMakeNumber(ctx, 1); // distinguish base set from derived set
return true;
}
static JSValueRef Base_callAsFunction(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
{
UNUSED_PARAM(function);
UNUSED_PARAM(thisObject);
UNUSED_PARAM(argumentCount);
UNUSED_PARAM(arguments);
UNUSED_PARAM(exception);
return JSValueMakeNumber(ctx, 1); // distinguish base call from derived call
}
static JSValueRef Base_returnHardNull(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
{
UNUSED_PARAM(ctx);
UNUSED_PARAM(function);
UNUSED_PARAM(thisObject);
UNUSED_PARAM(argumentCount);
UNUSED_PARAM(arguments);
UNUSED_PARAM(exception);
return 0; // should convert to undefined!
}
static JSStaticFunction Base_staticFunctions[] = {
{ "baseProtoDup", NULL, kJSPropertyAttributeNone },
{ "baseProto", Base_callAsFunction, kJSPropertyAttributeNone },
{ "baseHardNull", Base_returnHardNull, kJSPropertyAttributeNone },
{ 0, 0, 0 }
};
static JSStaticValue Base_staticValues[] = {
{ "baseDup", Base_get, Base_set, kJSPropertyAttributeNone },
{ "baseOnly", Base_get, Base_set, kJSPropertyAttributeNone },
{ 0, 0, 0, 0 }
};
static bool TestInitializeFinalize;
static void Base_initialize(JSContextRef context, JSObjectRef object)
{
UNUSED_PARAM(context);
if (TestInitializeFinalize) {
ASSERT((void*)1 == JSObjectGetPrivate(object));
JSObjectSetPrivate(object, (void*)2);
}
}
static unsigned Base_didFinalize;
static void Base_finalize(JSObjectRef object)
{
UNUSED_PARAM(object);
if (TestInitializeFinalize) {
ASSERT((void*)4 == JSObjectGetPrivate(object));
Base_didFinalize = true;
}
}
static JSClassRef Base_class(JSContextRef context)
{
UNUSED_PARAM(context);
static JSClassRef jsClass;
if (!jsClass) {
JSClassDefinition definition = kJSClassDefinitionEmpty;
definition.staticValues = Base_staticValues;
definition.staticFunctions = Base_staticFunctions;
definition.initialize = Base_initialize;
definition.finalize = Base_finalize;
jsClass = JSClassCreate(&definition);
}
return jsClass;
}
static JSValueRef Derived_get(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
{
UNUSED_PARAM(object);
UNUSED_PARAM(propertyName);
UNUSED_PARAM(exception);
return JSValueMakeNumber(ctx, 2); // distinguish base get form derived get
}
static bool Derived_set(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef value, JSValueRef* exception)
{
UNUSED_PARAM(ctx);
UNUSED_PARAM(object);
UNUSED_PARAM(propertyName);
UNUSED_PARAM(value);
*exception = JSValueMakeNumber(ctx, 2); // distinguish base set from derived set
return true;
}
static JSValueRef Derived_callAsFunction(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
{
UNUSED_PARAM(function);
UNUSED_PARAM(thisObject);
UNUSED_PARAM(argumentCount);
UNUSED_PARAM(arguments);
UNUSED_PARAM(exception);
return JSValueMakeNumber(ctx, 2); // distinguish base call from derived call
}
static JSStaticFunction Derived_staticFunctions[] = {
{ "protoOnly", Derived_callAsFunction, kJSPropertyAttributeNone },
{ "protoDup", NULL, kJSPropertyAttributeNone },
{ "baseProtoDup", Derived_callAsFunction, kJSPropertyAttributeNone },
{ 0, 0, 0 }
};
static JSStaticValue Derived_staticValues[] = {
{ "derivedOnly", Derived_get, Derived_set, kJSPropertyAttributeNone },
{ "protoDup", Derived_get, Derived_set, kJSPropertyAttributeNone },
{ "baseDup", Derived_get, Derived_set, kJSPropertyAttributeNone },
{ 0, 0, 0, 0 }
};
static void Derived_initialize(JSContextRef context, JSObjectRef object)
{
UNUSED_PARAM(context);
if (TestInitializeFinalize) {
ASSERT((void*)2 == JSObjectGetPrivate(object));
JSObjectSetPrivate(object, (void*)3);
}
}
static void Derived_finalize(JSObjectRef object)
{
if (TestInitializeFinalize) {
ASSERT((void*)3 == JSObjectGetPrivate(object));
JSObjectSetPrivate(object, (void*)4);
}
}
static JSClassRef Derived_class(JSContextRef context)
{
static JSClassRef jsClass;
if (!jsClass) {
JSClassDefinition definition = kJSClassDefinitionEmpty;
definition.parentClass = Base_class(context);
definition.staticValues = Derived_staticValues;
definition.staticFunctions = Derived_staticFunctions;
definition.initialize = Derived_initialize;
definition.finalize = Derived_finalize;
jsClass = JSClassCreate(&definition);
}
return jsClass;
}
static JSClassRef Derived2_class(JSContextRef context)
{
static JSClassRef jsClass;
if (!jsClass) {
JSClassDefinition definition = kJSClassDefinitionEmpty;
definition.parentClass = Derived_class(context);
jsClass = JSClassCreate(&definition);
}
return jsClass;
}
static JSValueRef print_callAsFunction(JSContextRef ctx, JSObjectRef functionObject, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
{
UNUSED_PARAM(functionObject);
UNUSED_PARAM(thisObject);
UNUSED_PARAM(exception);
ASSERT(JSContextGetGlobalContext(ctx) == context);
if (argumentCount > 0) {
JSStringRef string = JSValueToStringCopy(ctx, arguments[0], NULL);
size_t sizeUTF8 = JSStringGetMaximumUTF8CStringSize(string);
char* stringUTF8 = (char*)malloc(sizeUTF8);
JSStringGetUTF8CString(string, stringUTF8, sizeUTF8);
printf("%s\n", stringUTF8);
free(stringUTF8);
JSStringRelease(string);
}
return JSValueMakeUndefined(ctx);
}
static JSObjectRef myConstructor_callAsConstructor(JSContextRef context, JSObjectRef constructorObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
{
UNUSED_PARAM(constructorObject);
UNUSED_PARAM(exception);
JSObjectRef result = JSObjectMake(context, NULL, NULL);
if (argumentCount > 0) {
JSStringRef value = JSStringCreateWithUTF8CString("value");
JSObjectSetProperty(context, result, value, arguments[0], kJSPropertyAttributeNone, NULL);
JSStringRelease(value);
}
return result;
}
static JSObjectRef myBadConstructor_callAsConstructor(JSContextRef context, JSObjectRef constructorObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
{
UNUSED_PARAM(context);
UNUSED_PARAM(constructorObject);
UNUSED_PARAM(argumentCount);
UNUSED_PARAM(arguments);
UNUSED_PARAM(exception);
return 0;
}
static void globalObject_initialize(JSContextRef context, JSObjectRef object)
{
UNUSED_PARAM(object);
// Ensure that an execution context is passed in
ASSERT(context);
// Ensure that the global object is set to the object that we were passed
JSObjectRef globalObject = JSContextGetGlobalObject(context);
ASSERT(globalObject);
ASSERT(object == globalObject);
// Ensure that the standard global properties have been set on the global object
JSStringRef array = JSStringCreateWithUTF8CString("Array");
JSObjectRef arrayConstructor = JSValueToObject(context, JSObjectGetProperty(context, globalObject, array, NULL), NULL);
JSStringRelease(array);
UNUSED_PARAM(arrayConstructor);
ASSERT(arrayConstructor);
}
static JSValueRef globalObject_get(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
{
UNUSED_PARAM(object);
UNUSED_PARAM(propertyName);
UNUSED_PARAM(exception);
return JSValueMakeNumber(ctx, 3);
}
static bool globalObject_set(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef value, JSValueRef* exception)
{
UNUSED_PARAM(object);
UNUSED_PARAM(propertyName);
UNUSED_PARAM(value);
*exception = JSValueMakeNumber(ctx, 3);
return true;
}
static JSValueRef globalObject_call(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
{
UNUSED_PARAM(function);
UNUSED_PARAM(thisObject);
UNUSED_PARAM(argumentCount);
UNUSED_PARAM(arguments);
UNUSED_PARAM(exception);
return JSValueMakeNumber(ctx, 3);
}
static JSValueRef functionGC(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
{
UNUSED_PARAM(function);
UNUSED_PARAM(thisObject);
UNUSED_PARAM(argumentCount);
UNUSED_PARAM(arguments);
UNUSED_PARAM(exception);
JSGarbageCollect(context);
return JSValueMakeUndefined(context);
}
static JSStaticValue globalObject_staticValues[] = {
{ "globalStaticValue", globalObject_get, globalObject_set, kJSPropertyAttributeNone },
{ 0, 0, 0, 0 }
};
static JSStaticFunction globalObject_staticFunctions[] = {
{ "globalStaticFunction", globalObject_call, kJSPropertyAttributeNone },
{ "gc", functionGC, kJSPropertyAttributeNone },
{ 0, 0, 0 }
};
static char* createStringWithContentsOfFile(const char* fileName);
static void testInitializeFinalize()
{
JSObjectRef o = JSObjectMake(context, Derived_class(context), (void*)1);
UNUSED_PARAM(o);
ASSERT(JSObjectGetPrivate(o) == (void*)3);
}
static JSValueRef jsNumberValue = NULL;
static JSObjectRef aHeapRef = NULL;
static void makeGlobalNumberValue(JSContextRef context) {
JSValueRef v = JSValueMakeNumber(context, 420);
JSValueProtect(context, v);
jsNumberValue = v;
v = NULL;
}
static bool assertTrue(bool value, const char* message)
{
if (!value) {
if (message)
fprintf(stderr, "assertTrue failed: '%s'\n", message);
else
fprintf(stderr, "assertTrue failed.\n");
failed = 1;
}
return value;
}
static bool checkForCycleInPrototypeChain()
{
bool result = true;
JSGlobalContextRef context = JSGlobalContextCreate(0);
JSObjectRef object1 = JSObjectMake(context, /* jsClass */ 0, /* data */ 0);
JSObjectRef object2 = JSObjectMake(context, /* jsClass */ 0, /* data */ 0);
JSObjectRef object3 = JSObjectMake(context, /* jsClass */ 0, /* data */ 0);
JSObjectSetPrototype(context, object1, JSValueMakeNull(context));
ASSERT(JSValueIsNull(context, JSObjectGetPrototype(context, object1)));