OLD | NEW |
| (Empty) |
1 // Copyright 2013 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 // limitations under the License. | |
28 | |
29 // ECMAScript 402 API implementation is broken into separate files for | |
30 // each service. The build system combines them together into one | |
31 // Intl namespace. | |
32 | |
33 /** | |
34 * Initializes the given object so it's a valid BreakIterator instance. | |
35 * Useful for subclassing. | |
36 */ | |
37 function initializeBreakIterator(iterator, locales, options) { | |
38 if (iterator.hasOwnProperty('__initializedIntlObject')) { | |
39 throw new TypeError('Trying to re-initialize v8BreakIterator object.'); | |
40 } | |
41 | |
42 if (options === undefined) { | |
43 options = {}; | |
44 } | |
45 | |
46 var getOption = getGetOption(options, 'breakiterator'); | |
47 | |
48 var internalOptions = {}; | |
49 | |
50 defineWEProperty(internalOptions, 'type', getOption( | |
51 'type', 'string', ['character', 'word', 'sentence', 'line'], 'word')); | |
52 | |
53 var locale = resolveLocale('breakiterator', locales, options); | |
54 var resolved = Object.defineProperties({}, { | |
55 requestedLocale: {value: locale.locale, writable: true}, | |
56 type: {value: internalOptions.type, writable: true}, | |
57 locale: {writable: true} | |
58 }); | |
59 | |
60 var internalIterator = %CreateBreakIterator(locale.locale, | |
61 internalOptions, | |
62 resolved); | |
63 | |
64 Object.defineProperty(iterator, 'iterator', {value: internalIterator}); | |
65 Object.defineProperty(iterator, 'resolved', {value: resolved}); | |
66 Object.defineProperty(iterator, '__initializedIntlObject', | |
67 {value: 'breakiterator'}); | |
68 | |
69 return iterator; | |
70 } | |
71 | |
72 | |
73 /** | |
74 * Constructs Intl.v8BreakIterator object given optional locales and options | |
75 * parameters. | |
76 * | |
77 * @constructor | |
78 */ | |
79 %SetProperty(Intl, 'v8BreakIterator', function() { | |
80 var locales = arguments[0]; | |
81 var options = arguments[1]; | |
82 | |
83 if (!this || this === Intl) { | |
84 // Constructor is called as a function. | |
85 return new Intl.v8BreakIterator(locales, options); | |
86 } | |
87 | |
88 return initializeBreakIterator(toObject(this), locales, options); | |
89 }, | |
90 ATTRIBUTES.DONT_ENUM | |
91 ); | |
92 | |
93 | |
94 /** | |
95 * BreakIterator resolvedOptions method. | |
96 */ | |
97 %SetProperty(Intl.v8BreakIterator.prototype, 'resolvedOptions', function() { | |
98 if (%_IsConstructCall()) { | |
99 throw new TypeError(ORDINARY_FUNCTION_CALLED_AS_CONSTRUCTOR); | |
100 } | |
101 | |
102 if (!this || typeof this !== 'object' || | |
103 this.__initializedIntlObject !== 'breakiterator') { | |
104 throw new TypeError('resolvedOptions method called on a non-object or ' + | |
105 'on a object that is not Intl.v8BreakIterator.'); | |
106 } | |
107 | |
108 var segmenter = this; | |
109 var locale = getOptimalLanguageTag(segmenter.resolved.requestedLocale, | |
110 segmenter.resolved.locale); | |
111 | |
112 return { | |
113 locale: locale, | |
114 type: segmenter.resolved.type | |
115 }; | |
116 }, | |
117 ATTRIBUTES.DONT_ENUM | |
118 ); | |
119 %FunctionSetName(Intl.v8BreakIterator.prototype.resolvedOptions, | |
120 'resolvedOptions'); | |
121 %FunctionRemovePrototype(Intl.v8BreakIterator.prototype.resolvedOptions); | |
122 %SetNativeFlag(Intl.v8BreakIterator.prototype.resolvedOptions); | |
123 | |
124 | |
125 /** | |
126 * Returns the subset of the given locale list for which this locale list | |
127 * has a matching (possibly fallback) locale. Locales appear in the same | |
128 * order in the returned list as in the input list. | |
129 * Options are optional parameter. | |
130 */ | |
131 %SetProperty(Intl.v8BreakIterator, 'supportedLocalesOf', function(locales) { | |
132 if (%_IsConstructCall()) { | |
133 throw new TypeError(ORDINARY_FUNCTION_CALLED_AS_CONSTRUCTOR); | |
134 } | |
135 | |
136 return supportedLocalesOf('breakiterator', locales, arguments[1]); | |
137 }, | |
138 ATTRIBUTES.DONT_ENUM | |
139 ); | |
140 %FunctionSetName(Intl.v8BreakIterator.supportedLocalesOf, 'supportedLocalesOf'); | |
141 %FunctionRemovePrototype(Intl.v8BreakIterator.supportedLocalesOf); | |
142 %SetNativeFlag(Intl.v8BreakIterator.supportedLocalesOf); | |
143 | |
144 | |
145 /** | |
146 * Adopts text to segment using the iterator. Old text, if present, | |
147 * gets discarded. | |
148 */ | |
149 function adoptText(iterator, text) { | |
150 %BreakIteratorAdoptText(iterator.iterator, String(text)); | |
151 } | |
152 | |
153 | |
154 /** | |
155 * Returns index of the first break in the string and moves current pointer. | |
156 */ | |
157 function first(iterator) { | |
158 return %BreakIteratorFirst(iterator.iterator); | |
159 } | |
160 | |
161 | |
162 /** | |
163 * Returns the index of the next break and moves the pointer. | |
164 */ | |
165 function next(iterator) { | |
166 return %BreakIteratorNext(iterator.iterator); | |
167 } | |
168 | |
169 | |
170 /** | |
171 * Returns index of the current break. | |
172 */ | |
173 function current(iterator) { | |
174 return %BreakIteratorCurrent(iterator.iterator); | |
175 } | |
176 | |
177 | |
178 /** | |
179 * Returns type of the current break. | |
180 */ | |
181 function breakType(iterator) { | |
182 return %BreakIteratorBreakType(iterator.iterator); | |
183 } | |
184 | |
185 | |
186 addBoundMethod(Intl.v8BreakIterator, 'adoptText', adoptText, 1); | |
187 addBoundMethod(Intl.v8BreakIterator, 'first', first, 0); | |
188 addBoundMethod(Intl.v8BreakIterator, 'next', next, 0); | |
189 addBoundMethod(Intl.v8BreakIterator, 'current', current, 0); | |
190 addBoundMethod(Intl.v8BreakIterator, 'breakType', breakType, 0); | |
OLD | NEW |