V8 Project
v8::String Class Reference

A JavaScript string value (ECMA-262, 4.3.17). More...

#include <v8.h>

+ Inheritance diagram for v8::String:
+ Collaboration diagram for v8::String:

Classes

class  ExternalOneByteStringResource
 An ExternalOneByteStringResource is a wrapper around an one-byte string buffer that resides outside V8's heap. More...
 
class  ExternalStringResource
 An ExternalStringResource is a wrapper around a two-byte string buffer that resides outside V8's heap. More...
 
class  ExternalStringResourceBase
 
class  Utf8Value
 Converts an object to a UTF-8-encoded character array. More...
 
class  Value
 Converts an object to a two-byte string. More...
 

Public Types

enum  Encoding { UNKNOWN_ENCODING = 0x1 , TWO_BYTE_ENCODING = 0x0 , ASCII_ENCODING = 0x4 , ONE_BYTE_ENCODING = 0x4 }
 
enum  WriteOptions {
  NO_OPTIONS = 0 , HINT_MANY_WRITES_EXPECTED = 1 , NO_NULL_TERMINATION = 2 , PRESERVE_ASCII_NULL = 4 ,
  PRESERVE_ONE_BYTE_NULL = 4 , REPLACE_INVALID_UTF8 = 8
}
 Write the contents of the string to an external buffer. More...
 
enum  NewStringType { kNormalString , kInternalizedString , kUndetectableString }
 
typedef ExternalOneByteStringResource ExternalAsciiStringResource
 

Public Member Functions

int Length () const
 Returns the number of characters in this string. More...
 
int Utf8Length () const
 Returns the number of bytes in the UTF-8 encoded representation of this string. More...
 
bool IsOneByte () const
 Returns whether this string is known to contain only one byte data. More...
 
bool ContainsOnlyOneByte () const
 Returns whether this string contain only one byte data. More...
 
int Write (uint16_t *buffer, int start=0, int length=-1, int options=NO_OPTIONS) const
 
int WriteOneByte (uint8_t *buffer, int start=0, int length=-1, int options=NO_OPTIONS) const
 
int WriteUtf8 (char *buffer, int length=-1, int *nchars_ref=NULL, int options=NO_OPTIONS) const
 
bool IsExternal () const
 Returns true if the string is external. More...
 
bool IsExternalOneByte () const
 Returns true if the string is both external and one-byte. More...
 
bool IsExternalAscii () const
 
ExternalStringResourceBaseGetExternalStringResourceBase (Encoding *encoding_out) const
 If the string is an external string, return the ExternalStringResourceBase regardless of the encoding, otherwise return NULL. More...
 
ExternalStringResourceGetExternalStringResource () const
 Get the ExternalStringResource for an external string. More...
 
const ExternalOneByteStringResourceGetExternalOneByteStringResource () const
 Get the ExternalOneByteStringResource for an external one-byte string. More...
 
const ExternalAsciiStringResourceGetExternalAsciiStringResource () const
 
bool MakeExternal (ExternalStringResource *resource)
 Associate an external string resource with this string by transforming it in place so that existing references to this string in the JavaScript heap will use the external string resource. More...
 
bool MakeExternal (ExternalOneByteStringResource *resource)
 Associate an external string resource with this string by transforming it in place so that existing references to this string in the JavaScript heap will use the external string resource. More...
 
bool CanMakeExternal ()
 Returns true if this string can be made external. More...
 
- Public Member Functions inherited from v8::Value
bool IsUndefined () const
 Returns true if this value is the undefined value. More...
 
bool IsNull () const
 Returns true if this value is the null value. More...
 
bool IsTrue () const
 Returns true if this value is true. More...
 
bool IsFalse () const
 Returns true if this value is false. More...
 
bool IsName () const
 Returns true if this value is a symbol or a string. More...
 
bool IsString () const
 Returns true if this value is an instance of the String type. More...
 
bool IsSymbol () const
 Returns true if this value is a symbol. More...
 
bool IsFunction () const
 Returns true if this value is a function. More...
 
bool IsArray () const
 Returns true if this value is an array. More...
 
bool IsObject () const
 Returns true if this value is an object. More...
 
bool IsBoolean () const
 Returns true if this value is boolean. More...
 
bool IsNumber () const
 Returns true if this value is a number. More...
 
bool IsExternal () const
 Returns true if this value is external. More...
 
bool IsInt32 () const
 Returns true if this value is a 32-bit signed integer. More...
 
bool IsUint32 () const
 Returns true if this value is a 32-bit unsigned integer. More...
 
bool IsDate () const
 Returns true if this value is a Date. More...
 
bool IsArgumentsObject () const
 Returns true if this value is an Arguments object. More...
 
bool IsBooleanObject () const
 Returns true if this value is a Boolean object. More...
 
bool IsNumberObject () const
 Returns true if this value is a Number object. More...
 
bool IsStringObject () const
 Returns true if this value is a String object. More...
 
bool IsSymbolObject () const
 Returns true if this value is a Symbol object. More...
 
bool IsNativeError () const
 Returns true if this value is a NativeError. More...
 
