RubyInline 1.0.4 Released! (fwd)

Woohoo! another cool new toy to play with!

-pate

···

---------- Forwarded message ----------
Date: Tue, 10 Sep 2002 11:15:00 -0700
From: Ryan Davis ryand@zenspider.com
Reply-To: ruby@zenspider.com
To: Seattle Ruby Brigade! ruby@zenspider.com,
ruby-developers@lists.ruby-support.com
Subject: RubyInline 1.0.4 Released!

RubyInline 1.0.4 has been released!

Ruby Inline is my quick attempt to create an analog to Perl’s
Inline::C. The ruby version isn’t near as feature-full as the perl
version, but it is neat!

Here is the output from the example bundled with the package (factorial
5):

<507> ./example.rb slow
RubyInline 1.0.4
Type = Native, Iter = 1000000, time = 24.01048500 sec, 0.00002401 sec /
iter
<508> ./example.rb
RubyInline 1.0.4
Building /tmp/Mod_MyTest_fastfact.so with 'cc -shared -O -pipe -fPIC
-I /usr/local/lib/ruby/1.6/i386-freebsd4’
Type = Inline, Iter = 1000000, time = 5.40564900 sec, 0.00000541 sec /
iter
<509> ./example.rb
RubyInline 1.0.4
Type = Inline, Iter = 1000000, time = 5.27179200 sec, 0.00000527 sec /
iter

I’d love feedback and fixes for other platforms! The project is on
sourceforge at http://sourceforge.net/projects/rubyinline/

RubyInline 1.0.4 has been released!

Ruby Inline is my quick attempt to create an analog to Perl’s
Inline::C. The ruby version isn’t near as feature-full as the perl
version, but it is neat!

Um, what does Perl’s Inline::C do?

I’d love feedback and fixes for other platforms! The project is on
sourceforge at RubyInline download | SourceForge.net

Feedback:

That site doesn’t actually say what the code does; it assumes knowledge of
Perl’s Inline::C.
Perhaps there should be a brief but clear description of RubyInline.

James

So looking at this example, I guess on the second “nonslow” run it found
/tmp/Mod_MyTest_fastfact.so there already and reused it?

I haven’t looked at the sources or anything, so I’m just going from this
one example, but what would have happened if a naughty user had come
along first and created a /tmp/Mod_MyTest_fastfact.so that Did Mean
Things?

Tom.

···

<507> ./example.rb slow
RubyInline 1.0.4
Type = Native, Iter = 1000000, time = 24.01048500 sec, 0.00002401 sec /
iter
<508> ./example.rb
RubyInline 1.0.4
Building /tmp/Mod_MyTest_fastfact.so with ‘cc -shared -O -pipe -fPIC
-I /usr/local/lib/ruby/1.6/i386-freebsd4’
Type = Inline, Iter = 1000000, time = 5.40564900 sec, 0.00000541 sec /
iter
<509> ./example.rb
RubyInline 1.0.4
Type = Inline, Iter = 1000000, time = 5.27179200 sec, 0.00000527 sec /
iter


