Skip to content

Commit 55e38d8

Browse files
authored
Support "async function" in modifyFunction (#16565)
This is required to use `modifyFunction` in PthreadFS.
1 parent 3ffd97a commit 55e38d8

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

src/parseTools.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -931,32 +931,35 @@ function makeRetainedCompilerSettings() {
931931
const WASM_PAGE_SIZE = 65536;
932932

933933
// Receives a function as text, and a function that constructs a modified
934-
// function, to which we pass the parsed-out name, arguments, and body of the
935-
// function. Returns the output of that function.
934+
// function, to which we pass the parsed-out name, arguments, body, and possible
935+
// "async" prefix of the input function. Returns the output of that function.
936936
function modifyFunction(text, func) {
937937
// Match a function with a name.
938-
let match = text.match(/^\s*function\s+([^(]*)?\s*\(([^)]*)\)/);
938+
let match = text.match(/^\s*(async\s+)?function\s+([^(]*)?\s*\(([^)]*)\)/);
939+
let async_;
939940
let names;
940941
let args;
941942
let rest;
942943
if (match) {
943-
name = match[1];
944-
args = match[2];
944+
async_ = match[1] || '';
945+
name = match[2];
946+
args = match[3];
945947
rest = text.substr(match[0].length);
946948
} else {
947949
// Match a function without a name (we could probably use a single regex
948950
// for both, but it would be more complex).
949-
match = text.match(/^\s*function\(([^)]*)\)/);
951+
match = text.match(/^\s*(async\s+)?function\(([^)]*)\)/);
950952
assert(match, 'could not match function ' + text + '.');
951953
name = '';
952-
args = match[1];
954+
async_ = match[1] || '';
955+
args = match[2];
953956
rest = text.substr(match[0].length);
954957
}
955958
const bodyStart = rest.indexOf('{');
956959
assert(bodyStart >= 0);
957960
const bodyEnd = rest.lastIndexOf('}');
958961
assert(bodyEnd > 0);
959-
return func(name, args, rest.substring(bodyStart + 1, bodyEnd));
962+
return func(name, args, rest.substring(bodyStart + 1, bodyEnd), async_);
960963
}
961964

962965
function runOnMainThread(text) {

0 commit comments

Comments
 (0)