bool IsRegExp () const
 Returns true if this value is a RegExp. More...
 
bool IsGeneratorFunction () const
 Returns true if this value is a Generator function. More...
 
bool IsGeneratorObject () const
 Returns true if this value is a Generator object (iterator). More...
 
bool IsPromise () const
 Returns true if this value is a Promise. More...
 
bool IsMap () const
 Returns true if this value is a Map. More...
 
bool IsSet () const
 Returns true if this value is a Set. More...
 
bool IsWeakMap () const
 Returns true if this value is a WeakMap. More...
 
bool IsWeakSet () const
 Returns true if this value is a WeakSet. More...
 
bool IsArrayBuffer () const
 Returns true if this value is an ArrayBuffer. More...
 
bool IsArrayBufferView () const
 Returns true if this value is an ArrayBufferView. More...
 
bool IsTypedArray () const
 Returns true if this value is one of TypedArrays. More...
 
bool IsUint8Array () const
 Returns true if this value is an Uint8Array. More...
 
bool IsUint8ClampedArray () const
 Returns true if this value is an Uint8ClampedArray. More...
 
bool IsInt8Array () const
 Returns true if this value is an Int8Array. More...
 
bool IsUint16Array () const
 Returns true if this value is an Uint16Array. More...
 
bool IsInt16Array () const
 Returns true if this value is an Int16Array. More...
 
bool IsUint32Array () const
 Returns true if this value is an Uint32Array. More...
 
bool IsInt32Array () const
 Returns true if this value is an Int32Array. More...
 
bool IsFloat32Array () const
 Returns true if this value is a Float32Array. More...
 
bool IsFloat64Array () const
 Returns true if this value is a Float64Array. More...
 
bool IsDataView () const
 Returns true if this value is a DataView. More...
 
Local< BooleanToBoolean () const
 
Local< NumberToNumber () const
 
Local< StringToString () const
 
Local< StringToDetailString () const
 
Local< ObjectToObject () const
 
Local< IntegerToInteger () const
 
Local< Uint32ToUint32 () const
 
Local< Int32ToInt32 () const
 
Local< Uint32ToArrayIndex () const
 Attempts to convert a string to an array index. More...
 
bool BooleanValue () const
 
double NumberValue () const
 
int64_t IntegerValue () const
 
uint32_t Uint32Value () const
 
int32_t Int32Value () const
 
bool Equals (Handle< Value > that) const
 JS ==. More...
 
bool StrictEquals (Handle< Value > that) const
 
bool SameValue (Handle< Value > that) const
 

Static Public Member Functions

static v8::Local< v8::StringEmpty (Isolate *isolate)
 A zero length string. More...
 
static StringCast (v8::Value *obj)
 
static Local< StringNewFromUtf8 (Isolate *isolate, const char *data, NewStringType type=kNormalString, int length=-1)
 Allocates a new string from UTF-8 data. More...
 
static Local< StringNewFromOneByte (Isolate *isolate, const uint8_t *data, NewStringType type=kNormalString, int length=-1)
 Allocates a new string from Latin-1 data. More...
 
static Local< StringNewFromTwoByte (Isolate *isolate, const uint16_t *data, NewStringType type=kNormalString, int length=-1)
 Allocates a new string from UTF-16 data. More...
 
static Local< StringConcat (Handle< String > left, Handle< String > right)
 Creates a new string by concatenating the left and the right strings passed in as parameters. More...
 
static Local< StringNewExternal (Isolate *isolate, ExternalStringResource *resource)
 Creates a new external string using the data defined in the given resource. More...
 
static Local< StringNewExternal (Isolate *isolate, ExternalOneByteStringResource *resource)
 Creates a new external string using the one-byte data defined in the given resource. More...
 
- Static Public Member Functions inherited from v8::Name
static NameCast (v8::Value *obj)
 
- Static Public Member Functions inherited from v8::Value
template<class T >
static ValueCast (T *value)
 

Private Member Functions

void VerifyExternalStringResourceBase (ExternalStringResourceBase *v, Encoding encoding) const
 
void VerifyExternalStringResource (ExternalStringResource *val) const
 

Static Private Member Functions

static void CheckCast (v8::Value *obj)
 

Detailed Description

A JavaScript string value (ECMA-262, 4.3.17).

Definition at line 1752 of file v8.h.

Member Typedef Documentation

◆ ExternalAsciiStringResource

Member Enumeration Documentation

◆ Encoding

Enumerator
UNKNOWN_ENCODING 
TWO_BYTE_ENCODING 
ASCII_ENCODING 
ONE_BYTE_ENCODING 

Definition at line 1754 of file v8.h.

1754  {
1755  UNKNOWN_ENCODING = 0x1,
1756  TWO_BYTE_ENCODING = 0x0,
1757  ASCII_ENCODING = 0x4, // TODO(yangguo): deprecate this.
1758  ONE_BYTE_ENCODING = 0x4
1759  };
@ ONE_BYTE_ENCODING
Definition: v8.h:1758
@ UNKNOWN_ENCODING
Definition: v8.h:1755
@ ASCII_ENCODING
Definition: v8.h:1757
@ TWO_BYTE_ENCODING
Definition: v8.h:1756

