Skip to content

Commit ec178c9

Browse files
authored
Bysyncify: allow wildcard endings in import list (#2190)
This allows us to do things in emscripten like note that all env.invoke_* functions are important.
1 parent ba6cf2e commit ec178c9

3 files changed

Lines changed: 82 additions & 9 deletions

File tree

src/passes/Bysyncify.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@
185185
// Each module.base in that comma-separated list will be considered to
186186
// be an import that can unwind/rewind, and all others are assumed not to
187187
// (aside from the bysyncify.* imports, which are always assumed to).
188+
// Each entry can end in a '*' in which case it is matched as a prefix.
188189
//
189190
// --pass-arg=bysyncify-ignore-imports
190191
//
@@ -206,6 +207,7 @@
206207
#include "ir/module-utils.h"
207208
#include "ir/utils.h"
208209
#include "pass.h"
210+
#include "support/string.h"
209211
#include "support/unique_deferring_queue.h"
210212
#include "wasm-builder.h"
211213
#include "wasm.h"
@@ -960,14 +962,11 @@ struct Bysyncify : public Pass {
960962
// Find which things can change the state.
961963
auto stateChangingImports =
962964
runner->options.getArgumentOrDefault("bysyncify-imports", "");
963-
std::string separator = ",";
964965
auto ignoreImports =
965966
runner->options.getArgumentOrDefault("bysyncify-ignore-imports", "");
966967
bool allImportsCanChangeState =
967968
stateChangingImports == "" && ignoreImports == "";
968-
if (!allImportsCanChangeState) {
969-
stateChangingImports = separator + stateChangingImports + separator;
970-
}
969+
String::Split listedImports(stateChangingImports, ",");
971970
auto ignoreIndirect =
972971
runner->options.getArgumentOrDefault("bysyncify-ignore-indirect", "");
973972

@@ -978,8 +977,13 @@ struct Bysyncify : public Pass {
978977
if (allImportsCanChangeState) {
979978
return true;
980979
}
981-
std::string full = separator + module.str + '.' + base.str + separator;
982-
return stateChangingImports.find(full) != std::string::npos;
980+
std::string full = std::string(module.str) + '.' + base.str;
981+
for (auto& listedImport : listedImports) {
982+
if (String::wildcardMatch(listedImport, full)) {
983+
return true;
984+
}
985+
}
986+
return false;
983987
},
984988
ignoreIndirect == "");
985989

src/support/name.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17-
#ifndef wasm_support_string_h
18-
#define wasm_support_string_h
17+
#ifndef wasm_support_name_h
18+
#define wasm_support_name_h
1919

2020
#include <cstring>
2121

@@ -65,4 +65,4 @@ template<> struct hash<wasm::Name> : hash<cashew::IString> {};
6565

6666
} // namespace std
6767

68-
#endif // wasm_support_string_h
68+
#endif // wasm_support_name_h

src/support/string.h

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2019 WebAssembly Community Group participants
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
//
18+
// String helpers.
19+
//
20+
21+
#ifndef wasm_support_string_h
22+
#define wasm_support_string_h
23+
24+
#include <string>
25+
#include <vector>
26+
27+
namespace wasm {
28+
29+
namespace String {
30+
31+
// Creates a vector of the split parts of a string, by a delimiter.
32+
class Split : public std::vector<std::string> {
33+
public:
34+
Split(const std::string& input, const std::string& delim) {
35+
size_t lastEnd = 0;
36+
while (lastEnd < input.size()) {
37+
auto nextDelim = input.find(delim, lastEnd);
38+
if (nextDelim == std::string::npos) {
39+
nextDelim = input.size();
40+
}
41+
(*this).push_back(input.substr(lastEnd, nextDelim - lastEnd));
42+
lastEnd = nextDelim + delim.size();
43+
}
44+
}
45+
};
46+
47+
// Does a simple wildcard match between a pattern and a value. Currently
48+
// supports a '*' at the end of the pattern.
49+
inline bool wildcardMatch(const std::string& pattern,
50+
const std::string& value) {
51+
for (size_t i = 0; i < pattern.size(); i++) {
52+
if (i >= value.size()) {
53+
return false;
54+
}
55+
if (pattern[i] == '*') {
56+
return true;
57+
}
58+
if (pattern[i] != value[i]) {
59+
return false;
60+
}
61+
}
62+
return value.size() == pattern.size();
63+
}
64+
65+
} // namespace String
66+
67+
} // namespace wasm
68+
69+
#endif // wasm_support_string_h

0 commit comments

Comments
 (0)