.^. .-------------------------------------------------------.
/V\ | Tom Gilbert, London, England | http://linuxbrit.co.uk |
/( )\ | Open Source/UNIX consultant | tom@linuxbrit.co.uk |
^^-^^ `-------------------------------------------------------’

I love it! So far, I only tried the example, but I can’t wait to try it
on some of my own code.

It would be very nice to be able to provide optimized versions of
bottleneck methods, so I’d love to see something like this in the ruby
dist.

Thomas

···

On Tue, 2002-09-10 at 20:55, Pat Eyler wrote:

Woohoo! another cool new toy to play with!

-pate
---------- Forwarded message ----------
Date: Tue, 10 Sep 2002 11:15:00 -0700
From: Ryan Davis ryand@zenspider.com
Reply-To: ruby@zenspider.com
To: Seattle Ruby Brigade! ruby@zenspider.com,
ruby-developers@lists.ruby-support.com
Subject: RubyInline 1.0.4 Released!

RubyInline 1.0.4 has been released!

Ruby Inline is my quick attempt to create an analog to Perl’s
Inline::C. The ruby version isn’t near as feature-full as the perl
version, but it is neat!

Here is the output from the example bundled with the package (factorial
5):

<507> ./example.rb slow
RubyInline 1.0.4
Type = Native, Iter = 1000000, time = 24.01048500 sec, 0.00002401 sec /
iter
<508> ./example.rb
RubyInline 1.0.4
Building /tmp/Mod_MyTest_fastfact.so with ‘cc -shared -O -pipe -fPIC
-I /usr/local/lib/ruby/1.6/i386-freebsd4’
Type = Inline, Iter = 1000000, time = 5.40564900 sec, 0.00000541 sec /
iter
<509> ./example.rb
RubyInline 1.0.4
Type = Inline, Iter = 1000000, time = 5.27179200 sec, 0.00000527 sec /
iter

I’d love feedback and fixes for other platforms! The project is on
sourceforge at RubyInline download | SourceForge.net

I’m having a few problems running RubyInline on a Sun:

./example.rb
RubyInline 1.0.4
Building /tmp/Mod_MyTest_fastfact.so with ‘gcc -Wl,-G -shared -g -O2 -I /usr/local/lib/ruby/1.6/sparc-solaris2.8’
./inline.rb:64: [BUG] Segmentation fault
ruby 1.6.6 (2001-12-26) [sparc-solaris2.8]
Abort

./example.rb
RubyInline 1.0.4
./inline.rb:64: [BUG] Segmentation fault
ruby 1.6.6 (2001-12-26) [sparc-solaris2.8]
Abort

I changed require to load on line 64 and added a print statement and re-ran:

./example.rb
RubyInline 1.0.4
"/tmp/Mod_MyTest_fastfact.so"
/tmp/Mod_MyTest_fastfact.so:1: warning: useless use of a constant in void context
./inline.rb:65:in load': /tmp/Mod_MyTest_fastfact.so:1: Invalid char\177’ in expression (SyntaxError)
/tmp/Mod_MyTest_fastfact.so:1: Invalid char \001' in expression /tmp/Mod_MyTest_fastfact.so:1: Invalid char\002’ in expression
/tmp/Mod_MyTest_fastfact.so:1: Invalid char \001' in expression from ./inline.rb:65:ininline’
from ./example.rb:16:in fastfact' from ./example.rb:34 from ./example.rb:34:ineach’
from ./example.rb:34

Line 65 is:

load "#{so_name}"
···


Jim Freeze

Programming Ruby
def initialize; fun; end
A language with class

I want to give RubyInline a try, and I would like to know how to:

  • get self and use it to
    ** get member variables
    ** call (ruby) methods
  • handle ruby strings
  • handle ruby arrays

… in ruby inline C/Ruby C API.

Cheers,

Thomas

···

On Tue, 2002-09-10 at 20:55, Pat Eyler wrote:

Woohoo! another cool new toy to play with!

-pate
---------- Forwarded message ----------
Date: Tue, 10 Sep 2002 11:15:00 -0700
From: Ryan Davis ryand@zenspider.com
Reply-To: ruby@zenspider.com
To: Seattle Ruby Brigade! ruby@zenspider.com,
ruby-developers@lists.ruby-support.com
Subject: RubyInline 1.0.4 Released!

RubyInline 1.0.4 has been released!

Ruby Inline is my quick attempt to create an analog to Perl’s
Inline::C. The ruby version isn’t near as feature-full as the perl
version, but it is neat!

Here is the output from the example bundled with the package (factorial
5):

<507> ./example.rb slow
RubyInline 1.0.4
Type = Native, Iter = 1000000, time = 24.01048500 sec, 0.00002401 sec /
iter
<508> ./example.rb
RubyInline 1.0.4
Building /tmp/Mod_MyTest_fastfact.so with ‘cc -shared -O -pipe -fPIC
-I /usr/local/lib/ruby/1.6/i386-freebsd4’
Type = Inline, Iter = 1000000, time = 5.40564900 sec, 0.00000541 sec /
iter
<509> ./example.rb
RubyInline 1.0.4
Type = Inline, Iter = 1000000, time = 5.27179200 sec, 0.00000527 sec /
iter

I’d love feedback and fixes for other platforms! The project is on
sourceforge at RubyInline download | SourceForge.net

Here is an idea:

I understand that RubyInline can be a security risk,
but there are some fun things I can imagine if
someone could send down a rubyinline script to
a webserver.

Not only could one compile and run a C application,
but one could run and install a new OS…
Say, for example, find a Window’s server and install
Linux or BSD on it…

Now that would surprise a sysadmin when he
comes into the office in the morning and
discovers, “Hey, what’s my Linux doing on
my Window’s box?”

;>)

···


Jim Freeze

Programming Ruby
def initialize; fun; end
A language with class

Ryan,

I was able to make some progress by modifying inline.rb to read the correct
ENV variables and got to the point where it did:

C:>
cl -LD -nologo -MD -DNT=1 -Zi -O2b2xg- -G5 -Ic:\ruby\lib\ruby\1.7\i386-mswin
32 C:\WINDOWS\TEMP\Mod_MyTest_fastfact.c
Mod_MyTest_fastfact.c
Mod_MyTest_fastfact.obj : error LNK2019: unresolved external symbol
_rb_define_method referenced in function _Init_Mod_MyTest_fastfact
Mod_MyTest_fastfact.obj : error LNK2019: unresolved external symbol
_rb_define_module referenced in function _Init_Mod_MyTest_fastfact
Mod_MyTest_fastfact.dll : fatal error LNK1120: 2 unresolved externals

But then I did not know how to proceed … any suggestions ?

Thanks …
– Shanko

“Pat Eyler” pate@eylerfamily.org wrote in message
news:Pine.LNX.4.44.0209101454580.26961-100000@petrol.whirlycott.com

···

Woohoo! another cool new toy to play with!

-pate
---------- Forwarded message ----------
Date: Tue, 10 Sep 2002 11:15:00 -0700
From: Ryan Davis ryand@zenspider.com
Reply-To: ruby@zenspider.com
To: Seattle Ruby Brigade! ruby@zenspider.com,
ruby-developers@lists.ruby-support.com
Subject: RubyInline 1.0.4 Released!

RubyInline 1.0.4 has been released!

Ruby Inline is my quick attempt to create an analog to Perl’s
Inline::C. The ruby version isn’t near as feature-full as the perl
version, but it is neat!

Here is the output from the example bundled with the package (factorial
5):

<507> ./example.rb slow
RubyInline 1.0.4
Type = Native, Iter = 1000000, time = 24.01048500 sec, 0.00002401 sec /
iter
<508> ./example.rb
RubyInline 1.0.4
Building /tmp/Mod_MyTest_fastfact.so with ‘cc -shared -O -pipe -fPIC
-I /usr/local/lib/ruby/1.6/i386-freebsd4’
Type = Inline, Iter = 1000000, time = 5.40564900 sec, 0.00000541 sec /
iter
<509> ./example.rb
RubyInline 1.0.4
Type = Inline, Iter = 1000000, time = 5.27179200 sec, 0.00000527 sec /
iter

I’d love feedback and fixes for other platforms! The project is on
sourceforge at RubyInline download | SourceForge.net

RubyInline 1.0.4 has been released!

Ruby Inline is my quick attempt to create an analog to Perl’s
Inline::C. The ruby version isn’t near as feature-full as the perl
version, but it is neat!

Um, what does Perl’s Inline::C do?

the Inline:: modules for Perl, Python, and Ruby allow you to include
non-native code inside a script. here’s an example of Perl’s Inline::Ruby

use Inline Ruby;

$obj = Iterator->new(1, “2”, [3, 4], {5 => 6});
$obj->iter(&my_iter)->each;

sub iter {
use Data::Dumper;
print Dumper @_;
}

END
Ruby

class Iterator
def initialize(*elements)
@elements = elements
end
def each
for i in @elements
yield i
end
end
end

maybe someone would like to post a code sample of Inline::C from ruby?

-pate

···

On Wed, 11 Sep 2002, JamesBritt wrote:

I’d love feedback and fixes for other platforms! The project is on
sourceforge at RubyInline download | SourceForge.net

Feedback:

That site doesn’t actually say what the code does; it assumes knowledge of
Perl’s Inline::C.
Perhaps there should be a brief but clear description of RubyInline.

James

iirc, the code looks at the mod time of the .so compiled from the inlined
C code vs the mod time of the file containing the inlined C to decide
whether to compile a new .so

-pate

···

On Wed, 11 Sep 2002, Tom Gilbert wrote:

<507> ./example.rb slow
RubyInline 1.0.4
Type = Native, Iter = 1000000, time = 24.01048500 sec, 0.00002401 sec /
iter
<508> ./example.rb
RubyInline 1.0.4
Building /tmp/Mod_MyTest_fastfact.so with ‘cc -shared -O -pipe -fPIC
-I /usr/local/lib/ruby/1.6/i386-freebsd4’
Type = Inline, Iter = 1000000, time = 5.40564900 sec, 0.00000541 sec /
iter
<509> ./example.rb
RubyInline 1.0.4
Type = Inline, Iter = 1000000, time = 5.27179200 sec, 0.00000527 sec /
iter

So looking at this example, I guess on the second “nonslow” run it found
/tmp/Mod_MyTest_fastfact.so there already and reused it?

I haven’t looked at the sources or anything, so I’m just going from this
one example, but what would have happened if a naughty user had come
along first and created a /tmp/Mod_MyTest_fastfact.so that Did Mean
Things?

Tom.

.^. .-------------------------------------------------------.
/V\ | Tom Gilbert, London, England | http://linuxbrit.co.uk |
/( )\ | Open Source/UNIX consultant | tom@linuxbrit.co.uk |
^^-^^ `-------------------------------------------------------’

