Author Topic: ScriptBasic Extension Modules written in Oxygen  (Read 21353 times)

0 Members and 1 Guest are viewing this topic.

JRS

  • Guest
Re: ScriptBasic Extension Modules written in Oxygen
« Reply #45 on: May 28, 2012, 12:27:00 PM »
I have be working with Armando (AIR) on the CBX extension module and will release a Windows 32 DLL and Linux 64 bit shared object. (.so) This is the planned function list as it stands now.

  • DIM - Fast way to initialize a single dimension array with a few tricks.
  • FGET - File to string. (no need to OPEN, read or figure out it's length)
  • FPUT - String to file. (same applies here as well)
  • VARPTR - Returns the pointer to any SB variable.
  • PEEK - Returns a string starting at the address given and the number of bytes requested.
  • POKE - Assign memory a string  of bytes starting and the address provided.

Charles Pegge

  • Guest
Re: ScriptBasic Extension Modules written in Oxygen
« Reply #46 on: May 28, 2012, 08:47:30 PM »
Hi John,

Are you thinking about incorporating any of these into The ScriptBasic core eventually?

I think Dim, Fget and FPut are good candidates.

Charles

JRS

  • Guest
Re: ScriptBasic Extension Modules written in Oxygen
« Reply #47 on: May 28, 2012, 08:58:06 PM »
I'm still learning how to build extension modules. My next task is to learn how to add functionality to scriba's syntax command table. It shouldn't be too hard once I get the hang of it.

I could statically link CBX into scriba but you would still need to DECLARE the functions. This might be the next step before adding them as keywords in the Basic.  (sbhttpd static links MT and CGI extension modules into it's executable)

« Last Edit: May 28, 2012, 09:08:48 PM by JRS »

JRS

  • Guest
Re: ScriptBasic Extension Modules written in Oxygen
« Reply #48 on: May 29, 2012, 12:20:29 AM »
I have finished the new VARPTR and PEEK functions for the CBX extension module. I'm happy with the flexabilty these two functions offer to the SB programmer. The PEEK function only works with string variables at this time. I don't see POKE being much of job and will post it soon.

Code: [Select]
DECLARE SUB VARPTR ALIAS "varptr" LIB "cbx"
DECLARE SUB PEEK ALIAS "PEEK" LIB "cbx"

a = "JRS"
v = VARPTR(a)

PRINT PEEK(v,2),"\n"
PRINT PEEK(a,2),"\n"

v = VARPTR(MID(a,2))
PRINT PEEK(v,2),"\n"
PRINT PEEK(MID(a,2),2),"\n"
PRINT PEEK(v+1,1),"\n"

jrs@laptop:~/sb/test$ scriba peektest.sb
JR
JR
RS
RS
S
jrs@laptop:~/sb/test$


Code: C
  1. besFUNCTION(varptr)  
  2.   VARIABLE Argument;
  3.   long addr_ptr;
  4.  
  5.   Argument = besARGUMENT(1);
  6.   besDEREFERENCE(Argument);
  7.   addr_ptr = (long)LONGVALUE(Argument);
  8.  
  9.   besRETURN_LONG(addr_ptr);
  10. besEND
  11.  
  12.  
  13. besFUNCTION(PEEK)
  14.   VARIABLE Argument;
  15.   long addr_ptr;
  16.   int numbytes;
  17.  
  18.   Argument = besARGUMENT(1);
  19.   besDEREFERENCE(Argument);
  20.   addr_ptr = (long)LONGVALUE(Argument);
  21.  
  22.   Argument = besARGUMENT(2);
  23.   besDEREFERENCE(Argument);
  24.   numbytes = (int)LONGVALUE(Argument);
  25.  
  26.   besRETURN_MEM(addr_ptr,numbytes);
  27.  
  28. besEND
  29.  


Quote from: AIR
BTW, be very careful with POINTERS.  A common trap is trying to assign the value of an int to a pointer and vice-versa.  It's fine on a 32bit machine, but will throw errors on a 64bit machine because int's are ALWAYS 32bit.  Safe bet is to replace int with LONG, which is 32bit on those types of systems, and 64bit on 64bit systems (the same rule applies to POINTER).
« Last Edit: May 29, 2012, 01:35:05 AM by JRS »

JRS

  • Guest
Re: ScriptBasic Extension Modules written in Oxygen
« Reply #49 on: May 29, 2012, 03:02:46 AM »
My next task is to see how VARPTR and PEEK perform outside the ScriptBasic variable environment. In theory, I should be able to pass a variable initialized to 0, get the VARPTR of that numeric variable and pass it to a function that may assign a pointer to a structure. I could then use the pointer assigned with PEEK to retrieve elements of the structure.

I also want to create a test function in the CBX module that create a global char string outside the SB environment and returns the pointer (address) to SB. I will then us PEEK with the returned pointer to access the module global variable. I'll post my test results tomorrow. (bed time)

 

JRS

  • Guest
Re: ScriptBasic Extension Modules written in Oxygen
« Reply #50 on: May 29, 2012, 08:06:32 PM »
As things turned out, we now have a STRPTR and VARPTR function that works properly with PEEK.

Code: [Select]
DECLARE SUB STRPTR ALIAS "STRPTR" LIB "cbx"
DECLARE SUB VARPTR ALIAS "VARPTR" LIB "cbx"
DECLARE SUB PEEK ALIAS "PEEK" LIB "cbx"

a = "ABC"
v = STRPTR(a)

PRINT PEEK(v,2),"\n"
PRINT PEEK(a,2),"\n"

v = STRPTR(MID(a,2))
PRINT PEEK(v,2),"\n"
PRINT PEEK(MID(a,2),2),"\n"
PRINT PEEK(v+1,1),"\n"

b = 1 + 2
v = VARPTR(b)
PRINT v,"\n"
PRINT PEEK(v,1),"\n"
PRINT ASC(PEEK(v,1)),"\n"


JRS

  • Guest
Re: ScriptBasic Extension Modules written in Oxygen
« Reply #51 on: May 29, 2012, 09:57:53 PM »
I was testing the VARPTR and STRPTR functions with arrays and it seems to work. I'm still looking at floating point variables. I may need to create a PEEKD function to to deal with them. This library is still in flux as these new functions come online.

@Charles: I should be able to pass SB pointers to your O2 JIT script functions and they be valid in O2. This could save a lot of travel time.

Code: [Select]
DECLARE SUB STRPTR ALIAS "STRPTR" LIB "cbx"
DECLARE SUB VARPTR ALIAS "VARPTR" LIB "cbx"
DECLARE SUB PEEK ALIAS "PEEK" LIB "cbx"

b[1] = "CAT"
v = STRPTR(b[1])
PRINT v,"\n"
PRINT PEEK(v,3),"\n"

b[1] = 1
v = VARPTR(b[1])
PRINT v,"\n"
PRINT PEEK(v,1),"\n"
PRINT ASC(PEEK(v,1)),"\n"

a{"one"} = "SB"
v = STRPTR(a{"one"})
PRINT v,"\n"
PRINT PEEK(v,2),"\n"

jrs@laptop:~/sb/test$ scriba peektest.sb
28895367
CAT
28845048
<unprintable character symbol here>
1
28895401
SB
jrs@laptop:~/sb/test$

I have decided to change the module name to SB3. The idea is that version 3 of ScriptBasic can be prototyped in an extension module and allow users to try these upcoming core features of the next release. I think this will allow others to contribute to the project easier and learn the SB API. (macro mania - actual product using the name)
« Last Edit: May 30, 2012, 01:45:52 AM by JRS »

Charles Pegge

  • Guest
Re: ScriptBasic Extension Modules written in Oxygen
« Reply #52 on: May 30, 2012, 02:24:11 AM »
Hi John,
I don't think passing pointers to O2 would make any difference, since the module would still have to go through the parameter retrieval process to access the pointer itself.

However, my O2-side string access could be improved by offering direct access to the SB string instead of copying it into a new O2 string

GetZstringPar

This is ideal if the string is to be patched but not altered in length.

Code: OxygenBasic
  1.  
  2.     macro GetZstringPar(v,pst,pp,pn) 'works with char* /zstring*
  3.    scope
  4.     iopar(pst,pp,pn,0xf4)
  5.     @v=*q
  6.     end scope
  7.     end macro
  8.  
  9.     macro GetStringPar(v,pst,pp,pn)
  10.     scope
  11.     iopar(pst,pp,pn,0xf4)
  12.     zstring pz at *q
  13.     v=pz
  14.     end scope
  15.     end macro
  16.  

JRS

  • Guest
Re: ScriptBasic Extension Modules written in Oxygen
« Reply #53 on: May 30, 2012, 01:54:22 PM »
I think Peter (BaCon) did a nice job with his implementation of PEEK/POKE. I like the idea of defining the type of data your after. It could be a PITA if the SB user has to untangle a binary representation of a numeric variable getting a series of consecutive bytes. I'm not sure if I'm going to go with an option based setting or create variation functions.

Example:

PEEKB - BYTE
PEEKI - INTEGER
PEEKL - LONG
PEEKD - DOUBLE
PEEKF - FLOAT

PEEK alone assumes a BYTE and supports the length optional parameter. All other variations are one TYPE at a time. I could see down the road adding a template like option to extract complex structures and assign them to meaningful associative/mixed SB arrays.

Comments welcome!

Aurel

  • Guest
Re: ScriptBasic Extension Modules written in Oxygen
« Reply #54 on: May 30, 2012, 08:56:06 PM »
I don't know from where Peter(bacon) get this idea but same thing are implemented in PureBasic.
PEEKB - BYTE
PEEKI - INTEGER
PEEKL - LONG
PEEKD - DOUBLE
PEEKF - FLOAT

JRS

  • Guest
Re: ScriptBasic Extension Modules written in Oxygen
« Reply #55 on: May 30, 2012, 10:56:31 PM »
I looking for what the preference might be for PEEK & POKE data type handling. Peter used an option setting to determine what type the PEEK or POKE would return or set. As you have mentioned, other Basic languages use the method I posted. I'm leaning towards having separate function variations for PEEK & POKE.

Charles Pegge

  • Guest
Re: ScriptBasic Extension Modules written in Oxygen
« Reply #56 on: May 31, 2012, 04:03:03 AM »
There is no Peek and Poke in Oxygen. Instead, I have standardised on declarations like:

byte b at p

where sys variable p holds the address. Whenever p is changed then b will contain the byte at the new address.

The advantage is that any type may be specified, and arrays can also be used.

Charles

JRS

  • Guest
Re: ScriptBasic Extension Modules written in Oxygen
« Reply #57 on: May 31, 2012, 12:27:16 PM »
Quote
There is no Peek and Poke in Oxygen. Instead, I have standardised on declarations like:

byte b at p

Code: Text
  1. byte b at p
  2. integer i at p
  3. long l at p
  4. double d at p
  5. float f at p
  6.  

SB is sort of doing the same thing but using variations of the PEEK.

PEEKB - BYTE
PEEKI - INTEGER
PEEKL - LONG
PEEKD - DOUBLE
PEEKF - FLOAT
Posted on: May 30, 2012, 01:54:22 PM

JRS

  • Guest
Re: ScriptBasic Extension Modules written in Oxygen
« Reply #58 on: June 13, 2012, 11:51:48 AM »
Charles,

Armando started working on a cross platform SQLite3 extension module. (C) The SB ext. API (macros) sure makes interfacing to other libraries a breeze. The demo is based on your example earlier in this thread.

interface.c
Code: [Select]
/*
  FILE   : interface.c
  HEADER : interface.h
  BAS    : sqlite.bas
  AUTHOR : Armando I. Rivera

  DATE:

  CONTENT:
  This is the interface.c file for the ScriptBasic module sqlite3

NTLIBS:
UXLIBS: -lc -ldl -lpthread
DWLIBS: -lsqlite3 -lpthread
ADLIBS: sqlite3.a
*/

/*
TO_BAS:
declare sub     ::OPEN alias "sql3_open"         lib "sqlite"
declare sub     ::CLOSE alias "sql3_close"        lib "sqlite"
declare sub     ::EXECUTE alias "sql3_execute"      lib "sqlite"
declare sub     ::QUERY alias "sql3_query"        lib "sqlite"
declare sub     ::ROW alias "sql3_row"          lib "sqlite"
declare sub     ::ROW_VALUE alias "sql3_row_value"    lib "sqlite"
declare sub     ::COLUMN_COUNT alias "sql3_column_count" lib "sqlite"
declare sub     ::COLUMN_NAME alias "sql3_column_name"  lib "sqlite"
declare sub     ::FINALIZE alias "sql3_finalize"     lib "sqlite"
declare sub     ::VERSION alias "sql3_version"      lib "sqlite"
*/


#include <stdio.h>
#include <string.h>

#include "../../basext.h"
#include "sqlite3.h"

besVERSION_NEGOTIATE
  return (int)INTERFACE_VERSION;
besEND

besSUB_START
  long *p;

  besMODULEPOINTER = besALLOC(sizeof(long));
  if( besMODULEPOINTER == NULL )return 0;

  p = (long*)besMODULEPOINTER;
  return 0;
besEND

besSUB_FINISH
  long *p;

  p = (long*)besMODULEPOINTER;
  if( p == NULL )return 0;
  return 0;
besEND


besFUNCTION(sql3_open)
     sqlite3 *db;
     const char *fileName;
     int i;

     besARGUMENTS("s")
          &fileName
     besARGEND

     i = sqlite3_open(fileName, &db);
     besRETURN_POINTER(db)
besEND

besFUNCTION(sql3_close)
     sqlite3 *db;
     int i;

     besARGUMENTS("p")
          &db
     besARGEND

     i = sqlite3_close(db);
     besRETURN_LONG(i)
besEND

besFUNCTION(sql3_execute)
    sqlite3 *db;
    char *sqlcmd;
    int ret;

    besARGUMENTS("ps")
        &db,&sqlcmd
    besARGEND
    ret = sqlite3_exec(db,sqlcmd,NULL,NULL,NULL);
    besRETURN_LONG(ret)
besEND

besFUNCTION(sql3_query)
    sqlite3 *db;
    sqlite3_stmt *stmt;
    char *sqlcmd;
    int ret;

    besARGUMENTS("ps")
        &db,&sqlcmd
    besARGEND
    ret = sqlite3_prepare_v2(db,sqlcmd,strlen(sqlcmd)+1,&stmt,NULL);
    besRETURN_POINTER(stmt)
besEND

besFUNCTION(sql3_row)
     sqlite3_stmt *stmt;
     int i;

     besARGUMENTS("p")
          &stmt
     besARGEND

     i = sqlite3_step(stmt);
     besRETURN_LONG(i)

besEND

besFUNCTION(sql3_row_value)
     sqlite3_stmt *stmt;
     const char* cur_column_text;
     int i;

     besARGUMENTS("pi")
          &stmt,&i
     besARGEND

     cur_column_text = sqlite3_column_text(stmt,i);
     besRETURN_STRING(cur_column_text)

besEND

besFUNCTION(sql3_column_count)
     sqlite3_stmt *stmt;
     int i;

     besARGUMENTS("p")
          &stmt
     besARGEND

     i = sqlite3_column_count(stmt);
     besRETURN_LONG(i)

besEND

besFUNCTION(sql3_column_name)
     sqlite3_stmt *stmt;
     const char* cur_column_name;
     int i;

     besARGUMENTS("pi")
          &stmt,&i
     besARGEND

     cur_column_name = sqlite3_column_name(stmt,i);
     besRETURN_STRING(cur_column_name)

besEND

besFUNCTION(sql3_finalize)
     sqlite3_stmt *stmt;
     int i;

     besARGUMENTS("p")
          &stmt
     besARGEND

     i = sqlite3_finalize(stmt);
     besRETURN_LONG(i)
besEND

besFUNCTION(sql3_version)
    const char *ver = sqlite3_libversion();
    besRETURN_STRING(ver)
besEND


START_FUNCTION_TABLE(SQLITE_SLFST)
// Ext. module
  EXPORT_MODULE_FUNCTION(versmodu)
  EXPORT_MODULE_FUNCTION(bootmodu)
  EXPORT_MODULE_FUNCTION(finimodu)

// MOUDLE FUNCTIONS
  EXPORT_MODULE_FUNCTION(sql3_open)
  EXPORT_MODULE_FUNCTION(sql3_close)
  EXPORT_MODULE_FUNCTION(sql3_execute)
  EXPORT_MODULE_FUNCTION(sql3_query)
  EXPORT_MODULE_FUNCTION(sql3_row)
  EXPORT_MODULE_FUNCTION(sql3_row_value)
  EXPORT_MODULE_FUNCTION(sql3_column_count)
  EXPORT_MODULE_FUNCTION(sql3_column_name)
  EXPORT_MODULE_FUNCTION(sql3_finalize)
  EXPORT_MODULE_FUNCTION(sql3_version)
END_FUNCTION_TABLE

sqltest.sb
Code: [Select]
INCLUDE sqlite.bas

hdb=sqlite::open("testsql")
sqlite::execute(hdb,"create table demo (someval integer, sometxt text);")
sqlite::execute(hdb, "INSERT INTO demo VALUES (123,'hello');")
sqlite::execute(hdb, "INSERT INTO demo VALUES (234, 'cruel');")
sqlite::execute(hdb, "INSERT INTO demo VALUES (345, 'world');")

stmt = sqlite::query(hdb,"SELECT * FROM demo;")

while (sqlite::row(stmt) = 100)
    pr = pr & sqlite::row_value(stmt,0) & " - " & sqlite::row_value(stmt,1) & "\n"
wend

sqlite::close(hdb)

print pr

print "SQLite Version: ",sqlite::version(),"\n"

jrs@ip-10-166-185-35:~/tmp$ scriba sqltest.sb
123 - hello
234 - cruel
345 - world
SQLite Version: 3.7.12.1
jrs@ip-10-166-185-35:~/tmp$

Charles Pegge

  • Guest
Re: ScriptBasic Extension Modules written in Oxygen
« Reply #59 on: June 13, 2012, 12:57:23 PM »

Thats great, John!

Glut/OpenGL will be more of a challenge :)

Is there a way of launching SB without the console? This would be the preferred behaviour for graphical applications in Windows at least. The only way we could do it last time, was to embed SB in Oxygen.

Charles