[macruby-changes] [1536] MacRuby/branches/experimental

source_changes at macosforge.org source_changes at macosforge.org
Mon May 4 21:46:14 PDT 2009


Revision: 1536
          http://trac.macosforge.org/projects/ruby/changeset/1536
Author:   lsansonetti at apple.com
Date:     2009-05-04 21:46:14 -0700 (Mon, 04 May 2009)
Log Message:
-----------
moved the MacRuby ObjC API into a separate file

Modified Paths:
--------------
    MacRuby/branches/experimental/objc.m
    MacRuby/branches/experimental/rakelib/builder.rb

Added Paths:
-----------
    MacRuby/branches/experimental/MacRuby.m

Added: MacRuby/branches/experimental/MacRuby.m
===================================================================
--- MacRuby/branches/experimental/MacRuby.m	                        (rev 0)
+++ MacRuby/branches/experimental/MacRuby.m	2009-05-05 04:46:14 UTC (rev 1536)
@@ -0,0 +1,120 @@
+#import <Foundation/Foundation.h>
+#include "ruby/ruby.h"
+#include "ruby/node.h"
+#include "ruby/objc.h"
+#include "roxor.h"
+
+ at 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();
+	}
+    }
+    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]];
+}
+
+ at end
+
+ at 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)) {
+	    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];
+}
+
+ at end

Modified: MacRuby/branches/experimental/objc.m
===================================================================
--- MacRuby/branches/experimental/objc.m	2009-05-05 04:44:48 UTC (rev 1535)
+++ MacRuby/branches/experimental/objc.m	2009-05-05 04:46:14 UTC (rev 1536)
@@ -657,244 +657,3 @@
     return (id)objc_getProtocol([name UTF8String]);
 } 
 @end
