Compare commits

...

4 Commits

Author SHA1 Message Date
blueloveTH
24f3656356 ... 2024-08-30 18:14:44 +08:00
blueloveTH
19563e33d2 ... 2024-08-30 16:03:32 +08:00
blueloveTH
5ad606859f add py_getvmctx 2024-08-30 16:01:22 +08:00
blueloveTH
108d2fe969 change to 0-based 2024-08-30 15:37:13 +08:00
10 changed files with 36 additions and 64 deletions

View File

@ -1,41 +0,0 @@
---
icon: package
label: line_profiler
---
!!!
This module is optional. Set `PK_ENABLE_PROFILER` to `1` to enable it.
!!!
## Example
```python
from line_profiler import LineProfiler
def my_func():
a = 0
for i in range(1000000):
a += i
return a
lp = LineProfiler()
lp.add_function(my_func)
lp.runcall(my_func)
lp.print_stats()
```
```txt
Total time: 0.243s
File: 84_line_profiler.py
Function: my_func at line 3
Line # Hits Time Per Hit % Time Line Contents
==============================================================
3 def my_func():
4 1 0 0 0.0 a = 0
5 1000001 69 0 28.4 for i in range(1000000):
6 1000001 174 0 71.6 a += i
7 1 0 0 0.0 return a
```

View File

@ -13,11 +13,6 @@
#define PK_ENABLE_OS 1
#endif
// Enable `line_profiler` module and `breakpoint()` function
#ifndef PK_ENABLE_PROFILER // can be overridden by cmake
#define PK_ENABLE_PROFILER 0
#endif
// GC min threshold
#ifndef PK_GC_MIN_THRESHOLD // can be overridden by cmake
#define PK_GC_MIN_THRESHOLD 16384
@ -45,10 +40,4 @@
#define PK_PLATFORM_SEP '\\'
#else
#define PK_PLATFORM_SEP '/'
#endif
#ifdef NDEBUG
#define PK_DEBUG 0
#else
#define PK_DEBUG 1
#endif

View File

