Author Topic: ScriptBasic O2h Embedding  (Read 26919 times)

0 Members and 1 Guest are viewing this topic.

JRS

  • Guest
Re: ScriptBasic O2h Embedding
« Reply #15 on: May 08, 2011, 09:13:01 AM »
With DYC, you can load and access a DLL at any point during runtime. GTK-Server works in the same way with it's FFI solution. Scripting APIs is the primary goal of ScriptBasic rather than having to use a C compiler to create an extension module that must be loaded before the script runs.

I'll take a look at the examples using the enhanced SBO2.DLL you attached.

Thanks for all your help with this!
« Last Edit: May 08, 2011, 10:25:53 AM by JRS »

JRS

  • Guest
Re: ScriptBasic O2h Embedding
« Reply #16 on: May 08, 2011, 09:33:03 AM »
Here are a couple of examples of using a DYC like FFI using GTK-Server under Linux.

Self embeding of SB.
Code: [Select]
DECLARE SUB DLL ALIAS "_idll" LIB "gtk-server"
DECLARE SUB REQUIRE ALIAS "_idll_require" LIB "gtk-server"
DECLARE SUB DEFINE ALIAS "_idll_define" LIB "gtk-server"

REQUIRE "libiDLL.so"

DEFINE "sb_new NONE POINTER 0"
DEFINE "scriba_LoadConfiguration NONE INT 2 POINTER STRING"
DEFINE "scriba_SetFileName NONE INT 2 POINTER STRING"
DEFINE "scriba_Run NONE INT 2 POINTER STRING"
DEFINE "scriba_LoadSourceProgram NONE INT 1 POINTER"
DEFINE "scriba_destroy NONE NONE 1 POINTER"

pProgram = DLL("sb_new")
DLL("scriba_LoadConfiguration " & pProgram & " \"/etc/scriba/basic.conf\"")
DLL("scriba_SetFileName " & pProgram & " \"E01.sb\"")
DLL("scriba_LoadSourceProgram " & pProgram)
DLL("scriba_Run " & pProgram & " \"JRS\"")
DLL("scriba_destroy " & pProgram)

E01.sb
Code: [Select]
cmd = COMMAND()

PRINT "ARG = ",cmd,"\n"

FOR x = 1 TO 10
  PRINT x,"\n"
NEXT

jrs@Laptop:~/SB/test$ scriba embed.sb
ARG = JRS
1
2
3
4
5
6
7
8
9
10
jrs@Laptop:~/SB/test$


This example is scripting the SQLite3 API dynamically at runtime and using the VARPTR function added to GTK-Server for SB.
Code: [Select]
' SQLite3 Test Script

DECLARE SUB DLL ALIAS "_idll" LIB "gtk-server"
DECLARE SUB REQUIRE ALIAS "_idll_require" LIB "gtk-server"
DECLARE SUB DEFINE ALIAS "_idll_define" LIB "gtk-server"
DECLARE SUB VARPTR ALIAS "varptr" LIB "gtk-server"

REQUIRE "libsqlite3.so"

DEFINE "sqlite3_open NONE INT 2 STRING LONG"
DEFINE "sqlite3_exec NONE INT 5 LONG STRING INT NULL PTR_STRING"
DEFINE "sqlite3_prepare_v2 NONE INT 5 LONG STRING INT PTR_LONG NULL"
DEFINE "sqlite3_step NONE INT 1 LONG"
DEFINE "sqlite3_column_text NONE STRING 2 LONG INT"
DEFINE "sqlite3_close NONE INT 1 LONG"

CONST SQLITE_ROW = 100
db = 0
dberr = 0
stmt = 0

DLL("sqlite3_open \"testsql\" " & VARPTR(db))
errmsg = DLL("sqlite3_exec " & db & " \"CREATE TABLE demo(someval INTEGER,  sometxt TEXT);\" 0 0 " & VARPTR(dberr))
PRINT "DB ERROR: ",errmsg,"\n"
DLL("sqlite3_exec " & db & " \"INSERT INTO demo VALUES (123, 'Hello');\" 0 0 " & VARPTR(dberr))
DLL("sqlite3_exec " & db & " \"INSERT INTO demo VALUES (234, 'cruel');\" 0 0 " & VARPTR(dberr))
DLL("sqlite3_exec " & db & " \"INSERT INTO demo VALUES (345, 'world');\" 0 0 " & VARPTR(dberr))
result = DLL("sqlite3_prepare_v2 " & db & " \"SELECT * FROM demo;\" -1 " & VARPTR(stmt) & " 0")
SPLIT result BY " " TO ok, stmt

