diff --git a/.gitignore b/.gitignore index a03dcf1..ad4251a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ .cache -/build +/build* diff --git a/.vscode/launch.json b/.vscode/launch.json index 042ac30..2580e18 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -11,6 +11,14 @@ "program": "${command:cmake.launchTargetPath}", "args": [], "cwd": "${workspaceFolder}", + }, + { + "type": "lldb", + "request": "launch", + "name": "Debug (WASM)", + "program": "wasmtime", + "args": ["run", "-g", "${command:cmake.launchTargetPath}"], + "cwd": "${workspaceFolder}", } ] } diff --git a/CMakeLists.txt b/CMakeLists.txt index b43dda8..6280a4e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,6 +4,7 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON) set(CMAKE_CXX_STANDARD 20) option(BUILD_WITH_MODULES "Build with C++ Modules" ON) +option(BUILD_WITH_EXCEPTIONS "Build with C++ Exceptions" ON) if(BUILD_WITH_MODULES) if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") @@ -23,7 +24,10 @@ if(BUILD_WITH_MODULES) set(CMAKE_CXX_EXTENSIONS OFF) endif() +if(NOT BUILD_WITH_EXCEPTIONS) + add_compile_options(-fno-exceptions) +endif() +set(CMAKE_EXECUTABLE_SUFFIX ${EXECUTABLE_SUFFIX}) add_subdirectory(src) - diff --git a/arch-webassembly-toolchain.cmake b/arch-webassembly-toolchain.cmake new file mode 100644 index 0000000..56ad1e3 --- /dev/null +++ b/arch-webassembly-toolchain.cmake @@ -0,0 +1,14 @@ +set(CMAKE_SYSTEM_NAME Generic) +set(CMAKE_SYSTEM_VERSION 1) +set(CMAKE_SYSTEM_PROCESSOR wasm32) +set(triple wasm32-wasi) +set(EXECUTABLE_SUFFIX .wasm) + +set(CMAKE_C_COMPILER clang) +set(CMAKE_CXX_COMPILER clang++) +set(CMAKE_C_COMPILER_TARGET ${triple}) +set(CMAKE_CXX_COMPILER_TARGET ${triple}) + +set(CMAKE_SYSROOT /usr/share/wasi-sysroot) +set(BUILD_WITH_EXCEPTIONS false) +add_compile_options(-fexperimental-library) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ae2812e..ffcc1e7 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -11,9 +11,12 @@ add_executable(inheritance inheritance.cc) add_executable(enumerations enumerations.cc) add_executable(namespaces namespaces.cc) add_executable(FunctionArguments FunctionArguments.cc) -add_executable(exceptions exceptions.cc) +if (BUILD_WITH_EXCEPTIONS) + add_executable(exceptions exceptions.cc) +endif() add_executable(HeapMemory HeapMemory.cc) add_executable(AbstractClasses AbstractClasses.cc) add_executable(vector vector.cc) add_executable(Vector2 Vector2.cc) add_executable(TemplateVector TemplateVector.cc) +add_executable(buffer buffer.cc) diff --git a/src/TemplateVector.cc b/src/TemplateVector.cc index e87e353..63f0ab0 100644 --- a/src/TemplateVector.cc +++ b/src/TemplateVector.cc @@ -2,6 +2,7 @@ #include #include #include + template class Vector { private: @@ -22,11 +23,11 @@ public: T& operator[](int i) { if (i < 0 || size() <= i) { - #if __cpp_exceptions == 199711 +#if __cpp_exceptions == 199711 throw std::out_of_range { "Vector::operator[]" }; - #else +#else std::exit(1); - #endif +#endif } return elem[i]; diff --git a/src/buffer.cc b/src/buffer.cc new file mode 100644 index 0000000..5a22328 --- /dev/null +++ b/src/buffer.cc @@ -0,0 +1,60 @@ +#include +#include +#include +#include + +template +class Buffer { +public: + constexpr int size() + { + return S; + } + + T& operator[](int i) + { + if (i < 0 || size() <= i) { +#if __cpp_exceptions == 199711 + throw std::out_of_range { "Vector::operator[]" }; +#else + std::exit(1); +#endif + } + return elem[i]; + } + + T* begin() + { + return &elem[0]; + } + + T* end() + { + return &elem[0] + size(); + } + +private: + T elem[S]; +}; + +int main() +{ + Buffer doubleBuffer; + Buffer stringBuffer; + + for (int i = 0; i < doubleBuffer.size(); i++) { + doubleBuffer[i] = i; + } + + for (int i = 0; i < stringBuffer.size(); i++) { + stringBuffer[i] = std::format("this is element {}", i); + } + + for (double i : doubleBuffer) { + std::cout << i << std::endl; + } + + for (std::string i : stringBuffer) { + std::cout << i << std::endl; + } +} diff --git a/src/vector.cc b/src/vector.cc index ec2d1e3..5e2caf1 100644 --- a/src/vector.cc +++ b/src/vector.cc @@ -22,14 +22,15 @@ public: : elem(new double[a.sz]) , sz { a.sz } { - for (int i = 0; i!=sz;++i) { + for (int i = 0; i != sz; ++i) { elem[i] = a.elem[i]; } } - Vector& operator=(const Vector& a) { + Vector& operator=(const Vector& a) + { double* p = new double[a.sz]; - for (int i =0; i!=sz; ++i) { + for (int i = 0; i != sz; ++i) { p[i] = a.elem[i]; } delete[] elem; @@ -40,16 +41,24 @@ public: double& operator[](int i) { - if (i < 0 || i > size() - 1) { + if (i < 0 || size() <= i) { +#if __cpp_exceptions == 199711 throw std::out_of_range { "Vector::operator[]" }; +#else + std::exit(1); +#endif } return elem[i]; } void print_element(int i) { - if (i < 0 || i > size() - 1) { + if (i < 0 || size() <= i) { +#if __cpp_exceptions == 199711 throw std::out_of_range { "Vector::print_element(int i)" }; +#else + std::exit(1); +#endif } std::cout << std::format("element[{}] : {}", i, elem[i]) << std::endl; }