Invalid descriptors pointing to remote C ++ object

When a C ++ object that is exposed to v8 is deleted, how can I invalidate descriptors that may point to this object.

I am using v8 as a scripting interface for a larger application. Objects in a larger application are wrapped and accessible in v8 using the node ObjectWrap class.

The problem is that the lifetime of wrapped objects is limited. If in javascript I do something like:

var win = app.getWindow(); win.close(); // The C++ object that win references goes away console.log(win.width()); // This should fail. 

I want him to act like the comments say. After win.close () (or some other event may be outside of the JS control), any access to win or the duplicated descriptor should fail.

Currently, I have to mark the wrapped C ++ object as invalid and check the validity with every method call. Is this the only way to do this, or is there a way to mark the descriptor as already invalid?

+4
source share
1 answer

The only way that comes to mind is to have an extra function that gives an error when called. Then, when you call ".close", you can create properties on win that take precedence over prototype versions of the object.

 function closedError() { return new Error("Window Closed"); } win.close = function() { this.width = closedError; this.otherfunc = closedError; }; 

I do not have a compiler at the moment, but I am presenting something similar in C ++.

 static Handle<Value> Close(const Arguments& args) { HandleScope scope; NODE_SET_METHOD(args.This(), "width", Window::Width); NODE_SET_METHOD(args.This(), "otherfunc", Window::OtherFunc); } 
+1
source

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


All Articles