WHILE DLL("sqlite3_step " & stmt) = SQLITE_ROW
  PRINT DLL("sqlite3_column_text " & stmt & " " & 0) & " - " & DLL("sqlite3_column_text " & stmt & " " & 1),"\n"
WEND

DLL("sqlite3_close " & db)

jrs@laptop:~/sb/test$ scriba sqlite3.sb
DB ERROR: 0 (null)
123 - Hello
234 - cruel
345 - world
jrs@laptop:~/sb/test$

JRS

  • Guest
Re: ScriptBasic O2h Embedding
« Reply #17 on: May 08, 2011, 09:26:20 PM »
Charles,

Here is the sbo2demo6.sb redone a bit.

Code: [Select]
declare sub dll alias "dyc" lib "dyc"

'OXYGEN SOURCE CODE
'------------------
prog="""

  tab=chr 9
  single  v=(1 2 3 4)*2+1.25
  bstring s="result:" tab str(v) tab
  mov eax,s 'direct return (the string will be copied back to SB)
""" & 0x0

bufferlength = 1000
receive = space(bufferlength)

dll "ms,i,sbo2.dll,run,ZZL", prog, receive, bufferlength

split receive by "\t" to s1, s2, s3
print trim(receive),"\n"
print format("%6.4f",s2),"\n"

C:\o2h\A32\examples\SBO2>scriba sbo2-6.sb
result: 21.25
21.2500

C:\o2h\A32\examples\SBO2>

I just downloaded A33 and tried the above. I had use the sbo2.dll you recently posted to get it to work.

C:\o2h\a33\examples\SBO2>scriba sbo2-6.sb
result: 21.25
21.2500

C:\o2h\a33\examples\SBO2>
« Last Edit: May 09, 2011, 12:15:19 AM by JRS »

JRS

  • Guest
Re: ScriptBasic O2h Embedding
« Reply #18 on: May 09, 2011, 02:00:44 AM »
The following is the code Armando gave me to add VARPTR to GTK-Server so I could use SB variables as placeholders for pointers I may need to pass. I even created a STRPTR function in SB with a combination of VARPTR and calling the C API strcpy() via GTK-Server.

Code: [Select]
besFUNCTION(varptr)
  VARIABLE ptr;

  if(besARGNR>1) return EX_ERROR_TOO_MANY_ARGUMENTS;
  if(besARGNR<1) return EX_ERROR_TOO_FEW_ARGUMENTS;

  besALLOC_RETURN_LONG
  
  ptr = besARGUMENT(1);
  besDEREFERENCE(ptr);
  
  LONGVALUE(besRETURNVALUE) = (int)ptr;

besEND

You should be able to drop this into DYC verbatim to give it a VARPTR function. Can you add the couple extra functions SB requires to sbo2.dll to become an extension module so you have full access to the SB API? Passing buffers then becomes unnecessary. You would be able to control all of SB's variables and other aspects from O2h.

« Last Edit: May 09, 2011, 02:08:14 AM by JRS »

Charles Pegge

  • Guest
Re: ScriptBasic O2h Embedding
« Reply #19 on: May 09, 2011, 08:39:53 AM »
John,

Based on your mods to Demo 6, the next step is to create a persistent program from SB and be able to call its functions at any time from the SB application.

'COMPILE --> ERROR CHECK --> STARTUP --> FUNCTION CALLS --> STOP


This example also demonstrates direct coupling (by pointer) to the send buffer and also the receive buffer:


dll ("ms,i,sbo2.dll,callfun,LzZL", mainfun, send, receive, bufferlength)



Code: [Select]

'--------------------------------------
'WORKING WITH A PERSISTENT O2H PROGRAME
'--------------------------------------


declare sub dll alias "dyc" lib "dyc"

'OXYGEN SOURCE CODE
'
prog="""

  function main(zstring * SBSends, zstring * SBReceives, sys bufferlength) as sys external
    'print SBsends
    string tab=chr 9, cr=chr(13)+chr(10)
    single v=(1 2 3 4)*2+1.25
    SBReceives = SBSends cr "O2H responds" tab str(v) tab chr(0)
  end function

  map[1]= & main


""" & chr(0)

