V8 Project
v8::Context Class Reference

A sandboxed execution context with its own set of built-in objects and functions. More...

#include <v8.h>

+ Collaboration diagram for v8::Context:

Classes

class  Scope
 Stack-allocated class which sets the execution context for all operations executed within a local scope. More...
 

Public Member Functions

Local< ObjectGlobal ()
 Returns the global proxy object. More...
 
void DetachGlobal ()
 Detaches the global object from its context before the global object can be reused to create a new context. More...
 
void SetSecurityToken (Handle< Value > token)
 Sets the security token for the context. More...
 
void UseDefaultSecurityToken ()
 Restores the security token to the default value. More...
 
Handle< ValueGetSecurityToken ()
 Returns the security token of this context. More...
 
void Enter ()
 Enter this context. More...
 
void Exit ()
 Exit this context. More...
 
v8::IsolateGetIsolate ()
 Returns an isolate associated with a current context. More...
 
Local< ValueGetEmbedderData (int index)
 Gets the embedder data with the given index, which must have been set by a previous call to SetEmbedderData with the same index. More...
 
void SetEmbedderData (int index, Handle< Value > value)
 Sets the embedder data with the given index, growing the data as needed. More...
 
void * GetAlignedPointerFromEmbedderData (int index)
 Gets a 2-byte-aligned native pointer from the embedder data with the given index, which must have bees set by a previous call to SetAlignedPointerInEmbedderData with the same index. More...
 
void SetAlignedPointerInEmbedderData (int index, void *value)
 Sets a 2-byte-aligned native pointer in the embedder data with the given index, growing the data as needed. More...
 
void AllowCodeGenerationFromStrings (bool allow)
 Control whether code generation from strings is allowed. More...
 
bool IsCodeGenerationFromStringsAllowed ()
 Returns true if code generation from strings is allowed for the context. More...
 
void SetErrorMessageForCodeGenerationFromStrings (Handle< String > message)
 Sets the error description for the exception that is thrown when code generation from strings is not allowed and 'eval' or the 'Function' constructor are called. More...
 

Static Public Member Functions

static Local< ContextNew (Isolate *isolate, ExtensionConfiguration *extensions=NULL, Handle< ObjectTemplate > global_template=Handle< ObjectTemplate >(), Handle< Value > global_object=Handle< Value >())
 Creates a new context and returns a handle to the newly allocated context. More...
 

Private Member Functions

Local< ValueSlowGetEmbedderData (int index)
 
void * SlowGetAlignedPointerFromEmbedderData (int index)
 

Friends

class Value
 
class Script
 
class Object
 
class Function
 

Detailed Description

A sandboxed execution context with its own set of built-in objects and functions.

Definition at line 5443 of file v8.h.

Member Function Documentation

◆ AllowCodeGenerationFromStrings()

void v8::Context::AllowCodeGenerationFromStrings ( bool  allow)

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 'Function' constructor are used an exception will be thrown.

If code generation from strings is not allowed the V8::AllowCodeGenerationFromStrings callback will be invoked if set before blocking the call to 'eval' or the 'Function' constructor. If that callback returns true, the call will be allowed, otherwise an exception will be thrown. If no callback is set an exception will be thrown.

Definition at line 5289 of file api.cc.

5289  {
5290  i::Handle<i::Context> context = Utils::OpenHandle(this);
5291  i::Isolate* isolate = context->GetIsolate();
5292  ENTER_V8(isolate);
5293  context->set_allow_code_gen_from_strings(
5294  allow ? isolate->heap()->true_value() : isolate->heap()->false_value());
5295 }
#define ENTER_V8(isolate)
Definition: api.cc:54
static v8::internal::Handle< To > OpenHandle(v8::Local< From > handle)
Definition: api.h:288

References ENTER_V8, v8::internal::Isolate::heap(), and v8::Utils::OpenHandle().

+ Here is the call graph for this function:

◆ DetachGlobal()

void v8::Context::DetachGlobal ( )

Detaches the global object from its context before the global object can be reused to create a new context.

Definition at line 5281 of file api.cc.

