V8 Project
v8::ScriptCompiler Class Reference

For compiling scripts. More...

#include <v8.h>

+ Collaboration diagram for v8::ScriptCompiler:

Classes

struct  CachedData
 Compilation data that the embedder can cache and pass back to speed up future compilations. More...
 
class  ExternalSourceStream
 For streaming incomplete script data to V8. More...
 
class  ScriptStreamingTask
 A streaming task which the embedder must run on a background thread to stream scripts into V8. More...
 
class  Source
 Source code which can be then compiled to a UnboundScript or Script. More...
 
class  StreamedSource
 Source code which can be streamed into V8 in pieces. More...
 

Public Types

enum  CompileOptions {
  kNoCompileOptions = 0 , kProduceParserCache , kConsumeParserCache , kProduceCodeCache ,
  kConsumeCodeCache , kProduceDataToCache
}
 

Static Public Member Functions

static Local< UnboundScriptCompileUnbound (Isolate *isolate, Source *source, CompileOptions options=kNoCompileOptions)
 Compiles the specified script (context-independent). More...
 
static Local< ScriptCompile (Isolate *isolate, Source *source, CompileOptions options=kNoCompileOptions)
 Compiles the specified script (bound to current context). More...
 
static ScriptStreamingTaskStartStreamingScript (Isolate *isolate, StreamedSource *source, CompileOptions options=kNoCompileOptions)
 Returns a task which streams script data into V8, or NULL if the script cannot be streamed. More...
 
static Local< ScriptCompile (Isolate *isolate, StreamedSource *source, Handle< String > full_source_string, const ScriptOrigin &origin)
 Compiles a streamed script (bound to current context). More...
 

Detailed Description

For compiling scripts.

Definition at line 1019 of file v8.h.

Member Enumeration Documentation

◆ CompileOptions

Enumerator
kNoCompileOptions 
kProduceParserCache 
kConsumeParserCache 
kProduceCodeCache 
kConsumeCodeCache 
kProduceDataToCache 

Definition at line 1159 of file v8.h.

1159  {
1160  kNoCompileOptions = 0,
1165 
1166  // Support the previous API for a transition period.
1168  };
@ kProduceParserCache
Definition: v8.h:1161
@ kConsumeParserCache
Definition: v8.h:1162
@ kProduceCodeCache
Definition: v8.h:1163
@ kNoCompileOptions
Definition: v8.h:1160
@ kProduceDataToCache
Definition: v8.h:1167
@ kConsumeCodeCache
Definition: v8.h:1164

Member Function Documentation

◆ Compile() [1/2]

Local< Script > v8::ScriptCompiler::Compile ( Isolate isolate,
Source source,
CompileOptions  options = kNoCompileOptions 
)
static

Compiles the specified script (bound to current context).

Parameters
sourceScript source code.
pre_dataPre-parsing data, as obtained by ScriptData::PreCompile() using pre_data speeds compilation if it's done multiple times. Owned by caller, no references are kept when this function returns.
Returns
Compiled script object, bound to the context that was active when this function was called. When run it will always use this context.

Definition at line 1783 of file api.cc.

1786  {
1787  i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
1788  ON_BAILOUT(isolate, "v8::ScriptCompiler::Compile()", return Local<Script>());
1789  LOG_API(isolate, "ScriptCompiler::CompiletBound()");
1790  ENTER_V8(isolate);
1791  Local<UnboundScript> generic = CompileUnbound(v8_isolate, source, options);
1792  if (generic.IsEmpty()) return Local<Script>();
1793  return generic->BindToCurrentContext();
1794 }
#define ON_BAILOUT(isolate, location, code)
Definition: api.cc:60
#define LOG_API(isolate, expr)
Definition: api.cc:52
#define ENTER_V8(isolate)
Definition: api.cc:54
static Local< UnboundScript > CompileUnbound(Isolate *isolate, Source *source, CompileOptions options=kNoCompileOptions)
Compiles the specified script (context-independent).
Definition: api.cc:1703

References CompileUnbound(), ENTER_V8, LOG_API, and ON_BAILOUT.

Referenced by v8::Script::Compile().

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

◆ Compile() [2/2]

