Author Topic: Embedding into application  (Read 25561 times)

Bill

  • Guest
Embedding into application
« on: May 13, 2006, 10:45:59 PM »
I have managed to compile ScriptBasic using MinGW on windows XP.
I ahve written a simple application and embedded the scriba.a library into the application as per instructions. However upon starting from the MinGW console The following message appears and the application refuses to start:

Code: [Select]
<HTML><HEAD>
<title>Error page, syntax error</title>
</HEAD><BODY>
<H1>Error has happened in the code</H1><pre>
(0): error &H50:Internal error or the cached code is corrupt


Can anyone help me as to what the problem is?

Support

  • Administrator
  • *****
  • Posts: 22
    • View Profile
Embedding into application
« Reply #1 on: May 14, 2006, 10:03:48 AM »
Bill,

I haven't gotten into the embedding ScriptBasic but I'll try to help. My first question is where is the HTML coming from? Is this a CGI application? Can you provide more details to what your trying to do?

John

Bill

  • Guest
Embedding into application
« Reply #2 on: May 15, 2006, 12:12:37 PM »
John,

The message comes from the scriba library - report.c to be exact. I only see this message because I started the application from a dos box. The app itself does not start. I assume that something within scriba could not be initialized.

Zulfi.Ali

  • Guest
Re: Embedding into application
« Reply #3 on: July 15, 2010, 04:57:37 AM »
Hi,

I have finally managed to embed ScriptBasic into my VC8 application... Now, how can I redirect the output of the PRINT statements inside my script to the output window of my IDE or to another window so that I can debug my basic script?

I am guessing it has something to do with scriba_SetStdout() function but I am not sure of the windows callback function that I need to pass.

Help needed!!


Support

  • Administrator
  • *****
  • Posts: 22
    • View Profile
Re: Embedding into application
« Reply #4 on: July 15, 2010, 08:53:09 AM »
Quote from: ScriptBasic Developer Docs
You can call this function to define a special standard output function. This pointer should point to a function that accepts a (char, void *) arguments. Whenever the ScriptBasic program tries to send a character to the standard output it calls this function. The first parameter is the character to write, the second is the embedder pointer.

If the standard output function is not defined or the parameter is NULL the interpreter will write the normal stdout stream.

