This commit is contained in:
blueloveTH 2025-06-03 19:40:19 +08:00
parent dc4a21a594
commit 0ba44fc3cd
17 changed files with 148 additions and 73 deletions

View File

@ -7,15 +7,21 @@
#define SMALLMAP_T__HEADER #define SMALLMAP_T__HEADER
#define K py_Name #define K py_Name
#define V int #define V int
#define NAME c11_smallmap_n2i #define NAME c11_smallmap_n2d
#include "pocketpy/xmacros/smallmap.h" #include "pocketpy/xmacros/smallmap.h"
#undef SMALLMAP_T__HEADER #undef SMALLMAP_T__HEADER
#define SMALLMAP_T__HEADER
#define K int
#define V int
#define NAME c11_smallmap_d2d
#include "pocketpy/xmacros/smallmap.h"
#undef SMALLMAP_T__HEADER
#define SMALLMAP_T__HEADER #define SMALLMAP_T__HEADER
#define K c11_sv #define K c11_sv
#define V py_Name #define V py_Name
#define NAME c11_smallmap_s2n #define NAME c11_smallmap_v2n
#define less(a, b) (c11_sv__cmp((a), (b)) < 0) #define less(a, b) (c11_sv__cmp((a), (b)) < 0)
#define equal(a, b) (c11_sv__cmp((a), (b)) == 0) #define equal(a, b) (c11_sv__cmp((a), (b)) == 0)
#include "pocketpy/xmacros/smallmap.h" #include "pocketpy/xmacros/smallmap.h"

View File

@ -24,7 +24,7 @@
} while(0) } while(0)
#define c11__rtassert(cond) \ #define c11__rtassert(cond) \
if(!(cond)) c11__abort("Runtime assertion failed: %s", #cond) if(!(cond)) c11__abort("assertion failed: %s", #cond)
#define c11__min(a, b) ((a) < (b) ? (a) : (b)) #define c11__min(a, b) ((a) < (b) ? (a) : (b))
#define c11__max(a, b) ((a) > (b) ? (a) : (b)) #define c11__max(a, b) ((a) > (b) ? (a) : (b))

View File

@ -10,7 +10,7 @@ typedef struct {
} InternedEntry; } InternedEntry;
typedef struct { typedef struct {
c11_smallmap_s2n interned; c11_smallmap_v2n interned;
} InternedNames; } InternedNames;
void InternedNames__ctor(InternedNames* self); void InternedNames__ctor(InternedNames* self);

View File

@ -78,8 +78,8 @@ typedef struct CodeObject {
c11_vector /*T=py_Name*/ names; c11_vector /*T=py_Name*/ names;
int nlocals; int nlocals;
c11_smallmap_n2i varnames_inv; c11_smallmap_n2d varnames_inv;
c11_smallmap_n2i names_inv; c11_smallmap_n2d names_inv;
c11_vector /*T=CodeBlock*/ blocks; c11_vector /*T=CodeBlock*/ blocks;
c11_vector /*T=FuncDecl_*/ func_decls; c11_vector /*T=FuncDecl_*/ func_decls;
@ -114,7 +114,7 @@ typedef struct FuncDecl {
const char* docstring; // docstring of this function (weak ref) const char* docstring; // docstring of this function (weak ref)
FuncType type; FuncType type;
c11_smallmap_n2i kw_to_index; c11_smallmap_n2d kw_to_index;
} FuncDecl; } FuncDecl;
typedef FuncDecl* FuncDecl_; typedef FuncDecl* FuncDecl_;

View File

