fix a bug

This commit is contained in:
blueloveTH 2024-02-18 18:08:11 +08:00
parent 97c923e514
commit bbe3773154
3 changed files with 9 additions and 3 deletions

View File

@ -18,6 +18,7 @@
#include <type_traits>
#include <random>
#include <deque>
#include <initializer_list>
#define PK_VERSION "1.4.1"

View File

@ -19,6 +19,12 @@ struct pod_vector{
_data = (T*)pool64_alloc(_capacity * sizeof(T));
}
// support initializer list
pod_vector(std::initializer_list<T> 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));
}

View File

@ -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<unsigned char> buffer(list.size());
unsigned char* buffer = new unsigned char[list.size()];
for(int i=0; i<list.size(); i++){
i64 b = CAST(i64, list[i]);
if(b<0 || b>255) 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) {