Local< Script > v8::ScriptCompiler::Compile ( Isolate isolate,
StreamedSource source,
Handle< String full_source_string,
const ScriptOrigin origin 
)
static

Compiles a streamed script (bound to current context).

This can only be called after the streaming has finished (ScriptStreamingTask has been run). V8 doesn't construct the source string during streaming, so the embedder needs to pass the full source here.

Definition at line 1816 of file api.cc.

1819  {
1820  i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
1821  i::StreamedSource* source = v8_source->impl();
1822  ON_BAILOUT(isolate, "v8::ScriptCompiler::Compile()", return Local<Script>());
1823  LOG_API(isolate, "ScriptCompiler::Compile()");
1824  ENTER_V8(isolate);
1825  i::SharedFunctionInfo* raw_result = NULL;
1826 
1827  {
1828  i::HandleScope scope(isolate);
1829  i::Handle<i::String> str = Utils::OpenHandle(*(full_source_string));
1830  i::Handle<i::Script> script = isolate->factory()->NewScript(str);
1831  if (!origin.ResourceName().IsEmpty()) {
1832  script->set_name(*Utils::OpenHandle(*(origin.ResourceName())));
1833  }
1834  if (!origin.ResourceLineOffset().IsEmpty()) {
1835  script->set_line_offset(i::Smi::FromInt(
1836  static_cast<int>(origin.ResourceLineOffset()->Value())));
1837  }
1838  if (!origin.ResourceColumnOffset().IsEmpty()) {
1839  script->set_column_offset(i::Smi::FromInt(
1840  static_cast<int>(origin.ResourceColumnOffset()->Value())));
1841  }
1842  if (!origin.ResourceIsSharedCrossOrigin().IsEmpty()) {
1843  script->set_is_shared_cross_origin(origin.ResourceIsSharedCrossOrigin() ==
1844  v8::True(v8_isolate));
1845  }
1846  source->info->set_script(script);
1847  source->info->SetContext(isolate->global_context());
1848 
1849  EXCEPTION_PREAMBLE(isolate);
1850 
1851  // Do the parsing tasks which need to be done on the main thread. This will
1852  // also handle parse errors.
1853  source->parser->Internalize();
1854 
1857  if (source->info->function() != NULL) {
1858  // Parsing has succeeded.
1859  result =
1860  i::Compiler::CompileStreamedScript(source->info.get(), str->length());
1861  }
1862  has_pending_exception = result.is_null();
1863  if (has_pending_exception) isolate->ReportPendingMessages();
1864  EXCEPTION_BAILOUT_CHECK(isolate, Local<Script>());
1865 
1866  raw_result = *result;
1867  // The Handle<Script> will go out of scope soon; make sure CompilationInfo
1868  // doesn't point to it.
1869  source->info->set_script(i::Handle<i::Script>());
1870  } // HandleScope goes out of scope.
1871  i::Handle<i::SharedFunctionInfo> result(raw_result, isolate);
1872  Local<UnboundScript> generic = ToApiHandle<UnboundScript>(result);
1873  if (generic.IsEmpty()) {
1874  return Local<Script>();
1875  }
1876  return generic->BindToCurrentContext();
1877 }
#define EXCEPTION_BAILOUT_CHECK(isolate, value)
Definition: api.cc:93
#define EXCEPTION_PREAMBLE(isolate)
Definition: api.cc:67
static v8::internal::Handle< To > OpenHandle(v8::Local< From > handle)
Definition: api.h:288
static Handle< SharedFunctionInfo > CompileStreamedScript(CompilationInfo *info, int source_length)
Definition: compiler.cc:1230
bool is_null() const
Definition: handles.h:124
static Handle< T > null()
Definition: handles.h:123
Handle< Context > global_context()
Definition: isolate.cc:1344
void ReportPendingMessages()
Definition: isolate.cc:1194
Factory * factory()
Definition: isolate.h:982
static Smi * FromInt(int value)
Definition: objects-inl.h:1321
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
Handle< Boolean > True(Isolate *isolate)
Definition: v8.h:6854
SmartPointer< CompilationInfo > info

