forked from MacRuby/MacRuby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMacRuby.m
131 lines (112 loc) · 2.77 KB
/
MacRuby.m
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
123
124
125
126
127
128
129
130
131
/*
* MacRuby Objective-C API.
*
* This file is covered by the Ruby license. See COPYING for more details.
*
* Copyright (C) 2012, The MacRuby Team. All rights reserved.
* Copyright (C) 2009-2011, Apple Inc. All rights reserved.
*/
#import <Foundation/Foundation.h>
#include "macruby_internal.h"
#include "ruby/node.h"
#include "ruby/objc.h"
#include "vm.h"
#include "objc.h"
@implementation MacRuby
extern bool ruby_initialized;
+ (MacRuby *)sharedRuntime
{
static MacRuby *runtime = nil;
if (runtime == nil) {
runtime = [[MacRuby alloc] init];
if (!ruby_initialized) {
int argc = 0;
char **argv = NULL;
ruby_sysinit(&argc, &argv);
ruby_init();
ruby_init_loadpath();
rb_vm_init_compiler();
rb_vm_init_jit();
rb_objc_fix_relocatable_load_path();
rb_objc_load_loaded_frameworks_bridgesupport();
}
}
return runtime;
}
- (id)evaluateString:(NSString *)expression
{
return RB2OC(rb_eval_string([(NSString *)expression UTF8String]));
}
- (id)evaluateFileAtPath:(NSString *)path
{
return RB2OC(rb_f_require(Qnil, (VALUE)path));
}
- (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
{
#if MACRUBY_STATIC
printf("loadBridgeSupportFileAtPath: not supported in MacRuby static\n");
abort();
#else
rb_vm_load_bridge_support([path fileSystemRepresentation], NULL, 0);
#endif
}
- (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 *)xmalloc(sizeof(VALUE) * argc);
for (int i = 0; i < argc; i++) {
rargv[i] = OC2RB(argv[i]);
}
}
return RB2OC(rb_vm_call(OC2RB(self), sel, argc, rargv));
}
- (id)performRubySelector:(SEL)sel withArguments:firstArg, ...
{
va_list args;
int argc;
id *argv;
if (firstArg != nil) {
argc = 1;
va_start(args, firstArg);
while (va_arg(args, id) != NULL) {
argc++;
}
va_end(args);
argv = xmalloc(sizeof(id) * argc);
va_start(args, firstArg);
argv[0] = firstArg;
for (int 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