Author Topic: Building a ScriptBasic extension module  (Read 17468 times)

Offline AIR

  • BASIC Developer
  • Posts: 685
Re: Building a ScriptBasic extension module
« Reply #45 on: June 03, 2011, 07:07:14 PM »
Use the GSL_REAL and GSL_IMAG macros to extract the data from the struct. Then stuff them into a SB array and return that, so you can manipulate it using SB.

Offline John

  • Forum Support / SB Dev
  • Posts: 2747
    • ScriptBasic Open Source Project
Re: Building a ScriptBasic extension module
« Reply #46 on: June 03, 2011, 07:36:48 PM »
Sounds like a great way of handling it. This will allow the user to assign the results to any element (normal or associative array structure)

a[1000000000] = complex_rect(r,i)

PRINT "Real = ",a[1000000000,0],"/n"
PRINT "Imaginary = ",a[1000000000,1],"/n"

Offline John

  • Forum Support / SB Dev
  • Posts: 2747
    • ScriptBasic Open Source Project
Re: Building a ScriptBasic extension module
« Reply #47 on: June 03, 2011, 08:29:12 PM »
That isn't going to work.  >:(

Code: [Select]
FUNCTION complex_rect(arg1,arg2)
  LOCAL b
  b[0] = 0.123
  b[1] = 1.123
  complex_rect = b
END FUNCTION


a[1000000000] = complex_rect(r,i)

PRINT "Real = ",a[1000000000,0],"\n"
PRINT "Imaginary = ",a[1000000000,1],"\n"

(0): error &H2:Function can not return a whole array

The only time I have seen an SB extension module return an array given an undef variable name is FetchHash() and that is by using LEFTVALUE and being an argument.

I still think returning a string as two binary double values and using UNPACK to split them out to SB variables may be the easiest way without messing with the GSL calling prototype.

I'm assuming at this point that the extension module return has the same rules as a scriba function. (using same API under the covers)

Code: [Select]
FUNCTION complex_rect(arg1,arg2)
  LOCAL b
  b[0] = 0.123
  b[1] = 1.123
  complex_rect = b[0]
END FUNCTION


a[1000000000] = complex_rect(r,i)

PRINT "Real = ",FORMAT("%g",a[1000000000,0]),"\n"

Real = 0.123

It's a shame that SB can't return an array as a whole.
« Last Edit: June 03, 2011, 09:41:48 PM by JRS »

Offline AIR

  • BASIC Developer
  • Posts: 685
Re: Building a ScriptBasic extension module
« Reply #48 on: June 04, 2011, 12:41:33 PM »
Not sure what you're thinking there, but I was talking about something like this:

Code: [Select]
besFUNCTION(sb_complex_rect)
  double x,y;
  gsl_complex res, *resptr;

  besARGUMENTS("rr")
    &x,&y
  besARGEND 

  res = gsl_complex_rect(x, y);
  resptr = &res;
  besRETURN_POINTER(resptr);
besEND

besFUNCTION(sb_GSL_REAL)
  gsl_complex *p;

  p = besARGUMENT(1);
 
  besRETURN_DOUBLE(GSL_COMPLEX_P_REAL(p));
besEND

besFUNCTION(sb_GSL_IMAG)
  gsl_complex *p;

  p = besARGUMENT(1);
 
  besRETURN_DOUBLE(GSL_COMPLEX_P_IMAG(p));
besEND


Offline John

  • Forum Support / SB Dev
  • Posts: 2747
    • ScriptBasic Open Source Project
Re: Building a ScriptBasic extension module
« Reply #49 on: June 04, 2011, 04:59:17 PM »
Lost it in the S turns.  :D

Something isn't right. I get different results for the REAL part each time I run the test script changing nothing. The IMAG part is always zero. The other strange thing is that even though I'm using a format mask, it still returns a scientific notation based number.

Code: [Select]
DECLARE SUB sb_complex_rect ALIAS "sb_complex_rect" LIB "gsl"
DECLARE SUB sb_GSL_REAL ALIAS "sb_GSL_REAL" LIB "gsl"
DECLARE SUB sb_GSL_IMAG ALIAS "sb_GSL_IMAG" LIB "gsl"

pCR = sb_complex_rect(1.0,2.0)

r = sb_GSL_REAL(pCR)
i = sb_GSL_IMAG(pCR)

PRINT FORMAT("%.12g",r),"\n"
PRINT FORMAT("%.12g",i),"\n"

jrs@laptop:~/sb/test/gsl$ scriba compnum.sb
1.34175858007e-316
0

Offline John

  • Forum Support / SB Dev
  • Posts: 2747
    • ScriptBasic Open Source Project
Re: Building a ScriptBasic extension module
« Reply #50 on: June 05, 2011, 11:14:39 PM »
I have spent hours trying to make this work. I'm stuck and going to have to leave it in your hands to figure out why this isn't working.


Offline AIR

  • BASIC Developer
  • Posts: 685
Re: Building a ScriptBasic extension module
« Reply #51 on: June 06, 2011, 05:25:45 PM »
What have you tried?

The main issue is that SB returns everything as a string representation of the data.  Somehow the conversion going back into the module is getting messed up.

I've tried besHandleGetPointer and besHandleGetHandle with no success (you use those when dealing with pointers that more often than not get mangled when converted to SB's string represntation).

It might be that this is a lost cause.  The struct that's being filled isn't a pointer to a struct, which would simplify things.

I was thinking, though:  the point to calling complex_rect is to be able to get the resulting position vector, right?  So why not just return that, and be done with it?  Is it really useful to return the very numbers that are being passed to the function since we already know what they are?

Offline John

  • Forum Support / SB Dev
  • Posts: 2747
    • ScriptBasic Open Source Project
Re: Building a ScriptBasic extension module
« Reply #52 on: June 06, 2011, 06:01:15 PM »
Quote
What have you tried?

Just about every basext.h macro that is relevant.

We are creating a wrapper and I have no problem with us creating a higher level interface that works within the type-less nature of SB. Lets just do the hard stuff in C and return to scriba what it can deal with even if it changes the direct calling method one would normally do in C.