title: V8 ArrayBuffer from int array?
body: <p>I've seen <a href="https://stackoverflow.com/questions/39796251/copy-data-into-v8arraybuffer">Copy data into v8::ArrayBuffer</a> which is able to create an array buffer based on data like the following:</p>
  
  <pre><code>QByteArray data_buffer(file.readAll().data(), file.size());
  v8::Handle&lt;v8::ArrayBuffer&gt; ab = v8::ArrayBuffer::New(args.GetIsolate(), data_buffer.size());
  memcpy(ab-&gt;GetContents().Data(), data_buffer.data(), data_buffer.size());
  </code></pre>
  
  <p>But he doesn't explain how to do this in the ::New constructor itself. In fact he mentions:</p>
  
  <blockquote>
    <p>If I use the constructor from the documentation in which I pass a
    pointer to the data, I'll have to deal with the memory myself and I
    don't want that.</p>
  </blockquote>
  
  <p>As the <a href="https://v8docs.nodesource.com/node-0.12/d5/d6e/classv8_1_1_array_buffer.html" rel="nofollow noreferrer">documentation</a> says:</p>
  
  <blockquote>
    <p>◆ New() [2/2]</p>
  
  <pre><code>static Local&lt;ArrayBuffer&gt; New (   Isolate *   isolate,
  void *    data,
  size_t    byte_length 
  )     
  </code></pre>
    
    <p>static Create a new ArrayBuffer over an existing memory block. The
    created array buffer is immediately in externalized state. The memory
    block will not be reclaimed when a created ArrayBuffer is
    garbage-collected.</p>
  </blockquote>
  
  <p>That basically it requires a void* to automatically make the new data.</p>
  
  <p>I was a bit confused with how to do this for a simple int array, the following returns an arraybuffer with unpredictable results:</p>
  
  <p>C++ code:</p>
  
  <pre><code>#include &lt;node.h&gt;
  using namespace v8;
  
  void Buf(const FunctionCallbackInfo&lt;Value&gt;&amp; a) {
  
      Isolate* i = a.GetIsolate();
      int x[] = {6,9,8,7};
  
      void* temp = x;
  
      Local&lt;
          ArrayBuffer
      &gt; 
      v = 
      ArrayBuffer::New(i, temp, 5);
      a.GetReturnValue().Set(v);
  
  }
  
  void Initialize(Local&lt;Object&gt; exports) {
    NODE_SET_METHOD(exports, "buf", Buf);
  }
  
  NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
  </code></pre>
  
  <p>And then my NodeJS file :</p>
  
  <pre><code>let a = require("./addon"),
      ab = a.buf(),
      ar = new Uint8Array(ab)
  console.log(
      ar, ab
  );
  </code></pre>
  
  <p>but the output , when run multiple times, is giving pretty unpredictable results, which don't seem to match the values from the original array:</p>
  
  <pre><code>C:\Users\Coby\Documents\aa\atzmus\testServer&gt;node oy.js
  Uint8Array [ 224, 107, 122, 64, 1 ] ArrayBuffer { [Uint8Contents]: &lt;e0 6b 7a 40
  01&gt;, byteLength: 5 }
  
  C:\Users\Coby\Documents\aa\atzmus\testServer&gt;node oy.js
  Uint8Array [ 224, 107, 88, 64, 1 ] ArrayBuffer { [Uint8Contents]: &lt;e0 6b 58 40 0
  1&gt;, byteLength: 5 }
  
  C:\Users\Coby\Documents\aa\atzmus\testServer&gt;node oy.js
  Uint8Array [ 224, 107, 2, 64, 1 ] ArrayBuffer { [Uint8Contents]: &lt;e0 6b 02 40 01
  &gt;, byteLength: 5 }
  
  C:\Users\Coby\Documents\aa\atzmus\testServer&gt;node oy.js
  Uint8Array [ 224, 107, 216, 63, 1 ] ArrayBuffer { [Uint8Contents]: &lt;e0 6b d8 3f
  01&gt;, byteLength: 5 }
  
  C:\Users\Coby\Documents\aa\atzmus\testServer&gt;node oy.js
  Uint8Array [ 224, 107, 79, 64, 1 ] ArrayBuffer { [Uint8Contents]: &lt;e0 6b 4f 40 0
  1&gt;, byteLength: 5 }
  
  C:\Users\Coby\Documents\aa\atzmus\testServer&gt;node oy.js
  Uint8Array [ 224, 107, 54, 64, 1 ] ArrayBuffer { [Uint8Contents]: &lt;e0 6b 36 40 0
  1&gt;, byteLength: 5 }
  
  C:\Users\Coby\Documents\aa\atzmus\testServer&gt;node oy.js
  Uint8Array [ 224, 107, 159, 63, 1 ] ArrayBuffer { [Uint8Contents]: &lt;e0 6b 9f 3f
  01&gt;, byteLength: 5 }
  
  C:\Users\Coby\Documents\aa\atzmus\testServer&gt;node oy.js
  Uint8Array [ 224, 107, 104, 64, 1 ] ArrayBuffer { [Uint8Contents]: &lt;e0 6b 68 40
  01&gt;, byteLength: 5 }
  
  C:\Users\Coby\Documents\aa\atzmus\testServer&gt;node oy.js
  Uint8Array [ 224, 107, 170, 63, 1 ] ArrayBuffer { [Uint8Contents]: &lt;e0 6b aa 3f
  01&gt;, byteLength: 5 }
  </code></pre>
  
  <p>I think it's something to do with what was mentioned before about "handling the memory myself" -- but I don't know enough about this to solve it, AKA:</p>
  
  <p><strong>How do I create an ArrayBuffer in C++ from an int array, and read those exact int values back in NodeJS?</strong></p>