This repository has been archived by the owner on Dec 8, 2022. It is now read-only.
forked from 280north/narwhal
/
bootstrap.js
130 lines (113 loc) · 3.82 KB
/
bootstrap.js
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
(function(global, evalGlobal) {
// -- tlrobinson Tom Robinson
// -- kriskowal Kris Kowal Copyright (C) 2009-2010 MIT License
/*
this is a minimal engine-specific thunk for narwhal.js
that brings the NARWHAL_PATH environment variable into the global
scope using Rhino's special access to Java.
*/
var moduleScopingEnabled = false;
/* this gets used for several fixtures */
var context = Packages.org.mozilla.javascript.Context.getCurrentContext();
var setOptimizationLevel = function (n) {
context.setOptimizationLevel(Number(n));
};
context.setLanguageVersion(180);
context.getWrapFactory().setJavaPrimitiveWrap(false);
var prefix = "";
if (typeof NARWHAL_HOME != "undefined") {
prefix = NARWHAL_HOME;
delete NARWHAL_HOME;
} else {
prefix = String(Packages.java.lang.System.getenv("NARWHAL_HOME") || "");
}
if (typeof SEA != "undefined") {
Packages.java.lang.System.setProperty("SEA", SEA);
delete SEA;
}
var isFile = function (path) {
try { return new java.io.File(path).isFile(); } catch (e) {}
return false;
};
var read = function (path) {
var path = new java.io.File(path),
stream = new java.io.FileInputStream(path);
try {
var buffer = java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, path.length());
stream.read(buffer);
return String(new java.lang.String(buffer, "UTF-8"));
} finally {
stream.close();
}
};
var evaluate = function (text, fileName, lineNo) {
return function (inject) {
var names = [];
for (var name in inject)
if (Object.prototype.hasOwnProperty.call(inject, name))
names.push(name);
return context.compileFunction(
global,
"function(" + names.join(",") + "){" + text + "\n//*/\n}",
fileName,
lineNo,
null
).apply(null, names.map(function (name) {
return inject[name];
}));
};
};
var importScript = function (script) {
return context.evaluateReader(
global,
new Packages.java.io.FileReader(script),
script,
1,
null
);
};
var importScripts = function () {
for (var i = 0, ii = arguments.length; i < ii; i++) {
importScript(arguments[i]);
};
};
delete global.print;
var print = function (string) {
Packages.java.lang.System.out.println(String(string));
};
var narwhal = importScript(prefix + "/narwhal.js");
var debug = +String(Packages.java.lang.System.getenv("NARWHAL_DEBUG"));
var verbose = +String(Packages.java.lang.System.getenv("NARWHAL_VERBOSE"));
var os = String(Packages.java.lang.System.getProperty("os.name"));
try {
narwhal({
system: {
global: global,
evalGlobal: evalGlobal,
importScripts: importScripts,
engine: 'rhino',
engines: ['rhino', 'default'],
os: os,
print: print,
prefix: prefix,
evaluate: evaluate,
debug: debug,
verbose: verbose,
setOptimizationLevel: setOptimizationLevel
},
file: {
read: read,
isFile: isFile
}
});
} catch (e) {
if (e.rhinoException)
e.rhinoException.printStackTrace();
if (e.javaException)
e.javaException.printStackTrace();
print(e);
Packages.java.lang.System.exit(1);
}
})(this, function () {
return eval(arguments[0]);
});