mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 03:20:18 +00:00
Compare commits
3 Commits
8eff9abfbc
...
205f6ff225
Author | SHA1 | Date | |
---|---|---|---|
|
205f6ff225 | ||
|
397123e3c9 | ||
|
e04d4e790c |
@ -13,12 +13,6 @@
|
||||
#define PK_ENABLE_OS 0
|
||||
#endif
|
||||
|
||||
// Enable this if you are working with multi-threading (experimental)
|
||||
// This triggers necessary locks to make the VM thread-safe
|
||||
#ifndef PK_ENABLE_THREAD // can be overridden by cmake
|
||||
#define PK_ENABLE_THREAD 0
|
||||
#endif
|
||||
|
||||
// Enable `line_profiler` module and `breakpoint()` function
|
||||
#ifndef PK_ENABLE_PROFILER // can be overridden by cmake
|
||||
#define PK_ENABLE_PROFILER 0
|
||||
@ -56,9 +50,3 @@
|
||||
#else
|
||||
#define PK_PLATFORM_SEP '/'
|
||||
#endif
|
||||
|
||||
#if PK_ENABLE_THREAD
|
||||
#define PK_THREAD_LOCAL thread_local
|
||||
#else
|
||||
#define PK_THREAD_LOCAL static
|
||||
#endif
|
||||
|
@ -30,6 +30,7 @@ void c11_sbuf__write_hex(c11_sbuf* self, unsigned char, bool non_zero);
|
||||
void c11_sbuf__write_ptr(c11_sbuf* self, void*);
|
||||
// Submit the stream and return the final string. The stream becomes invalid after this call
|
||||
c11_string* c11_sbuf__submit(c11_sbuf* self);
|
||||
void c11_sbuf__py_submit(c11_sbuf* self, py_Ref out);
|
||||
|
||||
void pk_vsprintf(c11_sbuf* ss, const char* fmt, va_list args);
|
||||
void pk_sprintf(c11_sbuf* ss, const char* fmt, ...);
|
||||
|
@ -47,10 +47,15 @@ extern py_GlobalRef py_None;
|
||||
extern py_GlobalRef py_NIL;
|
||||
|
||||
/************* Global Setup *************/
|
||||
/// Initialize the VM.
|
||||
/// Initialize pocketpy and the default VM.
|
||||
void py_initialize();
|
||||
/// Finalize the VM.
|
||||
/// Finalize pocketpy.
|
||||
void py_finalize();
|
||||
/// Get the current VM index.
|
||||
int py_currentvm();
|
||||
/// Switch to a VM.
|
||||
/// @param index index of the VM ranging from 0 to 16 (exclusive). `0` is the default VM.
|
||||
void py_switchvm(int index);
|
||||
|
||||
/// Run a source string.
|
||||
/// @param source source string.
|
||||
|
@ -238,59 +238,47 @@ static int FixedMemoryPool__total_bytes(FixedMemoryPool* self) {
|
||||
return self->BlockCount * self->BlockSize;
|
||||
}
|
||||
|
||||
PK_THREAD_LOCAL FixedMemoryPool PoolExpr;
|
||||
PK_THREAD_LOCAL FixedMemoryPool PoolFrame;
|
||||
PK_THREAD_LOCAL MemoryPool PoolObject;
|
||||
PK_THREAD_LOCAL bool _Pools_initialized = false;
|
||||
static FixedMemoryPool PoolExpr;
|
||||
static FixedMemoryPool PoolFrame;
|
||||
static MemoryPool PoolObject;
|
||||
|
||||
void pk_MemoryPools__initialize(){
|
||||
if(_Pools_initialized) return;
|
||||
FixedMemoryPool__ctor(&PoolExpr, kPoolExprBlockSize, 64);
|
||||
FixedMemoryPool__ctor(&PoolFrame, kPoolFrameBlockSize, 128);
|
||||
MemoryPool__ctor(&PoolObject);
|
||||
_Pools_initialized = true;
|
||||
}
|
||||
|
||||
void pk_MemoryPools__finalize(){
|
||||
if(!_Pools_initialized) return;
|
||||
FixedMemoryPool__dtor(&PoolExpr);
|
||||
FixedMemoryPool__dtor(&PoolFrame);
|
||||
MemoryPool__dtor(&PoolObject);
|
||||
_Pools_initialized = false;
|
||||
}
|
||||
|
||||
void* PoolExpr_alloc() {
|
||||
assert(_Pools_initialized);
|
||||
return FixedMemoryPool__alloc(&PoolExpr);
|
||||
}
|
||||
|
||||
void PoolExpr_dealloc(void* p) {
|
||||
assert(_Pools_initialized);
|
||||
FixedMemoryPool__dealloc(&PoolExpr, p);
|
||||
}
|
||||
|
||||
void* PoolFrame_alloc() {
|
||||
assert(_Pools_initialized);
|
||||
return FixedMemoryPool__alloc(&PoolFrame);
|
||||
}
|
||||
|
||||
void PoolFrame_dealloc(void* p) {
|
||||
assert(_Pools_initialized);
|
||||
FixedMemoryPool__dealloc(&PoolFrame, p);
|
||||
}
|
||||
|
||||
void* PoolObject_alloc() {
|
||||
assert(_Pools_initialized);
|
||||
return MemoryPool__alloc(&PoolObject);
|
||||
}
|
||||
|
||||
void PoolObject_dealloc(void* p) {
|
||||
assert(_Pools_initialized);
|
||||
MemoryPool__dealloc(&PoolObject, p);
|
||||
}
|
||||
|
||||
void PoolObject_shrink_to_fit() {
|
||||
assert(_Pools_initialized);
|
||||
MemoryPool__shrink_to_fit(&PoolObject);
|
||||
}
|
||||
|
||||
|
@ -147,6 +147,12 @@ c11_string* c11_sbuf__submit(c11_sbuf* self) {
|
||||
return retval;
|
||||
}
|
||||
|
||||
void c11_sbuf__py_submit(c11_sbuf *self, py_Ref out){
|
||||
c11_string* res = c11_sbuf__submit(self);
|
||||
py_newstrn(out, res->data, res->size);
|
||||
c11_string__delete(res);
|
||||
}
|
||||
|
||||
void pk_vsprintf(c11_sbuf* ss, const char* fmt, va_list args) {
|
||||
while(*fmt) {
|
||||
char c = *fmt;
|
||||
|
@ -502,9 +502,7 @@ pk_FrameResult pk_VM__run_top_frame(pk_VM* self) {
|
||||
c11_sbuf__write_sv(&ss, py_tosv(&self->last_retval));
|
||||
}
|
||||
SP() = begin;
|
||||
c11_string* res = c11_sbuf__submit(&ss);
|
||||
py_newstrn(SP()++, res->data, res->size);
|
||||
c11_string__delete(res);
|
||||
c11_sbuf__py_submit(&ss, SP()++);
|
||||
DISPATCH();
|
||||
}
|
||||
/*****************************/
|
||||
@ -1109,8 +1107,6 @@ static bool format_object(py_Ref val, c11_sv spec) {
|
||||
}
|
||||
|
||||
c11_string__delete(body);
|
||||
c11_string* res = c11_sbuf__submit(&buf);
|
||||
py_newstrn(py_retval(), res->data, res->size);
|
||||
c11_string__delete(res);
|
||||
c11_sbuf__py_submit(&buf, py_retval());
|
||||
return true;
|
||||
}
|
@ -17,11 +17,13 @@ py_GlobalRef py_None;
|
||||
py_GlobalRef py_NIL;
|
||||
|
||||
static pk_VM pk_default_vm;
|
||||
static pk_VM* pk_all_vm[16];
|
||||
|
||||
void py_initialize() {
|
||||
pk_MemoryPools__initialize();
|
||||
py_Name__initialize();
|
||||
pk_current_vm = &pk_default_vm;
|
||||
|
||||
pk_current_vm = pk_all_vm[0] = &pk_default_vm;
|
||||
|
||||
// initialize some convenient references
|
||||
static py_TValue _True, _False, _None, _NIL;
|
||||
@ -37,12 +39,35 @@ void py_initialize() {
|
||||
}
|
||||
|
||||
void py_finalize() {
|
||||
for(int i = 1; i < 16; i++) {
|
||||
pk_VM* vm = pk_all_vm[i];
|
||||
if(vm) {
|
||||
pk_VM__dtor(vm);
|
||||
free(vm);
|
||||
}
|
||||
}
|
||||
pk_VM__dtor(&pk_default_vm);
|
||||
pk_current_vm = NULL;
|
||||
py_Name__finalize();
|
||||
pk_MemoryPools__finalize();
|
||||
}
|
||||
|
||||
void py_switchvm(int index){
|
||||
if(index < 0 || index >= 16) c11__abort("invalid vm index");
|
||||
if(!pk_all_vm[index]){
|
||||
pk_all_vm[index] = malloc(sizeof(pk_VM));
|
||||
pk_VM__ctor(pk_all_vm[index]);
|
||||
}
|
||||
pk_current_vm = pk_all_vm[index];
|
||||
}
|
||||
|
||||
int py_currentvm() {
|
||||
for(int i = 0; i < 16; i++) {
|
||||
if(pk_all_vm[i] == pk_current_vm) return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
const char* pk_opname(Opcode op) {
|
||||
const static char* OP_NAMES[] = {
|
||||
#define OPCODE(name) #name,
|
||||
|
@ -109,10 +109,8 @@ static bool _py_builtins__hex(int argc, py_Ref argv) {
|
||||
c11_sbuf__write_hex(&ss, cpnt, non_zero);
|
||||
if(cpnt != 0) non_zero = false;
|
||||
}
|
||||
// return VAR(ss.str());
|
||||
c11_string* res = c11_sbuf__submit(&ss);
|
||||
py_newstrn(py_retval(), res->data, res->size);
|
||||
c11_string__delete(res);
|
||||
|
||||
c11_sbuf__py_submit(&ss, py_retval());
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -288,9 +288,7 @@ static bool _py_dict__repr__(int argc, py_Ref argv) {
|
||||
is_first = false;
|
||||
}
|
||||
c11_sbuf__write_char(&buf, '}');
|
||||
c11_string* res = c11_sbuf__submit(&buf);
|
||||
py_newstrn(py_retval(), res->data, res->size);
|
||||
c11_string__delete(res);
|
||||
c11_sbuf__py_submit(&buf, py_retval());
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -78,9 +78,7 @@ static bool _py_BaseException__repr__(int argc, py_Ref argv) {
|
||||
c11_sbuf__write_sv(&ss, py_tosv(py_retval()));
|
||||
}
|
||||
c11_sbuf__write_char(&ss, ')');
|
||||
c11_string* res = c11_sbuf__submit(&ss);
|
||||
py_newstrn(py_retval(), res->data, res->size);
|
||||
c11_string__delete(res);
|
||||
c11_sbuf__py_submit(&ss, py_retval());
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -92,9 +90,7 @@ static bool _py_BaseException__str__(int argc, py_Ref argv) {
|
||||
if(!py_str(arg)) return false;
|
||||
c11_sbuf__write_sv(&ss, py_tosv(py_retval()));
|
||||
}
|
||||
c11_string* res = c11_sbuf__submit(&ss);
|
||||
py_newstrn(py_retval(), res->data, res->size);
|
||||
c11_string__delete(res);
|
||||
c11_sbuf__py_submit(&ss, py_retval());
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -172,10 +168,8 @@ bool py_exception(const char* name, const char* fmt, ...) {
|
||||
pk_vsprintf(&buf, fmt, args);
|
||||
va_end(args);
|
||||
|
||||
c11_string* res = c11_sbuf__submit(&buf);
|
||||
py_Ref message = py_pushtmp();
|
||||
py_newstrn(message, res->data, res->size);
|
||||
c11_string__delete(res);
|
||||
c11_sbuf__py_submit(&buf, message);
|
||||
|
||||
py_Ref exc_type = py_getdict(&pk_current_vm->builtins, py_name(name));
|
||||
if(exc_type == NULL) c11__abort("py_exception(): '%s' not found", name);
|
||||
|
@ -239,9 +239,7 @@ static bool _py_list__repr__(int argc, py_Ref argv) {
|
||||
if(i != self->count - 1) c11_sbuf__write_cstr(&buf, ", ");
|
||||
}
|
||||
c11_sbuf__write_char(&buf, ']');
|
||||
c11_string* res = c11_sbuf__submit(&buf);
|
||||
py_newstrn(py_retval(), res->data, res->size);
|
||||
c11_string__delete(res);
|
||||
c11_sbuf__py_submit(&buf, py_retval());
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -201,9 +201,7 @@ static bool _py_float__repr__(int argc, py_Ref argv) {
|
||||
c11_sbuf buf;
|
||||
c11_sbuf__ctor(&buf);
|
||||
c11_sbuf__write_f64(&buf, val, -1);
|
||||
c11_string* res = c11_sbuf__submit(&buf);
|
||||
py_newstrn(py_retval(), res->data, res->size);
|
||||
c11_string__delete(res);
|
||||
c11_sbuf__py_submit(&buf, py_retval());
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -40,9 +40,7 @@ static bool _py_object__repr__(int argc, py_Ref argv) {
|
||||
c11_sbuf buf;
|
||||
c11_sbuf__ctor(&buf);
|
||||
pk_sprintf(&buf, "<%t object at %p>", argv->type, argv->_obj);
|
||||
c11_string* res = c11_sbuf__submit(&buf);
|
||||
py_newstrn(py_retval(), res->data, res->size);
|
||||
c11_string__delete(res);
|
||||
c11_sbuf__py_submit(&buf, py_retval());
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -51,9 +49,7 @@ static bool _py_type__repr__(int argc, py_Ref argv) {
|
||||
c11_sbuf buf;
|
||||
c11_sbuf__ctor(&buf);
|
||||
pk_sprintf(&buf, "<class '%t'>", py_totype(argv));
|
||||
c11_string* res = c11_sbuf__submit(&buf);
|
||||
py_newstrn(py_retval(), res->data, res->size);
|
||||
c11_string__delete(res);
|
||||
c11_sbuf__py_submit(&buf, py_retval());
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -38,9 +38,7 @@ static bool _py_slice__repr__(int argc, py_Ref argv) {
|
||||
if(i != 2) c11_sbuf__write_cstr(&buf, ", ");
|
||||
}
|
||||
c11_sbuf__write_char(&buf, ')');
|
||||
c11_string* res = c11_sbuf__submit(&buf);
|
||||
py_newstrn(py_retval(), res->data, res->size);
|
||||
c11_string__delete(res);
|
||||
c11_sbuf__py_submit(&buf, py_retval());
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -154,9 +154,7 @@ static bool _py_str__repr__(int argc, py_Ref argv) {
|
||||
c11_sbuf buf;
|
||||
c11_sbuf__ctor(&buf);
|
||||
c11_sbuf__write_quoted(&buf, py_tosv(&argv[0]), '\'');
|
||||
c11_string* res = c11_sbuf__submit(&buf);
|
||||
py_newstrn(py_retval(), res->data, res->size);
|
||||
c11_string__delete(res);
|
||||
c11_sbuf__py_submit(&buf, py_retval());
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -294,9 +292,7 @@ static bool _py_str__join(int argc, py_Ref argv) {
|
||||
c11_string* item = py_touserdata(&p[i]);
|
||||
c11_sbuf__write_cstrn(&buf, item->data, item->size);
|
||||
}
|
||||
c11_string* res = c11_sbuf__submit(&buf);
|
||||
py_newstrn(py_retval(), res->data, res->size);
|
||||
c11_string__delete(res);
|
||||
c11_sbuf__py_submit(&buf, py_retval());
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -391,9 +387,7 @@ static bool _py_str__zfill(int argc, py_Ref argv) {
|
||||
c11_sbuf__write_char(&buf, '0');
|
||||
}
|
||||
c11_sbuf__write_sv(&buf, self);
|
||||
c11_string* res = c11_sbuf__submit(&buf);
|
||||
py_newstrn(py_retval(), res->data, res->size);
|
||||
c11_string__delete(res);
|
||||
c11_sbuf__py_submit(&buf, py_retval());
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -429,9 +423,7 @@ static bool _py_str__widthjust_impl(bool left, int argc, py_Ref argv) {
|
||||
}
|
||||
c11_sbuf__write_sv(&buf, self);
|
||||
}
|
||||
c11_string* res = c11_sbuf__submit(&buf);
|
||||
py_newstrn(py_retval(), res->data, res->size);
|
||||
c11_string__delete(res);
|
||||
c11_sbuf__py_submit(&buf, py_retval());
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -44,9 +44,7 @@ static bool _py_tuple__repr__(int argc, py_Ref argv) {
|
||||
}
|
||||
if(length == 1) c11_sbuf__write_char(&buf, ',');
|
||||
c11_sbuf__write_char(&buf, ')');
|
||||
c11_string* res = c11_sbuf__submit(&buf);
|
||||
py_newstrn(py_retval(), res->data, res->size);
|
||||
c11_string__delete(res);
|
||||
c11_sbuf__py_submit(&buf, py_retval());
|
||||
return true;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user