That’s what I’ve tried but it doesn’t seem to work for me. Here’s my C structure, where d_name
is the field in question:
typedef struct {
int32_t d_ino;
int32_t d_off;
uint16_t d_reclen;
uint16_t d_type;
char d_name[NAME_MAX];
} cbFS_dirent_t;
I describe it for s2o
as follows:
static const struct mjs_c_struct_member fsDirEntryDescription[] = {
{ "d_ino", offsetof(cbFS_dirent_t, d_ino), MJS_STRUCT_FIELD_TYPE_INT, NULL },
{ "d_off", offsetof(cbFS_dirent_t, d_off), MJS_STRUCT_FIELD_TYPE_INT, NULL },
{ "d_reclen", offsetof(cbFS_dirent_t, d_reclen), MJS_STRUCT_FIELD_TYPE_UINT16, NULL },
{ "d_type", offsetof(cbFS_dirent_t, d_type), MJS_STRUCT_FIELD_TYPE_UINT16, NULL },
{ "d_name", offsetof(cbFS_dirent_t, d_name), MJS_STRUCT_FIELD_TYPE_DATA, NULL },
{ NULL, 0, MJS_STRUCT_FIELD_TYPE_INVALID, NULL }
};
static const struct mjs_c_struct_member *mjsApiFsDirEntryDescription()
{
return fsDirEntryDescription;
}
static int mjsApiFsDirEntrySize()
{
return sizeof(cbFS_dirent_t);
}
So in mJS the code is:
let DirRead = function(dir) {
let result;
let entryC = c.malloc(ffi('int mjsApiFsDirEntrySize()')());
if (entryC !== 0) {
let retValue = ffi('int mjsApiFsReadDirR(void *, void *)')(dir, entryC); // Call the C function that fills in entryC
if (retValue === 0) {
let entryDescription = ffi('void *mjsApiFsDirEntryDescription()')();
let entryJs = s2o(entryC, entryDescription);
print("d_ino: ", entryJs.d_ino, ".\n");
print("d_off: ", entryJs.d_off, ".\n");
print("d_reclen: ", entryJs.d_reclen, ".\n");
print("d_type: ", entryJs.d_type, ".\n");
print("d_name: \"", entryJs.d_name, "\".\n");
result.type = entryJs.d_type;
// Since entryJs.d_name doesn't seem to be a string, I've tried doing this also but this gives me the "unsupported object type" error
result.name = mkstr(entryJs.d_name, 0, c.strlen(entryJs.d_name), true);
print("result.name: \"", result.name, "\".\n");
}
c.free(entryC);
}
When I run this, in the C function that fills in the struct
I print out the values of the fields:
d_ino 65568, d_off 0, d_reclen 24, d_type 8, d_name "at_client.js"
…but in mJS what I get is:
d_ino: 65568 .
d_off: 0 .
d_reclen: 24 .
d_type: 8 .
d_name: " ".
MJS Print: unsupported object type
As you can see, the numbers are correct but the string is nowhere to be seen. What am I doing wrong?