5281  {
5282  i::Handle<i::Context> context = Utils::OpenHandle(this);
5283  i::Isolate* isolate = context->GetIsolate();
5284  ENTER_V8(isolate);
5285  isolate->bootstrapper()->DetachGlobal(context);
5286 }
Bootstrapper * bootstrapper()
Definition: isolate.h:856

References v8::internal::Isolate::bootstrapper(), ENTER_V8, and v8::Utils::OpenHandle().

+ Here is the call graph for this function:

◆ Enter()

void v8::Context::Enter ( )

Enter this context.

After entering a context, all code compiled and run is compiled and run in this context. If another context is already entered, this old context is saved so it can be restored when the new context is exited.

Definition at line 612 of file api.cc.

612  {
614  i::Isolate* isolate = env->GetIsolate();
615  ENTER_V8(isolate);
617  impl->EnterContext(env);
618  impl->SaveContext(isolate->context());
619  isolate->set_context(*env);
620 }
void EnterContext(Handle< Context > context)
Definition: api.h:614
void SaveContext(Context *context)
Definition: api.h:599
HandleScopeImplementer * handle_scope_implementer()
Definition: isolate.h:901
void set_context(Context *context)
Definition: isolate.h:549
Context * context()
Definition: isolate.h:548

References v8::internal::Isolate::context(), ENTER_V8, v8::internal::HandleScopeImplementer::EnterContext(), v8::internal::Isolate::handle_scope_implementer(), v8::Utils::OpenHandle(), v8::internal::HandleScopeImplementer::SaveContext(), and v8::internal::Isolate::set_context().

+ Here is the call graph for this function:

◆ Exit()

void v8::Context::Exit ( )

Exit this context.

Exiting the current context restores the context that was in place when entering the current context.

Definition at line 623 of file api.cc.

623  {
625  i::Isolate* isolate = env->GetIsolate();
626  ENTER_V8(isolate);
628  if (!Utils::ApiCheck(impl->LastEnteredContextWas(env),
629  "v8::Context::Exit()",
630  "Cannot exit non-entered context")) {
631  return;
632  }
633  impl->LeaveContext();
634  isolate->set_context(impl->RestoreContext());
635 }
static bool ApiCheck(bool condition, const char *location, const char *message)
Definition: api.h:177
bool LastEnteredContextWas(Handle< Context > context)
Definition: api.h:624

References v8::Utils::ApiCheck(), ENTER_V8, v8::internal::Isolate::handle_scope_implementer(), v8::internal::HandleScopeImplementer::LastEnteredContextWas(), v8::internal::HandleScopeImplementer::LeaveContext(), v8::Utils::OpenHandle(), v8::internal::HandleScopeImplementer::RestoreContext(), and v8::internal::Isolate::set_context().

+ Here is the call graph for this function:

◆ GetAlignedPointerFromEmbedderData()

void * v8::Context::GetAlignedPointerFromEmbedderData ( int  index)
inline

Gets a 2-byte-aligned native pointer from the embedder data with the given index, which must have bees set by a previous call to SetAlignedPointerInEmbedderData with the same index.

Note that index 0 currently has a special meaning for Chrome's debugger.

Definition at line 6954 of file v8.h.

6954  {
6955 #ifndef V8_ENABLE_CHECKS
6956  typedef internal::Internals I;
6957  return I::ReadEmbedderData<void*>(this, index);
6958 #else
6960 #endif
6961 }
void * SlowGetAlignedPointerFromEmbedderData(int index)
Definition: api.cc:694
#define I(name, number_of_args, result_size)
Definition: runtime.cc:9248

References I.

Referenced by SetAlignedPointerInEmbedderData().

+ Here is the caller graph for this function:

◆ GetEmbedderData()

Local< Value > v8::Context::GetEmbedderData ( int  index)
inline

Gets the embedder data with the given index, which must have been set by a previous call to SetEmbedderData with the same index.

Note that index 0 currently has a special meaning for Chrome's debugger.

Definition at line 6939 of file v8.h.