./example.rb
RubyInline 1.0.4
Building /tmp/Mod_MyTest_fastfact.so with 'gcc -Wl,-G -shared -g -O2 -I /usr/local/lib/ruby/1.6/sparc-solaris2.8'
./inline.rb:64: [BUG] Segmentation fault
ruby 1.6.6 (2001-12-26) [sparc-solaris2.8]
Abort

Can you try with 1.6.7

nasun% ruby -v
ruby 1.6.7 (2002-03-01) [sparc-solaris2.8]
nasun%

nasun% ./example.rb
RubyInline 1.0.4
Building /tmp/Mod_MyTest_fastfact.so with 'gcc -Wl,-G -g -O2 -I /usr/local/lib/ruby/1.6/sparc-solaris2.8'
Type = Inline, Iter = 1000000, time = 8.38652900 sec, 0.00000839 sec / iter
nasun%

or use gdb to have a backtrace

    load "#{so_name}"

#load is only for .rb file

Guy Decoux

ts totally nailed this one. Please get back to me/us if 1.6.7 doesn’t
work w/ the original source. I really want to get this working on as
many platforms as possible ASAP.

···

On Wednesday, Sep 11, 2002, at 05:42 US/Pacific, Jim Freeze wrote:

I’m having a few problems running RubyInline on a Sun:

