javascript - Unwrapping objects members with V8/Node.JS -
i'm learning v8/node.js through 1 of project , i'm wondering if there way unwrap c++ object inherited node::ojectwrap (from node.js api) and members @ same time.
all examples can found have int members instance , of them try wrap c++ object members. here code explanation of want do:
// basic class contains vector member // note not interact v8 or node.js code class b { public: // constructors , methods... size_t getsize(void) { return vec_.size(); }; private: std::vector<int> vec_; } // class instanciate correctly in javascript code class : public node::objectwrap { public: static void init(v8::handle<v8::value> exports); static v8::handle<v8::value> new(const v8::arguments& args); // method use instanciate js object static v8::handle<v8::value> update(const v8::arguments& args); // method call private: a(b b) : b_(b) {}; ~a(void); private: static v8::persistent<v8::function> constructor_; b b_; } to keep things simple, here definitions of a::new , a::update. i'm using google v8 documentation sample code others methods.
v8::handle<v8::value> a::new(const v8::arguments& args) { v8::handlescope scope; if (args.isconstructorcall()) { b b; // fill vector of b n elements... // if print here size of a->b_.size(), n a* = new a(b); a->wrap(args.this()); return args.this(); } else // ... } v8::handle<v8::value> a::update(const v8::arguments& args) { v8::handlescope scope; a* = objectwrap::unwrap<a>(args.this()); // if print here size of a->b_.size(), garbage value } finally, javascript code:
var = new addon.a(/* constructor parameters *); // print size value a.update(); // print garbage value it seems b member of a not wrapped correctly. question is: possible wrap object members without inherited them of node::objectwrap ?
thanks help.
for may in same situation, found out error.
in fact, allocating b member on stack , since method call in a class statics, can have access garbage values between 2 calls (here a::new , a::update).
i solve problem allocating b object on heap.
Comments
Post a Comment