This commit is contained in:
blueloveTH 2023-12-21 00:09:44 +08:00
parent 8da205f4f9
commit d1080aab1f
3 changed files with 9 additions and 6 deletions

View File

@ -138,10 +138,13 @@ struct FuncDecl {
Str docstring; // docstring of this function Str docstring; // docstring of this function
bool is_simple; bool is_simple;
int keyword_to_index(StrName key) const{ NameDictInt kw_to_index;
for(const KwArg& item: kwargs) if(item.key == key) return item.index;
return -1; void add_kwarg(int index, StrName key, PyObject* value){
kw_to_index.set(key, index);
kwargs.push_back({index, key, value});
} }
void _gc_mark() const; void _gc_mark() const;
}; };

View File

@ -1037,7 +1037,7 @@ __EAT_DOTS_END:
if(value == nullptr){ if(value == nullptr){
SyntaxError(Str("default argument must be a literal")); SyntaxError(Str("default argument must be a literal"));
} }
decl->kwargs.push_back(FuncDecl::KwArg{index, name, value}); decl->add_kwarg(index, name, value);
} break; } break;
case 3: case 3:
decl->starred_kwarg = index; decl->starred_kwarg = index;

View File

@ -844,9 +844,9 @@ void VM::_prepare_py_call(PyObject** buffer, ArgsView args, ArgsView kwargs, con
for(int j=0; j<kwargs.size(); j+=2){ for(int j=0; j<kwargs.size(); j+=2){
StrName key(CAST(int, kwargs[j])); StrName key(CAST(int, kwargs[j]));
int index = decl->keyword_to_index(key); int index = decl->kw_to_index.try_get_likely_found(key);
// if key is an explicit key, set as local variable // if key is an explicit key, set as local variable
if(index != -1){ if(index >= 0){
buffer[index] = kwargs[j+1]; buffer[index] = kwargs[j+1];
}else{ }else{
// otherwise, set as **kwargs if possible // otherwise, set as **kwargs if possible