Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(72)

Side by Side Diff: src/extensions/experimental/break-iterator.cc

Issue 6598014: Adding break iterator support to the i18n api extension.... (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: '' Created 9 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 // Copyright 2011 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
28 #include "break-iterator.h"
29
30 #include "unicode/brkiter.h"
31 #include "unicode/locid.h"
32 #include "unicode/rbbi.h"
33 #include "unicode/uloc.h"
34
35 namespace v8 {
36 namespace internal {
37
38 v8::Persistent<v8::ObjectTemplate> BreakIterator::break_iterator_template_;
39
40 // Unpacks break iterator object from corresponding JavaScript object.
41 static icu::BreakIterator* UnpackBreakIterator(v8::Handle<v8::Object> obj) {
42 return static_cast<icu::BreakIterator*>(obj->GetPointerFromInternalField(0));
Mads Ager (chromium) 2011/02/28 13:03:32 You need to be more careful here. The users of Unp
Nebojša Ćirić 2011/02/28 20:42:41 Done.
43 }
44
45 // Release memory we allocated for the BreakIterator once the JS object that
46 // holds the pointer gets garbage collected.
47 static void DeleteBreakIterator(v8::Persistent<v8::Value> object,
48 void* param) {
49 delete UnpackBreakIterator(v8::Persistent<v8::Object>::Cast(object));
Mads Ager (chromium) 2011/02/28 13:03:32 You need to do object.Dispose() as well to delete
Nebojša Ćirić 2011/02/28 20:42:41 Done.
50 }
51
52 // Assigns new text to the iterator.
53 static v8::Handle<v8::Value> BreakIteratorAdoptText(
54 const v8::Arguments& args) {
55 if (args.Length() != 1 || !args[0]->IsString()) {
56 return v8::ThrowException(v8::Exception::SyntaxError(
57 v8::String::New("Text input is required.")));
58 }
59
60 icu::BreakIterator* break_iterator = UnpackBreakIterator(args.Holder());
61
62 v8::Local<v8::String> text_value = args[0]->ToString();
63 UnicodeString text(*v8::String::Value(text_value),
64 text_value->Length());
65
66 break_iterator->setText(text);
67
68 return v8::Undefined();
69 }
70
71 // Moves iterator to the beginning of the string and returns new position.
72 static v8::Handle<v8::Value> BreakIteratorFirst(const v8::Arguments& args) {
73 icu::BreakIterator* break_iterator = UnpackBreakIterator(args.Holder());
74
75 return v8::Int32::New(break_iterator->first());
76 }
77
78 // Moves iterator to the next position and returns it.
79 static v8::Handle<v8::Value> BreakIteratorNext(const v8::Arguments& args) {
80 icu::BreakIterator* break_iterator = UnpackBreakIterator(args.Holder());
81
82 return v8::Int32::New(break_iterator->next());
83 }
84
85 // Returns current iterator's current position.
86 static v8::Handle<v8::Value> BreakIteratorCurrent(const v8::Arguments& args) {
87 icu::BreakIterator* break_iterator = UnpackBreakIterator(args.Holder());
88
89 return v8::Int32::New(break_iterator->current());
90 }
91
92 // Returns type of the item from current position.
93 // This call is only valid for word break iterators. Others just return 0.
94 static v8::Handle<v8::Value> BreakIteratorBreakType(
95 const v8::Arguments& args) {
96 icu::BreakIterator* break_iterator = UnpackBreakIterator(args.Holder());
97
98 // TODO(cira): Remove cast once ICU fixes base BreakIterator class.
99 int32_t status =
100 static_cast<RuleBasedBreakIterator*>(break_iterator)->getRuleStatus();
101 // Keep return values in sync with JavaScript BreakType enum.
102 if (status >= UBRK_WORD_NONE && status < UBRK_WORD_NONE_LIMIT) {
103 return v8::Int32::New(UBRK_WORD_NONE);
104 } else if (status >= UBRK_WORD_NUMBER && status < UBRK_WORD_NUMBER_LIMIT) {
105 return v8::Int32::New(UBRK_WORD_NUMBER);
106 } else if (status >= UBRK_WORD_LETTER && status < UBRK_WORD_LETTER_LIMIT) {
107 return v8::Int32::New(UBRK_WORD_LETTER);
108 } else if (status >= UBRK_WORD_KANA && status < UBRK_WORD_KANA_LIMIT) {
109 return v8::Int32::New(UBRK_WORD_KANA);
110 } else if (status >= UBRK_WORD_IDEO && status < UBRK_WORD_IDEO_LIMIT) {
111 return v8::Int32::New(UBRK_WORD_IDEO);
112 } else {
113 return v8::Int32::New(-1);
114 }
115 }
116
117 v8::Handle<v8::Value> BreakIterator::JSBreakIterator(
118 const v8::Arguments& args) {
119 // No need to check args here, we do it in JavaScript.
120
121 const char* locale = *v8::String::Utf8Value(args[0]->ToString());
122 icu::Locale icu_locale(locale);
123
124 UErrorCode status = U_ZERO_ERROR;
125 icu::BreakIterator* break_iterator = NULL;
126 const char* type = *v8::String::Utf8Value(args[1]->ToString());
127 if (!strcmp(type, "character")) {
128 break_iterator =
129 icu::BreakIterator::createCharacterInstance(icu_locale, status);
130 } else if (!strcmp(type, "word")) {
131 break_iterator =
132 icu::BreakIterator::createWordInstance(icu_locale, status);
133 } else if (!strcmp(type, "sentence")) {
134 break_iterator =
135 icu::BreakIterator::createSentenceInstance(icu_locale, status);
136 } else if (!strcmp(type, "line")) {
137 break_iterator =
138 icu::BreakIterator::createLineInstance(icu_locale, status);
139 } else {
140 return v8::ThrowException(v8::Exception::SyntaxError(
141 v8::String::New("Invalid iterator type.")));
142 }
143
144 if (U_FAILURE(status)) {
145 delete break_iterator;
146 return v8::ThrowException(v8::Exception::Error(
147 v8::String::New("Failed to create break iterator.")));
148 }
149
150 v8::HandleScope handle_scope;
Mads Ager (chromium) 2011/02/28 13:03:32 Is there a reason not have have this handle scope
Nebojša Ćirić 2011/02/28 20:42:41 Done.
151
152 if (break_iterator_template_.IsEmpty()) {
153 v8::Handle<v8::ObjectTemplate> raw_template(v8::ObjectTemplate::New());
154 raw_template->SetInternalFieldCount(1);
155 raw_template->Set(v8::String::New("adoptText"),
156 v8::FunctionTemplate::New(BreakIteratorAdoptText));
157 raw_template->Set(v8::String::New("first"),
158 v8::FunctionTemplate::New(BreakIteratorFirst));
159 raw_template->Set(v8::String::New("next"),
160 v8::FunctionTemplate::New(BreakIteratorNext));
161 raw_template->Set(v8::String::New("current"),
162 v8::FunctionTemplate::New(BreakIteratorCurrent));
163 raw_template->Set(v8::String::New("breakType"),
164 v8::FunctionTemplate::New(BreakIteratorBreakType));
165 break_iterator_template_ =
166 v8::Persistent<v8::ObjectTemplate>::New(raw_template);
167 // Make template weak so we can delete iterator once GC kicks in.
Mads Ager (chromium) 2011/02/28 13:03:32 This is not the thing that you want to make weak.
Nebojša Ćirić 2011/02/28 20:42:41 Yip, it felt weird when I did it. Never occurred t
168 break_iterator_template_.MakeWeak(NULL, DeleteBreakIterator);
169 }
170
171 // Create an empty object wrapper.
172 v8::Handle<v8::Object> wrapper = break_iterator_template_->NewInstance();
173 // Set break iterator as internal field of the resulting JS object.
174 wrapper->SetPointerInInternalField(0, break_iterator);
Mads Ager (chromium) 2011/02/28 13:03:32 This is where you set up the correspondence. Here
Nebojša Ćirić 2011/02/28 20:42:41 Done.
175
176 return handle_scope.Close(wrapper);
177 }
178
179 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/extensions/experimental/break-iterator.h ('k') | src/extensions/experimental/experimental.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698