externalize-string-extension.cc 5.55 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
// Copyright 2010 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "externalize-string-extension.h"

namespace v8 {
namespace internal {

template <typename Char, typename Base>
class SimpleStringResource : public Base {
 public:
  // Takes ownership of |data|.
  SimpleStringResource(Char* data, size_t length)
      : data_(data),
        length_(length) {}

  virtual ~SimpleStringResource() { delete[] data_; }

  virtual const Char* data() const { return data_; }

  virtual size_t length() const { return length_; }

 private:
  Char* const data_;
  const size_t length_;
};


typedef SimpleStringResource<char, v8::String::ExternalAsciiStringResource>
    SimpleAsciiStringResource;
typedef SimpleStringResource<uc16, v8::String::ExternalStringResource>
    SimpleTwoByteStringResource;


const char* const ExternalizeStringExtension::kSource =
    "native function externalizeString();"
    "native function isAsciiString();";

63 64 65
v8::Handle<v8::FunctionTemplate>
ExternalizeStringExtension::GetNativeFunctionTemplate(
    v8::Isolate* isolate, v8::Handle<v8::String> str) {
66
  if (strcmp(*v8::String::Utf8Value(str), "externalizeString") == 0) {
67 68
    return v8::FunctionTemplate::New(isolate,
                                     ExternalizeStringExtension::Externalize);
69
  } else {
70
    ASSERT(strcmp(*v8::String::Utf8Value(str), "isAsciiString") == 0);
71 72
    return v8::FunctionTemplate::New(isolate,
                                     ExternalizeStringExtension::IsAscii);
73 74 75 76
  }
}


77 78
void ExternalizeStringExtension::Externalize(
    const v8::FunctionCallbackInfo<v8::Value>& args) {
79
  if (args.Length() < 1 || !args[0]->IsString()) {
80 81
    args.GetIsolate()->ThrowException(v8::String::NewFromUtf8(
        args.GetIsolate(),
82
        "First parameter to externalizeString() must be a string."));
83
    return;
84 85 86 87 88 89
  }
  bool force_two_byte = false;
  if (args.Length() >= 2) {
    if (args[1]->IsBoolean()) {
      force_two_byte = args[1]->BooleanValue();
    } else {
90 91
      args.GetIsolate()->ThrowException(v8::String::NewFromUtf8(
        args.GetIsolate(),
92 93
        "Second parameter to externalizeString() must be a boolean."));
      return;
94 95 96 97 98
    }
  }
  bool result = false;
  Handle<String> string = Utils::OpenHandle(*args[0].As<v8::String>());
  if (string->IsExternalString()) {
99 100
    args.GetIsolate()->ThrowException(v8::String::NewFromUtf8(
        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 107
    String::WriteToFlat(*string, data, 0, string->length());
    SimpleAsciiStringResource* resource = new SimpleAsciiStringResource(
108
        reinterpret_cast<char*>(data), string->length());
109
    result = string->MakeExternal(resource);
110
    if (result) {
111 112
      i::Isolate* isolate = reinterpret_cast<i::Isolate*>(args.GetIsolate());
      isolate->heap()->external_string_table()->AddString(*string);
113 114 115 116 117 118 119 120
    }
    if (!result) delete resource;
  } else {
    uc16* data = new uc16[string->length()];
    String::WriteToFlat(*string, data, 0, string->length());
    SimpleTwoByteStringResource* resource = new SimpleTwoByteStringResource(
        data, string->length());
    result = string->MakeExternal(resource);
121
    if (result) {
122 123
      i::Isolate* isolate = reinterpret_cast<i::Isolate*>(args.GetIsolate());
      isolate->heap()->external_string_table()->AddString(*string);
124 125 126 127
    }
    if (!result) delete resource;
  }
  if (!result) {
128 129
    args.GetIsolate()->ThrowException(v8::String::NewFromUtf8(
        args.GetIsolate(), "externalizeString() failed."));
130
    return;
131 132 133 134
  }
}


135 136
void ExternalizeStringExtension::IsAscii(
    const v8::FunctionCallbackInfo<v8::Value>& args) {
137
  if (args.Length() != 1 || !args[0]->IsString()) {
138 139
    args.GetIsolate()->ThrowException(v8::String::NewFromUtf8(
        args.GetIsolate(),
140
        "isAsciiString() requires a single string argument."));
141
    return;
142
  }
143 144 145
  bool is_one_byte =
      Utils::OpenHandle(*args[0].As<v8::String>())->IsOneByteRepresentation();
  args.GetReturnValue().Set(is_one_byte);
146 147 148
}

} }  // namespace v8::internal