v8  3.25.30(node0.11.13)
V8 is Google's open source JavaScript engine
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
externalize-string-extension.cc
Go to the documentation of this file.
1 // Copyright 2010 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
29 
30 namespace v8 {
31 namespace internal {
32 
33 template <typename Char, typename Base>
34 class SimpleStringResource : public Base {
35  public:
36  // Takes ownership of |data|.
38  : data_(data),
39  length_(length) {}
40 
41  virtual ~SimpleStringResource() { delete[] data_; }
42 
43  virtual const Char* data() const { return data_; }
44 
45  virtual size_t length() const { return length_; }
46 
47  private:
48  Char* const data_;
49  const size_t length_;
50 };
51 
52 
53 typedef SimpleStringResource<char, v8::String::ExternalAsciiStringResource>
57 
58 
59 const char* const ExternalizeStringExtension::kSource =
60  "native function externalizeString();"
61  "native function isAsciiString();";
62 
65  v8::Isolate* isolate, v8::Handle<v8::String> str) {
66  if (strcmp(*v8::String::Utf8Value(str), "externalizeString") == 0) {
67  return v8::FunctionTemplate::New(isolate,
69  } else {
70  ASSERT(strcmp(*v8::String::Utf8Value(str), "isAsciiString") == 0);
71  return v8::FunctionTemplate::New(isolate,
73  }
74 }
75 
76 
79  if (args.Length() < 1 || !args[0]->IsString()) {
80  args.GetIsolate()->ThrowException(v8::String::NewFromUtf8(
81  args.GetIsolate(),
82  "First parameter to externalizeString() must be a string."));
83  return;
84  }
85  bool force_two_byte = false;
86  if (args.Length() >= 2) {
87  if (args[1]->IsBoolean()) {
88  force_two_byte = args[1]->BooleanValue();
89  } else {
90  args.GetIsolate()->ThrowException(v8::String::NewFromUtf8(
91  args.GetIsolate(),
92  "Second parameter to externalizeString() must be a boolean."));
93  return;
94  }
95  }
96  bool result = false;
97  Handle<String> string = Utils::OpenHandle(*args[0].As<v8::String>());
98  if (string->IsExternalString()) {
99  args.GetIsolate()->ThrowException(v8::String::NewFromUtf8(
100  args.GetIsolate(),
101  "externalizeString() can't externalize twice."));
102  return;
103  }
104  if (string->IsOneByteRepresentation() && !force_two_byte) {
105  uint8_t* data = new uint8_t[string->length()];
106  String::WriteToFlat(*string, data, 0, string->length());
108  reinterpret_cast<char*>(data), string->length());
109  result = string->MakeExternal(resource);
110  if (result) {
111  i::Isolate* isolate = reinterpret_cast<i::Isolate*>(args.GetIsolate());
112  isolate->heap()->external_string_table()->AddString(*string);
113  }
114  if (!result) delete resource;
115  } else {
116  uc16* data = new uc16[string->length()];
117  String::WriteToFlat(*string, data, 0, string->length());
119  data, string->length());
120  result = string->MakeExternal(resource);
121  if (result) {
122  i::Isolate* isolate = reinterpret_cast<i::Isolate*>(args.GetIsolate());
123  isolate->heap()->external_string_table()->AddString(*string);
124  }
125  if (!result) delete resource;
126  }
127  if (!result) {
128  args.GetIsolate()->ThrowException(v8::String::NewFromUtf8(
129  args.GetIsolate(), "externalizeString() failed."));
130  return;
131  }
132 }
133 
134 
137  if (args.Length() != 1 || !args[0]->IsString()) {
138  args.GetIsolate()->ThrowException(v8::String::NewFromUtf8(
139  args.GetIsolate(),
140  "isAsciiString() requires a single string argument."));
141  return;
142  }
143  bool is_one_byte =
144  Utils::OpenHandle(*args[0].As<v8::String>())->IsOneByteRepresentation();
145  args.GetReturnValue().Set(is_one_byte);
146 }
147 
148 } } // namespace v8::internal
V8_INLINE Isolate * GetIsolate() const
Definition: v8.h:6061
V8_INLINE ReturnValue< T > GetReturnValue() const
Definition: v8.h:6067
static void Externalize(const v8::FunctionCallbackInfo< v8::Value > &args)
void AddString(String *string)
Definition: heap-inl.h:697
V8_INLINE int Length() const
Definition: v8.h:6079
#define ASSERT(condition)
Definition: checks.h:329
static void IsAscii(const v8::FunctionCallbackInfo< v8::Value > &args)
static Local< FunctionTemplate > New(Isolate *isolate, FunctionCallback callback=0, Handle< Value > data=Handle< Value >(), Handle< Signature > signature=Handle< Signature >(), int length=0)
Definition: api.cc:942
virtual v8::Handle< v8::FunctionTemplate > GetNativeFunctionTemplate(v8::Isolate *isolate, v8::Handle< v8::String > name)
Definition: v8.h:123
static v8::internal::Handle< To > OpenHandle(v8::Local< From > handle)
Definition: api.h:308
static void WriteToFlat(String *source, sinkchar *sink, int from, int to)
Definition: objects.cc:8635
ExternalStringTable * external_string_table()
Definition: heap.h:1799
uint16_t uc16
Definition: globals.h:309
SimpleStringResource< char, v8::String::ExternalAsciiStringResource > SimpleAsciiStringResource
SimpleStringResource< uc16, v8::String::ExternalStringResource > SimpleTwoByteStringResource
static Local< String > NewFromUtf8(Isolate *isolate, const char *data, NewStringType type=kNormalString, int length=-1)
Definition: api.cc:5417