3737import string
3838
3939
40- def ToCArray (filename , lines ):
41- return ',' .join (str (ord (c )) for c in lines )
40+ def ToCString (contents ):
41+ step = 20
42+ slices = (contents [i :i + step ] for i in xrange (0 , len (contents ), step ))
43+ slices = map (lambda s : ',' .join (str (ord (c )) for c in s ), slices )
44+ return ',\n ' .join (slices )
4245
4346
4447def ReadFile (filename ):
@@ -61,21 +64,6 @@ def ReadLines(filename):
6164 return result
6265
6366
64- def LoadConfigFrom (name ):
65- import ConfigParser
66- config = ConfigParser .ConfigParser ()
67- config .read (name )
68- return config
69-
70-
71- def ParseValue (string ):
72- string = string .strip ()
73- if string .startswith ('[' ) and string .endswith (']' ):
74- return string .lstrip ('[' ).rstrip (']' ).split ()
75- else :
76- return string
77-
78-
7967def ExpandConstants (lines , constants ):
8068 for key , value in constants .items ():
8169 lines = lines .replace (key , str (value ))
@@ -174,53 +162,37 @@ def ReadMacros(lines):
174162
175163
176164HEADER_TEMPLATE = """\
177- #ifndef node_natives_h
178- #define node_natives_h
179- namespace node {
180-
181- %(source_lines)s\
165+ #ifndef NODE_NATIVES_H_
166+ #define NODE_NATIVES_H_
182167
183- struct _native {
184- const char* name;
185- const unsigned char* source;
186- size_t source_len;
187- };
168+ #include <stdint.h>
188169
189- static const struct _native natives[] = { %(native_lines)s };
170+ #define NODE_NATIVES_MAP(V) \\
171+ {node_natives_map}
190172
191- }
192- #endif
193- """
194-
195-
196- NATIVE_DECLARATION = """\
197- { "%(id)s", %(escaped_id)s_native, sizeof(%(escaped_id)s_native) },
198- """
173+ namespace node {{
174+ {sources}
175+ }} // namespace node
199176
200- SOURCE_DECLARATION = """\
201- const unsigned char %(escaped_id)s_native[] = { %(data)s };
177+ #endif // NODE_NATIVES_H_
202178"""
203179
204180
205- GET_DELAY_INDEX_CASE = """\
206- if (strcmp(name, "%(id)s") == 0) return %(i)i;
181+ NODE_NATIVES_MAP = """\
182+ V({escaped_id}) \\
207183"""
208184
209185
210- GET_DELAY_SCRIPT_SOURCE_CASE = """\
211- if (index == %(i)i) return Vector<const char>(%(id)s, %(length)i);
186+ SOURCES = """\
187+ static const uint8_t {escaped_id}_name[] = {{
188+ {name}}};
189+ static const uint8_t {escaped_id}_data[] = {{
190+ {data}}};
212191"""
213192
214193
215- GET_DELAY_SCRIPT_NAME_CASE = """\
216- if (index == %(i)i) return Vector<const char>("%(name)s", %(length)i);
217- """
218-
219194def JS2C (source , target ):
220- ids = []
221- delay_ids = []
222195 modules = []
223- # Locate the macros file name.
224196 consts = {}
225197 macros = {}
226198 macro_lines = []
@@ -235,18 +207,14 @@ def JS2C(source, target):
235207 (consts , macros ) = ReadMacros (macro_lines )
236208
237209 # Build source code lines
238- source_lines = [ ]
239- source_lines_empty = []
240-
241- native_lines = []
210+ node_natives_map = []
211+ sources = []
242212
243213 for s in modules :
244- delay = str (s ).endswith ('-delay.js' )
245214 lines = ReadFile (str (s ))
246-
247215 lines = ExpandConstants (lines , consts )
248216 lines = ExpandMacros (lines , macros )
249- data = ToCArray ( s , lines )
217+ data = ToCString ( lines )
250218
251219 # On Windows, "./foo.bar" in the .gyp file is passed as "foo.bar"
252220 # so don't assume there is always a slash in the file path.
@@ -258,89 +226,19 @@ def JS2C(source, target):
258226 if '.' in id :
259227 id = id .split ('.' , 1 )[0 ]
260228
261- if delay : id = id [:- 6 ]
262- if delay :
263- delay_ids .append ((id , len (lines )))
264- else :
265- ids .append ((id , len (lines )))
266-
229+ name = ToCString (id )
267230 escaped_id = id .replace ('-' , '_' ).replace ('/' , '_' )
268- source_lines .append (SOURCE_DECLARATION % {
269- 'id' : id ,
270- 'escaped_id' : escaped_id ,
271- 'data' : data
272- })
273- source_lines_empty .append (SOURCE_DECLARATION % {
274- 'id' : id ,
275- 'escaped_id' : escaped_id ,
276- 'data' : 0
277- })
278- native_lines .append (NATIVE_DECLARATION % {
279- 'id' : id ,
280- 'escaped_id' : escaped_id
281- })
282-
283- # Build delay support functions
284- get_index_cases = [ ]
285- get_script_source_cases = [ ]
286- get_script_name_cases = [ ]
287-
288- i = 0
289- for (id , length ) in delay_ids :
290- native_name = "native %s.js" % id
291- get_index_cases .append (GET_DELAY_INDEX_CASE % { 'id' : id , 'i' : i })
292- get_script_source_cases .append (GET_DELAY_SCRIPT_SOURCE_CASE % {
293- 'id' : id ,
294- 'length' : length ,
295- 'i' : i
296- })
297- get_script_name_cases .append (GET_DELAY_SCRIPT_NAME_CASE % {
298- 'name' : native_name ,
299- 'length' : len (native_name ),
300- 'i' : i
301- });
302- i = i + 1
303-
304- for (id , length ) in ids :
305- native_name = "native %s.js" % id
306- get_index_cases .append (GET_DELAY_INDEX_CASE % { 'id' : id , 'i' : i })
307- get_script_source_cases .append (GET_DELAY_SCRIPT_SOURCE_CASE % {
308- 'id' : id ,
309- 'length' : length ,
310- 'i' : i
311- })
312- get_script_name_cases .append (GET_DELAY_SCRIPT_NAME_CASE % {
313- 'name' : native_name ,
314- 'length' : len (native_name ),
315- 'i' : i
316- });
317- i = i + 1
231+ node_natives_map .append (NODE_NATIVES_MAP .format (** locals ()))
232+ sources .append (SOURCES .format (** locals ()))
233+
234+ node_natives_map = '' .join (node_natives_map )
235+ sources = '' .join (sources )
318236
319237 # Emit result
320238 output = open (str (target [0 ]), "w" )
321- output .write (HEADER_TEMPLATE % {
322- 'builtin_count' : len (ids ) + len (delay_ids ),
323- 'delay_count' : len (delay_ids ),
324- 'source_lines' : "\n " .join (source_lines ),
325- 'native_lines' : "\n " .join (native_lines ),
326- 'get_index_cases' : "" .join (get_index_cases ),
327- 'get_script_source_cases' : "" .join (get_script_source_cases ),
328- 'get_script_name_cases' : "" .join (get_script_name_cases )
329- })
239+ output .write (HEADER_TEMPLATE .format (** locals ()))
330240 output .close ()
331241
332- if len (target ) > 1 :
333- output = open (str (target [1 ]), "w" )
334- output .write (HEADER_TEMPLATE % {
335- 'builtin_count' : len (ids ) + len (delay_ids ),
336- 'delay_count' : len (delay_ids ),
337- 'source_lines' : "\n " .join (source_lines_empty ),
338- 'get_index_cases' : "" .join (get_index_cases ),
339- 'get_script_source_cases' : "" .join (get_script_source_cases ),
340- 'get_script_name_cases' : "" .join (get_script_name_cases )
341- })
342- output .close ()
343-
344242def main ():
345243 natives = sys .argv [1 ]
346244 source_files = sys .argv [2 :]
0 commit comments