COMMUNITY

Create an Haxe/HL string from native C/C++ extension

I have seen various examples on how to read an Haxe string in a native C function, but none to create a string and return it to the Haxe code.

In hl.h a string is defined with this struct:

typedef struct {
	hl_type *t;
	uchar *bytes;
	int length;
} vstring;

based on this definition, should I create a string like this?

HL_PRIM void HL_NAME(create_string)(vstring* st)
{
    ...
    // st is supposed to be an empty string coming from the Haxe side
    st->bytes = hl_gc_alloc_noptr(size*2);
    st->length = size;
    memcpy(st->bytes, ..., size*2);
}

I do this on Haxe side from vbyte* (HL type alias, can be casted from const char*).
You can check this issue: https://github.com/HaxeFoundation/haxe/issues/8663
And related Kha PR as example: https://github.com/Kode/Kha/pull/1112