Skip to content

Commit bcaa88f

Browse files
committed
WasmBinaryReader: Consistent internal naming
For defined functions/globals/tables/memories use `f1`, `t2`, `m3` etc. This matches the longer names used for imports such as `fimport$1`, but keeps the names nice and short by avoiding the embedded dollar sign.
1 parent 6fe3d88 commit bcaa88f

File tree

248 files changed

+7202
-7041
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

248 files changed

+7202
-7041
lines changed

src/parser/context-decls.cpp

+9-6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ namespace wasm::WATParser {
2020

2121
namespace {
2222

23+
Name makeName(std::string prefix, size_t counter) {
24+
return Name(prefix + std::to_string(counter));
25+
}
26+
2327
void applyImportNames(Importable& item, ImportNames* names) {
2428
if (names) {
2529
item.module = names->mod;
@@ -55,7 +59,7 @@ ParseDeclsCtx::addFuncDecl(Index pos, Name name, ImportNames* importNames) {
5559
}
5660
f->setExplicitName(name);
5761
} else {
58-
name = (importNames ? "fimport$" : "") + std::to_string(funcCounter++);
62+
name = makeName(importNames ? "fimport$" : "f", funcCounter++);
5963
name = Names::getValidFunctionName(wasm, name);
6064
f->name = name;
6165
}
@@ -95,7 +99,7 @@ Result<Table*> ParseDeclsCtx::addTableDecl(Index pos,
9599
}
96100
t->setExplicitName(name);
97101
} else {
98-
name = (importNames ? "timport$" : "") + std::to_string(tableCounter++);
102+
name = makeName(importNames ? "timport$" : "t", tableCounter++);
99103
name = Names::getValidTableName(wasm, name);
100104
t->name = name;
101105
}
@@ -151,7 +155,7 @@ Result<Memory*> ParseDeclsCtx::addMemoryDecl(Index pos,
151155
}
152156
m->setExplicitName(name);
153157
} else {
154-
name = (importNames ? "mimport$" : "") + std::to_string(memoryCounter++);
158+
name = makeName(importNames ? "mimport$" : "m", memoryCounter++);
155159
name = Names::getValidMemoryName(wasm, name);
156160
m->name = name;
157161
}
@@ -196,8 +200,7 @@ ParseDeclsCtx::addGlobalDecl(Index pos, Name name, ImportNames* importNames) {
196200
}
197201
g->setExplicitName(name);
198202
} else {
199-
name =
200-
(importNames ? "gimport$" : "global$") + std::to_string(globalCounter++);
203+
name = makeName(importNames ? "gimport$" : "g", globalCounter++);
201204
name = Names::getValidGlobalName(wasm, name);
202205
g->name = name;
203206
}
@@ -277,7 +280,7 @@ ParseDeclsCtx::addTagDecl(Index pos, Name name, ImportNames* importNames) {
277280
}
278281
t->setExplicitName(name);
279282
} else {
280-
name = (importNames ? "eimport$" : "tag$") + std::to_string(tagCounter++);
283+
name = makeName(importNames ? "eimport$" : "tag$", tagCounter++);
281284
name = Names::getValidTagName(wasm, name);
282285
t->name = name;
283286
}

src/wasm/wasm-binary.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -2249,7 +2249,7 @@ void WasmBinaryReader::readMemories() {
22492249
BYN_TRACE("num: " << num << std::endl);
22502250
for (size_t i = 0; i < num; i++) {
22512251
BYN_TRACE("read one\n");
2252-
auto memory = Builder::makeMemory(makeName("", i));
2252+
auto memory = Builder::makeMemory(makeName("m", i));
22532253
getResizableLimits(memory->initial,
22542254
memory->max,
22552255
memory->shared,
@@ -2689,7 +2689,7 @@ void WasmBinaryReader::readFunctions() {
26892689
endOfFunction = pos + size;
26902690

26912691
auto func = std::make_unique<Function>();
2692-
func->name = makeName("", i);
2692+
func->name = makeName("f", i);
26932693
func->type = getTypeByFunctionIndex(numImports + i);
26942694
currFunction = func.get();
26952695

@@ -3047,7 +3047,7 @@ void WasmBinaryReader::readGlobals() {
30473047
}
30483048
auto* init = readExpression();
30493049
wasm.addGlobal(
3050-
Builder::makeGlobal(makeName("global$", i),
3050+
Builder::makeGlobal(makeName("g", i),
30513051
type,
30523052
init,
30533053
mutable_ ? Builder::Mutable : Builder::Immutable));
@@ -3367,7 +3367,7 @@ void WasmBinaryReader::readTableDeclarations() {
33673367
if (!elemType.isRef()) {
33683368
throwError("Table type must be a reference type");
33693369
}
3370-
auto table = Builder::makeTable(makeName("", i), elemType);
3370+
auto table = Builder::makeTable(makeName("t", i), elemType);
33713371
bool is_shared;
33723372
getResizableLimits(table->initial,
33733373
table->max,

test/br_to_exit.wasm.fromBinary

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(module
22
(type $0 (func))
3-
(func $0
3+
(func $f0
44
(block $label$0
55
(br $label$0)
66
)

test/br_to_try.wasm.fromBinary

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
(type $0 (func (param i32)))
33
(type $1 (func))
44
(tag $tag$0 (param i32))
5-
(func $0
5+
(func $f0
66
(try $label$3
77
(do
88
(block $label$1

test/break-to-return.wasm.fromBinary

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
(module
22
(type $0 (func (param i32 i32) (result i32)))
3-
(memory $0 256 256)
4-
(export "add" (func $0))
5-
(func $0 (param $0 i32) (param $1 i32) (result i32)
3+
(memory $m0 256 256)
4+
(export "add" (func $f0))
5+
(func $f0 (param $0 i32) (param $1 i32) (result i32)
66
(block $label$0 (result i32)
77
(br $label$0
88
(i32.add

test/break-within-catch.wasm.fromBinary

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
(type $0 (func (param i32)))
33
(type $1 (func))
44
(tag $tag$0 (param i32))
5-
(func $0
5+
(func $f0
66
(block $label$2
77
(try $label$3
88
(do

test/consume-stacky.wasm.fromBinary

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(module
22
(type $0 (func (result i32)))
3-
(memory $0 1 1)
4-
(func $0 (result i32)
3+
(memory $m0 1 1)
4+
(func $f0 (result i32)
55
(local $0 i32)
66
(local.set $0
77
(i32.const 1)

test/ctor-eval/bad-indirect-call.wast.out

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
(module
22
(type $v (func))
3-
(memory $0 256 256)
3+
(memory $m0 256 256)
44
(data $0 (i32.const 10) "waka waka waka waka waka")
5-
(table $0 1 1 funcref)
5+
(table $t0 1 1 funcref)
66
(elem $0 (i32.const 0) $call-indirect)
77
(export "test1" (func $test1))
88
(func $test1 (type $v)
9-
(call_indirect $0 (type $v)
9+
(call_indirect $t0 (type $v)
1010
(i32.const 1)
1111
)
1212
(i32.store8

test/ctor-eval/bad-indirect-call2.wast.out

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
(module
22
(type $v (func))
33
(import "env" "_abort" (func $_abort (type $v)))
4-
(memory $0 256 256)
4+
(memory $m0 256 256)
55
(data $0 (i32.const 10) "waka waka waka waka waka")
6-
(table $0 2 2 funcref)
6+
(table $t0 2 2 funcref)
77
(elem $0 (i32.const 0) $_abort $call-indirect)
88
(export "test1" (func $test1))
99
(func $test1 (type $v)
10-
(call_indirect $0 (type $v)
10+
(call_indirect $t0 (type $v)
1111
(i32.const 0)
1212
)
1313
(i32.store8

test/ctor-eval/bad-indirect-call3.wast.out

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
(type $0 (func (param externref)))
33
(type $1 (func))
44
(type $funcref_=>_none (func (param funcref)))
5-
(memory $0 256 256)
5+
(memory $m0 256 256)
66
(data $0 (i32.const 10) "waka waka waka waka waka")
7-
(table $0 1 1 funcref)
7+
(table $t0 1 1 funcref)
88
(elem $implicit-elem (i32.const 0) $callee)
99
(export "sig_mismatch" (func $sig_mismatch))
1010
(func $callee (type $0) (param $0 externref)
@@ -14,7 +14,7 @@
1414
)
1515
)
1616
(func $sig_mismatch (type $1)
17-
(call_indirect $0 (type $funcref_=>_none)
17+
(call_indirect $t0 (type $funcref_=>_none)
1818
(ref.null nofunc)
1919
(i32.const 0)
2020
)

test/ctor-eval/basics-flatten.wast.out

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(module
22
(type $v (func))
3-
(memory $0 256 256)
3+
(memory $m0 256 256)
44
(data $0 (i32.const 10) "nas\00\00\00aka\00yzkx waka wakm\00\00\00\00\00\00C")
55
(func $call-indirect (type $v)
66
(i32.store8

test/ctor-eval/basics.wast.out

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(module
22
(type $v (func))
3-
(memory $0 256 256)
3+
(memory $m0 256 256)
44
(data $0 (i32.const 10) "nas\00\00\00aka yzkx waka wakm\00\00\00\00\00\00C")
55
(func $call-indirect (type $v)
66
(i32.store8

test/ctor-eval/ignore-external-input.wast.out

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
(type $0 (func (result i32)))
33
(type $1 (func))
44
(import "wasi_snapshot_preview1" "something_else" (func $wasi_something_else (type $0) (result i32)))
5-
(memory $0 256 256)
5+
(memory $m0 256 256)
66
(data $0 (i32.const 28) "aaaa")
77
(export "test3" (func $test3))
88
(func $test3 (type $1)

test/ctor-eval/imported-global-2.wast.out

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(module
22
(type $0 (func (result i32)))
33
(import "env" "imported" (global $imported i32))
4-
(memory $0 256 256)
4+
(memory $m0 256 256)
55
(export "test1" (func $test1))
66
(export "keepalive" (func $keepalive))
77
(func $test1 (type $0) (result i32)

test/ctor-eval/imported-global.wast.out

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(module
22
(type $0 (func))
3-
(memory $0 256 256)
3+
(memory $m0 256 256)
44
(data $0 (i32.const 10) "waka waka waka waka waka")
55
(export "test1" (func $test1))
66
(func $test1 (type $0)

test/ctor-eval/indirect-call3.wast.out

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(module
22
(type $v (func))
33
(import "env" "_abort" (func $_abort (type $v)))
4-
(memory $0 256 256)
4+
(memory $m0 256 256)
55
(data $0 (i32.const 10) "waka waka xaka waka waka\00\00\00\00\00\00C")
66
(func $call-indirect (type $v)
77
(i32.store8

test/ctor-eval/just_some.wast.out

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(module
22
(type $0 (func))
3-
(memory $0 256 256)
3+
(memory $m0 256 256)
44
(data $0 (i32.const 10) "wasa waka waka waka waka")
55
(export "test2" (func $test2))
66
(export "test3" (func $test3))

test/ctor-eval/partial-locals-tee.wast.out

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
(type $0 (func (param i32 i32)))
33
(type $1 (func))
44
(import "import" "import" (func $import (type $0) (param i32 i32)))
5-
(memory $0 256 256)
5+
(memory $m0 256 256)
66
(data $0 (i32.const 10) "__s______________")
77
(export "test1" (func $test1_2))
88
(func $test1_2 (type $1)

test/ctor-eval/partial-locals.wast.out

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
(type $0 (func))
33
(import "import" "import" (func $import (type $0)))
44
(global $sp (mut i32) (i32.const 104))
5-
(memory $0 256 256)
5+
(memory $m0 256 256)
66
(data $0 (i32.const 10) "__s______________")
77
(export "test1" (func $test1_2))
88
(func $test1_2 (type $0)

test/ctor-eval/partial-return.wast

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
(data (i32.const 10) "_________________")
66

77
(export "test1" (func $test1))
8-
(export "memory" (memory $0))
8+
(export "memory" (memory 0))
99

1010
(func $test1
1111
;; A safe store, should alter memory
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(module
2-
(memory $0 256 256)
2+
(memory $m0 256 256)
33
(data $0 (i32.const 10) "__s______________")
4-
(export "memory" (memory $0))
4+
(export "memory" (memory $m0))
55
)

test/ctor-eval/partial.wast.out

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(module
22
(type $0 (func))
33
(import "import" "import" (func $import (type $0)))
4-
(memory $0 256 256)
4+
(memory $m0 256 256)
55
(data $0 (i32.const 10) "__s______________")
66
(export "test1" (func $test1_2))
77
(export "keepalive" (func $test1))

test/ctor-eval/unsafe_call.wast.out

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(module
22
(type $0 (func))
3-
(memory $0 256 256)
3+
(memory $m0 256 256)
44
(data $0 (i32.const 10) "waka waka waka waka waka")
55
(export "test1" (func $test1))
66
(func $test1 (type $0)

0 commit comments

Comments
 (0)