Reviewers: danno,

Description:
Add an API to let the embedder associate arbitrary data with a v8::Context.

I had hoped that v8::Context::SetData would let the embedder associate a void* with a v8::Context, but it turns out we can't use that API because the debugger makes assumptions about the sorts of values that will be stored via that API.

See https://bugs.webkit.org/show_bug.cgi?id=98679 for more context.

Please review this at https://codereview.chromium.org/11087020/

SVN Base: http://v8.googlecode.com/svn/trunk/

Affected files:
  M     include/v8.h
  M     src/api.cc
  M     src/contexts.h


Index: include/v8.h
===================================================================
--- include/v8.h        (revision 12669)
+++ include/v8.h        (working copy)
@@ -3722,6 +3722,13 @@
   Local<Value> GetData();

   /**
+ * Associate an opaque pointer with the context. This pointer can be used by
+   * the embedder to associate additional state with the context.
+   */
+  void SetEmbedderData(Handle<Value> data);
+  Local<Value> GetEmbedderData();
+
+  /**
    * Control whether code generation from strings is allowed. Calling
    * this method with false will disable 'eval' and the 'Function'
    * constructor for code running in this context. If 'eval' or the
Index: src/api.cc
===================================================================
--- src/api.cc  (revision 12669)
+++ src/api.cc  (working copy)
@@ -792,6 +792,27 @@
 }


+void Context::SetEmbedderData(v8::Handle<Value> data) {
+  i::Handle<i::Context> env = Utils::OpenHandle(this);
+  i::Isolate* isolate = env->GetIsolate();
+  if (IsDeadCheck(isolate, "v8::Context::SetEmbedderData()")) return;
+  i::Handle<i::Object> raw_data = Utils::OpenHandle(*data);
+  ASSERT(env->IsNativeContext());
+  env->set_embedder_data(*raw_data);
+}
+
+
+v8::Local<Value> Context::GetEmbedderData() {
+  i::Handle<i::Context> env = Utils::OpenHandle(this);
+  i::Isolate* isolate = env->GetIsolate();
+  if (IsDeadCheck(isolate, "v8::Context::GetEmbedderData()")) {
+    return Local<Value>();
+  }
+  i::Handle<i::Object> result(env->embedder_data(), isolate);
+  return Utils::ToLocal(result);
+}
+
+
 i::Object** v8::HandleScope::RawClose(i::Object** value) {
   if (!ApiCheck(!is_closed_,
                 "v8::HandleScope::Close()",
Index: src/contexts.h
===================================================================
--- src/contexts.h      (revision 12669)
+++ src/contexts.h      (working copy)
@@ -153,6 +153,7 @@
   V(OUT_OF_MEMORY_INDEX, Object, out_of_memory) \
   V(MAP_CACHE_INDEX, Object, map_cache) \
   V(CONTEXT_DATA_INDEX, Object, data) \
+  V(CONTEXT_EMBEDDER_DATA_INDEX, Object, embedder_data) \
V(ALLOW_CODE_GEN_FROM_STRINGS_INDEX, Object, allow_code_gen_from_strings) \
   V(ERROR_MESSAGE_FOR_CODE_GEN_FROM_STRINGS_INDEX, Object, \
     error_message_for_code_gen_from_strings) \
@@ -282,6 +283,7 @@
     CONTEXT_EXTENSION_FUNCTION_INDEX,
     OUT_OF_MEMORY_INDEX,
     CONTEXT_DATA_INDEX,
+    CONTEXT_EMBEDDER_DATA_INDEX,
     ALLOW_CODE_GEN_FROM_STRINGS_INDEX,
     ERROR_MESSAGE_FOR_CODE_GEN_FROM_STRINGS_INDEX,
     TO_COMPLETE_PROPERTY_DESCRIPTOR_INDEX,


--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev

Reply via email to