-
-extern int ruby_initialized; /* eval.c */
-
- at implementation MacRuby
-
-+ (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();
-	}
-    }
-    return runtime;
-}
-
-+ (MacRuby *)runtimeAttachedToProcessIdentifier:(pid_t)pid
-{
-    [NSException raise:NSGenericException format:@"not implemented yet"];
-    return nil;
-}
-
-static void
-rb_raise_ruby_exc_in_objc(VALUE ex)
-{
-    VALUE ex_name, ex_message, ex_backtrace;
-    NSException *ocex;
-    static ID name_id = 0;
-    static ID message_id = 0;
-    static ID backtrace_id = 0;
-
-    if (name_id == 0) {
-	name_id = rb_intern("name");
-	message_id = rb_intern("message");
-	backtrace_id = rb_intern("backtrace");
-    }
-
-    ex_name = rb_funcall(CLASS_OF(ex), name_id, 0);
-    ex_message = rb_funcall(ex, message_id, 0);
-    ex_backtrace = rb_funcall(ex, backtrace_id, 0);
-
-    ocex = [NSException exceptionWithName:(id)ex_name reason:(id)ex_message 
-		userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
-		    (id)ex, @"object", 
-		    (id)ex_backtrace, @"backtrace",
-		    NULL]];
-
-    [ocex raise];
-}
-
-static VALUE
-evaluateString_safe(VALUE expression)
-{
-    return rb_eval_string([(NSString *)expression UTF8String]);
-}
-
-static VALUE
-evaluateString_rescue(void)
-{
-    rb_raise_ruby_exc_in_objc(rb_errinfo());
-    return Qnil; /* not reached */
-}
-
-- (id)evaluateString:(NSString *)expression
-{
-    VALUE ret;
-
-    ret = rb_rescue2(evaluateString_safe, (VALUE)expression,
-	    	     evaluateString_rescue, Qnil,
-		     rb_eException, (VALUE)0);
-    return RB2OC(ret);
-}
-
-- (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]];
-}
-
- at end
-
- at implementation NSObject (MacRubyAdditions)
-
-- (id)performRubySelector:(SEL)sel
-{
-    return [self performRubySelector:sel withArguments:NULL];
-}
-
-struct performRuby_context
-{
-    VALUE rcv;
-    ID mid;
-    int argc;
-    VALUE *argv;
-    NODE *node;
-};
-
-static VALUE
-performRuby_safe(VALUE arg)
-{
-#if 0
-    struct performRuby_context *ud = (struct performRuby_context *)arg;
-    return rb_vm_call(GET_THREAD(), *(VALUE *)ud->rcv, ud->rcv, ud->mid, Qnil,
-		      ud->argc, ud->argv, ud->node, 0);
-#endif
-    return Qnil;
-}
-
-static VALUE
-performRuby_rescue(VALUE arg)
-{
-//    if (arg != 0) {
-//	ruby_current_thread = (rb_thread_t *)arg;
-//    }
-    rb_raise_ruby_exc_in_objc(rb_errinfo());
-    return Qnil; /* not reached */
-}
-
-- (id)performRubySelector:(SEL)sel withArguments:(id *)argv count:(int)argc
-{
-//    const bool need_protection = GET_THREAD()->thread_id != pthread_self();
-    NODE *node;
-    IMP imp;
-    VALUE *rargs, ret;
-    ID mid;
-
-    imp = NULL;
-    node = NULL;//TODO rb_objc_method_node2(*(VALUE *)self, sel, &imp);
-    if (node == NULL) {
-	if (imp != NULL) {
-	    [NSException raise:NSInvalidArgumentException format:
-		@"-[%@ %s] is not a pure Ruby method", self, (char *)sel];
-	}
-	else {
-	    [NSException raise:NSInvalidArgumentException format:
-		@"receiver %@ does not respond to %s", (char *)sel];
-	}
-    }
-
-    if (argc == 0) {
-	rargs = NULL;
-    }
-    else {
-	int i;
-	rargs = (VALUE *)alloca(sizeof(VALUE) * argc);
-	for (i = 0; i < argc; i++) {
-	    rargs[i] = OC2RB(argv[i]);
-	}
-    }
-   
-#if 0
-    rb_thread_t *th, *old = NULL;
-
-    if (need_protection) {
-	th = rb_thread_wrap_existing_native_thread(pthread_self());
-	native_mutex_lock(&GET_THREAD()->vm->global_interpreter_lock);
-	old = ruby_current_thread;
-	ruby_current_thread = th;
-    }
-#endif
-
-    mid = rb_intern((char *)sel);
-
-    struct performRuby_context ud;
-    ud.rcv = (VALUE)self;
-    ud.mid = mid;
-    ud.argc = argc;
-    ud.argv = rargs;
-    ud.node = node->nd_body;
-
-    ret = rb_rescue2(performRuby_safe, (VALUE)&ud,
-                     //performRuby_rescue, (VALUE)old,
-                     performRuby_rescue, Qnil,
-                     rb_eException, (VALUE)0);
- 
-#if 0
-    if (need_protection) {
-	native_mutex_unlock(&GET_THREAD()->vm->global_interpreter_lock);
-	ruby_current_thread = old;
-    }
-#endif
-
-    return RB2OC(ret);
-}
-
-- (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)) {
-	    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];
-}
-
- at end

Modified: MacRuby/branches/experimental/rakelib/builder.rb
===================================================================
--- MacRuby/branches/experimental/rakelib/builder.rb	2009-05-05 04:44:48 UTC (rev 1535)
+++ MacRuby/branches/experimental/rakelib/builder.rb	2009-05-05 04:46:14 UTC (rev 1536)
@@ -101,7 +101,7 @@
   random range rational re regcomp regenc regerror regexec regparse regsyntax
   ruby set signal sprintf st string struct time transcode util variable version
   thread id objc bs encoding main dln dmyext enc/ascii 
-  vm_eval prelude miniprelude gc-stub roxor
+  vm_eval prelude miniprelude gc-stub roxor MacRuby
 }
 
 class Builder
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20090504/61de3f6b/attachment.html>


More information about the macruby-changes mailing list