FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
WebKit/Source/JavaScriptCore/API/tests/testapi.c at main · WebKit/WebKit · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
WebKit
/
WebKit
Public
Notifications
You must be signed in to change notification settings
Fork
2.1k
Star
10.1k
Code
Pull requests
2.6k
Actions
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
WebKit
/
Source
/
JavaScriptCore
/
API
/
tests
/
testapi.c
Copy path
More file actions
More file actions
Latest commit
History
History
History
2465 lines (2053 loc) · 89.3 KB
Breadcrumbs
WebKit
/
Source
/
JavaScriptCore
/
API
/
tests
/
testapi.c
Copy path
File metadata and controls
2465 lines (2053 loc) · 89.3 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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-2025 Apple 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 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 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.
*/
#undef
ASSERT_ENABLED
#define
ASSERT_ENABLED
1
#include
"config.h"
#if
USE
(
CF
)
#include
"JavaScriptCore.h"
#else
#include
"JavaScript.h"
#endif
#include
"JSBasePrivate.h"
#include
"JSContextRefPrivate.h"
#include
"JSHeapFinalizerPrivate.h"
#include
"JSMarkingConstraintPrivate.h"
#include
"JSObjectRefPrivate.h"
#include
"JSScriptRefPrivate.h"
#include
"JSStringRefPrivate.h"
#include
"JSWeakPrivate.h"
#if
!
OS
(
WINDOWS
)
#include
<libgen.h>
#endif
#include
<limits.h>
#include
<math.h>
#include
<stdio.h>
#include
<stdlib.h>
#include
<string.h>
#include
<time.h>
#if
!
OS
(
WINDOWS
)
#include
<unistd.h>
#endif
#include
<wtf/Assertions.h>
#if
OS
(
WINDOWS
)
#include
<windows.h>
#endif
#include
"CompareAndSwapTest.h"
#include
"CustomGlobalObjectClassTest.h"
#include
"ExecutionTimeLimitTest.h"
#include
"FunctionOverridesTest.h"
#include
"FunctionToStringTests.h"
#include
"GlobalContextWithFinalizerTest.h"
#include
"JSONParseTest.h"
#include
"JSObjectGetProxyTargetTest.h"
#include
"MultithreadedMultiVMExecutionTest.h"
#include
"PingPongStackOverflowTest.h"
#include
"TemporalCoreTest.h"
#include
"TypedArrayCTest.h"
#include
"VMManagerStopTheWorldTest.h"
#if
defined(
JSC_SUPPORTS_SWIFT
)
&&
JSC_SUPPORTS_SWIFT
#include
"SwiftTestingHarness.h"
#endif
WTF_ALLOW_UNSAFE_BUFFER_USAGE_BEGIN
#if
JSC_OBJC_API_ENABLED
void
testObjectiveCAPI
(
const
char
*
);
#endif
void
initializeWTFForTesting
(
void
);
void
configureJSCForTesting
(
void
);
int
testLaunchJSCFromNonMainThread
(
const
char
*
filter
);
int
testCAPIViaCpp
(
const
char
*
filter
);
bool
assertTrue
(
bool
value
,
const
char
*
message
);
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
]);
fprintf
(
stderr
,
"value: %s\n"
,
jsBuffer
);
fprintf
(
stderr
,
"expectedValue: %s\n"
,
expectedValue
);
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
);
#if
USE
(
CF
)
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(%llu) != cfLength(%llu)\n"
, (
unsigned long long
)
jsLength
, (
unsigned long long
)
cfLength
);
failed
=
1
;
}
free
(
cfBuffer
);
#else
size_t
bufferSize
=
JSStringGetMaximumUTF8CStringSize
(
valueAsString
);
char
*
buffer
=
(
char
*
)
malloc
(
bufferSize
);
JSStringGetUTF8CString
(
valueAsString
,
buffer
,
bufferSize
);
if
(
strcmp
(
buffer
,
expectedValue
)) {
fprintf
(
stderr
,
"assertEqualsAsCharactersPtr failed: jsBuffer != cfBuffer\n"
);
failed
=
1
;
}
free
(
buffer
);
#endif
JSStringRelease
(
valueAsString
);
}
static
bool
timeZoneIsPST
(
void
)
{
char
timeZoneName
[
70
];
struct
tm
gtm
;
memset
(
&
gtm
,
0
,
sizeof
(
gtm
));
strftime
(
timeZoneName
,
sizeof
(
timeZoneName
),
"%Z"
,
&
gtm
);
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
throwException
(
JSContextRef
context
,
JSObjectRef
object
,
JSValueRef
*
exception
)
{
JSStringRef
script
=
JSStringCreateWithUTF8CString
(
"throw 'an exception'"
);
JSStringRef
sourceURL
=
JSStringCreateWithUTF8CString
(
"test script"
);
JSValueRef
result
=
JSEvaluateScript
(
context
,
script
,
object
,
sourceURL
,
1
,
exception
);
JSStringRelease
(
script
);
JSStringRelease
(
sourceURL
);
return
result
;
}
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
throwException
(
context
,
object
,
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"
)) {
throwException
(
context
,
object
,
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"
)) {
throwException
(
context
,
object
,
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
bool
isValueEqualToString
(
JSContextRef
context
,
JSValueRef
value
,
const
char
*
string
)
{
if
(!
JSValueIsString
(
context
,
value
))
return
false;
JSStringRef
valueString
=
JSValueToStringCopy
(
context
,
value
,
NULL
);
if
(!
valueString
)
return
false;
bool
isEqual
=
JSStringIsEqualToUTF8CString
(
valueString
,
string
);
JSStringRelease
(
valueString
);
return
isEqual
;
}
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
&&
isValueEqualToString
(
context
,
arguments
[
0
],
"throwOnCall"
)) {
throwException
(
context
,
object
,
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
&&
isValueEqualToString
(
context
,
arguments
[
0
],
"throwOnConstruct"
)) {
throwException
(
context
,
object
,
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
(
isValueEqualToString
(
context
,
possibleValue
,
"throwOnHasInstance"
)) {
throwException
(
context
,
constructor
,
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
NODELETE
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
NODELETE
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
const
JSStaticValue
evilStaticValues
[]
=
{
{
"nullGetSet"
,
0
,
0
,
kJSPropertyAttributeNone
},
{
"nullGetForwardSet"
,
0
,
MyObject_set_nullGetForwardSet
,
kJSPropertyAttributeNone
},
{
0
,
0
,
0
,
0
}
};
static
const
JSStaticFunction
evilStaticFunctions
[]
=
{
{
"nullCall"
,
0
,
kJSPropertyAttributeNone
},
{
0
,
0
,
0
}
};
static
const
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
,
};
static
const
JSClassDefinition
MyObject_convertToTypeWrapperDefinition
=
{
0
,
kJSClassAttributeNone
,
"MyObject"
,
NULL
,
NULL
,
NULL
,
NULL
,
NULL
,
NULL
,
NULL
,
NULL
,
NULL
,
NULL
,
NULL
,
NULL
,
NULL
,
MyObject_convertToTypeWrapper
,
};
static
const
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
) {
JSClassDefinition
classDefinition
=
MyObject_convertToTypeWrapperDefinition
;
JSClassDefinition
nullClassDefinition
=
MyObject_nullWrapperDefinition
;
JSClassRef
baseClass
=
JSClassCreate
(
&
MyObject_definition
);
classDefinition
.
parentClass
=
baseClass
;
JSClassRef
wrapperClass
=
JSClassCreate
(
&
classDefinition
);
nullClassDefinition
.
parentClass
=
wrapperClass
;
jsClass
=
JSClassCreate
(
&
nullClassDefinition
);
}
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
;
// Swallow all .x sets after 4.
return
count
++
>
4
;
}
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
);
}
static
const
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
);
}
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
;
}
static
const
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
;
}
static
const
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
NODELETE
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
const
JSStaticFunction
Base_staticFunctions
[]
=
{
{
"baseProtoDup"
,
NULL
,
kJSPropertyAttributeNone
},
{
"baseProto"
,
Base_callAsFunction
,
kJSPropertyAttributeNone
},
{
"baseHardNull"
,
Base_returnHardNull
,
kJSPropertyAttributeNone
},
{
0
,
0
,
0
}
};
static
const
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
const
JSStaticFunction
Derived_staticFunctions
[]
=
{
{
"protoOnly"
,
Derived_callAsFunction
,
kJSPropertyAttributeNone
},
{
"protoDup"
,
NULL
,
kJSPropertyAttributeNone
},
{
"baseProtoDup"
,
Derived_callAsFunction
,
kJSPropertyAttributeNone
},
{
0
,
0
,
0
}
};
static
const
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
NODELETE
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
);
JSObjectRef
globalObject
=
JSContextGetGlobalObject
(
context
);
ASSERT
(
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;
}
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL