masterkain / macruby

Ruby 1.9 ported to run directly on top of Mac OS X core technologies. Trunk mirror.

macruby / MacRuby.m
100644 122 lines (102 sloc) 2.527 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#import <Foundation/Foundation.h>
#include "ruby/ruby.h"
#include "ruby/node.h"
#include "ruby/objc.h"
#include "vm.h"
 
@implementation MacRuby
 
extern int ruby_initialized;
 
+ (MacRuby *)sharedRuntime
{
    static MacRuby *runtime = nil;
    if (runtime == nil) {
runtime = [[MacRuby alloc] init];
if (ruby_initialized == 0) {
int argc = 0;
char **argv = NULL;
ruby_sysinit(&argc, &argv);
ruby_init();
rb_vm_init_compiler();
}
    }
    return runtime;
}
 
+ (MacRuby *)runtimeAttachedToProcessIdentifier:(pid_t)pid
{
    [NSException raise:NSGenericException format:@"not implemented yet"];
    return nil;
}
 
- (id)evaluateString:(NSString *)expression
{
    return RB2OC(rb_eval_string([(NSString *)expression UTF8String]));
}
 
- (id)evaluateFileAtPath:(NSString *)path
{
    return [self evaluateString:[NSString stringWithContentsOfFile:path
usedEncoding:nil error:nil]];
}
 
- (id)evaluateFileAtURL:(NSURL *)URL
{
    if (![URL isFileURL]) {
[NSException raise:NSInvalidArgumentException format:
@"given URL is not a file URL"];
    }
    return [self evaluateFileAtPath:[URL relativePath]];
}
 
- (void)loadBridgeSupportFileAtPath:(NSString *)path
{
    rb_vm_load_bridge_support([path fileSystemRepresentation], NULL, 0);
}
 
- (void)loadBridgeSupportFileAtURL:(NSURL *)URL
{
    if (![URL isFileURL]) {
[NSException raise:NSInvalidArgumentException format:
@"given URL is not a file URL"];
    }
    [self loadBridgeSupportFileAtPath:[URL relativePath]];
}
 
@end
 
@implementation NSObject (MacRubyAdditions)
 
- (id)performRubySelector:(SEL)sel
{
    return [self performRubySelector:sel withArguments:NULL];
}
 
- (id)performRubySelector:(SEL)sel withArguments:(id *)argv count:(int)argc
{
    VALUE *rargv = NULL;
    if (argc > 0) {
rargv = (VALUE *)alloca(sizeof(VALUE) * argc);
int i;
for (i = 0; i < argc; i++) {
rargv[i] = OC2RB(argv[i]);
}
    }
 
    return RB2OC(rb_vm_call(OC2RB(self), sel, argc, rargv, false));
}
 
- (id)performRubySelector:(SEL)sel withArguments:firstArg, ...
{
    va_list args;
    int argc;
    id *argv;
 
    if (firstArg != nil) {
int i;
 
argc = 1;
va_start(args, firstArg);
while (va_arg(args, id) != NULL) {
argc++;
}
va_end(args);
argv = alloca(sizeof(id) * argc);
va_start(args, firstArg);
argv[0] = firstArg;
for (i = 1; i < argc; i++) {
argv[i] = va_arg(args, id);
}
va_end(args);
    }
    else {
argc = 0;
argv = NULL;
    }
 
    return [self performRubySelector:sel withArguments:argv count:argc];
}
 
@end