Created
January 28, 2013 22:37
-
-
Save enkore/4659960 to your computer and use it in GitHub Desktop.
Einfaches Beispiel für die Lua-API aus FreeBasic heraus (letzte Änderung: 18.8.2008 13:50)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include once "Lua/lauxlib.bi" | |
Declare Function luaopen_fbm Cdecl Alias "luaopen_fbm" (Byval L As lua_State Ptr) As Integer | |
Declare Function fadd Cdecl (Byval L As lua_State Ptr) As Integer | |
Declare Function fadd_each Cdecl (Byval L As lua_State Ptr) As Integer | |
Declare Function fSub Cdecl (Byval L As lua_State Ptr) As Integer | |
Declare Function fDiv Cdecl (Byval L As lua_State Ptr) As Integer | |
Declare Function fMul Cdecl (Byval L As lua_State Ptr) As Integer | |
Declare Function iadd Cdecl (Byval L As lua_State Ptr) As Integer | |
Declare Function iadd_each Cdecl (Byval L As lua_State Ptr) As Integer | |
Declare Function iSub Cdecl (Byval L As lua_State Ptr) As Integer | |
Declare Function iDiv Cdecl (Byval L As lua_State Ptr) As Integer | |
Declare Function iMul Cdecl (Byval L As lua_State Ptr) As Integer | |
Declare Function fSin Cdecl (Byval L As lua_State Ptr) As Integer | |
Declare Function fAsin Cdecl (Byval L As lua_State Ptr) As Integer | |
Declare Function fCos Cdecl (Byval L As lua_State Ptr) As Integer | |
Declare Function fAcos Cdecl (Byval L As lua_State Ptr) As Integer | |
Declare Function fTan Cdecl (Byval L As lua_State Ptr) As Integer | |
Declare Function fAtn Cdecl (Byval L As lua_State Ptr) As Integer | |
Declare Function fAtan2 Cdecl (Byval L As lua_State Ptr) As Integer | |
Declare Function fPow Cdecl (ByVal L As lua_State Ptr) As Integer | |
Function fadd Cdecl (Byval L As lua_State Ptr) As Integer Export | |
Dim As Integer numargs = Lua_gettop(L) | |
' gets the number of arguments | |
Dim As Double result = 0 | |
If numargs < 2 Then Return lual_error(L, "add needs at least 2 number arguments") | |
' this returns an error if less than 2 arguments were passed to the function | |
For i As Integer = 1 To numargs | |
result += lual_checknumber(L,i) | |
' checks each argument, if it is a number it will be added to result | |
Next | |
lua_pushnumber(L, result) ' pushes the result onto the stack | |
Return 1 ' this tells lua that there is one return value on the stack | |
End Function | |
Function fadd_each Cdecl (Byval L As lua_State Ptr) As Integer Export | |
Dim As Integer numargs = Lua_gettop(L) | |
' gets the number of arguments | |
If numargs < 2 Then Return lual_error(L, "add_eachf needs at least 2 number arguments") | |
Dim As Double results(1 To (numargs-1)) | |
Dim As Double amount = lual_checknumber(L,1) | |
For i As Integer = 2 To numargs | |
results(i-1) = amount + lual_checknumber(L,i) | |
Next | |
For i As Integer = 1 To (numargs-1) | |
lua_pushnumber(L, results(i)) ' pushes the result onto the stack | |
Next | |
Return (numargs-1) ' this tells lua that there is (numargs-1) return values on the stack | |
End Function | |
Function fSub Cdecl (Byval L As lua_State Ptr) As Integer Export | |
Dim As Integer numargs = Lua_gettop(L) | |
' gets the number of arguments | |
Dim As Double result = 0 | |
If numargs <> 2 Then Return lual_error(L, "sub needs 2 number arguments") | |
' this returns an error if less than 2 arguments were passed to the function | |
result = lual_checknumber(L,1) - lual_checknumber(L,2) | |
lua_pushnumber(L, result) ' pushes the result onto the stack | |
Return 1 ' this tells lua that there is one return value on the stack | |
End Function | |
Function fDiv Cdecl (Byval L As lua_State Ptr) As Integer Export | |
Dim As Integer numargs = Lua_gettop(L) | |
' gets the number of arguments | |
Dim As Double result = 0 | |
If numargs <> 2 Then Return lual_error(L, "div needs 2 number arguments") | |
' this returns an error if less than 2 arguments were passed to the function | |
If lual_checknumber(L, 2) = 0 Then Return lual_error(L, "div: division through zero") | |
result = lual_checknumber(L,1) / lual_checknumber(L,2) | |
lua_pushnumber(L, result) ' pushes the result onto the stack | |
Return 1 ' this tells lua that there is one return value on the stack | |
End Function | |
Function fMul Cdecl (Byval L As lua_State Ptr) As Integer Export | |
Dim As Integer numargs = Lua_gettop(L) | |
' gets the number of arguments | |
Dim As Double result = 0 | |
If numargs <> 2 Then Return lual_error(L, "mul needs at least 2 number arguments") | |
' this returns an error if less than 2 arguments were passed to the function | |
result = lual_checknumber(L,1) | |
For i As Integer = 2 To numargs | |
result *= lual_checknumber(L,i) | |
Next | |
lua_pushnumber(L, result) ' pushes the result onto the stack | |
Return 1 ' this tells lua that there is one return value on the stack | |
End Function | |
Function iadd Cdecl (Byval L As lua_State Ptr) As Integer Export | |
Dim As Integer numargs = Lua_gettop(L) | |
' gets the number of arguments | |
Dim As Integer result = 0 | |
If numargs < 2 Then Return lual_error(L, "add needs at least 2 number arguments") | |
' this returns an error if less than 2 arguments were passed to the function | |
For i As Integer = 1 To numargs | |
result += lual_checknumber(L,i) | |
' checks each argument, if it is a number it will be added to result | |
Next | |
lua_pushnumber(L, result) ' pushes the result onto the stack | |
Return 1 ' this tells lua that there is one return value on the stack | |
End Function | |
Function iadd_each Cdecl (Byval L As lua_State Ptr) As Integer Export | |
Dim As Integer numargs = Lua_gettop(L) | |
' gets the number of arguments | |
If numargs < 2 Then Return lual_error(L, "add_eachneeds at least 2 number arguments") | |
Dim As Integer results(1 To (numargs-1)) | |
Dim As Integer amount = lual_checknumber(L,1) | |
For i As Integer = 2 To numargs | |
results(i-1) = amount + lual_checknumber(L,i) | |
Next | |
For i As Integer = 1 To (numargs-1) | |
lua_pushnumber(L, results(i)) ' pushes the result onto the stack | |
Next | |
Return (numargs-1) ' this tells lua that there is (numargs-1) return values on the stack | |
End Function | |
Function iSub Cdecl (Byval L As lua_State Ptr) As Integer Export | |
Dim As Integer numargs = Lua_gettop(L) | |
' gets the number of arguments | |
Dim As Integer result = 0 | |
If numargs <> 2 Then Return lual_error(L, "sub needs 2 number arguments") | |
' this returns an error if less than 2 arguments were passed to the function | |
result = lual_checknumber(L,1) - lual_checknumber(L,2) | |
lua_pushnumber(L, result) ' pushes the result onto the stack | |
Return 1 ' this tells lua that there is one return value on the stack | |
End Function | |
Function iDiv Cdecl (Byval L As lua_State Ptr) As Integer Export | |
Dim As Integer numargs = Lua_gettop(L) | |
' gets the number of arguments | |
Dim As Integer result = 0 | |
If numargs <> 2 Then Return lual_error(L, "div needs 2 number arguments") | |
' this returns an error if less than 2 arguments were passed to the function | |
If lual_checknumber(L, 2) = 0 Then Return lual_error(L, "div: division through zero") | |
result = lual_checknumber(L,1) / lual_checknumber(L,2) | |
lua_pushnumber(L, result) ' pushes the result onto the stack | |
Return 1 ' this tells lua that there is one return value on the stack | |
End Function | |
Function iMul Cdecl (Byval L As lua_State Ptr) As Integer Export | |
Dim As Integer numargs = Lua_gettop(L) | |
' gets the number of arguments | |
Dim As Integer result = 0 | |
If numargs <> 2 Then Return lual_error(L, "mul needs at least 2 number arguments") | |
' this returns an error if less than 2 arguments were passed to the function | |
result = lual_checknumber(L,1) | |
For i As Integer = 2 To numargs | |
result *= lual_checknumber(L,i) | |
Next | |
lua_pushnumber(L, result) ' pushes the result onto the stack | |
Return 1 ' this tells lua that there is one return value on the stack | |
End Function | |
Function fSin Cdecl (Byval L As lua_State Ptr) As Integer Export | |
If Lua_gettop(L) <> 1 Then Return lual_error(L, "fsin needs 1 number argument") | |
' this returns an error if less than 2 arguments were passed to the function | |
Dim As Double a | |
a = lual_checknumber(L,1) | |
Asm | |
fld qword Ptr [a] 'st(0) = a | |
fsin ' sin(st(0)) | |
fstp st(1) | |
fstp qword Ptr [a] 'so brauchen wir b nicht^^ | |
End Asm | |
lua_pushnumber(L, a) ' pushes the result onto the stack | |
Return 1 ' this tells lua that there is one return value on the stack | |
End Function | |
Function fAsin Cdecl (Byval L As lua_State Ptr) As Integer Export | |
If Lua_gettop(L) <> 1 Then Return lual_error(L, "fasin needs 1 number argument") | |
' this returns an error if less than 2 arguments were passed to the function | |
Dim As Double a | |
a = lual_checknumber(L,1) | |
/'Asm | |
fld qword Ptr [a] 'st(0) = a | |
fsin ' sin(st(0)) | |
fstp st(1) | |
fstp qword Ptr [a] 'so brauchen wir b nicht^^ | |
End Asm'/ | |
a = ASin(a) | |
lua_pushnumber(L, a) ' pushes the result onto the stack | |
Return 1 ' this tells lua that there is one return value on the stack | |
End Function | |
Function fCos Cdecl (Byval L As lua_State Ptr) As Integer Export | |
If Lua_gettop(L) <> 1 Then Return lual_error(L, "fcos needs 1 number argument") | |
' this returns an error if less than 2 arguments were passed to the function | |
Dim As Double a | |
a = lual_checknumber(L,1) | |
Asm | |
fld qword Ptr [a] 'st(0) = a | |
fcos | |
fstp st(1) | |
fstp qword Ptr [a] 'so brauchen wir b nicht^^ | |
End Asm | |
lua_pushnumber(L, a) ' pushes the result onto the stack | |
Return 1 ' this tells lua that there is one return value on the stack | |
End Function | |
Function fAcos Cdecl (Byval L As lua_State Ptr) As Integer Export | |
If Lua_gettop(L) <> 1 Then Return lual_error(L, "facos needs 1 number argument") | |
' this returns an error if less than 2 arguments were passed to the function | |
Dim As Double a | |
a = lual_checknumber(L,1) | |
/'Asm | |
fld qword Ptr [a] 'st(0) = a | |
fstp st(1) | |
fstp qword Ptr [a] 'so brauchen wir b nicht^^ | |
End Asm'/ | |
a = ACos(a) | |
lua_pushnumber(L, a) ' pushes the result onto the stack | |
Return 1 ' this tells lua that there is one return value on the stack | |
End Function | |
Function fTan Cdecl (Byval L As lua_State Ptr) As Integer Export | |
If Lua_gettop(L) <> 1 Then Return lual_error(L, "ftan needs 1 number argument") | |
' this returns an error if less than 2 arguments were passed to the function | |
Dim As Double a | |
a = lual_checknumber(L,1) | |
/'Asm | |
fld qword Ptr [a] 'st(0) = a | |
fstp st(1) | |
fstp qword Ptr [a] 'so brauchen wir b nicht^^ | |
End Asm'/ | |
a = Tan(a) | |
lua_pushnumber(L, a) ' pushes the result onto the stack | |
Return 1 ' this tells lua that there is one return value on the stack | |
End Function | |
Function fAtn Cdecl (Byval L As lua_State Ptr) As Integer Export | |
If Lua_gettop(L) <> 1 Then Return lual_error(L, "fatn needs 1 number argument") | |
' this returns an error if less than 2 arguments were passed to the function | |
Dim As Double a | |
a = lual_checknumber(L,1) | |
/'Asm | |
fld qword Ptr [a] 'st(0) = a | |
fstp st(1) | |
fstp qword Ptr [a] 'so brauchen wir b nicht^^ | |
End Asm'/ | |
a = Atn(a) | |
lua_pushnumber(L, a) ' pushes the result onto the stack | |
Return 1 ' this tells lua that there is one return value on the stack | |
End Function | |
Function fAtan2 Cdecl (Byval L As lua_State Ptr) As Integer Export | |
If Lua_gettop(L) <> 2 Then Return lual_error(L, "fatan2 needs 2 number arguments") | |
' this returns an error if less than 2 arguments were passed to the function | |
Dim As Double a, b | |
a = lual_checknumber(L,1) | |
b = lual_checknumber(L,2) | |
/'Asm | |
fld qword Ptr [a] 'st(0) = a | |
fstp st(1) | |
fstp qword Ptr [a] 'so brauchen wir b nicht^^ | |
End Asm'/ | |
a = ATan2(a, b) | |
lua_pushnumber(L, a) ' pushes the result onto the stack | |
Return 1 ' this tells lua that there is one return value on the stack | |
End Function | |
Function fPow Cdecl (Byval L As lua_State Ptr) As Integer Export | |
If Lua_gettop(L) <> 2 Then Return lual_error(L, "pow needs 2 number arguments") | |
' this returns an error if less than 2 arguments were passed to the function | |
Dim As Double a, b | |
a = lual_checknumber(L,1) | |
b = lual_checknumber(L,2) | |
' by volta ; http://volta.de.tt/ | |
Asm | |
fld qword Ptr [b] 'st(0) = b | |
fld qword Ptr [a] 'st(0) = a ;st(1) = b | |
fyl2x 'st(0) = ln(a)*b | |
fst st(1) 'in st(1) kopieren | |
frndint 'st(0) runden = Integer-Anteil | |
fsub st(1),st(0) 'st(1) Vorkommaanteil auf 0 setzen | |
fld1 '1 laden | |
fscale 'temp:= 2 hoch Integer-Anteil | |
fld st(2) 'Nachkommaanteil laden | |
f2xm1 '2 hoch Nachkommaanteil -1 | |
fld1 '1 laden | |
faddp st(1),st(0) '+1 | |
fmulp st(1),st(0) '*temp | |
fstp st(1) | |
fstp st(1) | |
fstp qword Ptr [a] 'so brauchen wir c nicht^^ | |
End Asm | |
lua_pushnumber(L, a) ' pushes the result onto the stack | |
Return 1 ' this tells lua that there is one return value on the stack | |
End Function | |
' this array will be used to register the functions with lua | |
' with each pair, the first entry is the name that lua will use | |
' for the function, and the second entry is a pointer to the function | |
' the last element must be (0,0) | |
Dim Shared As luaL_reg fb_functions(0 To 19) => { _ | |
(StrPtr("addf"), @fadd) , _ | |
(StrPtr("add_eachf"), @fadd_each) , _ | |
(StrPtr("subf"), @fSub) , _ | |
(StrPtr("divf"), @fDiv) , _ | |
(StrPtr("mulf"), @fMul) , _ | |
_ | |
(StrPtr("add"), @iadd) , _ | |
(StrPtr("add_each"), @iadd_each) , _ | |
(StrPtr("sub"), @iSub) , _ | |
(StrPtr("div"), @iDiv) , _ | |
(StrPtr("mul"), @iMul) , _ | |
_ | |
(StrPtr("fsin"), @fSin) , _ | |
(StrPtr("fasin"), @fAsin) , _ | |
(StrPtr("fcos"), @fCos) , _ | |
(StrPtr("facos"), @fAcos) , _ | |
(StrPtr("ftan"), @fTan) , _ | |
(StrPtr("fatn"), @fAtn) , _ | |
(StrPtr("fatan2"), @fAtan2) , _ | |
(StrPtr("fpow"), @fPow) , _ | |
(0, 0) _ | |
} | |
' when using require("fbm") in a lua script | |
' lua will call this function from the dll | |
' note: the part after luaopen_ must match the filename of the dll | |
' so in this particular instance, this code must be compiled to "fbm.dll" | |
' (or "fbm.so" if on linux) | |
Function luaopen_fbm Cdecl Alias "luaopen_fbm" (Byval L As lua_State Ptr) As Integer Export | |
luaL_openlib(L, @"fbm", @fb_functions(0),0) ' this registers the functions with lua, putting them into the "fbm" table | |
Return 0 | |
End Function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment