Closed atvise closed 7 years ago
Hi @atvise, thanks!
Such a variable binding uses v8::ObjectTemplate::SetAccessor()
function to setup getter and setter function callbacks for the variable named array
in input
object template.
As I understand V8 internals, in expressions like input.array[0].id = 9
V8 tries to access input.array
. This leads to the array
getter callback invocation, and then V8 gets element of the array which was returned from the getter, and changes it.
But there is no expression in example above, which should look like input.array = [...]
, so there is no setter callback invocation, and no data update in myStructVec
variable on C++ side.
It's similar to modification of a temporary result returned from C++ function:
std::vector<MyStruct> getter()
{
return myStructVec; // returns a copy of myStructVec
}
getter()[0].id = 9; // no affect on myStructVec
And I see only a workaround like this:
// get data on C++ side,
// getter will convert `myStructVec` into an array of objects
// using `v8pp::convert<MyStruct>::to_v8()` for each vector item
var arr = input.array;
// modify data on JavaScript side
arr[0].id = 9;
arr[1].name = "TestName";
// Set data back to C++ side,
// setter will convert JS array of objects into `std::vector<MyStruct>`
// using `v8pp::convert<MyStruct>::from_v8` for each array element,
// and then will assign the result vector to myStructVec
input.array = arr
It works like a charm (although with large structs and lots of array elements a small change will also require all data to be copied - performance issue)
But nevertheless you helped me alot! Thank you very much !
Yes, there may be a performance issue for arrays with many elements.
V8 allows to set callbacks with ObjectTemplate::SetIndexedPropertyHandler()
to mimics array functionality in wrapped objects. But I've never used this in my projects, so v8pp
has no support for this.
Hi,
first of all: great library !
Just want to ask I've anyone know if it's possible to introduce an array of structs to v8 like:
Furthermore it should be possible to access and change the data within every MyStruct entry of the array in Javascript and access the changed data in C++ after the script finished running ?
I've tried it but can't seem to get it done right because the values aren't changed after executing:
Thank you very much for your time and help !
Regards, atvise