blueloveTH 2 年 前
コミット
bbe3773154
3 ファイル変更9 行追加3 行削除
  1. 1 0
      include/pocketpy/common.h
  2. 6 0
      include/pocketpy/vector.h
  3. 2 3
      src/pocketpy.cpp

+ 1 - 0
include/pocketpy/common.h

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

+ 6 - 0
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<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));
     }

+ 2 - 3
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<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) {