V8 Project
v8::internal::ExternalizeStringExtension Class Reference

#include <externalize-string-extension.h>

+ Inheritance diagram for v8::internal::ExternalizeStringExtension:
+ Collaboration diagram for v8::internal::ExternalizeStringExtension:

Public Member Functions

 ExternalizeStringExtension ()
 
virtual v8::Handle< v8::FunctionTemplateGetNativeFunctionTemplate (v8::Isolate *isolate, v8::Handle< v8::String > name)
 
- Public Member Functions inherited from v8::Extension
 Extension (const char *name, const char *source=0, int dep_count=0, const char **deps=0, int source_length=-1)
 
virtual ~Extension ()
 
const char * name () const
 
size_t source_length () const
 
const String::ExternalOneByteStringResourcesource () const
 
int dependency_count ()
 
const char ** dependencies ()
 
void set_auto_enable (bool value)
 
bool auto_enable ()
 

Static Public Member Functions

static void Externalize (const v8::FunctionCallbackInfo< v8::Value > &args)
 
static void IsOneByte (const v8::FunctionCallbackInfo< v8::Value > &args)
 

Static Private Attributes

static const char *const kSource
 

Detailed Description

Definition at line 13 of file externalize-string-extension.h.

Constructor & Destructor Documentation

◆ ExternalizeStringExtension()

v8::internal::ExternalizeStringExtension::ExternalizeStringExtension ( )
inline

Definition at line 15 of file externalize-string-extension.h.

15 : v8::Extension("v8/externalize", kSource) {}
Ignore.
Definition: v8.h:4008

Member Function Documentation

◆ Externalize()

void v8::internal::ExternalizeStringExtension::Externalize ( const v8::FunctionCallbackInfo< v8::Value > &  args)
static

Definition at line 54 of file externalize-string-extension.cc.

55  {
56  if (args.Length() < 1 || !args[0]->IsString()) {
58  args.GetIsolate(),
59  "First parameter to externalizeString() must be a string."));
60  return;
61  }
62  bool force_two_byte = false;
63  if (args.Length() >= 2) {
64  if (args[1]->IsBoolean()) {
65  force_two_byte = args[1]->BooleanValue();
66  } else {
68  args.GetIsolate(),
69  "Second parameter to externalizeString() must be a boolean."));
70  return;
71  }
72  }
73  bool result = false;
74  Handle<String> string = Utils::OpenHandle(*args[0].As<v8::String>());
75  if (string->IsExternalString()) {
77  args.GetIsolate(),
78  "externalizeString() can't externalize twice."));
79  return;
80  }
81  if (string->IsOneByteRepresentation() && !force_two_byte) {
82  uint8_t* data = new uint8_t[string->length()];
83  String::WriteToFlat(*string, data, 0, string->length());
85  reinterpret_cast<char*>(data), string->length());
86  result = string->MakeExternal(resource);
87  if (result) {
88  i::Isolate* isolate = reinterpret_cast<i::Isolate*>(args.GetIsolate());
89  isolate->heap()->external_string_table()->AddString(*string);
90  }
91  if (!result) delete resource;
92  } else {
93  uc16* data = new uc16[string->length()];
94  String::WriteToFlat(*string, data, 0, string->length());
96  data, string->length());
97  result = string->MakeExternal(resource);
98  if (result) {
99  i::Isolate* isolate = reinterpret_cast<i::Isolate*>(args.GetIsolate());
100  isolate->heap()->external_string_table()->AddString(*string);
101  }
102  if (!result) delete resource;
103  }
104  if (!result) {
106  args.GetIsolate(), "externalizeString() failed."));
107  return;
108  }
109 }
Isolate * GetIsolate() const
Definition: v8.h:6342
int Length() const
Definition: v8.h:6360
Local< Value > ThrowException(Local< Value > exception)
Schedules an exception to be thrown when returning to JavaScript.
Definition: api.cc:6411
static Local< String > NewFromUtf8(Isolate *isolate, const char *data, NewStringType type=kNormalString, int length=-1)
Allocates a new string from UTF-8 data.
Definition: api.cc:5447
static v8::internal::Handle< To > OpenHandle(v8::Local< From > handle)
Definition: api.h:288
void AddString(String *string)
Definition: heap-inl.h:644
ExternalStringTable * external_string_table()
Definition: heap.h:1207
static void WriteToFlat(String *source, sinkchar *sink, int from, int to)
Definition: objects.cc:8370
SimpleStringResource< char, v8::String::ExternalOneByteStringResource > SimpleOneByteStringResource
uint16_t uc16
Definition: globals.h:184
SimpleStringResource< uc16, v8::String::ExternalStringResource > SimpleTwoByteStringResource

