mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 11:30:18 +00:00
backup
This commit is contained in:
parent
1bd7ad0189
commit
dc4a21a594
@ -55,3 +55,9 @@
|
|||||||
#else
|
#else
|
||||||
#define PK_IS_DESKTOP_PLATFORM 0
|
#define PK_IS_DESKTOP_PLATFORM 0
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if defined(__GNUC__) || defined(__clang__)
|
||||||
|
#define PK_DEPRECATED [[deprecated]]
|
||||||
|
#else
|
||||||
|
#define PK_DEPRECATED
|
||||||
|
#endif
|
@ -36,7 +36,4 @@ void TypeList__dtor(TypeList* self);
|
|||||||
py_TypeInfo* TypeList__get(TypeList* self, py_Type index);
|
py_TypeInfo* TypeList__get(TypeList* self, py_Type index);
|
||||||
py_TypeInfo* TypeList__emplace(TypeList* self);
|
py_TypeInfo* TypeList__emplace(TypeList* self);
|
||||||
void TypeList__apply(TypeList* self, void (*f)(py_TypeInfo*, void*), void* ctx);
|
void TypeList__apply(TypeList* self, void (*f)(py_TypeInfo*, void*), void* ctx);
|
||||||
py_TValue* TypeList__magic(py_TypeInfo* self, unsigned index);
|
|
||||||
py_TValue* TypeList__magic_readonly(py_TypeInfo* self, unsigned index);
|
|
||||||
|
|
||||||
#define TypeList__magic_common(ti, index) ((ti)->magic_0 + ((index)-PK_MAGIC_SLOTS_UNCOMMON_LENGTH))
|
|
||||||
|
@ -96,7 +96,7 @@ void CodeObject__gc_mark(const CodeObject* self, c11_vector* p_stack);
|
|||||||
|
|
||||||
typedef struct FuncDeclKwArg {
|
typedef struct FuncDeclKwArg {
|
||||||
int index; // index in co->varnames
|
int index; // index in co->varnames
|
||||||
uint16_t key; // name of this argument
|
py_Name key; // name of this argument
|
||||||
py_TValue value; // default value
|
py_TValue value; // default value
|
||||||
} FuncDeclKwArg;
|
} FuncDeclKwArg;
|
||||||
|
|
||||||
|
@ -37,6 +37,7 @@ typedef struct NameDict {
|
|||||||
} NameDict;
|
} NameDict;
|
||||||
|
|
||||||
NameDict* NameDict__new(float load_factor);
|
NameDict* NameDict__new(float load_factor);
|
||||||
|
void NameDict__delete(NameDict* self);
|
||||||
void NameDict__ctor(NameDict* self, float load_factor);
|
void NameDict__ctor(NameDict* self, float load_factor);
|
||||||
void NameDict__dtor(NameDict* self);
|
void NameDict__dtor(NameDict* self);
|
||||||
py_TValue* NameDict__try_get(NameDict* self, py_Name key);
|
py_TValue* NameDict__try_get(NameDict* self, py_Name key);
|
||||||
|
@ -242,8 +242,8 @@ PK_API py_GlobalRef py_name2ref(py_Name);
|
|||||||
PK_API py_Name py_namev(c11_sv);
|
PK_API py_Name py_namev(c11_sv);
|
||||||
/// Convert a name to a `c11_sv`.
|
/// Convert a name to a `c11_sv`.
|
||||||
PK_API c11_sv py_name2sv(py_Name);
|
PK_API c11_sv py_name2sv(py_Name);
|
||||||
|
/// Check if the name matches the two underscores pattern. e.g. `__init__`, `__str__`, etc.
|
||||||
#define py_ismagicname(name) (name <= __missing__)
|
PK_API bool py_ismagicname(py_Name);
|
||||||
|
|
||||||
/************* Meta Operations *************/
|
/************* Meta Operations *************/
|
||||||
|
|
||||||
@ -318,7 +318,7 @@ PK_API bool py_issubclass(py_Type derived, py_Type base);
|
|||||||
|
|
||||||
/// Get the magic method from the given type only.
|
/// Get the magic method from the given type only.
|
||||||
/// The returned reference is always valid. However, its value may be `nil`.
|
/// The returned reference is always valid. However, its value may be `nil`.
|
||||||
PK_API py_GlobalRef py_tpgetmagic(py_Type type, py_Name name);
|
PK_API PK_DEPRECATED py_GlobalRef py_tpgetmagic(py_Type type, py_Name name);
|
||||||
/// Search the magic method from the given type to the base type.
|
/// Search the magic method from the given type to the base type.
|
||||||
/// Return `NULL` if not found.
|
/// Return `NULL` if not found.
|
||||||
PK_API py_GlobalRef py_tpfindmagic(py_Type, py_Name name);
|
PK_API py_GlobalRef py_tpfindmagic(py_Type, py_Name name);
|
||||||
@ -438,8 +438,8 @@ PK_API void py_bindfunc(py_Ref obj, const char* name, py_CFunction f);
|
|||||||
/// @param setter setter function. Use `NULL` if not needed.
|
/// @param setter setter function. Use `NULL` if not needed.
|
||||||
PK_API void
|
PK_API void
|
||||||
py_bindproperty(py_Type type, const char* name, py_CFunction getter, py_CFunction setter);
|
py_bindproperty(py_Type type, const char* name, py_CFunction getter, py_CFunction setter);
|
||||||
|
/// Bind a magic method to type.
|
||||||
#define py_bindmagic(type, __magic__, f) py_newnativefunc(py_tpgetmagic((type), __magic__), (f))
|
PK_API void py_bindmagic(py_Type type, py_Name name, py_CFunction f);
|
||||||
|
|
||||||
#define PY_CHECK_ARGC(n) \
|
#define PY_CHECK_ARGC(n) \
|
||||||
if(argc != n) return TypeError("expected %d arguments, got %d", n, argc)
|
if(argc != n) return TypeError("expected %d arguments, got %d", n, argc)
|
||||||
@ -737,11 +737,11 @@ enum py_PredefinedType {
|
|||||||
tp_locals,
|
tp_locals,
|
||||||
tp_code,
|
tp_code,
|
||||||
tp_dict,
|
tp_dict,
|
||||||
tp_dict_iterator, // 1 slot
|
tp_dict_iterator, // 1 slot
|
||||||
tp_property, // 2 slots (getter + setter)
|
tp_property, // 2 slots (getter + setter)
|
||||||
tp_star_wrapper, // 1 slot + int level
|
tp_star_wrapper, // 1 slot + int level
|
||||||
tp_staticmethod, // 1 slot
|
tp_staticmethod, // 1 slot
|
||||||
tp_classmethod, // 1 slot
|
tp_classmethod, // 1 slot
|
||||||
tp_NoneType,
|
tp_NoneType,
|
||||||
tp_NotImplementedType,
|
tp_NotImplementedType,
|
||||||
tp_ellipsis,
|
tp_ellipsis,
|
||||||
|
@ -44,7 +44,8 @@ void ManagedHeap__collect_if_needed(ManagedHeap* self) {
|
|||||||
const int lower = PK_GC_MIN_THRESHOLD / 2;
|
const int lower = PK_GC_MIN_THRESHOLD / 2;
|
||||||
float free_ratio = (float)avg_freed / self->gc_threshold;
|
float free_ratio = (float)avg_freed / self->gc_threshold;
|
||||||
int new_threshold = self->gc_threshold * (1 / free_ratio);
|
int new_threshold = self->gc_threshold * (1 / free_ratio);
|
||||||
// printf("gc_threshold=%d, avg_freed=%d, new_threshold=%d\n", self->gc_threshold, avg_freed, new_threshold);
|
// printf("gc_threshold=%d, avg_freed=%d, new_threshold=%d\n", self->gc_threshold, avg_freed,
|
||||||
|
// new_threshold);
|
||||||
self->gc_threshold = c11__min(c11__max(new_threshold, lower), upper);
|
self->gc_threshold = c11__min(c11__max(new_threshold, lower), upper);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -98,7 +99,9 @@ PyObject* ManagedHeap__gcnew(ManagedHeap* self, py_Type type, int slots, int uds
|
|||||||
if(slots >= 0) {
|
if(slots >= 0) {
|
||||||
memset(obj->flex, 0, slots * sizeof(py_TValue));
|
memset(obj->flex, 0, slots * sizeof(py_TValue));
|
||||||
} else {
|
} else {
|
||||||
NameDict__ctor((void*)obj->flex);
|
float load_factor = (type == tp_type || type == tp_module) ? PK_TYPE_ATTR_LOAD_FACTOR
|
||||||
|
: PK_INST_ATTR_LOAD_FACTOR;
|
||||||
|
NameDict__ctor((void*)obj->flex, load_factor);
|
||||||
}
|
}
|
||||||
|
|
||||||
self->gc_counter++;
|
self->gc_counter++;
|
||||||
|
@ -12,7 +12,7 @@ void TypeList__ctor(TypeList* self) {
|
|||||||
void TypeList__dtor(TypeList* self) {
|
void TypeList__dtor(TypeList* self) {
|
||||||
for(py_Type t = 0; t < self->length; t++) {
|
for(py_Type t = 0; t < self->length; t++) {
|
||||||
py_TypeInfo* info = TypeList__get(self, t);
|
py_TypeInfo* info = TypeList__get(self, t);
|
||||||
if(info->magic_1) PK_FREE(info->magic_1);
|
(void)info;
|
||||||
}
|
}
|
||||||
for(int i = 0; i < PK_MAX_CHUNK_LENGTH; i++) {
|
for(int i = 0; i < PK_MAX_CHUNK_LENGTH; i++) {
|
||||||
if(self->chunks[i]) PK_FREE(self->chunks[i]);
|
if(self->chunks[i]) PK_FREE(self->chunks[i]);
|
||||||
@ -47,28 +47,5 @@ void TypeList__apply(TypeList* self, void (*f)(py_TypeInfo*, void*), void* ctx)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
py_TValue* TypeList__magic(py_TypeInfo* self, unsigned index) {
|
|
||||||
if(index > __xor__) {
|
|
||||||
// common magic slots
|
|
||||||
return TypeList__magic_common(self, index);
|
|
||||||
}
|
|
||||||
// uncommon magic slots
|
|
||||||
if(self->magic_1 == NULL) {
|
|
||||||
self->magic_1 = PK_MALLOC(sizeof(py_TValue) * PK_MAGIC_SLOTS_UNCOMMON_LENGTH);
|
|
||||||
memset(self->magic_1, 0, sizeof(py_TValue) * PK_MAGIC_SLOTS_UNCOMMON_LENGTH);
|
|
||||||
}
|
|
||||||
return self->magic_1 + index;
|
|
||||||
}
|
|
||||||
|
|
||||||
py_TValue* TypeList__magic_readonly(py_TypeInfo* self, unsigned index) {
|
|
||||||
if(index > __xor__) {
|
|
||||||
// common magic slots
|
|
||||||
return TypeList__magic_common(self, index);
|
|
||||||
}
|
|
||||||
// uncommon magic slots
|
|
||||||
if(self->magic_1 == NULL) return py_NIL();
|
|
||||||
return self->magic_1 + index;
|
|
||||||
}
|
|
||||||
|
|
||||||
#undef CHUNK_SIZE
|
#undef CHUNK_SIZE
|
||||||
#undef LOG2_CHUNK_SIZE
|
#undef LOG2_CHUNK_SIZE
|
@ -132,6 +132,11 @@ NameDict* NameDict__new(float load_factor) {
|
|||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void NameDict__delete(NameDict* self) {
|
||||||
|
NameDict__dtor(self);
|
||||||
|
PK_FREE(self);
|
||||||
|
}
|
||||||
|
|
||||||
void NameDict__ctor(NameDict* self, float load_factor) {
|
void NameDict__ctor(NameDict* self, float load_factor) {
|
||||||
assert(load_factor > 0.0f && load_factor < 1.0f);
|
assert(load_factor > 0.0f && load_factor < 1.0f);
|
||||||
self->length = 0;
|
self->length = 0;
|
||||||
|
@ -229,14 +229,7 @@ bool pk_loadmethod(py_StackRef self, py_Name name) {
|
|||||||
|
|
||||||
py_Ref py_tpfindmagic(py_Type t, py_Name name) {
|
py_Ref py_tpfindmagic(py_Type t, py_Name name) {
|
||||||
assert(py_ismagicname(name));
|
assert(py_ismagicname(name));
|
||||||
py_TypeInfo* ti = pk__type_info(t);
|
return py_tpfindname(t, name);
|
||||||
do {
|
|
||||||
py_Ref f = TypeList__magic_readonly(ti, name);
|
|
||||||
assert(f != NULL);
|
|
||||||
if(!py_isnil(f)) return f;
|
|
||||||
ti = ti->base_ti;
|
|
||||||
} while(ti);
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
py_Ref py_tpfindname(py_Type t, py_Name name) {
|
py_Ref py_tpfindname(py_Type t, py_Name name) {
|
||||||
@ -249,10 +242,11 @@ py_Ref py_tpfindname(py_Type t, py_Name name) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
py_Ref py_tpgetmagic(py_Type type, py_Name name) {
|
PK_DEPRECATED py_Ref py_tpgetmagic(py_Type type, py_Name name) {
|
||||||
assert(py_ismagicname(name));
|
assert(py_ismagicname(name));
|
||||||
py_TypeInfo* ti = pk__type_info(type);
|
py_TypeInfo* ti = pk__type_info(type);
|
||||||
return TypeList__magic(ti, name);
|
py_Ref retval = py_getdict(&ti->self, name);
|
||||||
|
return retval != NULL ? retval : py_NIL();
|
||||||
}
|
}
|
||||||
|
|
||||||
py_Ref py_tpobject(py_Type type) {
|
py_Ref py_tpobject(py_Type type) {
|
||||||
|
@ -54,11 +54,10 @@ int py_bool(py_Ref val) {
|
|||||||
bool py_hash(py_Ref val, int64_t* out) {
|
bool py_hash(py_Ref val, int64_t* out) {
|
||||||
py_TypeInfo* ti = pk__type_info(val->type);
|
py_TypeInfo* ti = pk__type_info(val->type);
|
||||||
do {
|
do {
|
||||||
py_Ref slot_hash = TypeList__magic_common(ti, __hash__);
|
py_Ref slot_hash = py_getdict(&ti->self, __hash__);
|
||||||
if(py_isnone(slot_hash)) break;
|
if(!slot_hash || py_isnone(slot_hash)) break;
|
||||||
py_Ref slot_eq = TypeList__magic_common(ti, __eq__);
|
py_Ref slot_eq = py_getdict(&ti->self, __eq__);
|
||||||
if(!py_isnil(slot_eq)) {
|
if(slot_eq) {
|
||||||
if(py_isnil(slot_hash)) break;
|
|
||||||
if(!py_call(slot_hash, 1, val)) return false;
|
if(!py_call(slot_hash, 1, val)) return false;
|
||||||
if(!py_checkint(py_retval())) return false;
|
if(!py_checkint(py_retval())) return false;
|
||||||
*out = py_toint(py_retval());
|
*out = py_toint(py_retval());
|
||||||
|
@ -83,14 +83,22 @@ void py_bindproperty(py_Type type, const char* name, py_CFunction getter, py_CFu
|
|||||||
py_setdict(py_tpobject(type), py_name(name), &tmp);
|
py_setdict(py_tpobject(type), py_name(name), &tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void py_bindmagic(py_Type type, py_Name name, py_CFunction f) {
|
||||||
|
py_Ref tmp = py_emplacedict(py_tpobject(type), name);
|
||||||
|
py_newnativefunc(tmp, f);
|
||||||
|
}
|
||||||
|
|
||||||
void py_bind(py_Ref obj, const char* sig, py_CFunction f) {
|
void py_bind(py_Ref obj, const char* sig, py_CFunction f) {
|
||||||
py_TValue tmp;
|
py_TValue tmp;
|
||||||
py_Name name = py_newfunction(&tmp, sig, f, NULL, 0);
|
py_Name name = py_newfunction(&tmp, sig, f, NULL, 0);
|
||||||
py_setdict(obj, name, &tmp);
|
py_setdict(obj, name, &tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
py_Name
|
py_Name py_newfunction(py_OutRef out,
|
||||||
py_newfunction(py_OutRef out, const char* sig, py_CFunction f, const char* docstring, int slots) {
|
const char* sig,
|
||||||
|
py_CFunction f,
|
||||||
|
const char* docstring,
|
||||||
|
int slots) {
|
||||||
char buffer[256];
|
char buffer[256];
|
||||||
snprintf(buffer, sizeof(buffer), "def %s: pass", sig);
|
snprintf(buffer, sizeof(buffer), "def %s: pass", sig);
|
||||||
// fn(a, b, *c, d=1) -> None
|
// fn(a, b, *c, d=1) -> None
|
||||||
|
Loading…
x
Reference in New Issue
Block a user