|
10 | 10 | #include "lprefix.h" |
11 | 11 |
|
12 | 12 |
|
| 13 | +#include <algorithm> |
13 | 14 | #include <cstdarg> |
14 | 15 | #include <cstddef> |
15 | 16 | #include <cstring> |
@@ -66,14 +67,18 @@ static int getbaseline (const Proto *f, int pc, int *basepc) { |
66 | 67 | return f->getLineDefined(); |
67 | 68 | } |
68 | 69 | else { |
69 | | - int i = pc / MAXIWTHABS - 1; /* get an estimate */ |
70 | | - /* estimate must be a lower bound of the correct base */ |
71 | | - lua_assert(i < 0 || |
72 | | - (i < f->getAbsLineInfoSize() && f->getAbsLineInfo()[i].getPC() <= pc)); |
73 | | - while (i + 1 < f->getAbsLineInfoSize() && pc >= f->getAbsLineInfo()[i + 1].getPC()) |
74 | | - i++; /* low estimate; adjust it */ |
75 | | - *basepc = f->getAbsLineInfo()[i].getPC(); |
76 | | - return f->getAbsLineInfo()[i].getLine(); |
| 70 | + /* Binary search for the last AbsLineInfo with PC <= pc */ |
| 71 | + const AbsLineInfo* absLineInfo = f->getAbsLineInfo(); |
| 72 | + int size = f->getAbsLineInfoSize(); |
| 73 | + /* std::upper_bound finds first element with PC > pc, so we go back one */ |
| 74 | + auto it = std::upper_bound(absLineInfo, absLineInfo + size, pc, |
| 75 | + [](int target_pc, const AbsLineInfo& info) { |
| 76 | + return target_pc < info.getPC(); |
| 77 | + }); |
| 78 | + lua_assert(it != absLineInfo); /* we know there's at least one element with PC <= pc */ |
| 79 | + --it; /* go back to last element with PC <= pc */ |
| 80 | + *basepc = it->getPC(); |
| 81 | + return it->getLine(); |
77 | 82 | } |
78 | 83 | } |
79 | 84 |
|
@@ -690,13 +695,12 @@ static const char *funcnamefromcall (lua_State *L, CallInfo *ci, |
690 | 695 | ** region boundaries (undefined behavior in ISO C). |
691 | 696 | */ |
692 | 697 | static int instack (CallInfo *ci, const TValue *o) { |
693 | | - int pos; |
694 | 698 | StkId base = ci->funcRef().p + 1; |
695 | | - for (pos = 0; base + pos < ci->topRef().p; pos++) { |
696 | | - if (o == s2v(base + pos)) |
697 | | - return pos; |
698 | | - } |
699 | | - return -1; /* not found */ |
| 699 | + StkId end = ci->topRef().p; |
| 700 | + auto it = std::find_if(base, end, [o](const StackValue& sv) { |
| 701 | + return o == s2v(&sv); |
| 702 | + }); |
| 703 | + return (it != end) ? static_cast<int>(it - base) : -1; /* return position or -1 if not found */ |
700 | 704 | } |
701 | 705 |
|
702 | 706 |
|
|
0 commit comments