on master help sign in
Use Word64/uint64_t instead of CChar to represent bools
commit784ef0f706a1e791720cbfb5f267cca6a589b5f9 authorAli Abrar <aliabrar@gmail.com> dateMon 8 May 2017, 19:25 (9 years ago) parenta5bfacf
4 files changed+60−66
jsaddle-wkwebview/cbits-uikit/AppDelegate.m+33−31 jsaddle-wkwebview/cbits-uikit/ViewController.m+2−2 jsaddle-wkwebview/src-ghc/Language/Javascript/JSaddle/WKWebView.hs+16−15 jsaddle-wkwebview/src/Language/Javascript/JSaddle/WKWebView/Internal.hs+9−18
jsaddle-wkwebview/cbits-uikit/AppDelegate.m+33−31
@@ -1,9 +1,10 @@
11#import "AppDelegate.h"
22#import "ViewController.h"
33#import <UserNotifications/UserNotifications.h>
4+#import <stdint.h>
45
5−extern void handlerIO(HsStablePtr);
6−extern void handlerCString(const char * _Nonnull, HsStablePtr);
6+extern void callIO(HsStablePtr);
7+extern void callWithCString(const char * _Nonnull, HsStablePtr);
78
89@interface AppDelegate ()
910
@@ -18,12 +19,12 @@ HsStablePtr global_applicationDidEnterBackground = 0;
1819HsStablePtr global_applicationWillEnterForeground = 0;
1920HsStablePtr global_applicationWillTerminate = 0;
2021HsStablePtr global_applicationSignificantTimeChange = 0;
21−BOOL global_requestAuthorizationWithOptions = NO;
22−BOOL global_requestAuthorizationOptionBadge = NO;
23−BOOL global_requestAuthorizationOptionSound = NO;
24−BOOL global_requestAuthorizationOptionAlert = NO;
25−BOOL global_requestAuthorizationOptionCarPlay = NO;
26−BOOL global_registerForRemoteNotifications = NO;
22+uint64_t global_requestAuthorizationWithOptions = 0;
23+uint64_t global_requestAuthorizationOptionBadge = 0;
24+uint64_t global_requestAuthorizationOptionSound = 0;
25+uint64_t global_requestAuthorizationOptionAlert = 0;
26+uint64_t global_requestAuthorizationOptionCarPlay = 0;
27+uint64_t global_registerForRemoteNotifications = 0;
2728HsStablePtr global_didRegisterForRemoteNotificationsWithDeviceToken = 0;
2829HsStablePtr global_didFailToRegisterForRemoteNotificationsWithError = 0;
2930
@@ -31,7 +32,7 @@ HsStablePtr global_didFailToRegisterForRemoteNotificationsWithError = 0;
3132
3233- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
3334 // Tells the delegate that the launch process has begun but that state restoration has not yet occurred.
34− handlerIO(global_willFinishLaunchingWithOptions);
35+ callIO(global_willFinishLaunchingWithOptions);
3536 return YES;
3637}
3738
@@ -43,78 +44,79 @@ HsStablePtr global_didFailToRegisterForRemoteNotificationsWithError = 0;
4344 self.window.backgroundColor = [UIColor whiteColor];
4445 [self.window makeKeyAndVisible];
4546 UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
47+ center.delegate = self;
4648 if (global_requestAuthorizationWithOptions) {
4749 UNAuthorizationOptions options = (UNAuthorizationOptions)0;
4850 if (global_requestAuthorizationOptionBadge) {
49− options = options + UNAuthorizationOptionBadge;
51+ options = options | UNAuthorizationOptionBadge;
5052 }
5153 if (global_requestAuthorizationOptionSound) {
52− options = options + UNAuthorizationOptionSound;
54+ options = options | UNAuthorizationOptionSound;
5355 }
5456 if (global_requestAuthorizationOptionAlert) {
55− options = options + UNAuthorizationOptionAlert;
57+ options = options | UNAuthorizationOptionAlert;
5658 }
5759 if (global_requestAuthorizationOptionCarPlay) {
58− options = options + UNAuthorizationOptionCarPlay;
60+ options = options | UNAuthorizationOptionCarPlay;
5961 }
6062 [center requestAuthorizationWithOptions:(options)
6163 completionHandler:^(BOOL granted, NSError * _Nullable error) {
6264 // Handler used to alter application behavior based on types of notifications authorized
6365 }];
66+ if (global_registerForRemoteNotifications) {
67+ [application registerForRemoteNotifications];
68+ }
6469 }
65− if (global_registerForRemoteNotifications) {
66− [application registerForRemoteNotifications];
67− }
68− handlerIO(global_didFinishLaunchingWithOptions);
70+ callIO(global_didFinishLaunchingWithOptions);
6971 return YES;
7072}
7173
7274- (void)applicationWillResignActive:(UIApplication *)application {
7375 // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
7476 // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
75− handlerIO(global_applicationWillResignActive);
77+ callIO(global_applicationWillResignActive);
7678}
7779
7880
7981- (void)applicationDidEnterBackground:(UIApplication *)application {
8082 // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
8183 // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
82− handlerIO(global_applicationDidEnterBackground);
84+ callIO(global_applicationDidEnterBackground);
8385}
8486
8587
8688- (void)applicationWillEnterForeground:(UIApplication *)application {
8789 // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
88− handlerIO(global_applicationWillEnterForeground);
90+ callIO(global_applicationWillEnterForeground);
8991}
9092
9193
9294- (void)applicationDidBecomeActive:(UIApplication *)application {
9395 // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
94− handlerIO(global_applicationDidBecomeActive);
96+ callIO(global_applicationDidBecomeActive);
9597}
9698
9799
98100- (void)applicationWillTerminate:(UIApplication *)application {
99101 // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
100− handlerIO(global_applicationWillTerminate);
102+ callIO(global_applicationWillTerminate);
101103}
102104
103105- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
104106 // Tells the delegate that the app successfully registered with Apple Push Notification service (APNs).
105107 NSString *deviceTokenString = [deviceToken base64EncodedStringWithOptions: 0];
106− handlerCString([deviceTokenString UTF8String], global_didRegisterForRemoteNotificationsWithDeviceToken);
108+ callWithCString([deviceTokenString UTF8String], global_didRegisterForRemoteNotificationsWithDeviceToken);
107109}
108110
109111- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
110112 // Sent to the delegate when Apple Push Notification service cannot successfully complete the registration process.
111113 NSString *errorString = [error localizedDescription];
112− handlerCString([errorString UTF8String], global_didFailToRegisterForRemoteNotificationsWithError);
114+ callWithCString([errorString UTF8String], global_didFailToRegisterForRemoteNotificationsWithError);
113115}
114116
115117- (void)applicationSignificantTimeChange:(UIApplication *)application {
116118 // Tells the delegate when there is a significant change in the time.
117− handlerIO(global_applicationSignificantTimeChange);
119+ callIO(global_applicationSignificantTimeChange);
118120}
119121
120122@end
@@ -129,12 +131,12 @@ void runInWKWebView(HsStablePtr handler,
129131 HsStablePtr hs_applicationWillEnterForeground,
130132 HsStablePtr hs_applicationWillTerminate,
131133 HsStablePtr hs_applicationSignificantTimeChange,
132− const BOOL hs_requestAuthorizationWithOptions,
133− const BOOL hs_requestAuthorizationOptionBadge,
134− const BOOL hs_requestAuthorizationOptionSound,
135− const BOOL hs_requestAuthorizationOptionAlert,
136− const BOOL hs_requestAuthorizationOptionCarPlay,
137− const BOOL hs_registerForRemoteNotifications,
134+ const uint64_t hs_requestAuthorizationWithOptions,
135+ const uint64_t hs_requestAuthorizationOptionBadge,
136+ const uint64_t hs_requestAuthorizationOptionSound,
137+ const uint64_t hs_requestAuthorizationOptionAlert,
138+ const uint64_t hs_requestAuthorizationOptionCarPlay,
139+ const uint64_t hs_registerForRemoteNotifications,
138140 HsStablePtr hs_didRegisterForRemoteNotificationsWithDeviceToken,
139141 HsStablePtr hs_didFailToRegisterForRemoteNotificationsWithError) {
140142 @autoreleasepool {
jsaddle-wkwebview/cbits-uikit/ViewController.m+2−2
@@ -1,6 +1,6 @@
11#import "ViewController.h"
22
3−extern void withWebView(WKWebView *, HsStablePtr);
3+extern void callWithWebView(WKWebView *, HsStablePtr);
44
55@interface ViewController ()
66
@@ -27,7 +27,7 @@ extern void withWebView(WKWebView *, HsStablePtr);
2727
2828- (void)viewDidLoad {
2929 [super viewDidLoad];
30− withWebView(_webView, _handler);
30+ callWithWebView(_webView, _handler);
3131}
3232
3333
jsaddle-wkwebview/src-ghc/Language/Javascript/JSaddle/WKWebView.hs+16−15
@@ -14,10 +14,11 @@ import Data.ByteString (ByteString)
1414import Data.Default (Default, def)
1515import Data.Set (Set)
1616import qualified Data.Set as Set
17−import Language.Javascript.JSaddle.WKWebView.Internal (jsaddleMain, jsaddleMainFile, WKWebView(..), mainBundleResourcePath, boolToCChar)
17+import Data.Word
18+import Foreign.Marshal.Utils (fromBool)
19+import Language.Javascript.JSaddle.WKWebView.Internal (jsaddleMain, jsaddleMainFile, WKWebView(..), mainBundleResourcePath)
1820import System.Environment (getProgName)
19−import Foreign.C.String (CString, withCString, peekCString)
20−import Foreign.C.Types
21+import Foreign.C.String (CString, withCString)
2122import Foreign.StablePtr (StablePtr, newStablePtr)
2223import Language.Javascript.JSaddle (JSM)
2324
@@ -32,12 +33,12 @@ foreign import ccall runInWKWebView
3233 -> StablePtr (IO ()) -- applicationWillEnterForeground
3334 -> StablePtr (IO ()) -- applicationWillTerminate
3435 -> StablePtr (IO ()) -- applicationSignificantTimeChange
35− -> CChar -- whether to run requestAuthorizationWithOptions
36− -> CChar -- Ask for Badge authorization
37− -> CChar -- Ask for Sound authorization
38− -> CChar -- Ask for Alert authorization
39− -> CChar -- Ask for CarPlay authorization
40− -> CChar -- registerForRemoteNotifications
36+ -> Word64 -- whether to run requestAuthorizationWithOptions
37+ -> Word64 -- Ask for Badge authorization
38+ -> Word64 -- Ask for Sound authorization
39+ -> Word64 -- Ask for Alert authorization
40+ -> Word64 -- Ask for CarPlay authorization
41+ -> Word64 -- registerForRemoteNotifications
4142 -> StablePtr (CString -> IO ()) -- didRegisterForRemoteNotificationsWithDeviceToken
4243 -> StablePtr (CString -> IO ()) -- didFailToRegisterForRemoteNotificationsWithError
4344 -> IO ()
@@ -142,11 +143,11 @@ run' mUrl cfg f = do
142143 applicationWillEnterForeground
143144 applicationWillTerminate
144145 applicationSignificantTimeChange
145− (boolToCChar requestAuthorizationWithOptions)
146− (boolToCChar $ Set.member AuthorizationOption_Badge authorizationOptions)
147− (boolToCChar $ Set.member AuthorizationOption_Sound authorizationOptions)
148− (boolToCChar $ Set.member AuthorizationOption_Alert authorizationOptions)
149− (boolToCChar $ Set.member AuthorizationOption_CarPlay authorizationOptions)
150− (boolToCChar registerForRemoteNotifications)
146+ (fromBool requestAuthorizationWithOptions)
147+ (fromBool $ Set.member AuthorizationOption_Badge authorizationOptions)
148+ (fromBool $ Set.member AuthorizationOption_Sound authorizationOptions)
149+ (fromBool $ Set.member AuthorizationOption_Alert authorizationOptions)
150+ (fromBool $ Set.member AuthorizationOption_CarPlay authorizationOptions)
151+ (fromBool registerForRemoteNotifications)
151152 didRegisterForRemoteNotificationsWithDeviceToken
152153 didFailToRegisterForRemoteNotificationsWithError
jsaddle-wkwebview/src/Language/Javascript/JSaddle/WKWebView/Internal.hs+9−18
@@ -4,8 +4,6 @@ module Language.Javascript.JSaddle.WKWebView.Internal
44 , jsaddleMainFile
55 , WKWebView(..)
66 , mainBundleResourcePath
7− , boolFromCChar
8− , boolToCChar
97 ) where
108
119import Control.Monad (void, join)
@@ -19,7 +17,6 @@ import Data.ByteString.Lazy (ByteString, toStrict, fromStrict)
1917import Data.Aeson (encode, decode)
2018
2119import Foreign.C.String (CString)
22−import Foreign.C.Types (CChar (..))
2320import Foreign.Ptr (Ptr, nullPtr)
2421import Foreign.StablePtr (StablePtr, newStablePtr, deRefStablePtr)
2522
@@ -31,9 +28,9 @@ newtype WKWebView = WKWebView (Ptr WKWebView)
3128
3229foreign export ccall jsaddleStart :: StablePtr (IO ()) -> IO ()
3330foreign export ccall jsaddleResult :: StablePtr (Results -> IO ()) -> CString -> IO ()
34−foreign export ccall withWebView :: WKWebView -> StablePtr (WKWebView -> IO ()) -> IO ()
35−foreign export ccall handlerCString :: CString -> StablePtr (CString -> IO ()) -> IO ()
36−foreign export ccall handlerIO :: StablePtr (IO ()) -> IO ()
31+foreign export ccall callWithWebView :: WKWebView -> StablePtr (WKWebView -> IO ()) -> IO ()
32+foreign export ccall callWithCString :: CString -> StablePtr (CString -> IO ()) -> IO ()
33+foreign export ccall callIO :: StablePtr (IO ()) -> IO ()
3734foreign import ccall addJSaddleHandler :: WKWebView -> StablePtr (IO ()) -> StablePtr (Results -> IO ()) -> IO ()
3835foreign import ccall loadHTMLString :: WKWebView -> CString -> IO ()
3936foreign import ccall loadBundleFile :: WKWebView -> CString -> CString -> IO ()
@@ -85,27 +82,21 @@ jsaddleResult ptrHandler s = do
8582 Nothing -> error $ "jsaddle WebSocket decode failed : " <> show result
8683 Just r -> processResult r
8784
88−withWebView :: WKWebView -> StablePtr (WKWebView -> IO ()) -> IO ()
89−withWebView webView ptrF = do
85+callWithWebView :: WKWebView -> StablePtr (WKWebView -> IO ()) -> IO ()
86+callWithWebView webView ptrF = do
9087 f <- deRefStablePtr ptrF
9188 f webView
9289
93−handlerCString :: CString -> StablePtr (CString -> IO ()) -> IO ()
94−handlerCString c fptr = do
90+callWithCString :: CString -> StablePtr (CString -> IO ()) -> IO ()
91+callWithCString c fptr = do
9592 f <- deRefStablePtr fptr
9693 f c
9794
98−handlerIO :: StablePtr (IO ()) -> IO ()
99−handlerIO ptr = do
95+callIO :: StablePtr (IO ()) -> IO ()
96+callIO ptr = do
10097 f <- deRefStablePtr ptr
10198 f
10299
103−boolToCChar :: Bool -> CChar
104−boolToCChar a = if a then 1 else 0
105−
106−boolFromCChar :: CChar -> Bool
107−boolFromCChar a = a /= 0
108−
109100jsaddleJs :: ByteString
110101jsaddleJs = ghcjsHelpers <> "\
111102 \runJSaddleBatch = (function() {\n\