diff --git a/src/hb-wasm-api-buffer.hh b/src/hb-wasm-api-buffer.hh index 3164b7617..a5560bc25 100644 --- a/src/hb-wasm-api-buffer.hh +++ b/src/hb-wasm-api-buffer.hh @@ -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, diff --git a/src/hb-wasm-api-list.hh b/src/hb-wasm-api-list.hh index dc29b2f8f..cb60ee76a 100644 --- a/src/hb-wasm-api-list.hh +++ b/src/hb-wasm-api-list.hh @@ -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), diff --git a/src/hb-wasm-api.h b/src/hb-wasm-api.h index e5f6700d1..405189ed3 100644 --- a/src/hb-wasm-api.h +++ b/src/hb-wasm-api.h @@ -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)); diff --git a/src/wasm-graphite/shape.cc b/src/wasm-graphite/shape.cc index 546cfd5dc..963a00651 100644 --- a/src/wasm-graphite/shape.cc +++ b/src/wasm-graphite/shape.cc @@ -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));