Chromium Code Reviews| Index: frog/leg/compile_time_constants.dart |
| diff --git a/frog/leg/compile_time_constants.dart b/frog/leg/compile_time_constants.dart |
| index c4122e57b77447ec3bcd9c60ccf34abb5e6b9797..77a05a08006521d765a2c1088b25f584efeabbc8 100644 |
| --- a/frog/leg/compile_time_constants.dart |
| +++ b/frog/leg/compile_time_constants.dart |
| @@ -90,16 +90,84 @@ class CompileTimeConstantHandler extends CompilerTask { |
| }); |
| } |
| - String getJsCodeForVariable(VariableElement element) { |
| + StringBuffer writeJsCodeForVariable(StringBuffer buffer, |
| + VariableElement element) { |
| var value = initialVariableValues[element]; |
| - if (value === null) return "(void 0)"; |
| - if (value is num) return "$value"; |
| - if (value === true) return "true"; |
| - if (value === false) return "false"; |
| - |
| - // TODO(floitsch): support more values. |
| - compiler.unimplemented("CompileTimeConstantHandler.getJsCodeForVariable", |
| - node: element.parseNode(compiler)); |
| + if (value === null) { |
| + buffer.add("(void 0)"); |
| + } else if (value is num) { |
| + buffer.add("$value"); |
|
floitsch
2012/02/14 16:08:02
not your code, but could you please add parenthesi
karlklose
2012/02/15 10:37:30
Done.
|
| + } else if (value === true) { |
| + buffer.add("true"); |
| + } else if (value === false) { |
| + buffer.add("false"); |
| + } else if (value is DartString) { |
| + buffer.add("'"); |
| + writeEscapedString(value, buffer, (reason) { |
| + compiler.cancel("failed to write escaped string: $value"); |
| + }); |
| + buffer.add("'"); |
| + } else { |
| + // TODO(floitsch): support more values. |
| + compiler.unimplemented("CompileTimeConstantHandler" + |
| + "writeJsCodeForVariable", |
| + node: element.parseNode(compiler)); |
| + } |
| + return buffer; |
| + } |
| + |
| + /** |
| + * Write the contents of the quoted string to a [StringBuffer] in |
| + * a form that is valid as JavaScript string literal content. |
| + * The string is assumed quoted by single quote characters. |
| + */ |
| + static void writeEscapedString(DartString string, |
| + StringBuffer buffer, |
| + void cancel(String reason)) { |
| + Iterator<int> iterator = string.iterator(); |
| + while (iterator.hasNext()) { |
| + int code = iterator.next(); |
| + if (code === $SQ) { |
| + buffer.add(@"\'"); |
| + } else if (code === $LF) { |
| + buffer.add(@'\n'); |
| + } else if (code === $CR) { |
| + buffer.add(@'\r'); |
| + } else if (code === $LS) { |
| + // This Unicode line terminator and $PS are invalid in JS string |
| + // literals. |
| + buffer.add(@'\u2028'); |
| + } else if (code === $PS) { |
| + buffer.add(@'\u2029'); |
| + } else if (code === $BACKSLASH) { |
| + buffer.add(@'\\'); |
| + } else { |
| + if (code > 0xffff) { |
| + cancel("Unhandled non-BMP character: U+" + code.toRadixString(16)); |
| + } |
| + // TODO(lrn): Consider whether all codes above 0x7f really need to |
| + // be escaped. We build a Dart string here, so it should be a literal |
| + // stage that converts it to, e.g., UTF-8 for a JS interpreter. |
| + if (code < 0x20) { |
| + buffer.add(@'\x'); |
| + if (code < 0x10) buffer.add('0'); |
| + buffer.add(code.toRadixString(16)); |
| + } else if (code >= 0x80) { |
| + if (code < 0x100) { |
| + buffer.add(@'\x'); |
| + buffer.add(code.toRadixString(16)); |
| + } else { |
| + buffer.add(@'\u'); |
| + if (code < 0x1000) { |
| + buffer.add('0'); |
| + } |
| + buffer.add(code.toRadixString(16)); |
| + } |
| + } else { |
| + buffer.add(new String.fromCharCodes(<int>[code])); |
| + } |
| + } |
| + } |
| } |
| } |
| @@ -121,6 +189,10 @@ class CompileTimeConstantEvaluator extends AbstractVisitor { |
| } |
| visitLiteral(Literal literal) { |
| + if (literal is LiteralString) { |
| + assert(literal.asLiteralString().isValidated()); |
| + return literal.asLiteralString().dartString; |
| + } |
| return literal.value; |
| } |