@ -469,21 +469,32 @@ PK_API bool py_delitem(py_Ref self, py_Ref key) PY_RAISE;
/// The result will be set to `py_retval()`. /// The result will be set to `py_retval()`.
/// The stack remains unchanged after the operation. /// The stack remains unchanged after the operation.
PK_API bool py_binaryop(py_Ref lhs, py_Ref rhs, py_Name op, py_Name rop) PY_RAISE PY_RETURN; PK_API bool py_binaryop(py_Ref lhs, py_Ref rhs, py_Name op, py_Name rop) PY_RAISE PY_RETURN;
/// lhs + rhs
#define py_binaryadd(lhs, rhs) py_binaryop(lhs, rhs, __add__, __radd__) PK_API bool py_binaryadd(py_Ref lhs, py_Ref rhs) PY_RAISE PY_RETURN;
#define py_binarysub(lhs, rhs) py_binaryop(lhs, rhs, __sub__, __rsub__) /// lhs - rhs
#define py_binarymul(lhs, rhs) py_binaryop(lhs, rhs, __mul__, __rmul__) PK_API bool py_binarysub(py_Ref lhs, py_Ref rhs) PY_RAISE PY_RETURN;
#define py_binarytruediv(lhs, rhs) py_binaryop(lhs, rhs, __truediv__, __rtruediv__) /// lhs * rhs
#define py_binaryfloordiv(lhs, rhs) py_binaryop(lhs, rhs, __floordiv__, __rfloordiv__) PK_API bool py_binarymul(py_Ref lhs, py_Ref rhs) PY_RAISE PY_RETURN;
#define py_binarymod(lhs, rhs) py_binaryop(lhs, rhs, __mod__, __rmod__) /// lhs / rhs
#define py_binarypow(lhs, rhs) py_binaryop(lhs, rhs, __pow__, __rpow__) PK_API bool py_binarytruediv(py_Ref lhs, py_Ref rhs) PY_RAISE PY_RETURN;
/// lhs // rhs
#define py_binarylshift(lhs, rhs) py_binaryop(lhs, rhs, __lshift__, 0) PK_API bool py_binaryfloordiv(py_Ref lhs, py_Ref rhs) PY_RAISE PY_RETURN;
#define py_binaryrshift(lhs, rhs) py_binaryop(lhs, rhs, __rshift__, 0) /// lhs % rhs
#define py_binaryand(lhs, rhs) py_binaryop(lhs, rhs, __and__, 0) PK_API bool py_binarymod(py_Ref lhs, py_Ref rhs) PY_RAISE PY_RETURN;
#define py_binaryor(lhs, rhs) py_binaryop(lhs, rhs, __or__, 0) /// lhs ** rhs
#define py_binaryxor(lhs, rhs) py_binaryop(lhs, rhs, __xor__, 0) PK_API bool py_binarypow(py_Ref lhs, py_Ref rhs) PY_RAISE PY_RETURN;
#define py_binarymatmul(lhs, rhs) py_binaryop(lhs, rhs, __matmul__, 0) /// lhs << rhs
PK_API bool py_binarylshift(py_Ref lhs, py_Ref rhs) PY_RAISE PY_RETURN;
/// lhs >> rhs
PK_API bool py_binaryrshift(py_Ref lhs, py_Ref rhs) PY_RAISE PY_RETURN;
/// lhs & rhs
PK_API bool py_binaryand(py_Ref lhs, py_Ref rhs) PY_RAISE PY_RETURN;
/// lhs | rhs
PK_API bool py_binaryor(py_Ref lhs, py_Ref rhs) PY_RAISE PY_RETURN;
/// lhs ^ rhs
PK_API bool py_binaryxor(py_Ref lhs, py_Ref rhs) PY_RAISE PY_RETURN;
/// lhs @ rhs
PK_API bool py_binarymatmul(py_Ref lhs, py_Ref rhs) PY_RAISE PY_RETURN;
/************* Stack Operations *************/ /************* Stack Operations *************/

View File

@ -6,7 +6,7 @@
/* Input */ /* Input */
#define K int #define K int
#define V float #define V float
#define NAME c11_smallmap_i2f #define NAME c11_smallmap_d2f
#endif #endif
/* Optional Input */ /* Optional Input */

View File

@ -3,15 +3,21 @@
#define SMALLMAP_T__SOURCE #define SMALLMAP_T__SOURCE
#define K py_Name #define K py_Name
#define V int #define V int
#define NAME c11_smallmap_n2i #define NAME c11_smallmap_n2d
#include "pocketpy/xmacros/smallmap.h" #include "pocketpy/xmacros/smallmap.h"
#undef SMALLMAP_T__SOURCE #undef SMALLMAP_T__SOURCE
#define SMALLMAP_T__SOURCE
#define K int
#define V int
#define NAME c11_smallmap_d2d
#include "pocketpy/xmacros/smallmap.h"
#undef SMALLMAP_T__SOURCE
#define SMALLMAP_T__SOURCE #define SMALLMAP_T__SOURCE
#define K c11_sv #define K c11_sv
#define V py_Name #define V py_Name
#define NAME c11_smallmap_s2n #define NAME c11_smallmap_v2n
#define less(a, b) (c11_sv__cmp((a), (b)) < 0) #define less(a, b) (c11_sv__cmp((a), (b)) < 0)
#define equal(a, b) (c11_sv__cmp((a), (b)) == 0) #define equal(a, b) (c11_sv__cmp((a), (b)) == 0)
#include "pocketpy/xmacros/smallmap.h" #include "pocketpy/xmacros/smallmap.h"