◆ NewStringType

Enumerator
kNormalString 
kInternalizedString 
kUndetectableString 

Definition at line 1962 of file v8.h.

1962  {
1964  };
@ kInternalizedString
Definition: v8.h:1963
@ kNormalString
Definition: v8.h:1963
@ kUndetectableString
Definition: v8.h:1963

◆ WriteOptions

Write the contents of the string to an external buffer.

If no arguments are given, expects the buffer to be large enough to hold the entire string and NULL terminator. Copies the contents of the string and the NULL terminator into the buffer.

WriteUtf8 will not write partial UTF-8 sequences, preferring to stop before the end of the buffer.

Copies up to length characters into the output buffer. Only null-terminates if there is enough space in the buffer.

Parameters
bufferThe buffer into which the string will be copied.
startThe starting position within the string at which copying begins.
lengthThe number of characters to copy from the string. For WriteUtf8 the number of bytes in the buffer.
nchars_refThe number of characters written, can be NULL.
optionsVarious options that might affect performance of this or subsequent operations.
Returns
The number of characters copied to the buffer excluding the null terminator. For WriteUtf8: The number of bytes copied to the buffer including the null terminator (if written).
Enumerator
NO_OPTIONS 
HINT_MANY_WRITES_EXPECTED 
NO_NULL_TERMINATION 
PRESERVE_ASCII_NULL 
PRESERVE_ONE_BYTE_NULL 
REPLACE_INVALID_UTF8 

Definition at line 1809 of file v8.h.

1809  {
1810  NO_OPTIONS = 0,
1812  NO_NULL_TERMINATION = 2,
1813  PRESERVE_ASCII_NULL = 4, // TODO(yangguo): deprecate this.
1815  // Used by WriteUtf8 to replace orphan surrogate code units with the
1816  // unicode replacement character. Needs to be set to guarantee valid UTF-8
1817  // output.
1819  };
@ NO_OPTIONS
Definition: v8.h:1810
@ NO_NULL_TERMINATION
Definition: v8.h:1812
@ HINT_MANY_WRITES_EXPECTED
Definition: v8.h:1811
@ PRESERVE_ASCII_NULL
Definition: v8.h:1813
@ PRESERVE_ONE_BYTE_NULL
Definition: v8.h:1814
@ REPLACE_INVALID_UTF8
Definition: v8.h:1818

Member Function Documentation

◆ CanMakeExternal()

bool v8::String::CanMakeExternal ( )

Returns true if this string can be made external.

Definition at line 5597 of file api.cc.

5597  {
5598  if (!internal::FLAG_clever_optimizations) return false;
5600  i::Isolate* isolate = obj->GetIsolate();
5601 
5602  if (isolate->string_tracker()->IsFreshUnusedString(obj)) return false;
5603  int size = obj->Size(); // Byte size of the original string.
5604  if (size < i::ExternalString::kShortSize) return false;
5605  i::StringShape shape(*obj);
5606  return !shape.IsExternal();
5607 }
static v8::internal::Handle< To > OpenHandle(v8::Local< From > handle)
Definition: api.h:288
static const int kShortSize
Definition: objects.h:9137
StringTracker * string_tracker()
Definition: isolate.h:923
bool IsFreshUnusedString(Handle< String > string)
Definition: api.h:411
enable harmony numeric enable harmony object literal extensions Optimize object size

References v8::internal::StringTracker::IsFreshUnusedString(), v8::internal::ExternalString::kShortSize, v8::Utils::OpenHandle(), size, and v8::internal::Isolate::string_tracker().

+ Here is the call graph for this function:

◆ Cast()

String * v8::String::Cast ( v8::Value obj)
inlinestatic

Definition at line 6461 of file v8.h.

6461  {
6462 #ifdef V8_ENABLE_CHECKS
6463  CheckCast(value);
6464 #endif
6465  return static_cast<String*>(value);
6466 }
static void CheckCast(v8::Value *obj)
Definition: api.cc:2730

◆ CheckCast()

void v8::String::CheckCast ( v8::Value obj)
staticprivate

Definition at line 2730 of file api.cc.

2730  {
2732  Utils::ApiCheck(obj->IsString(),
2733  "v8::String::Cast()",
2734  "Could not convert to string");
2735 }
static bool ApiCheck(bool condition, const char *location, const char *message)
Definition: api.h:177

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

+ Here is the call graph for this function:

◆ Concat()

Local< String > v8::String::Concat ( Handle< String left,
Handle< String right 
)
static

Creates a new string by concatenating the left and the right strings passed in as parameters.

Definition at line 5486 of file api.cc.

