public
Description: A lean-and-mean Ruby/ObjC bridge
Homepage: http://rubyobjc.com
Clone URL: git://github.com/timburks/rubyobjc.git
rubyobjc / objc / runtime.m
100644 60 lines (53 sloc) 1.968 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
* runtime.m
*
* Cocoa runtime support for RubyObjC.
*
* Copyright (c) 2007 Tim Burks, Neon Design Technology, Inc.
* For more information about this file, visit http://www.rubyobjc.com.
*/
 
#import "rubyobjc.h"
#import <string.h>
#import <Foundation/Foundation.h>
 
void Init_objc_base();
void Init_objc();
 
/*
* Call this instead of NSApplicationMain. Give it the name of the main Ruby file for your application.
*/
int RubyObjC_ApplicationMain(const char* main_rb, int argc, const char* argv[])
{
    id pool = [[NSAutoreleasePool alloc] init];
    // Push the main_rb file name onto the argv list and initialize Ruby.
    int new_argc = 0;
    const char **new_argv = malloc (sizeof(char*) * (argc + 2));
    int i;
    for (i = 0; i < argc; i++)
        if (strncmp(argv[i], "-psn_", 5) != 0)
            new_argv[new_argc++] = argv[i];
    if (main_rb == NULL) main_rb = "main.rb";
    NSBundle* bundle = [NSBundle mainBundle];
    new_argv[new_argc++] = strdup([[bundle pathForResource:[NSString stringWithUTF8String: main_rb] ofType:nil] fileSystemRepresentation]);
    new_argv[new_argc] = NULL;
    ruby_init();
    ruby_options(new_argc, (char**) new_argv);
    // Push the resource directory into the Ruby load path.
    char *resource_path = strdup([[bundle resourcePath] fileSystemRepresentation]);
    extern VALUE rb_load_path;
    rb_ary_unshift(rb_load_path, rb_str_new2(resource_path));
    Init_objc_base();
    [pool release];
    // Start the Ruby interpreter. It will never return.
    pool = [[NSAutoreleasePool alloc] init];
    ruby_run();
  [pool release];
  return 0; // unreached
}
 
int RubyObjC_BundleStart(NSString *filename)
{
    ruby_init();
    Init_objc_base();
    NSError *err;
    NSString *program = [NSString stringWithContentsOfFile:filename encoding:NSASCIIStringEncoding error:&err];
    rb_eval_string([program cStringUsingEncoding:NSASCIIStringEncoding]);
    return true;
}