mainfun=1
send="SB Sends a message" & chr(0)
bufferlength = 1000
receive = space(bufferlength)

'COMPILE --> ERROR CHECK --> STARTUP --> FUNCTION CALLS --> STOP

compilerrl= dll( "ms,i,sbo2.dll,compile,ZZL", prog, receive, bufferlength)
if compilerrl>0 then
  print left(receive,compilerrl)
else
  dll ("ms,i,sbo2.dll,start,L",0)
  dll ("ms,i,sbo2.dll,callfun,LzZL", mainfun, send, receive, bufferlength)
  '
  split receive by "\t" to s1, s2, s3
  print s1,"\n"
  print format("%6.4f",s2),"\n"
  '
  dll ("ms,i,sbo2.dll,stop,L", 0)
end if


line input w

Charles

[attachment deleted by admin]

JRS

  • Guest
Re: ScriptBasic O2h Embedding
« Reply #20 on: May 09, 2011, 10:04:27 AM »
That is cool to see an exported function being called on-the-fly. I'm assuming this is all done in memory and a DLL isn't written to disk?

I think Peter did a great job with GTK-Server. I have been slowly gutting the interface to remove everything not SB and FFI specific. (TCP, FIFO, standalone exe, xForms, ...) I'm calling the new version iDLL (interpretive Dynamic Linking Library) The current GTK-Server for Windows Peter pre-compiled doesn't seem to work under Wine but works under Native Windows. (only tried XP) The DYC interface is very close and I always wondered if Peter used it as a POC for GTK-Server.

Is the next step turning SBO2.dll into a SB extension module so you have full access to the SB API?


Charles Pegge

  • Guest
Re: ScriptBasic O2h Embedding
« Reply #21 on: May 09, 2011, 11:11:07 AM »
Not yet John. The next step is to test with a threaded Window which will spin its own message loop independently and leave SB to interact with it asynchronously.

Charles

JRS

  • Guest
Re: ScriptBasic O2h Embedding
« Reply #22 on: May 09, 2011, 05:44:06 PM »
Charles,

I thought I would give GTK-Server under Windows XP a try and can't seem to get the O2h print text to show up in the dialog. What does #qNAN mean?

Code: [Select]
declare sub dll alias "_gtk" lib "gtk-server"

dll("gtk_server_require oxygen.dll")
dll("gtk_server_define o2_mode NONE NONE 1 LONG")
dll("gtk_server_define o2_asmo NONE NONE 1 STRING")
dll("gtk_server_define o2_basic NONE NONE 1 STRING")
dll("gtk_server_define o2_buf NONE LONG 1 LONG")
dll("gtk_server_define o2_errno NONE LONG 0")
dll("gtk_server_define o2_error NONE PTR_STRING 0")
dll("gtk_server_define o2_exec NONE LONG 1 LONG")
dll("gtk_server_define o2_get NONE STRING 0")
dll("gtk_server_define o2_len NONE LONG 0")
dll("gtk_server_define o2_prep NONE STRING 1 STRING")
dll("gtk_server_define o2_put NONE NONE 1 STRING")
dll("gtk_server_define o2_view NONE STRING 1 STRING")

o2src = """

print "Hello from OxygenBasic!"
""" & 0x0

dll("o2_mode 0")
dll("o2_basic " & o2src)
dll("o2_exec 0")

end



FWIW I put a o2_errno function call after the o2_basic and o2_exec and they both returned zero.

Code: [Select]
o2src = """

a$ = "Hello from OxygenBasic!"
print a$
""" & 0x0

This causes an exception error with Windows.
« Last Edit: May 09, 2011, 07:24:25 PM by JRS »

Charles Pegge

  • Guest
Re: ScriptBasic O2h Embedding
« Reply #23 on: May 09, 2011, 10:10:46 PM »
Hi John,

Could you try terminating the source code string with  & chr(0) instead of & 0x0.

I found SB did not concatenate a null so the compiler was overrunning source code strings.

qNAN is a message from the FPU saying that the value it has been given is Not A Number. 'q' means quiet. This indicates corrupt data at runtime.

May I have a copy of your GTK_server module for testing? My developer SB does not have it.

Charles

PS: o2_error and o2_errno (and o2_len) do not take parameters, hence the crash!

« Last Edit: May 09, 2011, 10:36:23 PM by Charles Pegge »