5486  {
5487  i::Handle<i::String> left_string = Utils::OpenHandle(*left);
5488  i::Isolate* isolate = left_string->GetIsolate();
5489  LOG_API(isolate, "String::New(char)");
5490  ENTER_V8(isolate);
5491  i::Handle<i::String> right_string = Utils::OpenHandle(*right);
5492  // We do not expect this to fail. Change this if it does.
5493  i::Handle<i::String> result = isolate->factory()->NewConsString(
5494  left_string, right_string).ToHandleChecked();
5495  return Utils::ToLocal(result);
5496 }
#define LOG_API(isolate, expr)
Definition: api.cc:52
#define ENTER_V8(isolate)
Definition: api.cc:54
static Local< Context > ToLocal(v8::internal::Handle< v8::internal::Context > obj)
Factory * factory()
Definition: isolate.h:982

References ENTER_V8, v8::internal::Isolate::factory(), LOG_API, v8::Utils::OpenHandle(), and v8::Utils::ToLocal().

Referenced by v8::GetStdout(), and v8::Shell::ReadFromStdin().

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

◆ ContainsOnlyOneByte()

bool v8::String::ContainsOnlyOneByte ( ) const

Returns whether this string contain only one byte data.

Will read the entire string in some cases.

Definition at line 4360 of file api.cc.

4360  {
4362  if (str->HasOnlyOneByteChars()) return true;
4363  ContainsOnlyOneByteHelper helper;
4364  return helper.Check(*str);
4365 }

References v8::ContainsOnlyOneByteHelper::Check(), and v8::Utils::OpenHandle().

+ Here is the call graph for this function:

◆ Empty()

Local< String > v8::String::Empty ( Isolate isolate)
inlinestatic

A zero length string.

Definition at line 6469 of file v8.h.

6469  {
6470  typedef internal::Object* S;
6471  typedef internal::Internals I;
6472  I::CheckInitialized(isolate);
6473  S* slot = I::GetRoot(isolate, I::kEmptyStringRootIndex);
6474  return Local<String>(reinterpret_cast<String*>(slot));
6475 }
kSerializedDataOffset Object
Definition: objects-inl.h:5322
#define I(name, number_of_args, result_size)
Definition: runtime.cc:9248
#define S(x)
Definition: version.cc:55

References I, and S.

Referenced by v8::GetStdout(), and v8::anonymous_namespace{api.cc}::NewString().

+ Here is the caller graph for this function:

◆ GetExternalAsciiStringResource()

const ExternalAsciiStringResource* v8::String::GetExternalAsciiStringResource ( ) const
inline

Definition at line 1956 of file v8.h.

1956  {
1958  }
const ExternalOneByteStringResource * GetExternalOneByteStringResource() const
Get the ExternalOneByteStringResource for an external one-byte string.
Definition: api.cc:4920

◆ GetExternalOneByteStringResource()

const v8::String::ExternalOneByteStringResource * v8::String::GetExternalOneByteStringResource ( ) const

Get the ExternalOneByteStringResource for an external one-byte string.

Returns NULL if IsExternalOneByte() doesn't return true.

Definition at line 4920 of file api.cc.

4920  {
4922  if (i::StringShape(*str).IsExternalOneByte()) {
4923  const void* resource =
4925  return reinterpret_cast<const ExternalOneByteStringResource*>(resource);
4926  } else {
4927  return NULL;
4928  }
4929 }
static Handle< T > cast(Handle< S > that)
Definition: handles.h:116
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

References v8::internal::Handle< T >::cast(), NULL, and v8::Utils::OpenHandle().

+ Here is the call graph for this function:

◆ GetExternalStringResource()

String::ExternalStringResource * v8::String::GetExternalStringResource ( ) const
inline

Get the ExternalStringResource for an external string.

Returns NULL if IsExternal() doesn't return true.

Definition at line 6478 of file v8.h.

6478  {
6479  typedef internal::Object O;
6480  typedef internal::Internals I;
6481  O* obj = *reinterpret_cast<O* const*>(this);
6482  String::ExternalStringResource* result;
6483  if (I::IsExternalTwoByteString(I::GetInstanceType(obj))) {
6484  void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
6485  result = reinterpret_cast<String::ExternalStringResource*>(value);
6486  } else {
6487  result = NULL;
6488  }
6489 #ifdef V8_ENABLE_CHECKS
6491 #endif
6492  return result;
6493 }
void VerifyExternalStringResource(ExternalStringResource *val) const
Definition: api.cc:4881

References I, and NULL.

◆ GetExternalStringResourceBase()

String::ExternalStringResourceBase * v8::String::GetExternalStringResourceBase ( String::Encoding encoding_out) const
inline

If the string is an external string, return the ExternalStringResourceBase regardless of the encoding, otherwise return NULL.

The encoding of the string is returned in encoding_out.

Definition at line 6496 of file v8.h.

6497  {
6498  typedef internal::Object O;
6499  typedef internal::Internals I;
6500  O* obj = *reinterpret_cast<O* const*>(this);
6501  int type = I::GetInstanceType(obj) & I::kFullStringRepresentationMask;
6502  *encoding_out = static_cast<Encoding>(type & I::kStringEncodingMask);
6503  ExternalStringResourceBase* resource = NULL;
6504  if (type == I::kExternalOneByteRepresentationTag ||
6505  type == I::kExternalTwoByteRepresentationTag) {
6506  void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
6507  resource = static_cast<ExternalStringResourceBase*>(value);
6508  }
6509 #ifdef V8_ENABLE_CHECKS
6510  VerifyExternalStringResourceBase(resource, *encoding_out);
6511 #endif
6512  return resource;
6513 }
Encoding
Definition: v8.h:1754
void VerifyExternalStringResourceBase(ExternalStringResourceBase *v, Encoding encoding) const
Definition: api.cc:4895
const uint32_t kStringEncodingMask
Definition: objects.h:555

