Re: [Factor-talk] Output parameters in alien

2014-11-28 Thread Jon Harper
After investigating a bit, it might be possible? If someone more
knowledgeable about the ffi could comment on the following:

FUNCTION: char* strcpy ( char * , c-string ) ;
: foo ( -- ) { { { char 2 } initial: B{ 0x41 0 } } } [ "B" strcpy drop ]
with-out-parameters utf8 alien>string print ;
: bar ( -- ) { { { char 2 } initial: B{ 0x41 0 } } } [ drop ]
with-out-parameters utf8 alien>string print ;

foo ! prints "B"
bar ! prints "A"

If there is no syntactic sugar yet for passing strings with
with-out-parameters, it might be interesting to add it I guess

PS: typed this manually, sorry if there are typos :)
Jon
Le 28 nov. 2014 20:43, "Jon Harper"  a écrit :

> John provided an alternative, but here are the explanations of the errors
> you got:
>
> On Fri, Nov 28, 2014 at 5:14 PM, Andrea Ferretti  > wrote:
>
>>   { { c-string } } [ "hello world" swap example_cp ] with-out-parameters
>>
> The syntax of with-out-parameters is an array of elements, which are
> either a c-type, or a triple { c-type intial: value }. So in your case, it
> should have been
>   { c-string } [ "hello world" swap example_cp ] with-out-parameters
>
> I get "index out of bounds: 0" which seems reasonable, since I pass an
>> empty c-string. But if I try something like
>>
>>   { { c-string initial: "hello earth" } } [ "hello world" swap
>> example_cp ] with-out-parameters
>>
>> I get "local-allocation-error".
>>
> The FFI only works with the optimizing compiler, so this code should be
> defined in a word, not run in the interactive listener. You can define the
> word in a source file, or directly in the listener and then call it just
> after you defined it
>
>
> Cheers,
> Jon
>
--
Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server
from Actuate! Instantly Supercharge Your Business Reports and Dashboards
with Interactivity, Sharing, Native Excel Exports, App Integration & more
Get technology previously reserved for billion-dollar corporations, FREE
http://pubads.g.doubleclick.net/gampad/clk?id=157005751&iu=/4140/ostg.clktrk___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk


Re: [Factor-talk] Output parameters in alien

2014-11-28 Thread Jon Harper
John provided an alternative, but here are the explanations of the errors
you got:

On Fri, Nov 28, 2014 at 5:14 PM, Andrea Ferretti 
wrote:

>   { { c-string } } [ "hello world" swap example_cp ] with-out-parameters
>
The syntax of with-out-parameters is an array of elements, which are either
a c-type, or a triple { c-type intial: value }. So in your case, it should
have been
  { c-string } [ "hello world" swap example_cp ] with-out-parameters

I get "index out of bounds: 0" which seems reasonable, since I pass an
> empty c-string. But if I try something like
>
>   { { c-string initial: "hello earth" } } [ "hello world" swap
> example_cp ] with-out-parameters
>
> I get "local-allocation-error".
>
The FFI only works with the optimizing compiler, so this code should be
defined in a word, not run in the interactive listener. You can define the
word in a source file, or directly in the listener and then call it just
after you defined it


Cheers,
Jon
--
Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server
from Actuate! Instantly Supercharge Your Business Reports and Dashboards
with Interactivity, Sharing, Native Excel Exports, App Integration & more
Get technology previously reserved for billion-dollar corporations, FREE
http://pubads.g.doubleclick.net/gampad/clk?id=157005751&iu=/4140/ostg.clktrk___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk


Re: [Factor-talk] Output parameters in alien

2014-11-28 Thread John Benediktsson
I think you won't be able to use with-out-parameters which seems to work
really well with value types, but maybe not so well for your example.  Part
of the problem might be the way c-string automatically encodes and decodes
using your encoding (utf8 by default I think).  Factor strings are not the
same as char*, so they have have to be converted into and out of "raw
memory" using an encoding when calling functions that take c-strings.

Here's an example using strcpy for convenience, and raw memory (using char*
instead of c-string).  We will try and preserve the dst input pointer
instead of using the return value.

 FUNCTION: char* strcpy ( char* dst, char* src ) ;

