Like a good C++ programmer, I'm trying to use shared_ptrs to manage the
lifetime of my C++ objects. I would expect this code below in testv8() to
invoke my ~Point destructor, but it's never called! Can anyone tell me
why?
Also, I have some additional questions inline. Thanks for your help!
-Jim Acquavella, Adobe Systems Inc.
//Sample class mapped to v8
class Point
{
public:
//constructor
Point(int x, int y):x_(x),y_(y){}
virtual ~Point()
{
assert(false); // Never called!
}
//variables
int x_, y_;
};
v8::Handle<v8::Value> GetPointX(v8::Local<v8::String> property,
const v8::AccessorInfo &info) {
using namespace v8;
Local<Object> self = info.Holder();
Local<External> wrap = Local<External>::Cast(self->GetInternalField(0));
void* ptr = wrap->Value();
int value = static_cast<Point*>(ptr)->x_;
return Integer::New(value);
}
void SetPointX(v8::Local<v8::String> property, v8::Local<v8::Value> value,
const v8::AccessorInfo& info) {
using namespace v8;
Local<Object> self = info.Holder();
Local<External> wrap = Local<External>::Cast(self->GetInternalField(0));
void* ptr = wrap->Value();
static_cast<Point*>(ptr)->x_ = value->Int32Value();
}
void testv8()
{
{
v8::V8::Initialize(); // Initialize v8. (Is this needed?? If so, why
isn't it mentioned here -> https://developers.google.com/v8/embed)
// Get the default Isolate created at startup.
v8::Isolate* isolateP = v8::Isolate::GetCurrent();
// Create a stack-allocated handle scope.
v8::HandleScope handle_scope(isolateP);
// Create a template for the global object where we set the
// built-in global functions.
v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New();
// Create a new context.
std::shared_ptr<v8::Persistent<v8::Context> > contextP(new
v8::Persistent<v8::Context>(v8::Context::New(NULL, global)));
std::shared_ptr<v8::Context::Scope> contextScopeP(new
v8::Context::Scope(*contextP));
{
using namespace v8;
Handle<FunctionTemplate> point_templ = FunctionTemplate::New();
point_templ->SetClassName(String::New("Point"));
Handle<ObjectTemplate> point_proto = point_templ->PrototypeTemplate();
Handle<ObjectTemplate> point_inst = point_templ->InstanceTemplate();
point_inst->SetInternalFieldCount(1);
point_inst->SetAccessor(String::New("x"), GetPointX, SetPointX);
Handle<Function> point_ctor = point_templ->GetFunction();
std::shared_ptr<Point> ptP(new Point(0, 0));
v8::Persistent<v8::Object> obj = wrapSharedPtr(point_ctor, ptP);
assert(obj.IsWeak(isolateP));
(*contextP)->Global()->Set(String::New("point"), obj);
obj.Dispose(isolateP);
Handle<String> source = String::New("point.x = 3;");
Handle<Script> script = Script::Compile(source);
Handle<Value> result = script->Run();
source = String::New("point.x;");
script = Script::Compile(source);
result = script->Run();
std::int32_t resultValue32 = result->Int32Value();
assert(3 == resultValue32);
}
// Dispose the persistent context.
contextP->Dispose(isolateP);
contextP->Clear();
contextScopeP.reset();
contextP.reset();
}
v8::V8::LowMemoryNotification(); // I saw this mentioned as a way to force
GC, but it doesn't help. Even so, I would think v8::V8::Dispose would
clear all garbage.
v8::V8::Dispose(); // Initialize v8. (Is this needed?? If so, why isn't
it mentioned here -> https://developers.google.com/v8/embed)
}
--
--
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users
---
You received this message because you are subscribed to the Google Groups
"v8-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.