Hi,

I was able to make some progress by modifying inline.rb to read the correct
ENV variables and got to the point where it did:

C:>
cl -LD -nologo -MD -DNT=1 -Zi -O2b2xg- -G5 -Ic:\ruby\lib\ruby\1.7\i386-mswin
32 C:\WINDOWS\TEMP\Mod_MyTest_fastfact.c

Libraries are missing. GNU ld ignores undefined symbols in
shared object by default, and VC link.exe doesn’t.

Untested patch.

— inline.rb~ Wed Sep 11 02:21:23 2002
+++ inline.rb Thu Sep 12 13:54:37 2002
@@ -34,4 +34,5 @@ module Inline

   cc = "#{Config::CONFIG['LDSHARED']} #{Config::CONFIG['CFLAGS']} -I #{hdrdir}"
  •  libs = "#{Config::CONFIG['LIBRUBYARG']} #{ConfigCONFIG['LIBS']}"
     src_name = "#{tmpdir}/#{mod_name}.c"
     puts "Building #{so_name} with '#{cc}'"
    

@@ -58,5 +59,5 @@ module Inline

   # Compiling
  •  `#{cc} -o #{so_name} #{src_name}`
    
  •  `#{cc} -o #{so_name} #{src_name} #{libs}`
    
    end
···

At Thu, 12 Sep 2002 12:19:13 +0900, Shashank Date wrote:


Nobu Nakada

Hi,

“Shashank Date” ADATE@kc.rr.com wrote in message
news:hGTf9.130052$27.2149343@twister.rdc-kc.rr.com

Ryan,

I was able to make some progress by modifying inline.rb to read the
correct
ENV variables and got to the point where it did:

C:>

cl -LD -nologo -MD -DNT=1 -Zi -O2b2xg- -G5 -Ic:\ruby\lib\ruby\1.7\i386-mswin

