| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 part of js_backend; | 5 part of js_backend; |
| 6 | 6 |
| 7 class ConstantEmitter implements ConstantVisitor { | 7 class ConstantEmitter implements ConstantVisitor { |
| 8 final Compiler compiler; | 8 final Compiler compiler; |
| 9 final Namer namer; | 9 final Namer namer; |
| 10 | 10 |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 } | 85 } |
| 86 | 86 |
| 87 /** | 87 /** |
| 88 * Write the contents of the quoted string to a [CodeBuffer] in | 88 * Write the contents of the quoted string to a [CodeBuffer] in |
| 89 * a form that is valid as JavaScript string literal content. | 89 * a form that is valid as JavaScript string literal content. |
| 90 * The string is assumed quoted by single quote characters. | 90 * The string is assumed quoted by single quote characters. |
| 91 */ | 91 */ |
| 92 void writeEscapedString(DartString string, | 92 void writeEscapedString(DartString string, |
| 93 CodeBuffer buffer, | 93 CodeBuffer buffer, |
| 94 Node diagnosticNode) { | 94 Node diagnosticNode) { |
| 95 Iterator<int> iterator = string.iterator(); | 95 void onError(code) { |
| 96 while (iterator.hasNext) { | 96 compiler.reportError( |
| 97 int code = iterator.next(); | 97 diagnosticNode, |
| 98 if (identical(code, $SQ)) { | 98 'Unhandled non-BMP character: U+${code.toRadixString(16)}'); |
| 99 buffer.add(r"\'"); | |
| 100 } else if (identical(code, $LF)) { | |
| 101 buffer.add(r'\n'); | |
| 102 } else if (identical(code, $CR)) { | |
| 103 buffer.add(r'\r'); | |
| 104 } else if (identical(code, $LS)) { | |
| 105 // This Unicode line terminator and $PS are invalid in JS string | |
| 106 // literals. | |
| 107 buffer.add(r'\u2028'); | |
| 108 } else if (identical(code, $PS)) { | |
| 109 buffer.add(r'\u2029'); | |
| 110 } else if (identical(code, $BACKSLASH)) { | |
| 111 buffer.add(r'\\'); | |
| 112 } else { | |
| 113 if (code > 0xffff) { | |
| 114 compiler.reportError( | |
| 115 diagnosticNode, | |
| 116 'Unhandled non-BMP character: U+${code.toRadixString(16)}'); | |
| 117 } | |
| 118 // TODO(lrn): Consider whether all codes above 0x7f really need to | |
| 119 // be escaped. We build a Dart string here, so it should be a literal | |
| 120 // stage that converts it to, e.g., UTF-8 for a JS interpreter. | |
| 121 if (code < 0x20) { | |
| 122 buffer.add(r'\x'); | |
| 123 if (code < 0x10) buffer.add('0'); | |
| 124 buffer.add(code.toRadixString(16)); | |
| 125 } else if (code >= 0x80) { | |
| 126 if (code < 0x100) { | |
| 127 buffer.add(r'\x'); | |
| 128 } else { | |
| 129 buffer.add(r'\u'); | |
| 130 if (code < 0x1000) { | |
| 131 buffer.add('0'); | |
| 132 } | |
| 133 } | |
| 134 buffer.add(code.toRadixString(16)); | |
| 135 } else { | |
| 136 buffer.addCharCode(code); | |
| 137 } | |
| 138 } | |
| 139 } | 99 } |
| 100 writeJsonEscapedCharsOn(string.iterator(), buffer, onError); |
| 140 } | 101 } |
| 141 | 102 |
| 142 void visitString(StringConstant constant) { | 103 void visitString(StringConstant constant) { |
| 143 buffer.add("'"); | 104 buffer.add("'"); |
| 144 writeEscapedString(constant.value, buffer, constant.node); | 105 writeEscapedString(constant.value, buffer, constant.node); |
| 145 buffer.add("'"); | 106 buffer.add("'"); |
| 146 } | 107 } |
| 147 | 108 |
| 148 void emitCanonicalVersion(Constant constant) { | 109 void emitCanonicalVersion(Constant constant) { |
| 149 String name = namer.constantName(constant); | 110 String name = namer.constantName(constant); |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 buffer.add(getJsConstructor(constant.type.element)); | 206 buffer.add(getJsConstructor(constant.type.element)); |
| 246 buffer.add("("); | 207 buffer.add("("); |
| 247 for (int i = 0; i < constant.fields.length; i++) { | 208 for (int i = 0; i < constant.fields.length; i++) { |
| 248 if (i != 0) buffer.add(", "); | 209 if (i != 0) buffer.add(", "); |
| 249 _visit(constant.fields[i]); | 210 _visit(constant.fields[i]); |
| 250 } | 211 } |
| 251 buffer.add(")"); | 212 buffer.add(")"); |
| 252 } | 213 } |
| 253 } | 214 } |
| 254 } | 215 } |
| OLD | NEW |