void scriba_SetStdout(pSbProgram pProgram,
                      void *fpStdoutFunction
  ){
Embedding the Interpreter

Zulfi.Ali

  • Guest
Re: Embedding into application
« Reply #5 on: July 15, 2010, 09:16:33 PM »
Thanks for your help... i managed to create a call back function and display the output in an edit control box...but i had to set the embedder pointer as well since i used a global callback function...

My next task is to exchange data between the C environment and the script... i need to pass variables into the script and then get results back from the script... i could not find any example of how this is to be achieved so once again i need ur assistance...

how can i handle the data exchange if my variable count is high? I need to exchange only doubles as the script is meant for user defined calculations...


Support

  • Administrator
  • *****
  • Posts: 22
    • View Profile
Re: Embedding into application
« Reply #6 on: July 15, 2010, 09:36:41 PM »
Another BCX embedded example to help you take your next step. Notice this example shows the results if you just load your script and access functions and variables and what the results are if you run the script.

BCX_embed4.bas
Code: [Select]
#include "\scriptbasic\source\scriba.h"
#include "\scriptbasic\source\getopt.h"

$LIBRARY "libscriba.lib"

' NOTE: Windows specific above this line and generic BCX below.

Dim pProgram As pSbProgram
Dim v As long
Dim f1 As long
Dim f2 As long
Dim dVal=11 as long
Dim cArgs As long
Dim ReturnData As SbData
dim ArgData[4] As SbData

' LOADING AND RUNNING THE PROGRAM
pProgram = scriba_new(malloc, free)
scriba_SetFileName(pProgram, "E03.bas")
scriba_LoadSourceProgram(pProgram)
scriba_NoRun(pProgram)

'' ACCESSING GLOBAL DATA
v = scriba_LookupVariableByName(pProgram, "main::a")

scriba_SetVariable(pProgram, v, 2, 500, 0, "", 0)

'' CALLING SIMPLE SUBROUTINE
f1 = scriba_LookupFunctionByName(pProgram, "main::dprint")
scriba_Call(pProgram, f1)

' CALLING FUNCTION, RETURNING DATA AND GETTING ALTERED PARAMS
f2 = scriba_LookupFunctionByName(pProgram, "main::eprint")

' SETUP ARGUMENTS

for cArgs=0 to 3
  ArgData[cArgs].type = SBT_DOUBLE
  ArgData[cArgs].size = 0
  ArgData[cArgs].v.d = dVal+cArgs
next

scriba_CallArgEx(pProgram, f2, &ReturnData, cArgs, ArgData)
print "Return type:",ReturnData.type
print "Value:";

' READ RETURNED VALUE
select case ReturnData.type
 case SBT_UNDEF  :                print "Undefined"
 case SBT_DOUBLE :                print ReturnData.v.d
 case SBT_LONG   :                print ReturnData.v.l
 case =SBT_STRING or =SBT_ZCHAR : print (CHAR PTR)ReturnData.v.s
end select

scriba_destroy(pProgram)

ScriptBasic - E03.bas
Code: [Select]
' ----------------
' GLOBAL VARIABLES
' ================
'
a=42
b=sqr(2)
s="hello world!"

q=dprint()
q=eprint(1,2,3,4)

'print "Enter: "
'line input q

' ---------
' FUNCTIONS
' =========

function dprint
  sp="  "
  cr="\n"
  tab=chr$(9)
  print "Dprint Globals: " & s & sp & a & sp & b & cr
  ' line input q
  dprint=1
end function

function eprint(a,b,c,d)
  sp="  "
  cr="\n"
  tab=chr$(9)
  print "Eprint Args: " & a & sp & b & sp & c & sp & d & cr
  ' line input q
  ans=a+b+c+d
  eprint="Sum = " & ans
end function

Results:(scriba_NoRun)
Code: [Select]
C:\Program Files\BCX\sb>BCX_embed4
Dprint Globals:   500
Eprint Args: 11.000000  12.000000  13.000000  14.000000
Return type: 3
Value:Sum = 50

C:\Program Files\BCX\sb>

Results: (Scriba_Run)
Code: [Select]
C:\Program Files\BCX\sb>BCX_embed4
Dprint Globals: hello world!  42  1.414214
Eprint Args: 1  2  3  4
Dprint Globals: hello world!  500  1.414214
Eprint Args: 11.000000  12.000000  13.000000  14.000000
Return type: 3
Value:Sum = 50

C:\Program Files\BCX\sb>
« Last Edit: July 15, 2010, 09:40:40 PM by support »

Zulfi.Ali

  • Guest
Re: Embedding into application
« Reply #7 on: July 26, 2010, 02:20:19 AM »
Hi,

Using your examples, I have managed to pass values to my script variables but I have a problem; I need to pass arrays into the script.  How can I achieve that?  I tried to access variables using "main::x[0]" but this didnt work.

Suggestions needed...

Thanks

Zulfi.Ali

  • Guest
Re: Embedding into application
« Reply #8 on: July 27, 2010, 06:55:25 PM »
Hi,

I am still struggling with the task of setting array elements from my C program... Is it possible? If not then I would have to abandon my work and look at other alternatives for a scripting language.

Help needed urgently

Thanks

Support

  • Administrator
  • *****
  • Posts: 22
    • View Profile
Re: Embedding into application
« Reply #9 on: July 27, 2010, 07:27:06 PM »
Have you looked at the developer docs for embedding ScriptBasic?

The ScriptBasic WIki is a great place for information.

http://www.scriptbasic.org/wiki


Zulfi.Ali

  • Guest
Re: Embedding into application
« Reply #10 on: July 27, 2010, 07:39:41 PM »
I have looked at the developer docs esp for embedding ScriptBasic.... but no reference to setting array elements...

If you have any suggestion then do let me know as I will try it out myself...

Support

  • Administrator
  • *****
  • Posts: 22
    • View Profile
Re: Embedding into application
« Reply #11 on: July 27, 2010, 08:03:10 PM »
Maybe Armando or Peter can chime in and shed some light on accessing arrays.


Verhas

  • Guest
Re: Embedding into application
« Reply #12 on: August 01, 2010, 07:28:17 AM »
To embed ScriptBasic into an application the interface developed in the source file scriba.c should be used. This is fine so far, as I can see that you use the functions defined in that file. You only struggle with the fact that this interface does not give access to you to global variables that contain arrays. This is by design and I will try to e4xplain it, and I will try to convince you to try to find some other way to solve your problem, rather than wanting directly manipulate array variables.

A ScriptBasic program has global variables and functions and functions also have local variables. The global variables are stored in a table and arrays can build up quite complex structures. These structures
may even lead to memory leak in the sense that some unused memory is not released or reused by ScriptBasic until it finishes the execution. Because the memory structure of ScriptBasis is complex I decided
not to provide interface to access variables only to functions. Later on I bended due to popular pressure from users of ScriptBasic and now it is possible to set/get global variables before invoking functions. But still you can not set a variable to hold an array, and calling

Code: [Select]
v = scriba_LookupVariableByName(pProgram, "main::a[0]")
is a total non-sense. This would try to look up a variable that has the name
Code: [Select]
main::a[0] which is indeed not a valid variable name.

My suggestion is that you set one global variable to hold a string. String in ScriptBasic is an arbitrary length binary value, therefore it can hold any value. After that your BASIC code should split and convert this value to an array. Something like:

Code: [Select]
v = scriba_LookupVariableByName(pProgram, "main::a")

scriba_SetVariable(pProgram, v, SBT_STRING, 0, 0, binaryStringData , lengthOfString)

note that the variable binaryStringData need not be zero terminated, and may contain zero characters. After this the BASIC program could

Code: [Select]

i = 0
while length(a) > 0
 unpack a format "I4" to q[i]
 i=i+1
 a = mid$(a,4)
wend

I did not check this code, may be erroneous, but the general idea should be fine.

Support

  • Administrator
  • *****
  • Posts: 22
    • View Profile
Re: Embedding into application
« Reply #13 on: August 01, 2010, 12:14:18 PM »
I ran into the same array limitation using the MT module. (can't store arrays as session variables) I solved my problem using the T extension module functions ArrayToString() and StringToArray().

 

Zulfi.Ali

  • Guest
Re: Embedding into application
« Reply #14 on: August 06, 2010, 12:06:56 AM »
Hi,

Thanks for your reply... now I am trying to pass the array as a string... my problem is how do I create the string from the array in my C program to pass to the variable?  The T module is designed to be used from inside the BASIC script so I cannot use the ArrayToString() function...

More help needed..