6939  {
6940 #ifndef V8_ENABLE_CHECKS
6941  typedef internal::Object O;
6942  typedef internal::HeapObject HO;
6943  typedef internal::Internals I;
6944  HO* context = *reinterpret_cast<HO**>(this);
6945  O** result =
6946  HandleScope::CreateHandle(context, I::ReadEmbedderData<O*>(this, index));
6947  return Local<Value>(reinterpret_cast<Value*>(result));
6948 #else
6949  return SlowGetEmbedderData(index);
6950 #endif
6951 }
Local< Value > SlowGetEmbedderData(int index)
Definition: api.cc:674
friend class Value
Definition: v8.h:5591
static internal::Object ** CreateHandle(internal::Isolate *isolate, internal::Object *value)
Definition: api.cc:579
kSerializedDataOffset Object
Definition: objects-inl.h:5322

References v8::HandleScope::CreateHandle(), and I.

Referenced by SetEmbedderData().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ GetIsolate()

v8::Isolate * v8::Context::GetIsolate ( )

Returns an isolate associated with a current context.

Definition at line 5261 of file api.cc.

5261  {
5263  return reinterpret_cast<Isolate*>(env->GetIsolate());
5264 }

References v8::Utils::OpenHandle().

+ Here is the call graph for this function:

◆ GetSecurityToken()

Handle< Value > v8::Context::GetSecurityToken ( )

Returns the security token of this context.

Definition at line 5252 of file api.cc.

5252  {
5253  i::Isolate* isolate = i::Isolate::Current();
5255  i::Object* security_token = env->security_token();
5256  i::Handle<i::Object> token_handle(security_token, isolate);
5257  return Utils::ToLocal(token_handle);
5258 }
static Local< Context > ToLocal(v8::internal::Handle< v8::internal::Context > obj)

References v8::Utils::OpenHandle(), and v8::Utils::ToLocal().

+ Here is the call graph for this function:

◆ Global()

v8::Local< v8::Object > v8::Context::Global ( )

Returns the global proxy object.

Global proxy object is a thin wrapper whose prototype points to actual context's global object with the properties like Object, etc. This is done that way for security reasons (for more details see https://wiki.mozilla.org/Gecko:SplitWindow).

Please note that changes to global proxy object prototype most probably would break VM—v8 expects only global object as a prototype of global proxy object.

Definition at line 5267 of file api.cc.

5267  {
5268  i::Handle<i::Context> context = Utils::OpenHandle(this);
5269  i::Isolate* isolate = context->GetIsolate();
5270  i::Handle<i::Object> global(context->global_proxy(), isolate);
5271  // TODO(dcarney): This should always return the global proxy
5272  // but can't presently as calls to GetProtoype will return the wrong result.
5274  global)->IsDetachedFrom(context->global_object())) {
5275  global = i::Handle<i::Object>(context->global_object(), isolate);
5276  }
5278 }

References v8::Utils::OpenHandle(), and v8::Utils::ToLocal().

+ Here is the call graph for this function:

◆ IsCodeGenerationFromStringsAllowed()

bool v8::Context::IsCodeGenerationFromStringsAllowed ( )

Returns true if code generation from strings is allowed for the context.

For more details see AllowCodeGenerationFromStrings(bool) documentation.

Definition at line 5298 of file api.cc.

5298  {
5299  i::Handle<i::Context> context = Utils::OpenHandle(this);
5300  return !context->allow_code_gen_from_strings()->IsFalse();
5301 }

References v8::Utils::OpenHandle().

+ Here is the call graph for this function:

◆ New()

Local< Context > v8::Context::New ( v8::Isolate external_isolate,
v8::ExtensionConfiguration extensions = NULL,
v8::Handle< ObjectTemplate global_template = Handle<ObjectTemplate>(),
v8::Handle< Value global_object = Handle<Value>() 
)
static

Creates a new context and returns a handle to the newly allocated context.

Parameters
isolateThe isolate in which to create the context.
extensionsAn optional extension configuration containing the extensions to be installed in the newly created context.
global_templateAn optional object template from which the global object for the newly created context will be created.
global_objectAn optional global object to be reused for the newly created context. This global object must have been created by a previous call to Context::New with the same global template. The state of the global object will be completely reset and only object identify will remain.

Definition at line 5217 of file api.cc.

