factor/vm/os-macosx.mm

93 lines
2.7 KiB
Plaintext
Raw Normal View History

2009-05-02 05:04:19 -04:00
#import <Cocoa/Cocoa.h>
#include <mach/mach_time.h>
#include <sys/utsname.h>
2009-05-02 05:04:19 -04:00
#include "master.hpp"
2013-05-11 22:24:31 -04:00
namespace factor {
2009-05-04 02:46:13 -04:00
2013-05-11 22:24:31 -04:00
void factor_vm::c_to_factor_toplevel(cell quot) { c_to_factor(quot); }
2009-05-02 05:04:19 -04:00
// Darwin 9 is 10.5, Darwin 10 is 10.6
// http://en.wikipedia.org/wiki/Darwin_(operating_system)#Release_history
2013-05-11 22:24:31 -04:00
void early_init(void) {
struct utsname u;
int n;
uname(&u);
sscanf(u.release, "%d", &n);
if (n < 9) {
std::cout << "Factor requires Mac OS X 10.5 or later.\n";
exit(1);
}
2009-05-02 05:04:19 -04:00
}
// You must free() this yourself.
2013-05-11 22:24:31 -04:00
const char* vm_executable_path(void) {
return safe_strdup([[[NSBundle mainBundle] executablePath] UTF8String]);
2009-05-02 05:04:19 -04:00
}
2013-05-11 22:24:31 -04:00
const char* default_image_path(void) {
NSBundle* bundle = [NSBundle mainBundle];
NSString* path = [bundle bundlePath];
NSString* executablePath = [[bundle executablePath] stringByResolvingSymlinksInPath];
NSString* executable = [executablePath lastPathComponent];
2013-05-11 22:24:31 -04:00
NSString* image = [executable stringByAppendingString:@".image"];
2009-05-02 05:04:19 -04:00
2013-05-11 22:24:31 -04:00
NSString* returnVal;
2009-05-02 05:04:19 -04:00
2013-05-11 22:24:31 -04:00
if ([path hasSuffix:@".app"] || [path hasSuffix:@".app/"]) {
NSFileManager* mgr = [NSFileManager defaultManager];
2009-05-02 05:04:19 -04:00
2013-05-11 22:24:31 -04:00
NSString* imageInBundle =
[[path stringByAppendingPathComponent:@"Contents/Resources"]
stringByAppendingPathComponent:image];
NSString* imageAlongBundle = [[path stringByDeletingLastPathComponent]
stringByAppendingPathComponent:image];
2009-05-02 05:04:19 -04:00
2013-05-11 22:24:31 -04:00
returnVal = ([mgr fileExistsAtPath:imageInBundle] ? imageInBundle
: imageAlongBundle);
} else if ([executablePath hasSuffix:@".app/Contents/MacOS/factor"]) {
returnVal = executablePath;
returnVal = [returnVal stringByDeletingLastPathComponent];
returnVal = [returnVal stringByDeletingLastPathComponent];
returnVal = [returnVal stringByDeletingLastPathComponent];
returnVal = [returnVal stringByDeletingLastPathComponent];
returnVal = [returnVal stringByAppendingPathComponent:image];
} else {
2013-05-11 22:24:31 -04:00
returnVal = [path stringByAppendingPathComponent:image];
}
2009-05-02 05:04:19 -04:00
2013-05-11 22:24:31 -04:00
return [returnVal UTF8String];
2009-05-02 05:04:19 -04:00
}
2013-05-11 22:24:31 -04:00
void factor_vm::init_signals(void) {
unix_init_signals();
mach_initialize();
2009-05-02 05:04:19 -04:00
}
// Amateurs at Apple: implement this function, properly!
2013-05-11 22:24:31 -04:00
Protocol* objc_getProtocol(char* name) {
if (strcmp(name, "NSTextInput") == 0)
return @protocol(NSTextInput);
else
return nil;
2009-05-02 05:04:19 -04:00
}
2009-05-04 02:46:13 -04:00
uint64_t nano_count() {
uint64_t time = mach_absolute_time();
2013-05-11 22:24:31 -04:00
static uint64_t scaling_factor = 0;
2013-05-11 22:24:31 -04:00
if (!scaling_factor) {
mach_timebase_info_data_t info;
kern_return_t ret = mach_timebase_info(&info);
if (ret != 0)
fatal_error("mach_timebase_info failed", ret);
scaling_factor = info.numer / info.denom;
}
return time * scaling_factor;
}
2009-05-04 02:46:13 -04:00
}