References v8::internal::ExternalStringTable::AddString(), v8::internal::Heap::external_string_table(), v8::FunctionCallbackInfo< T >::GetIsolate(), v8::internal::Isolate::heap(), v8::FunctionCallbackInfo< T >::Length(), v8::String::NewFromUtf8(), v8::Utils::OpenHandle(), v8::Isolate::ThrowException(), and v8::internal::String::WriteToFlat().

Referenced by GetNativeFunctionTemplate().

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

◆ GetNativeFunctionTemplate()

v8::Handle< v8::FunctionTemplate > v8::internal::ExternalizeStringExtension::GetNativeFunctionTemplate ( v8::Isolate isolate,
v8::Handle< v8::String name 
)
virtual

Reimplemented from v8::Extension.

Definition at line 41 of file externalize-string-extension.cc.

42  {
43  if (strcmp(*v8::String::Utf8Value(str), "externalizeString") == 0) {
44  return v8::FunctionTemplate::New(isolate,
46  } else {
47  DCHECK(strcmp(*v8::String::Utf8Value(str), "isOneByteString") == 0);
48  return v8::FunctionTemplate::New(isolate,
50  }
51 }
static Local< FunctionTemplate > New(Isolate *isolate, FunctionCallback callback=0, Handle< Value > data=Handle< Value >(), Handle< Signature > signature=Handle< Signature >(), int length=0)
Creates a function template.
Definition: api.cc:904
Converts an object to a UTF-8-encoded character array.
Definition: v8.h:2048
static void IsOneByte(const v8::FunctionCallbackInfo< v8::Value > &args)
static void Externalize(const v8::FunctionCallbackInfo< v8::Value > &args)
#define DCHECK(condition)
Definition: logging.h:205

References DCHECK, Externalize(), IsOneByte(), and v8::FunctionTemplate::New().

+ Here is the call graph for this function:

◆ IsOneByte()

void v8::internal::ExternalizeStringExtension::IsOneByte ( const v8::FunctionCallbackInfo< v8::Value > &  args)
static

Definition at line 112 of file externalize-string-extension.cc.

113  {
114  if (args.Length() != 1 || !args[0]->IsString()) {
116  args.GetIsolate(),
117  "isOneByteString() requires a single string argument."));
118  return;
119  }
120  bool is_one_byte =
121  Utils::OpenHandle(*args[0].As<v8::String>())->IsOneByteRepresentation();
122  args.GetReturnValue().Set(is_one_byte);
123 }
ReturnValue< T > GetReturnValue() const
Definition: v8.h:6348

References v8::FunctionCallbackInfo< T >::GetIsolate(), v8::FunctionCallbackInfo< T >::GetReturnValue(), v8::FunctionCallbackInfo< T >::Length(), v8::String::NewFromUtf8(), v8::Utils::OpenHandle(), and v8::Isolate::ThrowException().

Referenced by GetNativeFunctionTemplate().

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

Member Data Documentation

◆ kSource

const char *const v8::internal::ExternalizeStringExtension::kSource
staticprivate
Initial value:
=
"native function externalizeString();"
"native function isOneByteString();"

Definition at line 23 of file externalize-string-extension.h.


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