JRS

  • Guest
Re: ScriptBasic O2h Embedding
« Reply #24 on: May 09, 2011, 11:34:53 PM »
That didn't work either. I even tried it without a null and get the same message.

You can get GTK-Server from Peter's site.

http://www.gtk-server.org

Within the modules -> ScriptBasic folder, you will notice gtk-server.dll (goes in your modules SB directory) and the gtk.bas (goes in your SB include but I don't use this file)

Quote
PS: o2_error and o2_errno (and o2_len) do not take parameters, hence the crash!

These functions were defined without a argument.

dll("gtk_server_define o2_errno NONE LONG 0")
dll("gtk_server_define o2_error NONE PTR_STRING 0")
dll("gtk_server_define o2_len NONE LONG 0")

The first parameter after the function name is the callback, (not used) next is the return type, then the number of arguments. (zero in the case of these three functions) The PTR_STRING is a special type that returns the string contents instead if the pointer for it.

This document should help explain the definition options.  gtk-server.cfg

Update

o2src = "print chr(50)"

dll("o2_basic \"" & o2src & "\"")

This prints a 2 which is what I would expect. GTK-Server uses the S-LANG syntax and a space separates the parameters. It can get tricky when an argument has embedded spaces.

o2src = """
print chr(74) chr(82) chr(83)
"""

This prints JRS

I can't get it to work with any embedded " characters. I don't know if this is a GTK-Server or OXYGEN.DLL issue.
« Last Edit: May 10, 2011, 12:48:20 AM by JRS »

Charles Pegge

  • Guest
Re: ScriptBasic O2h Embedding
« Reply #25 on: May 10, 2011, 03:40:43 AM »

John,

Got the GTK server and module in place

I can get it to run. (I can deliberately crash it with Assembler) but I can't get it to display a message on my system. Very odd.

I don't think GTK offers any advantage over DYC anyway. We still need an SBO2.

Charles

JRS

  • Guest
Re: ScriptBasic O2h Embedding
« Reply #26 on: May 10, 2011, 08:34:14 AM »
It was worth a try. Support the Gtk under Windows is fading and Peter isn't doing any further development with GTK-Server now that he has BaCon. Thanks for having s look at it.

« Last Edit: May 10, 2011, 08:51:11 AM by JRS »

Charles Pegge

  • Guest
Re: ScriptBasic O2h Embedding
« Reply #27 on: May 10, 2011, 09:58:05 AM »

The Gtk module is large! (250k+)

Anyway I've now got a threaded window running nicely from SB and also more flexible parameter passing with an extra function in SBO2:

This is how it could be used:

SB code
Code: [Select]
   dll ("ms,i,sbo2.dll,callflex,LLLL", SetPosition,2,x,y)
   dll ("ms,i,sbo2.dll,callflex,LLLLZ", SetText,3,x,y,"Hello!")

Charles

JRS

  • Guest
Re: ScriptBasic O2h Embedding
« Reply #28 on: May 10, 2011, 11:29:20 AM »
Sweet!

I have to agree that Gtk for Windows is a losing battle. Qt is the only hope for a cross platform solution and with Microsoft partnering with Nokia on the mobile end, this may be an easier task moving forward.

Quote
The Gtk module is large! (250k+)

DYC is 200K less.  :)

I can see using O2 as a replacement for the scriba C wrapper that was used as an example for the embeddable SBAPI. This would make the Windows version much more attractive with a GUI IDE/Designer/Debugger instead of just an easy to use console scripting language. This would also remove any limitations and give SB a JIT compiler option for those processor intensive tasks.

O2 embeds SB which embeds O2 for GUI, JIT compiling, OpenGL and more.

I will start testing as soon as you are ready to release a new SBO2.dll to work with.


« Last Edit: May 10, 2011, 06:41:50 PM by JRS »

Charles Pegge

  • Guest
Re: ScriptBasic O2h Embedding
« Reply #29 on: May 12, 2011, 01:21:08 AM »

Hi John,

I now have a threaded window working under SB with a simple protocol for passing messages between SB and the Window thread. But I have put some of the components into the inc/ folder so I think the best thing to do is issue a new Oxygen release with all the pieces in the right place.

I have also made some headway - boring my way into the ScriptBasic internals. and deconstructing the macros. The interface is heavily pointered but I don't need many calls to implement an O2 extension module, so I will extract only what is needed from the headers.

Charles