]> git.saurik.com Git - apple/javascriptcore.git/blob - API/tests/Regress141809.mm
JavaScriptCore-7600.1.4.17.5.tar.gz
[apple/javascriptcore.git] / API / tests / Regress141809.mm
1 /*
2 * Copyright (C) 2015 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #import "config.h"
27 #import "Regress141809.h"
28
29 #import <objc/objc.h>
30 #import <objc/runtime.h>
31
32 #if JSC_OBJC_API_ENABLED
33
34 extern "C" void checkResult(NSString *description, bool passed);
35 extern "C" void JSSynchronousGarbageCollectForDebugging(JSContextRef);
36
37 @protocol TestClassAExports <JSExport>
38 @end
39
40 @interface TestClassA : NSObject<TestClassAExports>
41 @end
42
43 @implementation TestClassA
44 @end
45
46 @protocol TestClassBExports <JSExport>
47 - (NSString *)name;
48 @end
49
50 @interface TestClassB : TestClassA <TestClassBExports>
51 @end
52
53 @implementation TestClassB
54 - (NSString *)name
55 {
56 return @"B";
57 }
58 @end
59
60 @protocol TestClassCExports <JSExport>
61 - (NSString *)name;
62 @end
63
64 @interface TestClassC : TestClassB <TestClassCExports>
65 @end
66
67 @implementation TestClassC
68 - (NSString *)name
69 {
70 return @"C";
71 }
72 @end
73
74 void runRegress141809()
75 {
76 // Test that the ObjC API can correctly re-construct the synthesized
77 // prototype and constructor of JS exported ObjC classes.
78 // See <https://webkit.org/b/141809>
79 @autoreleasepool {
80 JSContext *context = [[JSContext alloc] init];
81 context[@"print"] = ^(NSString* str) {
82 NSLog(@"%@", str);
83 };
84
85 [context evaluateScript:@"function dumpPrototypes(obj) { \
86 var objDepth = 0; \
87 var currObj = obj; \
88 var objChain = ''; \
89 do { \
90 var propIndex = 0; \
91 var props = ''; \
92 Object.getOwnPropertyNames(currObj).forEach(function(val, idx, array) { \
93 props += ((propIndex > 0 ? ', ' : '') + val); \
94 propIndex++; \
95 }); \
96 var str = ''; \
97 if (!objDepth) \
98 str += 'obj '; \
99 else { \
100 for (i = 0; i < objDepth; i++) \
101 str += ' '; \
102 str += '--> proto '; \
103 } \
104 str += currObj; \
105 if (props) \
106 str += (' with ' + propIndex + ' props: ' + props); \
107 print(str); \
108 objChain += (str + '\\n'); \
109 objDepth++; \
110 currObj = Object.getPrototypeOf(currObj); \
111 } while (currObj); \
112 return { objDepth: objDepth, objChain: objChain }; \
113 }"];
114 JSValue* dumpPrototypes = context[@"dumpPrototypes"];
115
116 JSValue* resultBeforeGC = nil;
117 @autoreleasepool {
118 TestClassC* obj = [[TestClassC alloc] init];
119 resultBeforeGC = [dumpPrototypes callWithArguments:@[obj]];
120 }
121
122 JSSynchronousGarbageCollectForDebugging([context JSGlobalContextRef]);
123
124 @autoreleasepool {
125 TestClassC* obj = [[TestClassC alloc] init];
126 JSValue* resultAfterGC = [dumpPrototypes callWithArguments:@[obj]];
127 checkResult(@"object and prototype chain depth is 5 deep", [resultAfterGC[@"objDepth"] toInt32] == 5);
128 checkResult(@"object and prototype chain depth before and after GC matches", [resultAfterGC[@"objDepth"] toInt32] == [resultBeforeGC[@"objDepth"] toInt32]);
129 checkResult(@"object and prototype chain before and after GC matches", [[resultAfterGC[@"objChain"] toString] isEqualToString:[resultBeforeGC[@"objChain"] toString]]);
130 }
131 }
132 }
133
134 #endif // JSC_OBJC_API_ENABLED