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')) { |
| 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 %FunctionSetName(Intl.v8BreakIterator.prototype.resolvedOptions, |
| 122 'resolvedOptions'); |
| 123 %FunctionRemovePrototype(Intl.v8BreakIterator.prototype.resolvedOptions); |
| 124 %SetNativeFlag(Intl.v8BreakIterator.prototype.resolvedOptions); |
| 125 |
| 126 |
| 127 /** |
| 128 * Returns the subset of the given locale list for which this locale list |
| 129 * has a matching (possibly fallback) locale. Locales appear in the same |
| 130 * order in the returned list as in the input list. |
| 131 * Options are optional parameter. |
| 132 */ |
| 133 %SetProperty(Intl.v8BreakIterator, 'supportedLocalesOf', function(locales) { |
| 134 if (%_IsConstructCall()) { |
| 135 throw new TypeError(ORDINARY_FUNCTION_CALLED_AS_CONSTRUCTOR); |
| 136 } |
| 137 |
| 138 return supportedLocalesOf('breakiterator', locales, arguments[1]); |
| 139 }, |
| 140 ATTRIBUTES.DONT_ENUM |
| 141 ); |
| 142 %FunctionSetName(Intl.v8BreakIterator.supportedLocalesOf, 'supportedLocalesOf'); |
| 143 %FunctionRemovePrototype(Intl.v8BreakIterator.supportedLocalesOf); |
| 144 %SetNativeFlag(Intl.v8BreakIterator.supportedLocalesOf); |
| 145 |
| 146 |
| 147 /** |
| 148 * Adopts text to segment using the iterator. Old text, if present, |
| 149 * gets discarded. |
| 150 */ |
| 151 function adoptText(iterator, text) { |
| 152 native function NativeJSBreakIteratorAdoptText(); |
| 153 NativeJSBreakIteratorAdoptText(iterator.iterator, String(text)); |
| 154 } |
| 155 |
| 156 |
| 157 /** |
| 158 * Returns index of the first break in the string and moves current pointer. |
| 159 */ |
| 160 function first(iterator) { |
| 161 native function NativeJSBreakIteratorFirst(); |
| 162 return NativeJSBreakIteratorFirst(iterator.iterator); |
| 163 } |
| 164 |
| 165 |
| 166 /** |
| 167 * Returns the index of the next break and moves the pointer. |
| 168 */ |
| 169 function next(iterator) { |
| 170 native function NativeJSBreakIteratorNext(); |
| 171 return NativeJSBreakIteratorNext(iterator.iterator); |
| 172 } |
| 173 |
| 174 |
| 175 /** |
| 176 * Returns index of the current break. |
| 177 */ |
| 178 function current(iterator) { |
| 179 native function NativeJSBreakIteratorCurrent(); |
| 180 return NativeJSBreakIteratorCurrent(iterator.iterator); |
| 181 } |
| 182 |
| 183 |
| 184 /** |
| 185 * Returns type of the current break. |
| 186 */ |
| 187 function breakType(iterator) { |
| 188 native function NativeJSBreakIteratorBreakType(); |
| 189 return NativeJSBreakIteratorBreakType(iterator.iterator); |
| 190 } |
| 191 |
| 192 |
| 193 addBoundMethod(Intl.v8BreakIterator, 'adoptText', adoptText, 1); |
| 194 addBoundMethod(Intl.v8BreakIterator, 'first', first, 0); |
| 195 addBoundMethod(Intl.v8BreakIterator, 'next', next, 0); |
| 196 addBoundMethod(Intl.v8BreakIterator, 'current', current, 0); |
| 197 addBoundMethod(Intl.v8BreakIterator, 'breakType', breakType, 0); |
OLD | NEW |