externalize-string-extension.cc 4.14 KB
Newer Older
1
// Copyright 2010 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5
#include "src/extensions/externalize-string-extension.h"
6

7
#include "src/api/api-inl.h"
8
#include "src/execution/isolate.h"
9
#include "src/handles/handles.h"
10
#include "src/objects/objects-inl.h"
11

12 13 14 15 16 17 18 19 20 21 22
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) {}

23
  ~SimpleStringResource() override { delete[] data_; }
24

25
  const Char* data() const override { return data_; }
26

27
  size_t length() const override { return length_; }
28 29 30 31 32 33

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

34 35 36 37
using SimpleOneByteStringResource =
    SimpleStringResource<char, v8::String::ExternalOneByteStringResource>;
using SimpleTwoByteStringResource =
    SimpleStringResource<uc16, v8::String::ExternalStringResource>;
38 39 40

const char* const ExternalizeStringExtension::kSource =
    "native function externalizeString();"
41 42
    "native function isOneByteString();"
    "function x() { return 1; }";
43

44
v8::Local<v8::FunctionTemplate>
45
ExternalizeStringExtension::GetNativeFunctionTemplate(
46
    v8::Isolate* isolate, v8::Local<v8::String> str) {
47
  if (strcmp(*v8::String::Utf8Value(isolate, str), "externalizeString") == 0) {
48 49
    return v8::FunctionTemplate::New(isolate,
                                     ExternalizeStringExtension::Externalize);
50
  } else {
51 52
    DCHECK_EQ(strcmp(*v8::String::Utf8Value(isolate, str), "isOneByteString"),
              0);
53
    return v8::FunctionTemplate::New(isolate,
54
                                     ExternalizeStringExtension::IsOneByte);
55 56 57 58
  }
}


59 60
void ExternalizeStringExtension::Externalize(
    const v8::FunctionCallbackInfo<v8::Value>& args) {
61
  if (args.Length() < 1 || !args[0]->IsString()) {
62 63 64
    args.GetIsolate()->ThrowException(v8::String::NewFromUtf8Literal(
        args.GetIsolate(),
        "First parameter to externalizeString() must be a string."));
65
    return;
66 67 68 69
  }
  bool force_two_byte = false;
  if (args.Length() >= 2) {
    if (args[1]->IsBoolean()) {
70
      force_two_byte = args[1]->BooleanValue(args.GetIsolate());
71
    } else {
72 73 74
      args.GetIsolate()->ThrowException(v8::String::NewFromUtf8Literal(
          args.GetIsolate(),
          "Second parameter to externalizeString() must be a boolean."));
75
      return;
76 77 78
    }
  }
  bool result = false;
79
  Handle<String> string = Utils::OpenHandle(*args[0].As<v8::String>());
80
  if (!string->SupportsExternalization()) {
81 82
    args.GetIsolate()->ThrowException(v8::String::NewFromUtf8Literal(
        args.GetIsolate(), "string does not support externalization."));
83
    return;
84
  }
85
  if (string->IsOneByteRepresentation() && !force_two_byte) {
86
    uint8_t* data = new uint8_t[string->length()];
87
    String::WriteToFlat(*string, data, 0, string->length());
88
    SimpleOneByteStringResource* resource = new SimpleOneByteStringResource(
89
        reinterpret_cast<char*>(data), string->length());
90
    result = Utils::ToLocal(string)->MakeExternal(resource);
91 92 93 94 95 96
    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());
97
    result = Utils::ToLocal(string)->MakeExternal(resource);
98 99 100
    if (!result) delete resource;
  }
  if (!result) {
101 102
    args.GetIsolate()->ThrowException(v8::String::NewFromUtf8Literal(
        args.GetIsolate(), "externalizeString() failed."));
103
    return;
104 105 106 107
  }
}


108
void ExternalizeStringExtension::IsOneByte(
109
    const v8::FunctionCallbackInfo<v8::Value>& args) {
110
  if (args.Length() != 1 || !args[0]->IsString()) {
111 112 113
    args.GetIsolate()->ThrowException(v8::String::NewFromUtf8Literal(
        args.GetIsolate(),
        "isOneByteString() requires a single string argument."));
114
    return;
115
  }
116 117 118
  bool is_one_byte =
      Utils::OpenHandle(*args[0].As<v8::String>())->IsOneByteRepresentation();
  args.GetReturnValue().Set(is_one_byte);
119 120
}

121 122
}  // namespace internal
}  // namespace v8