View File

@ -224,7 +224,7 @@ void pk_vsprintf(c11_sbuf* ss, const char* fmt, va_list args) {
break; break;
} }
case 'n': { case 'n': {
py_Name n = va_arg(args, int); py_Name n = va_arg(args, void*);
c11_sbuf__write_cstr(ss, py_name2str(n)); c11_sbuf__write_cstr(ss, py_name2str(n));
break; break;
} }

View File

@ -62,8 +62,8 @@ typedef struct Ctx {
int curr_iblock; int curr_iblock;
bool is_compiling_class; bool is_compiling_class;
c11_vector /*T=Expr_p*/ s_expr; c11_vector /*T=Expr_p*/ s_expr;
c11_smallmap_n2i global_names; c11_smallmap_n2d global_names;
c11_smallmap_s2n co_consts_string_dedup_map; // this stores 0-based index instead of pointer c11_smallmap_v2n co_consts_string_dedup_map; // this stores 0-based index instead of pointer
} Ctx; } Ctx;
typedef struct Expr Expr; typedef struct Expr Expr;
@ -101,7 +101,7 @@ typedef struct NameExpr {
void NameExpr__emit_(Expr* self_, Ctx* ctx) { void NameExpr__emit_(Expr* self_, Ctx* ctx) {
NameExpr* self = (NameExpr*)self_; NameExpr* self = (NameExpr*)self_;
int index = c11_smallmap_n2i__get(&ctx->co->varnames_inv, self->name, -1); int index = c11_smallmap_n2d__get(&ctx->co->varnames_inv, self->name, -1);
if(self->scope == NAME_LOCAL && index >= 0) { if(self->scope == NAME_LOCAL && index >= 0) {
// we know this is a local variable // we know this is a local variable
Ctx__emit_(ctx, OP_LOAD_FAST, index, self->line); Ctx__emit_(ctx, OP_LOAD_FAST, index, self->line);
@ -711,7 +711,7 @@ static void BinaryExpr__dtor(Expr* self_) {
vtdelete(self->rhs); vtdelete(self->rhs);
} }
static uint16_t cmp_token2name(TokenIndex token) { static py_Name cmp_token2name(TokenIndex token) {
switch(token) { switch(token) {
case TK_LT: return __lt__; case TK_LT: return __lt__;
case TK_LE: return __le__; case TK_LE: return __le__;
@ -1075,8 +1075,8 @@ static void Ctx__ctor(Ctx* self, CodeObject* co, FuncDecl* func, int level) {
self->curr_iblock = 0; self->curr_iblock = 0;
self->is_compiling_class = false; self->is_compiling_class = false;
c11_vector__ctor(&self->s_expr, sizeof(Expr*)); c11_vector__ctor(&self->s_expr, sizeof(Expr*));
c11_smallmap_n2i__ctor(&self->global_names); c11_smallmap_n2d__ctor(&self->global_names);
c11_smallmap_s2n__ctor(&self->co_consts_string_dedup_map); c11_smallmap_v2n__ctor(&self->co_consts_string_dedup_map);
} }
static void Ctx__dtor(Ctx* self) { static void Ctx__dtor(Ctx* self) {
@ -1085,13 +1085,13 @@ static void Ctx__dtor(Ctx* self) {
vtdelete(c11__getitem(Expr*, &self->s_expr, i)); vtdelete(c11__getitem(Expr*, &self->s_expr, i));
} }
c11_vector__dtor(&self->s_expr); c11_vector__dtor(&self->s_expr);
c11_smallmap_n2i__dtor(&self->global_names); c11_smallmap_n2d__dtor(&self->global_names);
// free the dedup map // free the dedup map
c11__foreach(c11_smallmap_s2n_KV, &self->co_consts_string_dedup_map, p_kv) { c11__foreach(c11_smallmap_v2n_KV, &self->co_consts_string_dedup_map, p_kv) {
const char* p = p_kv->key.data; const char* p = p_kv->key.data;
PK_FREE((void*)p); PK_FREE((void*)p);
} }
c11_smallmap_s2n__dtor(&self->co_consts_string_dedup_map); c11_smallmap_v2n__dtor(&self->co_consts_string_dedup_map);
} }
static int Ctx__prepare_loop_divert(Ctx* self, int line, bool is_break) { static int Ctx__prepare_loop_divert(Ctx* self, int line, bool is_break) {
@ -1211,7 +1211,7 @@ static int Ctx__add_const_string(Ctx* self, c11_sv key) {
int index = self->co->consts.length - 1; int index = self->co->consts.length - 1;
return index; return index;
} }
uintptr_t* val = c11_smallmap_s2n__try_get(&self->co_consts_string_dedup_map, key); uintptr_t* val = c11_smallmap_v2n__try_get(&self->co_consts_string_dedup_map, key);
if(val) { if(val) {
return *val; return *val;
} else { } else {
@ -1222,7 +1222,7 @@ static int Ctx__add_const_string(Ctx* self, c11_sv key) {
char* new_buf = PK_MALLOC(key.size + 1); char* new_buf = PK_MALLOC(key.size + 1);
memcpy(new_buf, key.data, key.size); memcpy(new_buf, key.data, key.size);
new_buf[key.size] = 0; new_buf[key.size] = 0;
c11_smallmap_s2n__set(&self->co_consts_string_dedup_map, c11_smallmap_v2n__set(&self->co_consts_string_dedup_map,
(c11_sv){new_buf, key.size}, (c11_sv){new_buf, key.size},
index); index);
return index; return index;
@ -1747,7 +1747,7 @@ static Error* exprName(Compiler* self) {
py_Name name = py_namev(Token__sv(prev())); py_Name name = py_namev(Token__sv(prev()));
NameScope scope = name_scope(self); NameScope scope = name_scope(self);
// promote this name to global scope if needed // promote this name to global scope if needed
if(c11_smallmap_n2i__contains(&ctx()->global_names, name)) { if(c11_smallmap_n2d__contains(&ctx()->global_names, name)) {
if(self->src->is_dynamic) return SyntaxError(self, "cannot use global keyword here"); if(self->src->is_dynamic) return SyntaxError(self, "cannot use global keyword here");
scope = NAME_GLOBAL; scope = NAME_GLOBAL;
} }
@ -2727,7 +2727,7 @@ static Error* compile_stmt(Compiler* self) {
do { do {
consume(TK_ID); consume(TK_ID);
py_Name name = py_namev(Token__sv(prev())); py_Name name = py_namev(Token__sv(prev()));
c11_smallmap_n2i__set(&ctx()->global_names, name, 0); c11_smallmap_n2d__set(&ctx()->global_names, name, 0);
} while(match(TK_COMMA)); } while(match(TK_COMMA));
consume_end_stmt(); consume_end_stmt();
break; break;

View File

@ -1295,6 +1295,58 @@ bool py_binaryop(py_Ref lhs, py_Ref rhs, py_Name op, py_Name rop) {
return ok; return ok;
} }
bool py_binaryadd(py_Ref lhs, py_Ref rhs) {
return py_binaryop(lhs, rhs, __add__, __radd__);
}
bool py_binarysub(py_Ref lhs, py_Ref rhs) {
return py_binaryop(lhs, rhs, __sub__, __rsub__);
}
bool py_binarymul(py_Ref lhs, py_Ref rhs) {
return py_binaryop(lhs, rhs, __mul__, __rmul__);
}
bool py_binarytruediv(py_Ref lhs, py_Ref rhs) {
return py_binaryop(lhs, rhs, __truediv__, __rtruediv__);
}
bool py_binaryfloordiv(py_Ref lhs, py_Ref rhs) {
return py_binaryop(lhs, rhs, __floordiv__, __rfloordiv__);
}
bool py_binarymod(py_Ref lhs, py_Ref rhs) {
return py_binaryop(lhs, rhs, __mod__, __rmod__);
}
bool py_binarypow(py_Ref lhs, py_Ref rhs) {
return py_binaryop(lhs, rhs, __pow__, __rpow__);
}
bool py_binarylshift(py_Ref lhs, py_Ref rhs) {
return py_binaryop(lhs, rhs, __lshift__, 0);
}
bool py_binaryrshift(py_Ref lhs, py_Ref rhs) {
return py_binaryop(lhs, rhs, __rshift__, 0);
}
bool py_binaryand(py_Ref lhs, py_Ref rhs) {
return py_binaryop(lhs, rhs, __and__, 0);
}
bool py_binaryor(py_Ref lhs, py_Ref rhs) {
return py_binaryop(lhs, rhs, __or__, 0);
}
bool py_binaryxor(py_Ref lhs, py_Ref rhs) {
return py_binaryop(lhs, rhs, __xor__, 0);
}
bool py_binarymatmul(py_Ref lhs, py_Ref rhs) {
return py_binaryop(lhs, rhs, __matmul__, 0);
}
static bool stack_format_object(VM* self, c11_sv spec) { static bool stack_format_object(VM* self, c11_sv spec) {
// format TOS via `spec` inplace // format TOS via `spec` inplace
// spec: '!r:.2f', '.2f' // spec: '!r:.2f', '.2f'

View File

@ -16,7 +16,7 @@ void ValueStack__dtor(ValueStack* self) { self->sp = self->begin; }
void FastLocals__to_dict(py_TValue* locals, const CodeObject* co) { void FastLocals__to_dict(py_TValue* locals, const CodeObject* co) {
py_StackRef dict = py_pushtmp(); py_StackRef dict = py_pushtmp();
py_newdict(dict); py_newdict(dict);
c11__foreach(c11_smallmap_n2i_KV, &co->varnames_inv, entry) { c11__foreach(c11_smallmap_n2d_KV, &co->varnames_inv, entry) {
py_TValue* value = &locals[entry->value]; py_TValue* value = &locals[entry->value];
if(!py_isnil(value)) { if(!py_isnil(value)) {
bool ok = py_dict_setitem(dict, py_name2ref(entry->key), value); bool ok = py_dict_setitem(dict, py_name2ref(entry->key), value);
@ -30,7 +30,7 @@ void FastLocals__to_dict(py_TValue* locals, const CodeObject* co) {
NameDict* FastLocals__to_namedict(py_TValue* locals, const CodeObject* co) { NameDict* FastLocals__to_namedict(py_TValue* locals, const CodeObject* co) {
NameDict* dict = NameDict__new(PK_INST_ATTR_LOAD_FACTOR); NameDict* dict = NameDict__new(PK_INST_ATTR_LOAD_FACTOR);
c11__foreach(c11_smallmap_n2i_KV, &co->varnames_inv, entry) { c11__foreach(c11_smallmap_n2d_KV, &co->varnames_inv, entry) {
py_Ref val = &locals[entry->value]; py_Ref val = &locals[entry->value];
if(!py_isnil(val)) NameDict__set(dict, entry->key, val); if(!py_isnil(val)) NameDict__set(dict, entry->key, val);
} }
@ -166,7 +166,7 @@ int Frame__delglobal(py_Frame* self, py_Name name) {
py_StackRef Frame__getlocal_noproxy(py_Frame* self, py_Name name) { py_StackRef Frame__getlocal_noproxy(py_Frame* self, py_Name name) {
assert(!self->is_locals_special); assert(!self->is_locals_special);
int index = c11_smallmap_n2i__get(&self->co->varnames_inv, name, -1); int index = c11_smallmap_n2d__get(&self->co->varnames_inv, name, -1);
if(index == -1) return NULL; if(index == -1) return NULL;
return &self->locals[index]; return &self->locals[index];
} }

View File

@ -6,7 +6,7 @@
#undef MAGIC_METHOD #undef MAGIC_METHOD
void InternedNames__ctor(InternedNames* self) { void InternedNames__ctor(InternedNames* self) {
c11_smallmap_s2n__ctor(&self->interned); c11_smallmap_v2n__ctor(&self->interned);
// initialize all magic names // initialize all magic names
#define MAGIC_METHOD(x) x = py_name(#x); #define MAGIC_METHOD(x) x = py_name(#x);
@ -16,10 +16,10 @@ void InternedNames__ctor(InternedNames* self) {
void InternedNames__dtor(InternedNames* self) { void InternedNames__dtor(InternedNames* self) {
for(int i = 0; i < self->interned.length; i++) { for(int i = 0; i < self->interned.length; i++) {
c11_smallmap_s2n_KV* kv = c11__at(c11_smallmap_s2n_KV, &self->interned, i); c11_smallmap_v2n_KV* kv = c11__at(c11_smallmap_v2n_KV, &self->interned, i);
PK_FREE((void*)kv->value); PK_FREE((void*)kv->value);
} }
c11_smallmap_s2n__dtor(&self->interned); c11_smallmap_v2n__dtor(&self->interned);
} }
py_Name py_name(const char* name) { py_Name py_name(const char* name) {
@ -31,7 +31,7 @@ py_Name py_name(const char* name) {
py_Name py_namev(c11_sv name) { py_Name py_namev(c11_sv name) {
InternedNames* self = &pk_current_vm->names; InternedNames* self = &pk_current_vm->names;
py_Name index = c11_smallmap_s2n__get(&self->interned, name, 0); py_Name index = c11_smallmap_v2n__get(&self->interned, name, 0);
if(index != 0) return index; if(index != 0) return index;
// generate new index // generate new index
InternedEntry* p = PK_MALLOC(sizeof(InternedEntry) + name.size + 1); InternedEntry* p = PK_MALLOC(sizeof(InternedEntry) + name.size + 1);
@ -41,7 +41,7 @@ py_Name py_namev(c11_sv name) {
memset(&p->obj, 0, sizeof(py_TValue)); memset(&p->obj, 0, sizeof(py_TValue));
index = (py_Name)p; index = (py_Name)p;
// save to _interned // save to _interned
c11_smallmap_s2n__set(&self->interned, (c11_sv){p->data, name.size}, index); c11_smallmap_v2n__set(&self->interned, (c11_sv){p->data, name.size}, index);
return index; return index;
} }

View File

@ -443,7 +443,7 @@ static bool
for(int j = 0; j < kwargc; j++) { for(int j = 0; j < kwargc; j++) {
py_Name key = (py_Name)py_toint(&p1[2 * j]); py_Name key = (py_Name)py_toint(&p1[2 * j]);
int index = c11_smallmap_n2i__get(&decl->kw_to_index, key, -1); int index = c11_smallmap_n2d__get(&decl->kw_to_index, key, -1);
// if key is an explicit key, set as local variable // if key is an explicit key, set as local variable
if(index >= 0) { if(index >= 0) {
buffer[index] = p1[2 * j + 1]; buffer[index] = p1[2 * j + 1];
@ -668,7 +668,7 @@ void ManagedHeap__mark(ManagedHeap* self) {
} }
// mark interned names // mark interned names
for(int i = 0; i < vm->names.interned.length; i++) { for(int i = 0; i < vm->names.interned.length; i++) {
c11_smallmap_s2n_KV* kv = c11__at(c11_smallmap_s2n_KV, &vm->names.interned, i); c11_smallmap_v2n_KV* kv = c11__at(c11_smallmap_v2n_KV, &vm->names.interned, i);
InternedEntry* entry = (InternedEntry*)kv->value; InternedEntry* entry = (InternedEntry*)kv->value;
pk__mark_value(&entry->obj); pk__mark_value(&entry->obj);
} }

View File

@ -442,7 +442,7 @@ static py_i64 pkl__header_read_int(const unsigned char** p, char sep) {
return out; return out;
} }
bool py_pickle_loads_body(const unsigned char* p, int memo_length, c11_smallmap_n2i* type_mapping); bool py_pickle_loads_body(const unsigned char* p, int memo_length, c11_smallmap_d2d* type_mapping);
bool py_pickle_loads(const unsigned char* data, int size) { bool py_pickle_loads(const unsigned char* data, int size) {
const unsigned char* p = data; const unsigned char* p = data;
@ -452,8 +452,8 @@ bool py_pickle_loads(const unsigned char* data, int size) {
return ValueError("invalid pickle data"); return ValueError("invalid pickle data");
p += 4; p += 4;
c11_smallmap_n2i type_mapping; c11_smallmap_d2d type_mapping;
c11_smallmap_n2i__ctor(&type_mapping); c11_smallmap_d2d__ctor(&type_mapping);
while(true) { while(true) {
if(*p == '\n') { if(*p == '\n') {
@ -464,25 +464,25 @@ bool py_pickle_loads(const unsigned char* data, int size) {
c11_sv path = pkl__header_read_sv(&p, ')'); c11_sv path = pkl__header_read_sv(&p, ')');
py_Type new_type = pkl__header_find_type(path); py_Type new_type = pkl__header_find_type(path);
if(new_type == 0) { if(new_type == 0) {
c11_smallmap_n2i__dtor(&type_mapping); c11_smallmap_d2d__dtor(&type_mapping);
return ImportError("cannot find type '%v'", path); return ImportError("cannot find type '%v'", path);
} }
if(type != new_type) c11_smallmap_n2i__set(&type_mapping, type, new_type); if(type != new_type) c11_smallmap_d2d__set(&type_mapping, type, new_type);
} }
int memo_length = pkl__header_read_int(&p, '\n'); int memo_length = pkl__header_read_int(&p, '\n');
bool ok = py_pickle_loads_body(p, memo_length, &type_mapping); bool ok = py_pickle_loads_body(p, memo_length, &type_mapping);
c11_smallmap_n2i__dtor(&type_mapping); c11_smallmap_d2d__dtor(&type_mapping);
return ok; return ok;
} }
static py_Type pkl__fix_type(py_Type type, c11_smallmap_n2i* type_mapping) { static py_Type pkl__fix_type(py_Type type, c11_smallmap_d2d* type_mapping) {
int new_type = c11_smallmap_n2i__get(type_mapping, type, -1); int new_type = c11_smallmap_d2d__get(type_mapping, type, -1);
if(new_type != -1) return (py_Type)new_type; if(new_type != -1) return (py_Type)new_type;
return type; return type;
} }
bool py_pickle_loads_body(const unsigned char* p, int memo_length, c11_smallmap_n2i* type_mapping) { bool py_pickle_loads_body(const unsigned char* p, int memo_length, c11_smallmap_d2d* type_mapping) {
py_StackRef p0 = py_peek(0); py_StackRef p0 = py_peek(0);
py_Ref p_memo = py_newtuple(py_pushtmp(), memo_length); py_Ref p_memo = py_newtuple(py_pushtmp(), memo_length);
while(true) { while(true) {

View File

@ -20,7 +20,7 @@ static void FuncDecl__dtor(FuncDecl* self) {
CodeObject__dtor(&self->code); CodeObject__dtor(&self->code);
c11_vector__dtor(&self->args); c11_vector__dtor(&self->args);
c11_vector__dtor(&self->kwargs); c11_vector__dtor(&self->kwargs);
c11_smallmap_n2i__dtor(&self->kw_to_index); c11_smallmap_n2d__dtor(&self->kw_to_index);
} }
FuncDecl_ FuncDecl__rcnew(SourceData_ src, c11_sv name) { FuncDecl_ FuncDecl__rcnew(SourceData_ src, c11_sv name) {
@ -39,7 +39,7 @@ FuncDecl_ FuncDecl__rcnew(SourceData_ src, c11_sv name) {
self->docstring = NULL; self->docstring = NULL;
self->type = FuncType_UNSET; self->type = FuncType_UNSET;
c11_smallmap_n2i__ctor(&self->kw_to_index); c11_smallmap_n2d__ctor(&self->kw_to_index);
return self; return self;
} }
@ -71,7 +71,7 @@ void FuncDecl__add_arg(FuncDecl* self, py_Name name) {
void FuncDecl__add_kwarg(FuncDecl* self, py_Name name, const py_TValue* value) { void FuncDecl__add_kwarg(FuncDecl* self, py_Name name, const py_TValue* value) {
int index = CodeObject__add_varname(&self->code, name); int index = CodeObject__add_varname(&self->code, name);
c11_smallmap_n2i__set(&self->kw_to_index, name, index); c11_smallmap_n2d__set(&self->kw_to_index, name, index);
FuncDeclKwArg* item = c11_vector__emplace(&self->kwargs); FuncDeclKwArg* item = c11_vector__emplace(&self->kwargs);
item->index = index; item->index = index;
item->key = name; item->key = name;
@ -127,8 +127,8 @@ void CodeObject__ctor(CodeObject* self, SourceData_ src, c11_sv name) {
c11_vector__ctor(&self->names, sizeof(py_Name)); c11_vector__ctor(&self->names, sizeof(py_Name));
self->nlocals = 0; self->nlocals = 0;
c11_smallmap_n2i__ctor(&self->varnames_inv); c11_smallmap_n2d__ctor(&self->varnames_inv);
c11_smallmap_n2i__ctor(&self->names_inv); c11_smallmap_n2d__ctor(&self->names_inv);
c11_vector__ctor(&self->blocks, sizeof(CodeBlock)); c11_vector__ctor(&self->blocks, sizeof(CodeBlock));
c11_vector__ctor(&self->func_decls, sizeof(FuncDecl_)); c11_vector__ctor(&self->func_decls, sizeof(FuncDecl_));
@ -151,8 +151,8 @@ void CodeObject__dtor(CodeObject* self) {
c11_vector__dtor(&self->varnames); c11_vector__dtor(&self->varnames);
c11_vector__dtor(&self->names); c11_vector__dtor(&self->names);
c11_smallmap_n2i__dtor(&self->varnames_inv); c11_smallmap_n2d__dtor(&self->varnames_inv);
c11_smallmap_n2i__dtor(&self->names_inv); c11_smallmap_n2d__dtor(&self->names_inv);
c11_vector__dtor(&self->blocks); c11_vector__dtor(&self->blocks);
@ -174,21 +174,21 @@ void Function__ctor(Function* self, FuncDecl_ decl, py_GlobalRef module, py_Ref
} }
int CodeObject__add_varname(CodeObject* self, py_Name name) { int CodeObject__add_varname(CodeObject* self, py_Name name) {
int index = c11_smallmap_n2i__get(&self->varnames_inv, name, -1); int index = c11_smallmap_n2d__get(&self->varnames_inv, name, -1);
if(index >= 0) return index; if(index >= 0) return index;
c11_vector__push(py_Name, &self->varnames, name); c11_vector__push(py_Name, &self->varnames, name);
self->nlocals++; self->nlocals++;
index = self->varnames.length - 1; index = self->varnames.length - 1;
c11_smallmap_n2i__set(&self->varnames_inv, name, index); c11_smallmap_n2d__set(&self->varnames_inv, name, index);
return index; return index;
} }
int CodeObject__add_name(CodeObject* self, py_Name name) { int CodeObject__add_name(CodeObject* self, py_Name name) {
int index = c11_smallmap_n2i__get(&self->names_inv, name, -1); int index = c11_smallmap_n2d__get(&self->names_inv, name, -1);
if(index >= 0) return index; if(index >= 0) return index;
c11_vector__push(py_Name, &self->names, name); c11_vector__push(py_Name, &self->names, name);
index = self->names.length - 1; index = self->names.length - 1;
c11_smallmap_n2i__set(&self->names_inv, name, index); c11_smallmap_n2d__set(&self->names_inv, name, index);
return index; return index;
} }

View File

@ -752,11 +752,11 @@ py_TValue pk_builtins__register() {
// some patches // some patches
py_bindmagic(tp_NoneType, __repr__, NoneType__repr__); py_bindmagic(tp_NoneType, __repr__, NoneType__repr__);
*py_tpgetmagic(tp_NoneType, __hash__) = *py_None(); py_setdict(py_tpobject(tp_NoneType), __hash__, py_None());
py_bindmagic(tp_ellipsis, __repr__, ellipsis__repr__); py_bindmagic(tp_ellipsis, __repr__, ellipsis__repr__);
*py_tpgetmagic(tp_ellipsis, __hash__) = *py_None(); py_setdict(py_tpobject(tp_ellipsis), __hash__, py_None());
py_bindmagic(tp_NotImplementedType, __repr__, NotImplementedType__repr__); py_bindmagic(tp_NotImplementedType, __repr__, NotImplementedType__repr__);
*py_tpgetmagic(tp_NotImplementedType, __hash__) = *py_None(); py_setdict(py_tpobject(tp_NotImplementedType), __hash__, py_None());
return *builtins; return *builtins;
} }

View File

@ -79,7 +79,7 @@ py_Type pk_namedict__register() {
py_bindmagic(type, __setitem__, namedict__setitem__); py_bindmagic(type, __setitem__, namedict__setitem__);
py_bindmagic(type, __delitem__, namedict__delitem__); py_bindmagic(type, __delitem__, namedict__delitem__);
py_bindmagic(type, __contains__, namedict__contains__); py_bindmagic(type, __contains__, namedict__contains__);
py_newnone(py_tpgetmagic(type, __hash__)); py_setdict(py_tpobject(type), __hash__, py_None());
py_bindmethod(type, "items", namedict_items); py_bindmethod(type, "items", namedict_items);
py_bindmethod(type, "clear", namedict_clear); py_bindmethod(type, "clear", namedict_clear);
return type; return type;