Skip to content

Commit 7a5d93b

Browse files
jujkripken
andauthored
Fix preprocess_c_code() function to work under Closure optimization. (#26051)
Fix `preprocess_c_code()` JS library function to work under Closure optimization. --------- Co-authored-by: Alon Zakai <[email protected]>
1 parent 035c704 commit 7a5d93b

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

src/lib/libc_preprocessor.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ addToLibrary({
8181

8282
// Returns a tokenized array of the given string expression, i.e. "FOO > BAR && BAZ" -> ["FOO", ">", "BAR", "&&", "BAZ"]
8383
// Optionally keeps whitespace as tokens to be able to reconstruct the original input string.
84+
/**
85+
* @param {string} exprString
86+
* @param {(number|boolean)=} keepWhitespace Optional, can be omitted. Defaults to false.
87+
*/
8488
function tokenize(exprString, keepWhitespace) {
8589
var out = [], len = exprString.length;
8690
for (var i = 0; i <= len; ++i) {
@@ -109,8 +113,12 @@ addToLibrary({
109113
}
110114

111115
// Expands preprocessing macros on substring str[lineStart...lineEnd]
112-
function expandMacros(str, lineStart, lineEnd) {
113-
if (lineEnd === undefined) lineEnd = str.length;
116+
/**
117+
* @param {string} str
118+
* @param {number} lineStart
119+
* @param {number=} lineEnd Optional, may be omitted.
120+
*/
121+
function expandMacros(str, lineStart, lineEnd=str.length) {
114122
var len = str.length;
115123
var out = '';
116124
for (var i = lineStart; i < lineEnd; ++i) {
@@ -168,7 +176,7 @@ addToLibrary({
168176

169177
if (operatorAndPriority == 13 /* parens '(' */) {
170178
// Find the closing parens position
171-
var j = find_closing_parens_index(tokens, i);
179+
j = find_closing_parens_index(tokens, i);
172180
if (j) {
173181
tokens.splice(i, j+1-i, buildExprTree(tokens.slice(i+1, j)));
174182
return tokens;
@@ -224,7 +232,7 @@ addToLibrary({
224232
if (i < 0) i = len;
225233

226234
// Find the first non-whitespace character on the line.
227-
for (var j = lineStart; j < i && isWhitespace(code, j); ++j);
235+
for (var j = lineStart; j < i && isWhitespace(code, j);) ++j;
228236

229237
// Is this a non-preprocessor directive line?
230238
var thisLineIsInActivePreprocessingBlock = stack[stack.length-1];

test/test_other.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10686,8 +10686,12 @@ def test_function_exports_are_small(self, args, opt, closure):
1068610686

1068710687
# Tests the library_c_preprocessor.js functionality.
1068810688
@crossplatform
10689-
def test_c_preprocessor(self):
10690-
self.do_runf('test_c_preprocessor.c', cflags=['--js-library', path_from_root('src/lib/libc_preprocessor.js')])
10689+
@parameterized({
10690+
'': ([],),
10691+
'closure': (['--closure=1'],),
10692+
})
10693+
def test_c_preprocessor(self, args):
10694+
self.do_runf('test_c_preprocessor.c', cflags=['--js-library', path_from_root('src/lib/libc_preprocessor.js')] + args)
1069110695

1069210696
# Test that legacy settings that have been fixed to a specific value and their value can no longer be changed,
1069310697
def test_legacy_settings_forbidden_to_change(self):

0 commit comments

Comments
 (0)