OLD | NEW |
| (Empty) |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "mojo/bindings/js/unicode.h" | |
6 | |
7 #include "gin/arguments.h" | |
8 #include "gin/array_buffer.h" | |
9 #include "gin/object_template_builder.h" | |
10 #include "gin/per_isolate_data.h" | |
11 #include "gin/public/wrapper_info.h" | |
12 | |
13 namespace mojo { | |
14 namespace js { | |
15 | |
16 namespace { | |
17 | |
18 v8::Handle<v8::Value> DecodeUtf8String(const gin::Arguments& args, | |
19 const gin::ArrayBufferView& buffer) { | |
20 assert(static_cast<int>(buffer.num_bytes()) >= 0); | |
21 return v8::String::NewFromUtf8(args.isolate(), | |
22 reinterpret_cast<char*>(buffer.bytes()), | |
23 v8::String::kNormalString, | |
24 static_cast<int>(buffer.num_bytes())); | |
25 } | |
26 | |
27 int EncodeUtf8String(const gin::Arguments& args, | |
28 v8::Handle<v8::Value> str_value, | |
29 const gin::ArrayBufferView& buffer) { | |
30 assert(static_cast<int>(buffer.num_bytes()) >= 0); | |
31 v8::Handle<v8::String> str = str_value->ToString(); | |
32 int num_bytes = str->WriteUtf8( | |
33 reinterpret_cast<char*>(buffer.bytes()), | |
34 static_cast<int>(buffer.num_bytes()), | |
35 NULL, | |
36 v8::String::NO_NULL_TERMINATION | v8::String::REPLACE_INVALID_UTF8); | |
37 return num_bytes; | |
38 } | |
39 | |
40 int Utf8Length(v8::Handle<v8::Value> str_value) { | |
41 return str_value->ToString()->Utf8Length(); | |
42 } | |
43 | |
44 gin::WrapperInfo g_wrapper_info = { gin::kEmbedderNativeGin }; | |
45 | |
46 } // namespace | |
47 | |
48 const char Unicode::kModuleName[] = "mojo/public/js/bindings/unicode"; | |
49 | |
50 v8::Local<v8::Value> Unicode::GetModule(v8::Isolate* isolate) { | |
51 gin::PerIsolateData* data = gin::PerIsolateData::From(isolate); | |
52 v8::Local<v8::ObjectTemplate> templ = data->GetObjectTemplate( | |
53 &g_wrapper_info); | |
54 | |
55 if (templ.IsEmpty()) { | |
56 templ = gin::ObjectTemplateBuilder(isolate) | |
57 .SetMethod("decodeUtf8String", DecodeUtf8String) | |
58 .SetMethod("encodeUtf8String", EncodeUtf8String) | |
59 .SetMethod("utf8Length", Utf8Length) | |
60 .Build(); | |
61 | |
62 data->SetObjectTemplate(&g_wrapper_info, templ); | |
63 } | |
64 | |
65 return templ->NewInstance(); | |
66 } | |
67 | |
68 } // namespace js | |
69 } // namespace mojo | |
OLD | NEW |