References v8::internal::Compiler::CompileStreamedScript(), ENTER_V8, EXCEPTION_BAILOUT_CHECK, EXCEPTION_PREAMBLE, v8::internal::Isolate::factory(), v8::internal::Smi::FromInt(), v8::internal::Isolate::global_context(), v8::ScriptCompiler::StreamedSource::impl(), v8::internal::StreamedSource::info, v8::internal::Handle< T >::is_null(), LOG_API, NULL, v8::internal::Handle< T >::null(), ON_BAILOUT, v8::Utils::OpenHandle(), v8::internal::StreamedSource::parser, v8::internal::Isolate::ReportPendingMessages(), v8::ScriptOrigin::ResourceColumnOffset(), v8::ScriptOrigin::ResourceIsSharedCrossOrigin(), v8::ScriptOrigin::ResourceLineOffset(), v8::ScriptOrigin::ResourceName(), and v8::True().

+ Here is the call graph for this function:

◆ CompileUnbound()

Local< UnboundScript > v8::ScriptCompiler::CompileUnbound ( Isolate isolate,
Source source,
CompileOptions  options = kNoCompileOptions 
)
static

Compiles the specified script (context-independent).

Cached data as part of the source object can be optionally produced to be consumed later to speed up compilation of identical source scripts.

Note that when producing cached data, the source must point to NULL for cached data. When consuming cached data, the cached data must have been produced by the same version of V8.

Parameters
sourceScript source code.
Returns
Compiled script object (context independent; for running it must be bound to a context).

Definition at line 1703 of file api.cc.

1706  {
1707  i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
1708  ON_BAILOUT(isolate, "v8::ScriptCompiler::CompileUnbound()",
1709  return Local<UnboundScript>());
1710 
1711  // Support the old API for a transition period:
1712  // - kProduceToCache -> kProduceParserCache
1713  // - kNoCompileOptions + cached_data != NULL -> kConsumeParserCache
1714  if (options == kProduceDataToCache) {
1715  options = kProduceParserCache;
1716  } else if (options == kNoCompileOptions && source->cached_data) {
1717  options = kConsumeParserCache;
1718  }
1719 
1720  i::ScriptData* script_data = NULL;
1721  if (options == kConsumeParserCache || options == kConsumeCodeCache) {
1722  DCHECK(source->cached_data);
1723  // ScriptData takes care of pointer-aligning the data.
1724  script_data = new i::ScriptData(source->cached_data->data,
1725  source->cached_data->length);
1726  }
1727 
1728  i::Handle<i::String> str = Utils::OpenHandle(*(source->source_string));
1729  LOG_API(isolate, "ScriptCompiler::CompileUnbound");
1730  ENTER_V8(isolate);
1731  i::SharedFunctionInfo* raw_result = NULL;
1732  { i::HandleScope scope(isolate);
1733  i::Handle<i::Object> name_obj;
1734  int line_offset = 0;
1735  int column_offset = 0;
1736  bool is_shared_cross_origin = false;
1737  if (!source->resource_name.IsEmpty()) {
1738  name_obj = Utils::OpenHandle(*(source->resource_name));
1739  }
1740  if (!source->resource_line_offset.IsEmpty()) {
1741  line_offset = static_cast<int>(source->resource_line_offset->Value());
1742  }
1743  if (!source->resource_column_offset.IsEmpty()) {
1744  column_offset =
1745  static_cast<int>(source->resource_column_offset->Value());
1746  }
1747  if (!source->resource_is_shared_cross_origin.IsEmpty()) {
1748  v8::Isolate* v8_isolate = reinterpret_cast<v8::Isolate*>(isolate);
1749  is_shared_cross_origin =
1750  source->resource_is_shared_cross_origin == v8::True(v8_isolate);
1751  }
1752  EXCEPTION_PREAMBLE(isolate);
1754  str, name_obj, line_offset, column_offset, is_shared_cross_origin,
1755  isolate->global_context(), NULL, &script_data, options,
1757  has_pending_exception = result.is_null();
1758  if (has_pending_exception && script_data != NULL) {
1759  // This case won't happen during normal operation; we have compiled
1760  // successfully and produced cached data, and but the second compilation
1761  // of the same source code fails.
1762  delete script_data;
1763  script_data = NULL;
1764  }
1765  EXCEPTION_BAILOUT_CHECK(isolate, Local<UnboundScript>());
1766  raw_result = *result;
1767 
1768  if ((options == kProduceParserCache || options == kProduceCodeCache) &&
1769  script_data != NULL) {
1770  // script_data now contains the data that was generated. source will
1771  // take the ownership.
1772  source->cached_data = new CachedData(
1773  script_data->data(), script_data->length(), CachedData::BufferOwned);
1774  script_data->ReleaseDataOwnership();
1775  }
1776  delete script_data;
1777  }
1778  i::Handle<i::SharedFunctionInfo> result(raw_result, isolate);
1779  return ToApiHandle<UnboundScript>(result);
1780 }
Isolate represents an isolated instance of the V8 engine.
Definition: v8.h:4356
static Handle< SharedFunctionInfo > CompileScript(Handle< String > source, Handle< Object > script_name, int line_offset, int column_offset, bool is_shared_cross_origin, Handle< Context > context, v8::Extension *extension, ScriptData **cached_data, ScriptCompiler::CompileOptions compile_options, NativesFlag is_natives_code)
Definition: compiler.cc:1134
const byte * data() const
Definition: compiler.h:40
int length() const
Definition: compiler.h:41
#define DCHECK(condition)
Definition: logging.h:205
@ NOT_NATIVES_CODE
Definition: globals.h:401

