OLD | NEW |
| (Empty) |
1 #!/usr/bin/env python | |
2 # | |
3 # Copyright 2011 the V8 project authors. All rights reserved. | |
4 # Redistribution and use in source and binary forms, with or without | |
5 # modification, are permitted provided that the following conditions are | |
6 # met: | |
7 # | |
8 # * Redistributions of source code must retain the above copyright | |
9 # notice, this list of conditions and the following disclaimer. | |
10 # * Redistributions in binary form must reproduce the above | |
11 # copyright notice, this list of conditions and the following | |
12 # disclaimer in the documentation and/or other materials provided | |
13 # with the distribution. | |
14 # * Neither the name of Google Inc. nor the names of its | |
15 # contributors may be used to endorse or promote products derived | |
16 # from this software without specific prior written permission. | |
17 # | |
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
29 | |
30 # This is a utility for converting I18N JavaScript source code into C-style | |
31 # char arrays. It is used for embedded JavaScript code in the V8 | |
32 # library. | |
33 # This is a pared down copy of v8/tools/js2c.py that avoids use of | |
34 # v8/src/natives.h and produces different cc template. | |
35 | |
36 import os, re, sys, string | |
37 | |
38 | |
39 def ToCArray(lines): | |
40 result = [] | |
41 for chr in lines: | |
42 value = ord(chr) | |
43 assert value < 128 | |
44 result.append(str(value)) | |
45 result.append("0") | |
46 return ", ".join(result) | |
47 | |
48 | |
49 def RemoveCommentsAndTrailingWhitespace(lines): | |
50 lines = re.sub(r'//.*\n', '\n', lines) # end-of-line comments | |
51 lines = re.sub(re.compile(r'/\*.*?\*/', re.DOTALL), '', lines) # comments. | |
52 lines = re.sub(r'\s+\n+', '\n', lines) # trailing whitespace | |
53 return lines | |
54 | |
55 | |
56 def ReadFile(filename): | |
57 file = open(filename, "rt") | |
58 try: | |
59 lines = file.read() | |
60 finally: | |
61 file.close() | |
62 return lines | |
63 | |
64 | |
65 EVAL_PATTERN = re.compile(r'\beval\s*\('); | |
66 WITH_PATTERN = re.compile(r'\bwith\s*\('); | |
67 | |
68 | |
69 def Validate(lines, file): | |
70 lines = RemoveCommentsAndTrailingWhitespace(lines) | |
71 # Because of simplified context setup, eval and with is not | |
72 # allowed in the natives files. | |
73 eval_match = EVAL_PATTERN.search(lines) | |
74 if eval_match: | |
75 raise ("Eval disallowed in natives: %s" % file) | |
76 with_match = WITH_PATTERN.search(lines) | |
77 if with_match: | |
78 raise ("With statements disallowed in natives: %s" % file) | |
79 | |
80 | |
81 HEADER_TEMPLATE = """\ | |
82 // Copyright 2011 Google Inc. All Rights Reserved. | |
83 | |
84 // This file was generated from .js source files by gyp. If you | |
85 // want to make changes to this file you should either change the | |
86 // javascript source files or the i18n-js2c.py script. | |
87 | |
88 #include "src/extensions/experimental/i18n-natives.h" | |
89 | |
90 namespace v8 { | |
91 namespace internal { | |
92 | |
93 // static | |
94 const char* I18Natives::GetScriptSource() { | |
95 // JavaScript source gets injected here. | |
96 static const char i18n_source[] = {%s}; | |
97 | |
98 return i18n_source; | |
99 } | |
100 | |
101 } // internal | |
102 } // v8 | |
103 """ | |
104 | |
105 | |
106 def JS2C(source, target): | |
107 filename = str(source) | |
108 | |
109 lines = ReadFile(filename) | |
110 Validate(lines, filename) | |
111 data = ToCArray(lines) | |
112 | |
113 # Emit result | |
114 output = open(target, "w") | |
115 output.write(HEADER_TEMPLATE % data) | |
116 output.close() | |
117 | |
118 | |
119 def main(): | |
120 target = sys.argv[1] | |
121 source = sys.argv[2] | |
122 JS2C(source, target) | |
123 | |
124 | |
125 if __name__ == "__main__": | |
126 main() | |
OLD | NEW |