@@ -18,355 +18,15 @@ module.exports = function (grunt) {
18
18
19
19
} ) ;
20
20
21
+ grunt . loadTasks ( './tasks' ) ;
21
22
grunt . loadNpmTasks ( 'grunt-contrib-jshint' ) ;
22
23
grunt . loadNpmTasks ( 'grunt-contrib-uglify' ) ;
23
24
24
- grunt . registerTask ( 'compile-data' , 'Compile the data into the polyfill' , compileData ) ;
25
25
grunt . registerTask ( 'default' , function ( ) {
26
26
grunt . task . run ( 'jshint' ) ;
27
27
grunt . task . run ( 'uglify' ) ;
28
28
29
29
if ( grunt . option ( 'complete' ) )
30
30
grunt . task . run ( 'compile-data' ) ;
31
31
} ) ;
32
- grunt . registerTask ( 'update-tests' , 'refreshes the tests found in tests/test262' , updateTests ) ;
33
-
34
-
35
- /**
36
- * Refreshes the tests found in tests/test262.
37
- */
38
- function updateTests ( ) {
39
- var gruntTaskDone = this . async ( ) ,
40
- LIBS = {
41
- async : require ( 'async' ) ,
42
- fs : require ( 'fs' ) ,
43
- http : require ( 'http' ) ,
44
- path : require ( 'path' ) ,
45
- vm : require ( 'vm' )
46
- } ,
47
- URL_BASE = 'http://hg.ecmascript.org' ,
48
- testsURL ,
49
- tempDir ,
50
- testsTarball ,
51
- testsDir ;
52
-
53
- LIBS . async . series ( [
54
- function ( asyncTaskDone ) {
55
- var resErr ,
56
- resBody = '' ;
57
- grunt . log . writeln ( 'looking for tests tarball...' ) ;
58
- /*
59
- // DEBUGGING
60
- testsURL = 'http://hg.ecmascript.org/tests/test262/archive/d067d2f0ca30.tar.gz';
61
- grunt.log.ok('tests URL: ' + testsURL);
62
- asyncTaskDone();
63
- return;
64
- */
65
-
66
- LIBS . http . get ( URL_BASE + '/tests/test262/file/' , function ( res ) {
67
- if ( 200 !== res . statusCode ) {
68
- asyncTaskDone ( new Error ( 'failed to GET ' + URL_BASE + '/tests/test262/file/' ) ) ;
69
- return ;
70
- }
71
- res . on ( 'data' , function ( data ) {
72
- resBody += data . toString ( ) ;
73
- } ) ;
74
- res . on ( 'error' , function ( err ) {
75
- resErr = err ;
76
- } ) ;
77
- res . on ( 'end' , function ( ) {
78
- var matches ;
79
- matches = resBody . match ( / < a h r e f = " ( \/ t e s t s \/ t e s t 2 6 2 \/ a r c h i v e \/ [ ^ . ] + .t a r .g z ) " > g z < \/ a > / ) ;
80
- testsURL = matches [ 1 ] ;
81
- if ( ! testsURL ) {
82
- asyncTaskDone ( new Error ( 'failed to find tar.gz of tests' ) ) ;
83
- return ;
84
- }
85
- if ( '/' === testsURL [ 0 ] ) {
86
- testsURL = URL_BASE + testsURL ;
87
- }
88
- grunt . log . ok ( 'tests URL: ' + testsURL ) ;
89
- asyncTaskDone ( resErr ) ;
90
- } ) ;
91
- } ) . end ( ) ;
92
- } ,
93
-
94
- function ( asyncTaskDone ) {
95
- grunt . log . writeln ( 'making temporary directory...' ) ;
96
- /*
97
- // DEBUGGING
98
- tempDir = '/tmp/grunt.o4xlDE3o';
99
- grunt.log.ok('temporary directory: ' + tempDir);
100
- asyncTaskDone();
101
- return;
102
- */
103
- grunt . util . spawn ( {
104
- cmd : 'mktemp' ,
105
- args : [ '-d' , '/tmp/grunt.XXXXXXXX' ]
106
- } , function ( err , results ) {
107
- tempDir = results . stdout ;
108
- grunt . log . ok ( 'temporary directory: ' + tempDir ) ;
109
- asyncTaskDone ( err ) ;
110
- } ) ;
111
- } ,
112
-
113
- function ( asyncTaskDone ) {
114
- var resErr ,
115
- resBody = new Buffer ( 0 ) ,
116
- reportEveryBytes = 300000 ,
117
- reportBytes = 0 ;
118
- grunt . log . writeln ( 'downloading tests tarball...' ) ;
119
- /*
120
- // DEBUGGING
121
- testsTarball = tempDir + '/d067d2f0ca30.tar.gz';
122
- grunt.log.ok('tests tarball: ' + testsTarball);
123
- asyncTaskDone();
124
- return;
125
- */
126
- LIBS . http . get ( testsURL , function ( res ) {
127
- if ( 200 !== res . statusCode ) {
128
- asyncTaskDone ( new Error ( 'failed to GET ' + testsUR ) ) ;
129
- return ;
130
- }
131
- res . on ( 'data' , function ( data ) {
132
- // We need to use the Buffer class to safely handle binary
133
- // data (octet streams). Alas, it's not resizable so we
134
- // need to reallocate as we go along.
135
- var newBuffer = Buffer ( resBody . length + data . length ) ;
136
- resBody . copy ( newBuffer , 0 ) ;
137
- data . copy ( newBuffer , resBody . length ) ;
138
- resBody = newBuffer ;
139
- reportBytes += data . length ;
140
- if ( reportBytes >= reportEveryBytes ) {
141
- grunt . log . ok ( 'got ' + resBody . length + ' bytes' ) ;
142
- reportBytes = 0 ;
143
- }
144
- } ) ;
145
- res . on ( 'error' , function ( err ) {
146
- resErr = err ;
147
- } ) ;
148
- res . on ( 'end' , function ( ) {
149
- testsTarball = LIBS . path . resolve ( tempDir , LIBS . path . basename ( testsURL ) ) ;
150
- grunt . file . write ( testsTarball , resBody . toString ( 'binary' ) , { encoding : 'binary' } ) ;
151
- grunt . log . ok ( 'tests tarball: ' + testsTarball ) ;
152
- asyncTaskDone ( resErr ) ;
153
- } ) ;
154
- } ) . end ( ) ;
155
- } ,
156
-
157
- function ( asyncTaskDone ) {
158
- grunt . log . writeln ( 'expanding tests tarball...' ) ;
159
- /*
160
- // DEBUGGING
161
- testsDir = tempDir + '/test262-d067d2f0ca30';
162
- grunt.log.ok('tests directory: ' + testsDir);
163
- asyncTaskDone();
164
- return;
165
- */
166
- grunt . util . spawn ( {
167
- cmd : 'tar' ,
168
- args : [ 'xfz' , LIBS . path . basename ( testsTarball ) ] ,
169
- opts : {
170
- cwd : tempDir
171
- }
172
- } , function ( err , results ) {
173
- testsDir = LIBS . path . resolve ( tempDir , 'test262-' + LIBS . path . basename ( testsTarball ) . split ( '.' ) [ 0 ] ) ;
174
- grunt . log . ok ( 'tests directory: ' + testsDir ) ;
175
- asyncTaskDone ( err ) ;
176
- } ) ;
177
- } ,
178
-
179
- function ( asyncTaskDone ) {
180
- grunt . log . writeln ( 'clearing old tests/test262...' ) ;
181
- var doomed = grunt . file . expand ( __dirname + '/tests/test262/*' ) ;
182
- doomed . forEach ( function ( path ) {
183
- grunt . file . delete ( path ) ;
184
- } ) ;
185
- grunt . log . ok ( 'done' ) ;
186
- asyncTaskDone ( ) ;
187
- } ,
188
-
189
- function ( asyncTaskDone ) {
190
- grunt . log . writeln ( 'copying from tarball to tests/test262...' ) ;
191
- grunt . file . copy (
192
- LIBS . path . resolve ( testsDir , 'LICENSE' ) ,
193
- LIBS . path . resolve ( __dirname , 'tests/test262/LICENSE' )
194
- ) ;
195
-
196
- [ 'tools' , 'test' ] . forEach ( function ( dir ) {
197
- grunt . log . ok ( dir ) ;
198
- var files = grunt . file . expand (
199
- LIBS . path . resolve ( testsDir , dir ) + '/**'
200
- )
201
- files . forEach ( function ( srcPath ) {
202
- if ( ! grunt . file . isFile ( srcPath ) ) {
203
- return ;
204
- }
205
- var destPath = srcPath . replace ( testsDir , LIBS . path . resolve ( __dirname , 'tests/test262' ) ) ;
206
- grunt . file . copy ( srcPath , destPath ) ;
207
- } ) ;
208
- } ) ;
209
- grunt . log . ok ( 'done' ) ;
210
- asyncTaskDone ( ) ;
211
- } ,
212
-
213
- function ( asyncTaskDone ) {
214
- grunt . log . writeln ( 'removing `Collator` tests...' ) ;
215
-
216
- // fixup constructor lists
217
- [ 'tests/test262/test/harness/testIntl.js' ,
218
- 'tests/test262/test/suite/intl402/ch08/8.0/8.0_L15.js'
219
- ] . forEach ( function ( path ) {
220
- grunt . log . ok ( 'adjusting ' + path ) ;
221
- var contents = grunt . file . read ( path ) ;
222
- contents = contents . replace ( / ( \[ ) ( " C o l l a t o r " , ) / , '$1/*$2*/' ) ;
223
- grunt . file . write ( path , contents ) ;
224
- } ) ;
225
-
226
- // these are just trouble
227
- [ 'tests/test262/test/suite/intl402/ch09/9.2/9.2.5_11_g_ii_2.js' ,
228
- 'tests/test262/test/suite/intl402/ch10' ,
229
- 'tests/test262/test/suite/intl402/ch13/13.1'
230
- ] . forEach ( function ( path ) {
231
- grunt . log . ok ( 'removing ' + path ) ;
232
- grunt . file . delete ( path ) ;
233
- } ) ;
234
-
235
- asyncTaskDone ( ) ;
236
- } ,
237
-
238
- function ( asyncTaskDone ) {
239
- grunt . log . writeln ( 'rebuilding tests/test262/pages...' ) ;
240
- var path = 'tests/build-pages.js' ,
241
- content = grunt . file . read ( path ) ,
242
- sandbox = {
243
- __dirname : LIBS . path . join ( __dirname , 'tests' ) ,
244
- require : require ,
245
- console : {
246
- log : function ( msg ) {
247
- grunt . log . ok ( msg ) ;
248
- }
249
- }
250
- } ;
251
- try {
252
- LIBS . vm . runInNewContext ( content , sandbox , path ) ;
253
- } catch ( err ) {
254
- asyncTaskDone ( err ) ;
255
- }
256
- grunt . log . ok ( 'done' ) ;
257
- asyncTaskDone ( ) ;
258
- } ,
259
-
260
- function ( asyncTaskDone ) {
261
- grunt . log . writeln ( 'cleaning up temporary directory...' ) ;
262
- grunt . util . spawn ( {
263
- cmd : 'rm' ,
264
- args : [ '-rf' , tempDir ]
265
- } , function ( err ) {
266
- asyncTaskDone ( err ) ;
267
- } ) ;
268
- }
269
-
270
- ] , function ( err ) {
271
- if ( err ) {
272
- grunt . log . error ( err . message ) ;
273
- }
274
- gruntTaskDone ( err ) ;
275
- } ) ;
276
- }
277
-
278
-
279
- /**
280
- * Compiles all JSON data into the polyfill and saves it as Intl.complete.js
281
- */
282
- function compileData ( ) {
283
- var
284
- locData = { } ,
285
- objStrs = { } ,
286
- objs = [ ] ,
287
- prims = [ ] ,
288
-
289
- valCount = 0 ,
290
- objCount = 0 ,
291
-
292
- fileData = '' ,
293
- fs = require ( 'fs' ) ,
294
- locales = fs . readdirSync ( 'locale-data/json/' ) ,
295
- Intl = String ( fs . readFileSync ( 'Intl.js' ) ) ;
296
-
297
- fileData += Intl . slice ( 0 , Intl . lastIndexOf ( 'return Intl;' ) ) + '(function () {' ;
298
-
299
- locales . forEach ( function ( file ) {
300
- locData [ file . slice ( 0 , file . indexOf ( '.' ) ) ] = JSON . parse ( fs . readFileSync ( 'locale-data/json/' + file ) , reviver ) ;
301
- } ) ;
302
-
303
- function reviver ( k , v ) {
304
- var idx ;
305
-
306
- if ( k === 'locale' )
307
- return undefined ;
308
-
309
- else if ( typeof v === 'string' ) {
310
- idx = prims . indexOf ( v ) ;
311
- valCount ++ ;
312
-
313
- if ( idx === - 1 )
314
- idx += prims . push ( v ) ;
315
-
316
- return '###prims[' + idx + ']###' ;
317
- }
318
-
319
- else if ( typeof v === 'object' && v !== null ) {
320
- var str = JSON . stringify ( v ) ;
321
- objCount ++ ;
322
-
323
- if ( objStrs . hasOwnProperty ( str ) )
324
- return objStrs [ str ] ;
325
-
326
- else {
327
- // We need to make sure this object is not added to the same
328
- // array as an object it references (and we need to check
329
- // this recursively)
330
- var
331
- depth ,
332
- objDepths = [ 0 ] ;
333
-
334
- for ( var key in v ) {
335
- if ( typeof v [ key ] === 'string' && ( depth = v [ key ] . match ( / ^ # # # o b j s \[ ( \d + ) / ) ) )
336
- objDepths . push ( + depth [ 1 ] + 1 ) ;
337
- }
338
-
339
- depth = Math . max . apply ( Math , objDepths ) ;
340
-
341
- if ( ! Array . isArray ( objs [ depth ] ) )
342
- objs [ depth ] = [ ] ;
343
-
344
- idx = objs [ depth ] . push ( v ) - 1 ;
345
- objStrs [ str ] = '###objs[' + depth + '][' + idx + ']###' ;
346
-
347
- return objStrs [ str ] ;
348
- }
349
- }
350
- }
351
-
352
- fileData += 'var a=' + JSON . stringify ( prims ) + ',b=[];' ;
353
- objs . forEach ( function ( val , idx ) {
354
- var ref = JSON . stringify ( val ) . replace ( / " # # # ( o b j s | p r i m s ) ( \[ [ ^ # ] + ) # # # " / g, replacer ) ;
355
-
356
- fileData += 'b[' + idx + ']=' + ref + ';' ;
357
- } ) ;
358
-
359
- for ( var k in locData )
360
- fileData += 'addLocaleData(' + locData [ k ] . replace ( / # # # ( o b j s | p r i m s ) ( \[ [ ^ # ] + ) # # # / , replacer ) + ', "' + k + '");' ;
361
-
362
- fileData += '})();\n' + Intl . slice ( Intl . lastIndexOf ( 'return Intl;' ) ) ;
363
- fs . writeFileSync ( 'Intl.complete.js' , fileData ) ;
364
-
365
- grunt . log . writeln ( 'Total number of reused strings is ' + prims . length + ' (reduced from ' + valCount + ')' ) ;
366
- grunt . log . writeln ( 'Total number of reused objects is ' + Object . keys ( objStrs ) . length + ' (reduced from ' + objCount + ')' ) ;
367
- }
368
-
369
- function replacer ( $0 , type , loc ) {
370
- return ( type === 'prims' ? 'a' : 'b' ) + loc ;
371
- }
372
32
} ;
0 commit comments