References I, v8::internal::kStringEncodingMask, and NULL.

◆ IsExternal()

bool v8::String::IsExternal ( ) const

Returns true if the string is external.

Definition at line 4869 of file api.cc.

4869  {
4871  return i::StringShape(*str).IsExternalTwoByte();
4872 }

References v8::Utils::OpenHandle().

+ Here is the call graph for this function:

◆ IsExternalAscii()

bool v8::String::IsExternalAscii ( ) const
inline

Definition at line 1853 of file v8.h.

1853 { return IsExternalOneByte(); }
bool IsExternalOneByte() const
Returns true if the string is both external and one-byte.
Definition: api.cc:4875

◆ IsExternalOneByte()

bool v8::String::IsExternalOneByte ( ) const

Returns true if the string is both external and one-byte.

Definition at line 4875 of file api.cc.

4875  {
4877  return i::StringShape(*str).IsExternalOneByte();
4878 }

References v8::Utils::OpenHandle().

+ Here is the call graph for this function:

◆ IsOneByte()

bool v8::String::IsOneByte ( ) const

Returns whether this string is known to contain only one byte data.

Does not read the string. False negatives are possible.

Definition at line 4245 of file api.cc.

4245  {
4247  return str->HasOnlyOneByteChars();
4248 }

References v8::Utils::OpenHandle().

+ Here is the call graph for this function:

◆ Length()

int v8::String::Length ( ) const

Returns the number of characters in this string.

Definition at line 4239 of file api.cc.

4239  {
4241  return str->length();
4242 }

References v8::Utils::OpenHandle().

+ Here is the call graph for this function:

◆ MakeExternal() [1/2]

bool v8::String::MakeExternal ( ExternalOneByteStringResource resource)

Associate an external string resource with this string by transforming it in place so that existing references to this string in the JavaScript heap will use the external string resource.

The external string resource's character contents need to be equivalent to this string. Returns true if the string has been changed to be an external string. The string is not modified if the operation fails. See NewExternal for information on the lifetime of the resource.

Definition at line 5570 of file api.cc.

5571  {
5573  i::Isolate* isolate = obj->GetIsolate();
5574  if (i::StringShape(*obj).IsExternal()) {
5575  return false; // Already an external string.
5576  }
5577  ENTER_V8(isolate);
5578  if (isolate->string_tracker()->IsFreshUnusedString(obj)) {
5579  return false;
5580  }
5581  if (isolate->heap()->IsInGCPostProcessing()) {
5582  return false;
5583  }
5584  CHECK(resource && resource->data());
5585 
5586  bool result = obj->MakeExternal(resource);
5587  // Assert that if CanMakeExternal(), then externalizing actually succeeds.
5588  DCHECK(!CanMakeExternal() || result);
5589  if (result) {
5590  DCHECK(obj->IsExternalString());
5591  isolate->heap()->external_string_table()->AddString(*obj);
5592  }
5593  return result;
5594 }
bool CanMakeExternal()
Returns true if this string can be made external.
Definition: api.cc:5597
void AddString(String *string)
Definition: heap-inl.h:644
ExternalStringTable * external_string_table()
Definition: heap.h:1207
bool IsInGCPostProcessing()
Definition: heap.h:957
#define CHECK(condition)
Definition: logging.h:36
#define DCHECK(condition)
Definition: logging.h:205

References v8::internal::ExternalStringTable::AddString(), CHECK, v8::String::ExternalOneByteStringResource::data(), DCHECK, ENTER_V8, v8::internal::Heap::external_string_table(), v8::internal::Isolate::heap(), v8::internal::StringTracker::IsFreshUnusedString(), v8::internal::Heap::IsInGCPostProcessing(), v8::Utils::OpenHandle(), and v8::internal::Isolate::string_tracker().

+ Here is the call graph for this function:

◆ MakeExternal() [2/2]

bool v8::String::MakeExternal ( ExternalStringResource resource)

Associate an external string resource with this string by transforming it in place so that existing references to this string in the JavaScript heap will use the external string resource.

The external string resource's character contents need to be equivalent to this string. Returns true if the string has been changed to be an external string. The string is not modified if the operation fails. See NewExternal for information on the lifetime of the resource.

Definition at line 5528 of file api.cc.

