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 native function NativeJSCreateBreakIterator(); | |
39 | |
40 if (iterator.hasOwnProperty('__initializedIntlObject')) { | |
arv (Not doing code reviews)
2013/07/08 17:59:23
Now that this is in V8 we should not use hacks lik
| |
41 throw new TypeError('Trying to re-initialize v8BreakIterator object.'); | |
42 } | |
43 | |
44 if (options === undefined) { | |
45 options = {}; | |
46 } | |
47 | |
48 var getOption = getGetOption(options, 'breakiterator'); | |
49 | |
50 var internalOptions = {}; | |
51 | |
52 defineWEProperty(internalOptions, 'type', getOption( | |
53 'type', 'string', ['character', 'word', 'sentence', 'line'], 'word')); | |
54 | |
55 var locale = resolveLocale('breakiterator', locales, options); | |
56 var resolved = Object.defineProperties({}, { | |
57 requestedLocale: {value: locale.locale, writable: true}, | |
58 type: {value: internalOptions.type, writable: true}, | |
59 locale: {writable: true} | |
60 }); | |
61 | |
62 var internalIterator = NativeJSCreateBreakIterator(locale.locale, | |
63 internalOptions, | |
64 resolved); | |
65 | |
66 Object.defineProperty(iterator, 'iterator', {value: internalIterator}); | |
67 Object.defineProperty(iterator, 'resolved', {value: resolved}); | |
68 Object.defineProperty(iterator, '__initializedIntlObject', | |
69 {value: 'breakiterator'}); | |
70 | |
71 return iterator; | |
72 } | |
73 | |
74 | |
75 /** | |
76 * Constructs Intl.v8BreakIterator object given optional locales and options | |
77 * parameters. | |
78 * | |
79 * @constructor | |
80 */ | |
81 %SetProperty(Intl, 'v8BreakIterator', function() { | |
82 var locales = arguments[0]; | |
83 var options = arguments[1]; | |
84 | |
85 if (!this || this === Intl) { | |
86 // Constructor is called as a function. | |
87 return new Intl.v8BreakIterator(locales, options); | |
88 } | |
89 | |
90 return initializeBreakIterator(toObject(this), locales, options); | |
91 }, | |
92 ATTRIBUTES.DONT_ENUM | |
93 ); | |
94 | |
95 | |
96 /** | |
97 * BreakIterator resolvedOptions method. | |
98 */ | |
99 %SetProperty(Intl.v8BreakIterator.prototype, 'resolvedOptions', function() { | |
100 if (%_IsConstructCall()) { | |
101 throw new TypeError(ORDINARY_FUNCTION_CALLED_AS_CONSTRUCTOR); | |
102 } | |
103 | |
104 if (!this || typeof this !== 'object' || | |
105 this.__initializedIntlObject !== 'breakiterator') { | |
106 throw new TypeError('resolvedOptions method called on a non-object or ' + | |
107 'on a object that is not Intl.v8BreakIterator.'); | |
108 } | |
109 | |
110 var segmenter = this; | |
111 var locale = getOptimalLanguageTag(segmenter.resolved.requestedLocale, | |
112 segmenter.resolved.locale); | |
113 | |
114 return { | |
115 locale: locale, | |
116 type: segmenter.resolved.type | |
117 }; | |
118 }, | |
119 ATTRIBUTES.DONT_ENUM | |
120 ); | |
121 %FunctionRemovePrototype(Intl.v8BreakIterator.prototype.resolvedOptions); | |
122 | |
123 | |
124 /** | |
125 * Returns the subset of the given locale list for which this locale list | |
126 * has a matching (possibly fallback) locale. Locales appear in the same | |
127 * order in the returned list as in the input list. | |
128 * Options are optional parameter. | |
129 */ | |
130 %SetProperty(Intl.v8BreakIterator, 'supportedLocalesOf', function(locales) { | |
131 if (%_IsConstructCall()) { | |
132 throw new TypeError(ORDINARY_FUNCTION_CALLED_AS_CONSTRUCTOR); | |
133 } | |
134 | |
135 return supportedLocalesOf('breakiterator', locales, arguments[1]); | |
136 }, | |
137 ATTRIBUTES.DONT_ENUM | |
138 ); | |
139 %FunctionRemovePrototype(Intl.v8BreakIterator.supportedLocalesOf); | |
140 | |
141 | |
142 /** | |
143 * Adopts text to segment using the iterator. Old text, if present, | |
144 * gets discarded. | |
145 */ | |
146 function adoptText(iterator, text) { | |
147 native function NativeJSBreakIteratorAdoptText(); | |
148 NativeJSBreakIteratorAdoptText(iterator.iterator, String(text)); | |
149 } | |
150 | |
151 | |
152 /** | |
153 * Returns index of the first break in the string and moves current pointer. | |
154 */ | |
155 function first(iterator) { | |
156 native function NativeJSBreakIteratorFirst(); | |
157 return NativeJSBreakIteratorFirst(iterator.iterator); | |
158 } | |
159 | |
160 | |
161 /** | |
162 * Returns the index of the next break and moves the pointer. | |
163 */ | |
164 function next(iterator) { | |
165 native function NativeJSBreakIteratorNext(); | |
166 return NativeJSBreakIteratorNext(iterator.iterator); | |
167 } | |
168 | |
169 | |
170 /** | |
171 * Returns index of the current break. | |
172 */ | |
173 function current(iterator) { | |
174 native function NativeJSBreakIteratorCurrent(); | |
175 return NativeJSBreakIteratorCurrent(iterator.iterator); | |
176 } | |
177 | |
178 | |
179 /** | |
180 * Returns type of the current break. | |
181 */ | |
182 function breakType(iterator) { | |
183 native function NativeJSBreakIteratorBreakType(); | |
184 return NativeJSBreakIteratorBreakType(iterator.iterator); | |
185 } | |
186 | |
187 | |
188 addBoundMethod(Intl.v8BreakIterator, 'adoptText', adoptText, 1); | |
189 addBoundMethod(Intl.v8BreakIterator, 'first', first, 0); | |
190 addBoundMethod(Intl.v8BreakIterator, 'next', next, 0); | |
191 addBoundMethod(Intl.v8BreakIterator, 'current', current, 0); | |
192 addBoundMethod(Intl.v8BreakIterator, 'breakType', breakType, 0); | |
OLD | NEW |