Commit dabfd714 authored by weinig@apple.com's avatar weinig@apple.com
Browse files

[Cocoa] Add a little template magic to the inline API::Object storage to...

[Cocoa] Add a little template magic to the inline API::Object storage to remove the need for all the reinterpret_casts
https://bugs.webkit.org/show_bug.cgi?id=125024

Reviewed by Dan Bernstein.

Introduce API::ObjectStorage which wraps std::aligned_storage and adds some convenience functions
to reinterpret the data as the corresponding type. Deploy it everywhere we were previously using 
std::aligned_storage.

* Shared/Cocoa/WKNSArray.mm:
* Shared/Cocoa/WKNSDictionary.mm:
* Shared/Cocoa/WKObject.h:
* UIProcess/API/Cocoa/WKBackForwardList.mm:
* UIProcess/API/Cocoa/WKBackForwardListItem.mm:
* UIProcess/API/Cocoa/WKBrowsingContextController.mm:
* UIProcess/API/Cocoa/WKBrowsingContextGroup.mm:
* UIProcess/API/Cocoa/WKNavigationData.mm:
* UIProcess/API/Cocoa/WKProcessGroup.mm:


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@159881 268f45cc-cd09-0410-ab3c-d52691b4dbfc
parent 7dd3a0e9
2013-11-29 Sam Weinig <sam@webkit.org>
[Cocoa] Add a little template magic to the inline API::Object storage to remove the need for all the reinterpret_casts
https://bugs.webkit.org/show_bug.cgi?id=125024
Reviewed by Dan Bernstein.
Introduce API::ObjectStorage which wraps std::aligned_storage and adds some convenience functions
to reinterpret the data as the corresponding type. Deploy it everywhere we were previously using
std::aligned_storage.
* Shared/Cocoa/WKNSArray.mm:
* Shared/Cocoa/WKNSDictionary.mm:
* Shared/Cocoa/WKObject.h:
* UIProcess/API/Cocoa/WKBackForwardList.mm:
* UIProcess/API/Cocoa/WKBackForwardListItem.mm:
* UIProcess/API/Cocoa/WKBrowsingContextController.mm:
* UIProcess/API/Cocoa/WKBrowsingContextGroup.mm:
* UIProcess/API/Cocoa/WKNavigationData.mm:
* UIProcess/API/Cocoa/WKProcessGroup.mm:
2013-11-29 Sam Weinig <sam@webkit.org>
Fix crashing API tests.
......
......@@ -29,12 +29,12 @@
#if WK_API_ENABLED
@implementation WKNSArray {
std::aligned_storage<sizeof(API::Array), std::alignment_of<API::Array>::value>::type _array;
API::ObjectStorage<API::Array> _array;
}
- (void)dealloc
{
reinterpret_cast<API::Array*>(&_array)->~Array();
_array->~Array();
[super dealloc];
}
......@@ -43,12 +43,12 @@
- (NSUInteger)count
{
return reinterpret_cast<API::Array*>(&_array)->size();
return _array->size();
}
- (id)objectAtIndex:(NSUInteger)i
{
API::Object* object = reinterpret_cast<API::Array*>(&_array)->at(i);
API::Object* object = _array->at(i);
return object ? object->wrapper() : [NSNull null];
}
......@@ -63,7 +63,7 @@
- (API::Object&)_apiObject
{
return *reinterpret_cast<API::Array*>(&_array);
return *_array;
}
@end
......
......@@ -33,12 +33,12 @@
using namespace WebKit;
@implementation WKNSDictionary {
std::aligned_storage<sizeof(ImmutableDictionary), std::alignment_of<ImmutableDictionary>::value>::type _dictionary;
API::ObjectStorage<ImmutableDictionary> _dictionary;
}
- (void)dealloc
{
reinterpret_cast<ImmutableDictionary*>(&_dictionary)->~ImmutableDictionary();
_dictionary->~ImmutableDictionary();
[super dealloc];
}
......@@ -53,7 +53,7 @@ using namespace WebKit;
- (NSUInteger)count
{
return reinterpret_cast<ImmutableDictionary*>(&_dictionary)->size();
return _dictionary->size();
}
- (id)objectForKey:(id)key
......@@ -62,7 +62,7 @@ using namespace WebKit;
return nil;
bool exists;
API::Object* value = reinterpret_cast<ImmutableDictionary*>(&_dictionary)->get((NSString *)key, exists);
API::Object* value = _dictionary->get((NSString *)key, exists);
if (!exists)
return nil;
......@@ -71,17 +71,17 @@ using namespace WebKit;
- (NSEnumerator *)keyEnumerator
{
return [wrapper(*reinterpret_cast<ImmutableDictionary*>(&_dictionary)->keys()) objectEnumerator];
return [wrapper(*_dictionary->keys()) objectEnumerator];
}
#pragma mark NSCopying protocol implementation
- (id)copyWithZone:(NSZone *)zone
{
if (!reinterpret_cast<ImmutableDictionary*>(&_dictionary)->isMutable())
if (!_dictionary->isMutable())
return [self retain];
auto map = reinterpret_cast<ImmutableDictionary*>(&_dictionary)->map();
auto map = _dictionary->map();
return ImmutableDictionary::adopt(map).leakRef()->wrapper();
}
......@@ -89,7 +89,7 @@ using namespace WebKit;
- (API::Object&)_apiObject
{
return *reinterpret_cast<API::Object*>(&_dictionary);
return *_dictionary;
}
@end
......
......@@ -27,8 +27,20 @@
#if WK_API_ENABLED
#import <type_traits>
namespace API {
class Object;
template<typename T>
struct ObjectStorage {
T* get() { return reinterpret_cast<T*>(&data); }
T& operator*() { return *reinterpret_cast<T*>(&data); }
T* operator->() { return reinterpret_cast<T*>(&data); }
typename std::aligned_storage<sizeof(T), std::alignment_of<T>::value>::type data;
};
}
@protocol WKObject <NSObject>
......
......@@ -34,12 +34,12 @@
using namespace WebKit;
@implementation WKBackForwardList {
std::aligned_storage<sizeof(WebBackForwardList), std::alignment_of<WebBackForwardList>::value>::type _list;
API::ObjectStorage<WebBackForwardList> _list;
}
- (void)dealloc
{
reinterpret_cast<WebBackForwardList*>(&_list)->~WebBackForwardList();
_list->~WebBackForwardList();
[super dealloc];
}
......@@ -54,37 +54,37 @@ static WKBackForwardListItem *toWKBackForwardListItem(WebBackForwardListItem* it
- (WKBackForwardListItem *)currentItem
{
return toWKBackForwardListItem(reinterpret_cast<WebBackForwardList*>(&_list)->currentItem());
return toWKBackForwardListItem(_list->currentItem());
}
- (WKBackForwardListItem *)backItem
{
return toWKBackForwardListItem(reinterpret_cast<WebBackForwardList*>(&_list)->backItem());
return toWKBackForwardListItem(_list->backItem());
}
- (WKBackForwardListItem *)forwardItem
{
return toWKBackForwardListItem(reinterpret_cast<WebBackForwardList*>(&_list)->forwardItem());
return toWKBackForwardListItem(_list->forwardItem());
}
- (WKBackForwardListItem *)itemAtIndex:(NSInteger)index
{
return toWKBackForwardListItem(reinterpret_cast<WebBackForwardList*>(&_list)->itemAtIndex(index));
return toWKBackForwardListItem(_list->itemAtIndex(index));
}
- (NSUInteger)backListCount
{
return reinterpret_cast<WebBackForwardList*>(&_list)->backListCount();
return _list->backListCount();
}
- (NSUInteger)forwardListCount
{
return reinterpret_cast<WebBackForwardList*>(&_list)->forwardListCount();
return _list->forwardListCount();
}
- (NSArray *)backListWithLimit:(NSUInteger)limit
{
RefPtr<API::Array> list = reinterpret_cast<WebBackForwardList*>(&_list)->backListAsAPIArrayWithLimit(limit);
RefPtr<API::Array> list = _list->backListAsAPIArrayWithLimit(limit);
if (!list)
return nil;
......@@ -93,7 +93,7 @@ static WKBackForwardListItem *toWKBackForwardListItem(WebBackForwardListItem* it
- (NSArray *)forwardListWithLimit:(NSUInteger)limit
{
RefPtr<API::Array> list = reinterpret_cast<WebBackForwardList*>(&_list)->forwardListAsAPIArrayWithLimit(limit);
RefPtr<API::Array> list = _list->forwardListAsAPIArrayWithLimit(limit);
if (!list)
return nil;
......@@ -104,7 +104,7 @@ static WKBackForwardListItem *toWKBackForwardListItem(WebBackForwardListItem* it
- (API::Object&)_apiObject
{
return *reinterpret_cast<API::Object*>(&_list);
return *_list;
}
@end
......
......@@ -33,39 +33,39 @@
using namespace WebKit;
@implementation WKBackForwardListItem {
std::aligned_storage<sizeof(WebBackForwardListItem), std::alignment_of<WebBackForwardListItem>::value>::type _item;
API::ObjectStorage<WebBackForwardListItem> _item;
}
- (void)dealloc
{
reinterpret_cast<WebBackForwardListItem*>(&_item)->~WebBackForwardListItem();
_item->~WebBackForwardListItem();
[super dealloc];
}
- (NSURL *)URL
{
return [NSURL _web_URLWithWTFString:reinterpret_cast<WebBackForwardListItem*>(&_item)->url() relativeToURL:nil];
return [NSURL _web_URLWithWTFString:_item->url() relativeToURL:nil];
}
- (NSString *)title
{
if (!reinterpret_cast<WebBackForwardListItem*>(&_item)->title())
if (!_item->title())
return nil;
return reinterpret_cast<WebBackForwardListItem*>(&_item)->title();
return _item->title();
}
- (NSURL *)originalURL
{
return [NSURL _web_URLWithWTFString:reinterpret_cast<WebBackForwardListItem*>(&_item)->originalURL() relativeToURL:nil];
return [NSURL _web_URLWithWTFString:_item->originalURL() relativeToURL:nil];
}
#pragma mark WKObject protocol implementation
- (API::Object&)_apiObject
{
return *reinterpret_cast<API::Object*>(&_item);
return *_item;
}
@end
......@@ -74,7 +74,7 @@ using namespace WebKit;
- (WebKit::WebBackForwardListItem&)_item
{
return *reinterpret_cast<WebBackForwardListItem*>(&_item);
return *_item;
}
@end
......
......@@ -109,7 +109,7 @@ static NSString * const frameErrorKey = @"WKBrowsingContextFrameErrorKey";
@end
@implementation WKBrowsingContextController {
std::aligned_storage<sizeof(WebPageProxy), std::alignment_of<WebPageProxy>::value>::type _page;
API::ObjectStorage<WebPageProxy> _page;
std::unique_ptr<PageLoadStateObserver> _pageLoadStateObserver;
}
......@@ -118,8 +118,8 @@ static NSString * const frameErrorKey = @"WKBrowsingContextFrameErrorKey";
- (void)dealloc
{
reinterpret_cast<WebPageProxy*>(&_page)->pageLoadState().removeObserver(*_pageLoadStateObserver);
reinterpret_cast<WebPageProxy*>(&_page)->~WebPageProxy();
_page->pageLoadState().removeObserver(*_pageLoadStateObserver);
_page->~WebPageProxy();
[super dealloc];
}
......@@ -127,12 +127,12 @@ static NSString * const frameErrorKey = @"WKBrowsingContextFrameErrorKey";
- (void)_finishInitialization
{
_pageLoadStateObserver = std::make_unique<PageLoadStateObserver>(self);
reinterpret_cast<WebPageProxy*>(&_page)->pageLoadState().addObserver(*_pageLoadStateObserver);
_page->pageLoadState().addObserver(*_pageLoadStateObserver);
}
- (WKProcessGroup *)processGroup
{
WebContext* context = reinterpret_cast<WebPageProxy*>(&_page)->process()->context();
WebContext* context = _page->process()->context();
if (!context)
return nil;
return wrapper(*context);
......@@ -140,7 +140,7 @@ static NSString * const frameErrorKey = @"WKBrowsingContextFrameErrorKey";
- (WKBrowsingContextGroup *)browsingContextGroup
{
WebPageGroup* pageGroup = reinterpret_cast<WebPageProxy*>(&_page)->pageGroup();
WebPageGroup* pageGroup = _page->pageGroup();
if (!pageGroup)
return nil;
return wrapper(*pageGroup);
......@@ -148,7 +148,7 @@ static NSString * const frameErrorKey = @"WKBrowsingContextFrameErrorKey";
- (WKPageRef)_pageRef
{
return toAPI(reinterpret_cast<WebPageProxy*>(&_page));
return toAPI(_page.get());
}
#pragma mark Loading
......@@ -186,7 +186,7 @@ static NSString * const frameErrorKey = @"WKBrowsingContextFrameErrorKey";
if (userData)
wkUserData = ObjCObjectGraph::create(userData);
WKPageLoadURLRequestWithUserData(toAPI(reinterpret_cast<WebPageProxy*>(&_page)), wkRequest.get(), (WKTypeRef)wkUserData.get());
WKPageLoadURLRequestWithUserData(toAPI(_page.get()), wkRequest.get(), (WKTypeRef)wkUserData.get());
}
- (void)loadFileURL:(NSURL *)URL restrictToFilesWithin:(NSURL *)allowedDirectory
......@@ -206,7 +206,7 @@ static NSString * const frameErrorKey = @"WKBrowsingContextFrameErrorKey";
if (userData)
wkUserData = ObjCObjectGraph::create(userData);
WKPageLoadFileWithUserData(toAPI(reinterpret_cast<WebPageProxy*>(&_page)), wkURL.get(), wkAllowedDirectory.get(), (WKTypeRef)wkUserData.get());
WKPageLoadFileWithUserData(toAPI(_page.get()), wkURL.get(), wkAllowedDirectory.get(), (WKTypeRef)wkUserData.get());
}
- (void)loadHTMLString:(NSString *)HTMLString baseURL:(NSURL *)baseURL
......@@ -228,7 +228,7 @@ static NSString * const frameErrorKey = @"WKBrowsingContextFrameErrorKey";
if (userData)
wkUserData = ObjCObjectGraph::create(userData);
WKPageLoadHTMLStringWithUserData(toAPI(reinterpret_cast<WebPageProxy*>(&_page)), wkHTMLString.get(), wkBaseURL.get(), (WKTypeRef)wkUserData.get());
WKPageLoadHTMLStringWithUserData(toAPI(_page.get()), wkHTMLString.get(), wkBaseURL.get(), (WKTypeRef)wkUserData.get());
}
- (void)loadAlternateHTMLString:(NSString *)string baseURL:(NSURL *)baseURL forUnreachableURL:(NSURL *)unreachableURL
......@@ -239,7 +239,7 @@ static NSString * const frameErrorKey = @"WKBrowsingContextFrameErrorKey";
CString unreachableURLString;
getURLBytes((CFURLRef)unreachableURL, unreachableURLString);
reinterpret_cast<WebPageProxy*>(&_page)->loadAlternateHTMLString(string, String::fromUTF8(baseURLString), String::fromUTF8(unreachableURLString));
_page->loadAlternateHTMLString(string, String::fromUTF8(baseURLString), String::fromUTF8(unreachableURLString));
}
- (void)loadData:(NSData *)data MIMEType:(NSString *)MIMEType textEncodingName:(NSString *)encodingName baseURL:(NSURL *)baseURL
......@@ -276,54 +276,54 @@ static void releaseNSData(unsigned char*, const void* data)
if (userData)
wkUserData = ObjCObjectGraph::create(userData);
WKPageLoadDataWithUserData(toAPI(reinterpret_cast<WebPageProxy*>(&_page)), toAPI(wkData.get()), wkMIMEType.get(), wkEncodingName.get(), wkBaseURL.get(), (WKTypeRef)wkUserData.get());
WKPageLoadDataWithUserData(toAPI(_page.get()), toAPI(wkData.get()), wkMIMEType.get(), wkEncodingName.get(), wkBaseURL.get(), (WKTypeRef)wkUserData.get());
}
- (void)stopLoading
{
WKPageStopLoading(toAPI(reinterpret_cast<WebPageProxy*>(&_page)));
WKPageStopLoading(toAPI(_page.get()));
}
- (void)reload
{
WKPageReload(toAPI(reinterpret_cast<WebPageProxy*>(&_page)));
WKPageReload(toAPI(_page.get()));
}
- (void)reloadFromOrigin
{
WKPageReloadFromOrigin(toAPI(reinterpret_cast<WebPageProxy*>(&_page)));
WKPageReloadFromOrigin(toAPI(_page.get()));
}
#pragma mark Back/Forward
- (void)goForward
{
WKPageGoForward(toAPI(reinterpret_cast<WebPageProxy*>(&_page)));
WKPageGoForward(toAPI(_page.get()));
}
- (BOOL)canGoForward
{
return WKPageCanGoForward(toAPI(reinterpret_cast<WebPageProxy*>(&_page)));
return WKPageCanGoForward(toAPI(_page.get()));
}
- (void)goBack
{
WKPageGoBack(toAPI(reinterpret_cast<WebPageProxy*>(&_page)));
WKPageGoBack(toAPI(_page.get()));
}
- (BOOL)canGoBack
{
return WKPageCanGoBack(toAPI(reinterpret_cast<WebPageProxy*>(&_page)));
return WKPageCanGoBack(toAPI(_page.get()));
}
- (void)goToBackForwardListItem:(WKBackForwardListItem *)item
{
reinterpret_cast<WebPageProxy*>(&_page)->goToBackForwardItem(&item._item);
_page->goToBackForwardItem(&item._item);
}
- (WKBackForwardList *)backForwardList
{
WebBackForwardList* list = reinterpret_cast<WebPageProxy*>(&_page)->backForwardList();
WebBackForwardList* list = _page->backForwardList();
if (!list)
return nil;
......@@ -334,56 +334,56 @@ static void releaseNSData(unsigned char*, const void* data)
- (NSURL *)activeURL
{
return autoreleased(WKPageCopyActiveURL(toAPI(reinterpret_cast<WebPageProxy*>(&_page))));
return autoreleased(WKPageCopyActiveURL(toAPI(_page.get())));
}
- (NSURL *)provisionalURL
{
return autoreleased(WKPageCopyProvisionalURL(toAPI(reinterpret_cast<WebPageProxy*>(&_page))));
return autoreleased(WKPageCopyProvisionalURL(toAPI(_page.get())));
}
- (NSURL *)committedURL
{
return autoreleased(WKPageCopyCommittedURL(toAPI(reinterpret_cast<WebPageProxy*>(&_page))));
return autoreleased(WKPageCopyCommittedURL(toAPI(_page.get())));
}
- (NSURL *)unreachableURL
{
return [NSURL _web_URLWithWTFString:reinterpret_cast<WebPageProxy*>(&_page)->pageLoadState().unreachableURL() relativeToURL:nil];
return [NSURL _web_URLWithWTFString:_page->pageLoadState().unreachableURL() relativeToURL:nil];
}
- (double)estimatedProgress
{
return reinterpret_cast<WebPageProxy*>(&_page)->estimatedProgress();
return _page->estimatedProgress();
}
#pragma mark Active Document Introspection
- (NSString *)title
{
return autoreleased(WKPageCopyTitle(toAPI(reinterpret_cast<WebPageProxy*>(&_page))));
return autoreleased(WKPageCopyTitle(toAPI(_page.get())));
}
#pragma mark Zoom
- (CGFloat)textZoom
{
return WKPageGetTextZoomFactor(toAPI(reinterpret_cast<WebPageProxy*>(&_page)));
return WKPageGetTextZoomFactor(toAPI(_page.get()));
}
- (void)setTextZoom:(CGFloat)textZoom
{
return WKPageSetTextZoomFactor(toAPI(reinterpret_cast<WebPageProxy*>(&_page)), textZoom);
return WKPageSetTextZoomFactor(toAPI(_page.get()), textZoom);
}
- (CGFloat)pageZoom
{
return WKPageGetPageZoomFactor(toAPI(reinterpret_cast<WebPageProxy*>(&_page)));
return WKPageGetPageZoomFactor(toAPI(_page.get()));
}
- (void)setPageZoom:(CGFloat)pageZoom
{
return WKPageSetPageZoomFactor(toAPI(reinterpret_cast<WebPageProxy*>(&_page)), pageZoom);
return WKPageSetPageZoomFactor(toAPI(_page.get()), pageZoom);
}
static NSError *createErrorWithRecoveryAttempter(WKErrorRef wkError, WKFrameRef frame, WKBrowsingContextController *browsingContext)
......@@ -617,9 +617,9 @@ static void setUpPagePolicyClient(WKBrowsingContextController *browsingContext,
{
_loadDelegate = loadDelegate;
if (_loadDelegate)
setUpPageLoaderClient(self, toAPI(reinterpret_cast<WebPageProxy*>(&_page)));
setUpPageLoaderClient(self, toAPI(_page.get()));
else
WKPageSetPageLoaderClient(toAPI(reinterpret_cast<WebPageProxy*>(&_page)), nullptr);;
WKPageSetPageLoaderClient(toAPI(_page.get()), nullptr);;
}
- (id <WKBrowsingContextPolicyDelegate>)policyDelegate
......@@ -631,9 +631,9 @@ static void setUpPagePolicyClient(WKBrowsingContextController *browsingContext,
{
_policyDelegate = policyDelegate;
if (_policyDelegate)
setUpPagePolicyClient(self, toAPI(reinterpret_cast<WebPageProxy*>(&_page)));
setUpPagePolicyClient(self, toAPI(_page.get()));
else
WKPageSetPagePolicyClient(toAPI(reinterpret_cast<WebPageProxy*>(&_page)), nullptr);;
WKPageSetPagePolicyClient(toAPI(_page.get()), nullptr);;
}
+ (NSMutableSet *)customSchemes
......@@ -699,12 +699,12 @@ static void setUpPagePolicyClient(WKBrowsingContextController *browsingContext,
return;
}
WKPageSetPaginationMode(toAPI(reinterpret_cast<WebPageProxy*>(&_page)), mode);
WKPageSetPaginationMode(toAPI(_page.get()), mode);
}
- (WKBrowsingContextPaginationMode)paginationMode
{
switch (WKPageGetPaginationMode(toAPI(reinterpret_cast<WebPageProxy*>(&_page)))) {
switch (WKPageGetPaginationMode(toAPI(_page.get()))) {
case kWKPaginationModeUnpaginated:
return WKPaginationModeUnpaginated;
case kWKPaginationModeLeftToRight:
......@@ -723,42 +723,42 @@ static void setUpPagePolicyClient(WKBrowsingContextController *browsingContext,
- (void)setPaginationBehavesLikeColumns:(BOOL)behavesLikeColumns
{
WKPageSetPaginationBehavesLikeColumns(toAPI(reinterpret_cast<WebPageProxy*>(&_page)), behavesLikeColumns);
WKPageSetPaginationBehavesLikeColumns(toAPI(_page.get()), behavesLikeColumns);
}
- (BOOL)paginationBehavesLikeColumns
{
return WKPageGetPaginationBehavesLikeColumns(toAPI(reinterpret_cast<WebPageProxy*>(&_page)));
return WKPageGetPaginationBehavesLikeColumns(toAPI(_page.get()));
}
- (void)setPageLength:(CGFloat)pageLength
{
WKPageSetPageLength(toAPI(reinterpret_cast<WebPageProxy*>(&_page)), pageLength);
WKPageSetPageLength(toAPI(_page.get()), pageLength);
}
- (CGFloat)pageLength
{
return WKPageGetPageLength(toAPI(reinterpret_cast<WebPageProxy*>(&_page)));
return WKPageGetPageLength(toAPI(_page.get()));
}
- (void)setGapBetweenPages:(CGFloat)gapBetweenPages
{
WKPageSetGapBetweenPages(toAPI(reinterpret_cast<WebPageProxy*>(&_page)), gapBetweenPages);
WKPageSetGapBetweenPages(toAPI(_page.get()), gapBetweenPages);
}
- (CGFloat)gapBetweenPages
{
return WKPageGetGapBetweenPages(toAPI(reinterpret_cast<WebPageProxy*>(&_page)));
return WKPageGetGapBetweenPages(toAPI(_page.get()));
}
- (NSUInteger)pageCount
{