This commit is contained in:
blueloveTH 2024-08-10 21:38:40 +08:00
parent 0f5ce54c66
commit c4c7b9ef25
11 changed files with 28 additions and 21 deletions

View File

@ -89,3 +89,7 @@ if(PK_USE_CJSON)
target_link_libraries(${PROJECT_NAME} PRIVATE cjson) target_link_libraries(${PROJECT_NAME} PRIVATE cjson)
endif() endif()
# link math library
if(UNIX)
target_link_libraries(${PROJECT_NAME} PRIVATE m)
endif()

View File

@ -22,7 +22,7 @@ SRC=$(find src/ -name "*.c")
echo "> Compiling and linking source files... " echo "> Compiling and linking source files... "
FLAGS="-std=c11 -O1 -Wfatal-errors -Iinclude" FLAGS="-std=c11 -O1 -Wfatal-errors -Iinclude -DNDEBUG"
if [[ "$OSTYPE" == "darwin"* ]]; then if [[ "$OSTYPE" == "darwin"* ]]; then
LIB_EXTENSION=".dylib" LIB_EXTENSION=".dylib"

View File

@ -18,17 +18,17 @@ assert config in ['Debug', 'Release', 'RelWithDebInfo']
os.chdir("build") os.chdir("build")
code = os.system(f"cmake .. -DPK_USE_CJSON=ON -DPK_ENABLE_OS=ON -DCMAKE_BUILD_TYPE={config}") code = os.system(f"cmake .. -DPK_ENABLE_OS=ON -DCMAKE_BUILD_TYPE={config}")
assert code == 0 assert code == 0
code = os.system(f"cmake --build . --config {config}") code = os.system(f"cmake --build . --config {config}")
assert code == 0 assert code == 0
if sys.platform == "win32": if sys.platform == "win32":
shutil.copy(f"{config}/main.exe", "../main.exe") shutil.copy(f"{config}/main.exe", "../main.exe")
shutil.copy(f"{config}/pocketpy.dll", "../pocketpy.dll") # shutil.copy(f"{config}/pocketpy.dll", "../pocketpy.dll")
elif sys.platform == "darwin": elif sys.platform == "darwin":
shutil.copy("main", "../main") shutil.copy("main", "../main")
shutil.copy("libpocketpy.dylib", "../libpocketpy.dylib") # shutil.copy("libpocketpy.dylib", "../libpocketpy.dylib")
else: else:
shutil.copy("main", "../main") shutil.copy("main", "../main")
shutil.copy("libpocketpy.so", "../libpocketpy.so") # shutil.copy("libpocketpy.so", "../libpocketpy.so")

View File

@ -460,7 +460,7 @@ bool py_call(py_Ref f, int argc, py_Ref argv) PY_RAISE;
/// This function does extra checks to help you debug `py_CFunction`. /// This function does extra checks to help you debug `py_CFunction`.
bool py_callcfunc(py_CFunction f, int argc, py_Ref argv) PY_RAISE; bool py_callcfunc(py_CFunction f, int argc, py_Ref argv) PY_RAISE;
#else #else
#define py_callcfunc(f, argc, argv) f(argc, argv) #define py_callcfunc(f, argc, argv) (f((argc), (argv)))
#endif #endif
/// Python equivalent to `str(val)`. /// Python equivalent to `str(val)`.

View File

@ -17,7 +17,7 @@ void py_Name__initialize() {
} }
c11_vector__ctor(&_r_interned, sizeof(c11_sv)); c11_vector__ctor(&_r_interned, sizeof(c11_sv));
#define MAGIC_METHOD(x) assert(x == py_name(#x)); #define MAGIC_METHOD(x) if(x != py_name(#x)) abort();
#include "pocketpy/xmacros/magics.h" #include "pocketpy/xmacros/magics.h"
#undef MAGIC_METHOD #undef MAGIC_METHOD
} }

View File