5528  {
5530  i::Isolate* isolate = obj->GetIsolate();
5531  if (i::StringShape(*obj).IsExternal()) {
5532  return false; // Already an external string.
5533  }
5534  ENTER_V8(isolate);
5535  if (isolate->string_tracker()->IsFreshUnusedString(obj)) {
5536  return false;
5537  }
5538  if (isolate->heap()->IsInGCPostProcessing()) {
5539  return false;
5540  }
5541  CHECK(resource && resource->data());
5542 
5543  bool result = obj->MakeExternal(resource);
5544  // Assert that if CanMakeExternal(), then externalizing actually succeeds.
5545  DCHECK(!CanMakeExternal() || result);
5546  if (result) {
5547  DCHECK(obj->IsExternalString());
5548  isolate->heap()->external_string_table()->AddString(*obj);
5549  }
5550  return result;
5551 }

References v8::internal::ExternalStringTable::AddString(), CHECK, v8::String::ExternalStringResource::data(), DCHECK, ENTER_V8, v8::internal::Heap::external_string_table(), v8::internal::Isolate::heap(), v8::internal::StringTracker::IsFreshUnusedString(), v8::internal::Heap::IsInGCPostProcessing(), v8::Utils::OpenHandle(), and v8::internal::Isolate::string_tracker().

+ Here is the call graph for this function:

◆ NewExternal() [1/2]

Local< String > v8::String::NewExternal ( Isolate isolate,
ExternalOneByteStringResource resource 
)
static

Creates a new external string using the one-byte data defined in the given resource.

When the external string is no longer live on V8's heap the resource will be disposed by calling its Dispose method. The caller of this function should not otherwise delete or modify the resource. Neither should the underlying buffer be deallocated or modified except through the destructor of the external string resource.

Definition at line 5554 of file api.cc.

5555  {
5556  i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
5557  LOG_API(i_isolate, "String::NewExternal");
5558  ENTER_V8(i_isolate);
5559  CHECK(resource && resource->data());
5560  EXCEPTION_PREAMBLE(i_isolate);
5561  i::Handle<i::String> string;
5562  has_pending_exception =
5563  !NewExternalOneByteStringHandle(i_isolate, resource).ToHandle(&string);
5564  EXCEPTION_BAILOUT_CHECK(i_isolate, Local<String>());
5565  i_isolate->heap()->external_string_table()->AddString(*string);
5566  return Utils::ToLocal(string);
5567 }
#define EXCEPTION_BAILOUT_CHECK(isolate, value)
Definition: api.cc:93
#define EXCEPTION_PREAMBLE(isolate)
Definition: api.cc:67
static i::MaybeHandle< i::String > NewExternalOneByteStringHandle(i::Isolate *isolate, v8::String::ExternalOneByteStringResource *resource)
Definition: api.cc:5505

References v8::internal::ExternalStringTable::AddString(), CHECK, v8::String::ExternalOneByteStringResource::data(), ENTER_V8, EXCEPTION_BAILOUT_CHECK, EXCEPTION_PREAMBLE, v8::internal::Heap::external_string_table(), v8::internal::Isolate::heap(), LOG_API, v8::NewExternalOneByteStringHandle(), and v8::Utils::ToLocal().

+ Here is the call graph for this function:

◆ NewExternal() [2/2]

Local< String > v8::String::NewExternal ( Isolate isolate,
ExternalStringResource resource 
)
static

Creates a new external string using the data defined in the given resource.

When the external string is no longer live on V8's heap the resource will be disposed by calling its Dispose method. The caller of this function should not otherwise delete or modify the resource. Neither should the underlying buffer be deallocated or modified except through the destructor of the external string resource.

Definition at line 5511 of file api.cc.

5513  {
5514  i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
5515  LOG_API(i_isolate, "String::NewExternal");
5516  ENTER_V8(i_isolate);
5517  CHECK(resource && resource->data());
5518  EXCEPTION_PREAMBLE(i_isolate);
5519  i::Handle<i::String> string;
5520  has_pending_exception =
5521  !NewExternalStringHandle(i_isolate, resource).ToHandle(&string);
5522  EXCEPTION_BAILOUT_CHECK(i_isolate, Local<String>());
5523  i_isolate->heap()->external_string_table()->AddString(*string);
5524  return Utils::ToLocal(string);
5525 }
static i::MaybeHandle< i::String > NewExternalStringHandle(i::Isolate *isolate, v8::String::ExternalStringResource *resource)
Definition: api.cc:5499

References v8::internal::ExternalStringTable::AddString(), CHECK, v8::String::ExternalStringResource::data(), ENTER_V8, EXCEPTION_BAILOUT_CHECK, EXCEPTION_PREAMBLE, v8::internal::Heap::external_string_table(), v8::internal::Isolate::heap(), LOG_API, v8::NewExternalStringHandle(), and v8::Utils::ToLocal().

+ Here is the call graph for this function:

◆ NewFromOneByte()

Local< String > v8::String::NewFromOneByte ( Isolate isolate,
const uint8_t *  data,
NewStringType  type = kNormalString,
int  length = -1 
)
static

Allocates a new string from Latin-1 data.

Definition at line 5460 of file api.cc.

5463  {
5464  return NewString(isolate,
5465  "v8::String::NewFromOneByte()",
5466  "String::NewFromOneByte",
5467  data,
5468  type,
5469  length);
5470 }
Local< String > NewString(Isolate *v8_isolate, const char *location, const char *env, const Char *data, String::NewStringType type, int length)
Definition: api.cc:5420

