From ee37607a188607f772696d7ca97e57a5ecd3df8d Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sat, 2 Dec 2023 13:47:01 +0800 Subject: [PATCH] update examples --- examples/abort-the-vm/build.sh | 1 + examples/abort-the-vm/main.cpp | 54 ++++++++++++ examples/abort-the-vm/user_config.h | 84 +++++++++++++++++++ examples/{ => simple}/1_type_cast.cpp | 0 .../{ => simple}/2_module_and_binding.cpp | 0 examples/{ => simple}/3_list_operation.cpp | 0 .../{ => simple}/4_dictionary_operation.cpp | 0 examples/{ => simple}/5_python_scripting.cpp | 0 examples/{ => simple}/6_type_checker.cpp | 0 9 files changed, 139 insertions(+) create mode 100644 examples/abort-the-vm/build.sh create mode 100644 examples/abort-the-vm/main.cpp create mode 100644 examples/abort-the-vm/user_config.h rename examples/{ => simple}/1_type_cast.cpp (100%) rename examples/{ => simple}/2_module_and_binding.cpp (100%) rename examples/{ => simple}/3_list_operation.cpp (100%) rename examples/{ => simple}/4_dictionary_operation.cpp (100%) rename examples/{ => simple}/5_python_scripting.cpp (100%) rename examples/{ => simple}/6_type_checker.cpp (100%) diff --git a/examples/abort-the-vm/build.sh b/examples/abort-the-vm/build.sh new file mode 100644 index 00000000..be74cc92 --- /dev/null +++ b/examples/abort-the-vm/build.sh @@ -0,0 +1 @@ +g++ -o main -O1 main.cpp -std=c++17 -pthread diff --git a/examples/abort-the-vm/main.cpp b/examples/abort-the-vm/main.cpp new file mode 100644 index 00000000..bb486831 --- /dev/null +++ b/examples/abort-the-vm/main.cpp @@ -0,0 +1,54 @@ +// custom config to enable `vm->_ceval_on_step` and handle KeyboardInterrupt +#define PK_USER_CONFIG_H +#include "pocketpy.h" + +#include +#include +#include +#include + +using namespace pkpy; + +class MyVM: public VM{ +public: + std::atomic _flag = false; + + MyVM(): VM(){ + this->_ceval_on_step = [](VM* _vm, Frame* frame, Bytecode bc){ + MyVM* vm = (MyVM*)_vm; + if(vm->_flag){ + vm->_flag = false; + vm->KeyboardInterrupt(); + } + }; + } + void KeyboardInterrupt(){ + _error("KeyboardInterrupt", ""); + } +}; + +MyVM* vm; + +void set_the_flag_handler(int _){ + vm->_flag = true; +} + +int main(){ + signal(SIGINT, set_the_flag_handler); + + vm = new MyVM(); + REPL* repl = new REPL(vm); + bool need_more_lines = false; + while(true){ + std::cout << (need_more_lines ? "... " : ">>> "); + std::string line; + std::getline(std::cin, line); + vm->_flag = false; // reset the flag before each input + + // here I use linux signal to interrupt the vm + // you can run this line in a thread for more flexibility + need_more_lines = repl->input(line); + } + delete vm; + return 0; +} diff --git a/examples/abort-the-vm/user_config.h b/examples/abort-the-vm/user_config.h new file mode 100644 index 00000000..e136833c --- /dev/null +++ b/examples/abort-the-vm/user_config.h @@ -0,0 +1,84 @@ +#pragma once + +/*************** feature settings ***************/ + +// Whether to compile os-related modules or not +#ifndef PK_ENABLE_OS // can be overrided by cmake +#define PK_ENABLE_OS 1 +#endif + +// Enable this if you are working with multi-threading (experimental) +// This triggers necessary locks to make the VM thread-safe +#ifndef PK_ENABLE_THREAD // can be overrided by cmake +#define PK_ENABLE_THREAD 0 +#endif + +// Enable this for `vm->_ceval_on_step` +#define PK_ENABLE_CEVAL_CALLBACK 1 + +// Whether to use `std::function` to do bindings or not +// By default, functions to be binded must be a C function pointer without capture +// However, someone thinks it's not convenient. +// By setting this to 1, capturing lambdas can be binded, +// but it's slower and may cause severe "code bloat", also needs more time to compile. +#define PK_ENABLE_STD_FUNCTION 0 + +/*************** debug settings ***************/ + +// Enable this may help you find bugs +#define PK_DEBUG_EXTRA_CHECK 0 + +// Do not edit the following settings unless you know what you are doing +#define PK_DEBUG_NO_BUILTINS 0 +#define PK_DEBUG_DIS_EXEC 0 +#define PK_DEBUG_CEVAL_STEP 0 +#define PK_DEBUG_FULL_EXCEPTION 0 +#define PK_DEBUG_MEMORY_POOL 0 +#define PK_DEBUG_NO_MEMORY_POOL 0 +#define PK_DEBUG_NO_AUTO_GC 0 +#define PK_DEBUG_GC_STATS 0 + +/*************** internal settings ***************/ + +// This is the maximum size of the value stack in void* units +// The actual size in bytes equals `sizeof(void*) * PK_VM_STACK_SIZE` +#define PK_VM_STACK_SIZE 32768 + +// This is the maximum number of arguments in a function declaration +// including positional arguments, keyword-only arguments, and varargs +// (not recommended to change this / it should be less than 200) +#define PK_MAX_CO_VARNAMES 32 + +namespace pkpy{ + // Hash table load factor (smaller ones mean less collision but more memory) + // For class instance + inline const float kInstAttrLoadFactor = 0.67f; + // For class itself + inline const float kTypeAttrLoadFactor = 0.5f; + + #ifdef _WIN32 + inline const char kPlatformSep = '\\'; + #else + inline const char kPlatformSep = '/'; + #endif +} + +#ifdef _MSC_VER +#pragma warning (disable:4267) +#pragma warning (disable:4100) +#pragma warning (disable:4244) +#pragma warning (disable:4996) +#endif + +#ifdef _MSC_VER +#define PK_ENABLE_COMPUTED_GOTO 0 +#define UNREACHABLE() __assume(0) +#else +#define PK_ENABLE_COMPUTED_GOTO 1 +#define UNREACHABLE() __builtin_unreachable() +#endif + + +#if PK_DEBUG_CEVAL_STEP && defined(PK_ENABLE_COMPUTED_GOTO) +#undef PK_ENABLE_COMPUTED_GOTO +#endif diff --git a/examples/1_type_cast.cpp b/examples/simple/1_type_cast.cpp similarity index 100% rename from examples/1_type_cast.cpp rename to examples/simple/1_type_cast.cpp diff --git a/examples/2_module_and_binding.cpp b/examples/simple/2_module_and_binding.cpp similarity index 100% rename from examples/2_module_and_binding.cpp rename to examples/simple/2_module_and_binding.cpp diff --git a/examples/3_list_operation.cpp b/examples/simple/3_list_operation.cpp similarity index 100% rename from examples/3_list_operation.cpp rename to examples/simple/3_list_operation.cpp diff --git a/examples/4_dictionary_operation.cpp b/examples/simple/4_dictionary_operation.cpp similarity index 100% rename from examples/4_dictionary_operation.cpp rename to examples/simple/4_dictionary_operation.cpp diff --git a/examples/5_python_scripting.cpp b/examples/simple/5_python_scripting.cpp similarity index 100% rename from examples/5_python_scripting.cpp rename to examples/simple/5_python_scripting.cpp diff --git a/examples/6_type_checker.cpp b/examples/simple/6_type_checker.cpp similarity index 100% rename from examples/6_type_checker.cpp rename to examples/simple/6_type_checker.cpp