diff --git a/include/pocketpy/common.h b/include/pocketpy/common.h index e7ca0603..ac40d29c 100644 --- a/include/pocketpy/common.h +++ b/include/pocketpy/common.h @@ -18,6 +18,7 @@ #include #include #include +#include #define PK_VERSION "1.4.1" diff --git a/include/pocketpy/vector.h b/include/pocketpy/vector.h index f7b99539..574f3677 100644 --- a/include/pocketpy/vector.h +++ b/include/pocketpy/vector.h @@ -19,6 +19,12 @@ struct pod_vector{ _data = (T*)pool64_alloc(_capacity * sizeof(T)); } + // support initializer list + pod_vector(std::initializer_list il): _size(il.size()), _capacity(std::max(N, _size)) { + _data = (T*)pool64_alloc(_capacity * sizeof(T)); + for(int i=0; i<_size; i++) _data[i] = *(il.begin() + i); + } + pod_vector(int size): _size(size), _capacity(std::max(N, size)) { _data = (T*)pool64_alloc(_capacity * sizeof(T)); } diff --git a/src/pocketpy.cpp b/src/pocketpy.cpp index 92a81a38..69aa03a6 100644 --- a/src/pocketpy.cpp +++ b/src/pocketpy.cpp @@ -1048,14 +1048,13 @@ void init_builtins(VM* _vm) { // tp_bytes _vm->bind_constructor<2>(_vm->_t(VM::tp_bytes), [](VM* vm, ArgsView args){ List& list = CAST(List&, args[1]); - pod_vector buffer(list.size()); + unsigned char* buffer = new unsigned char[list.size()]; for(int i=0; i255) vm->ValueError("byte must be in range[0, 256)"); buffer[i] = (char)b; } - auto detached = buffer.detach(); - return VAR(Bytes(detached.first, detached.second)); + return VAR(Bytes(buffer, list.size())); }); _vm->bind__getitem__(VM::tp_bytes, [](VM* vm, PyObject* obj, PyObject* index) {