5
5
var fs = require ( 'fs' ) ;
6
6
var path = require ( 'path' ) ;
7
7
8
+ var START_MARKER = '// EMSCRIPTEN_START_FUNCS\n' ;
9
+ var END_MARKER = '// EMSCRIPTEN_END_FUNCS\n' ;
10
+
8
11
function countLines ( s ) {
9
12
var count = 0 ;
10
13
for ( var i = 0 , l = s . length ; i < l ; i ++ ) {
@@ -26,14 +29,13 @@ function countLines(s) {
26
29
function extractComments ( source , commentHandler ) {
27
30
var state = 'code' ;
28
31
var commentContent = '' ;
29
- var functionStartIdx = source . indexOf ( '// EMSCRIPTEN_START_FUNCS' ) ;
30
- var functionEndIdx = source . lastIndexOf ( '// EMSCRIPTEN_END_FUNCS' ) ;
31
- var lineCount = countLines ( source . slice ( 0 , functionStartIdx ) ) ;
32
+ var functionStartIdx = source . indexOf ( START_MARKER ) ;
33
+ var functionEndIdx = source . lastIndexOf ( END_MARKER ) ;
34
+ var lineCount = countLines ( source . slice ( 0 , functionStartIdx ) ) + 2 ;
32
35
33
- for ( var i = functionStartIdx ; i < functionEndIdx ; i ++ ) {
36
+ for ( var i = functionStartIdx + START_MARKER . length ; i < functionEndIdx ; i ++ ) {
34
37
var c = source [ i ] ;
35
38
var nextC = source [ i + 1 ] ;
36
- if ( c === '\n' ) lineCount ++ ;
37
39
switch ( state ) {
38
40
case 'code' :
39
41
if ( c === '/' ) {
@@ -62,7 +64,9 @@ function extractComments(source, commentHandler) {
62
64
if ( c === '\\' ) i ++ ;
63
65
else if ( c === '"' ) state = 'code' ;
64
66
break ;
65
- }
67
+ }
68
+
69
+ if ( c === '\n' ) lineCount ++ ;
66
70
}
67
71
}
68
72
@@ -153,9 +157,9 @@ if (require.main === module) {
153
157
var newMappings = { } ;
154
158
// uglify processes the code between EMSCRIPTEN_START_FUNCS and
155
159
// EMSCRIPTEN_END_FUNCS, so its line number maps are relative to those
156
- // markers. we correct for that here.
157
- var startFuncsLineNumber = countLines (
158
- source . slice ( 0 , source . indexOf ( '// EMSCRIPTEN_START_FUNCS' ) ) ) + 2 ;
160
+ // markers. we correct for that here. +2 = 1 for the newline in the marker
161
+ // and 1 to make it a 1-based index.
162
+ var startFuncsLineNumber = countLines ( source . slice ( 0 , source . indexOf ( START_MARKER ) ) ) + 2 ;
159
163
for ( var line in optimizedMappings ) {
160
164
var originalLineNumber = optimizedMappings [ line ] . originalLineNumber + startFuncsLineNumber ;
161
165
if ( originalLineNumber in mappings ) {
0 commit comments