This repository was archived by the owner on Jun 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathMetaScript.js
More file actions
362 lines (316 loc) · 13.8 KB
/
MetaScript.js
File metadata and controls
362 lines (316 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
Copyright 2013 Daniel Wirtz <[email protected]>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/**
* @license MetaScript (c) 2013 Daniel Wirtz <[email protected]>
* Released under the Apache License, Version 2.0
* see: https://github.com/dcodeIO/MetaScript for details
*/ //
(function(global) {
// not strict
// This is a rather small program with lots of comments, so everyone can hack it easily.
/**
* Constructs a new MetaScript instance.
* @param {string=} source Source to compile
* @constructor
*/
var MetaScript = function(source) {
/**
* Meta program.
* @type {?string}
*/
this.program = typeof source !== 'undefined' ? MetaScript.compile(source) : null;
};
/**
* Whether running under node.js or not.
* @type {boolean}
* @const
*/
MetaScript.IS_NODE = typeof require === 'function' && typeof process !== 'undefined' && typeof process.nextTick === 'function';
/**
* Compiles the specified source to a meta program and returns its source.
* @param {string} source Source
* @returns {string} Meta program
*/
MetaScript.compile = function(source) {
source = source+"";
var index = 0, // Current working index
expr = /(\/\/\?|\/\*\?)(=?)/g, // Line/block expression
exprLine = /\n|$/g, // Line terminator
exprBlock = /\*\//g, // Block terminator
match, matchEnd, // Matches
s, // Temporary string
indent = '', // Indentation
lastIndent = '', // Last indentation
out = []; // Output stack
// Escapes a string to be used in a JavaScript string enclosed in single quotes
function escape(s) {
return s.replace(/'/g, '\\\'').replace(/\r/g, '\\r').replace(/\n/g, '\\n');
}
// Evaluates a meta expression
function evaluate(expr) {
if (expr.substring(0, 2) === '==') {
return 'write(JSON.stringify('+expr.substring(2).trim()+'));\n';
} else if (expr.substring(0, 1) === '=') {
return 'write('+expr.substring(1).trim()+');\n';
} else if (expr !== '') {
return expr+'\n';
} else return '';
}
// Appends additional content to the program, if not empty
function append(source) {
if (s === '') return;
var index = 0,
expr = /\n/g,
s,
match;
while (match = expr.exec(source)) {
s = source.substring(index, match.index+1);
if (s !== '') out.push(' write(\''+escape(s)+'\');\n');
index = match.index+1;
}
s = source.substring(index, source.length);
if (s !== '') out.push(' write(\''+escape(s)+'\');\n');
}
// Turn the meta inside out:
while (match = expr.exec(source)) {
// Get leading contents
s = source.substring(index, match.index);
// Look if it is a line or a block of meta
if (match[1].indexOf('*') < 0) { // Line
// Trim whitespaces in front of the line and remember the indentation
if (match[2] !== '=')
s = s.replace(/(^|\n)([ \t]*)$/, function($0, $1, $2) { indent = $2; return $1; });
// Append leading contents
append(s);
// Find the end of the line
exprLine.lastIndex = match.index;
matchEnd = exprLine.exec(source);
// Expose indentation and evaluate expression
if (indent !== lastIndent) {
out.push('__=\''+escape(lastIndent = indent)+'\';\n');
}
out.push(evaluate(source.substring(match.index+3, matchEnd.index).trim()));
if (match[2] === '=')
out.push('writeln();\n');
// Move on
index = matchEnd.index+1;
} else { // Block
// Trim whitespaces in front of the block and remember the indentation
if (match[2] !== '=')
s = s.replace(/(^|\n)([ \t]*)$/, function($0, $1, $2) { indent = $2; return $1; });
// Append leading contents
append(s);
// Find the end of the block
exprBlock.lastIndex = match.index;
if (matchEnd = exprBlock.exec(source)) {
// Expose indentation and evaluate expression
if (indent !== lastIndent) {
out.push('__=\''+escape(lastIndent = indent)+'\';\n');
}
out.push(evaluate(source.substring(match.index+3, matchEnd.index).trim()));
// Move on
index = matchEnd.index+2;
// Trim whitespaces after the block if using a dedicated line
if (source.substr(index, 2) === '\r\n') {
index += 2;
} else if (source.substr(index, 1) === '\n')
index += 1;
} else throw(new Error("Unterminated meta block at "+match.index));
}
expr.lastIndex = index;
}
// Append the remaining contents
append(source.substring(index));
// And return the program
return out.join('');
};
/**
* Compiles the source to a meta program and transforms it using the specified scope. On node.js, this will wrap the
* entire process in a new VM context.
* @param {string} source Source
* @param {Object} scope Scope
* @param {string=} basedir Base directory for includes, defaults to `.` on node and `/` in the browser
* @returns {string} Transformed source
*/
MetaScript.transform = function(source, scope, basedir) {
if (MetaScript.IS_NODE) {
var sandbox;
require("vm").runInNewContext('__result = new MetaScript(__source).transform(__scope, __basedir);', sandbox = {
__source: source,
__scope: scope,
__basedir: basedir,
MetaScript: MetaScript
});
return sandbox.__result;
} else {
return new MetaScript(source).transform(scope, basedir);
}
};
/**
* Transforms the meta program.
* @param {Object} scope Scope
* @param {string=} basedir Base directory for includes, defaults to `.` on node and `/` in the browser
* @returns {string} Transformed source
*/
MetaScript.prototype.transform = function(scope, basedir) {
basedir = basedir || (MetaScript.IS_NODE ? "." : "/");
var vars = [];
for (var k in (scope || {})) {
if (scope.hasOwnProperty(k)) {
vars.push(k+" = "+JSON.stringify(scope[k])+";\n");
}
}
var __program = vars.join('')+this.program, // Meta program
__out = [], // Output buffer
__ = ''; // Indentation
///////////////////////////////////////////// Built-in functions ///////////////////////////////////////////////
/**
* Writes some contents to the document (no indentation).
* @param {*} s Contents to write
*/
function write(s) {
__out.push(s+"");
}
/**
* Writes some contents to the document, followed by a new line.
* @param {*} s Contents to write
*/
function writeln(s) {
if (typeof s === 'undefined') s = '';
write(s+"\n");
}
/**
* Extracts the directory name from a file name.
* @param {string} filename File name
* @returns {string} Directory name, defaults to `.`
*/
function dirname(filename) {
var p = Math.max(filename.lastIndexOf("/"), filename.lastIndexOf("\\\\"));
if (p >= 0) return filename.substring(0, p);
return ".";
}
/**
* Intents a block of text.
* @param {string} str Text to indent
* @param {string|number} indent Whitespace text to use for indentation or the number of whitespaces to use
* @returns {string} Indented text
*/
function indent(str, indent) {
if (typeof indent === 'number') {
var indent_str = '';
while (indent_str.length < indent) indent_str += ' ';
indent = indent_str;
}
var lines = str.split(/\n/);
for (var i=0; i<lines.length; i++) {
if (lines[i].trim() !== '')
lines[i] = indent + lines[i];
}
return lines.join("\n");
}
/**
* Includes another source file.
* @param {string} __filename File to include
* @param {boolean} __absolute Whether the path is absolute, defaults to `false` for a relative path
*/
function include(__filename, __absolute) {
__filename = __absolute ? __filename : (basedir === '/' ? basedir : basedir + '/') + __filename;
var __source;
if (MetaScript.IS_NODE) {
var files = require("glob").sync(__filename);
__source = "";
files.forEach(function(file, i) {
if (__source !== '') // Add line break between includes
__source += __source.indexOf('\r\n') >= 0 ? '\r\n' : '\n';
__source += require("fs").readFileSync(file)+"";
});
} else { // Pull it synchronously, FIXME: Is this working?
var request = XHR();
request.open('GET', filename, false);
request.send(null);
if (typeof request.responseText === 'string') { // status is 0 on local filesystem
__source = request.responseText;
} else throw(new Error("Failed to fetch '"+filename+"': "+request.status));
}
try {
var __program = MetaScript.compile(indent(__source, __));
eval(__program); // see: (*)
} catch (err) {
if (err.rethrow) throw(err);
err = new Error(err.message+" in included meta program of '"+__filename+"':\n"+indent(__source, 4));
err.rethrow = true;
throw(err);
}
}
/**
* Escaoes a string to be used inside of a single or double quote enclosed JavaScript string.
* @param {string} s String to escape
* @returns {string} Escaped string
*/
function escapestr(s) {
return s.replace(/'/g, '\\\'')
.replace(/"/g, '\\"')
.replace(/\r/g, '\\r')
.replace(/\n/g, '\\n');
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
try {
(function() {
// Using a wrapper function we enforce a unified behaviour of 'var's between the main and included
// sources, making them always local. Of course it would be possible to make just the main source's vars
// globally visible, but that'd be kinda hard to explain and maintain in a reliable way.
eval(__program); // see: (*)
})();
return __out.join('');
} catch (err) {
if (err.rethrow) throw(err);
err = new Error(err.message+" in main meta program:\n"+indent(vars.join('')+this.program, 4));
err.rethrow = true;
throw(err);
}
};
// (*) The use of eval() is - of course - potentially evil, but there is no way around it without making the library
// harder to use. To limit the impact we always use a fresh VM context under node.js in MetaScript.transform.
/**
* Constructs a XMLHttpRequest object.
* @returns {!XMLHttpRequest}
* @inner
*/
function XHR() {
var XMLHttpFactories = [
function () {return new XMLHttpRequest()},
function () {return new ActiveXObject("Msxml2.XMLHTTP")},
function () {return new ActiveXObject("Msxml3.XMLHTTP")},
function () {return new ActiveXObject("Microsoft.XMLHTTP")}
];
var xhr = null;
for (var i=0;i<XMLHttpFactories.length;i++) {
try { xhr = XMLHttpFactories[i](); }
catch (e) { continue; }
break;
}
if (!xhr) throw(new Error("XMLHttpRequest is not supported"));
return xhr;
}
// Enable module loading if available
if (typeof module != 'undefined' && module["exports"]) { // CommonJS
module["exports"] = MetaScript;
} else if (typeof define != 'undefined' && define["amd"]) { // AMD
define(function() { return MetaScript; });
} else { // Shim
if (!global["dcodeIO"]) {
global["dcodeIO"] = {};
}
global["dcodeIO"]["MetaScript"] = MetaScript;
}
})(this);