5221  {
5222  i::Isolate* isolate = reinterpret_cast<i::Isolate*>(external_isolate);
5223  LOG_API(isolate, "Context::New");
5224  ON_BAILOUT(isolate, "v8::Context::New()", return Local<Context>());
5225  i::HandleScope scope(isolate);
5226  ExtensionConfiguration no_extensions;
5227  if (extensions == NULL) extensions = &no_extensions;
5228  i::Handle<i::Context> env =
5229  CreateEnvironment(isolate, extensions, global_template, global_object);
5230  if (env.is_null()) return Local<Context>();
5231  return Utils::ToLocal(scope.CloseAndEscape(env));
5232 }
#define ON_BAILOUT(isolate, location, code)
Definition: api.cc:60
#define LOG_API(isolate, expr)
Definition: api.cc:52
bool is_null() const
Definition: handles.h:124
enable harmony numeric enable harmony object literal extensions Optimize object Array DOM strings and string trace pretenuring decisions of HAllocate instructions Enables optimizations which favor memory size over execution speed maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining trace the tracking of allocation sites deoptimize every n garbage collections perform array bounds checks elimination analyze liveness of environment slots and zap dead values flushes the cache of optimized code for closures on every GC allow uint32 values on optimize frames if they are used only in safe operations track concurrent recompilation artificial compilation delay in ms do not emit check maps for constant values that have a leaf deoptimize the optimized code if the layout of the maps changes enable context specialization in TurboFan execution budget before interrupt is triggered max percentage of megamorphic generic ICs to allow optimization enable use of SAHF instruction if enable use of VFP3 instructions if available enable use of NEON instructions if enable use of SDIV and UDIV instructions if enable use of MLS instructions if enable loading bit constant by means of movw movt instruction enable unaligned accesses for enable use of d16 d31 registers on ARM this requires VFP3 force all emitted branches to be in long enable alignment of csp to bytes on platforms which prefer the register to always be NULL
static i::Handle< i::Context > CreateEnvironment(i::Isolate *isolate, v8::ExtensionConfiguration *extensions, v8::Handle< ObjectTemplate > global_template, v8::Handle< Value > maybe_global_proxy)
Definition: api.cc:5151

References v8::internal::HandleScope::CloseAndEscape(), v8::CreateEnvironment(), v8::internal::Handle< T >::is_null(), LOG_API, NULL, ON_BAILOUT, and v8::Utils::ToLocal().

Referenced by v8::Shell::CreateEvaluationContext(), v8::Shell::InitializeDebugger(), main(), and v8::Shell::RealmCreate().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ SetAlignedPointerInEmbedderData()

void v8::Context::SetAlignedPointerInEmbedderData ( int  index,
void *  value 
)

Sets a 2-byte-aligned native pointer in the embedder data with the given index, growing the data as needed.

Note that index 0 currently has a special meaning for Chrome's debugger.

Definition at line 702 of file api.cc.

702  {
703  const char* location = "v8::Context::SetAlignedPointerInEmbedderData()";
704  i::Handle<i::FixedArray> data = EmbedderDataFor(this, index, true, location);
705  data->set(index, EncodeAlignedAsSmi(value, location));
707 }
void * GetAlignedPointerFromEmbedderData(int index)
Gets a 2-byte-aligned native pointer from the embedder data with the given index, which must have bee...
Definition: v8.h:6954
#define DCHECK_EQ(v1, v2)
Definition: logging.h:206
static i::Smi * EncodeAlignedAsSmi(void *value, const char *location)
Definition: api.cc:644
static i::Handle< i::FixedArray > EmbedderDataFor(Context *context, int index, bool can_grow, const char *location)
Definition: api.cc:651

References DCHECK_EQ, v8::EmbedderDataFor(), v8::EncodeAlignedAsSmi(), and GetAlignedPointerFromEmbedderData().

+ Here is the call graph for this function:

◆ SetEmbedderData()

void v8::Context::SetEmbedderData ( int  index,
v8::Handle< Value value 
)

Sets the embedder data with the given index, growing the data as needed.

Note that index 0 currently has a special meaning for Chrome's debugger.

Definition at line 683 of file api.cc.

