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