Author Topic: ScriptBasic Embedding  (Read 416 times)

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
ScriptBasic Embedding
« on: June 13, 2024, 05:08:35 PM »
For those looking for an embedded scripting engine solution, here are a few examples of ScriptBasic (libscriba.dll) embedded in OxygenBasic (32/64 bit) and FreeBasic (32 bit).

SB embedded in OxygenBasic 32 bit

SB embedded in OxygenBasic 64 bit

SB embedded in FreeBasic 32 bit





« Last Edit: June 13, 2024, 10:52:53 PM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: ScriptBasic Embedding
« Reply #1 on: June 15, 2024, 04:40:32 PM »
The SBT extension allows you to develop your SB embedded scripts using ScriptBasic as the host language. An added feature of SBT is you can run your scripts as threads. The MT extension module allows you to share variables between theads and the parent process.

SBT Module

Code: ScriptBasic
  1. MODULE SBT
  2.  
  3. DECLARE SUB ::SB_New ALIAS "SB_New" LIB "sbt"
  4. DECLARE SUB ::SB_Configure ALIAS "SB_Configure" LIB "sbt"
  5. DECLARE SUB ::SB_Load ALIAS "SB_Load" LIB "sbt"
  6. DECLARE SUB ::SB_LoadStr ALIAS "SB_LoadStr" LIB "sbt"
  7. DECLARE SUB ::SB_Run ALIAS "SB_Run" LIB "sbt"
  8. DECLARE SUB ::SB_NoRun ALIAS "SB_NoRun" LIB "sbt"
  9. DECLARE SUB ::SB_ThreadStart ALIAS "SB_ThreadStart" LIB "sbt"
  10. DECLARE SUB ::SB_ThreadEnd ALIAS "SB_ThreadEnd" LIB "sbt"
  11. DECLARE SUB ::SB_GetVar ALIAS "SB_GetVar" LIB "sbt"
  12. DECLARE SUB ::SB_SetUndef ALIAS "SB_SetUndef" LIB "sbt"
  13. DECLARE SUB ::SB_SetInt ALIAS "SB_SetInt" LIB "sbt"
  14. DECLARE SUB ::SB_SetDbl ALIAS "SB_SetDbl" LIB "sbt"
  15. DECLARE SUB ::SB_SetStr ALIAS "SB_SetStr" LIB "sbt"
  16. DECLARE SUB ::SB_ResetVars ALIAS "SB_ResetVars" LIB "sbt"
  17. DECLARE SUB ::SB_msSleep ALIAS "SB_msSleep" LIB "sbt"
  18. DECLARE SUB ::SB_CallSub ALIAS "SB_CallSub" LIB "sbt"
  19. DECLARE SUB ::SB_CallSubArgs ALIAS "SB_CallSubArgs" LIB "sbt"
  20. DECLARE SUB ::SB_Destroy ALIAS "SB_Destroy" LIB "sbt"
  21.  
  22. END MODULE
  23.  

An example of using SBT.

Code: ScriptBasic
  1. ' SBT Demo
  2.  
  3. IMPORT sbt.sbi
  4.  
  5. sb_code = """
  6. FUNCTION prtvars(a, b, c)
  7.  PRINT a,"\\n"
  8.  PRINT FORMAT("%g\\n", b)
  9.  PRINT c,"\\n"
  10.  prtvars = "Function Return"
  11. END FUNCTION
  12.  
  13. a = 0
  14. b = 0
  15. c = ""
  16. """
  17.  
  18. sb = SB_New()
  19. SB_Configure sb, "C:/Windows/SCRIBA.INI"
  20. SB_Loadstr sb, sb_code
  21. SB_NoRun sb
  22. ' Call function before running script
  23. funcrtn = SB_CallSubArgs(sb,"main::prtvars", 123, 1.23, "One, Two, Three")
  24. PRINT funcrtn,"\n"
  25. ' Run script initializing globals
  26. SB_Run sb, ""
  27. ' Assign variables values
  28. SB_SetInt sb, "main::a", 321
  29. SB_SetDbl sb, "main::b", 32.1
  30. SB_SetStr sb, "main::c", "Three,Two,One" & CHR(0)
  31. ' Call function again with variables assigned in the previous step
  32. SB_CallSubArgs sb, "main::prtvars", _
  33.           SB_GetVar(sb, "main::a"), _
  34.           SB_GetVar(sb, "main::b"), _
  35.           SB_GetVar(sb, "main::c")
  36. SB_Destroy sb
  37.  

Output

123
1.23
One, Two, Three
Function Return
321
32.1
Three,Two,One


This is an example of running a script as a thread and using MT as status flag variable.

sbt_main.sb
Code: ScriptBasic
  1. ' SBT Main
  2.  
  3. IMPORT mt.bas
  4. IMPORT sbt.bas
  5.  
  6. SBT::SB_ThreadStart("sbt_thread.sb", "","C:/Windows/SCRIBA.INI")
  7.  
  8. FOR x = 1 TO 10
  9.   PRINT "M:",x,"\n"
  10.   SBT::sb_msSleep(100)
  11. NEXT
  12.  
  13. SBT::SB_msSleep(1000)
  14.  
  15. PRINT "Thread ",mt::GetVariable("thread_status"),"\n"
  16.  

sbt_thread.sb
Code: ScriptBasic
  1. ' SBT Thread
  2.  
  3. IMPORT mt.bas
  4. IMPORT sbt.bas
  5.  
  6. FOR x = 1 TO 10
  7.   PRINT "T:",x,"\n"
  8.   SBT::SB_msSleep(100)
  9. NEXT
  10.  
  11. mt::SetVariable "thread_status","Completed"
  12.  
  13. SBT::SB_ThreadEnd
  14.  

Output


C:\ScriptBasic64\examples>scriba sbt_main.sb
T:1
T:2
T:3
T:4
T:5
M:1
T:6
M:2
T:7
M:3
T:8
M:4
T:9
M:5
T:10
M:6
M:7
M:8
M:9
M:10
Thread Completed

C:\ScriptBasic64\examples>

« Last Edit: June 15, 2024, 07:04:08 PM by John »