on master help sign in
Use Word64/uint64_t instead of CChar to represent bools
784ef0fAli Abrar
jsaddle-wkwebview/cbits-uikit/AppDelegate.m+33−31
@@ -1,9 +1,10 @@
1#import "AppDelegate.h"
1#import "AppDelegate.h"
2#import "ViewController.h"
2#import "ViewController.h"
3#import <UserNotifications/UserNotifications.h>
3#import <UserNotifications/UserNotifications.h>
4+#import <stdint.h>
4
5
5−extern void handlerIO(HsStablePtr);
6+extern void callIO(HsStablePtr);
6−extern void handlerCString(const char * _Nonnull, HsStablePtr);
7+extern void callWithCString(const char * _Nonnull, HsStablePtr);
7
8
8@interface AppDelegate ()
9@interface AppDelegate ()
9
10
@@ -18,12 +19,12 @@ HsStablePtr global_applicationDidEnterBackground = 0;
18HsStablePtr global_applicationWillEnterForeground = 0;
19HsStablePtr global_applicationWillEnterForeground = 0;
19HsStablePtr global_applicationWillTerminate = 0;
20HsStablePtr global_applicationWillTerminate = 0;
20HsStablePtr global_applicationSignificantTimeChange = 0;
21HsStablePtr global_applicationSignificantTimeChange = 0;
21−BOOL global_requestAuthorizationWithOptions = NO;
22+uint64_t global_requestAuthorizationWithOptions = 0;
22−BOOL global_requestAuthorizationOptionBadge = NO;
23+uint64_t global_requestAuthorizationOptionBadge = 0;
23−BOOL global_requestAuthorizationOptionSound = NO;
24+uint64_t global_requestAuthorizationOptionSound = 0;
24−BOOL global_requestAuthorizationOptionAlert = NO;
25+uint64_t global_requestAuthorizationOptionAlert = 0;
25−BOOL global_requestAuthorizationOptionCarPlay = NO;
26+uint64_t global_requestAuthorizationOptionCarPlay = 0;
26−BOOL global_registerForRemoteNotifications = NO;
27+uint64_t global_registerForRemoteNotifications = 0;
27HsStablePtr global_didRegisterForRemoteNotificationsWithDeviceToken = 0;
28HsStablePtr global_didRegisterForRemoteNotificationsWithDeviceToken = 0;
28HsStablePtr global_didFailToRegisterForRemoteNotificationsWithError = 0;
29HsStablePtr global_didFailToRegisterForRemoteNotificationsWithError = 0;
29
30
@@ -31,7 +32,7 @@ HsStablePtr global_didFailToRegisterForRemoteNotificationsWithError = 0;
31
32
32- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
33- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
33 // Tells the delegate that the launch process has begun but that state restoration has not yet occurred.
34 // 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);
35 return YES;
36 return YES;
36}
37}
37
38
@@ -43,78 +44,79 @@ HsStablePtr global_didFailToRegisterForRemoteNotificationsWithError = 0;
43 self.window.backgroundColor = [UIColor whiteColor];
44 self.window.backgroundColor = [UIColor whiteColor];
44 [self.window makeKeyAndVisible];
45 [self.window makeKeyAndVisible];
45 UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
46 UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
47+ center.delegate = self;
46 if (global_requestAuthorizationWithOptions) {
48 if (global_requestAuthorizationWithOptions) {
47 UNAuthorizationOptions options = (UNAuthorizationOptions)0;
49 UNAuthorizationOptions options = (UNAuthorizationOptions)0;
48 if (global_requestAuthorizationOptionBadge) {
50 if (global_requestAuthorizationOptionBadge) {
49− options = options + UNAuthorizationOptionBadge;
51+ options = options | UNAuthorizationOptionBadge;
50 }
52 }
51 if (global_requestAuthorizationOptionSound) {
53 if (global_requestAuthorizationOptionSound) {
52− options = options + UNAuthorizationOptionSound;
54+ options = options | UNAuthorizationOptionSound;
53 }
55 }
54 if (global_requestAuthorizationOptionAlert) {
56 if (global_requestAuthorizationOptionAlert) {
55− options = options + UNAuthorizationOptionAlert;
57+ options = options | UNAuthorizationOptionAlert;
56 }
58 }
57 if (global_requestAuthorizationOptionCarPlay) {
59 if (global_requestAuthorizationOptionCarPlay) {
58− options = options + UNAuthorizationOptionCarPlay;
60+ options = options | UNAuthorizationOptionCarPlay;
59 }
61 }
60 [center requestAuthorizationWithOptions:(options)
62 [center requestAuthorizationWithOptions:(options)
61 completionHandler:^(BOOL granted, NSError * _Nullable error) {
63 completionHandler:^(BOOL granted, NSError * _Nullable error) {
62 // Handler used to alter application behavior based on types of notifications authorized
64 // Handler used to alter application behavior based on types of notifications authorized
63 }];
65 }];
66+ if (global_registerForRemoteNotifications) {
67+ [application registerForRemoteNotifications];
68+ }
64 }
69 }
65− if (global_registerForRemoteNotifications) {
66− [application registerForRemoteNotifications];
67− }
68− handlerIO(global_didFinishLaunchingWithOptions);
70+ callIO(global_didFinishLaunchingWithOptions);
69 return YES;
71 return YES;
70}
72}
71
73
72- (void)applicationWillResignActive:(UIApplication *)application {
74- (void)applicationWillResignActive:(UIApplication *)application {
73 // 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.
75 // 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.
74 // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
76 // 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);
76}
78}
77
79
78
80
79- (void)applicationDidEnterBackground:(UIApplication *)application {
81- (void)applicationDidEnterBackground:(UIApplication *)application {
80 // 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.
82 // 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.
81 // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
83 // 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);
83}
85}
84
86
85
87
86- (void)applicationWillEnterForeground:(UIApplication *)application {
88- (void)applicationWillEnterForeground:(UIApplication *)application {
87 // 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.
89 // 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);
89}
91}
90
92
91
93
92- (void)applicationDidBecomeActive:(UIApplication *)application {
94- (void)applicationDidBecomeActive:(UIApplication *)application {
93 // 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.
95 // 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);
95}
97}
96
98
97
99
98- (void)applicationWillTerminate:(UIApplication *)application {
100- (void)applicationWillTerminate:(UIApplication *)application {
99 // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
101 // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
100− handlerIO(global_applicationWillTerminate);
102+ callIO(global_applicationWillTerminate);
101}
103}
102
104
103- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
105- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
104 // Tells the delegate that the app successfully registered with Apple Push Notification service (APNs).
106 // Tells the delegate that the app successfully registered with Apple Push Notification service (APNs).
105 NSString *deviceTokenString = [deviceToken base64EncodedStringWithOptions: 0];
107 NSString *deviceTokenString = [deviceToken base64EncodedStringWithOptions: 0];
106− handlerCString([deviceTokenString UTF8String], global_didRegisterForRemoteNotificationsWithDeviceToken);
108+ callWithCString([deviceTokenString UTF8String], global_didRegisterForRemoteNotificationsWithDeviceToken);
107}
109}
108
110
109- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
111- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
110 // Sent to the delegate when Apple Push Notification service cannot successfully complete the registration process.
112 // Sent to the delegate when Apple Push Notification service cannot successfully complete the registration process.
111 NSString *errorString = [error localizedDescription];
113 NSString *errorString = [error localizedDescription];
112− handlerCString([errorString UTF8String], global_didFailToRegisterForRemoteNotificationsWithError);
114+ callWithCString([errorString UTF8String], global_didFailToRegisterForRemoteNotificationsWithError);
113}
115}
114
116
115- (void)applicationSignificantTimeChange:(UIApplication *)application {
117- (void)applicationSignificantTimeChange:(UIApplication *)application {
116 // Tells the delegate when there is a significant change in the time.
118 // Tells the delegate when there is a significant change in the time.
117− handlerIO(global_applicationSignificantTimeChange);
119+ callIO(global_applicationSignificantTimeChange);
118}
120}
119
121
120@end
122@end
@@ -129,12 +131,12 @@ void runInWKWebView(HsStablePtr handler,
129 HsStablePtr hs_applicationWillEnterForeground,
131 HsStablePtr hs_applicationWillEnterForeground,
130 HsStablePtr hs_applicationWillTerminate,
132 HsStablePtr hs_applicationWillTerminate,
131 HsStablePtr hs_applicationSignificantTimeChange,
133 HsStablePtr hs_applicationSignificantTimeChange,
132− const BOOL hs_requestAuthorizationWithOptions,
134+ const uint64_t hs_requestAuthorizationWithOptions,
133− const BOOL hs_requestAuthorizationOptionBadge,
135+ const uint64_t hs_requestAuthorizationOptionBadge,
134− const BOOL hs_requestAuthorizationOptionSound,
136+ const uint64_t hs_requestAuthorizationOptionSound,
135− const BOOL hs_requestAuthorizationOptionAlert,
137+ const uint64_t hs_requestAuthorizationOptionAlert,
136− const BOOL hs_requestAuthorizationOptionCarPlay,
138+ const uint64_t hs_requestAuthorizationOptionCarPlay,
137− const BOOL hs_registerForRemoteNotifications,
139+ const uint64_t hs_registerForRemoteNotifications,
138 HsStablePtr hs_didRegisterForRemoteNotificationsWithDeviceToken,
140 HsStablePtr hs_didRegisterForRemoteNotificationsWithDeviceToken,
139 HsStablePtr hs_didFailToRegisterForRemoteNotificationsWithError) {
141 HsStablePtr hs_didFailToRegisterForRemoteNotificationsWithError) {
140 @autoreleasepool {
142 @autoreleasepool {
jsaddle-wkwebview/cbits-uikit/ViewController.m+2−2
@@ -1,6 +1,6 @@
1#import "ViewController.h"
1#import "ViewController.h"
2
2
3−extern void withWebView(WKWebView *, HsStablePtr);
3+extern void callWithWebView(WKWebView *, HsStablePtr);
4
4
5@interface ViewController ()
5@interface ViewController ()
6
6
@@ -27,7 +27,7 @@ extern void withWebView(WKWebView *, HsStablePtr);
27
27
28- (void)viewDidLoad {
28- (void)viewDidLoad {
29 [super viewDidLoad];
29 [super viewDidLoad];
30− withWebView(_webView, _handler);
30+ callWithWebView(_webView, _handler);
31}
31}
32
32
33
33
jsaddle-wkwebview/src-ghc/Language/Javascript/JSaddle/WKWebView.hs+16−15
@@ -14,10 +14,11 @@ import Data.ByteString (ByteString)
14import Data.Default (Default, def)
14import Data.Default (Default, def)
15import Data.Set (Set)
15import Data.Set (Set)
16import qualified Data.Set as Set
16import qualified Data.Set as Set
17+import Data.Word
18+import Foreign.Marshal.Utils (fromBool)
17−import Language.Javascript.JSaddle.WKWebView.Internal (jsaddleMain, jsaddleMainFile, WKWebView(..), mainBundleResourcePath, boolToCChar)
19+import Language.Javascript.JSaddle.WKWebView.Internal (jsaddleMain, jsaddleMainFile, WKWebView(..), mainBundleResourcePath)
18import System.Environment (getProgName)
20import System.Environment (getProgName)
19−import Foreign.C.String (CString, withCString, peekCString)
21+import Foreign.C.String (CString, withCString)
20−import Foreign.C.Types
21import Foreign.StablePtr (StablePtr, newStablePtr)
22import Foreign.StablePtr (StablePtr, newStablePtr)
22import Language.Javascript.JSaddle (JSM)
23import Language.Javascript.JSaddle (JSM)
23
24
@@ -32,12 +33,12 @@ foreign import ccall runInWKWebView
32 -> StablePtr (IO ()) -- applicationWillEnterForeground
33 -> StablePtr (IO ()) -- applicationWillEnterForeground
33 -> StablePtr (IO ()) -- applicationWillTerminate
34 -> StablePtr (IO ()) -- applicationWillTerminate
34 -> StablePtr (IO ()) -- applicationSignificantTimeChange
35 -> StablePtr (IO ()) -- applicationSignificantTimeChange
35− -> CChar -- whether to run requestAuthorizationWithOptions
36+ -> Word64 -- whether to run requestAuthorizationWithOptions
36− -> CChar -- Ask for Badge authorization
37+ -> Word64 -- Ask for Badge authorization
37− -> CChar -- Ask for Sound authorization
38+ -> Word64 -- Ask for Sound authorization
38− -> CChar -- Ask for Alert authorization
39+ -> Word64 -- Ask for Alert authorization
39− -> CChar -- Ask for CarPlay authorization
40+ -> Word64 -- Ask for CarPlay authorization
40− -> CChar -- registerForRemoteNotifications
41+ -> Word64 -- registerForRemoteNotifications
41 -> StablePtr (CString -> IO ()) -- didRegisterForRemoteNotificationsWithDeviceToken
42 -> StablePtr (CString -> IO ()) -- didRegisterForRemoteNotificationsWithDeviceToken
42 -> StablePtr (CString -> IO ()) -- didFailToRegisterForRemoteNotificationsWithError
43 -> StablePtr (CString -> IO ()) -- didFailToRegisterForRemoteNotificationsWithError
43 -> IO ()
44 -> IO ()
@@ -142,11 +143,11 @@ run' mUrl cfg f = do
142 applicationWillEnterForeground
143 applicationWillEnterForeground
143 applicationWillTerminate
144 applicationWillTerminate
144 applicationSignificantTimeChange
145 applicationSignificantTimeChange
145− (boolToCChar requestAuthorizationWithOptions)
146+ (fromBool requestAuthorizationWithOptions)
146− (boolToCChar $ Set.member AuthorizationOption_Badge authorizationOptions)
147+ (fromBool $ Set.member AuthorizationOption_Badge authorizationOptions)
147− (boolToCChar $ Set.member AuthorizationOption_Sound authorizationOptions)
148+ (fromBool $ Set.member AuthorizationOption_Sound authorizationOptions)
148− (boolToCChar $ Set.member AuthorizationOption_Alert authorizationOptions)
149+ (fromBool $ Set.member AuthorizationOption_Alert authorizationOptions)
149− (boolToCChar $ Set.member AuthorizationOption_CarPlay authorizationOptions)
150+ (fromBool $ Set.member AuthorizationOption_CarPlay authorizationOptions)
150− (boolToCChar registerForRemoteNotifications)
151+ (fromBool registerForRemoteNotifications)
151 didRegisterForRemoteNotificationsWithDeviceToken
152 didRegisterForRemoteNotificationsWithDeviceToken
152 didFailToRegisterForRemoteNotificationsWithError
153 didFailToRegisterForRemoteNotificationsWithError
jsaddle-wkwebview/src/Language/Javascript/JSaddle/WKWebView/Internal.hs+9−18
@@ -4,8 +4,6 @@ module Language.Javascript.JSaddle.WKWebView.Internal
4 , jsaddleMainFile
4 , jsaddleMainFile
5 , WKWebView(..)
5 , WKWebView(..)
6 , mainBundleResourcePath
6 , mainBundleResourcePath
7− , boolFromCChar
8− , boolToCChar
9 ) where
7 ) where
10
8
11import Control.Monad (void, join)
9import Control.Monad (void, join)
@@ -19,7 +17,6 @@ import Data.ByteString.Lazy (ByteString, toStrict, fromStrict)
19import Data.Aeson (encode, decode)
17import Data.Aeson (encode, decode)
20
18
21import Foreign.C.String (CString)
19import Foreign.C.String (CString)
22−import Foreign.C.Types (CChar (..))
23import Foreign.Ptr (Ptr, nullPtr)
20import Foreign.Ptr (Ptr, nullPtr)
24import Foreign.StablePtr (StablePtr, newStablePtr, deRefStablePtr)
21import Foreign.StablePtr (StablePtr, newStablePtr, deRefStablePtr)
25
22
@@ -31,9 +28,9 @@ newtype WKWebView = WKWebView (Ptr WKWebView)
31
28
32foreign export ccall jsaddleStart :: StablePtr (IO ()) -> IO ()
29foreign export ccall jsaddleStart :: StablePtr (IO ()) -> IO ()
33foreign export ccall jsaddleResult :: StablePtr (Results -> IO ()) -> CString -> IO ()
30foreign export ccall jsaddleResult :: StablePtr (Results -> IO ()) -> CString -> IO ()
34−foreign export ccall withWebView :: WKWebView -> StablePtr (WKWebView -> IO ()) -> IO ()
31+foreign export ccall callWithWebView :: WKWebView -> StablePtr (WKWebView -> IO ()) -> IO ()
35−foreign export ccall handlerCString :: CString -> StablePtr (CString -> IO ()) -> IO ()
32+foreign export ccall callWithCString :: CString -> StablePtr (CString -> IO ()) -> IO ()
36−foreign export ccall handlerIO :: StablePtr (IO ()) -> IO ()
33+foreign export ccall callIO :: StablePtr (IO ()) -> IO ()
37foreign import ccall addJSaddleHandler :: WKWebView -> StablePtr (IO ()) -> StablePtr (Results -> IO ()) -> IO ()
34foreign import ccall addJSaddleHandler :: WKWebView -> StablePtr (IO ()) -> StablePtr (Results -> IO ()) -> IO ()
38foreign import ccall loadHTMLString :: WKWebView -> CString -> IO ()
35foreign import ccall loadHTMLString :: WKWebView -> CString -> IO ()
39foreign import ccall loadBundleFile :: WKWebView -> CString -> CString -> IO ()
36foreign import ccall loadBundleFile :: WKWebView -> CString -> CString -> IO ()
@@ -85,27 +82,21 @@ jsaddleResult ptrHandler s = do
85 Nothing -> error $ "jsaddle WebSocket decode failed : " <> show result
82 Nothing -> error $ "jsaddle WebSocket decode failed : " <> show result
86 Just r -> processResult r
83 Just r -> processResult r
87
84
88−withWebView :: WKWebView -> StablePtr (WKWebView -> IO ()) -> IO ()
85+callWithWebView :: WKWebView -> StablePtr (WKWebView -> IO ()) -> IO ()
89−withWebView webView ptrF = do
86+callWithWebView webView ptrF = do
90 f <- deRefStablePtr ptrF
87 f <- deRefStablePtr ptrF
91 f webView
88 f webView
92
89
93−handlerCString :: CString -> StablePtr (CString -> IO ()) -> IO ()
90+callWithCString :: CString -> StablePtr (CString -> IO ()) -> IO ()
94−handlerCString c fptr = do
91+callWithCString c fptr = do
95 f <- deRefStablePtr fptr
92 f <- deRefStablePtr fptr
96 f c
93 f c
97
94
98−handlerIO :: StablePtr (IO ()) -> IO ()
95+callIO :: StablePtr (IO ()) -> IO ()
99−handlerIO ptr = do
96+callIO ptr = do
100 f <- deRefStablePtr ptr
97 f <- deRefStablePtr ptr
101 f
98 f
102
99
103−boolToCChar :: Bool -> CChar
104−boolToCChar a = if a then 1 else 0
105−
106−boolFromCChar :: CChar -> Bool
107−boolFromCChar a = a /= 0
108−
109jsaddleJs :: ByteString
100jsaddleJs :: ByteString
110jsaddleJs = ghcjsHelpers <> "\
101jsaddleJs = ghcjsHelpers <> "\
111 \runJSaddleBatch = (function() {\n\
102 \runJSaddleBatch = (function() {\n\