Inline-Ruby

 view release on metacpan or  search on metacpan

Ruby.xs  view on Meta::CPAN


    return rb2pl(rb_retval);
}

MODULE = Inline::Ruby	PACKAGE = Inline::Ruby	PREFIX = my_

BOOT:
#ifdef	CREATE_RUBY
do_rbinit();
rb_gc_start(); /* important */
rb_funcall(rb_stdout, rb_intern("sync="), 1, 1);
#endif

PROTOTYPES: DISABLE

#=============================================================================
# This is called to evaluate ruby code (duh!). It uses rb_rescue to trap any
# compile errors raised by the interpreter. If an exception is thrown we
# return an undef, and set the global variable "$@".
#=============================================================================
int
config_var(str)
	char*	str
    CODE:
	if (strEQ(str, "CHECK_CONTEXT"))
#ifdef	CHECK_CONTEXT
	    RETVAL = 1;
#else
	    RETVAL = 0;
#endif
	else if (strEQ(str, "FLATTEN_ARRAYS"))
#ifdef	FLATTEN_ARRAYS
	    RETVAL = 1;
#else
	    RETVAL = 0;
#endif
	else if (strEQ(str, "FLATTEN_CALLBACK_ARGS"))
#ifdef	FLATTEN_CALLBACK_ARGS
	    RETVAL = 1;
#else
	    RETVAL = 0;
#endif
	else {
	    if (PL_dowarn)
		warn("Inline::Ruby::config_var: unknown config var '%s'", str);
	    XSRETURN_UNDEF;
	}
    OUTPUT:
	RETVAL

#=============================================================================
# This is called to evaluate ruby code (duh!). It uses rb_rescue to trap any
# compile errors raised by the interpreter. If an exception is thrown we
# return an undef, and set the global variable "$@".
#=============================================================================
void
my_rb_eval(str)
	char*	str
    PREINIT:
	SV*	pl_retval;
    PPCODE:
	Printf(("About to evaluate some Ruby code:\n"));
        Printf(("%s\n", str));
        Printf(("__END__"));
#ifdef rb_set_errinfo
    rb_set_errinfo(Qnil); /* reset GET_THREAD()->errinfo */
#endif
	pl_retval = rb2pl(rb_rescue2(&my_eval_string, rb_str_new2(str),
				  &my_error_trap, Qnil, rb_eException, 0));
	Printf(("Done.\n"));
#if defined CHECK_CONTEXT && defined FLATTEN_ARRAYS
	FLATTEN_RETVAL(pl_retval);
#else
	PRESERVE_RETVAL(pl_retval);
#endif

#=============================================================================
# This sub calls a global ruby method. It takes the name of the method to run,
# an optional iterator, and any arguments to the method.
#=============================================================================
#undef	NUM_FIXED_ARGS
#define	NUM_FIXED_ARGS 1
void
my_rb_call_function(FNAME, ...)
	char*	FNAME
    PREINIT:
	VALUE	argv;
	SV*	pl_retval;
    PPCODE:
	Printf(("rb_call_function(\"%s\")\n", FNAME));
	INIT_RUBY_ARGV(argv);
	pl_retval = call_ruby_method(Qnil, FNAME, NULL, argv);
#if defined CHECK_CONTEXT && defined FLATTEN_ARRAYS
	FLATTEN_RETVAL(pl_retval);
#else
	PRESERVE_RETVAL(pl_retval);
#endif

#=============================================================================
# This is called whenever you need to call a class method. It takes the name
# of the class, the method name, an iterator block, and any arguments to the
# method. If the iterator block is not a reference to a Perl subroutine, it is
# not passed to the ruby method.
#=============================================================================
#undef  NUM_FIXED_ARGS
#define NUM_FIXED_ARGS 2
void
my_rb_call_class_method(KLASS, mname, ...)
	char*	KLASS
	char*	mname
    PREINIT:
	VALUE	klass;
	VALUE	argv;
	SV*	pl_retval;
    PPCODE:
	Printf(("rb_call_class_method('%s', '%s', ...)\n",KLASS,mname));

	INIT_RUBY_ARGV(argv);
	klass = rb_str_new2(KLASS);
	pl_retval = call_ruby_method(klass, mname, NULL, argv);
#if defined CHECK_CONTEXT && defined FLATTEN_ARRAYS
	FLATTEN_RETVAL(pl_retval);
#else
	PRESERVE_RETVAL(pl_retval);
#endif

#=============================================================================
# This is called whenever you need to call an instance method. It takes the
# instance of the class, the method name, an iterator block, and any arguments
# to the method. If the iterator block is not a reference to a Perl
# subroutine, it is not passed to the ruby method.
#=============================================================================
#undef  NUM_FIXED_ARGS
#define NUM_FIXED_ARGS 2
void
my_rb_call_instance_method(_inst, mname, ...)
	SV*	_inst
	char*	mname
    PREINIT:
	VALUE	inst;
	VALUE	argv;
	SV*	pl_retval;
	SV*	iter;
    PPCODE:
	Printf(("rb_call_instance_method(%p, '%s', ...)\n",
		_inst, mname));

	if (isa_InlineRubyWrapper(_inst)) {
	    inst = UNWRAP_RUBY_OBJ(_inst);
	    iter = INLINE_MAGIC(_inst)->iter;
	    Printf(("inst (%p) successfully passed the PVMG test\n", (void *) inst));
	}
	else {
	    croak("Object is not a wrapped Inline::Ruby::Object object");
	    XSRETURN_EMPTY;
	}

	INIT_RUBY_ARGV(argv);
	pl_retval = call_ruby_method(inst, mname, iter, argv);
#if defined CHECK_CONTEXT && defined FLATTEN_ARRAYS
	FLATTEN_RETVAL(pl_retval);
#else
	PRESERVE_RETVAL(pl_retval);
#endif

#=============================================================================
# This sub calls a global ruby method. It takes the name of the method to run,
# an optional iterator, and any arguments to the method.
#=============================================================================
SV*
my_rb_iter(obj, iter=NULL)
	SV*	obj
	SV*	iter
    CODE:
	/* Case 1: obj is an instance method */
	if (items == 2 && isa_InlineRubyWrapper(obj)) {
	    RETVAL = rb2pl(pl2rb(obj));	/* deep copy */
	    INLINE_MAGIC(RETVAL)->iter = iter;
	    SvREFCNT_inc(iter);
	}
	/* Case 2: obj is a class name */
	else if (items == 2 && SvTYPE(obj) == SVt_PV) {
	    RETVAL = new_InlineRubyWrapper(rb_str_new2(SvPV_nolen(obj)), iter);
	}
	else if (items == 1)
	    RETVAL = new_InlineRubyWrapper(Qnil, obj);	/* pass sub in obj */
	else
	    RETVAL = new_InlineRubyWrapper(Qnil, iter);
    OUTPUT:
	RETVAL

MODULE = Inline::Ruby	PACKAGE = Inline::Ruby::Object

void
DESTROY(obj)
        SV*	obj
    CODE:
	if (isa_InlineRubyWrapper(obj)) {
        /*
        XXX - this somehow conflicts with free_InlineRubyWrapper,
        so it's commented out until we find a way to get them work together:

 	    VALUE rb_object = UNWRAP_RUBY_OBJ(obj);
	    */



( run in 0.539 second using v1.01-cache-2.11-cpan-5511b514fd6 )