On Tue, Oct 13, 2020 at 3:37 PM Elling B <elling.bjas...@gmail.com> wrote:
>
> I found this helper method in a tutorial a couple of days ago.
>
> It reinterpret casts a v8::Persistent to a v8::Local, with the intended use 
> of calling V8 API functions that require a local handle to a context, without 
> actually having to create a new v8::Local each time:
>
> template <class TypeName>
> inline v8::Local<TypeName> ToLocal(const v8::Persistent<TypeName>& persistent)
> {
>   return 
> *reinterpret_cast<v8::Local<TypeName>*>(const_cast<v8::Persistent<TypeName>*>(&persistent));
> }
>
> I'm new to V8, so I find myself wondering if it's safe or not. Or if it can 
> be safe in some cases.

Full disclosure: that StrongPersistentToLocal() helper from the
article is my code. I introduced that trick in the Node.js code base
years ago and looks like people copied it. Mea culpa. :broken_smile:

So, it works, but it's not particularly safe. Caveats:

- don't use it on weakly persistent handles, only strong ones, and

- don't call .Reset() while the Local is still in use

Node.js (ab)uses it for performance reasons but most of the time you
should just use `Local<Value>::New(isolate, persistent)`.

-- 
-- 
v8-users mailing list
v8-users@googlegroups.com
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 v8-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/v8-users/CAHQurc8QawY_UjnM_3CciinG6yMN7Q%2BD%2BjbVFeGWsJ6Caxj8mA%40mail.gmail.com.

Reply via email to