diff --git a/CMakeLists.txt b/CMakeLists.txt index 026634df..e0f64ce1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -89,3 +89,7 @@ if(PK_USE_CJSON) target_link_libraries(${PROJECT_NAME} PRIVATE cjson) endif() +# link math library +if(UNIX) + target_link_libraries(${PROJECT_NAME} PRIVATE m) +endif() \ No newline at end of file diff --git a/build.sh b/build.sh index a1387c71..a304428a 100644 --- a/build.sh +++ b/build.sh @@ -22,7 +22,7 @@ SRC=$(find src/ -name "*.c") 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 LIB_EXTENSION=".dylib" diff --git a/cmake_build.py b/cmake_build.py index 30fb58a1..e7c3f8ae 100644 --- a/cmake_build.py +++ b/cmake_build.py @@ -18,17 +18,17 @@ assert config in ['Debug', 'Release', 'RelWithDebInfo'] 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 code = os.system(f"cmake --build . --config {config}") assert code == 0 if sys.platform == "win32": 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": shutil.copy("main", "../main") - shutil.copy("libpocketpy.dylib", "../libpocketpy.dylib") + # shutil.copy("libpocketpy.dylib", "../libpocketpy.dylib") else: shutil.copy("main", "../main") - shutil.copy("libpocketpy.so", "../libpocketpy.so") + # shutil.copy("libpocketpy.so", "../libpocketpy.so") diff --git a/include/pocketpy/pocketpy.h b/include/pocketpy/pocketpy.h index dfd06ba1..3f97fea8 100644 --- a/include/pocketpy/pocketpy.h +++ b/include/pocketpy/pocketpy.h @@ -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`. bool py_callcfunc(py_CFunction f, int argc, py_Ref argv) PY_RAISE; #else -#define py_callcfunc(f, argc, argv) f(argc, argv) +#define py_callcfunc(f, argc, argv) (f((argc), (argv))) #endif /// Python equivalent to `str(val)`. diff --git a/src/common/strname.c b/src/common/strname.c index 42e52a1e..b0b270ad 100644 --- a/src/common/strname.c +++ b/src/common/strname.c @@ -17,7 +17,7 @@ void py_Name__initialize() { } 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" #undef MAGIC_METHOD } diff --git a/src/interpreter/vm.c b/src/interpreter/vm.c index 20a8a684..31fd7dcf 100644 --- a/src/interpreter/vm.c +++ b/src/interpreter/vm.c @@ -17,7 +17,7 @@ static char* pk_default_import_file(const char* path) { long size = ftell(f); fseek(f, 0, SEEK_SET); char* buffer = malloc(size + 1); - fread(buffer, 1, size, f); + size = fread(buffer, 1, size, f); buffer[size] = 0; fclose(f); return buffer; diff --git a/src/public/internal.c b/src/public/internal.c index a416ea44..9da074e3 100644 --- a/src/public/internal.c +++ b/src/public/internal.c @@ -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) { py_StackRef p0 = py_peek(0); py_newnil(py_retval()); bool ok = f(argc, argv); 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()`?"); - if(py_isnil(py_retval())) + } + if(py_isnil(py_retval())) { c11__abort( "py_CFunction returns nothing! Did you forget to call `py_newnone(py_retval())`?"); + } return true; } +#endif bool py_vectorcall(uint16_t argc, uint16_t kwargc) { return VM__vectorcall(pk_current_vm, argc, kwargc, false) != RES_ERROR; diff --git a/src/public/modules.c b/src/public/modules.c index 574e9355..df580715 100644 --- a/src/public/modules.c +++ b/src/public/modules.c @@ -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) { ManagedHeap* heap = &pk_current_vm->heap; - PyObject* obj = ManagedHeap__new(heap, tp_module, -1, 0); py_Ref r0 = py_pushtmp(); py_Ref r1 = py_pushtmp(); *r0 = (py_TValue){ - .type = obj->type, + .type = tp_module, .is_ptr = true, - ._obj = obj, + ._obj = ManagedHeap__new(heap, tp_module, -1, 0), }; int last_dot = c11_sv__rindex((c11_sv){path, strlen(path)}, '.'); diff --git a/src/public/stack_ops.c b/src/public/stack_ops.c index 7bb0c9cb..49539a02 100644 --- a/src/public/stack_ops.c +++ b/src/public/stack_ops.c @@ -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) { 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) { NameDict__set(PyObject__dict(self->_obj), name, *val); } else { diff --git a/src2/main.c b/src2/main.c index 094950a2..39d8f381 100644 --- a/src2/main.c +++ b/src2/main.c @@ -41,7 +41,11 @@ int main(int argc, char** argv) { if(argc == 1) { 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("Type \"exit()\" to exit.\n"); diff --git a/tests/00_tmp.py b/tests/00_tmp.py deleted file mode 100644 index 05f0b0cb..00000000 --- a/tests/00_tmp.py +++ /dev/null @@ -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} \ No newline at end of file