Compare commits

...

7 Commits

Author SHA1 Message Date
blueloveTH
70e824a6b6 fix #371 2025-06-07 02:19:55 +08:00
blueloveTH
c1e4440a9f Update importlib.c 2025-06-07 01:55:36 +08:00
blueloveTH
96f4b9ea44 some fix 2025-06-07 01:52:57 +08:00
blueloveTH
ba058ba331 remove py_assign 2025-06-07 01:49:48 +08:00
blueloveTH
67296767c7 add gc_mark 2025-06-07 01:34:15 +08:00
blueloveTH
6de065899a Update name.h 2025-06-06 23:12:01 +08:00
blueloveTH
da6c2b35bb Update name.c 2025-06-06 22:56:56 +08:00
12 changed files with 45 additions and 15 deletions

View File

@ -2,7 +2,7 @@
#include "WebSocketChannel.h" #include "WebSocketChannel.h"
#include "libhv_bindings.hpp" #include "libhv_bindings.hpp"
#include "http/server/WebSocketServer.h" #include "http/server/WebSocketServer.h"
#include "pocketpy/pocketpy.h" #include "pocketpy.h"
struct libhv_HttpServer { struct libhv_HttpServer {
hv::HttpService http_service; hv::HttpService http_service;

View File

@ -1,6 +1,6 @@
#include "HttpMessage.h" #include "HttpMessage.h"
#include "libhv_bindings.hpp" #include "libhv_bindings.hpp"
#include "pocketpy/pocketpy.h" #include "pocketpy.h"
#include "http/client/WebSocketClient.h" #include "http/client/WebSocketClient.h"
struct libhv_WebSocketClient { struct libhv_WebSocketClient {

View File

@ -1,3 +1,7 @@
#pragma once #pragma once
#define PK_IS_PUBLIC_INCLUDE
#include "pocketpy/pocketpy.h" #include "pocketpy/pocketpy.h"
#undef PK_IS_PUBLIC_INCLUDE

View File

@ -2,6 +2,10 @@
#include "pocketpy/pocketpy.h" #include "pocketpy/pocketpy.h"
#ifdef __cplusplus
extern "C" {
#endif
void pk_names_initialize(); void pk_names_initialize();
void pk_names_finalize(); void pk_names_finalize();
@ -12,4 +16,8 @@ void pk_names_finalize();
py_Name py_namev(c11_sv name); py_Name py_namev(c11_sv name);
c11_sv py_name2sv(py_Name index); c11_sv py_name2sv(py_Name index);
py_Name py_name(const char* name); py_Name py_name(const char* name);
const char* py_name2str(py_Name index); const char* py_name2str(py_Name index);
#ifdef __cplusplus
} // extern "C"
#endif

View File

@ -28,6 +28,12 @@ typedef double py_f64;
/// A generic destructor function. /// A generic destructor function.
typedef void (*py_Dtor)(void*); typedef void (*py_Dtor)(void*);
#ifdef PK_IS_PUBLIC_INCLUDE
typedef struct py_TValue {
char _[16];
} py_TValue;
#endif
/// A string view type. It is helpful for passing strings which are not null-terminated. /// A string view type. It is helpful for passing strings which are not null-terminated.
typedef struct c11_sv { typedef struct c11_sv {
const char* data; const char* data;
@ -71,7 +77,9 @@ typedef struct py_Callbacks {
/// Flush the output buffer of `print`. /// Flush the output buffer of `print`.
void (*flush)(); void (*flush)();
/// Used by `input` to get a character. /// Used by `input` to get a character.
int (*getchar)(); int (*getchr)();
/// Used by `gc.collect()` to mark extra objects for garbage collection.
void (*gc_mark)(void (*f)(py_Ref val, void* ctx), void* ctx);
} py_Callbacks; } py_Callbacks;
/// Native function signature. /// Native function signature.
@ -370,8 +378,6 @@ PK_API void py_setglobal(py_Name name, py_Ref val);
/// Get variable in the `builtins` module. /// Get variable in the `builtins` module.
PK_API py_ItemRef py_getbuiltin(py_Name name); PK_API py_ItemRef py_getbuiltin(py_Name name);
/// Equivalent to `*dst = *src`.
PK_API void py_assign(py_Ref dst, py_Ref src);
/// Get the last return value. /// Get the last return value.
PK_API py_GlobalRef py_retval(); PK_API py_GlobalRef py_retval();
@ -447,8 +453,9 @@ PK_API void py_bindmagic(py_Type type, py_Name name, py_CFunction f);
#define PY_CHECK_ARG_TYPE(i, type) \ #define PY_CHECK_ARG_TYPE(i, type) \
if(!py_checktype(py_arg(i), type)) return false if(!py_checktype(py_arg(i), type)) return false
#define py_offset(p, i) ((py_Ref)((char*)p + ((i) << 4))) #define py_offset(p, i) ((p) + (i))
#define py_arg(i) py_offset(argv, i) #define py_arg(i) (&argv[i])
#define py_assign(dst, src) *(dst) = *(src)
/************* Python Equivalents *************/ /************* Python Equivalents *************/

View File

@ -2,6 +2,7 @@
#include "pocketpy/common/name.h" #include "pocketpy/common/name.h"
#include "pocketpy/common/str.h" #include "pocketpy/common/str.h"
#include "pocketpy/common/threads.h"
#include "pocketpy/pocketpy.h" #include "pocketpy/pocketpy.h"
#include <stdatomic.h> #include <stdatomic.h>
@ -43,7 +44,7 @@ void pk_names_finalize() {
py_Name py_namev(c11_sv name) { py_Name py_namev(c11_sv name) {
while(atomic_flag_test_and_set(&pk_string_table.lock)) { while(atomic_flag_test_and_set(&pk_string_table.lock)) {
// busy-wait until the lock is released c11_thrd_yield();
} }
uint64_t hash = c11_sv__hash(name); uint64_t hash = c11_sv__hash(name);
int index = hash & 0xFFFF; int index = hash & 0xFFFF;

View File

@ -32,6 +32,8 @@ static void pk_default_print(const char* data) { printf("%s", data); }
static void pk_default_flush() { fflush(stdout); } static void pk_default_flush() { fflush(stdout); }
static int pk_default_getchr() { return getchar(); }
void LineProfiler__tracefunc(py_Frame* frame, enum py_TraceEvent event) { void LineProfiler__tracefunc(py_Frame* frame, enum py_TraceEvent event) {
LineProfiler* self = &pk_current_vm->line_profiler; LineProfiler* self = &pk_current_vm->line_profiler;
if(self->enabled && event == TRACE_EVENT_LINE) { LineProfiler__tracefunc_line(self, frame); } if(self->enabled && event == TRACE_EVENT_LINE) { LineProfiler__tracefunc_line(self, frame); }
@ -75,7 +77,7 @@ void VM__ctor(VM* self) {
self->callbacks.importfile = pk_default_importfile; self->callbacks.importfile = pk_default_importfile;
self->callbacks.print = pk_default_print; self->callbacks.print = pk_default_print;
self->callbacks.flush = pk_default_flush; self->callbacks.flush = pk_default_flush;
self->callbacks.getchar = getchar; self->callbacks.getchr = pk_default_getchr;
self->last_retval = *py_NIL(); self->last_retval = *py_NIL();
self->curr_exception = *py_NIL(); self->curr_exception = *py_NIL();
@ -635,6 +637,11 @@ void CodeObject__gc_mark(const CodeObject* self, c11_vector* p_stack) {
} }
} }
static void pk__mark_value_func(py_Ref val, void* ctx) {
c11_vector* p_stack = ctx;
pk__mark_value(val);
}
void ManagedHeap__mark(ManagedHeap* self) { void ManagedHeap__mark(ManagedHeap* self) {
VM* vm = pk_current_vm; VM* vm = pk_current_vm;
c11_vector* p_stack = &self->gc_roots; c11_vector* p_stack = &self->gc_roots;
@ -666,6 +673,8 @@ void ManagedHeap__mark(ManagedHeap* self) {
for(int i = 0; i < c11__count_array(vm->reg); i++) { for(int i = 0; i < c11__count_array(vm->reg); i++) {
pk__mark_value(&vm->reg[i]); pk__mark_value(&vm->reg[i]);
} }
// mark user func
if(vm->callbacks.gc_mark) vm->callbacks.gc_mark(pk__mark_value_func, p_stack);
/*****************************/ /*****************************/
while(p_stack->length > 0) { while(p_stack->length > 0) {
PyObject* obj = c11_vector__back(PyObject*, p_stack); PyObject* obj = c11_vector__back(PyObject*, p_stack);
@ -803,7 +812,7 @@ int py_replinput(char* buf, int max_size) {
printf(">>> "); printf(">>> ");
while(true) { while(true) {
int c = pk_current_vm->callbacks.getchar(); int c = pk_current_vm->callbacks.getchr();
if(c == EOF) return -1; if(c == EOF) return -1;
if(c == '\n') { if(c == '\n') {

View File

@ -1,3 +1,4 @@
#include "pocketpy/objects/base.h"
#include "pocketpy/pocketpy.h" #include "pocketpy/pocketpy.h"
static bool importlib_reload(int argc, py_Ref argv) { static bool importlib_reload(int argc, py_Ref argv) {
@ -10,4 +11,4 @@ void pk__add_module_importlib() {
py_Ref mod = py_newmodule("importlib"); py_Ref mod = py_newmodule("importlib");
py_bindfunc(mod, "reload", importlib_reload); py_bindfunc(mod, "reload", importlib_reload);
} }

View File

@ -1,3 +1,4 @@
#include "pocketpy/objects/base.h"
#include "pocketpy/pocketpy.h" #include "pocketpy/pocketpy.h"
#include <stdlib.h> #include <stdlib.h>

View File

@ -1,4 +1,5 @@
#include "pocketpy/common/str.h" #include "pocketpy/common/str.h"
#include "pocketpy/objects/base.h"
#include "pocketpy/pocketpy.h" #include "pocketpy/pocketpy.h"
const static c11_u32_range kEastAsianWidthRanges[] = { const static c11_u32_range kEastAsianWidthRanges[] = {

View File

@ -205,7 +205,7 @@ static bool builtins_input(int argc, py_Ref argv) {
c11_sbuf buf; c11_sbuf buf;
c11_sbuf__ctor(&buf); c11_sbuf__ctor(&buf);
while(true) { while(true) {
int c = py_callbacks()->getchar(); int c = py_callbacks()->getchr();
if(c == '\n' || c == '\r') break; if(c == '\n' || c == '\r') break;
if(c == EOF) break; if(c == EOF) break;
c11_sbuf__write_char(&buf, c); c11_sbuf__write_char(&buf, c);

View File

@ -68,8 +68,6 @@ py_GlobalRef py_inspect_currentmodule() {
py_Frame* py_inspect_currentframe() { return pk_current_vm->top_frame; } py_Frame* py_inspect_currentframe() { return pk_current_vm->top_frame; }
void py_assign(py_Ref dst, py_Ref src) { *dst = *src; }
/* Stack References */ /* Stack References */
py_Ref py_peek(int i) { py_Ref py_peek(int i) {
assert(i <= 0); assert(i <= 0);