How to fill v8 array?

I have a vector std::vector<std::string> path , and I would like to copy it to a v8 array and return it from my function.

I tried to create a new array

 v8::Handle<v8::Array> result; 

and putting the values ​​from path to result , but no luck. I also tried several options

 return scope.Close(v8::Array::New(/* I've tried many things in here */)); 

without success.

This question is a similar question, but I don't seem to duplicate the results.

How do you populate v8 arrays?

+4
source share
1 answer

This example directly from the Attachment Guide seems very close to what you want - replace the new Integer objects with the new String objects.

 // This function returns a new array with three elements, x, y, and z. Handle<Array> NewPointArray(int x, int y, int z) { // We will be creating temporary handles so we use a handle scope. HandleScope handle_scope; // Create a new empty array. Handle<Array> array = Array::New(3); // Return an empty result if there was an error creating the array. if (array.IsEmpty()) return Handle<Array>(); // Fill out the values array->Set(0, Integer::New(x)); array->Set(1, Integer::New(y)); array->Set(2, Integer::New(z)); // Return the value through Close. return handle_scope.Close(array); } 

I would read the semantics of local and persistent descriptors, because I think that is where you are stuck.

This line:

 v8::Handle<v8::Array> result; 

It does not create a new array - it only creates a Handle, which can later be filled with an array.

+7
source

Source: https://habr.com/ru/post/1481681/


All Articles