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
|
/* Copyright (c) 2006 Irakli Gozalishvili <rfobic@gmail.com>
See the file LICENSE for licensing information. */
/**
* Bootstrap file for the mozilla platform.
*/
(function(global, evalGlobal) {
global.arguments = __narwhal_args__;
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cu = Components.utils;
const Env = Cc["@mozilla.org/process/environment;1"].getService(Ci.nsIEnvironment);
var moduleScopingEnabled = false;
var debug = true;
var NARWHAL_PATH = Env.exists('NARWHAL_PATH') ? Env.get('NARWHAL_PATH') : null,
NARWHAL_HOME = Env.exists('NARWHAL_HOME') ? Env.get('NARWHAL_HOME') : null;
function print (message) {
dump(message + '\n')
}
function getFileUri(file) {
return Cc['@mozilla.org/network/io-service;1'].getService(Ci.nsIIOService)
.getProtocolHandler('file').QueryInterface(Ci.nsIFileProtocolHandler)
.getURLSpecFromFile(file);
}
function getFile(path) {
var file = Cc['@mozilla.org/file/local;1'].createInstance(Ci.nsILocalFile);
file.initWithPath(path);
for (var i=1; i < arguments.length; i++) file.append(arguments[i])
return file;
}
function read(path) {
const MODE_RDONLY = 0x01;
const PERMS_FILE = 0644;
var result = [];
try {
var fis = Cc["@mozilla.org/network/file-input-stream;1"].createInstance(Ci.nsIFileInputStream);
fis.init(getFile(path), MODE_RDONLY, PERMS_FILE, false);
var lis = fis.QueryInterface(Ci.nsILineInputStream);
var line = { value: null };
var haveMore;
do {
haveMore = lis.readLine(line)
result.push(line.value);
} while (haveMore)
} catch(e) {
print('Error:' + e.message);
print('Stack:' + e.stack);
} finally {
fis.close();
}
return result.join('\n');
}
function isFile(path) {
try {
var file = getFile(path);
return (file.exists() && file.isFile());
} catch (e) {
return false;
}
}
function evaluateInSandbox(code, path, lineNo) {
lineNo = lineNo || 0;
path = path || "anonymus";
var scope;
if (moduleScopingEnabled) {
scope = Cu.Sandbox(Cc["@mozilla.org/systemprincipal;1"].createInstance(Ci.nsIPrincipal));
scope.__proto__ = global;
} else {
scope = global;
}
var source = "(function(require,exports,module,system,print){" + code +"/**/\n})";
return Cu.evalInSandbox(source, scope, "1.8", path, lineNo);
}
function evaluateInGlobal(code, path, lineNo) {
lineNo = lineNo || 0;
path = path || "anonymus";
var source = "(function(require,exports,module,system,print){" + code +"/**/\n})";
return Cu.evalInSandbox(source, global, "1.8", path, lineNo);
}
var path = getFile(NARWHAL_HOME, 'narwhal.js').path;
var narwhal = Cu.evalInSandbox(read(path), global, "1.8", path, 0)
narwhal({
global: global,
evalGlobal: evalGlobal, //evaluateInGlobal,
evaluate: evaluateInSandbox,
platform: 'xulrunner',
platforms: ['xulrunner', 'default'],
debug: debug,
print: print,
fs: {
read: read,
isFile: isFile
},
prefix: NARWHAL_HOME,
path: NARWHAL_PATH
});
})(this, function () {
// no lexical arguments so they do not mask
// variables by the same name in global scope.
return eval(arguments[0]);
});
|