Compare commits

..

No commits in common. "205f6ff2256f704854bff9a51bb86b64ad0473d6" and "8eff9abfbc8681785ca99418098ea409a1cbfd07" have entirely different histories.

16 changed files with 82 additions and 61 deletions

View File

@ -13,6 +13,12 @@
#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
@ -50,3 +56,9 @@
#else
#define PK_PLATFORM_SEP '/'
#endif
#if PK_ENABLE_THREAD
#define PK_THREAD_LOCAL thread_local
#else
#define PK_THREAD_LOCAL static
#endif

View File

@ -30,7 +30,6 @@ 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, ...);

View File

@ -47,15 +47,10 @@ extern py_GlobalRef py_None;
extern py_GlobalRef py_NIL;
/************* Global Setup *************/
/// Initialize pocketpy and the default VM.
/// Initialize the VM.
void py_initialize();
/// Finalize pocketpy.
/// Finalize the VM.
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.

View File

@ -238,47 +238,59 @@ static int FixedMemoryPool__total_bytes(FixedMemoryPool* self) {
return self->BlockCount * self->BlockSize;
}
static FixedMemoryPool PoolExpr;
static FixedMemoryPool PoolFrame;
static MemoryPool PoolObject;
PK_THREAD_LOCAL FixedMemoryPool PoolExpr;
PK_THREAD_LOCAL FixedMemoryPool PoolFrame;
PK_THREAD_LOCAL MemoryPool PoolObject;
PK_THREAD_LOCAL bool _Pools_initialized = false;
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);
}

View File

@ -147,12 +147,6 @@ 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;

View File

@ -502,7 +502,9 @@ pk_FrameResult pk_VM__run_top_frame(pk_VM* self) {
c11_sbuf__write_sv(&ss, py_tosv(&self->last_retval));
}
SP() = begin;
c11_sbuf__py_submit(&ss, SP()++);
c11_string* res = c11_sbuf__submit(&ss);
py_newstrn(SP()++, res->data, res->size);
c11_string__delete(res);
DISPATCH();
}
/*****************************/
@ -1107,6 +1109,8 @@ static bool format_object(py_Ref val, c11_sv spec) {
}
c11_string__delete(body);
c11_sbuf__py_submit(&buf, py_retval());
c11_string* res = c11_sbuf__submit(&buf);
py_newstrn(py_retval(), res->data, res->size);
c11_string__delete(res);
return true;
}

View File

@ -17,13 +17,11 @@ 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_all_vm[0] = &pk_default_vm;
pk_current_vm = &pk_default_vm;
// initialize some convenient references
static py_TValue _True, _False, _None, _NIL;
@ -39,35 +37,12 @@ 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,

View File

@ -109,8 +109,10 @@ static bool _py_builtins__hex(int argc, py_Ref argv) {
c11_sbuf__write_hex(&ss, cpnt, non_zero);
if(cpnt != 0) non_zero = false;
}
c11_sbuf__py_submit(&ss, py_retval());
// return VAR(ss.str());
c11_string* res = c11_sbuf__submit(&ss);
py_newstrn(py_retval(), res->data, res->size);
c11_string__delete(res);
return true;
}

View File

@ -288,7 +288,9 @@ static bool _py_dict__repr__(int argc, py_Ref argv) {
is_first = false;
}
c11_sbuf__write_char(&buf, '}');
c11_sbuf__py_submit(&buf, py_retval());
c11_string* res = c11_sbuf__submit(&buf);
py_newstrn(py_retval(), res->data, res->size);
c11_string__delete(res);
return true;
}

View File

@ -78,7 +78,9 @@ 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_sbuf__py_submit(&ss, py_retval());
c11_string* res = c11_sbuf__submit(&ss);
py_newstrn(py_retval(), res->data, res->size);
c11_string__delete(res);
return true;
}
@ -90,7 +92,9 @@ 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_sbuf__py_submit(&ss, py_retval());
c11_string* res = c11_sbuf__submit(&ss);
py_newstrn(py_retval(), res->data, res->size);
c11_string__delete(res);
return true;
}
@ -168,8 +172,10 @@ 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();
c11_sbuf__py_submit(&buf, message);
py_newstrn(message, res->data, res->size);
c11_string__delete(res);
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);

View File

@ -239,7 +239,9 @@ 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_sbuf__py_submit(&buf, py_retval());
c11_string* res = c11_sbuf__submit(&buf);
py_newstrn(py_retval(), res->data, res->size);
c11_string__delete(res);
return true;
}

View File

@ -201,7 +201,9 @@ 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_sbuf__py_submit(&buf, py_retval());
c11_string* res = c11_sbuf__submit(&buf);
py_newstrn(py_retval(), res->data, res->size);
c11_string__delete(res);
return true;
}

View File

@ -40,7 +40,9 @@ 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_sbuf__py_submit(&buf, py_retval());
c11_string* res = c11_sbuf__submit(&buf);
py_newstrn(py_retval(), res->data, res->size);
c11_string__delete(res);
return true;
}
@ -49,7 +51,9 @@ 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_sbuf__py_submit(&buf, py_retval());
c11_string* res = c11_sbuf__submit(&buf);
py_newstrn(py_retval(), res->data, res->size);
c11_string__delete(res);
return true;
}

View File

@ -38,7 +38,9 @@ static bool _py_slice__repr__(int argc, py_Ref argv) {
if(i != 2) c11_sbuf__write_cstr(&buf, ", ");
}
c11_sbuf__write_char(&buf, ')');
c11_sbuf__py_submit(&buf, py_retval());
c11_string* res = c11_sbuf__submit(&buf);
py_newstrn(py_retval(), res->data, res->size);
c11_string__delete(res);
return true;
}

View File

@ -154,7 +154,9 @@ 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_sbuf__py_submit(&buf, py_retval());
c11_string* res = c11_sbuf__submit(&buf);
py_newstrn(py_retval(), res->data, res->size);
c11_string__delete(res);
return true;
}
@ -292,7 +294,9 @@ 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_sbuf__py_submit(&buf, py_retval());
c11_string* res = c11_sbuf__submit(&buf);
py_newstrn(py_retval(), res->data, res->size);
c11_string__delete(res);
return true;
}
@ -387,7 +391,9 @@ static bool _py_str__zfill(int argc, py_Ref argv) {
c11_sbuf__write_char(&buf, '0');
}
c11_sbuf__write_sv(&buf, self);
c11_sbuf__py_submit(&buf, py_retval());
c11_string* res = c11_sbuf__submit(&buf);
py_newstrn(py_retval(), res->data, res->size);
c11_string__delete(res);
return true;
}
@ -423,7 +429,9 @@ static bool _py_str__widthjust_impl(bool left, int argc, py_Ref argv) {
}
c11_sbuf__write_sv(&buf, self);
}
c11_sbuf__py_submit(&buf, py_retval());
c11_string* res = c11_sbuf__submit(&buf);
py_newstrn(py_retval(), res->data, res->size);
c11_string__delete(res);
return true;
}

View File

@ -44,7 +44,9 @@ static bool _py_tuple__repr__(int argc, py_Ref argv) {
}
if(length == 1) c11_sbuf__write_char(&buf, ',');
c11_sbuf__write_char(&buf, ')');
c11_sbuf__py_submit(&buf, py_retval());
c11_string* res = c11_sbuf__submit(&buf);
py_newstrn(py_retval(), res->data, res->size);
c11_string__delete(res);
return true;
}