@ -17,7 +17,7 @@ static char* pk_default_import_file(const char* path) {
long size = ftell(f); long size = ftell(f);
fseek(f, 0, SEEK_SET); fseek(f, 0, SEEK_SET);
char* buffer = malloc(size + 1); char* buffer = malloc(size + 1);
fread(buffer, 1, size, f); size = fread(buffer, 1, size, f);
buffer[size] = 0; buffer[size] = 0;
fclose(f); fclose(f);
return buffer; return buffer;

View File

@ -91,18 +91,22 @@ bool py_call(py_Ref f, int argc, py_Ref argv) {
} }
} }
#if PK_DEBUG
bool py_callcfunc(py_CFunction f, int argc, py_Ref argv) { bool py_callcfunc(py_CFunction f, int argc, py_Ref argv) {
py_StackRef p0 = py_peek(0); py_StackRef p0 = py_peek(0);
py_newnil(py_retval()); py_newnil(py_retval());
bool ok = f(argc, argv); bool ok = f(argc, argv);
if(!ok) return false; if(!ok) return false;
if(py_peek(0) != p0) if(py_peek(0) != p0) {
c11__abort("py_CFunction corrupts the stack! Did you forget to call `py_pop()`?"); c11__abort("py_CFunction corrupts the stack! Did you forget to call `py_pop()`?");
if(py_isnil(py_retval())) }
if(py_isnil(py_retval())) {
c11__abort( c11__abort(
"py_CFunction returns nothing! Did you forget to call `py_newnone(py_retval())`?"); "py_CFunction returns nothing! Did you forget to call `py_newnone(py_retval())`?");
}
return true; return true;
} }
#endif
bool py_vectorcall(uint16_t argc, uint16_t kwargc) { bool py_vectorcall(uint16_t argc, uint16_t kwargc) {
return VM__vectorcall(pk_current_vm, argc, kwargc, false) != RES_ERROR; return VM__vectorcall(pk_current_vm, argc, kwargc, false) != RES_ERROR;

View File

@ -21,15 +21,14 @@ void py_setglobal(py_Name name, py_Ref val) { py_setdict(&pk_current_vm->main, n
py_Ref py_newmodule(const char* path) { py_Ref py_newmodule(const char* path) {
ManagedHeap* heap = &pk_current_vm->heap; ManagedHeap* heap = &pk_current_vm->heap;
PyObject* obj = ManagedHeap__new(heap, tp_module, -1, 0);
py_Ref r0 = py_pushtmp(); py_Ref r0 = py_pushtmp();
py_Ref r1 = py_pushtmp(); py_Ref r1 = py_pushtmp();
*r0 = (py_TValue){ *r0 = (py_TValue){
.type = obj->type, .type = tp_module,
.is_ptr = true, .is_ptr = true,
._obj = obj, ._obj = ManagedHeap__new(heap, tp_module, -1, 0),
}; };
int last_dot = c11_sv__rindex((c11_sv){path, strlen(path)}, '.'); int last_dot = c11_sv__rindex((c11_sv){path, strlen(path)}, '.');

View File

@ -21,6 +21,9 @@ py_Ref py_getdict(py_Ref self, py_Name name) {
void py_setdict(py_Ref self, py_Name name, py_Ref val) { void py_setdict(py_Ref self, py_Name name, py_Ref val) {
assert(self && self->is_ptr); assert(self && self->is_ptr);
// if(py_isidentical(self, &pk_current_vm->main)){
// printf("Setting main: %s\n", py_name2str(name));
// }
if(!py_ismagicname(name) || self->type != tp_type) { if(!py_ismagicname(name) || self->type != tp_type) {
NameDict__set(PyObject__dict(self->_obj), name, *val); NameDict__set(PyObject__dict(self->_obj), name, *val);
} else { } else {

View File

@ -41,7 +41,11 @@ int main(int argc, char** argv) {
if(argc == 1) { if(argc == 1) {
printf("pocketpy " PK_VERSION " (" __DATE__ ", " __TIME__ ") "); printf("pocketpy " PK_VERSION " (" __DATE__ ", " __TIME__ ") ");
printf("[%d bit] on %s\n", (int)(sizeof(void*) * 8), PY_SYS_PLATFORM_STRING); printf("[%d bit] on %s", (int)(sizeof(void*) * 8), PY_SYS_PLATFORM_STRING);
#if PK_DEBUG
printf(" (DEBUG)");
#endif
printf("\n");
printf("https://github.com/pocketpy/pocketpy\n"); printf("https://github.com/pocketpy/pocketpy\n");
printf("Type \"exit()\" to exit.\n"); printf("Type \"exit()\" to exit.\n");

View File

@ -1,7 +0,0 @@
from cmath import isclose, sqrt
res = sqrt(1+2j)
assert isclose(res, 1.272019649514069+0.7861513777574233j)
a = 1+2j
{a: 1}