[wasm-api] Add buffer_contents_realloc

This commit is contained in:
Behdad Esfahbod 2023-02-24 09:20:42 -07:00
parent 07ece17495
commit 2d295183b8
4 changed files with 38 additions and 3 deletions

View File

@ -51,6 +51,38 @@ buffer_contents_free (HB_WASM_EXEC_ENV
contents->length = 0;
}
void
buffer_contents_realloc (HB_WASM_EXEC_ENV
ptr_t(buffer_contents_t) contentsptr,
uint32_t size)
{
HB_STRUCT_TYPE (buffer_contents_t, contents);
if (unlikely (!contents))
return;
if (size <= contents->length)
return;
unsigned bytes;
if (hb_unsigned_mul_overflows (size, sizeof (glyph_info_t), &bytes))
return;
// TODO bounds check?
uint32_t infoptr = contents->info;
uint32_t posptr = contents->pos;
const char *info = (const char *) addr_app_to_native (infoptr);
const char *pos = (const char *) addr_app_to_native (posptr);
contents->info = wasm_runtime_module_dup_data (module_inst, info, bytes);
contents->pos = wasm_runtime_module_dup_data (module_inst, pos, bytes);
module_free (infoptr);
module_free (posptr);
// TODO Check success
contents->length = size;
}
void
buffer_copy_contents (HB_WASM_EXEC_ENV_COMPOUND
ptr_t(buffer_t) bufferref)
@ -67,7 +99,6 @@ buffer_copy_contents (HB_WASM_EXEC_ENV_COMPOUND
ret.pos = wasm_runtime_module_dup_data (module_inst, (const char *) buffer->pos, length * sizeof (buffer->pos[0]));
}
bool_t
buffer_set_contents (HB_WASM_EXEC_ENV
ptr_t(buffer_t) bufferref,

View File

@ -56,6 +56,7 @@ static NativeSymbol _hb_wasm_native_symbols[] =
/* buffer */
NATIVE_SYMBOL ("(i)", buffer_contents_free),
NATIVE_SYMBOL ("(ii)", buffer_contents_realloc),
NATIVE_SYMBOL ("(ii)", buffer_copy_contents),
NATIVE_SYMBOL ("(ii)i", buffer_set_contents),

View File

@ -115,6 +115,10 @@ typedef struct
ptr_t(glyph_position_t) pos;
} buffer_contents_t;
HB_WASM_API (void, buffer_contents_realloc) (HB_WASM_EXEC_ENV
ptr_t(buffer_contents_t),
uint32_t size);
HB_WASM_API (void, buffer_contents_free) (HB_WASM_EXEC_ENV
ptr_t(buffer_contents_t));

View File

@ -78,8 +78,7 @@ shape (font_t *font, buffer_t *buffer)
};
length = glyph_count;
//contents.info = (glyph_info_t *) realloc (contents.info, length * sizeof (glyph_info_t));
//contents.pos = (glyph_position_t *) realloc (contents.pos, length * sizeof (glyph_position_t));
buffer_contents_realloc (&contents, length);
cluster_t *clusters = (cluster_t *) malloc (length * sizeof (cluster_t));
uint32_t *gids = (uint32_t *) malloc (length * sizeof (uint32_t));