|
| 1 | +#!/usr/bin/env node |
1 | 2 | // Copyright 2018 The Emscripten Authors. All rights reserved. |
2 | 3 | // Emscripten is available under two separate licenses, the MIT license and the |
3 | 4 | // University of Illinois/NCSA Open Source License. Both these licenses can be |
|
11 | 12 | // file with modified settings and supply the filename here. |
12 | 13 | // shell file This is the file that will be processed by the preprocessor |
13 | 14 |
|
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; |
22 | 15 |
|
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'); |
32 | 18 |
|
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; |
45 | 21 |
|
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; |
53 | 35 | } |
54 | | - return filename; |
55 | 36 | } |
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; |
116 | 38 | } |
117 | 39 |
|
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 | +}; |
121 | 44 |
|
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 | +}; |
132 | 48 |
|
133 | 49 | var settings_file = arguments_[0]; |
134 | 50 | var shell_file = arguments_[1]; |
135 | 51 | var process_macros = arguments_.indexOf('--expandMacros') >= 0; |
136 | 52 |
|
137 | | -load(settings_file); |
| 53 | +load(settings_file) |
138 | 54 | load('utility.js'); |
139 | 55 | load('modules.js'); |
140 | 56 | load('parseTools.js'); |
|
0 commit comments