Run harbour Ruby C extension¶
ruby extconf.rb
... => Makefile
Makefile corrections:
DLDFLAGS = -L/usr/harbour/lib -L. -Wl,-Bsymbolic-functions -rdynamic -Wl,-export-dynamic LIBS = $(LIBRUBYARG_SHARED) -lncurses -lslang -lharbour -lgtcrs -la_plus_b -lX11 -lpthread -ldl -lcrypt -lm -lc
a_plus_b.prg¶
static n:=0 function n_1() n++ ? "n=", n function n_2() n := 2*n ? "n=", n function a_plus_b(a, b) ? "a=", a ? "b=", b return a + b function inkey_0() inkey(0) return nil
harbour_a_plus_b.c¶
ruby ekstenzija
#include <hbapi.h> #include "hbvmpub.h" #include "hbpcode.h" #include "hbinit.h" #include "hbvm.h" #include "hbapiitm.h" #include "extend.api" #ifdef __HARBOUR__ #include "hbundoc.api" #endif #include <ruby.h> HB_FUNC_EXTERN( HB_GT_CRS ); void _hb_lnk_ForceLink_build( void ) { HB_FUNC_EXEC( HB_GT_CRS ); } HB_EXTERN_BEGIN extern char * s_defaultGT; extern char * s_pszLinkedMain; HB_EXTERN_END HB_CALL_ON_STARTUP_BEGIN( hb_lnk_SetDefault_build ) s_defaultGT = "CRS"; HB_CALL_ON_STARTUP_END( hb_lnk_SetDefault_build ) static VALUE harbour_init(VALUE self, VALUE format, VALUE num) { hb_vmInit(0); return INT2FIX(1); } static VALUE harbour_n_1(VALUE self, VALUE format, VALUE num) { HB_FUN_N_1(); return INT2FIX(1); } static VALUE harbour_n_2(VALUE self, VALUE format, VALUE num) { HB_FUN_N_2(); return INT2FIX(1); } static VALUE harbour_inkey_0(VALUE self, VALUE format, VALUE num) { HB_FUN_INKEY_0(); return INT2FIX(1); } static VALUE harbour_f1(VALUE self, VALUE format, VALUE num) { double dbl_1 = 1; double dbl_2 = 10; PHB_ITEM p1 = hb_itemPutND( NULL, dbl_1 ); PHB_ITEM p2 = hb_itemPutND( NULL, dbl_2 ); PHB_ITEM pRez = hb_itemDoC( "A_PLUS_B", 2, p1, p2, 0 ); if HB_IS_NUMBER(pRez) { printf("is number"); dbl_2 = hb_itemGetND( pRez ); printf("rezultat %lf", dbl_2); hb_itemRelease(pRez); } hb_itemRelease( p1 ); hb_itemRelease( p2 ); return INT2FIX(1); } void Init_harbour_a_plus_b() { /* register the method to the Kernel module */ rb_define_method(rb_mKernel, "harbour_init", RUBY_METHOD_FUNC(harbour_init), 0); rb_define_method(rb_mKernel, "harbour_f1", RUBY_METHOD_FUNC(harbour_f1), 0); rb_define_method(rb_mKernel, "harbour_n_1", RUBY_METHOD_FUNC(harbour_n_1), 0); rb_define_method(rb_mKernel, "harbour_n_2", RUBY_METHOD_FUNC(harbour_n_2), 0); rb_define_method(rb_mKernel, "harbour_inkey_0", RUBY_METHOD_FUNC(harbour_inkey_0), 0); }
test_harbour_a_plus_b.rb
require 'harbour_a_plus_b' harbour_init harbour_f1 harbour_n_1 harbour_n_2 harbour_n_2 for i in 1..10 harbour_n_1 harbour_inkey_0 end
run_test.sh
#!/bin/bash LD_LIBRARY_PATH=/usr/harbour/lib:. ruby test.rbit work's as expected:
- static vars are preserved
- inkey_0 wait for user input