Behavior of GetEmbedderData() for unset fields (how to check if SetEmbedderData() already called)

12 views
Skip to first unread message

ytp...@gmail.com

unread,
Jun 4, 2019, 8:46:22 PM6/4/19
to v8-dev
I have some code that sets data using Context::SetEmbedderData().  I'd like to have an assert verifying that this is only done once on a given V8 Context, i.e. assert that the embedder data field at a given index is currently unset.

Is it safe to do something like this?
ASSERT( context->GetNumberOfEmbedderDataFields() <= kMyDataIndex || context->GetEmbedderData(kMyDataIndex).isEmpty() );

Seems like there are three cases here and I'm unsure of the behavior of the 2nd one:
  1. SetEmbedderData() has never been called with an index >= kMyDataIndex -- it's illegal to call GetEmbedderData() with this index since it would read from past the end of the storage array (or fail in a debug build, on this line).  But we can check for this case via GetNumberOfEmbedderDataFields() as seen above.
  2. SetEmbedderData() has been called with an index > kMyDataIndex, but has not been called with index == kMyDataIndex -- is calling GetEmbedderData(kMyDataIndex) going to return an empty Handle, or is it going to read from uninitialized memory?
  3. SetEmbedderData() has already been called with index == kMyDataIndex -- so GetEmbedderData(kMyDataIndex) is safe and will return the value stored before (failing the assert as desired).
Thanks in advance!

- Peter

Ben Noordhuis

unread,
Jun 5, 2019, 5:01:06 AM6/5/19
to v8-...@googlegroups.com
It should return a handle for which handle->IsUndefined() is true.

You should probably not try that with
context->GetAlignedPointerFromEmbedderData(), I expect that to hit an
assert in debug builds and return a bogus value in release builds.

ytp...@gmail.com

unread,
Jun 5, 2019, 6:58:47 PM6/5/19
to v8-dev
Ah ok -- so that's what the "undefined_value()" referenced in Factory::CopyArrayAndGrow() means I suppose?  I wasn't able to find a definition for that function.

Thanks for the answer!
- P
Reply all
Reply to author
Forward
0 new messages