References v8::ScriptCompiler::CachedData::BufferOwned, v8::ScriptCompiler::Source::cached_data, v8::internal::Compiler::CompileScript(), v8::ScriptCompiler::CachedData::data, v8::internal::ScriptData::data(), DCHECK, ENTER_V8, EXCEPTION_BAILOUT_CHECK, EXCEPTION_PREAMBLE, v8::internal::Isolate::global_context(), v8::internal::Handle< T >::is_null(), kConsumeCodeCache, kConsumeParserCache, kNoCompileOptions, kProduceCodeCache, kProduceDataToCache, kProduceParserCache, v8::ScriptCompiler::CachedData::length, v8::internal::ScriptData::length(), LOG_API, v8::internal::NOT_NATIVES_CODE, NULL, ON_BAILOUT, v8::Utils::OpenHandle(), v8::internal::ScriptData::ReleaseDataOwnership(), v8::ScriptCompiler::Source::resource_column_offset, v8::ScriptCompiler::Source::resource_is_shared_cross_origin, v8::ScriptCompiler::Source::resource_line_offset, v8::ScriptCompiler::Source::resource_name, v8::ScriptCompiler::Source::source_string, and v8::True().

Referenced by Compile(), v8::Shell::CompileString(), and v8::Shell::RealmEval().

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

◆ StartStreamingScript()

ScriptCompiler::ScriptStreamingTask * v8::ScriptCompiler::StartStreamingScript ( Isolate isolate,
StreamedSource source,
CompileOptions  options = kNoCompileOptions 
)
static

Returns a task which streams script data into V8, or NULL if the script cannot be streamed.

The user is responsible for running the task on a background thread and deleting it. When ran, the task starts parsing the script, and it will request data from the StreamedSource as needed. When ScriptStreamingTask::Run exits, all data has been streamed and the script can be compiled (see Compile below).

This API allows to start the streaming with as little data as possible, and the remaining data (for example, the ScriptOrigin) is passed to Compile.

Definition at line 1797 of file api.cc.

1798  {
1799  i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
1800  if (!isolate->global_context().is_null() &&
1801  !isolate->global_context()->IsNativeContext()) {
1802  // The context chain is non-trivial, and constructing the corresponding
1803  // non-trivial Scope chain outside the V8 heap is not implemented. Don't
1804  // stream the script. This will only occur if Harmony scoping is enabled and
1805  // a previous script has introduced "let" or "const" variables. TODO(marja):
1806  // Implement externalizing ScopeInfos and constructing non-trivial Scope
1807  // chains independent of the V8 heap so that we can stream also in this
1808  // case.
1809  return NULL;
1810  }
1811  return new i::BackgroundParsingTask(source->impl(), options,
1812  i::FLAG_stack_size, isolate);
1813 }

References v8::internal::Isolate::global_context(), v8::ScriptCompiler::StreamedSource::impl(), and NULL.

+ Here is the call graph for this function:

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