View Full Version : Oxygen can call thinBasic functions
Charles Pegge
14-06-2009, 07:43
thinBasic calls Oxygen function -<-----> Oxygen calls thinBasic function
With the latest Oxygen, it is now possible to read the PB version of thincore.inc ( included in the example zip below).
Using two of the more recent thincore functions enables Oxygen to obtain a function token from thinBasic then use it to invoke the thinBasic function.
Latest Oxygen: http://community.thinbasic.com/index.php?topic=2517
uses "oxygen"
'-------------------------------
'thinBasic calls Oxygen function
'Oxygen calls thinBasic function
'===============================
dim v as long
dim p1,p2,p3,p4 as long
dim src as string
src = "
'
#o2h
'
'
include `thincore.inc`
'
'---------------------------------------
function callback_token() as long at #p2
'=======================================
function=thinBasic_FunctionParseAndGetPtr 0
end function
'--------------------------------------------------
function callback_to(byval a as long) as long at #p3
'==================================================
dim as double d
dim as string s
dim as double p(10)
p(1)=42
thinBasic_FunctionCall_ByPtr a, 1,&p, d,s
print `thinBasic returns: ` str d
function=1
end function
'------------------
sub finish() at #p1
'==================
terminate
end sub
'
"
'===============================================================
'msgbox 0, o2_prep src
o2_basic src
if len(o2_error) then
msgbox 0, o2_error : stop
end if
o2_exec
declare sub finish() at p1
declare function callback_token() as long at p2
declare function callback_to(byval a as long) as long at p3
'===============================================================
'----------------------------------------------
function targetfun(byval a as double) as double
'==============================================
msgbox 0, "ThinBasic receives: "+str$(a)
function=a*2
end function
'----
'TEST
'====
v=callback_token targetfun
'v=callback_token "targetfun"
callback_to v
finish
ErosOlmi
14-06-2009, 08:36
You are working like a Caterpillar Charles.
Thank you very much.
_________________________________________________
Latest Oxygen will be natively present in next thinBasic beta 1.7.8.0
Charles Pegge
14-06-2009, 10:11
Thanks Eros,
A fundamental update to Oxygen will be dispatched in the next 2 hours or so. This will enable o2 to be accessed as a standard dll - an important step towards being able to create TB modules in O2 basic.
Charles
\/
(O) :)
(O)(O)(O)(O)(O)
^ ^ ^ ^ ^
Petr Schreiber
14-06-2009, 10:22
Thanks for your work Charles :)
Michael Hartlef
14-06-2009, 10:31
Thanks Eros,
A fundamental update to Oxygen will be dispatched in the next 2 hours or so. This will enable o2 to be accessed as a standard dll - an important step towards being able to create TB modules in O2 basic.
Charles
\/
(O) :)
(O)(O)(O)(O)(O)
^ ^ ^ ^ ^
I was actually thinking that it was possible allready but that would be an awesome addition and I would see if I can use it to develop a module with it.
Charles Pegge
14-06-2009, 11:25
Hi Michael,
yes I had a primitive version - assembler only with a very limited set of run time functions. This will tap into the Oxygen module for its runtime but there are a few more steps to take on the DLL generation side.
Charles
ReneMiner
18-07-2013, 20:25
OK, what I don't understand- I post the changed script as I have it on my pc. - It's a "modern" version of the first - so uses Link instead of At.
You'll need to have thinCore.inc saved next to the script to run it.
uses "oxygen"
'-------------------------------
'thinBasic calls Oxygen function
'Oxygen calls thinBasic function
'===============================
'REQUIRES THINCORE.INC from thinBasic SDK.
Dim p1,p2,p3 As Long
String src = RawText
'
#basic
'
'
include "thincore.inc"
'
'------------------------------------------
function callback_token() as long link #p2
'==========================================
function=thinBasic_FunctionParseAndGetPtr 0
end function
'------------------------------------------------------
function callback_to(byval a as long) as long link #p3
'======================================================
dim as double d
dim as string s
dim as double p(10)
p(1)=42
s="!!"
###
thinBasic_FunctionCall_ByPtr a, 1, &p, d, s
###
print `thinBasic returns to Oxygen: ` s
'print `thinBasic returns to Oxygen: ` str d
function=1
end function
'------------------
Sub finish() link #p1
'==================
terminate
end sub
'
End RawText
'===============================================================
o2_basic src
if len(o2_error) then
msgbox 0, o2_error : stop
end if
o2_exec
declare sub finish() at p1
declare function callback_token() as long at p2
Declare Function callback_to(ByVal Long) As Long At p3
'===============================================================
'-----------------------------------------------------------------
function targetfun(byval a as double) as string
'=================================================================
msgbox 0, "ThinBasic receives from Oxygen: "+str$(a)
function=str$(a*2)
end function
'----
'TEST
'====
callback_to callback_token targetfun
finish
now my question - what I don't get is -
- see lines 61, 77 and also 20... how can "function callback_token" use "targetfun" as a parameter?
- line 34: does "&p" flush the whole array p(10) ?
and Edit: some smaller questions
So in the meantime I think to have found out that o2 can share previously in tB dimensioned variables with the current running tB-script by prefixing them with some # ?
Would that also work with UDT and arrays?
Just globals or also locals from within the function or sub that called O2_Exec ?
What means ### ? Some remark?
Would it be possible to execute for example TBGL-statements when I include it through O2 somehow?
Are arrays 0 or 1-based?
Charles Pegge
18-07-2013, 21:44
This is not the easiest example to understand. It had trouble understanding it myself. Who wrote this stuff?! :)
- see lines 61, 77 and also 20... how can "function callback_token" use "targetfun" as a parameter?
The thinBasic function's token is passed as an integer, for use by thincore to invoke it.
and Edit: some smaller questions
So in the meantime I think to have found out that o2 can share previously in tB dimensioned variables with the current running tB-script by prefixing them with some # ?
Would that also work with UDT and arrays?
Just globals or also locals from within the function or sub that called O2_Exec ?
What means ### ? Some remark?
Would it be possible to execute for example TBGL-statements when I include it through O2 somehow?
Are arrays 0 or 1-based?
Arrays:
are 1 based by default, but this can be changed with indexbase 0 'or any other base
Sharing global variables directly with ThinBasic:
dim as long a at #a ' Oxygen a is mapped to thinBasic a
String variables
dim as bstring s at #s 'use bstrings to map thinBasic strings
Arrays:
dim as single arr[100] at #arr 'Oxy arr is mapped to thinBasic arr, but no boundary checks.
UDTs:
These are also similarly supported
type vector
single x
single y
end type
'the user must ensure that the types match.
vector v at #vec ' v mapped to thinBasic vec.
###
...
###
used to define a block window for display of assembly code.
Use o2_prep(src) instead of o2_basic(src), and a string containing the assembly code will be returned instead of compilation.
ReneMiner
18-07-2013, 22:19
This is not the easiest example to understand. It had trouble understanding it myself. Who wrote this stuff?! :)
...
...and why do almost all O2-examples return the Answer to the Ultimate Question of Life, the Universe, and Everything (42) (http://en.wikipedia.org/wiki/42_Puzzle#Answer_to_the_Ultimate_Question_of_Life.2C_the_Universe.2C_and_Everything_.2842.29)?
perhaps there are some white mice working while you're deep within your dreams
Charles Pegge
18-07-2013, 22:40
Habit I think.
360 would be better. A more meaningful number in many ways.