References v8::anonymous_namespace{api.cc}::NewString().

+ Here is the call graph for this function:

◆ NewFromTwoByte()

Local< String > v8::String::NewFromTwoByte ( Isolate isolate,
const uint16_t *  data,
NewStringType  type = kNormalString,
int  length = -1 
)
static

Allocates a new string from UTF-16 data.

Definition at line 5473 of file api.cc.

5476  {
5477  return NewString(isolate,
5478  "v8::String::NewFromTwoByte()",
5479  "String::NewFromTwoByte",
5480  data,
5481  type,
5482  length);
5483 }

References v8::anonymous_namespace{api.cc}::NewString().

+ Here is the call graph for this function:

◆ NewFromUtf8()

Local< String > v8::String::NewFromUtf8 ( Isolate isolate,
const char *  data,
NewStringType  type = kNormalString,
int  length = -1 
)
static

Allocates a new string from UTF-8 data.

Definition at line 5447 of file api.cc.

5450  {
5451  return NewString(isolate,
5452  "v8::String::NewFromUtf8()",
5453  "String::NewFromUtf8",
5454  data,
5455  type,
5456  length);
5457 }

References v8::anonymous_namespace{api.cc}::NewString().

Referenced by v8::internal::AddCounter(), v8::internal::AddNumber(), v8::internal::AddNumber64(), v8::Shell::AddOSMethods(), v8::Shell::ChangeDirectory(), v8::CheckItsADirectory(), v8::ChildLaunchedOK(), v8::ReadLineEditor::CompletionGenerator(), v8::Shell::CreateEvaluationContext(), v8::Shell::CreateGlobalTemplate(), v8::Shell::DebugCommandToJSONRequest(), v8::Shell::DebugMessageDetails(), v8::SourceGroup::Execute(), v8::Shell::ExecuteString(), v8::internal::ExternalizeStringExtension::Externalize(), v8::Shell::GetCompletions(), v8::GetStdout(), v8::GetTimeouts(), v8::GetWellKnownSymbol(), v8::HandleDebugEvent(), v8::ExecArgs::Init(), v8::Shell::InstallUtilityScript(), v8::internal::ExternalizeStringExtension::IsOneByte(), v8::Shell::Load(), main(), v8::Shell::MakeDirectory(), v8::mkdirp(), v8::Object::ObjectProtoToString(), v8::ReadLineEditor::Prompt(), v8::SourceGroup::ReadFile(), v8::Shell::ReadFile(), v8::Shell::ReadFromStdin(), v8::Shell::RemoveDirectory(), v8::Shell::RunShell(), v8::Template::Set(), v8::Shell::SetEnvironment(), v8::Shell::SetUMask(), v8::Shell::System(), v8::Throw(), v8::Shell::UnsetEnvironment(), v8::Shell::Version(), and v8::WaitForChild().

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

◆ Utf8Length()

int v8::String::Utf8Length ( ) const

Returns the number of bytes in the UTF-8 encoded representation of this string.

Definition at line 4560 of file api.cc.

4560  {
4562  i::Isolate* isolate = str->GetIsolate();
4563  return v8::Utf8Length(*str, isolate);
4564 }
static int Utf8Length(i::String *str, i::Isolate *isolate)
Definition: api.cc:4549

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

+ Here is the call graph for this function:

◆ VerifyExternalStringResource()

void v8::String::VerifyExternalStringResource ( ExternalStringResource val) const
private

Definition at line 4881 of file api.cc.

4882  {
4884  const v8::String::ExternalStringResource* expected;
4885  if (i::StringShape(*str).IsExternalTwoByte()) {
4886  const void* resource =
4888  expected = reinterpret_cast<const ExternalStringResource*>(resource);
4889  } else {
4890  expected = NULL;
4891  }
4892  CHECK_EQ(expected, value);
4893 }
An ExternalStringResource is a wrapper around a two-byte string buffer that resides outside V8's heap...
Definition: v8.h:1885
#define CHECK_EQ(expected, value)
Definition: logging.h:169

References v8::internal::Handle< T >::cast(), CHECK_EQ, NULL, and v8::Utils::OpenHandle().

+ Here is the call graph for this function:

◆ VerifyExternalStringResourceBase()

void v8::String::VerifyExternalStringResourceBase ( ExternalStringResourceBase v,
Encoding  encoding 
) const
private

Definition at line 4895 of file api.cc.

4896  {
4899  Encoding expectedEncoding;
4900  if (i::StringShape(*str).IsExternalOneByte()) {
4901  const void* resource =
4903  expected = reinterpret_cast<const ExternalStringResourceBase*>(resource);
4904  expectedEncoding = ONE_BYTE_ENCODING;
4905  } else if (i::StringShape(*str).IsExternalTwoByte()) {
4906  const void* resource =
4908  expected = reinterpret_cast<const ExternalStringResourceBase*>(resource);
4909  expectedEncoding = TWO_BYTE_ENCODING;
4910  } else {
4911  expected = NULL;
4912  expectedEncoding =
4913  str->IsOneByteRepresentation() ? ONE_BYTE_ENCODING : TWO_BYTE_ENCODING;
4914  }
4915  CHECK_EQ(expected, value);
4916  CHECK_EQ(expectedEncoding, encoding);
4917 }

