Welcome, Guest.
Please login or register.
Using callbacks with CCall convention
Forum Login
Login Name: Create a new account
Password:     Forgot password

XBLite Forum    General Boards    XBLite Programming  ›  Using callbacks with CCall convention
Users Browsing Forum
No Members and 1 Guests

Using callbacks with CCall convention  This thread currently has 2,213 views. Print Print Thread
1 Pages 1 Recommend Thread
26
August 24, 2008, 10:25am Report to Moderator Report to Moderator
Guest User
Some libraries need to use user callBacks.Native convention for XBlite is STDCall but some of theses library request CCall conventions in their callbacks (NEWTON,LUA,etc...)
Hopefully, Xblite owns inline ASM so nothing is lost, this is just a little trick that does the job.
First, write your function normaly and compile (F9 only, don't build).
Look at yourprogram.asm and check for your function end (use search because ASM code is very very very very (and so on) long).It looks like this :

Quoted Text
end.SV_NEWTON_RaycastNOTfilter.mye2:  ;;; Function end label for Assembly Programmers.
end.func117.mye2:
     lea     esp,[ebp-20]                    ;;; i110
     pop     ebx                    ;;; restore ebx
     pop     edi                    ;;; restore edi
     pop     esi                    ;;; restore esi
     leave                         ;;; replaces 'mov esp,ebp' 'pop ebp'
     ret     20               ;;; i111a


So just copy asm code and remove the number after 'ret' (in fact it removes arguments from stack but in CCall, caller removes the arguments so that unbalance the stack)

And when your funcs returns a value ?

This is simple, if it is an integer, return the value on EAX, if it is a float, return on ST0.You can use a local variable to write the value and load it into EAX or ST0 after.Example :

FUNCTION SINGLE DoSomething(a,b,c,d)

SINGLE float

float=1.25!

ASM fld d[DoSomething.float]
(copy here ASM code like i wrotte before)

If you use some standarts values, this is a shortcut

Quoted Text
FLD1     Push +1.0 onto the FPU register stack.
FLDL2T     Push log210 onto the FPU register stack.
FLDL2E     Push log2e onto the FPU register stack.
FLDPI     Push p onto the FPU register stack.
FLDLG2     Push log102 onto the FPU register stack.
FLDLN2     Push loge2 onto the FPU register stack.
FLDZ     Push +0.0 onto the FPU register stack.

Easy Assembler Shell v3.99 Copyright(C) 1999 Roman Novgorodov



For integers, this is the same methods execpt you must use EAX

A complete real example, a callback filter for raycasting in NEWTON :

Quoted Text

'
' ########################################
' #####  SV_NEWTON_RaycastNOTfilter  #####
' ########################################
'
'will stop hit on first body except body that initiate raycast
'
FUNCTION SINGLE SV_NEWTON_RaycastNOTfilter (body,CVector3 normal,collision,from_body,SINGLE param)
SHARED SERVER_INFO ServerI

IF body=from_body THEN
     
ASM          fld d[SV_NEWTON_RaycastNOTfilter.param]

ELSE     
     ServerI.rayCastResult=body
ASM          fldz

ENDIF

ASM     lea     esp,[ebp-20]     
ASM pop     ebx                    
ASM     pop     edi                    
ASM     pop     esi                    
ASM     leave                    
ASM     ret     
END FUNCTION


I hope this could be usefull to someone else.
Logged
E-mail E-mail
Rhett Thompson
August 28, 2008, 3:09pm Report to Moderator Report to Moderator

Baby Member
Posts: 23
I had to use a very similar method to use both Lua and Newton, and I'm glad you posted this.  It would be a very good demo to be added into next Xblite release.
Logged Offline
Site Site Private Message Private message AIM AIM Windows Live Messenger WLM Reply: 1 - 6
Rhett Thompson
August 29, 2008, 2:14am Report to Moderator Report to Moderator

Baby Member
Posts: 23
Heh.  You can more easily define the function in Xblite.

Code
DECLARE CFUNCTION Test(arg)
...
CFUNCTION Test(arg)


I've tested this with Newton, and it works as expected
Logged Offline
Site Site Private Message Private message AIM AIM Windows Live Messenger WLM Reply: 2 - 6
26
August 29, 2008, 4:38am Report to Moderator Report to Moderator
Guest User
I must update a bit some of my knowledges, i miss this one !
Anyway, this is great, no more asm tricks to have CCall callbacks , that rocks
Logged
E-mail E-mail Reply: 3 - 6
13
August 29, 2008, 7:01am Report to Moderator Report to Moderator
Guest User
There are a lot of tricks in xblite that take some time to find.  Not because of a lack of documentation - the documentation is great!  The demos are great!  It is just that there is so much capability in xblite that it takes a while to find it all.  It is a very "rich" language.

Greg
Logged
E-mail E-mail Reply: 4 - 6
Rhett Thompson
August 29, 2008, 1:03pm Report to Moderator Report to Moderator

Baby Member
Posts: 23
I know it's great, Xblite is by far my favorite language
Logged Offline
Site Site Private Message Private message AIM AIM Windows Live Messenger WLM Reply: 5 - 6
26
August 29, 2008, 9:36pm Report to Moderator Report to Moderator
Guest User
Yes , XBlite is great, it has evolved a lot from XBasic times.I remember when i was using XBasic and features that were missing.Now XBlite owns a lot of theses features (my favorites are m4 and inline ASM ) and is able to come up to my expectations..It is my favorite language too.
Logged
E-mail E-mail Reply: 6 - 6
1 Pages 1 Recommend Thread
Print Print Thread

XBLite Forum    General Boards    XBLite Programming  ›  Using callbacks with CCall convention

Thread Rating
There is currently no rating for this thread