sproutit-narwhal 0.1.106
Sign up to get free protection for your applications and to get access to all the features.
- data/DISTRIBUTION.yml +15 -0
- data/README.md +86 -0
- data/Rakefile +349 -0
- data/VERSION.yml +7 -0
- data/bin/activate +50 -0
- data/bin/activate.bash +50 -0
- data/bin/activate.cmd +3 -0
- data/bin/js +67 -0
- data/bin/json +2 -0
- data/bin/narwhal +67 -0
- data/bin/narwhal.cmd +29 -0
- data/bin/sea +45 -0
- data/bin/sea.cmd +25 -0
- data/bin/tusk +2 -0
- data/bin/tusk.cmd +5 -0
- data/catalog.json +902 -0
- data/docs/available-packages.md +32 -0
- data/docs/browser-api-plan.md +290 -0
- data/docs/browser-api.md +153 -0
- data/docs/download.md +25 -0
- data/docs/engines.md +32 -0
- data/docs/json-tool.md +121 -0
- data/docs/lib/binary.wiki +242 -0
- data/docs/lib/file.wiki +325 -0
- data/docs/lib/os/popen.md +70 -0
- data/docs/modules.md +38 -0
- data/docs/narwhal.md +487 -0
- data/docs/packages-howto.md +32 -0
- data/docs/packages.md +30 -0
- data/docs/posts/2009-07-29-hello-0.1.md +19 -0
- data/docs/quick-start.md +69 -0
- data/docs/sea.md +49 -0
- data/engines/browser/lib/binary.js +2 -0
- data/engines/browser/lib/reactor.js +21 -0
- data/engines/browser/lib/system.js +3 -0
- data/engines/default/lib/array.js +164 -0
- data/engines/default/lib/binary-engine.js +53 -0
- data/engines/default/lib/binary.js +755 -0
- data/engines/default/lib/date.js +8 -0
- data/engines/default/lib/file-engine.js +119 -0
- data/engines/default/lib/function.js +119 -0
- data/engines/default/lib/global.js +11 -0
- data/engines/default/lib/io-engine.js +26 -0
- data/engines/default/lib/json.js +488 -0
- data/engines/default/lib/object.js +69 -0
- data/engines/default/lib/os-engine.js +3 -0
- data/engines/default/lib/reactor.js +12 -0
- data/engines/default/lib/string.js +84 -0
- data/engines/default/lib/system.js +20 -0
- data/engines/default/lib/worker.js +133 -0
- data/engines/jsc/README.md +18 -0
- data/engines/jsc/bootstrap.js +53 -0
- data/engines/jsc/deps/http-parser/LICENSE +77 -0
- data/engines/jsc/deps/http-parser/README.md +145 -0
- data/engines/jsc/deps/http-parser/http_parser.c +6087 -0
- data/engines/jsc/deps/http-parser/http_parser.h +141 -0
- data/engines/jsc/deps/http-parser/http_parser.rl +500 -0
- data/engines/jsc/deps/http-parser/test.c +858 -0
- data/engines/jsc/include/binary-engine.h +11 -0
- data/engines/jsc/include/io-engine.h +23 -0
- data/engines/jsc/include/narwhal.h +427 -0
- data/engines/jsc/lib/file-engine.js +31 -0
- data/engines/jsc/lib/http.js +1 -0
- data/engines/jsc/lib/io-engine.js +202 -0
- data/engines/jsc/lib/os-engine.js +25 -0
- data/engines/jsc/lib/system.js +18 -0
- data/engines/jsc/lib/zip.js +1 -0
- data/engines/jsc/narwhal-jsc.c +273 -0
- data/engines/jsc/narwhal.c +29 -0
- data/engines/jsc/package.json +8 -0
- data/engines/jsc/src/binary-engine.cc +290 -0
- data/engines/jsc/src/file-engine.cc +405 -0
- data/engines/jsc/src/io-engine.cc +423 -0
- data/engines/jsc/src/jack/handler/jill.cc +710 -0
- data/engines/jsc/src/os-engine.cc +210 -0
- data/engines/rhino/bin/narwhal-rhino +68 -0
- data/engines/rhino/bin/narwhal-rhino.cmd +34 -0
- data/engines/rhino/bootstrap.js +119 -0
- data/engines/rhino/jars/jline.jar +0 -0
- data/engines/rhino/jars/jna.jar +0 -0
- data/engines/rhino/jars/js.jar +0 -0
- data/engines/rhino/lib/binary-engine.js +83 -0
- data/engines/rhino/lib/concurrency.js +6 -0
- data/engines/rhino/lib/event-queue.js +18 -0
- data/engines/rhino/lib/file-engine.js +216 -0
- data/engines/rhino/lib/http-client-engine.js +90 -0
- data/engines/rhino/lib/http-engine.js +10 -0
- data/engines/rhino/lib/io-engine.js +347 -0
- data/engines/rhino/lib/md5-engine.js +40 -0
- data/engines/rhino/lib/os-engine.js +150 -0
- data/engines/rhino/lib/packages-engine.js +71 -0
- data/engines/rhino/lib/sandbox-engine.js +70 -0
- data/engines/rhino/lib/system.js +38 -0
- data/engines/rhino/lib/worker-engine.js +23 -0
- data/engines/rhino/lib/zip.js +78 -0
- data/engines/rhino/package.json +4 -0
- data/engines/secure/lib/file.js +6 -0
- data/engines/secure/lib/system.js +6 -0
- data/engines/template/bin/narwhal-engine-name +32 -0
- data/engines/template/bootstrap.js +40 -0
- data/engines/template/lib/file-engine.js +118 -0
- data/engines/template/lib/system.js +17 -0
- data/examples/browser-deployment-jackconfig.js +35 -0
- data/examples/fibonacci-worker.js +35 -0
- data/examples/fibonacci.js +19 -0
- data/examples/hello +2 -0
- data/examples/narwhal +3 -0
- data/examples/not-quite-a-quine.js +1 -0
- data/extconf.rb +44 -0
- data/gem_bin/narwhal +5 -0
- data/gem_bin/sea +4 -0
- data/gem_bin/tusk +4 -0
- data/lib/args.js +849 -0
- data/lib/base16.js +16 -0
- data/lib/base64.js +120 -0
- data/lib/codec/base64.js +8 -0
- data/lib/crc32.js +60 -0
- data/lib/file-bootstrap.js +187 -0
- data/lib/file.js +659 -0
- data/lib/hash.js +28 -0
- data/lib/hashp.js +65 -0
- data/lib/html.js +16 -0
- data/lib/http-client.js +134 -0
- data/lib/http.js +17 -0
- data/lib/io.js +98 -0
- data/lib/jsmin.js +315 -0
- data/lib/jsonpath.js +89 -0
- data/lib/logger.js +55 -0
- data/lib/md4.js +146 -0
- data/lib/md5.js +164 -0
- data/lib/mime.js +166 -0
- data/lib/narwhal.js +102 -0
- data/lib/narwhal/client.js +261 -0
- data/lib/narwhal/compile.js +99 -0
- data/lib/narwhal/env.js +140 -0
- data/lib/narwhal/inline.js +106 -0
- data/lib/narwhal/json.js +324 -0
- data/lib/narwhal/json.md +178 -0
- data/lib/narwhal/repl.js +96 -0
- data/lib/narwhal/server-test.js +6 -0
- data/lib/narwhal/server.js +270 -0
- data/lib/narwhal/tusk.js +170 -0
- data/lib/narwhal/tusk/bin.js +13 -0
- data/lib/narwhal/tusk/bundle.js +0 -0
- data/lib/narwhal/tusk/catalog.js +22 -0
- data/lib/narwhal/tusk/clone.js +66 -0
- data/lib/narwhal/tusk/consolidate.js +25 -0
- data/lib/narwhal/tusk/create-catalog.js +80 -0
- data/lib/narwhal/tusk/engine.js +42 -0
- data/lib/narwhal/tusk/freeze.js +0 -0
- data/lib/narwhal/tusk/init.js +56 -0
- data/lib/narwhal/tusk/install.js +288 -0
- data/lib/narwhal/tusk/list.js +20 -0
- data/lib/narwhal/tusk/orphans.js +0 -0
- data/lib/narwhal/tusk/reheat.js +15 -0
- data/lib/narwhal/tusk/remove.js +15 -0
- data/lib/narwhal/tusk/search.js +145 -0
- data/lib/narwhal/tusk/update.js +21 -0
- data/lib/narwhal/tusk/upgrade.js +0 -0
- data/lib/os.js +33 -0
- data/lib/packages.js +423 -0
- data/lib/printf.js +169 -0
- data/lib/promise.js +352 -0
- data/lib/querystring.js +176 -0
- data/lib/ref-send.js +257 -0
- data/lib/regexp.js +12 -0
- data/lib/sandbox.js +422 -0
- data/lib/sha.js +112 -0
- data/lib/sha256.js +102 -0
- data/lib/struct.js +228 -0
- data/lib/term.js +179 -0
- data/lib/test/assert.js +95 -0
- data/lib/test/equiv.js +188 -0
- data/lib/test/jsdump.js +165 -0
- data/lib/test/runner.js +129 -0
- data/lib/unload.js +13 -0
- data/lib/uri.js +378 -0
- data/lib/url.js +5 -0
- data/lib/utf8.js +64 -0
- data/lib/util.js +985 -0
- data/lib/uuid.js +89 -0
- data/lib/xregexp.js +521 -0
- data/local.json.template +1 -0
- data/narwhal.gemspec +105 -0
- data/narwhal.js +213 -0
- data/package.json +26 -0
- data/packages/readline/engines/default/lib/readline.js +4 -0
- data/packages/readline/engines/rhino/lib/readline.js +6 -0
- data/packages/readline/package.json +5 -0
- data/sources.json +207 -0
- data/tests/all-tests.js +17 -0
- data/tests/args.js +31 -0
- data/tests/args/domain.js +215 -0
- data/tests/args/options.js +36 -0
- data/tests/args/shifting.js +92 -0
- data/tests/args/validation.js +31 -0
- data/tests/base64.js +23 -0
- data/tests/commonjs.js +3 -0
- data/tests/commonjs/all-tests.js +12 -0
- data/tests/commonjs/bytearray-encodings-tests.js +69 -0
- data/tests/commonjs/bytearray-tests.js +465 -0
- data/tests/commonjs/bytestring-encodings-tests.js +89 -0
- data/tests/commonjs/bytestring-tests.js +263 -0
- data/tests/commonjs/es5/all-tests.js +3 -0
- data/tests/commonjs/es5/bind.js +29 -0
- data/tests/commonjs/file-tests.js +315 -0
- data/tests/commonjs/file/dirname.js +31 -0
- data/tests/commonjs/file/extension.js +45 -0
- data/tests/commonjs/file/is-absolute.js +11 -0
- data/tests/commonjs/file/iterator.js +101 -0
- data/tests/commonjs/file/normal.js +27 -0
- data/tests/commonjs/file/path.js +17 -0
- data/tests/commonjs/file/relative.js +42 -0
- data/tests/commonjs/file/resolve.js +44 -0
- data/tests/commonjs/module-tests.js +9 -0
- data/tests/commonjs/modules/absolute/b.js +1 -0
- data/tests/commonjs/modules/absolute/program.js +5 -0
- data/tests/commonjs/modules/absolute/submodule/a.js +3 -0
- data/tests/commonjs/modules/absolute/test.js +9 -0
- data/tests/commonjs/modules/all-tests.js +47 -0
- data/tests/commonjs/modules/config.js +11 -0
- data/tests/commonjs/modules/cyclic/a.js +4 -0
- data/tests/commonjs/modules/cyclic/b.js +4 -0
- data/tests/commonjs/modules/cyclic/program.js +10 -0
- data/tests/commonjs/modules/cyclic/test.js +9 -0
- data/tests/commonjs/modules/determinism/program.js +3 -0
- data/tests/commonjs/modules/determinism/submodule/a.js +8 -0
- data/tests/commonjs/modules/determinism/submodule/b.js +2 -0
- data/tests/commonjs/modules/determinism/test.js +9 -0
- data/tests/commonjs/modules/exactExports/a.js +3 -0
- data/tests/commonjs/modules/exactExports/program.js +4 -0
- data/tests/commonjs/modules/exactExports/test.js +9 -0
- data/tests/commonjs/modules/hasOwnProperty/hasOwnProperty.js +0 -0
- data/tests/commonjs/modules/hasOwnProperty/program.js +3 -0
- data/tests/commonjs/modules/hasOwnProperty/test.js +9 -0
- data/tests/commonjs/modules/hasOwnProperty/toString.js +0 -0
- data/tests/commonjs/modules/method/a.js +12 -0
- data/tests/commonjs/modules/method/program.js +8 -0
- data/tests/commonjs/modules/method/test.js +9 -0
- data/tests/commonjs/modules/missing/program.js +8 -0
- data/tests/commonjs/modules/missing/test.js +9 -0
- data/tests/commonjs/modules/monkeys/a.js +1 -0
- data/tests/commonjs/modules/monkeys/program.js +4 -0
- data/tests/commonjs/modules/monkeys/test.js +9 -0
- data/tests/commonjs/modules/nested/a/b/c/d.js +3 -0
- data/tests/commonjs/modules/nested/program.js +3 -0
- data/tests/commonjs/modules/nested/test.js +9 -0
- data/tests/commonjs/modules/relative/program.js +5 -0
- data/tests/commonjs/modules/relative/submodule/a.js +1 -0
- data/tests/commonjs/modules/relative/submodule/b.js +2 -0
- data/tests/commonjs/modules/relative/test.js +9 -0
- data/tests/commonjs/modules/transitive/a.js +1 -0
- data/tests/commonjs/modules/transitive/b.js +1 -0
- data/tests/commonjs/modules/transitive/c.js +3 -0
- data/tests/commonjs/modules/transitive/program.js +3 -0
- data/tests/commonjs/modules/transitive/test.js +9 -0
- data/tests/file/all-tests.js +61 -0
- data/tests/file/fnmatch.js +102 -0
- data/tests/file/glob.js +466 -0
- data/tests/file/match.js +102 -0
- data/tests/global.js +6 -0
- data/tests/global/array.js +19 -0
- data/tests/hashes.js +94 -0
- data/tests/html.js +13 -0
- data/tests/io/stringio.js +21 -0
- data/tests/os/all-tests.js +4 -0
- data/tests/os/popen.js +41 -0
- data/tests/os/system.js +22 -0
- data/tests/printf.js +123 -0
- data/tests/query-string.js +87 -0
- data/tests/sandbox/byte-io.js +20 -0
- data/tests/sandbox/fileName.js +3 -0
- data/tests/sandbox/foo.js +0 -0
- data/tests/sandbox/reload.js +79 -0
- data/tests/string.js +35 -0
- data/tests/uri.js +41 -0
- data/tests/util/all-tests.js +79 -0
- data/tests/util/array.js +207 -0
- data/tests/util/array/is-arguments.js +29 -0
- data/tests/util/array/is-array-like.js +29 -0
- data/tests/util/case.js +9 -0
- data/tests/util/collection.js +104 -0
- data/tests/util/eq.js +57 -0
- data/tests/util/expand.js +45 -0
- data/tests/util/object.js +125 -0
- data/tests/util/operator.js +25 -0
- data/tests/util/range.js +19 -0
- data/tests/util/repr.js +26 -0
- data/tests/util/string.js +34 -0
- data/tests/util/unique.js +12 -0
- metadata +434 -0
@@ -0,0 +1,70 @@
|
|
1
|
+
|
2
|
+
; popen(process):Popen
|
3
|
+
: process may be a String or Array of args. If it is a String, popen uses "/bin/sh", "-e" to execute your program.
|
4
|
+
|
5
|
+
Popen
|
6
|
+
-----
|
7
|
+
|
8
|
+
; stdin
|
9
|
+
: a text writer IO object attached to the stdin of subprocess.
|
10
|
+
; stdout
|
11
|
+
: a text reader IO object attached to the stdout of the supbrocess.
|
12
|
+
; stderr
|
13
|
+
: a text reader IO object attached to the stderr of the subprocess.
|
14
|
+
|
15
|
+
; wait() -> code:Number
|
16
|
+
: blocks execution until the subprocess has exited.
|
17
|
+
|
18
|
+
; communicate([stdin[, stdout[, stderr]]]):Communicate
|
19
|
+
: communicates with the process on stdin, stdout, and stderr concurrently with the attached streams. the given stdin may be a stream, string, or stringio stream. stdout and stderr may be streams or stringio objects, and default to stringio. communicate returns the pipes used to communicate with stdin, stdout, and stderr, by default providing stdout and stderr as stringio objects that have accumulated the entire output and errput of the subprocess.
|
20
|
+
|
21
|
+
Communicate
|
22
|
+
-----------
|
23
|
+
|
24
|
+
; stdin
|
25
|
+
: the text reader object used to communicate with the subprocess. if no stdin was provided as an argument to communicate, stdin will be an empty stringio. if a string was provided, stdin will be a stringio with whatever input was not consumed by the subprocess. if any other stream was provided, stdin will be that stream.
|
26
|
+
; stdout
|
27
|
+
: the text writer object that the subprocess wrote to on stdout. if no stdout was provided as an argument to communicate, stdout will be a stringio containing all of the accumulated output of the subprocess.
|
28
|
+
; stderr
|
29
|
+
: the text writer object that the subprocess wrote to on stderr. if no stderr was provided as an argument to communicate, stderr will be a stringio containing all of the accumulated errput of the subprocess.
|
30
|
+
; code
|
31
|
+
: the exit code of the subprocess
|
32
|
+
|
33
|
+
Examples
|
34
|
+
--------
|
35
|
+
|
36
|
+
This is the implementation of "system" in the "os" module:
|
37
|
+
|
38
|
+
exports.system = function (command, options) {
|
39
|
+
var process = exports.popen(command, options);
|
40
|
+
return process.communicate(
|
41
|
+
'',
|
42
|
+
system.stdout,
|
43
|
+
system.stderr
|
44
|
+
).code;
|
45
|
+
};
|
46
|
+
|
47
|
+
This is the implementation of the "command" function in the "os" module. "command" executes a subprocess and returns all of the output of the subprocess as a String. If the exit status code of the subprocess is non-zero, it throws an error containing all of the accumulated errput of the subprocess.
|
48
|
+
|
49
|
+
exports.command = function (command) {
|
50
|
+
var process = exports.popen(command);
|
51
|
+
var result = process.communicate();
|
52
|
+
if (result.code !== 0)
|
53
|
+
throw new Error(result.stderr.read());
|
54
|
+
return result.stdout.read();
|
55
|
+
};
|
56
|
+
|
57
|
+
This function uses the "sort" command to sort a list of numbers.
|
58
|
+
|
59
|
+
var io = require("io");
|
60
|
+
var os = require("os");
|
61
|
+
|
62
|
+
var list = io.StringIO();
|
63
|
+
list.print("30");
|
64
|
+
list.print("4");
|
65
|
+
list.print("1000");
|
66
|
+
list.print("200");
|
67
|
+
|
68
|
+
var process = os.popen(["sort", "-n", "-"]);
|
69
|
+
process.communicate(list).stdout.forEach(print);
|
70
|
+
|
data/docs/modules.md
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
|
2
|
+
Narwhal Modules
|
3
|
+
===============
|
4
|
+
|
5
|
+
Narwhal "scripts" are [ServerJS](https://wiki.mozilla.org/ServerJS) compatible modules, much like Python or Ruby modules. You do not have to use module pattern boilerplate; every module has its own local scope. You can get the exports object of another module by calling `require`.
|
6
|
+
|
7
|
+
var file = require("file");
|
8
|
+
file.isFile("foo.txt");
|
9
|
+
|
10
|
+
Module identifiers for `require` come in three flavors: "top-level", "relative", and "absolute". In the above case, "file" is a "top-level" identifier, so it will load any module called "file.js" in the "lib" directory of whichever package comes first in the load path. Relative identifiers have "." or ".." as their first term, and terms are delimited with "/". So, in the "foo/bar" module, "require('./baz')" will load "foo/baz". Absolute module identifiers should not be used directly, but are produced when you execute a program module outside the module path. The module is identified by its fully-qualified path, starting with "/".
|
11
|
+
|
12
|
+
You can export an object by assigning it to `exports`.
|
13
|
+
|
14
|
+
exports.foo = function () {
|
15
|
+
return "Hello";
|
16
|
+
};
|
17
|
+
|
18
|
+
In a module, you also get a `module` object that has `module.id` and `module.path` properties so you can inspect your own top-level module identifier, and the path of your own module file. You also get a `require.main` property that tells you the top-level module identifier of the module that started the program.
|
19
|
+
|
20
|
+
if (require.id == require.main)
|
21
|
+
main();
|
22
|
+
|
23
|
+
var settings = require(require.main);
|
24
|
+
|
25
|
+
var fs = require("file");
|
26
|
+
var path = fs.path(module.path);
|
27
|
+
var indexHtml = path.resolve("./template/index.html").read();
|
28
|
+
|
29
|
+
Inside a module, you also get a `print` function and a `system` object. The `system` object is equivalent to the `system` module, but always available. It exports `stdin`, `stdout`, `stderr`, `args`, `platform`, `platforms`, `prefix`, and other handy routines.
|
30
|
+
|
31
|
+
|
32
|
+
A Brief Tour
|
33
|
+
============
|
34
|
+
|
35
|
+
The main modules of the standard library are "system", "file", "io", and "os". There are also handy "json", "args", and "util" modules for a JSON codec, command line argument parsers, and utility functions.
|
36
|
+
|
37
|
+
I have already introduced `system`. The "file" module implements functions like "open", "read", "write", "copy", "move", "list", and others. "os" exports commands like "system", "command", "exit", "popen", and "enquote" for shell args.
|
38
|
+
|
data/docs/narwhal.md
ADDED
@@ -0,0 +1,487 @@
|
|
1
|
+
|
2
|
+
How Narwhal Works
|
3
|
+
=================
|
4
|
+
|
5
|
+
This document provides information on how to use `bin/narwhal`
|
6
|
+
through its command line options, environment variables,
|
7
|
+
and configuration files, then descends into the exact
|
8
|
+
maddenning details of how it goes about bootstrapping
|
9
|
+
and configuring itself.
|
10
|
+
|
11
|
+
|
12
|
+
Glossary
|
13
|
+
--------
|
14
|
+
|
15
|
+
* module: a JavaScript file that gets its own local scope
|
16
|
+
and certain free variables so that it may export and import
|
17
|
+
APIs.
|
18
|
+
|
19
|
+
* library: a directory that contains additional top-level
|
20
|
+
modules.
|
21
|
+
|
22
|
+
* package: a downloadable and installable component that
|
23
|
+
may include a library of additional modules, as well
|
24
|
+
as executables, source code, or other resources.
|
25
|
+
|
26
|
+
* sandbox: a system of module instances. sandboxes
|
27
|
+
are not necessarily secure in our parlance, but are
|
28
|
+
the finest security boundary Narwhal can support.
|
29
|
+
All modules in a sandbox are mutually vulnerable to
|
30
|
+
each other and to their containing sandbox. By
|
31
|
+
injecting frozen modules into a sandbox, or through
|
32
|
+
dependency injection using the `system` variable,
|
33
|
+
it will be eventually possible to construct secure
|
34
|
+
sandboxes. In a secure sandbox, monkey patching
|
35
|
+
globals will not be possible, and strict mode will
|
36
|
+
be enforced. However, all secure sandboxes will
|
37
|
+
be able to share the same primordial objects, particularly
|
38
|
+
Array, so managed communication among sandboxes will
|
39
|
+
be possible.
|
40
|
+
|
41
|
+
* sea: a sea for Narwhal is like a virtual environment.
|
42
|
+
for simplicity, the directory schema of a package, a sea,
|
43
|
+
and Narwhal itself are all the same. They all
|
44
|
+
have their own configuration and libraries, but Narwhal
|
45
|
+
always starts searching for packages and modules
|
46
|
+
in the current sea before searching for packages and
|
47
|
+
modules in the main Narwhal installation, or system
|
48
|
+
Narwhal installation.
|
49
|
+
|
50
|
+
|
51
|
+
Command Line Options
|
52
|
+
--------------------
|
53
|
+
|
54
|
+
* `-e -c --command COMMAND`
|
55
|
+
|
56
|
+
evaluate command (final option)
|
57
|
+
|
58
|
+
* `-r --require MODULE`
|
59
|
+
|
60
|
+
pre-load a module
|
61
|
+
|
62
|
+
* `-m --module MAIN`
|
63
|
+
|
64
|
+
run a module as a script (final option)
|
65
|
+
|
66
|
+
* `-I --include LIB`
|
67
|
+
|
68
|
+
add a library path to loader in the position of
|
69
|
+
highest precedence
|
70
|
+
|
71
|
+
* `-p --package PACKAGEPREFIXES`
|
72
|
+
|
73
|
+
add a package prefix directory
|
74
|
+
|
75
|
+
* `-d --debug`
|
76
|
+
|
77
|
+
set debug mode, system.debug = true
|
78
|
+
|
79
|
+
* `-P --no-packages`
|
80
|
+
|
81
|
+
do not load packages automatically
|
82
|
+
|
83
|
+
* `-v --verbose`
|
84
|
+
|
85
|
+
verbose mode: trace 'require' calls.
|
86
|
+
|
87
|
+
* `-l --log LEVEL`
|
88
|
+
|
89
|
+
set the log level (critical, error, warn, info, debug)
|
90
|
+
|
91
|
+
* `-: --path DELIMITER`
|
92
|
+
|
93
|
+
prints an augmented PATH with all package bins/
|
94
|
+
|
95
|
+
* `-V --version`
|
96
|
+
|
97
|
+
print Narwhal version number and exit.
|
98
|
+
|
99
|
+
|
100
|
+
Environment Variables
|
101
|
+
---------------------
|
102
|
+
|
103
|
+
* `NARWHAL_DEFAULT_ENGINE` may be set in `narwhal.conf` to a
|
104
|
+
engine name like `rhino`, `v8`, or `xulrunner`. Use
|
105
|
+
`tusk engines` for a complete list and consult the `README` in
|
106
|
+
that engine directory for details about its function and
|
107
|
+
readiness for use.
|
108
|
+
|
109
|
+
* `NARWHAL_ENGINE` may be set at the command line, but is
|
110
|
+
otherwise set to `NARWHAL_DEFAULT_ENGINE` by `bin/narwhal`
|
111
|
+
and exposed in JavaScript as `system.engine`. This
|
112
|
+
is the name of the JavaScript engine in use.
|
113
|
+
|
114
|
+
* `NARWHAL_HOME` is the path to the `narwhal` directory and
|
115
|
+
is available in JavaScript as `system.prefix`.
|
116
|
+
|
117
|
+
* `NARWHAL_ENGINE_HOME` is the path to the narwhal
|
118
|
+
engine directory, where `bootstrap.js` may be found,
|
119
|
+
and is set by `bin/narwhal`.
|
120
|
+
|
121
|
+
* `NARWHAL_PATH` and `JS_PATH` can be used to add
|
122
|
+
high priority library directories to the module
|
123
|
+
search path. These values are accessible in most
|
124
|
+
sandboxes as the `require.loader.paths` variable,
|
125
|
+
and may be editable in place with methods like
|
126
|
+
`shift`, `unshift`, and `splice`. Replacing
|
127
|
+
`require.loader.paths` with a new Array may not
|
128
|
+
have any effect. In secure sandboxes, `paths`
|
129
|
+
are not available.
|
130
|
+
|
131
|
+
* `NARWHAL_DEBUG` is an informational variable that
|
132
|
+
can also be set with the `-d` and `--debug` command
|
133
|
+
line options, and accessed or changed from within a
|
134
|
+
JavaScript module as `system.debug`. `NARWHAL_DEBUG`
|
135
|
+
gets coerced to a `Number`, and the options stack,
|
136
|
+
so `js -ddd -e 'print(system.debug)'` will print 3.
|
137
|
+
|
138
|
+
* `NARWHAL_VERBOSE` instructs the module loader
|
139
|
+
to report when modules have started and finished
|
140
|
+
loading. This environment variable must be used
|
141
|
+
to catalog modules that are loaded in the
|
142
|
+
bootstrapping process. Otherwise, you can use
|
143
|
+
the `-v` and `--verbose` options for the same
|
144
|
+
effect for modules that are loaded after the
|
145
|
+
command line arguments have been parsed, which
|
146
|
+
happens before packages are loaded.
|
147
|
+
|
148
|
+
* `NARWHAL_DEBUGGER` starts Narwhal with a debugger
|
149
|
+
GUI if one is available for the engine. For the
|
150
|
+
Rhino-Java engine, this activates the Rhino Java
|
151
|
+
AWT-based debugger.
|
152
|
+
|
153
|
+
* `SEA` is an environment variable set by `sea` that
|
154
|
+
notifies `narwhal` to search the given virtual
|
155
|
+
environment for packages first. This function can
|
156
|
+
be approximated by using the `-p` or `--package`
|
157
|
+
options to the `narwhal` or `js` command, and is
|
158
|
+
inspectable from within a module as the variable
|
159
|
+
`system.packagePrefixes[0]`.
|
160
|
+
|
161
|
+
* `SEALVL` (sea level) is an informational environment
|
162
|
+
variable provided by the `sea` command, analogous to
|
163
|
+
`SHLVL` (shell level) that is the number of instances
|
164
|
+
of `sea` the present shell is running in.
|
165
|
+
|
166
|
+
* `NARWHAL_JS_VERSION` refers to the JavaScript version,
|
167
|
+
that defaults to `"170"` for "1.7.0", and is used by
|
168
|
+
Rhino on Java to determine the valid JavaScript syntax.
|
169
|
+
|
170
|
+
|
171
|
+
Configuration Files
|
172
|
+
-------------------
|
173
|
+
|
174
|
+
* `narwhal.conf` may be provided to configure site-specific
|
175
|
+
or virtual-environment (sea) specific environment
|
176
|
+
variables like `NARWHAL_DEFAULT_ENGINE`. You can
|
177
|
+
also opt to specify `NARWHAL_ENGINE`, but that obviates
|
178
|
+
the possibility of allowing the user to override
|
179
|
+
the narwhal engine at the command line. `narwhal.conf`
|
180
|
+
follows the BSD convention of using shell scripts as
|
181
|
+
configuration files, so you may use any `bash` syntax
|
182
|
+
in this file. A `narwhal.conf.template` exists for
|
183
|
+
illustration.
|
184
|
+
|
185
|
+
* `package.json` describes the Narwhal package. Narwhal
|
186
|
+
itself is laid out as a package, so it might be used
|
187
|
+
as a standard library package for other engines that
|
188
|
+
might host module systems independently. `package.json`
|
189
|
+
names the package, its metadata, and its dependencies.
|
190
|
+
`package.json` should not be edited.
|
191
|
+
|
192
|
+
* `local.json` may be created to override the values
|
193
|
+
provided in `package.json` for site-specific configurations.
|
194
|
+
A `local.json.template` exists to illustrate how this
|
195
|
+
might be used to tell Narwhal that the parent directory
|
196
|
+
contains packages, as this is a common development
|
197
|
+
scenario.
|
198
|
+
|
199
|
+
* `sources.json` contains data for Tusk on where to
|
200
|
+
find `package.json` files and `package.zip` archives
|
201
|
+
so that it can create a catalog of all installable
|
202
|
+
packages, their descriptions, and dependencies.
|
203
|
+
This file should not be edited unless the intention
|
204
|
+
is to update the defaults provided for everyone.
|
205
|
+
|
206
|
+
* `.tusk/sources.json` may be created for site-specific
|
207
|
+
package sources and overrides the normal `sources.json`.
|
208
|
+
|
209
|
+
* `catalog.json` is meant to be maintained as a centrally
|
210
|
+
managed catalog that may be downloaded from Github to
|
211
|
+
`.tusk/catalog.json` using `tusk update`.
|
212
|
+
|
213
|
+
* `.tusk/catalog.json` is where `tusk` looks for information
|
214
|
+
about packages that can be downloaded and installed.
|
215
|
+
It may be downloaded with `tusk update` or built from
|
216
|
+
`sources.json` or `.tusk/sources.json` using
|
217
|
+
`tusk create-catalog`.
|
218
|
+
|
219
|
+
|
220
|
+
Bootstrapping Narwhal
|
221
|
+
---------------------
|
222
|
+
|
223
|
+
Narwhal launches in stages. On UNIX-like systems, Narwhal starts with a `bash` script, an engine specific `bash` script, an engine specific JavaScript, then the common JavaScript.
|
224
|
+
|
225
|
+
* `bin/narwhal`
|
226
|
+
|
227
|
+
At this stage, Narwhal uses only environment variables
|
228
|
+
for configuration. This script discovers its own
|
229
|
+
location on the file system and sources `narwhal.conf`
|
230
|
+
as a shell script to load any system-level configuration
|
231
|
+
variables like `NARWHAL_DEFAULT_ENGINE`. From there,
|
232
|
+
it discerns and exports the `NARWHAL_ENGINE` and
|
233
|
+
`NARWHAL_ENGINE_HOME` environment variables.
|
234
|
+
It then executes the
|
235
|
+
engine-specific script,
|
236
|
+
`$NARWHAL_ENGINE_HOME/bin/narwhal-$NARWHAL_ENGINE`.
|
237
|
+
|
238
|
+
* `engines/{engine}/bin/narwhal-{engine}`
|
239
|
+
|
240
|
+
This `bash` script performs some engine-specific
|
241
|
+
configuration, like augmenting the Java `CLASSPATH`
|
242
|
+
for the Rhino engine, and executes the
|
243
|
+
engine-specific bootstrap JavaScript using the
|
244
|
+
JavaScript engine for the engine.
|
245
|
+
|
246
|
+
Some engines, like `k7` require the JavaScript engine
|
247
|
+
to be on the `PATH`. The Rhino engine just expects
|
248
|
+
Java to be on the `PATH`, and uses the `js.jar` included
|
249
|
+
in the repository.
|
250
|
+
|
251
|
+
* `engines/{engine}/bootstrap.js`
|
252
|
+
|
253
|
+
This engine-specific JavaScript uses whatever
|
254
|
+
minimal mechanisms the JavaScript engine provides
|
255
|
+
for reading files and environment variables to
|
256
|
+
read and evaluate `narwhal.js`. `narwhal.js` evaluates
|
257
|
+
to a function expression that accepts a zygotic
|
258
|
+
`system` `Object`, to be replaced later by loading
|
259
|
+
the `system` module proper. `bootstrap.js` provides a
|
260
|
+
`system` object with `global`, `evalGlobal`, `engine`,
|
261
|
+
a `engines` Array, `print`, `fs.read`, `fs.isFile`,
|
262
|
+
`prefix`, `packagePrefixes`, and optionally `evaluate`,
|
263
|
+
`debug`, or `verbose`.
|
264
|
+
|
265
|
+
* `global` is the `global` `Object`. This is
|
266
|
+
passed explicitly in anticipation of times
|
267
|
+
when it will be much harder to grab this
|
268
|
+
object in engines where its name varies
|
269
|
+
(like `window`, or `this`) and where it will
|
270
|
+
be unsafe to assume that `this` defaults
|
271
|
+
to `global` for functions called anonymously.
|
272
|
+
|
273
|
+
* `evalGlobal` is a function that calls `eval` in
|
274
|
+
a scope where no global variables are masked
|
275
|
+
by local variables, but `var` declarations
|
276
|
+
are localized. This is passed explicitly
|
277
|
+
in anticipation of situations down the line where
|
278
|
+
it will be harder to call `eval` in a pristine
|
279
|
+
scope chain.
|
280
|
+
|
281
|
+
* `engine` is a synonym for the `NARWHAL_ENGINE`
|
282
|
+
environment variable, the name of the engine.
|
283
|
+
This variable is informational.
|
284
|
+
|
285
|
+
* `prefix` is a synonym for the `NARWHAL_HOME`
|
286
|
+
environment variable, the path leading to the
|
287
|
+
`narwhal` package containing `bin/narwhal`.
|
288
|
+
|
289
|
+
* `packagePrefixes` is a prioritized Array of all of
|
290
|
+
the package directories to search for packages
|
291
|
+
when that time comes. The first package prefix
|
292
|
+
should be the `SEA` environment variable, if it
|
293
|
+
exists and has a path. This is the first place
|
294
|
+
that the `packages` module will look for
|
295
|
+
packages to load. The last package prefix is
|
296
|
+
simply the `prefix`, `NARWHAL_HOME`. The `SEA`
|
297
|
+
prefix appears first so that virtual environments
|
298
|
+
can load their own package versions.
|
299
|
+
|
300
|
+
* `engines` is an Array of engine names, used
|
301
|
+
to extend the module search path at various stages
|
302
|
+
to include engine specific libraries. There will
|
303
|
+
usually be more than one engine in this Array.
|
304
|
+
For Rhino, it is `['rhino', 'default']`. The
|
305
|
+
`default` engine contains many "catch-all" modules
|
306
|
+
that, while being engine-specific, are also
|
307
|
+
general enough to be shared among almost all
|
308
|
+
engines. Other engines are likely to share
|
309
|
+
dynamically linked C modules in a "c" engine,
|
310
|
+
and the "rhino" engine itself is useful for
|
311
|
+
the "helma" engine.
|
312
|
+
|
313
|
+
* `print` is a temporary shortcut for writing a line to
|
314
|
+
a logging console or standard output, favoring
|
315
|
+
the latter if it is available.
|
316
|
+
|
317
|
+
* `fs` is a pimitive duck-type of the `file` module,
|
318
|
+
which will be loaded later. The module loader
|
319
|
+
uses `read` and `isFile` to load the initial modules.
|
320
|
+
|
321
|
+
* `evaluate` is a module evaluator. If the engine
|
322
|
+
does not provide an evaluator, the `sandbox` module
|
323
|
+
has a suitable default, but some engines provide
|
324
|
+
their own. For example, the "secure" engine
|
325
|
+
injects a safe, hermetic evaluator. `evaluate`
|
326
|
+
accepts a module as a String, and optionally
|
327
|
+
a file name and line number for debugging purposes.
|
328
|
+
`evaluate` returns a module factory `Function`
|
329
|
+
that accepts `require`, `exports`, `module`, `system`,
|
330
|
+
and `print`, the module-specific free variables for
|
331
|
+
getting the exported APIs of other modules, providing
|
332
|
+
their own exports, reading their meta data, and
|
333
|
+
conveniently accessing the `system` module and `print`
|
334
|
+
function respectively.
|
335
|
+
|
336
|
+
* `debug` is informational, may be used anywhere, and
|
337
|
+
is read from the `NARWHAL_DEBUG` environment variable,
|
338
|
+
and may be set later by the `-d` or `--debug` command
|
339
|
+
options.
|
340
|
+
|
341
|
+
* `verbose` instructs the module loader to log when
|
342
|
+
module start and finish loading, and is read
|
343
|
+
from the `NARWHAL_VERBOSE` environment variable,
|
344
|
+
and may be set later by the `-v` or `--verbose` command
|
345
|
+
options. To log the coming and going of modules
|
346
|
+
as they occur **before** the packages and program
|
347
|
+
modules get loaded, you must use the environment
|
348
|
+
variable.
|
349
|
+
|
350
|
+
* `narwhal.js`
|
351
|
+
|
352
|
+
This is the common script that creates a module loader,
|
353
|
+
makes the global scope consistent across engines,
|
354
|
+
finishes the `system` module, parses command line arguments,
|
355
|
+
loads packages, executes the desired program, and
|
356
|
+
finally calls the unload event for cleanup or running
|
357
|
+
a daemon event loop.
|
358
|
+
|
359
|
+
When Narwhal is embedded, the recommended practice is to load the `bootstrap.js` engine script directly, skipping the shell script phases.
|
360
|
+
|
361
|
+
Some engines, like Helma or GPSEE, may provide their own module loader implementation. In that case, they may bypass all of this bootstrapping business and simply include Narwhal as if it were a mere package.
|
362
|
+
|
363
|
+
No system has been constructed for Windows systems yet.
|
364
|
+
|
365
|
+
|
366
|
+
Narwhal Script
|
367
|
+
--------------
|
368
|
+
|
369
|
+
The `narwhal.js` script is the next layer of blubber.
|
370
|
+
|
371
|
+
* `sandbox` module (loaded manually from `lib/sandbox.js`),
|
372
|
+
provides the means to construct a `require` function
|
373
|
+
so all other modules can be loaded.
|
374
|
+
* `global` module, monkey patches the transitive globals
|
375
|
+
so that every engine receives the same ServerJS
|
376
|
+
and EcmaScript 5 global object, or as near to that
|
377
|
+
as possible.
|
378
|
+
* `system` module, including the `file` and `logger`
|
379
|
+
modules, which is provided for convenience as a free
|
380
|
+
variable in all modules.
|
381
|
+
* `narwhal` module parses arguments.
|
382
|
+
* `packages` module loads packages.
|
383
|
+
* `packages-engine` loads jars for Java/Rhino.
|
384
|
+
* run command
|
385
|
+
* `unload` module sends an `unload` signal to any
|
386
|
+
observers, usually for cleanup or to kick off event loops.
|
387
|
+
|
388
|
+
|
389
|
+
Sandbox Module
|
390
|
+
--------------
|
391
|
+
|
392
|
+
The sandbox module provides a basic module `Loader` for
|
393
|
+
module files on disk, a `MultiLoader` for plugable module
|
394
|
+
factory loaders (for things like Objective-J modules and
|
395
|
+
dynamically linked C modules), a `Sandbox` for creating and
|
396
|
+
memoizing module instances from the module factories. The
|
397
|
+
sandbox module is useful for creating new sandboxes from
|
398
|
+
within the main sandbox, which is useful for creating cheap
|
399
|
+
module system reloaders that will instantiate fresh modules but
|
400
|
+
only go to disk when the underlying module text has changed.
|
401
|
+
|
402
|
+
|
403
|
+
Global Module
|
404
|
+
-------------
|
405
|
+
|
406
|
+
The global module is engine-specific, and there is sharable
|
407
|
+
version in the default engine. The purpose of the global
|
408
|
+
module is to load modules like "json", "string", "array", and
|
409
|
+
"binary", that monkey patch the globals if necessary to
|
410
|
+
bring every engine up to speed with EcmaScript 5 and
|
411
|
+
the ServerJS standard.
|
412
|
+
|
413
|
+
|
414
|
+
System Module
|
415
|
+
-------------
|
416
|
+
|
417
|
+
The system module provides the ServerJS
|
418
|
+
[System](https://wiki.mozilla.org/ServerJS/System) module
|
419
|
+
standard, for standard IO streams, arguments, and environment
|
420
|
+
variables. The system module goes beyond spec by being
|
421
|
+
a free variable available in all modules, and by providing
|
422
|
+
`print`, `fs`, and `log` variables (at the time of this
|
423
|
+
writing). `print` is a late-bound alias for
|
424
|
+
`system.stdout.print`, which is to say that replacing
|
425
|
+
`system.stdout` will cause `print` to redirect to the new output
|
426
|
+
stream. `fs` is an alias for the `file` module, while `log` is a `Logger` instance from the `logger` module that prints time-stamped log messages to `system.stderr`.
|
427
|
+
|
428
|
+
|
429
|
+
Narwhal Module
|
430
|
+
--------------
|
431
|
+
|
432
|
+
The Narwhal module contains the command line parser declarations
|
433
|
+
for Narwhal, and an Easter egg.
|
434
|
+
|
435
|
+
|
436
|
+
Packages Module
|
437
|
+
---------------
|
438
|
+
|
439
|
+
The packages module analyzes and installs packages, such that their libraries are available in the module search path, and also installs some engine-specific package components like Java archives at run-time. The package loader uses a five pass algorithm:
|
440
|
+
|
441
|
+
* find and read package.json for every accessible package,
|
442
|
+
collating them into a catalog. This involves a breadth
|
443
|
+
first topological search of the `packages/` directory of each
|
444
|
+
`package` in the `system.packagePrefixes` Array. This
|
445
|
+
guarantees that the packages installed in the Sea
|
446
|
+
(virtual environment) can override the versions installed
|
447
|
+
with the system.
|
448
|
+
* verify that the catalog is internally consistent, dropping
|
449
|
+
any package that depends on another package that is
|
450
|
+
not installed.
|
451
|
+
* sort the libraries from packages so that libraries
|
452
|
+
that "depend" on other packages get higher precedence
|
453
|
+
in the module search path.
|
454
|
+
* "analyze" the packages in order. This involves finding
|
455
|
+
the library directories in each package, including
|
456
|
+
engine-specific libraries for all of the
|
457
|
+
`system.engines`, and performing engine-specific
|
458
|
+
analysis like finding the Java archives (`jars`) installed
|
459
|
+
in each package.
|
460
|
+
* "synthesize" a configuration from the analysis. This
|
461
|
+
involves setting the module search path, and performing
|
462
|
+
engine-specific synthesis, like installing a Java
|
463
|
+
class loader for the Java archives, and creating a new,
|
464
|
+
global `Packages` object.
|
465
|
+
|
466
|
+
Much of the weight of code in the `packages` module concerns
|
467
|
+
using both the conventional locations for libraries and whatnot,
|
468
|
+
but also handling overriden configuration values, gracefully
|
469
|
+
accepting both single Strings and Arrays of multiple options
|
470
|
+
for all directories. For example, `packages` assumes that
|
471
|
+
each package has a `lib` directory. However, the package may
|
472
|
+
provide a `package.json` that states that `lib` has been put
|
473
|
+
somewhere else, like `{"lib": "lib/js"}`, or even multiple
|
474
|
+
locations like `{"lib": ["lib/js", "usr/lib/js"]}`. This
|
475
|
+
applies to "packages" and "jars" as well.
|
476
|
+
|
477
|
+
|
478
|
+
Unload Module
|
479
|
+
-------------
|
480
|
+
|
481
|
+
When the program is finished, Narwhal checks whether the
|
482
|
+
"unload" module has been used. If so, it calls the "send"
|
483
|
+
function exported by that module, so that any observers attached
|
484
|
+
with the "when" method get called in first on first off order.
|
485
|
+
This is handy for modules like "reactor" that initiate an event
|
486
|
+
loop.
|
487
|
+
|