References v8::internal::Handle< T >::cast(), CHECK_EQ, NULL, and v8::Utils::OpenHandle().

+ Here is the call graph for this function:

◆ Write()

int v8::String::Write ( uint16_t *  buffer,
int  start = 0,
int  length = -1,
int  options = NO_OPTIONS 
) const

Definition at line 4861 of file api.cc.

4864  {
4865  return WriteHelper(this, buffer, start, length, options);
4866 }
static int WriteHelper(const String *string, CharType *buffer, int start, int length, int options)
Definition: api.cc:4824

References v8::WriteHelper().

+ Here is the call graph for this function:

◆ WriteOneByte()

int v8::String::WriteOneByte ( uint8_t *  buffer,
int  start = 0,
int  length = -1,
int  options = NO_OPTIONS 
) const

Definition at line 4853 of file api.cc.

4856  {
4857  return WriteHelper(this, buffer, start, length, options);
4858 }

References v8::WriteHelper().

+ Here is the call graph for this function:

◆ WriteUtf8()

int v8::String::WriteUtf8 ( char *  buffer,
int  length = -1,
int nchars_ref = NULL,
int  options = NO_OPTIONS 
) const

Definition at line 4772 of file api.cc.

4775  {
4776  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
4777  LOG_API(isolate, "String::WriteUtf8");
4778  ENTER_V8(isolate);
4780  if (options & HINT_MANY_WRITES_EXPECTED) {
4781  str = i::String::Flatten(str); // Flatten the string for efficiency.
4782  }
4783  const int string_length = str->length();
4784  bool write_null = !(options & NO_NULL_TERMINATION);
4785  bool replace_invalid_utf8 = (options & REPLACE_INVALID_UTF8);
4786  int max16BitCodeUnitSize = unibrow::Utf8::kMax16BitCodeUnitSize;
4787  // First check if we can just write the string without checking capacity.
4788  if (capacity == -1 || capacity / max16BitCodeUnitSize >= string_length) {
4789  Utf8WriterVisitor writer(buffer, capacity, true, replace_invalid_utf8);
4790  const int kMaxRecursion = 100;
4791  bool success = RecursivelySerializeToUtf8(*str, &writer, kMaxRecursion);
4792  if (success) return writer.CompleteWrite(write_null, nchars_ref);
4793  } else if (capacity >= string_length) {
4794  // First check that the buffer is large enough.
4795  int utf8_bytes = v8::Utf8Length(*str, str->GetIsolate());
4796  if (utf8_bytes <= capacity) {
4797  // one-byte fast path.
4798  if (utf8_bytes == string_length) {
4799  WriteOneByte(reinterpret_cast<uint8_t*>(buffer), 0, capacity, options);
4800  if (nchars_ref != NULL) *nchars_ref = string_length;
4801  if (write_null && (utf8_bytes+1 <= capacity)) {
4802  return string_length + 1;
4803  }
4804  return string_length;
4805  }
4806  if (write_null && (utf8_bytes+1 > capacity)) {
4807  options |= NO_NULL_TERMINATION;
4808  }
4809  // Recurse once without a capacity limit.
4810  // This will get into the first branch above.
4811  // TODO(dcarney) Check max left rec. in Utf8Length and fall through.
4812  return WriteUtf8(buffer, -1, nchars_ref, options);
4813  }
4814  }
4815  // Recursive slow path can potentially be unreasonable slow. Flatten.
4816  str = i::String::Flatten(str);
4817  Utf8WriterVisitor writer(buffer, capacity, false, replace_invalid_utf8);
4818  i::String::VisitFlat(&writer, *str);
4819  return writer.CompleteWrite(write_null, nchars_ref);
4820 }
static const unsigned kMax16BitCodeUnitSize
Definition: unicode.h:152
int WriteOneByte(uint8_t *buffer, int start=0, int length=-1, int options=NO_OPTIONS) const
Definition: api.cc:4853
int WriteUtf8(char *buffer, int length=-1, int *nchars_ref=NULL, int options=NO_OPTIONS) const
Definition: api.cc:4772
static ConsString * VisitFlat(Visitor *visitor, String *string, int offset=0)
Definition: objects-inl.h:3416
static Handle< String > Flatten(Handle< String > string, PretenureFlag pretenure=NOT_TENURED)
Definition: objects-inl.h:3354
static bool RecursivelySerializeToUtf8(i::String *current, Utf8WriterVisitor *writer, int recursion_budget)
Definition: api.cc:4752

References v8::Utf8WriterVisitor::CompleteWrite(), ENTER_V8, v8::internal::String::Flatten(), unibrow::Utf8::kMax16BitCodeUnitSize, LOG_API, NULL, v8::Utils::OpenHandle(), v8::RecursivelySerializeToUtf8(), v8::Utf8Length(), and v8::internal::String::VisitFlat().

+ Here is the call graph for this function:

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