Skip to content

Commit ffadecc

Browse files
committed
find_var_htab: skip entries with scope_level != 0
When a `var` shadows a block-scoped `let` of the same name, find_var_htab returned the block-scoped entry. find_var rejected it (scope_level != 0) and fell through to an O(n) linear scan. Add scope_level == 0 check to the htab probe loop so non-scope-0 entries are skipped. This lets find_var treat htab miss as definitive.
1 parent a653771 commit ffadecc

1 file changed

Lines changed: 4 additions & 5 deletions

File tree

quickjs.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23378,7 +23378,8 @@ static int find_var_htab(JSFunctionDef *fd, JSAtom var_name)
2337823378
p = &fd->vars_htab[i & m];
2337923379
if (*p == UINT32_MAX)
2338023380
return -1;
23381-
if (fd->vars[*p].var_name == var_name)
23381+
if (fd->vars[*p].var_name == var_name &&
23382+
fd->vars[*p].scope_level == 0)
2338223383
return *p;
2338323384
i += j;
2338423385
j += 1; // quadratic probing
@@ -23405,11 +23406,9 @@ static int find_var(JSContext *ctx, JSFunctionDef *fd, JSAtom name)
2340523406

2340623407
if (fd->vars_htab) {
2340723408
i = find_var_htab(fd, name);
23408-
if (i == -1)
23409-
goto not_found;
23410-
vd = &fd->vars[i];
23411-
if (vd->scope_level == 0)
23409+
if (i >= 0)
2341223410
return i;
23411+
goto not_found;
2341323412
}
2341423413
for(i = fd->var_count; i-- > 0;) {
2341523414
vd = &fd->vars[i];

0 commit comments

Comments
 (0)