2 * Copyright (C) 2015 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #import "Regress141809.h"
30 #import <objc/runtime.h>
32 #if JSC_OBJC_API_ENABLED
34 extern "C" void checkResult(NSString *description, bool passed);
35 extern "C" void JSSynchronousGarbageCollectForDebugging(JSContextRef);
37 @protocol TestClassAExports <JSExport>
40 @interface TestClassA : NSObject<TestClassAExports>
43 @implementation TestClassA
46 @protocol TestClassBExports <JSExport>
50 @interface TestClassB : TestClassA <TestClassBExports>
53 @implementation TestClassB
60 @protocol TestClassCExports <JSExport>
64 @interface TestClassC : TestClassB <TestClassCExports>
67 @implementation TestClassC
74 void runRegress141809()
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>
80 JSContext *context = [[JSContext alloc] init];
81 context[@"print"] = ^(NSString* str) {
85 [context evaluateScript:@"function dumpPrototypes(obj) { \
92 Object.getOwnPropertyNames(currObj).forEach(function(val, idx, array) { \
93 props += ((propIndex > 0 ? ', ' : '') + val); \
100 for (i = 0; i < objDepth; i++) \
102 str += '--> proto '; \
106 str += (' with ' + propIndex + ' props: ' + props); \
108 objChain += (str + '\\n'); \
110 currObj = Object.getPrototypeOf(currObj); \
112 return { objDepth: objDepth, objChain: objChain }; \
114 JSValue* dumpPrototypes = context[@"dumpPrototypes"];
116 JSValue* resultBeforeGC = nil;
118 TestClassC* obj = [[TestClassC alloc] init];
119 resultBeforeGC = [dumpPrototypes callWithArguments:@[obj]];
122 JSSynchronousGarbageCollectForDebugging([context JSGlobalContextRef]);
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]]);
134 #endif // JSC_OBJC_API_ENABLED