Skip to content

Commit 220136b

Browse files
authored
Cleanup JavaScript compiler tools. NFC. (#12013)
This change makes are 3 primary JavaScript compiler tools more like normal node programs. - Make them executable and add #! lines - Remove compatibility boilerplate that makes them runnable outside of node.
1 parent d4a4538 commit 220136b

File tree

5 files changed

+56
-241
lines changed

5 files changed

+56
-241
lines changed

src/compiler.js

100644100755
Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#!/usr/bin/env node
12
/**
23
* @license
34
* Copyright 2010 The Emscripten Authors
@@ -9,10 +10,6 @@
910
var nodeFS = require('fs');
1011
var nodePath = require('path');
1112

12-
// *** Environment setup code ***
13-
14-
// Expose functionality in the same simple way that the shells work
15-
// Note that we pollute the global namespace here, otherwise we break in node
1613
print = function(x) {
1714
process['stdout'].write(x + '\n');
1815
};
@@ -34,31 +31,25 @@ function find(filename) {
3431

3532
read = function(filename) {
3633
var absolute = find(filename);
37-
return nodeFS['readFileSync'](absolute).toString();
34+
return nodeFS.readFileSync(absolute).toString();
3835
};
3936

40-
load = function(f) {
41-
globalEval(read(f));
37+
function load(f) {
38+
eval.call(null, read(f));
4239
};
4340

44-
function globalEval(x) {
45-
eval.call(null, x);
46-
}
47-
4841
// Basic utilities
49-
5042
load('utility.js');
5143

5244
// Load settings, can be overridden by commandline
53-
54-
load('settings.js');
55-
load('settings_internal.js');
45+
load('./settings.js');
46+
load('./settings_internal.js');
5647

5748
var arguments_ = process['argv'].slice(2);
58-
var settings_file = arguments_[0];
49+
var settingsFile = arguments_[0];
5950

60-
if (settings_file) {
61-
var settings = JSON.parse(read(settings_file));
51+
if (settingsFile) {
52+
var settings = JSON.parse(read(settingsFile));
6253
for (var key in settings) {
6354
var value = settings[key];
6455
if (value[0] == '@') {
@@ -69,7 +60,7 @@ if (settings_file) {
6960
// continue normally; assume it is not a response file
7061
}
7162
}
72-
eval(key + ' = ' + JSON.stringify(value));
63+
global[key] = eval(JSON.stringify(value));
7364
}
7465
}
7566

tools/js-optimizer.js

100644100755
Lines changed: 17 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#!/usr/bin/env node
12
// Copyright 2011 The Emscripten Authors. All rights reserved.
23
// Emscripten is available under two separate licenses, the MIT license and the
34
// University of Illinois/NCSA Open Source License. Both these licenses can be
@@ -15,127 +16,32 @@
1516
// instead of returning it to the previous call frame where we check?
1617
// TODO: Share EMPTY_NODE instead of emptyNode that constructs?
1718

18-
if (!Math.fround) {
19-
var froundBuffer = new Float32Array(1);
20-
Math.fround = function(x) { froundBuffer[0] = x; return froundBuffer[0] };
21-
}
19+
var uglify = require('../third_party/uglify-js');
20+
var fs = require('fs');
21+
var path = require('path');
22+
var fs = require('fs');
2223

23-
// *** Environment setup code ***
24-
var arguments_ = [];
24+
var arguments_ = process['argv'].slice(2);
2525
var debug = false;
2626

27-
var ENVIRONMENT_IS_NODE = typeof process === 'object';
28-
var ENVIRONMENT_IS_WEB = typeof window === 'object';
29-
var ENVIRONMENT_IS_WORKER = typeof importScripts === 'function';
30-
var ENVIRONMENT_IS_SHELL = !ENVIRONMENT_IS_WEB && !ENVIRONMENT_IS_NODE && !ENVIRONMENT_IS_WORKER;
31-
32-
if (ENVIRONMENT_IS_NODE) {
33-
// Expose functionality in the same simple way that the shells work
34-
// Note that we pollute the global namespace here, otherwise we break in node
35-
print = function(x) {
36-
process['stdout'].write(x + '\n');
37-
};
38-
printErr = function(x) {
39-
process['stderr'].write(x + '\n');
40-
};
41-
42-
var nodeFS = require('fs');
43-
var nodePath = require('path');
44-
45-
if (!nodeFS.existsSync) {
46-
nodeFS.existsSync = function(path) {
47-
try {
48-
return !!nodeFS.readFileSync(path);
49-
} catch(e) {
50-
return false;
51-
}
52-
}
53-
}
54-
55-
function find(filename) {
56-
var prefixes = [nodePath.join(__dirname, '..', 'src'), process.cwd()];
57-
for (var i = 0; i < prefixes.length; ++i) {
58-
var combined = nodePath.join(prefixes[i], filename);
59-
if (nodeFS.existsSync(combined)) {
60-
return combined;
61-
}
62-
}
63-
return filename;
64-
}
65-
66-
read = function(filename) {
67-
var absolute = find(filename);
68-
return nodeFS['readFileSync'](absolute).toString();
69-
};
70-
71-
load = function(f) {
72-
globalEval(read(f));
73-
};
74-
75-
arguments_ = process['argv'].slice(2);
76-
77-
} else if (ENVIRONMENT_IS_SHELL) {
78-
// Polyfill over SpiderMonkey/V8 differences
79-
if (!this['read']) {
80-
this['read'] = function(f) { snarf(f) };
81-
}
82-
83-
if (typeof scriptArgs != 'undefined') {
84-
arguments_ = scriptArgs;
85-
} else if (typeof arguments != 'undefined') {
86-
arguments_ = arguments;
87-
}
88-
89-
} else if (ENVIRONMENT_IS_WEB) {
90-
this['print'] = printErr = function(x) {
91-
console.log(x);
92-
};
93-
94-
this['read'] = function(url) {
95-
var xhr = new XMLHttpRequest();
96-
xhr.open('GET', url, false);
97-
xhr.send(null);
98-
return xhr.responseText;
99-
};
100-
101-
if (this['arguments']) {
102-
arguments_ = arguments;
103-
}
104-
} else if (ENVIRONMENT_IS_WORKER) {
105-
// We can do very little here...
106-
107-
this['load'] = importScripts;
108-
109-
} else {
110-
throw 'Unknown runtime environment. Where are we?';
111-
}
112-
113-
function globalEval(x) {
114-
eval.call(null, x);
115-
}
116-
117-
if (typeof load === 'undefined' && typeof read != 'undefined') {
118-
this['load'] = function(f) {
119-
globalEval(read(f));
120-
};
27+
function printErr(x) {
28+
process.stderr.write(x + '\n');
12129
}
12230

123-
if (typeof printErr === 'undefined') {
124-
this['printErr'] = function(){};
31+
function print(x) {
32+
process.stdout.write(x + '\n');
12533
}
12634

127-
if (typeof print === 'undefined') {
128-
this['print'] = printErr;
35+
function read(filename) {
36+
return fs.readFileSync(filename).toString();
12937
}
130-
// *** Environment setup code ***
13138

132-
var uglify = require('../third_party/uglify-js');
133-
var fs = require('fs');
134-
var path = require('path');
135-
136-
// Load some modules
39+
function load(f) {
40+
f = path.join(__dirname, f)
41+
eval.call(null, read(f));
42+
};
13743

138-
load('utility.js');
44+
load('../src/utility.js');
13945

14046
// Utilities
14147

tools/js_optimizer.py

100644100755
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#!/usr/bin/env python3
12
# Copyright 2012 The Emscripten Authors. All rights reserved.
23
# Emscripten is available under two separate licenses, the MIT license and the
34
# University of Illinois/NCSA Open Source License. Both these licenses can be

tools/preprocessor.js

100644100755
Lines changed: 27 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#!/usr/bin/env node
12
// Copyright 2018 The Emscripten Authors. All rights reserved.
23
// Emscripten is available under two separate licenses, the MIT license and the
34
// University of Illinois/NCSA Open Source License. Both these licenses can be
@@ -11,130 +12,45 @@
1112
// file with modified settings and supply the filename here.
1213
// shell file This is the file that will be processed by the preprocessor
1314

14-
// *** Environment setup code ***
15-
var arguments_ = [];
16-
var debug = false;
17-
18-
var ENVIRONMENT_IS_NODE = typeof process === 'object';
19-
var ENVIRONMENT_IS_WEB = typeof window === 'object';
20-
var ENVIRONMENT_IS_WORKER = typeof importScripts === 'function';
21-
var ENVIRONMENT_IS_SHELL = !ENVIRONMENT_IS_WEB && !ENVIRONMENT_IS_NODE && !ENVIRONMENT_IS_WORKER;
2215

23-
if (ENVIRONMENT_IS_NODE) {
24-
// Expose functionality in the same simple way that the shells work
25-
// Note that we pollute the global namespace here, otherwise we break in node
26-
print = function(x) {
27-
process['stdout'].write(x + '\n');
28-
};
29-
printErr = function(x) {
30-
process['stderr'].write(x + '\n');
31-
};
16+
var fs = require('fs');
17+
var path = require('path');
3218

33-
var nodeFS = require('fs');
34-
var nodePath = require('path');
35-
36-
if (!nodeFS.existsSync) {
37-
nodeFS.existsSync = function(path) {
38-
try {
39-
return !!nodeFS.readFileSync(path);
40-
} catch(e) {
41-
return false;
42-
}
43-
}
44-
}
19+
var arguments_ = process['argv'].slice(2);
20+
var debug = false;
4521

46-
function find(filename) {
47-
var prefixes = [process.cwd(), nodePath.join(__dirname, '..', 'src')];
48-
for (var i = 0; i < prefixes.length; ++i) {
49-
var combined = nodePath.join(prefixes[i], filename);
50-
if (nodeFS.existsSync(combined)) {
51-
return combined;
52-
}
22+
print = function(x) {
23+
process['stdout'].write(x + '\n');
24+
};
25+
printErr = function(x) {
26+
process['stderr'].write(x + '\n');
27+
};
28+
29+
function find(filename) {
30+
var prefixes = [process.cwd(), path.join(__dirname, '..', 'src')];
31+
for (var i = 0; i < prefixes.length; ++i) {
32+
var combined = path.join(prefixes[i], filename);
33+
if (fs.existsSync(combined)) {
34+
return combined;
5335
}
54-
return filename;
5536
}
56-
57-
read = function(filename) {
58-
var absolute = find(filename);
59-
return nodeFS['readFileSync'](absolute).toString();
60-
};
61-
62-
load = function(f) {
63-
globalEval(read(f));
64-
};
65-
66-
arguments_ = process['argv'].slice(2);
67-
68-
} else if (ENVIRONMENT_IS_SHELL) {
69-
// Polyfill over SpiderMonkey/V8 differences
70-
if (!this['read']) {
71-
this['read'] = function(f) { snarf(f) };
72-
}
73-
74-
if (typeof scriptArgs != 'undefined') {
75-
arguments_ = scriptArgs;
76-
} else if (typeof arguments != 'undefined') {
77-
arguments_ = arguments;
78-
}
79-
80-
} else if (ENVIRONMENT_IS_WEB) {
81-
this['print'] = printErr = function(x) {
82-
console.log(x);
83-
};
84-
85-
this['read'] = function(url) {
86-
var xhr = new XMLHttpRequest();
87-
xhr.open('GET', url, false);
88-
xhr.send(null);
89-
return xhr.responseText;
90-
};
91-
92-
if (this['arguments']) {
93-
arguments_ = arguments;
94-
}
95-
} else if (ENVIRONMENT_IS_WORKER) {
96-
// We can do very little here...
97-
98-
this['load'] = importScripts;
99-
100-
} else {
101-
throw 'Unknown runtime environment. Where are we?';
102-
}
103-
104-
function globalEval(x) {
105-
eval.call(null, x);
106-
}
107-
108-
if (typeof load === 'undefined' && typeof read != 'undefined') {
109-
this['load'] = function(f) {
110-
globalEval(read(f));
111-
};
112-
}
113-
114-
if (typeof printErr === 'undefined') {
115-
this['printErr'] = function(){};
37+
return filename;
11638
}
11739

118-
if (typeof print === 'undefined') {
119-
this['print'] = printErr;
120-
}
40+
read = function(filename) {
41+
var absolute = find(filename);
42+
return fs.readFileSync(absolute).toString();
43+
};
12144

122-
// *** Environment setup code ***
123-
124-
// These global functions are referenced by parseTools, although only 'assert'
125-
// is used by the preprocess function, so 'set' can be just a stub.
126-
assert = function(condition, text) {
127-
if (!condition) {
128-
abort('Assertion failed: ' + text);
129-
}
130-
}
131-
set = function() {}
45+
load = function(f) {
46+
eval.call(null, read(f));
47+
};
13248

13349
var settings_file = arguments_[0];
13450
var shell_file = arguments_[1];
13551
var process_macros = arguments_.indexOf('--expandMacros') >= 0;
13652

137-
load(settings_file);
53+
load(settings_file)
13854
load('utility.js');
13955
load('modules.js');
14056
load('parseTools.js');

tools/shared.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ def run_js_tool(filename, jsargs=[], *args, **kw):
233233
implemented in javascript.
234234
"""
235235
command = NODE_JS + [filename] + jsargs
236+
print_compiler_stage(command)
236237
return check_call(command, *args, **kw).stdout
237238

238239

0 commit comments

Comments
 (0)