683  {
684  const char* location = "v8::Context::SetEmbedderData()";
685  i::Handle<i::FixedArray> data = EmbedderDataFor(this, index, true, location);
686  if (data.is_null()) return;
688  data->set(index, *val);
689  DCHECK_EQ(*Utils::OpenHandle(*value),
691 }
Local< Value > GetEmbedderData(int index)
Gets the embedder data with the given index, which must have been set by a previous call to SetEmbedd...
Definition: v8.h:6939

References DCHECK_EQ, v8::EmbedderDataFor(), GetEmbedderData(), v8::internal::Handle< T >::is_null(), and v8::Utils::OpenHandle().

+ Here is the call graph for this function:

◆ SetErrorMessageForCodeGenerationFromStrings()

void v8::Context::SetErrorMessageForCodeGenerationFromStrings ( Handle< String message)

Sets the error description for the exception that is thrown when code generation from strings is not allowed and 'eval' or the 'Function' constructor are called.

Definition at line 5304 of file api.cc.

5305  {
5306  i::Handle<i::Context> context = Utils::OpenHandle(this);
5307  i::Handle<i::String> error_handle = Utils::OpenHandle(*error);
5308  context->set_error_message_for_code_gen_from_strings(*error_handle);
5309 }

References v8::Utils::OpenHandle().

+ Here is the call graph for this function:

◆ SetSecurityToken()

void v8::Context::SetSecurityToken ( Handle< Value token)

Sets the security token for the context.

To access an object in another context, the security tokens must match.

Definition at line 5235 of file api.cc.

5235  {
5236  i::Isolate* isolate = i::Isolate::Current();
5237  ENTER_V8(isolate);
5239  i::Handle<i::Object> token_handle = Utils::OpenHandle(*token);
5240  env->set_security_token(*token_handle);
5241 }

References ENTER_V8, and v8::Utils::OpenHandle().

+ Here is the call graph for this function:

◆ SlowGetAlignedPointerFromEmbedderData()

void * v8::Context::SlowGetAlignedPointerFromEmbedderData ( int  index)
private

Definition at line 694 of file api.cc.

694  {
695  const char* location = "v8::Context::GetAlignedPointerFromEmbedderData()";
696  i::Handle<i::FixedArray> data = EmbedderDataFor(this, index, false, location);
697  if (data.is_null()) return NULL;
698  return DecodeSmiToAligned(data->get(index), location);
699 }
static void * DecodeSmiToAligned(i::Object *value, const char *location)
Definition: api.cc:638

References v8::DecodeSmiToAligned(), v8::EmbedderDataFor(), v8::internal::Handle< T >::is_null(), and NULL.

+ Here is the call graph for this function:

◆ SlowGetEmbedderData()

v8::Local< v8::Value > v8::Context::SlowGetEmbedderData ( int  index)
private

Definition at line 674 of file api.cc.

674  {
675  const char* location = "v8::Context::GetEmbedderData()";
676  i::Handle<i::FixedArray> data = EmbedderDataFor(this, index, false, location);
677  if (data.is_null()) return Local<Value>();
678  i::Handle<i::Object> result(data->get(index), data->GetIsolate());
679  return Utils::ToLocal(result);
680 }

References v8::EmbedderDataFor(), v8::internal::Handle< T >::is_null(), and v8::Utils::ToLocal().

+ Here is the call graph for this function:

◆ UseDefaultSecurityToken()

void v8::Context::UseDefaultSecurityToken ( )

Restores the security token to the default value.

Definition at line 5244 of file api.cc.

5244  {
5245  i::Isolate* isolate = i::Isolate::Current();
5246  ENTER_V8(isolate);
5248  env->set_security_token(env->global_object());
5249 }

References ENTER_V8, and v8::Utils::OpenHandle().

+ Here is the call graph for this function:

Friends And Related Function Documentation

◆ Function

friend class Function
friend

Definition at line 5594 of file v8.h.

◆ Object

friend class Object
friend

Definition at line 5593 of file v8.h.

◆ Script

friend class Script
friend

Definition at line 5592 of file v8.h.

◆ Value

friend class Value
friend

Definition at line 5591 of file v8.h.


The documentation for this class was generated from the following files: