]> gitweb.factorcode.org Git - factor.git/blob - vm/os-macosx.mm
VM: Refactor os-* to Factor style
[factor.git] / vm / os-macosx.mm
1 #import <Cocoa/Cocoa.h>
2
3 #include <mach/mach_time.h>
4 #include <sys/utsname.h>
5
6 #include "master.hpp"
7
8 namespace factor {
9
10 void factor_vm::c_to_factor_toplevel(cell quot) { c_to_factor(quot); }
11
12 // Darwin 9 is 10.5, Darwin 10 is 10.6
13 // http://en.wikipedia.org/wiki/Darwin_(operating_system)#Release_history
14 void early_init(void) {
15   struct utsname u;
16   int n;
17   uname(&u);
18   sscanf(u.release, "%d", &n);
19   if (n < 9) {
20     std::cout << "Factor requires Mac OS X 10.5 or later.\n";
21     exit(1);
22   }
23 }
24
25 const char* vm_executable_path(void) {
26   return [[[NSBundle mainBundle] executablePath] UTF8String];
27 }
28
29 const char* default_image_path(void) {
30   NSBundle* bundle = [NSBundle mainBundle];
31   NSString* path = [bundle bundlePath];
32   NSString* executable = [[bundle executablePath] lastPathComponent];
33   NSString* image = [executable stringByAppendingString:@".image"];
34
35   NSString* returnVal;
36
37   if ([path hasSuffix:@".app"] || [path hasSuffix:@".app/"]) {
38     NSFileManager* mgr = [NSFileManager defaultManager];
39
40     NSString* imageInBundle =
41         [[path stringByAppendingPathComponent:@"Contents/Resources"]
42             stringByAppendingPathComponent:image];
43     NSString* imageAlongBundle = [[path stringByDeletingLastPathComponent]
44         stringByAppendingPathComponent:image];
45
46     returnVal = ([mgr fileExistsAtPath:imageInBundle] ? imageInBundle
47                                                       : imageAlongBundle);
48   } else
49     returnVal = [path stringByAppendingPathComponent:image];
50
51   return [returnVal UTF8String];
52 }
53
54 void factor_vm::init_signals(void) {
55   unix_init_signals();
56   mach_initialize();
57 }
58
59 /* Amateurs at Apple: implement this function, properly! */
60 Protocol* objc_getProtocol(char* name) {
61   if (strcmp(name, "NSTextInput") == 0)
62     return @protocol(NSTextInput);
63   else
64     return nil;
65 }
66
67 u64 nano_count() {
68   u64 time = mach_absolute_time();
69
70   static u64 scaling_factor = 0;
71   if (!scaling_factor) {
72     mach_timebase_info_data_t info;
73     kern_return_t ret = mach_timebase_info(&info);
74     if (ret != 0)
75       fatal_error("mach_timebase_info failed", ret);
76     scaling_factor = info.numer / info.denom;
77   }
78
79   return time * scaling_factor;
80 }
81
82 }