32 C:\WINDOWS\TEMP\Mod_MyTest_fastfact.c
Mod_MyTest_fastfact.c
Mod_MyTest_fastfact.obj : error LNK2019: unresolved external symbol
_rb_define_method referenced in function _Init_Mod_MyTest_fastfact
Mod_MyTest_fastfact.obj : error LNK2019: unresolved external symbol
_rb_define_module referenced in function _Init_Mod_MyTest_fastfact
Mod_MyTest_fastfact.dll : fatal error LNK1120: 2 unresolved externals

But then I did not know how to proceed … any suggestions ?

Thanks …
– Shanko

Here is my modified version of inline.rb for Windows XP

···

#!/usr/local/bin/ruby -w

require “rbconfig”

def caller_method_name()
/`([^']+)'/.match(caller(2).first)[1]
end
public :caller_method_name

module Inline

VERSION = ‘1.0.5’

def inline(args, src)

tmpdir = ENV['TMP'] + "/.ruby_inline"

unless File.directory? tmpdir then
  $stderr.puts "NOTE: creating #{tmpdir} for RubyInline"
  Dir.mkdir(tmpdir, 0700)
end

myclass = self.class
mymethod = self.caller_method_name
mod_name = "Mod_#{myclass}_#{mymethod}"
so_name = "#{tmpdir}/#{mod_name}.so"

unless File.file? so_name and File.mtime($0) < File.mtime(so_name) then
  # extracted from mkmf.rb
  srcdir  = Config::CONFIG["srcdir"]
  archdir = Config::CONFIG["archdir"]
  if File.exist? archdir + "/ruby.h"

hdrdir = archdir
elsif File.exist? srcdir + “/ruby.h”
hdrdir = srcdir
else
$stderr.puts “ERROR: Can’t find header files for ruby. Exiting…”
exit 1
end

  cc = "#{Config::CONFIG['LDSHARED']} #{Config::CONFIG['CFLAGS']} -I

#{hdrdir}"
libs = " #{Config::CONFIG[“libdir”]}/#{Config::CONFIG[‘LIBRUBYARG’]}"
src_name = “#{tmpdir}/#{mod_name}.c”
$stderr.puts “Building #{so_name} with ‘#{cc}’”

  s = %Q{

#include “ruby.h”

static VALUE t_#{mymethod}(int argc, VALUE *argv, VALUE self) {
#{src}
}

VALUE c#{mod_name};

void Init_#{mod_name}() {
c#{mod_name} = rb_define_module(“#{mod_name}”);
rb_define_method(c#{mod_name}, "#{mymethod}", t#{mymethod}, -1);
}
}

  # Generating code
  f = File.new(src_name, "w")
  f.puts s
  f.close

  # Compiling
  cmd = "#{cc} -o #{so_name} #{src_name} #{libs}".gsub('/','\\') +
    " -link /INCREMENTAL:no /EXPORT:Init_#{mod_name}"
  `#{cmd}`
end

# Loading & Replacing w/ new method
require "#{so_name}"
myclass.class_eval("include #{mod_name}")
myclass.class_eval("alias_method :old_#{mymethod}, :#{mymethod}")
myclass.class_eval("alias_method :#{mymethod}, :_#{mymethod}")

# Calling
return method("_#{mymethod}").call(*args)

end
end

Park Heesob

In article Pine.LNX.4.21.0209101712440.3470-100000@sanpietro.red-bean.com,

RubyInline 1.0.4 has been released!

Ruby Inline is my quick attempt to create an analog to Perl’s
Inline::C. The ruby version isn’t near as feature-full as the perl
version, but it is neat!

Um, what does Perl’s Inline::C do?

the Inline:: modules for Perl, Python, and Ruby allow you to include
non-native code inside a script. here’s an example of Perl’s Inline::Ruby

use Inline Ruby;

$obj = Iterator->new(1, “2”, [3, 4], {5 => 6});
$obj->iter(&my_iter)->each;

sub iter {
use Data::Dumper;
print Dumper @_;
}

END
Ruby

class Iterator
def initialize(*elements)
@elements = elements
end
def each
for i in @elements
yield i
end
end
end

I was shown this example last night at our PDX.rb meeting… I found it
hard to believe that you could pass a Perl subroutine reference to a Ruby
iterator and that it would work - but it does. Cool. Twisted, but cool.

maybe someone would like to post a code sample of Inline::C from ruby?

Here’s the code from the example that comes with RubyInline:

#!/usr/local/bin/ruby -w

