fix path_len

This commit is contained in:
blueloveTH 2025-06-14 19:26:14 +08:00
parent f9debd804e
commit 04a44a4aa6
2 changed files with 9 additions and 7 deletions

View File

@ -72,14 +72,12 @@ static int BinTree__cmp_cstr(void* lhs, void* rhs) {
return strcmp(l, r);
}
static int BinTree__cmp_voidp(void* lhs, void* rhs) {
return lhs < rhs ? -1 : (lhs > rhs ? 1 : 0);
}
static int BinTree__cmp_voidp(void* lhs, void* rhs) { return lhs < rhs ? -1 : (lhs > rhs ? 1 : 0); }
void VM__ctor(VM* self) {
self->top_frame = NULL;
static const BinTreeConfig modules_config = {
const static BinTreeConfig modules_config = {
.f_cmp = BinTree__cmp_cstr,
.need_free_key = true,
};
@ -114,7 +112,7 @@ void VM__ctor(VM* self) {
ManagedHeap__ctor(&self->heap);
ValueStack__ctor(&self->stack);
static const BinTreeConfig cached_names_config = {
const static BinTreeConfig cached_names_config = {
.f_cmp = BinTree__cmp_voidp,
.need_free_key = false,
};
@ -417,6 +415,7 @@ py_Type pk_newtype(const char* name,
}
py_Type py_newtype(const char* name, py_Type base, const py_GlobalRef module, void (*dtor)(void*)) {
if(strlen(name) == 0) c11__abort("type name cannot be empty");
py_Type type = pk_newtype(name, base, module, dtor, false, false);
if(module) py_setdict(module, py_name(name), py_tpobject(type));
return type;

View File

@ -24,7 +24,10 @@ void py_setglobal(py_Name name, py_Ref val) { py_setdict(&pk_current_vm->main, n
py_Ref py_newmodule(const char* path) {
ManagedHeap* heap = &pk_current_vm->heap;
if(strlen(path) > PK_MAX_MODULE_PATH_LEN) c11__abort("module path too long: %s", path);
int path_len = strlen(path);
if(path_len > PK_MAX_MODULE_PATH_LEN) c11__abort("module path too long: %s", path);
if(path_len == 0) c11__abort("module path cannot be empty");
py_Ref r0 = py_pushtmp();
py_Ref r1 = py_pushtmp();
@ -35,7 +38,7 @@ py_Ref py_newmodule(const char* path) {
._obj = ManagedHeap__new(heap, tp_module, -1, 0),
};
int last_dot = c11_sv__rindex((c11_sv){path, strlen(path)}, '.');
int last_dot = c11_sv__rindex((c11_sv){path, path_len}, '.');
if(last_dot == -1) {
py_newstr(r1, path);
py_setdict(r0, __name__, r1);