freenode/#ecl - IRC Chatlog
Search
17:52:24
princesspancake
hello there. is there a way, from C, to create a CL object for a foreign type ? I can't find anything in the manual regarding that
17:58:04
princesspancake
i mean, i have a C function called in my shared library (generated with ECL), which receives an argument. I want to convert that argument into a cl_object to pass it to my entry point in Lisp
18:00:34
princesspancake
thanks, for now i'm declaring a C global variable using ffi:clines and then using ffi:def-foreign-var but it's not very nice
18:01:32
jackdaniel
if you want to get back the pointer, you call ecl_foreign_data_ointer_safe(that_cl_object);
18:01:46
jackdaniel
I think that it is documented somewhere, but I'm too busy now to double-check that
18:02:08
princesspancake
well i have the manual open for 4 days now, and never seen anything like that
18:02:51
pjb
you would call a constructor. For example: void f(int x){ cl_object cl_x=ecl_make_fixnum(x); cl_object cl_res=cl_funcall(2,cl_fun_x,cl_x); }
18:03:57
princesspancake
i tried, it's a large codebase for me and last time i wasn't even able to find ecl_init_module or ecl_argv iirc
18:04:22
jackdaniel
it is fine that you've asked, there is no point in grokking undocumented features without prior knowledge
18:05:25
princesspancake
well i'm making something that can only be done with ECL, and it's a plugin for a game engine using a C interface, so i have a good amount of low level code to generated from lisp
18:06:36
jackdaniel
n.b you may use a shorthand versions: ecl_make_pointer(x) and ecl_to_pointer(x). either way, this interface is not composable with structures afaikt
18:07:32
princesspancake
well the data i have is simply a pointer, i can hack that issue with a little ffi:c-inline
18:09:06
princesspancake
ok, ecl_make_pointer(x) takes a void* right ? and the resulting FFI object has a type of :pointer-void, am i correct ?
18:12:22
princesspancake
also, what should i pass to the `cl_boot` function ? it requires an ARGC and ARGV but with that game engine i can't access those. for now i'm hacking it with a static char** array {"ecl"} but i have no idea if it will backfire one day
18:19:06
jackdaniel
if you leave the function that initializes ecl, then it should be a static global, sure
18:27:06
princesspancake
with `ECL_CATCH_ALL_*`, is there a way in the `IF_CAUGHT`brench to get what have been caught or get a message or something ?
18:38:40
jackdaniel
sure, doing things in lisp is safer (however it is not that unlikely that you may hit ecl's bug resulting in a segfault anyway)
18:59:50
princesspancake
is there an example of ECL_HANDLER_CASE in ecl's codebase ? i have trouble figuring out how to use it
19:08:53
jackdaniel
as of example, I won't write it -- here's a brief example: https://common-lisp.net/project/ecl/static/manual/Conditions.html#index-ECL_005fHANDLER_005fCASE
19:09:25
jackdaniel
basically the first argument to ecl_handler_case is the position in the list (indexed from 1) passed as a second argument to ECL_HANDLER_CASE_BEGIN
19:40:53
princesspancake
hm if i have a lisp function returning a string, how do i access that string as a char* ?
19:42:17
jackdaniel
https://common-lisp.net/project/ecl/static/manual/Foreign-Function-Interface.html#Foreign-Strings (ffi:convert-to-foreign-string str)
19:44:36
jackdaniel
hm, you may be right that ecl_to_pointer is necessary, because indeed the function should be available in lisp env
19:45:08
princesspancake
ye i dont want to deal with char* allocation and manipulation in C so i'm making my error formatter in lisp and will just call it ahah
19:45:39
jackdaniel
note, that before conferting lisp string to c string, you must ensure that it is a base string
19:53:00
jackdaniel
it either returns a base string (lisp object), or signals an error if it can't coerce it
19:55:40
jackdaniel
that may be further converted to a foreign string (either with ffi:c-t-f-s or with ecl_base_string_ointer_safe)
19:55:56
jackdaniel
the former returns cl_object that is a foreign object, the latter returns a pointer directly
19:58:51
jackdaniel
by default ecl comes with a conservative gc (however you may build it with precise gc)