require “inline”

class MyTest

include Inline

def factorial(n)
f = 1
n.downto(1) { |x| f = f * x }
f
end

def fastfact(*args)
#what follows is C code:
inline args, <<-END
int i, f=1;
for (i = FIX2INT(argv[0]); i >= 1; i–) { f = f * i; }
return INT2FIX(f);
END
#end of C code
end

end

t = MyTest.new()

max = 1000000

puts “RubyInline #{Inline::VERSION}”

if ARGV.length == 0 then
type = “Inline”
tstart = Time.now
(1…max).each { |n| r = t.fastfact(5); if r != 120 then puts “ACK! -
#{r}”; end }
tend = Time.now
else
type = “Native”
tstart = Time.now
(1…max).each { |n| r = t.factorial(5); if r != 120 then puts “ACK! -
#{r}”; end }
tend = Time.now
end

total = tend - tstart
avg = total / max
printf “Type = #{type}, Iter = #{max}, time = %.8f sec, %.8f sec /
iter\n”, total, avg
###end of example

So the C code in the MyTest#fastfact method is converted to Ruby-C code
and compiled into a shared library with gcc. The .so file is stored in
the /tmp directory so that on subsequent runs the C code doesn’t need to
be recompiled.

Is this similar to what Python’s Boost tool does?

Perl has Inline modules available for several languages including Ruby (as
shown above by Pat). Now it might be nice to have a RubyInline::Perl
module so people can make use of legacy Perl code in their Ruby apps.

Phil

···

Pat Eyler pate@red-bean.com wrote:

On Wed, 11 Sep 2002, JamesBritt wrote:

Um, what does Perl’s Inline::C do?

the Inline:: modules for Perl, Python, and Ruby allow you to include
non-native code inside a script. here’s an example of Perl’s Inline::Ruby

Very neat.

Thanks,

James

So it’s totally insecure then =P

Tom.

···

iirc, the code looks at the mod time of the .so compiled from the inlined
C code vs the mod time of the file containing the inlined C to decide
whether to compile a new .so


.^. .-------------------------------------------------------.
/V\ | Tom Gilbert, London, England | http://linuxbrit.co.uk |
/( )\ | Open Source/UNIX consultant | tom@linuxbrit.co.uk |
^^-^^ `-------------------------------------------------------’

Oops, I did not notice that we were using 1.6.6.
Will try with 1.6.7.

Jim

···

On Wed, Sep 11, 2002 at 10:10:08PM +0900, ts wrote:

./example.rb
RubyInline 1.0.4
Building /tmp/Mod_MyTest_fastfact.so with ‘gcc -Wl,-G -shared -g -O2 -I /usr/local/lib/ruby/1.6/sparc-solaris2.8’
./inline.rb:64: [BUG] Segmentation fault
ruby 1.6.6 (2001-12-26) [sparc-solaris2.8]
Abort

Can you try with 1.6.7


Jim Freeze

Programming Ruby
def initialize; fun; end
A language with class

Experts,

After a couple of wrong starts (forgot to set the .NET settings ;-)) I was
able to get RubyInline working on my Windows XP (Home Edition). See results
below:

C:>ruby example.rb
RubyInline 1.0.5
Type = Inline, Iter = 1000000, time = 4.33600000 sec, 0.00000434 sec / iter

Thanks a lot Park, nobu and Ryan for your attention.
My final request is that these changes (see Park’s version below) be
refactored into the new 1.0.5 release (enhanced security) that Ryan came out
with.

TIA,

– Shanko

“Park Heesob” phasis@kornet.net wrote in message
news:h9Wf9.254$GH5.1246@news.hananet.net

Hi,

“Shashank Date” ADATE@kc.rr.com wrote in message
news:hGTf9.130052$27.2149343@twister.rdc-kc.rr.com

Ryan,

I was able to make some progress by modifying inline.rb to read the
correct
ENV variables and got to the point where it did:

C:>

cl -LD -nologo -MD -DNT=1 -Zi -O2b2xg- -G5 -Ic:\ruby\lib\ruby\1.7\i386-mswin

32 C:\WINDOWS\TEMP\Mod_MyTest_fastfact.c
Mod_MyTest_fastfact.c
Mod_MyTest_fastfact.obj : error LNK2019: unresolved external symbol
_rb_define_method referenced in function _Init_Mod_MyTest_fastfact
Mod_MyTest_fastfact.obj : error LNK2019: unresolved external symbol
_rb_define_module referenced in function _Init_Mod_MyTest_fastfact
Mod_MyTest_fastfact.dll : fatal error LNK1120: 2 unresolved externals

But then I did not know how to proceed … any suggestions ?

Thanks …
– Shanko

Here is my modified version of inline.rb for Windows XP


#!/usr/local/bin/ruby -w

require “rbconfig”

def caller_method_name()
/`([^']+)'/.match(caller(2).first)[1]
end
public :caller_method_name

module Inline

VERSION = ‘1.0.5’

def inline(args, src)

tmpdir = ENV['TMP'] + "/.ruby_inline"

unless File.directory? tmpdir then
  $stderr.puts "NOTE: creating #{tmpdir} for RubyInline"
  Dir.mkdir(tmpdir, 0700)
end

myclass = self.class
mymethod = self.caller_method_name
mod_name = "Mod_#{myclass}_#{mymethod}"
so_name = "#{tmpdir}/#{mod_name}.so"

unless File.file? so_name and File.mtime($0) < File.mtime(so_name)

then

  # extracted from mkmf.rb
  srcdir  = Config::CONFIG["srcdir"]
  archdir = Config::CONFIG["archdir"]
  if File.exist? archdir + "/ruby.h"

hdrdir = archdir
elsif File.exist? srcdir + “/ruby.h”
hdrdir = srcdir
else
$stderr.puts “ERROR: Can’t find header files for ruby. Exiting…”
exit 1
end

  cc = "#{Config::CONFIG['LDSHARED']} #{Config::CONFIG['CFLAGS']} -I

#{hdrdir}"
libs = "
#{Config::CONFIG[“libdir”]}/#{Config::CONFIG[‘LIBRUBYARG’]}"

···
  src_name = "#{tmpdir}/#{mod_name}.c"
  $stderr.puts "Building #{so_name} with '#{cc}'"

  s = %Q{

#include “ruby.h”

static VALUE t_#{mymethod}(int argc, VALUE *argv, VALUE self) {
#{src}
}

VALUE c#{mod_name};

void Init_#{mod_name}() {
c#{mod_name} = rb_define_module(“#{mod_name}”);
rb_define_method(c#{mod_name}, "#{mymethod}", t#{mymethod}, -1);
}
}

  # Generating code
  f = File.new(src_name, "w")
  f.puts s
  f.close

  # Compiling
  cmd = "#{cc} -o #{so_name} #{src_name} #{libs}".gsub('/','\\') +
    " -link /INCREMENTAL:no /EXPORT:Init_#{mod_name}"
  `#{cmd}`
end

# Loading & Replacing w/ new method
require "#{so_name}"
myclass.class_eval("include #{mod_name}")
myclass.class_eval("alias_method :old_#{mymethod}, :#{mymethod}")
myclass.class_eval("alias_method :#{mymethod}, :_#{mymethod}")

# Calling
return method("_#{mymethod}").call(*args)

end
end

Park Heesob

tmpdir = ENV['TMP'] + "/.ruby_inline"

I’ve left /tmp in favor of the home directory. Is there something like
ENV[‘HOME’] on win?

Further, this change of yours doesn’t reflect the option of using
ENV[‘INLINEDIR’]. Any reason why?

  # Compiling
  cmd = "#{cc} -o #{so_name} #{src_name} #{libs}".gsub('/','\\') +
    " -link /INCREMENTAL:no /EXPORT:Init_#{mod_name}"
  `#{cmd}`
end

Why didn’t rbconfig add the -link line that you have above? I’m not
willing or able to accept this change as-is, because that should be
part of your rbconfig setup, if it isn’t, then that wasn’t the way ruby
was built and it is suspect. This change makes this section no longer
platform independent. This is further tangled by the gsub, as if the
-link section did come from rbconfig, then the gsub would mess it up.

Finally, the “.gsub(‘/’, ‘\’)” is also not platform independent. I’m
frustrated that it’s even necessary. As I understand it, perl just
automatically changes things like that (at least on open and other
obvious file references). Python has the very useful ‘os’ module. Is
this something that we need? How should I deal with this?

Suggestions?

···

On Wednesday, Sep 11, 2002, at 23:19 US/Pacific, Park Heesob wrote: