leak some names in global

This commit is contained in:
blueloveTH 2024-06-16 01:21:12 +08:00
parent e7fe38cdf7
commit a5cf3aa426
4 changed files with 188 additions and 137 deletions

View File

@ -256,7 +256,7 @@ struct StrName {
StrName() : index(0) {} StrName() : index(0) {}
explicit StrName(uint16_t index) : index(index) {} StrName(uint16_t index) : index(index) {}
StrName(const char* s) : index(get(s).index) {} StrName(const char* s) : index(get(s).index) {}
@ -275,18 +275,16 @@ struct StrName {
bool operator> (const StrName& other) const noexcept { return sv() > other.sv(); } bool operator> (const StrName& other) const noexcept { return sv() > other.sv(); }
inline static StrName get(std::string_view s){ inline static StrName get(std::string_view s){
uint16_t index = pkpy_StrName__map({s.data(), (int)s.size()}); uint16_t index = pkpy_StrName__map2({s.data(), (int)s.size()});
return StrName(index); return StrName(index);
} }
std::string_view sv() const{ std::string_view sv() const{
c11_string s = pkpy_StrName__rmap(index); return pkpy_StrName__rmap(index);
return std::string_view(s.data, s.size);
} }
const char* c_str() const{ const char* c_str() const{
c11_string s = pkpy_StrName__rmap(index); return pkpy_StrName__rmap(index);
return s.data;
} }
}; };
@ -401,63 +399,6 @@ struct CString {
operator const char* () const { return ptr; } operator const char* () const { return ptr; }
}; };
// unary operators
extern const StrName __repr__;
extern const StrName __str__;
extern const StrName __hash__;
extern const StrName __len__;
extern const StrName __iter__;
extern const StrName __next__;
extern const StrName __neg__;
// logical operators
extern const StrName __eq__;
extern const StrName __lt__;
extern const StrName __le__;
extern const StrName __gt__;
extern const StrName __ge__;
extern const StrName __contains__;
// binary operators
extern const StrName __add__;
extern const StrName __radd__;
extern const StrName __sub__;
extern const StrName __rsub__;
extern const StrName __mul__;
extern const StrName __rmul__;
extern const StrName __truediv__;
extern const StrName __floordiv__;
extern const StrName __mod__;
extern const StrName __pow__;
extern const StrName __matmul__;
extern const StrName __lshift__;
extern const StrName __rshift__;
extern const StrName __and__;
extern const StrName __or__;
extern const StrName __xor__;
extern const StrName __invert__;
// indexer
extern const StrName __getitem__;
extern const StrName __setitem__;
extern const StrName __delitem__;
// specials
extern const StrName __new__;
extern const StrName __init__;
extern const StrName __call__;
extern const StrName __divmod__;
extern const StrName __enter__;
extern const StrName __exit__;
extern const StrName __name__;
extern const StrName __all__;
extern const StrName __package__;
extern const StrName __path__;
extern const StrName __class__;
extern const StrName __missing__;
extern const StrName pk_id_add;
extern const StrName pk_id_set;
extern const StrName pk_id_long;
extern const StrName pk_id_complex;
#define DEF_SNAME(name) const static StrName name(#name) #define DEF_SNAME(name) const static StrName name(#name)
} // namespace pkpy } // namespace pkpy

View File

@ -7,12 +7,71 @@
extern "C" { extern "C" {
#endif #endif
uint16_t pkpy_StrName__map(c11_string name); uint16_t pkpy_StrName__map(const char*);
c11_string pkpy_StrName__rmap(uint16_t index); uint16_t pkpy_StrName__map2(c11_string);
const char* pkpy_StrName__rmap(uint16_t index);
void pkpy_StrName__initialize(); void pkpy_StrName__initialize();
void pkpy_StrName__finalize(); void pkpy_StrName__finalize();
// unary operators
extern uint16_t __repr__;
extern uint16_t __str__;
extern uint16_t __hash__;
extern uint16_t __len__;
extern uint16_t __iter__;
extern uint16_t __next__;
extern uint16_t __neg__;
// logical operators
extern uint16_t __eq__;
extern uint16_t __lt__;
extern uint16_t __le__;
extern uint16_t __gt__;
extern uint16_t __ge__;
extern uint16_t __contains__;
// binary operators
extern uint16_t __add__;
extern uint16_t __radd__;
extern uint16_t __sub__;
extern uint16_t __rsub__;
extern uint16_t __mul__;
extern uint16_t __rmul__;
extern uint16_t __truediv__;
extern uint16_t __floordiv__;
extern uint16_t __mod__;
extern uint16_t __pow__;
extern uint16_t __matmul__;
extern uint16_t __lshift__;
extern uint16_t __rshift__;
extern uint16_t __and__;
extern uint16_t __or__;
extern uint16_t __xor__;
extern uint16_t __invert__;
// indexer
extern uint16_t __getitem__;
extern uint16_t __setitem__;
extern uint16_t __delitem__;
// specials
extern uint16_t __new__;
extern uint16_t __init__;
extern uint16_t __call__;
extern uint16_t __divmod__;
extern uint16_t __enter__;
extern uint16_t __exit__;
extern uint16_t __name__;
extern uint16_t __all__;
extern uint16_t __package__;
extern uint16_t __path__;
extern uint16_t __class__;
extern uint16_t __missing__;
extern uint16_t pk_id_add;
extern uint16_t pk_id_set;
extern uint16_t pk_id_long;
extern uint16_t pk_id_complex;
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -1,68 +0,0 @@
#include "pocketpy/common/str.hpp"
#include <cassert>
#include <ostream>
#include <algorithm>
#include <cmath>
#include <map>
namespace pkpy {
// unary operators
const StrName __repr__ = StrName::get("__repr__");
const StrName __str__ = StrName::get("__str__");
const StrName __hash__ = StrName::get("__hash__");
const StrName __len__ = StrName::get("__len__");
const StrName __iter__ = StrName::get("__iter__");
const StrName __next__ = StrName::get("__next__");
const StrName __neg__ = StrName::get("__neg__");
// logical operators
const StrName __eq__ = StrName::get("__eq__");
const StrName __lt__ = StrName::get("__lt__");
const StrName __le__ = StrName::get("__le__");
const StrName __gt__ = StrName::get("__gt__");
const StrName __ge__ = StrName::get("__ge__");
const StrName __contains__ = StrName::get("__contains__");
// binary operators
const StrName __add__ = StrName::get("__add__");
const StrName __radd__ = StrName::get("__radd__");
const StrName __sub__ = StrName::get("__sub__");
const StrName __rsub__ = StrName::get("__rsub__");
const StrName __mul__ = StrName::get("__mul__");
const StrName __rmul__ = StrName::get("__rmul__");
const StrName __truediv__ = StrName::get("__truediv__");
const StrName __floordiv__ = StrName::get("__floordiv__");
const StrName __mod__ = StrName::get("__mod__");
const StrName __pow__ = StrName::get("__pow__");
const StrName __matmul__ = StrName::get("__matmul__");
const StrName __lshift__ = StrName::get("__lshift__");
const StrName __rshift__ = StrName::get("__rshift__");
const StrName __and__ = StrName::get("__and__");
const StrName __or__ = StrName::get("__or__");
const StrName __xor__ = StrName::get("__xor__");
const StrName __invert__ = StrName::get("__invert__");
// indexer
const StrName __getitem__ = StrName::get("__getitem__");
const StrName __setitem__ = StrName::get("__setitem__");
const StrName __delitem__ = StrName::get("__delitem__");
// specials
const StrName __new__ = StrName::get("__new__");
const StrName __init__ = StrName::get("__init__");
const StrName __call__ = StrName::get("__call__");
const StrName __divmod__ = StrName::get("__divmod__");
const StrName __enter__ = StrName::get("__enter__");
const StrName __exit__ = StrName::get("__exit__");
const StrName __name__ = StrName::get("__name__");
const StrName __all__ = StrName::get("__all__");
const StrName __package__ = StrName::get("__package__");
const StrName __path__ = StrName::get("__path__");
const StrName __class__ = StrName::get("__class__");
const StrName __missing__ = StrName::get("__missing__");
const StrName pk_id_add = StrName::get("add");
const StrName pk_id_set = StrName::get("set");
const StrName pk_id_long = StrName::get("long");
const StrName pk_id_complex = StrName::get("complex");
} // namespace pkpy

View File

@ -18,6 +18,63 @@ void pkpy_StrName__initialize(){
} }
c11_vector__ctor(&_r_interned, sizeof(c11_string)); c11_vector__ctor(&_r_interned, sizeof(c11_string));
_initialized = true; _initialized = true;
// unary operators
__repr__ = pkpy_StrName__map("__repr__");
__str__ = pkpy_StrName__map("__str__");
__hash__ = pkpy_StrName__map("__hash__");
__len__ = pkpy_StrName__map("__len__");
__iter__ = pkpy_StrName__map("__iter__");
__next__ = pkpy_StrName__map("__next__");
__neg__ = pkpy_StrName__map("__neg__");
// logical operators
__eq__ = pkpy_StrName__map("__eq__");
__lt__ = pkpy_StrName__map("__lt__");
__le__ = pkpy_StrName__map("__le__");
__gt__ = pkpy_StrName__map("__gt__");
__ge__ = pkpy_StrName__map("__ge__");
__contains__ = pkpy_StrName__map("__contains__");
// binary operators
__add__ = pkpy_StrName__map("__add__");
__radd__ = pkpy_StrName__map("__radd__");
__sub__ = pkpy_StrName__map("__sub__");
__rsub__ = pkpy_StrName__map("__rsub__");
__mul__ = pkpy_StrName__map("__mul__");
__rmul__ = pkpy_StrName__map("__rmul__");
__truediv__ = pkpy_StrName__map("__truediv__");
__floordiv__ = pkpy_StrName__map("__floordiv__");
__mod__ = pkpy_StrName__map("__mod__");
__pow__ = pkpy_StrName__map("__pow__");
__matmul__ = pkpy_StrName__map("__matmul__");
__lshift__ = pkpy_StrName__map("__lshift__");
__rshift__ = pkpy_StrName__map("__rshift__");
__and__ = pkpy_StrName__map("__and__");
__or__ = pkpy_StrName__map("__or__");
__xor__ = pkpy_StrName__map("__xor__");
__invert__ = pkpy_StrName__map("__invert__");
// indexer
__getitem__ = pkpy_StrName__map("__getitem__");
__setitem__ = pkpy_StrName__map("__setitem__");
__delitem__ = pkpy_StrName__map("__delitem__");
// specials
__new__ = pkpy_StrName__map("__new__");
__init__ = pkpy_StrName__map("__init__");
__call__ = pkpy_StrName__map("__call__");
__divmod__ = pkpy_StrName__map("__divmod__");
__enter__ = pkpy_StrName__map("__enter__");
__exit__ = pkpy_StrName__map("__exit__");
__name__ = pkpy_StrName__map("__name__");
__all__ = pkpy_StrName__map("__all__");
__package__ = pkpy_StrName__map("__package__");
__path__ = pkpy_StrName__map("__path__");
__class__ = pkpy_StrName__map("__class__");
__missing__ = pkpy_StrName__map("__missing__");
pk_id_add = pkpy_StrName__map("add");
pk_id_set = pkpy_StrName__map("set");
pk_id_long = pkpy_StrName__map("long");
pk_id_complex = pkpy_StrName__map("complex");
} }
void pkpy_StrName__finalize(){ void pkpy_StrName__finalize(){
@ -26,7 +83,11 @@ void pkpy_StrName__finalize(){
c11_vector__dtor(&_r_interned); c11_vector__dtor(&_r_interned);
} }
uint16_t pkpy_StrName__map(c11_string name){ uint16_t pkpy_StrName__map(const char* name){
return pkpy_StrName__map2((c11_string){name, strlen(name)});
}
uint16_t pkpy_StrName__map2(c11_string name){
// TODO: PK_GLOBAL_SCOPE_LOCK() // TODO: PK_GLOBAL_SCOPE_LOCK()
if(!_initialized){ if(!_initialized){
pkpy_StrName__initialize(); // lazy init pkpy_StrName__initialize(); // lazy init
@ -49,9 +110,67 @@ uint16_t pkpy_StrName__map(c11_string name){
return index; return index;
} }
c11_string pkpy_StrName__rmap(uint16_t index){ const char* pkpy_StrName__rmap(uint16_t index){
assert(_initialized); assert(_initialized);
assert(index > 0 && index <= _interned.count); assert(index > 0 && index <= _interned.count);
char* p = c11__getitem(char*, &_r_interned, index - 1); return c11__getitem(char*, &_r_interned, index - 1);
return (c11_string){p, strlen(p)};
} }
// unary operators
uint16_t __repr__;
uint16_t __str__;
uint16_t __hash__;
uint16_t __len__;
uint16_t __iter__;
uint16_t __next__;
uint16_t __neg__;
// logical operators
uint16_t __eq__;
uint16_t __lt__;
uint16_t __le__;
uint16_t __gt__;
uint16_t __ge__;
uint16_t __contains__;
// binary operators
uint16_t __add__;
uint16_t __radd__;
uint16_t __sub__;
uint16_t __rsub__;
uint16_t __mul__;
uint16_t __rmul__;
uint16_t __truediv__;
uint16_t __floordiv__;
uint16_t __mod__;
uint16_t __pow__;
uint16_t __matmul__;
uint16_t __lshift__;
uint16_t __rshift__;
uint16_t __and__;
uint16_t __or__;
uint16_t __xor__;
uint16_t __invert__;
// indexer
uint16_t __getitem__;
uint16_t __setitem__;
uint16_t __delitem__;
// specials
uint16_t __new__;
uint16_t __init__;
uint16_t __call__;
uint16_t __divmod__;
uint16_t __enter__;
uint16_t __exit__;
uint16_t __name__;
uint16_t __all__;
uint16_t __package__;
uint16_t __path__;
uint16_t __class__;
uint16_t __missing__;
uint16_t pk_id_add;
uint16_t pk_id_set;
uint16_t pk_id_long;
uint16_t pk_id_complex;