From 7a948aae582e27186190b1f1d797331681746c9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20A=C4=9Facan?= Date: Tue, 17 Jun 2025 10:26:51 +0100 Subject: [PATCH 1/2] Fix handling of declarative segments Declarative segments can be referred to in runtime, but they will be empty. Update handling to create an empty segment, instead of ignoring. Enable `elem.wast` in the spec test suite. --- scripts/test/shared.py | 1 - src/parser/context-decls.cpp | 13 +++++++++++++ src/parser/contexts.h | 2 +- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/scripts/test/shared.py b/scripts/test/shared.py index 237f59a9b6d..005db54be5d 100644 --- a/scripts/test/shared.py +++ b/scripts/test/shared.py @@ -421,7 +421,6 @@ def get_tests(test_dir, extensions=[], recursive=False): 'const.wast', # Hex float constant not recognized as out of range 'conversions.wast', # Promoted NaN should be canonical 'data.wast', # Constant global references allowed by GC - 'elem.wast', # Requires modeling empty declarative segments 'f32.wast', # Adding -0 and -nan should give a canonical NaN 'f64.wast', # Adding -0 and -nan should give a canonical NaN 'float_exprs.wast', # Adding 0 and NaN should give canonical NaN diff --git a/src/parser/context-decls.cpp b/src/parser/context-decls.cpp index c124689d334..cd1c0e13c25 100644 --- a/src/parser/context-decls.cpp +++ b/src/parser/context-decls.cpp @@ -241,6 +241,19 @@ Result<> ParseDeclsCtx::addElem( return Ok{}; } +Result<> ParseDeclsCtx::addDeclareElem(Name name, ElemListT&&, Index) { + auto e = std::make_unique(); + if (name) { + e->setExplicitName(name); + } else { + name = std::to_string(elemCounter++); + name = Names::getValidElementSegmentName(wasm, name); + e->name = name; + } + wasm.addElementSegment(std::move(e)); + return Ok{}; +} + Result<> ParseDeclsCtx::addData(Name name, MemoryIdxT*, std::optional, diff --git a/src/parser/contexts.h b/src/parser/contexts.h index 48234829b5a..ef9ea48958f 100644 --- a/src/parser/contexts.h +++ b/src/parser/contexts.h @@ -1116,7 +1116,7 @@ struct ParseDeclsCtx : NullTypeParserCtx, NullInstrParserCtx { Result<> addElem(Name, TableIdxT*, std::optional, ElemListT&&, Index); - Result<> addDeclareElem(Name, ElemListT&&, Index) { return Ok{}; } + Result<> addDeclareElem(Name, ElemListT&&, Index); Result<> addData(Name name, MemoryIdxT*, From cd9e67bc2c02acbe0950358adeefe7f62e0eb285 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20A=C4=9Facan?= Date: Tue, 17 Jun 2025 11:38:22 +0100 Subject: [PATCH 2/2] Update lit tests -- not final! --- test/lit/basic/multi-table.wast | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test/lit/basic/multi-table.wast b/test/lit/basic/multi-table.wast index 2d6b436a62d..2b9114b03ce 100644 --- a/test/lit/basic/multi-table.wast +++ b/test/lit/basic/multi-table.wast @@ -76,6 +76,8 @@ ;; CHECK-TEXT: (elem $empty func) ;; CHECK-BIN: (elem $empty func) (elem $empty func) + ;; CHECK-TEXT: (elem $declarative func) + ;; CHECK-BIN: (elem $declarative func) (elem $declarative declare func $h) ;; This elem will be emitted as usesExpressions because of the type of the @@ -142,7 +144,9 @@ ;; CHECK-BIN-NODEBUG: (elem $8 func) -;; CHECK-BIN-NODEBUG: (elem $9 (table $3) (i32.const 0) (ref null $0) (item (ref.func $0)) (item (ref.func $2))) +;; CHECK-BIN-NODEBUG: (elem $9 func) + +;; CHECK-BIN-NODEBUG: (elem $10 (table $3) (i32.const 0) (ref null $0) (item (ref.func $0)) (item (ref.func $2))) ;; CHECK-BIN-NODEBUG: (func $0 (type $0) ;; CHECK-BIN-NODEBUG-NEXT: (drop