sproutit-narwhal 0.1.106

Sign up to get free protection for your applications and to get access to all the features.
Files changed (291) hide show
  1. data/DISTRIBUTION.yml +15 -0
  2. data/README.md +86 -0
  3. data/Rakefile +349 -0
  4. data/VERSION.yml +7 -0
  5. data/bin/activate +50 -0
  6. data/bin/activate.bash +50 -0
  7. data/bin/activate.cmd +3 -0
  8. data/bin/js +67 -0
  9. data/bin/json +2 -0
  10. data/bin/narwhal +67 -0
  11. data/bin/narwhal.cmd +29 -0
  12. data/bin/sea +45 -0
  13. data/bin/sea.cmd +25 -0
  14. data/bin/tusk +2 -0
  15. data/bin/tusk.cmd +5 -0
  16. data/catalog.json +902 -0
  17. data/docs/available-packages.md +32 -0
  18. data/docs/browser-api-plan.md +290 -0
  19. data/docs/browser-api.md +153 -0
  20. data/docs/download.md +25 -0
  21. data/docs/engines.md +32 -0
  22. data/docs/json-tool.md +121 -0
  23. data/docs/lib/binary.wiki +242 -0
  24. data/docs/lib/file.wiki +325 -0
  25. data/docs/lib/os/popen.md +70 -0
  26. data/docs/modules.md +38 -0
  27. data/docs/narwhal.md +487 -0
  28. data/docs/packages-howto.md +32 -0
  29. data/docs/packages.md +30 -0
  30. data/docs/posts/2009-07-29-hello-0.1.md +19 -0
  31. data/docs/quick-start.md +69 -0
  32. data/docs/sea.md +49 -0
  33. data/engines/browser/lib/binary.js +2 -0
  34. data/engines/browser/lib/reactor.js +21 -0
  35. data/engines/browser/lib/system.js +3 -0
  36. data/engines/default/lib/array.js +164 -0
  37. data/engines/default/lib/binary-engine.js +53 -0
  38. data/engines/default/lib/binary.js +755 -0
  39. data/engines/default/lib/date.js +8 -0
  40. data/engines/default/lib/file-engine.js +119 -0
  41. data/engines/default/lib/function.js +119 -0
  42. data/engines/default/lib/global.js +11 -0
  43. data/engines/default/lib/io-engine.js +26 -0
  44. data/engines/default/lib/json.js +488 -0
  45. data/engines/default/lib/object.js +69 -0
  46. data/engines/default/lib/os-engine.js +3 -0
  47. data/engines/default/lib/reactor.js +12 -0
  48. data/engines/default/lib/string.js +84 -0
  49. data/engines/default/lib/system.js +20 -0
  50. data/engines/default/lib/worker.js +133 -0
  51. data/engines/jsc/README.md +18 -0
  52. data/engines/jsc/bootstrap.js +53 -0
  53. data/engines/jsc/deps/http-parser/LICENSE +77 -0
  54. data/engines/jsc/deps/http-parser/README.md +145 -0
  55. data/engines/jsc/deps/http-parser/http_parser.c +6087 -0
  56. data/engines/jsc/deps/http-parser/http_parser.h +141 -0
  57. data/engines/jsc/deps/http-parser/http_parser.rl +500 -0
  58. data/engines/jsc/deps/http-parser/test.c +858 -0
  59. data/engines/jsc/include/binary-engine.h +11 -0
  60. data/engines/jsc/include/io-engine.h +23 -0
  61. data/engines/jsc/include/narwhal.h +427 -0
  62. data/engines/jsc/lib/file-engine.js +31 -0
  63. data/engines/jsc/lib/http.js +1 -0
  64. data/engines/jsc/lib/io-engine.js +202 -0
  65. data/engines/jsc/lib/os-engine.js +25 -0
  66. data/engines/jsc/lib/system.js +18 -0
  67. data/engines/jsc/lib/zip.js +1 -0
  68. data/engines/jsc/narwhal-jsc.c +273 -0
  69. data/engines/jsc/narwhal.c +29 -0
  70. data/engines/jsc/package.json +8 -0
  71. data/engines/jsc/src/binary-engine.cc +290 -0
  72. data/engines/jsc/src/file-engine.cc +405 -0
  73. data/engines/jsc/src/io-engine.cc +423 -0
  74. data/engines/jsc/src/jack/handler/jill.cc +710 -0
  75. data/engines/jsc/src/os-engine.cc +210 -0
  76. data/engines/rhino/bin/narwhal-rhino +68 -0
  77. data/engines/rhino/bin/narwhal-rhino.cmd +34 -0
  78. data/engines/rhino/bootstrap.js +119 -0
  79. data/engines/rhino/jars/jline.jar +0 -0
  80. data/engines/rhino/jars/jna.jar +0 -0
  81. data/engines/rhino/jars/js.jar +0 -0
  82. data/engines/rhino/lib/binary-engine.js +83 -0
  83. data/engines/rhino/lib/concurrency.js +6 -0
  84. data/engines/rhino/lib/event-queue.js +18 -0
  85. data/engines/rhino/lib/file-engine.js +216 -0
  86. data/engines/rhino/lib/http-client-engine.js +90 -0
  87. data/engines/rhino/lib/http-engine.js +10 -0
  88. data/engines/rhino/lib/io-engine.js +347 -0
  89. data/engines/rhino/lib/md5-engine.js +40 -0
  90. data/engines/rhino/lib/os-engine.js +150 -0
  91. data/engines/rhino/lib/packages-engine.js +71 -0
  92. data/engines/rhino/lib/sandbox-engine.js +70 -0
  93. data/engines/rhino/lib/system.js +38 -0
  94. data/engines/rhino/lib/worker-engine.js +23 -0
  95. data/engines/rhino/lib/zip.js +78 -0
  96. data/engines/rhino/package.json +4 -0
  97. data/engines/secure/lib/file.js +6 -0
  98. data/engines/secure/lib/system.js +6 -0
  99. data/engines/template/bin/narwhal-engine-name +32 -0
  100. data/engines/template/bootstrap.js +40 -0
  101. data/engines/template/lib/file-engine.js +118 -0
  102. data/engines/template/lib/system.js +17 -0
  103. data/examples/browser-deployment-jackconfig.js +35 -0
  104. data/examples/fibonacci-worker.js +35 -0
  105. data/examples/fibonacci.js +19 -0
  106. data/examples/hello +2 -0
  107. data/examples/narwhal +3 -0
  108. data/examples/not-quite-a-quine.js +1 -0
  109. data/extconf.rb +44 -0
  110. data/gem_bin/narwhal +5 -0
  111. data/gem_bin/sea +4 -0
  112. data/gem_bin/tusk +4 -0
  113. data/lib/args.js +849 -0
  114. data/lib/base16.js +16 -0
  115. data/lib/base64.js +120 -0
  116. data/lib/codec/base64.js +8 -0
  117. data/lib/crc32.js +60 -0
  118. data/lib/file-bootstrap.js +187 -0
  119. data/lib/file.js +659 -0
  120. data/lib/hash.js +28 -0
  121. data/lib/hashp.js +65 -0
  122. data/lib/html.js +16 -0
  123. data/lib/http-client.js +134 -0
  124. data/lib/http.js +17 -0
  125. data/lib/io.js +98 -0
  126. data/lib/jsmin.js +315 -0
  127. data/lib/jsonpath.js +89 -0
  128. data/lib/logger.js +55 -0
  129. data/lib/md4.js +146 -0
  130. data/lib/md5.js +164 -0
  131. data/lib/mime.js +166 -0
  132. data/lib/narwhal.js +102 -0
  133. data/lib/narwhal/client.js +261 -0
  134. data/lib/narwhal/compile.js +99 -0
  135. data/lib/narwhal/env.js +140 -0
  136. data/lib/narwhal/inline.js +106 -0
  137. data/lib/narwhal/json.js +324 -0
  138. data/lib/narwhal/json.md +178 -0
  139. data/lib/narwhal/repl.js +96 -0
  140. data/lib/narwhal/server-test.js +6 -0
  141. data/lib/narwhal/server.js +270 -0
  142. data/lib/narwhal/tusk.js +170 -0
  143. data/lib/narwhal/tusk/bin.js +13 -0
  144. data/lib/narwhal/tusk/bundle.js +0 -0
  145. data/lib/narwhal/tusk/catalog.js +22 -0
  146. data/lib/narwhal/tusk/clone.js +66 -0
  147. data/lib/narwhal/tusk/consolidate.js +25 -0
  148. data/lib/narwhal/tusk/create-catalog.js +80 -0
  149. data/lib/narwhal/tusk/engine.js +42 -0
  150. data/lib/narwhal/tusk/freeze.js +0 -0
  151. data/lib/narwhal/tusk/init.js +56 -0
  152. data/lib/narwhal/tusk/install.js +288 -0
  153. data/lib/narwhal/tusk/list.js +20 -0
  154. data/lib/narwhal/tusk/orphans.js +0 -0
  155. data/lib/narwhal/tusk/reheat.js +15 -0
  156. data/lib/narwhal/tusk/remove.js +15 -0
  157. data/lib/narwhal/tusk/search.js +145 -0
  158. data/lib/narwhal/tusk/update.js +21 -0
  159. data/lib/narwhal/tusk/upgrade.js +0 -0
  160. data/lib/os.js +33 -0
  161. data/lib/packages.js +423 -0
  162. data/lib/printf.js +169 -0
  163. data/lib/promise.js +352 -0
  164. data/lib/querystring.js +176 -0
  165. data/lib/ref-send.js +257 -0
  166. data/lib/regexp.js +12 -0
  167. data/lib/sandbox.js +422 -0
  168. data/lib/sha.js +112 -0
  169. data/lib/sha256.js +102 -0
  170. data/lib/struct.js +228 -0
  171. data/lib/term.js +179 -0
  172. data/lib/test/assert.js +95 -0
  173. data/lib/test/equiv.js +188 -0
  174. data/lib/test/jsdump.js +165 -0
  175. data/lib/test/runner.js +129 -0
  176. data/lib/unload.js +13 -0
  177. data/lib/uri.js +378 -0
  178. data/lib/url.js +5 -0
  179. data/lib/utf8.js +64 -0
  180. data/lib/util.js +985 -0
  181. data/lib/uuid.js +89 -0
  182. data/lib/xregexp.js +521 -0
  183. data/local.json.template +1 -0
  184. data/narwhal.gemspec +105 -0
  185. data/narwhal.js +213 -0
  186. data/package.json +26 -0
  187. data/packages/readline/engines/default/lib/readline.js +4 -0
  188. data/packages/readline/engines/rhino/lib/readline.js +6 -0
  189. data/packages/readline/package.json +5 -0
  190. data/sources.json +207 -0
  191. data/tests/all-tests.js +17 -0
  192. data/tests/args.js +31 -0
  193. data/tests/args/domain.js +215 -0
  194. data/tests/args/options.js +36 -0
  195. data/tests/args/shifting.js +92 -0
  196. data/tests/args/validation.js +31 -0
  197. data/tests/base64.js +23 -0
  198. data/tests/commonjs.js +3 -0
  199. data/tests/commonjs/all-tests.js +12 -0
  200. data/tests/commonjs/bytearray-encodings-tests.js +69 -0
  201. data/tests/commonjs/bytearray-tests.js +465 -0
  202. data/tests/commonjs/bytestring-encodings-tests.js +89 -0
  203. data/tests/commonjs/bytestring-tests.js +263 -0
  204. data/tests/commonjs/es5/all-tests.js +3 -0
  205. data/tests/commonjs/es5/bind.js +29 -0
  206. data/tests/commonjs/file-tests.js +315 -0
  207. data/tests/commonjs/file/dirname.js +31 -0
  208. data/tests/commonjs/file/extension.js +45 -0
  209. data/tests/commonjs/file/is-absolute.js +11 -0
  210. data/tests/commonjs/file/iterator.js +101 -0
  211. data/tests/commonjs/file/normal.js +27 -0
  212. data/tests/commonjs/file/path.js +17 -0
  213. data/tests/commonjs/file/relative.js +42 -0
  214. data/tests/commonjs/file/resolve.js +44 -0
  215. data/tests/commonjs/module-tests.js +9 -0
  216. data/tests/commonjs/modules/absolute/b.js +1 -0
  217. data/tests/commonjs/modules/absolute/program.js +5 -0
  218. data/tests/commonjs/modules/absolute/submodule/a.js +3 -0
  219. data/tests/commonjs/modules/absolute/test.js +9 -0
  220. data/tests/commonjs/modules/all-tests.js +47 -0
  221. data/tests/commonjs/modules/config.js +11 -0
  222. data/tests/commonjs/modules/cyclic/a.js +4 -0
  223. data/tests/commonjs/modules/cyclic/b.js +4 -0
  224. data/tests/commonjs/modules/cyclic/program.js +10 -0
  225. data/tests/commonjs/modules/cyclic/test.js +9 -0
  226. data/tests/commonjs/modules/determinism/program.js +3 -0
  227. data/tests/commonjs/modules/determinism/submodule/a.js +8 -0
  228. data/tests/commonjs/modules/determinism/submodule/b.js +2 -0
  229. data/tests/commonjs/modules/determinism/test.js +9 -0
  230. data/tests/commonjs/modules/exactExports/a.js +3 -0
  231. data/tests/commonjs/modules/exactExports/program.js +4 -0
  232. data/tests/commonjs/modules/exactExports/test.js +9 -0
  233. data/tests/commonjs/modules/hasOwnProperty/hasOwnProperty.js +0 -0
  234. data/tests/commonjs/modules/hasOwnProperty/program.js +3 -0
  235. data/tests/commonjs/modules/hasOwnProperty/test.js +9 -0
  236. data/tests/commonjs/modules/hasOwnProperty/toString.js +0 -0
  237. data/tests/commonjs/modules/method/a.js +12 -0
  238. data/tests/commonjs/modules/method/program.js +8 -0
  239. data/tests/commonjs/modules/method/test.js +9 -0
  240. data/tests/commonjs/modules/missing/program.js +8 -0
  241. data/tests/commonjs/modules/missing/test.js +9 -0
  242. data/tests/commonjs/modules/monkeys/a.js +1 -0
  243. data/tests/commonjs/modules/monkeys/program.js +4 -0
  244. data/tests/commonjs/modules/monkeys/test.js +9 -0
  245. data/tests/commonjs/modules/nested/a/b/c/d.js +3 -0
  246. data/tests/commonjs/modules/nested/program.js +3 -0
  247. data/tests/commonjs/modules/nested/test.js +9 -0
  248. data/tests/commonjs/modules/relative/program.js +5 -0
  249. data/tests/commonjs/modules/relative/submodule/a.js +1 -0
  250. data/tests/commonjs/modules/relative/submodule/b.js +2 -0
  251. data/tests/commonjs/modules/relative/test.js +9 -0
  252. data/tests/commonjs/modules/transitive/a.js +1 -0
  253. data/tests/commonjs/modules/transitive/b.js +1 -0
  254. data/tests/commonjs/modules/transitive/c.js +3 -0
  255. data/tests/commonjs/modules/transitive/program.js +3 -0
  256. data/tests/commonjs/modules/transitive/test.js +9 -0
  257. data/tests/file/all-tests.js +61 -0
  258. data/tests/file/fnmatch.js +102 -0
  259. data/tests/file/glob.js +466 -0
  260. data/tests/file/match.js +102 -0
  261. data/tests/global.js +6 -0
  262. data/tests/global/array.js +19 -0
  263. data/tests/hashes.js +94 -0
  264. data/tests/html.js +13 -0
  265. data/tests/io/stringio.js +21 -0
  266. data/tests/os/all-tests.js +4 -0
  267. data/tests/os/popen.js +41 -0
  268. data/tests/os/system.js +22 -0
  269. data/tests/printf.js +123 -0
  270. data/tests/query-string.js +87 -0
  271. data/tests/sandbox/byte-io.js +20 -0
  272. data/tests/sandbox/fileName.js +3 -0
  273. data/tests/sandbox/foo.js +0 -0
  274. data/tests/sandbox/reload.js +79 -0
  275. data/tests/string.js +35 -0
  276. data/tests/uri.js +41 -0
  277. data/tests/util/all-tests.js +79 -0
  278. data/tests/util/array.js +207 -0
  279. data/tests/util/array/is-arguments.js +29 -0
  280. data/tests/util/array/is-array-like.js +29 -0
  281. data/tests/util/case.js +9 -0
  282. data/tests/util/collection.js +104 -0
  283. data/tests/util/eq.js +57 -0
  284. data/tests/util/expand.js +45 -0
  285. data/tests/util/object.js +125 -0
  286. data/tests/util/operator.js +25 -0
  287. data/tests/util/range.js +19 -0
  288. data/tests/util/repr.js +26 -0
  289. data/tests/util/string.js +34 -0
  290. data/tests/util/unique.js +12 -0
  291. metadata +434 -0
