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 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
77 } | 77 } |
78 | 78 |
79 void visitTrue(TrueConstant constant) { | 79 void visitTrue(TrueConstant constant) { |
80 buffer.add("true"); | 80 buffer.add("true"); |
81 } | 81 } |
82 | 82 |
83 void visitFalse(FalseConstant constant) { | 83 void visitFalse(FalseConstant constant) { |
84 buffer.add("false"); | 84 buffer.add("false"); |
85 } | 85 } |
86 | 86 |
87 void addHexEscape(CodeBuffer buffer, int code) { | |
88 assert(code < 0x10000); | |
89 if (code < 0x20) { | |
90 buffer.add(r'\x'); | |
91 if (code < 0x10) buffer.add('0'); | |
92 buffer.add(code.toRadixString(16)); | |
93 } else { | |
94 if (code < 0x100) { | |
95 assert(code >= 0x80); | |
96 buffer.add(r'\x'); | |
97 } else { | |
98 buffer.add(r'\u'); | |
99 if (code < 0x1000) { | |
100 buffer.add('0'); | |
101 } | |
102 } | |
103 buffer.add(code.toRadixString(16)); | |
104 } | |
105 } | |
106 | |
87 /** | 107 /** |
88 * Write the contents of the quoted string to a [CodeBuffer] in | 108 * Write the contents of the quoted string to a [CodeBuffer] in |
89 * a form that is valid as JavaScript string literal content. | 109 * a form that is valid as JavaScript string literal content. |
90 * The string is assumed quoted by single quote characters. | 110 * The string is assumed quoted by single quote characters. |
91 */ | 111 */ |
92 void writeEscapedString(DartString string, | 112 void writeEscapedString(DartString string, |
93 CodeBuffer buffer, | 113 CodeBuffer buffer, |
94 Node diagnosticNode) { | 114 Node diagnosticNode) { |
95 Iterator<int> iterator = string.iterator(); | 115 Iterator<int> iterator = string.iterator(); |
96 while (iterator.hasNext) { | 116 while (iterator.hasNext) { |
97 int code = iterator.next(); | 117 int code = iterator.next(); |
98 if (identical(code, $SQ)) { | 118 if (identical(code, $SQ)) { |
99 buffer.add(r"\'"); | 119 buffer.add(r"\'"); |
100 } else if (identical(code, $LF)) { | 120 } else if (identical(code, $LF)) { |
101 buffer.add(r'\n'); | 121 buffer.add(r'\n'); |
102 } else if (identical(code, $CR)) { | 122 } else if (identical(code, $CR)) { |
103 buffer.add(r'\r'); | 123 buffer.add(r'\r'); |
104 } else if (identical(code, $LS)) { | 124 } else if (identical(code, $LS)) { |
105 // This Unicode line terminator and $PS are invalid in JS string | 125 // This Unicode line terminator and $PS are invalid in JS string |
106 // literals. | 126 // literals. |
107 buffer.add(r'\u2028'); | 127 addHexEscape(buffer, 0x2028); |
108 } else if (identical(code, $PS)) { | 128 } else if (identical(code, $PS)) { |
109 buffer.add(r'\u2029'); | 129 addHexEscape(buffer, 0x2029); |
110 } else if (identical(code, $BACKSLASH)) { | 130 } else if (identical(code, $BACKSLASH)) { |
111 buffer.add(r'\\'); | 131 buffer.add(r'\\'); |
112 } else { | 132 } 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) { | 133 if (code < 0x20) { |
122 buffer.add(r'\x'); | 134 addHexEscape(buffer, code); |
123 if (code < 0x10) buffer.add('0'); | |
124 buffer.add(code.toRadixString(16)); | |
125 } else if (code >= 0x80) { | 135 } else if (code >= 0x80) { |
126 if (code < 0x100) { | 136 // TODO(lrn): Consider whether all codes above 0x7f really need to |
127 buffer.add(r'\x'); | 137 // be escaped. We build a Dart string here, so it should be a literal |
138 // stage that converts it to, e.g., UTF-8 for a JS interpreter. | |
139 if (code >= 0x10000) { | |
floitsch
2012/11/08 15:28:21
Why not move the surrogate-code into addHexEscape?
erikcorry
2012/11/15 13:28:25
This part has all been rewritten with the new JSON
| |
140 // Surrogate pair. | |
141 addHexEscape(buffer, 0xd800 + (((code - 0x10000) >> 10) & 0x3ff)); | |
142 addHexEscape(buffer, 0xdc00 + (code & 0x3ff)); | |
128 } else { | 143 } else { |
129 buffer.add(r'\u'); | 144 addHexEscape(buffer, code); |
130 if (code < 0x1000) { | |
131 buffer.add('0'); | |
132 } | |
133 } | 145 } |
134 buffer.add(code.toRadixString(16)); | |
135 } else { | 146 } else { |
136 buffer.addCharCode(code); | 147 buffer.addCharCode(code); |
137 } | 148 } |
138 } | 149 } |
139 } | 150 } |
140 } | 151 } |
141 | 152 |
142 void visitString(StringConstant constant) { | 153 void visitString(StringConstant constant) { |
143 buffer.add("'"); | 154 buffer.add("'"); |
144 writeEscapedString(constant.value, buffer, constant.node); | 155 writeEscapedString(constant.value, buffer, constant.node); |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
245 buffer.add(getJsConstructor(constant.type.element)); | 256 buffer.add(getJsConstructor(constant.type.element)); |
246 buffer.add("("); | 257 buffer.add("("); |
247 for (int i = 0; i < constant.fields.length; i++) { | 258 for (int i = 0; i < constant.fields.length; i++) { |
248 if (i != 0) buffer.add(", "); | 259 if (i != 0) buffer.add(", "); |
249 _visit(constant.fields[i]); | 260 _visit(constant.fields[i]); |
250 } | 261 } |
251 buffer.add(")"); | 262 buffer.add(")"); |
252 } | 263 } |
253 } | 264 } |
254 } | 265 } |
OLD | NEW |