We will make a word that takes a Factor string, encodes it into a utf8
byte-array, allocates a new byte-array destination, then calls strcpy,
drops the return value, and uses the destination byte-array to decode back
into a Factor string.

 : do-strcpy ( src -- dst )
 utf8 string>alien
 [ length  dup ] [ strcpy drop ] bi
 utf8 alien>string ;

 IN: scratchpad "hello" do-strcpy .
 "hello"

You can use byte-arrays, or raw memory allocated with malloc (make sure to
&free with destructors, or free manually).







On Fri, Nov 28, 2014 at 8:14 AM, Andrea Ferretti 
wrote:

> I am trying to interface with C code. Everything seems to work more or
> less fine, until I have to deal with output parameters, that is,
> parameters in C functions which are meant to be passed buffers which
> are filled by the function.
>
> I have made a very basic example which only copies a string and I have
> made a shared library called "libexample.so" which exports
>
>   void example_cp(char *in, char *out);
>
> I have verified that I can call the shared library from C.
>
> Now in factor I have defined an interface like this
>
>   USING: alien alien.c-types alien.syntax alien.libraries ;
>   IN: example-alien
>
>   <<
> "libexample" "/home/papillon/esperimenti/ssl/libexample.so" cdecl
> add-library
>   >>
>
>   LIBRARY: libexample
>   FUNCTION: void example_cp ( c-string in, c-string out ) ;
>
> In the listener I try to use example_cp, but I am not sure how to pass
> a preallocated char buffer. If I try
>
>   { { c-string } } [ "hello world" swap example_cp ] with-out-parameters
>
> I get "index out of bounds: 0" which seems reasonable, since I pass an
> empty c-string. But if I try something like
>
>   { { c-string initial: "hello earth" } } [ "hello world" swap
> example_cp ] with-out-parameters
>
> I get "local-allocation-error".
>
> What is the correct way to pass a preallocated buffer?
>
>
> --
> Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server
> from Actuate! Instantly Supercharge Your Business Reports and Dashboards
> with Interactivity, Sharing, Native Excel Exports, App Integration & more
> Get technology previously reserved for billion-dollar corporations, FREE
>
> http://pubads.g.doubleclick.net/gampad/clk?id=157005751&iu=/4140/ostg.clktrk
> ___
> Factor-talk mailing list
> Factor-talk@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/factor-talk
>
--
Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server
from Actuate! Instantly Supercharge Your Business Reports and Dashboards
with Interactivity, Sharing, Native Excel Exports, App Integration & more
Get technology previously reserved for billion-dollar corporations, FREE
http://pubads.g.doubleclick.net/gampad/clk?id=157005751&iu=/4140/ostg.clktrk___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk


[Factor-talk] Output parameters in alien

2014-11-28 Thread Andrea Ferretti
I am trying to interface with C code. Everything seems to work more or
less fine, until I have to deal with output parameters, that is,
parameters in C functions which are meant to be passed buffers which
are filled by the function.

I have made a very basic example which only copies a string and I have
made a shared library called "libexample.so" which exports

  void example_cp(char *in, char *out);

I have verified that I can call the shared library from C.

Now in factor I have defined an interface like this

  USING: alien alien.c-types alien.syntax alien.libraries ;
  IN: example-alien

  <<
"libexample" "/home/papillon/esperimenti/ssl/libexample.so" cdecl
add-library
  >>

  LIBRARY: libexample
  FUNCTION: void example_cp ( c-string in, c-string out ) ;

In the listener I try to use example_cp, but I am not sure how to pass
a preallocated char buffer. If I try

  { { c-string } } [ "hello world" swap example_cp ] with-out-parameters

I get "index out of bounds: 0" which seems reasonable, since I pass an
empty c-string. But if I try something like

  { { c-string initial: "hello earth" } } [ "hello world" swap
example_cp ] with-out-parameters

I get "local-allocation-error".

What is the correct way to pass a preallocated buffer?

--
Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server
from Actuate! Instantly Supercharge Your Business Reports and Dashboards
with Interactivity, Sharing, Native Excel Exports, App Integration & more
Get technology previously reserved for billion-dollar corporations, FREE
http://pubads.g.doubleclick.net/gampad/clk?id=157005751&iu=/4140/ostg.clktrk
___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk