1. Attempting to subscript an atom Error[SOLVED]
- Posted by Icy_Viking Oct 14, 2022
- 1008 views
- Last edited Oct 17, 2022
Hello all,
When trying to wrap the add-on functions for Allegro, I came across this error: Attempting to subscript an atom
I posted the code from where the error is coming from. Let me know if more code is needed. I am using the FFI.e I'm not sure why I would get that error as it just returning a boolean variable.
Wrapper code
include std/ffi.e include allegro.e export constant prim = open_dll("allegro_primitives-5.2"), --this needed to have .dll added to the end xal_get_allegro_primitives_version = define_c_func(prim,"al_get_allegro_primitives_version",{},C_UINT) --this was missing a "+" public function al_get_allegro_primitives_version() return c_func(xal_get_allegro_primitives_version,{}) end function export constant xal_init_primitives_addon = define_c_func(prim,"+al_init_primitives_addon",{},C_BOOL), xal_is_primitives_addon_initialized = define_c_func(prim,"+al_is_primitives_addon_initialized",{},C_BOOL), xal_shutdown_primitives_addon = define_c_proc(prim,"+al_shutdown_primitives_addon",{}), xal_draw_prim = define_c_func(prim,"+al_draw_prim",{C_POINTER,C_POINTER,C_POINTER,C_INT,C_INT,C_INT},C_INT), xal_draw_indexed_prim = define_c_func(prim,"+al_draw_indexed_prim",{C_POINTER,C_POINTER,C_POINTER,C_POINTER,C_INT,C_INT},C_INT), xal_draw_vertex_buffer = define_c_func(prim,"+al_draw_vertex_buffer",{C_POINTER,C_POINTER,C_INT,C_INT,C_INT},C_INT), xal_draw_indexed_buffer = define_c_func(prim,"+al_draw_indexed_buffer",{C_POINTER,C_POINTER,C_POINTER,C_INT,C_INT,C_INT},C_INT) public function al_init_primitives_addon() return c_func(xal_init_primitives_addon,{}) --error is coming from this line end function public function al_is_primitives_addon_initialized() return c_func(xal_is_primitives_addon_initialized,{}) end function public procedure al_shutdown_primitives_addon() c_proc(xal_shutdown_primitives_addon,{}) end procedure
Demo program
--Simple Display Window demo --Draw primitive shapes include allegro.e include keycodes.e include std/ffi.e include primitives.e procedure main() atom x = al_install_system(ALLEGRO_VERSION_INT,0) atom a = al_init_primitives_addon() if x = -1 then puts(1,"Failed to init Allegro!\n") abort(0) end if if a = -1 then puts(1,"Failed to init addon!\n") abort(0) end if atom dis = al_create_display(800,600) if dis = -1 then puts(1,"Failed to create display!\n") abort(0) end if al_set_window_title(dis,"Primitive Demo") sequence BLUE = al_map_rgb(0,0,255) al_draw_line(10,10,10,10,BLUE,10) al_flip_display() al_rest(5.0) al_destroy_display(dis) end procedure main()
2. Re: Attempting to subscript an atom Error
- Posted by ChrisB (moderator) Oct 15, 2022
- 990 views
Try
public function al_init_primitives_addon() object x x = c_func(xal_init_primitives_addon,{}) --error is coming from this line return x end function
and
export constant xal_init_primitives_addon = define_c_func(prim,"+al_init_primitives_addon",{},C_INT),
and
object a = al_init_primitives_addon()
Cheers
Chris
3. Re: Attempting to subscript an atom Error
- Posted by Icy_Viking Oct 15, 2022
- 941 views
Try
public function al_init_primitives_addon() object x x = c_func(xal_init_primitives_addon,{}) --error is coming from this line return x end function
and
export constant xal_init_primitives_addon = define_c_func(prim,"+al_init_primitives_addon",{},C_INT),
and
object a = al_init_primitives_addon()
Cheers
Chris
Thanks for trying to help Chris, but it didn't work. Same error came up.
4. Re: Attempting to subscript an atom Error
- Posted by ChrisB (moderator) Oct 16, 2022
- 887 views
hmm,
bool al_init_primitives_addon(void)
returns a bool, doesn't want anything sending to it.
Try
export constant xal_init_primitives_addon = define_c_func(prim,"+al_init_primitives_addon",{C_INT},C_BOOL), public function al_init_primitives_addon() return c_func(xal_init_primitives_addon, 0) --error is coming from this line end function
Chris
5. Re: Attempting to subscript an atom Error
- Posted by Icy_Viking Oct 16, 2022
- 878 views
hmm,
bool al_init_primitives_addon(void)
returns a bool, doesn't want anything sending to it.
Try
export constant xal_init_primitives_addon = define_c_func(prim,"+al_init_primitives_addon",{C_INT},C_BOOL), public function al_init_primitives_addon() return c_func(xal_init_primitives_addon, 0) --error is coming from this line end function
Chris
Thanks Chris, but it doesn't work either. Maybe Greg can help.
6. Re: Attempting to subscript an atom Error
- Posted by petelomax Oct 16, 2022
- 880 views
xal_get_allegro_primitives_version = define_c_func(prim,"al_get_allegro_primitives_version",{},C_UINT) xal_init_primitives_addon = define_c_func(prim,"+al_init_primitives_addon",{},C_BOOL),
Could it be that you don't want the + at all, or (less likely) the first is missing a +?
Also, have you checked that none of the xal_init_primitives_addon etc are getting set to -1?
And open_dll() isn't returning null - in my experience Windows is a bit fussy about not having ".dll" at the end of the name, but that might be a Phix thing.
7. Re: Attempting to subscript an atom Error
- Posted by irv Oct 16, 2022
- 862 views
type xfn(atom x) -- type is your friend return x > -1 end type export xfn xal_init_primitives_addon = define_c_func(prim,"+al_init_primitives_addon",{},C_BOOL), xal_is_primitives_addon_initialized = define_c_func(prim,"+al_is_primitives_addon_initialized",{},C_BOOL), xal_shutdown_primitives_addon = define_c_proc(prim,"+al_shutdown_primitives_addon",{}), xal_draw_prim = define_c_func(prim,"+al_draw_prim",{C_POINTER,C_POINTER,C_POINTER,C_INT,C_INT,C_INT},C_INT)
8. Re: Attempting to subscript an atom Error[SOLVED]
- Posted by Icy_Viking Oct 16, 2022
- 853 views
xal_get_allegro_primitives_version = define_c_func(prim,"al_get_allegro_primitives_version",{},C_UINT) xal_init_primitives_addon = define_c_func(prim,"+al_init_primitives_addon",{},C_BOOL),
Could it be that you don't want the + at all, or (less likely) the first is missing a +?
Also, have you checked that none of the xal_init_primitives_addon etc are getting set to -1?
And open_dll() isn't returning null - in my experience Windows is a bit fussy about not having ".dll" at the end of the name, but that might be a Phix thing.
Thank you Pete. I forgot to add .dll when loading the library and the first function was missing the "+". The primitive demo now works.
--Simple Display Window demo --Draw primitive shapes include allegro.e include keycodes.e include std/ffi.e include primitives.e procedure main() atom x = al_install_system(ALLEGRO_VERSION_INT,0) atom a = al_init_primitives_addon() if x = -1 then puts(1,"Failed to init Allegro!\n") abort(0) end if if a = -1 then puts(1,"Failed to init addon!\n") abort(0) end if atom dis = al_create_display(800,600) if dis = -1 then puts(1,"Failed to create display!\n") abort(0) end if al_set_window_title(dis,"Primitive Demo") sequence BLUE = al_map_rgb(0,0,255) al_draw_line(800 / 2,0,800,600,BLUE,0) al_flip_display() al_rest(5.0) al_destroy_display(dis) end procedure main()
9. Re: Attempting to subscript an atom Error[SOLVED]
- Posted by ghaberek (admin) Oct 17, 2022
- 819 views
Thank you Pete. I forgot to add .dll when loading the library and the first function was missing the "+". The primitive demo now works.
Sorry I had a busy weekend so I wasn't able to reply in time. The problem here is simply that define_c_func() is returning -1 because it could not find the function name, and c_func() isn't checking the function ID correctly. I will get some more error-checking added soon to help with this.
Could it be that you don't want the + at all, or (less likely) the first is missing a +?
FYI for anyone reading along, and I'll make sure the documentation reflects this as well, the indication of STDCALL (no plus) or CDECL (with plus) calling convention only affects 32-bit Windows and nothing else and I encourage everyone to use 64-bit Euphoria if they are on a 64-bit platform (which they should be) where the plus is silently ignored.
Also, have you checked that none of the xal_init_primitives_addon etc are getting set to -1?
This is the core of the issue. I'm currently using negative function IDs in the wrappers for c_func/proc to avoid collision with the positive function IDs returned by the interpreter's own define_c_func/proc. I just need a few checks in there and then I'll call crash() if the ID is not valid, as the interpreter would do.
You can see the offending code and its lack of error checking here: https://github.com/ghaberek/libffi-euphoria/blob/main/include/std/ffi.e#L900
And open_dll() isn't returning null - in my experience Windows is a bit fussy about not having ".dll" at the end of the name, but that might be a Phix thing.
I'm entertaining the idea of adding some library name guessing features to open_dll(), which would help avoid needing a bunch of ifdef PLATFORM then blocks or having a bunch of failed attempts to load libraries that don't exist.
So for libraries that have a common root name, you'd just call open_dll("foobar") and it would try foobar.dll, libfoobar.so, etc. based on the current platform.
-Greg
10. Re: Attempting to subscript an atom Error[SOLVED]
- Posted by Icy_Viking Oct 17, 2022
- 796 views
Thank you Pete. I forgot to add .dll when loading the library and the first function was missing the "+". The primitive demo now works.
Sorry I had a busy weekend so I wasn't able to reply in time. The problem here is simply that define_c_func() is returning -1 because it could not find the function name, and c_func() isn't checking the function ID correctly. I will get some more error-checking added soon to help with this.
Could it be that you don't want the + at all, or (less likely) the first is missing a +?
FYI for anyone reading along, and I'll make sure the documentation reflects this as well, the indication of STDCALL (no plus) or CDECL (with plus) calling convention only affects 32-bit Windows and nothing else and I encourage everyone to use 64-bit Euphoria if they are on a 64-bit platform (which they should be) where the plus is silently ignored.
Also, have you checked that none of the xal_init_primitives_addon etc are getting set to -1?
This is the core of the issue. I'm currently using negative function IDs in the wrappers for c_func/proc to avoid collision with the positive function IDs returned by the interpreter's own define_c_func/proc. I just need a few checks in there and then I'll call crash() if the ID is not valid, as the interpreter would do.
You can see the offending code and its lack of error checking here: https://github.com/ghaberek/libffi-euphoria/blob/main/include/std/ffi.e#L900
And open_dll() isn't returning null - in my experience Windows is a bit fussy about not having ".dll" at the end of the name, but that might be a Phix thing.
I'm entertaining the idea of adding some library name guessing features to open_dll(), which would help avoid needing a bunch of ifdef PLATFORM then blocks or having a bunch of failed attempts to load libraries that don't exist.
So for libraries that have a common root name, you'd just call open_dll("foobar") and it would try foobar.dll, libfoobar.so, etc. based on the current platform.
-Greg
Those are some cool features you have in plan, Greg. I do plan to start using the 64-bit versions of shared libraries(DLLs/SOs) eventually. I'm using the 32-bit version(s) for compatibility reasons. I guess I could do a platform check for the 32-bit or 64-bit version in the meantime.
11. Re: Attempting to subscript an atom Error[SOLVED]
- Posted by mitgedanken Oct 19, 2022
- 741 views
Sorry, but I must say something: You all are fantastic!