mtsiakiris
mtsiakiris

Reputation: 190

How to store and recall v8::Value from javascript on C++ vector

Lets say that my class is like this:

namespace foo
{
    class Item
    {
    public:
        Item();
        ~Item();

        std::string Name;
        v8::Local<v8::Value> DataValue;
        bool ReadOnly;
    }; 

    static std::vector<Item *> GlobalCollection;
}

I have a vector of Item and my getMessage("Name") have to return the DataValue member of class Item after searching inside the vector to find the coresponding Name.

So far, I am getting the v8 object reference value of DataValue to my javascript code.

While debugging I see that the v8::Value does not exist any more after getting the Item from the vector. I have only a v8::Object value instead with no actual data.

I'm trying to achieve (javascript):

var a = my.global('VAR1', 1); // returns true / false
var b = my.global('VAR1'); // must return 1 (number)

var x = 'Store this globaly!';
var c = my.global("VAR2", x); // returns true / false
var d = my.global("VAR2"); // must return string "Store this globaly!"

x could be any javascript value or object.

Thanx

Upvotes: 0

Views: 560

Answers (1)

mtsiakiris
mtsiakiris

Reputation: 190

One posible solution is the following: Not tested for function values...

class Item
{
public:
    Item();
    ~Item();

    std::string Name;
    v8::Persistent<v8::Object> DataValue;
    bool ReadOnly;
};

INSIDE SET FUNCTION:
v8::Local<v8::Object> dataValue;

item = new foo::Item();
item->Name = varName;
item->DataValue = v8::Persistent<v8::Object>::New(dataValue);

std::vector<foo::Item *>::iterator it;
it = foo::GlobalCollection.end();
it = foo::GlobalCollection.insert(it, item);

INSIDE GET FUNCTION:
v8::Local<v8::Value> ret;
foo::Item *item = bla-bla...;
ret = v8::Local<v8::Value>::New(item->DataValue);
return ret;

Hope this will be helpful...

Upvotes: 1

Related Questions