@ -32,6 +32,7 @@ typedef struct VM {
bool is_curr_exc_handled; // handled by try-except block but not cleared yet
py_TValue reg[8]; // users' registers
void* ctx; // user-defined context
py_StackRef __curr_class;
py_StackRef __curr_function;

View File

@ -92,6 +92,10 @@ PK_EXPORT int py_currentvm();
PK_EXPORT void py_switchvm(int index);
/// Reset the current VM.
PK_EXPORT void py_resetvm();
/// Get the current VM context. This is used for user-defined data.
PK_EXPORT void* py_getvmctx();
/// Set the current VM context. This is used for user-defined data.
PK_EXPORT void py_setvmctx(void* ctx);
/// Set `sys.argv`. Used for storing command-line arguments.
PK_EXPORT void py_sys_setargv(int argc, char** argv);
/// Setup the callbacks for the current VM.
@ -115,13 +119,13 @@ PK_EXPORT bool py_eval(const char* source, py_Ref module) PY_RAISE PY_RETURN;
/// Example:
/// `py_newstr(py_r0(), "abc");`
/// `py_newint(py_r1(), 123);`
/// `py_smartexec("print(_1, _2)", NULL, py_r0(), py_r1());`
/// `py_smartexec("print(_0, _1)", NULL, py_r0(), py_r1());`
/// `// "abc 123" will be printed`.
PK_EXPORT bool py_smartexec(const char* source, py_Ref module, ...) PY_RAISE PY_RETURN;
/// Evaluate a source string with smart interpretation.
/// Example:
/// `py_newstr(py_r0(), "abc");`
/// `py_smarteval("len(_1)", NULL, py_r0());`
/// `py_smarteval("len(_)", NULL, py_r0());`
/// `int res = py_toint(py_retval());`
/// `// res will be 3`.
PK_EXPORT bool py_smarteval(const char* source, py_Ref module, ...) PY_RAISE PY_RETURN;
@ -229,6 +233,8 @@ PK_EXPORT py_f64 py_tofloat(py_Ref);
/// If successful, return true and set the value to `out`.
/// Otherwise, return false and raise `TypeError`.
PK_EXPORT bool py_castfloat(py_Ref, py_f64* out) PY_RAISE;
/// 32-bit version of `py_castfloat`.
PK_EXPORT bool py_castfloat32(py_Ref, float* out) PY_RAISE;
/// Cast a `int` object in python to `int64_t`.
PK_EXPORT bool py_castint(py_Ref, py_i64* out) PY_RAISE;
/// Convert a `bool` object in python to `bool`.
@ -544,7 +550,7 @@ PK_EXPORT bool py_isidentical(py_Ref, py_Ref);
/// The stack remains unchanged after the operation.
PK_EXPORT bool py_call(py_Ref f, int argc, py_Ref argv) PY_RAISE PY_RETURN;
#if PK_DEBUG
#ifndef NDEBUG
/// Call a `py_CFunction` in a safe way.
/// This function does extra checks to help you debug `py_CFunction`.
PK_EXPORT bool py_callcfunc(py_CFunction f, int argc, py_Ref argv) PY_RAISE PY_RETURN;

View File

@ -87,7 +87,7 @@ FrameResult VM__run_top_frame(VM* self) {
__NEXT_STEP:
byte = *frame->ip;
#if PK_DEBUG
#ifndef NDEBUG
pk_print_stack(self, frame, byte);
// assert(!py_checkexc(true));
#endif

View File

@ -71,6 +71,7 @@ void VM__ctor(VM* self) {
self->curr_exception = *py_NIL;
self->is_curr_exc_handled = false;
self->ctx = NULL;
self->__curr_class = NULL;
self->__curr_function = NULL;

View File

@ -24,6 +24,14 @@ bool py_castfloat(py_Ref self, double* out) {
}
}
bool py_castfloat32(py_Ref self, float *out){
switch(self->type) {
case tp_int: *out = (float)self->_i64; return true;
case tp_float: *out = (float)self->_f64; return true;
default: return TypeError("expected 'int' or 'float', got '%t'", self->type);
}
}
bool py_castint(py_Ref self, int64_t* out) {
if(self->type == tp_int) {
*out = self->_i64;

View File

@ -75,6 +75,14 @@ int py_currentvm() {
return -1;
}
void* py_getvmctx(){
return pk_current_vm->ctx;
}
void py_setvmctx(void* ctx){
pk_current_vm->ctx = ctx;
}
void py_sys_setargv(int argc, char** argv) {
py_GlobalRef sys = py_getmodule("sys");
py_Ref argv_list = py_getdict(sys, py_name("argv"));
@ -110,7 +118,7 @@ bool py_call(py_Ref f, int argc, py_Ref argv) {
}
}
#if PK_DEBUG
#ifndef NDEBUG
bool py_callcfunc(py_CFunction f, int argc, py_Ref argv) {
py_StackRef p0 = py_peek(0);
py_newnil(py_retval());

View File

@ -536,14 +536,14 @@ static bool
// [globals, locals, code]
CodeObject* co = py_touserdata(py_peek(-1));
py_StackRef locals = py_peek(-2);
int max_index = 0;
int max_index = -1;
c11__foreach(Bytecode, &co->codes, bc) {
if(bc->op == OP_LOAD_NAME) {
c11_sv name = py_name2sv(bc->arg);
if(name.data[0] != '_') continue;
int index;
if(name.size == 1) {
index = 1;
index = 0;
} else if(name.size == 2 && isdigit(name.data[1])) {
index = name.data[1] - '0';
} else {
@ -553,17 +553,17 @@ static bool
}
}
if(max_index == 0) { return ValueError("no placeholder found in the source"); }
if(max_index == -1) return ValueError("no placeholder found in the source");
for(int i = 1; i <= max_index; i++) {
for(int i = 0; i <= max_index; i++) {
py_Ref val = va_arg(args, py_Ref);
char buf[3];
buf[0] = '_';
buf[1] = '0' + i;
buf[2] = '\0';
py_dict_setitem_by_str(locals, buf, val);
if(i == 1) {
// _ => _1
if(i == 0) {
// _ => _0
py_dict_setitem_by_str(locals, "_", val);
}
}

View File

@ -43,7 +43,7 @@ int main(int argc, char** argv) {
if(argc == 1) {
printf("pocketpy " PK_VERSION " (" __DATE__ ", " __TIME__ ") ");
printf("[%d bit] on %s", (int)(sizeof(void*) * 8), PY_SYS_PLATFORM_STRING);
#if PK_DEBUG
#ifndef NDEBUG
printf(" (DEBUG)");
#endif
printf("\n");