[sedonadev] how to pass a sedona buf array to native and back.

49 views
Skip to first unread message

keith bradley

unread,
May 2, 2017, 12:37:19 PM5/2/17
to sedo...@googlegroups.com
How does one create a sys::Buf , gets it's pointer in memory, then pass this to a native function to be filled?


Sedona (myClass.sedona):

Buf    myBuf[29]
native   int   myNative(myBuf)



Native (myKit_myComp.c):

Cell myKit_myComp_myNative(SedonaVM* vm, Cell* params)
{
  *myPointer = (void*) params[1].ival;

  // fill buffer

  // return

}


Mateusz Klatecki

unread,
May 17, 2017, 8:32:46 AM5/17/17
to Sedona Framework
Hi,

sys::Buf is complex class which contains size, length and byte array with data. Although it is possible to pass a pointer to a Buf object and perform an operation on it in native function, it is not trivial and I would not recommend it.

I would rather use a normal byte array and work on it native function.

myClass.sedona:
byte[29] myArray
native   int   myNative(byte[] arr)

Cell myKit_myComp_myNative(SedonaVM* vm, Cell* params)
{
  uint8_t *myPointer =  params[1].aval;

  // fill buffer

for (int i=0;i<29;i++) {
myPointer[i] = i
}

  // return
return nullCell
}

keith bradley

unread,
May 17, 2017, 10:17:59 AM5/17/17
to sedo...@googlegroups.com
Thanks Mateusz!

I will take your advice.


--
You received this message because you are subscribed to the Google Groups "Sedona Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sedonadev+unsubscribe@googlegroups.com.
To post to this group, send email to sedo...@googlegroups.com.
Visit this group at https://groups.google.com/group/sedonadev.
For more options, visit https://groups.google.com/d/optout.

BobWirka

unread,
May 25, 2017, 5:06:46 PM5/25/17
to Sedona Framework
Here is an example of how a native function can access a 'Buf' object passed from a component function:

Cell MyNativeFcn_print(SedonaVM* vm, Cell* params)
{
    Cell        retval;
    uint8_t* s = params[0].aval;

    /*
     * Format of the passed 'Buf' object seems to be:
     * s[0..1] uint16_t Unknown; probably a 'handle'.
     * s[2..3] uint16_t Number of bytes.
     * s[4..n] uint8_t[] Array of bytes.
     *
     * Data is LITTLE ENDIAN.
     *
     * In the case of a string, the last character is '0', so
     * you can do a printf on s[4]. Note that '123' shows up as
     * an array of length 4, with '0' as the 4th byte.
     */

    // Print the string.
    printf("%s\n" , &s[4]);
    // And return.
    retval.ival = 0;
    return retval;
}

Bob Wirka

keith bradley

unread,
May 25, 2017, 10:08:38 PM5/25/17
to sedo...@googlegroups.com
Thanks Bob !

I will chew on this.

Reply all
Reply to author
Forward
0 new messages