]> gitweb.factorcode.org Git - factor.git/blob - vm/os-macosx.mm
vm: speed up nano-count primitive on Mac OS X
[factor.git] / vm / os-macosx.mm
1 #import <Cocoa/Cocoa.h>
2
3 #include <mach/mach_time.h>
4 #include "master.hpp"
5
6 namespace factor
7 {
8
9 void factor_vm::c_to_factor_toplevel(cell quot)
10 {
11         for(;;)
12         {
13 NS_DURING
14                 c_to_factor(quot);
15                 NS_VOIDRETURN;
16 NS_HANDLER
17                 ctx->push(allot_alien(false_object,(cell)localException));
18                 quot = special_objects[OBJ_COCOA_EXCEPTION];
19                 if(!tagged<object>(quot).type_p(QUOTATION_TYPE))
20                 {
21                         /* No Cocoa exception handler was registered, so
22                         basis/cocoa/ is not loaded. So we pass the exception
23                         along. */
24                         [localException raise];
25                 }
26 NS_ENDHANDLER
27         }
28 }
29
30 void early_init(void)
31 {
32         SInt32 version;
33         Gestalt(gestaltSystemVersion,&version);
34         if(version < 0x1050)
35         {
36                 printf("Factor requires Mac OS X 10.5 or later.\n");
37                 exit(1);
38         }
39
40         [[NSAutoreleasePool alloc] init];
41 }
42
43 const char *vm_executable_path(void)
44 {
45         return [[[NSBundle mainBundle] executablePath] UTF8String];
46 }
47
48 const char *default_image_path(void)
49 {
50         NSBundle *bundle = [NSBundle mainBundle];
51         NSString *path = [bundle bundlePath];
52         NSString *executable = [[bundle executablePath] lastPathComponent];
53         NSString *image = [executable stringByAppendingString:@".image"];
54
55         NSString *returnVal;
56
57         if([path hasSuffix:@".app"] || [path hasSuffix:@".app/"])
58         {
59                 NSFileManager *mgr = [NSFileManager defaultManager];
60
61                 NSString *imageInBundle = [[path stringByAppendingPathComponent:@"Contents/Resources"] stringByAppendingPathComponent:image];
62                 NSString *imageAlongBundle = [[path stringByDeletingLastPathComponent] stringByAppendingPathComponent:image];
63
64                 returnVal = ([mgr fileExistsAtPath:imageInBundle]
65                         ? imageInBundle : imageAlongBundle);
66         }
67         else
68                 returnVal = [path stringByAppendingPathComponent:image];
69
70         return [returnVal UTF8String];
71 }
72
73 void factor_vm::init_signals(void)
74 {
75         unix_init_signals();
76         mach_initialize();
77 }
78
79 /* Amateurs at Apple: implement this function, properly! */
80 Protocol *objc_getProtocol(char *name)
81 {
82         if(strcmp(name,"NSTextInput") == 0)
83                 return @protocol(NSTextInput);
84         else
85                 return nil;
86 }
87
88 u64 nano_count()
89 {
90         u64 time = mach_absolute_time();
91
92         static u64 scaling_factor = 0;
93         if(!scaling_factor)
94         {
95                 mach_timebase_info_data_t info;
96                 kern_return_t ret = mach_timebase_info(&info);
97                 if(ret != 0)
98                         fatal_error("mach_timebase_info failed",ret);
99                 scaling_factor = info.numer/info.denom;
100         }
101
102         return time * scaling_factor;
103 }
104
105 }