@@ -0,0 +1,31 @@
1
+ var file = require("file");
2
+
3
+ exports.mkdirs = function(path) {
4
+ var components = file.Path(path).split();
5
+ for (var i = 0; i < components.length; i++) {
6
+ var dir = file.join.apply(null, components.slice(0, i+1));
7
+ if (!file.isDirectory(dir))
8
+ file.mkdir(dir);
9
+ }
10
+ };
11
+
12
+ exports.touch = function (path, mtime) {
13
+ if (mtime === undefined || mtime === null)
14
+ mtime = new Date();
15
+
16
+ if (!file.exists(path))
17
+ file.write(path, "");
18
+
19
+ return file.touchImpl(path, mtime.getTime());
20
+ };
21
+
22
+ exports.rename = function(source, target) {
23
+ source = file.path(source);
24
+ target = source.resolve(target);
25
+
26
+ return file.renameImpl(source, target);
27
+ };
28
+
29
+ exports.canonical = function(path) {
30
+ return file.normal(file.canonicalImpl(path));
31
+ };
@@ -0,0 +1 @@
1
+ system.stderr.print("Warning: http module not yet implemented for narwhal-jsc.");
@@ -0,0 +1,202 @@
1
+ var exports = require("io-engine"),
2
+ ByteString = require("binary").ByteString,
3
+ binary_engine = require("binary-engine"),
4
+ B_ALLOC = binary_engine.B_ALLOC,
5
+ B_COPY = binary_engine.B_COPY;
6
+
7
+ exports.IO.prototype.copy = function (output, mode, options) {
8
+ while (true) {
9
+ var buffer = this.read(null);
10
+ if (!buffer.length)
11
+ break;
12
+ output.write(buffer);
13
+ }
14
+ output.flush();
15
+ return this;
16
+ };
17
+
18
+ var ByteIO = exports.ByteIO = function(binary) {
19
+ this.buffer = [];
20
+ this.length = 0;
21
+
22
+ if (binary) this.write(binary);
23
+ }
24
+
25
+ ByteIO.prototype.toByteString = function() {
26
+ var result = this.read();
27
+
28
+ // replace the buffer with the collapsed ByteString
29
+ this.buffer = [result];
30
+ this.length = result.length;
31
+
32
+ return result;
33
+ }
34
+
35
+ ByteIO.prototype.decodeToString = function(charset) {
36
+ return charset ?
37
+ this.toByteString().decodeToString(charset) :
38
+ this.toByteString().decodeToString();
39
+ }
40
+
41
+ ByteIO.prototype.read = function(length) {
42
+ if (typeof length !== "number") length = this.length;
43
+
44
+ if (length <= 0)
45
+ return new ByteString();
46
+
47
+ var bytes = B_ALLOC(length),
48
+ position = 0;
49
+
50
+ while (position < length && this.length > 0) {
51
+ var segment = this.buffer[0];
52
+
53
+ var len = Math.min(segment._length, length - position);
54
+
55
+ B_COPY(segment._bytes, segment._offset, bytes, position, len);
56
+
57
+ position += len;
58
+ this.length -= len;
59
+
60
+ if (len < segment._length) {
61
+ this.buffer[0] = segment.slice(len);
62
+ } else {
63
+ this.buffer.shift();
64
+ }
65
+ }
66
+
67
+ return new ByteString(bytes, 0, position);
68
+ }
69
+
70
+ ByteIO.prototype.write = function(object) {
71
+ var segment = object.toByteString();
72
+
73
+ this.buffer.push(segment);
74
+ this.length += segment.length;
75
+
76
+ return this;
77
+ }
78
+
79
+ ByteIO.prototype.flush = function() {
80
+ return this;
81
+ }
82
+
83
+ ByteIO.prototype.close = function() {
84
+ }
85
+
86
+ var TextIOWrapper = exports.TextIOWrapper = function (raw, mode, lineBuffering, buffering, charset, options) {
87
+ if (mode.update) {
88
+ return new exports.TextIOStream(raw, lineBuffering, buffering, charset, options);
89
+ } else if (mode.write || mode.append) {
90
+ return new exports.TextOutputStream(raw, lineBuffering, buffering, charset, options);
91
+ } else if (mode.read) {
92
+ return new exports.TextInputStream(raw, lineBuffering, buffering, charset, options);
93
+ } else {
94
+ throw new Error("file must be opened for read, write, or append mode.");
95
+ }
96
+ };
97
+
98
+
99
+ var TextInputStream = exports.TextInputStream;
100
+
101
+ TextInputStream.prototype.readLine = TextInputStream.prototype.readLine || function () {
102
+ var buffer = [],
103
+ last;
104
+
105
+ do {
106
+ last = this.read(1);
107
+ buffer.push(last);
108
+ } while (last !== "\n" && last !== "");
109
+
110
+ return buffer.join("");
111
+ };
112
+
113
+ TextInputStream.prototype.next = TextInputStream.prototype.next || function () {
114
+ var line = this.readLine().slice(0, -1);
115
+ if (!line.length)
116
+ throw StopIteration;
117
+ return line;
118
+ };
119
+
120
+ TextInputStream.prototype.iterator = TextInputStream.prototype.iterator || function () {
121
+ return this;
122
+ };
123
+
124
+ TextInputStream.prototype.forEach = TextInputStream.prototype.forEach || function (block, context) {
125
+ var line;
126
+ while (true) {
127
+ try {
128
+ line = this.next();
129
+ } catch (exception) {
130
+ break;
131
+ }
132
+ block.call(context, line);
133
+ }
134
+ };
135
+
136
+ TextInputStream.prototype.input = TextInputStream.prototype.input || function () {
137
+ throw "NYI";
138
+ };
139
+
140
+ TextInputStream.prototype.readLines = TextInputStream.prototype.readLines || function () {
141
+ var lines = [];
142
+ do {
143
+ var line = this.readLine();
144
+ if (line.length)
145
+ lines.push(line);
146
+ } while (line.length);
147
+ return lines;
148
+ };
149
+
150
+ TextInputStream.prototype.readInto = TextInputStream.prototype.readInto || function (buffer) {
151
+ throw "NYI";
152
+ };
153
+
154
+ TextInputStream.prototype.copy = TextInputStream.prototype.copy || function (output, mode, options) {
155
+ do {
156
+ var line = this.readLine();
157
+ output.write(line).flush();
158
+ } while (line.length);
159
+ return this;
160
+ };
161
+
162
+ TextInputStream.prototype.close = TextInputStream.prototype.close || function () {
163
+ this.raw.close();
164
+ };
165
+
166
+
167
+ var TextOutputStream = exports.TextOutputStream = function (raw, lineBuffering, buffering, charset, options) {
168
+ this.raw = raw;
169
+ this.charset = charset;
170
+ }
171
+
172
+ TextOutputStream.prototype.write = function (str) {
173
+ this.raw.write(str.toByteString(this.charset));
174
+ return this;
175
+ };
176
+
177
+ TextOutputStream.prototype.writeLine = function (line) {
178
+ this.write(line + "\n"); // todo recordSeparator
179
+ return this;
180
+ };
181
+
182
+ TextOutputStream.prototype.writeLines = function (lines) {
183
+ lines.forEach(this.writeLine, this);
184
+ return this;
185
+ };
186
+
187
+ TextOutputStream.prototype.print = function () {
188
+ this.write(Array.prototype.join.call(arguments, " ") + "\n");
189
+ this.flush();
190
+ // todo recordSeparator, fieldSeparator
191
+ return this;
192
+ };
193
+
194
+ TextOutputStream.prototype.flush = function () {
195
+ this.raw.flush();
196
+ return this;
197
+ };
198
+
199
+ TextOutputStream.prototype.close = function () {
200
+ this.raw.close();
201
+ return this;
202
+ };
@@ -0,0 +1,25 @@
1
+ var exports = require("os-engine");
2
+
3
+ var io = require("io");
4
+
5
+ exports.system = function (command) {
6
+ if (Array.isArray(command)) {
7
+ command = command.map(require("os").enquote).join(" ");
8
+ }
9
+
10
+ return exports.systemImpl(command);
11
+ };
12
+
13
+ exports.popen = function (command) {
14
+ if (Array.isArray(command)) {
15
+ command = command.map(require("os").enquote).join(" ");
16
+ }
17
+
18
+ var result = exports.popenImpl(command);
19
+
20
+ result.stdin = new io.TextOutputStream(result.stdin);
21
+ result.stdout = new io.TextInputStream(result.stdout);
22
+ result.stderr = new io.TextInputStream(result.stderr);
23
+
24
+ return result;
25
+ };
@@ -0,0 +1,18 @@
1
+
2
+ var io = require("./io");
3
+
4
+ exports.stdin = new io.TextInputStream(new io.IO(io.STDIN_FILENO, -1));
5
+ exports.stdout = new io.TextOutputStream(new io.IO(-1, io.STDOUT_FILENO));
6
+ exports.stderr = new io.TextOutputStream(new io.IO(-1, io.STDERR_FILENO));
7
+
8
+ exports.args = ARGS;
9
+ delete ARGS;
10
+
11
+ exports.env = ENV;
12
+ delete ENV;
13
+
14
+ exports.fs = require('./file');
15
+
16
+ // default logger
17
+ var Logger = require("logger").Logger;
18
+ exports.log = new Logger(exports.stdout);
@@ -0,0 +1 @@
1
+ system.stderr.print("Warning: zip module not yet implemented for narwhal-jsc.");
@@ -0,0 +1,273 @@
1
+ #define NOTRACE
2
+
3
+ #include <JavaScriptCore/JavaScriptCore.h>
4
+ #include <stdio.h>
5
+ #include <sys/stat.h>
6
+ #include <readline/readline.h>
7
+ #include <readline/history.h>
8
+
9
+ #include <narwhal.h>
10
+
11
+ #include <dlfcn.h>
12
+
13
+ #ifdef JSCOCOA
14
+ #import <JSCocoa/JSCocoa.h>
15
+ #endif
16
+
17
+ #ifdef WEBKIT
18
+ #import <Foundation/Foundation.h>
19
+ #import <WebKit/WebKit.h>
20
+ #endif
21
+
22
+ NarwhalContext narwhal_context;
23
+
24
+ // Reads a file into a v8 string.
25
+ JSStringRef ReadFile(const char* name) {
26
+ FILE* file = fopen(name, "rb");
27
+ if (file == NULL) return NULL;
28
+
29
+ fseek(file, 0, SEEK_END);
30
+ int size = ftell(file);
31
+ rewind(file);
32
+
33
+ char *chars = (char*)malloc(sizeof(char) * (size + 1));
34
+ int i;
35
+ for (i = 0; i < size;) {
36
+ int read = fread(&chars[i], 1, size - i, file);
37
+ i += read;
38
+ }
39
+ chars[size] = '\0';
40
+ fclose(file);
41
+ JSStringRef result = JSStringCreateWithUTF8CString(chars);
42
+ free(chars);
43
+ return result;
44
+ }
45
+
46
+ FUNCTION(NW_print)
47
+ {
48
+ size_t i;
49
+ for (i = 0; i < ARGC; i++) {
50
+ JS_Print(ARGV(i));
51
+ if (_exception)
52
+ return NULL;
53
+ }
54
+
55
+ return JS_undefined;
56
+ }
57
+ END
58
+
59
+ FUNCTION(NW_read, ARG_UTF8(path))
60
+ {
61
+ ARG_COUNT(1);
62
+
63
+ JSStringRef contents = ReadFile(path);
64
+
65
+ if (contents) {
66
+ JSValueRef result = JSValueMakeString(_context, contents);
67
+ JSStringRelease(contents);
68
+
69
+ if (result)
70
+ return result;
71
+ }
72
+
73
+ *_exception = JSValueMakeStringWithUTF8CString(_context, "read() error occurred");
74
+
75
+ return JS_undefined;
76
+ }
77
+ END
78
+
79
+ FUNCTION(NW_isFile, ARG_UTF8(path))
80
+ {
81
+ ARG_COUNT(1);
82
+
83
+ struct stat stat_info;
84
+ int ret = stat(path, &stat_info);
85
+
86
+ return JS_bool(ret != -1 && S_ISREG(stat_info.st_mode));
87
+ }
88
+ END
89
+
90
+ typedef JSObjectCallAsFunctionCallback factory_t;
91
+ typedef const char *(*getModuleName_t)(NarwhalContext *);
92
+
93
+ FUNCTION(NW_requireNative, ARG_UTF8(topId), ARG_UTF8(path))
94
+ {
95
+ ARG_COUNT(2)
96
+
97
+ DEBUG("RequireNative: topId=[%s] path=[%s] \n", topId, path);
98
+
99
+ void *handle = dlopen(path, RTLD_LOCAL | RTLD_LAZY);
100
+ if (handle == NULL) {
101
+ printf("dlopen error: %s\n", dlerror());
102
+ return JS_null;
103
+ }
104
+
105
+ getModuleName_t narwhalModuleInit = (getModuleName_t)dlsym(handle, "narwhalModuleInit");
106
+ if (narwhalModuleInit == NULL) {
107
+ fprintf(stderr, "dlsym (getModuleName) error: %s\n", dlerror());
108
+ return JS_null;
109
+ }
110
+
111
+ const char *moduleName = narwhalModuleInit(&narwhal_context);
112
+
113
+ factory_t func = (factory_t)dlsym(handle, moduleName);
114
+ if (func == NULL) {
115
+ fprintf(stderr, "dlsym (%s) error: %s\n", moduleName, dlerror());
116
+ return JS_null;
117
+ }
118
+
119
+ return JS_fn(func);
120
+ }
121
+ END
122
+
123
+ JSObjectRef envpToObject(JSGlobalContextRef _context, JSValueRef *_exception, char *envp[])
124
+ {
125
+ JSObjectRef ENV = JSObjectMake(_context, NULL, NULL);
126
+
127
+ char *key, *value;
128
+ while (key = *(envp++)) {
129
+ value = strchr(key, '=') + 1;
130
+ *(value - 1) = '\0';
131
+
132
+ SET_VALUE(ENV, key, JS_str_utf8(value, strlen(value)));
133
+ HANDLE_EXCEPTION(true, true);
134
+
135
+ *(value - 1) = '=';
136
+ }
137
+
138
+ return ENV;
139
+ }
140
+
141
+ JSObjectRef argvToArray(JSGlobalContextRef _context, JSValueRef *_exception, int argc, char *argv[])
142
+ {
143
+ JSValueRef arguments[argc];
144
+
145
+ int i;
146
+ for (i = 0; i < argc; i++)
147
+ arguments[i] = JSValueMakeStringWithUTF8CString(_context, argv[i]);
148
+
149
+ JSObjectRef ARGS = JSObjectMakeArray(_context, argc, arguments, _exception);
150
+ HANDLE_EXCEPTION(true, true);
151
+
152
+ return ARGS;
153
+ }
154
+
155
+ // The read-eval-execute loop of the shell.
156
+ void* RunShell(JSContextRef _context) {
157
+ printf("Narwhal version %s, JavaScriptCore engine\n", NARWHAL_VERSION);
158
+ while (true)
159
+ {
160
+ #ifdef JSCOCOA
161
+ NSAutoreleasePool *pool = [NSAutoreleasePool new];
162
+ #endif
163
+ char *str = readline("> ");
164
+ if (str && *str)
165
+ add_history(str);
166
+
167
+ if (!str)
168
+ break;
169
+
170
+ JSStringRef source = JSStringCreateWithUTF8CString(str);
171
+ free(str);
172
+
173
+ LOCK();
174
+
175
+ JSValueRef exception = NULL;
176
+ JSValueRef *_exception = &exception;
177
+ if (JSCheckScriptSyntax(_context, source, 0, 0, _exception) && !(*_exception))
178
+ {
179
+ JSValueRef value = JSEvaluateScript(_context, source, 0, 0, 0, _exception);
180
+ HANDLE_EXCEPTION(true, false);
181
+
182
+ if (value && !JSValueIsUndefined(_context, value)) {
183
+ JS_Print(value);
184
+ HANDLE_EXCEPTION(true, false);
185
+ }
186
+ }
187
+ else
188
+ {
189
+ HANDLE_EXCEPTION(true, false);
190
+ }
191
+
192
+ JSStringRelease(source);
193
+
194
+ UNLOCK();
195
+
196
+ #ifdef JSCOCOA
197
+ [pool drain];
198
+ #endif
199
+ }
200
+ printf("\n");
201
+ }
202
+
203
+ JSValueRef narwhal(JSGlobalContextRef _context, JSValueRef *_exception, int argc, char *argv[], char *envp[])
204
+ {
205
+ pthread_mutex_t _mutex = PTHREAD_MUTEX_INITIALIZER;
206
+
207
+ narwhal_context.context = _context;
208
+ narwhal_context.mutex = &_mutex;
209
+
210
+ LOCK();
211
+
212
+ JSObjectRef global = JS_GLOBAL;
213
+
214
+ SET_VALUE(global, "print", JS_fn(NW_print));
215
+ SET_VALUE(global, "isFile", JS_fn(NW_isFile));
216
+ SET_VALUE(global, "read", JS_fn(NW_read));
217
+ SET_VALUE(global, "requireNative", JS_fn(NW_requireNative));
218
+ SET_VALUE(global, "ARGS", CALL(argvToArray, argc, argv));
219
+ SET_VALUE(global, "ENV", CALL(envpToObject, envp));
220
+
221
+ // Load bootstrap.js
222
+ char *NARWHAL_ENGINE_HOME = getenv("NARWHAL_ENGINE_HOME");
223
+ char *bootstrapPathRelative = "/bootstrap.js";
224
+ char bootstrapPathFull[strlen(NARWHAL_ENGINE_HOME) + strlen(bootstrapPathRelative) + 1];
225
+ snprintf(bootstrapPathFull, sizeof(bootstrapPathFull), "%s%s", NARWHAL_ENGINE_HOME, bootstrapPathRelative);
226
+
227
+ JSStringRef bootstrapSource = ReadFile(bootstrapPathFull);
228
+ if (!bootstrapSource) {
229
+ THROW("Error reading bootstrap.js\n");
230
+ }
231
+
232
+ JSEvaluateScript(_context, bootstrapSource, 0, 0, 0, _exception);
233
+ JSStringRelease(bootstrapSource);
234
+
235
+ UNLOCK();
236
+
237
+ if (!*_exception && argc <= 1)
238
+ RunShell(_context);
239
+ }
240
+
241
+ int main(int argc, char *argv[], char *envp[])
242
+ {
243
+ #ifdef WEBKIT
244
+ NSAutoreleasePool *pool = [NSAutoreleasePool new];
245
+ WebView * webView = [[WebView alloc] init];
246
+ JSGlobalContextRef _context = [[webView mainFrame] globalContext];
247
+ #elif defined(JSCOCOA)
248
+ NSAutoreleasePool *pool = [NSAutoreleasePool new];
249
+ JSCocoaController *jsc = [JSCocoa new];
250
+ JSGlobalContextRef _context = [jsc ctx];
251
+ #else
252
+ JSGlobalContextRef _context = JSGlobalContextCreate(NULL);
253
+ #endif
254
+
255
+ JSValueRef exception = NULL;
256
+ JSValueRef *_exception = &exception;
257
+
258
+ CALL(narwhal, argc, argv, envp);
259
+
260
+ int code = 0;
261
+ if (*_exception) {
262
+ code = 1;
263
+ JS_Print(*_exception);
264
+ }
265
+
266
+ #ifdef WEBKIT
267
+ [pool drain];
268
+ #elif defined(JSCOCOA)
269
+ [pool drain];
270
+ #endif
271
+
272
+ return code;
273
+ }