@@ -221,7 +221,7 @@ namemap_clear(struct nametable *table, struct namemap *map)
221221 map -> entries = NULL ;
222222}
223223
224- void
224+ static int
225225namemap_lookup (const struct module * m , const struct namemap * map ,
226226 uint32_t funcidx , struct name * name )
227227{
@@ -240,15 +240,15 @@ namemap_lookup(const struct module *m, const struct namemap *map,
240240 const uint8_t * p = m -> bin + offset ;
241241 name -> nbytes = read_leb_u32_nocheck (& p );
242242 name -> data = (const char * )p ;
243- return ;
243+ return 0 ;
244244 }
245245 if (naming -> idx < funcidx ) {
246246 left = mid + 1 ;
247247 } else {
248248 right = mid ;
249249 }
250250 }
251- set_name_cstr ( name , unknown_name ) ;
251+ return ENOENT ;
252252}
253253#endif /* defined(TOYWASM_ENABLE_WASM_NAME_SECTION) */
254254
@@ -307,20 +307,37 @@ nametable_clear(struct nametable *table)
307307#endif
308308}
309309
310+ static int
311+ exports_lookup (const struct module * m , enum externtype type , uint32_t idx ,
312+ struct name * name )
313+ {
314+ uint32_t i ;
315+ for (i = 0 ; i < m -> nexports ; i ++ ) {
316+ const struct wasm_export * e = & m -> exports [i ];
317+ if (e -> desc .type == type && e -> desc .idx == idx ) {
318+ * name = e -> name ;
319+ return 0 ;
320+ }
321+ }
322+ return ENOENT ;
323+ }
324+
310325void
311326nametable_lookup_func (struct nametable * table , const struct module * m ,
312327 uint32_t funcidx , struct name * name )
313328{
314329#if defined(TOYWASM_ENABLE_WASM_NAME_SECTION )
315330 nametable_add_module (table , m );
316- if (table -> module == m ) {
317- namemap_lookup (table -> module , & table -> funcs , funcidx , name );
318- } else {
319- set_name_cstr (name , unknown_name );
331+ if (table -> module == m &&
332+ namemap_lookup (table -> module , & table -> funcs , funcidx , name ) == 0 ) {
333+ return ;
320334 }
321- #else
322- set_name_cstr (name , unknown_name );
323335#endif
336+ /* use exported name as a fallback */
337+ if (exports_lookup (m , EXTERNTYPE_FUNC , funcidx , name ) == 0 ) {
338+ return ;
339+ }
340+ set_name_cstr (name , unknown_name );
324341}
325342
326343void
@@ -329,15 +346,17 @@ nametable_lookup_global(struct nametable *table, const struct module *m,
329346{
330347#if defined(TOYWASM_ENABLE_WASM_NAME_SECTION )
331348 nametable_add_module (table , m );
332- if (table -> module == m ) {
333- namemap_lookup (table -> module , & table -> globals , globalidx ,
334- name );
335- } else {
336- set_name_cstr (name , unknown_name );
349+ if (table -> module == m &&
350+ namemap_lookup (table -> module , & table -> globals , globalidx , name ) ==
351+ 0 ) {
352+ return ;
337353 }
338- #else
339- set_name_cstr (name , unknown_name );
340354#endif
355+ /* use exported name as a fallback */
356+ if (exports_lookup (m , EXTERNTYPE_GLOBAL , globalidx , name ) == 0 ) {
357+ return ;
358+ }
359+ set_name_cstr (name , unknown_name );
341360}
342361
343362void
@@ -346,14 +365,12 @@ nametable_lookup_data(struct nametable *table, const struct module *m,
346365{
347366#if defined(TOYWASM_ENABLE_WASM_NAME_SECTION )
348367 nametable_add_module (table , m );
349- if (table -> module == m ) {
350- namemap_lookup (table -> module , & table -> datas , dataidx , name );
351- } else {
352- set_name_cstr (name , unknown_name );
368+ if (table -> module == m &&
369+ namemap_lookup (table -> module , & table -> datas , dataidx , name ) == 0 ) {
370+ return ;
353371 }
354- #else
355- set_name_cstr (name , unknown_name );
356372#endif
373+ set_name_cstr (name , unknown_name );
357374}
358375
359376void
@@ -364,10 +381,8 @@ nametable_lookup_module(struct nametable *table, const struct module *m,
364381 nametable_add_module (table , m );
365382 if (table -> module == m && table -> module_name .data != NULL ) {
366383 * name = table -> module_name ;
367- } else {
368- set_name_cstr (name , unknown_name );
384+ return ;
369385 }
370- #else
371- set_name_cstr (name , unknown_name );
372386#endif
387+ set_name_cstr (name , unknown_name );
373388}
0 commit comments