mirror of
https://github.com/pocketpy/pocketpy
synced 2026-03-25 06:30:17 +00:00
Compare commits
No commits in common. "054987a241c9d3edfd05ad200dfd44727c2bee00" and "332a51945ce0aadddd39e5ed374c18a0a70087d8" have entirely different histories.
054987a241
...
332a51945c
@ -68,8 +68,6 @@ typedef struct py_Callbacks {
|
|||||||
char* (*importfile)(const char*);
|
char* (*importfile)(const char*);
|
||||||
/// Used by `print` to output a string.
|
/// Used by `print` to output a string.
|
||||||
void (*print)(const char*);
|
void (*print)(const char*);
|
||||||
/// Flush the output buffer of `print`.
|
|
||||||
void (*flush)();
|
|
||||||
/// Used by `input` to get a character.
|
/// Used by `input` to get a character.
|
||||||
int (*getchar)();
|
int (*getchar)();
|
||||||
} py_Callbacks;
|
} py_Callbacks;
|
||||||
|
|||||||
@ -18,9 +18,6 @@ def memory_usage() -> str:
|
|||||||
def is_user_defined_type(t: type) -> bool:
|
def is_user_defined_type(t: type) -> bool:
|
||||||
"""Check if a type is user-defined. This means the type was created by executing python `class` statement."""
|
"""Check if a type is user-defined. This means the type was created by executing python `class` statement."""
|
||||||
|
|
||||||
def enable_full_buffering_mode() -> None:
|
|
||||||
"""Enable full buffering mode for ASCII drawings."""
|
|
||||||
|
|
||||||
def currentvm() -> int:
|
def currentvm() -> int:
|
||||||
"""Return the current VM index."""
|
"""Return the current VM index."""
|
||||||
|
|
||||||
|
|||||||
@ -29,8 +29,6 @@ static char* pk_default_importfile(const char* path) {
|
|||||||
|
|
||||||
static void pk_default_print(const char* data) { printf("%s", data); }
|
static void pk_default_print(const char* data) { printf("%s", data); }
|
||||||
|
|
||||||
static void pk_default_flush() { fflush(stdout); }
|
|
||||||
|
|
||||||
static void py_TypeInfo__ctor(py_TypeInfo* self,
|
static void py_TypeInfo__ctor(py_TypeInfo* self,
|
||||||
py_Name name,
|
py_Name name,
|
||||||
py_Type index,
|
py_Type index,
|
||||||
@ -69,7 +67,6 @@ 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.getchar = getchar;
|
self->callbacks.getchar = getchar;
|
||||||
|
|
||||||
self->last_retval = *py_NIL();
|
self->last_retval = *py_NIL();
|
||||||
|
|||||||
@ -65,14 +65,6 @@ static bool pkpy_is_user_defined_type(int argc, py_Ref argv) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool pkpy_enable_full_buffering_mode(int argc, py_Ref argv) {
|
|
||||||
PY_CHECK_ARGC(0);
|
|
||||||
static char buf[1024 * 1024 * 1];
|
|
||||||
setvbuf(stdout, buf, _IOFBF, sizeof(buf));
|
|
||||||
py_newnone(py_retval());
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool pkpy_currentvm(int argc, py_Ref argv) {
|
static bool pkpy_currentvm(int argc, py_Ref argv) {
|
||||||
PY_CHECK_ARGC(0);
|
PY_CHECK_ARGC(0);
|
||||||
py_newint(py_retval(), py_currentvm());
|
py_newint(py_retval(), py_currentvm());
|
||||||
@ -419,9 +411,7 @@ static void pk_ComputeThread__register(py_Ref mod) {
|
|||||||
|
|
||||||
py_bindmethod(type, "submit_exec", ComputeThread_submit_exec);
|
py_bindmethod(type, "submit_exec", ComputeThread_submit_exec);
|
||||||
py_bindmethod(type, "submit_eval", ComputeThread_submit_eval);
|
py_bindmethod(type, "submit_eval", ComputeThread_submit_eval);
|
||||||
py_bind(py_tpobject(type),
|
py_bind(py_tpobject(type), "submit_call(self, eval_src, *args, **kwargs)", ComputeThread_submit_call);
|
||||||
"submit_call(self, eval_src, *args, **kwargs)",
|
|
||||||
ComputeThread_submit_call);
|
|
||||||
|
|
||||||
py_bindmethod(type, "exec", ComputeThread_exec);
|
py_bindmethod(type, "exec", ComputeThread_exec);
|
||||||
py_bindmethod(type, "eval", ComputeThread_eval);
|
py_bindmethod(type, "eval", ComputeThread_eval);
|
||||||
@ -463,7 +453,6 @@ void pk__add_module_pkpy() {
|
|||||||
|
|
||||||
py_bindfunc(mod, "memory_usage", pkpy_memory_usage);
|
py_bindfunc(mod, "memory_usage", pkpy_memory_usage);
|
||||||
py_bindfunc(mod, "is_user_defined_type", pkpy_is_user_defined_type);
|
py_bindfunc(mod, "is_user_defined_type", pkpy_is_user_defined_type);
|
||||||
py_bindfunc(mod, "enable_full_buffering_mode", pkpy_enable_full_buffering_mode);
|
|
||||||
|
|
||||||
py_bindfunc(mod, "currentvm", pkpy_currentvm);
|
py_bindfunc(mod, "currentvm", pkpy_currentvm);
|
||||||
|
|
||||||
|
|||||||
@ -200,12 +200,12 @@ static bool builtins_input(int argc, py_Ref argv) {
|
|||||||
if(!py_checkstr(argv)) return false;
|
if(!py_checkstr(argv)) return false;
|
||||||
prompt = py_tostr(argv);
|
prompt = py_tostr(argv);
|
||||||
}
|
}
|
||||||
py_callbacks()->print(prompt);
|
pk_current_vm->callbacks.print(prompt);
|
||||||
|
|
||||||
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 = pk_current_vm->callbacks.getchar();
|
||||||
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);
|
||||||
@ -323,15 +323,11 @@ static bool builtins_round(int argc, py_Ref argv) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static bool builtins_print(int argc, py_Ref argv) {
|
static bool builtins_print(int argc, py_Ref argv) {
|
||||||
// print(*args, sep=' ', end='\n', flush=False)
|
// print(*args, sep=' ', end='\n')
|
||||||
py_TValue* args = py_tuple_data(argv);
|
py_TValue* args = py_tuple_data(argv);
|
||||||
int length = py_tuple_len(argv);
|
int length = py_tuple_len(argv);
|
||||||
PY_CHECK_ARG_TYPE(1, tp_str);
|
|
||||||
PY_CHECK_ARG_TYPE(2, tp_str);
|
|
||||||
PY_CHECK_ARG_TYPE(3, tp_bool);
|
|
||||||
c11_sv sep = py_tosv(py_arg(1));
|
c11_sv sep = py_tosv(py_arg(1));
|
||||||
c11_sv end = py_tosv(py_arg(2));
|
c11_sv end = py_tosv(py_arg(2));
|
||||||
bool flush = py_tobool(py_arg(3));
|
|
||||||
c11_sbuf buf;
|
c11_sbuf buf;
|
||||||
c11_sbuf__ctor(&buf);
|
c11_sbuf__ctor(&buf);
|
||||||
for(int i = 0; i < length; i++) {
|
for(int i = 0; i < length; i++) {
|
||||||
@ -344,8 +340,7 @@ static bool builtins_print(int argc, py_Ref argv) {
|
|||||||
}
|
}
|
||||||
c11_sbuf__write_sv(&buf, end);
|
c11_sbuf__write_sv(&buf, end);
|
||||||
c11_string* res = c11_sbuf__submit(&buf);
|
c11_string* res = c11_sbuf__submit(&buf);
|
||||||
py_callbacks()->print(res->data);
|
pk_current_vm->callbacks.print(res->data);
|
||||||
if(flush) py_callbacks()->flush();
|
|
||||||
c11_string__delete(res);
|
c11_string__delete(res);
|
||||||
py_newnone(py_retval());
|
py_newnone(py_retval());
|
||||||
return true;
|
return true;
|
||||||
@ -727,7 +722,7 @@ py_TValue pk_builtins__register() {
|
|||||||
py_bindfunc(builtins, "divmod", builtins_divmod);
|
py_bindfunc(builtins, "divmod", builtins_divmod);
|
||||||
py_bindfunc(builtins, "round", builtins_round);
|
py_bindfunc(builtins, "round", builtins_round);
|
||||||
|
|
||||||
py_bind(builtins, "print(*args, sep=' ', end='\\n', flush=False)", builtins_print);
|
py_bind(builtins, "print(*args, sep=' ', end='\\n')", builtins_print);
|
||||||
|
|
||||||
py_bindfunc(builtins, "isinstance", builtins_isinstance);
|
py_bindfunc(builtins, "isinstance", builtins_isinstance);
|
||||||
py_bindfunc(builtins, "issubclass", builtins_issubclass);
|
py_bindfunc(builtins, "issubclass", builtins_issubclass);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user