Bind a New Module in Engine

Let me try to return Image

This line crash.

It’s strange that if I call that function from jsLoadImage() in jsb_global.cpp, it works although it return ImageInfo.

I guess it’s better to see by Anydesk.

I added one function in MyObject class.

uint8_t* getImageUncompressed() {
int w = 200, h = 200, depth = 4;
uint8_t* binRGBA = ccnew uint8_t[w * h * depth];
memset(binRGBA, 0, w * h * depth);
for (int i = 0; i < w; i++) {
for (int j = 0; j < h; j++) {
int pxIdx = i * h + j;
binRGBA[pxIdx * 4] = 255;
binRGBA[pxIdx * 4 + 1] = 255;
binRGBA[pxIdx * 4 + 2] = 255;
binRGBA[pxIdx * 4 + 3] = 255;
}
}

    return binRGBA;

}

And this is binding function generated in jsb_my_module_auto.cpp.
static bool js_my_ns_MyObject_getImageUncompressed(se::State& s)
{
CC_UNUSED bool ok = true;
const auto& args = s.args();
size_t argc = args.size();
my_ns::MyObject *arg1 = (my_ns::MyObject *) NULL ;
uint8_t *result = 0 ;

if(argc != 0) {
    SE_REPORT_ERROR("wrong number of arguments: %d, was expecting %d", (int)argc, 0);
    return false;
}
arg1 = SE_THIS_OBJECT<my_ns::MyObject>(s);
if (nullptr == arg1) return true;
result = (uint8_t *)(arg1)->getImageUncompressed();

ok &= nativevalue_to_se(result, s.rval(), s.thisObject());
SE_PRECONDITION2(ok, false, "Error processing arguments");
SE_HOLD_RETURN_VALUE(result, s.thisObject(), s.rval()); 


return true;

}
SE_BIND_FUNC(js_my_ns_MyObject_getImageUncompressed)

It crashes in this line;
ok &= nativevalue_to_se(result, s.rval(), s.thisObject());

What’s wrong?

The pointer of primitive types like uint8_t*/float*/int* are not supported to be bound to JS because the framework doesn’t know how to convert the pointer primitive type to JS, it doesn’t know the exact buffer size.

So if you need to copy a buffer to JS actually, you could return a ccstd::vector<uint8_t> instead.
Copying buffer from CPP to JS will cost a lot. I don’t know why you need to hack the image loading process.

It works, Thank you very much!