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,29 @@
1
+ #include <narwhal.h>
2
+
3
+ //#ifdef WEBKIT
4
+
5
+ JSObjectRef JSObjectMakeArray(JSContextRef _context, size_t argc, const JSValueRef argv[], JSValueRef* _exception)
6
+ {
7
+ JSObjectRef Array = GET_OBJECT(JS_GLOBAL, "Array");
8
+ return CALL_AS_CONSTRUCTOR(Array, argc, argv);
9
+ }
10
+
11
+ JSObjectRef JSObjectMakeDate(JSContextRef _context, size_t argc, const JSValueRef argv[], JSValueRef* _exception)
12
+ {
13
+ JSObjectRef Date = GET_OBJECT(JS_GLOBAL, "Date");
14
+ return CALL_AS_CONSTRUCTOR(Date, argc, argv);
15
+ }
16
+
17
+ JSObjectRef JSObjectMakeError(JSContextRef _context, size_t argc, const JSValueRef argv[], JSValueRef* _exception)
18
+ {
19
+ JSObjectRef Error = GET_OBJECT(JS_GLOBAL, "Error");
20
+ return CALL_AS_CONSTRUCTOR(Error, argc, argv);
21
+ }
22
+
23
+ JSObjectRef JSObjectMakeRegExp(JSContextRef _context, size_t argc, const JSValueRef argv[], JSValueRef* _exception)
24
+ {
25
+ JSObjectRef RegExp = GET_OBJECT(JS_GLOBAL, "RegExp");
26
+ return CALL_AS_CONSTRUCTOR(RegExp, argc, argv);
27
+ }
28
+
29
+ //#endif
@@ -0,0 +1,8 @@
1
+ {
2
+ "name": "narwhal-jsc",
3
+ "dependencies": ["narwhal"],
4
+ "engine": "jsc",
5
+ "author": "Tom Robinson (http://tlrobinson.net/)",
6
+ "description": "JavaScriptCore engine support for Narwhal",
7
+ "keywords": ["narwhal", "engine", "jsc", "javascriptcore", "webkit"]
8
+ }
@@ -0,0 +1,290 @@
1
+ #include <narwhal.h>
2
+ #include <stdlib.h>
3
+ #include <string.h>
4
+ #include <iconv.h>
5
+ #include <errno.h>
6
+
7
+ #include <binary-engine.h>
8
+
9
+ DESTRUCTOR(Bytes_finalize)
10
+ {
11
+ GET_INTERNAL(BytesPrivate *, data, object);
12
+ DEBUG("freeing bytes=[%p]\n", data);
13
+ if (data) {
14
+ if (data->buffer)
15
+ free(data->buffer);
16
+ data->buffer = NULL;
17
+ free(data);
18
+ }
19
+ }
20
+ END
21
+
22
+ #ifdef NARWHAL_JSC
23
+
24
+ JSClassRef Bytes_class(JSContextRef _context)
25
+ {
26
+ static JSClassRef jsClass;
27
+ if (!jsClass)
28
+ {
29
+ JSClassDefinition definition = kJSClassDefinitionEmpty;
30
+ definition.finalize = Bytes_finalize;
31
+
32
+ jsClass = JSClassCreate(&definition);
33
+ }
34
+ return jsClass;
35
+ }
36
+
37
+ JSObjectRef Bytes_new(JSContextRef _context, JSValueRef *_exception, char* buffer, int length)
38
+ {
39
+ BytesPrivate *data = (BytesPrivate*)malloc(sizeof(BytesPrivate));
40
+ if (!data) return NULL;
41
+
42
+ data->length = length;
43
+ data->buffer = buffer;
44
+
45
+ return JSObjectMake(_context, Bytes_class(_context), data);
46
+ }
47
+
48
+ #elif NARWHAL_V8
49
+
50
+ OBJECT(Bytes_new, 2, char* buffer, size_t length)
51
+ {
52
+ BytesPrivate *data = (BytesPrivate*)malloc(sizeof(BytesPrivate));
53
+
54
+ data->length = length;
55
+ data->buffer = buffer;
56
+
57
+ SET_INTERNAL(self, data);
58
+
59
+ Persistent<Object> _weak_handle = Persistent<Object>::New(self);
60
+ _weak_handle.MakeWeak(NULL, &Bytes_finalize);
61
+
62
+ return self;
63
+ }
64
+ END
65
+
66
+ #endif
67
+
68
+ FUNCTION(B_ALLOC, ARG_INT(length))
69
+ {
70
+ char *buffer = (char*)calloc(length, sizeof(char));
71
+ if (!buffer)
72
+ THROW("B_ALLOC: Couldn't alloc buffer");
73
+
74
+ return CALL(Bytes_new, buffer, length);
75
+ }
76
+ END
77
+
78
+ FUNCTION(B_LENGTH, ARG_OBJ(bytes))
79
+ {
80
+ GET_INTERNAL(BytesPrivate*, byte_data, bytes);
81
+ return JS_int(byte_data->length);
82
+ }
83
+ END
84
+
85
+ FUNCTION(B_FILL, ARG_OBJ(bytes), ARG_INT(from), ARG_INT(to), ARG_INT(value))
86
+ {
87
+ GET_INTERNAL(BytesPrivate*, byte_data, bytes);
88
+
89
+ if (!byte_data->buffer)
90
+ THROW("B_FILL: NULL buffer");
91
+
92
+ if (from > to || from < 0 || to >= byte_data->length)
93
+ THROW("B_FILL: tried to fill beyond bounds");
94
+
95
+ memset(byte_data->buffer + from, value, to - from);
96
+
97
+ return JS_undefined;
98
+ }
99
+ END
100
+
101
+ FUNCTION(B_COPY, ARG_OBJ(src), ARG_INT(srcOffset), ARG_OBJ(dst), ARG_INT(dstOffset), ARG_INT(length))
102
+ {
103
+ GET_INTERNAL(BytesPrivate*, src_data, src);
104
+ GET_INTERNAL(BytesPrivate*, dst_data, dst);
105
+
106
+ if (srcOffset + length > src_data->length || dstOffset + length > dst_data->length)
107
+ THROW("B_COPY: tried to copy beyond bounds");
108
+
109
+ memcpy(dst_data->buffer + dstOffset, src_data->buffer + srcOffset, length);
110
+
111
+ return JS_undefined;
112
+ }
113
+ END
114
+
115
+ FUNCTION(B_GET, ARG_OBJ(bytes), ARG_INT(index))
116
+ {
117
+ GET_INTERNAL(BytesPrivate*, bytes_data, bytes);
118
+
119
+ if (index >= bytes_data->length)
120
+ THROW("B_GET: tried get beyond bounds");
121
+
122
+ unsigned char b = bytes_data->buffer[index];
123
+
124
+ return JS_int((int)b);
125
+ }
126
+ END
127
+
128
+ FUNCTION(B_SET, ARG_OBJ(bytes), ARG_INT(index), ARG_INT(value))
129
+ {
130
+ GET_INTERNAL(BytesPrivate*, bytes_data, bytes);
131
+
132
+ if (index >= bytes_data->length)
133
+ THROW("B_SET: tried set beyond bounds");
134
+ if (value < 0 || value >= 256)
135
+ THROW("B_SET: tried to set out of byte range");
136
+
137
+ bytes_data->buffer[index] = (unsigned char)value;
138
+
139
+ return JS_undefined;
140
+ }
141
+ END
142
+
143
+ FUNCTION(B_DECODE_DEFAULT, ARG_OBJ(bytes), ARG_INT(offset), ARG_INT(length))
144
+ {
145
+ GET_INTERNAL(BytesPrivate*, bytes_data, bytes);
146
+ return JS_str_utf8((char *)(bytes_data->buffer + offset), length);
147
+ }
148
+ END
149
+
150
+ FUNCTION(B_ENCODE_DEFAULT, ARG_UTF8(string))
151
+ {
152
+ int length = strlen(string);
153
+
154
+ char* buffer = (char*)calloc(length, sizeof(char));
155
+ if (!buffer)
156
+ THROW("B_ENCODE_DEFAULT: Couldn't alloc buffer");
157
+
158
+ memcpy(buffer, string, length);
159
+
160
+ return CALL(Bytes_new, buffer, length);
161
+ }
162
+ END
163
+
164
+ int transcode(char *src, size_t srcLength, char **dstOut, size_t *dstLengthOut, const char *srcCodec, const char *dstCodec)
165
+ {
166
+ /*
167
+ printf("src=%p srcLength=%d srcCodec=%s dstCodec=%s\n", src, srcLength, srcCodec, dstCodec);
168
+ for (size_t i = 0; i < srcLength; i++)
169
+ printf("%02x ",(unsigned char)src[i]);
170
+ printf("\n");
171
+ //*/
172
+
173
+ int dst_length = srcLength*10; // FIXME!!!!!!
174
+ char *dst = (char*)calloc(dst_length, 1);
175
+ if (!dst) {
176
+ perror("transcode");
177
+ return 0;
178
+ }
179
+
180
+ iconv_t cd = iconv_open(dstCodec, srcCodec);
181
+ if (cd == (iconv_t)-1) {
182
+ perror("transcode");
183
+ return 0;
184
+ }
185
+
186
+ char *src_buf = src, *dst_buf = dst;
187
+ size_t src_bytes_left = (size_t)srcLength, dst_bytes_left = (size_t)dst_length, converted=0;
188
+
189
+ while (dst_bytes_left > 0)
190
+ {
191
+ if (src_bytes_left == 0)
192
+ break;
193
+ if ((converted = iconv(cd, &src_buf, &src_bytes_left, &dst_buf, &dst_bytes_left)) == (size_t)-1)
194
+ {
195
+ if (errno != EINVAL)
196
+ {
197
+ perror("transcode");
198
+ return 0;
199
+ }
200
+ }
201
+ }
202
+
203
+ if (dst_bytes_left >= sizeof (wchar_t))
204
+ *((wchar_t *) dst_buf) = L'\0';
205
+
206
+ if (iconv_close(cd)) {
207
+ perror("transcode");
208
+ return 0;
209
+ }
210
+
211
+ if (src_bytes_left > 0) {
212
+ perror("transcode");
213
+ return 0;
214
+ }
215
+
216
+ *dstOut = dst;
217
+ *dstLengthOut = (dst_length - dst_bytes_left);
218
+
219
+ return 1;
220
+ }
221
+
222
+ FUNCTION(B_DECODE, ARG_OBJ(bytes), ARG_INT(offset), ARG_INT(srcLength), ARG_UTF8(codec))
223
+ {
224
+ GET_INTERNAL(BytesPrivate*, src_data, bytes);
225
+
226
+ char *dst;
227
+ size_t dstLength;
228
+
229
+ if (!transcode((char *)(src_data->buffer + offset), srcLength, &dst, &dstLength, codec, UTF_16_ENCODING))
230
+ THROW("B_DECODE: iconv error");
231
+
232
+ NWString string = JS_str_utf16(dst, dstLength);
233
+ free(dst);
234
+
235
+ return string;
236
+ }
237
+ END
238
+
239
+ FUNCTION(B_ENCODE, ARG_STR(string), ARG_UTF8(codec))
240
+ {
241
+ char *src, *dst;
242
+ size_t srcLength, dstLength;
243
+
244
+ if (!GET_UTF16(string, &src, &srcLength))
245
+ THROW("BLAHHHHH");
246
+
247
+ if (!transcode(src, srcLength, &dst, &dstLength, UTF_16_ENCODING, codec))
248
+ THROW("B_ENCODE: iconv error");
249
+
250
+ free(src);
251
+
252
+ return CALL(Bytes_new, dst, dstLength);
253
+ }
254
+ END
255
+
256
+ FUNCTION(B_TRANSCODE, ARG_OBJ(srcBytes), ARG_INT(srcOffset), ARG_INT(srcLength), ARG_UTF8(srcCodec), ARG_UTF8(dstCodec))
257
+ {
258
+ GET_INTERNAL(BytesPrivate*, src_data, srcBytes);
259
+
260
+ char *dst;
261
+ size_t dstLength;
262
+
263
+ char *src = (char *)(src_data->buffer + srcOffset);
264
+ if (!transcode(src, srcLength, &dst, &dstLength, srcCodec, dstCodec))
265
+ THROW("B_TRANSCODE: iconv error");
266
+
267
+ return CALL(Bytes_new, dst, dstLength);
268
+ }
269
+ END
270
+
271
+ NARWHAL_MODULE(binary_engine)
272
+ {
273
+ EXPORTS("B_LENGTH", JS_fn(B_LENGTH));
274
+ EXPORTS("B_ALLOC", JS_fn(B_ALLOC));
275
+ EXPORTS("B_FILL", JS_fn(B_FILL));
276
+ EXPORTS("B_COPY", JS_fn(B_COPY));
277
+ EXPORTS("B_GET", JS_fn(B_GET));
278
+ EXPORTS("B_SET", JS_fn(B_SET));
279
+
280
+ EXPORTS("B_DECODE", JS_fn(B_DECODE));
281
+ EXPORTS("B_ENCODE", JS_fn(B_ENCODE));
282
+
283
+ EXPORTS("B_DECODE_DEFAULT", JS_fn(B_DECODE_DEFAULT));
284
+ EXPORTS("B_ENCODE_DEFAULT", JS_fn(B_ENCODE_DEFAULT));
285
+
286
+ EXPORTS("B_TRANSCODE", JS_fn(B_TRANSCODE));
287
+
288
+ EXPORTS("DEFAULT_CODEC", JS_str_utf8("UTF-8", strlen("UTF-8")));
289
+ }
290
+ END_NARWHAL_MODULE
@@ -0,0 +1,405 @@
1
+ #include <narwhal.h>
2
+
3
+ #include <dirent.h>
4
+ #include <errno.h>
5
+ #include <fcntl.h>
6
+ #include <limits.h>
7
+ #include <stdlib.h>
8
+ #include <string.h>
9
+ #include <sys/stat.h>
10
+ #include <unistd.h>
11
+ #include <utime.h>
12
+
13
+ #define PATH_OR_DOT(path) (path[0] == '\0' ? "." : path)
14
+
15
+ NWObject io;
16
+
17
+ FUNCTION(F_cwd)
18
+ {
19
+ ARG_COUNT(0);
20
+
21
+ char *cwd = getcwd(NULL, 0);
22
+ if (!cwd)
23
+ THROW("Couldn't get cwd.");
24
+
25
+ NWValue cwdValue = JS_str_utf8(cwd, strlen(cwd));
26
+ free(cwd);
27
+
28
+ return cwdValue;
29
+ }
30
+ END
31
+
32
+ FUNCTION(F_canonicalImpl, ARG_UTF8_CAST(path))
33
+ {
34
+ ARG_COUNT(1);
35
+
36
+ // FIXME: http://insanecoding.blogspot.com/2007/11/pathmax-simply-isnt.html
37
+ char resolved_name[PATH_MAX];
38
+
39
+ char *canon = realpath(PATH_OR_DOT(path), resolved_name);
40
+ if (!canon)
41
+ return JS_str_utf8(path, strlen(path));
42
+
43
+ return JS_str_utf8(canon, strlen(canon));
44
+ }
45
+ END
46
+
47
+ FUNCTION(F_mtime, ARG_UTF8_CAST(path))
48
+ {
49
+ ARG_COUNT(1);
50
+
51
+ struct stat stat_info;
52
+ int ret = stat(PATH_OR_DOT(path), &stat_info);
53
+ if (ret < 0)
54
+ THROW("mtime: %s", strerror(errno));
55
+
56
+ long long timestamp = ((long long)(stat_info.st_mtime)) * 1000;
57
+
58
+ return JS_date(timestamp);
59
+ }
60
+ END
61
+
62
+ FUNCTION(F_size, ARG_UTF8_CAST(path))
63
+ {
64
+ ARG_COUNT(1);
65
+
66
+ struct stat stat_info;
67
+ int ret = stat(PATH_OR_DOT(path), &stat_info);
68
+
69
+ return JS_int(stat_info.st_size);
70
+ }
71
+ END
72
+
73
+ FUNCTION(F_stat, ARG_UTF8_CAST(path))
74
+ {
75
+ ARG_COUNT(1);
76
+
77
+ struct stat stat_info;
78
+ int ret = stat(PATH_OR_DOT(path), &stat_info);
79
+
80
+ return JS_undefined;
81
+ }
82
+ END
83
+
84
+ FUNCTION(F_exists, ARG_UTF8_CAST(path))
85
+ {
86
+ ARG_COUNT(1);
87
+
88
+ struct stat stat_info;
89
+ int ret = stat(PATH_OR_DOT(path), &stat_info);
90
+
91
+ return JS_bool(ret != -1);
92
+ }
93
+ END
94
+
95
+ FUNCTION(F_linkExists, ARG_UTF8_CAST(path))
96
+ {
97
+ ARG_COUNT(1);
98
+
99
+ struct stat stat_info;
100
+ int ret = stat(PATH_OR_DOT(path), &stat_info);
101
+
102
+ return JS_bool(ret != -1 && (S_ISREG(stat_info.st_mode) || S_ISLNK(stat_info.st_mode)));
103
+ }
104
+ END
105
+
106
+ FUNCTION(F_isDirectory, ARG_UTF8_CAST(path))
107
+ {
108
+ ARG_COUNT(1);
109
+
110
+ struct stat stat_info;
111
+ int ret = stat(PATH_OR_DOT(path), &stat_info);
112
+
113
+ return JS_bool(ret != -1 && S_ISDIR(stat_info.st_mode));
114
+ }
115
+ END
116
+
117
+ FUNCTION(F_isFile, ARG_UTF8_CAST(path))
118
+ {
119
+ ARG_COUNT(1);
120
+
121
+ struct stat stat_info;
122
+ int ret = stat(PATH_OR_DOT(path), &stat_info);
123
+
124
+ return JS_bool(ret != -1 && S_ISREG(stat_info.st_mode));
125
+ }
126
+ END
127
+
128
+ FUNCTION(F_isLink, ARG_UTF8_CAST(path))
129
+ {
130
+ ARG_COUNT(1);
131
+
132
+ struct stat stat_info;
133
+ int ret = lstat(PATH_OR_DOT(path), &stat_info);
134
+
135
+ return JS_bool(ret != -1 && S_ISLNK(stat_info.st_mode));
136
+ }
137
+ END
138
+
139
+ FUNCTION(F_isReadable, ARG_UTF8_CAST(path))
140
+ {
141
+ ARG_COUNT(1);
142
+
143
+ return JS_bool(access(PATH_OR_DOT(path), R_OK) == 0);
144
+ }
145
+ END
146
+
147
+ FUNCTION(F_isWritable, ARG_UTF8_CAST(path))
148
+ {
149
+ ARG_COUNT(1);
150
+
151
+ return JS_bool(access(PATH_OR_DOT(path), W_OK) == 0);
152
+ }
153
+ END
154
+
155
+ FUNCTION(F_chmod, ARG_UTF8_CAST(path), ARG_INT(mode))
156
+ {
157
+ ARG_COUNT(2);
158
+
159
+ if (chmod(PATH_OR_DOT(path), mode) < 0)
160
+ THROW("failed to chmod %s", path);
161
+
162
+ return JS_undefined;
163
+ }
164
+ END
165
+
166
+ FUNCTION(F_chown, ARG_UTF8_CAST(path), ARG_UTF8_CAST(owner), ARG_UTF8_CAST(group))
167
+ {
168
+ ARG_COUNT(3);
169
+
170
+ THROW("NYI chown");
171
+
172
+ return JS_undefined;
173
+ }
174
+ END
175
+
176
+ FUNCTION(F_link, ARG_UTF8_CAST(source), ARG_UTF8_CAST(target))
177
+ {
178
+ ARG_COUNT(1);
179
+
180
+ THROW("NYI link");
181
+
182
+ return JS_undefined;
183
+ }
184
+ END
185
+
186
+ FUNCTION(F_symlink, ARG_UTF8_CAST(source), ARG_UTF8_CAST(target))
187
+ {
188
+ ARG_COUNT(2);
189
+
190
+ if (symlink(PATH_OR_DOT(source), PATH_OR_DOT(target)) < 0)
191
+ THROW("failed to symlink %s to %s", source, target);
192
+
193
+ return JS_undefined;
194
+ }
195
+ END
196
+
197
+ FUNCTION(F_renameImpl, ARG_UTF8_CAST(source), ARG_UTF8_CAST(target))
198
+ {
199
+ ARG_COUNT(2);
200
+
201
+ if (rename(PATH_OR_DOT(source), PATH_OR_DOT(target)) < 0)
202
+ THROW("failed to rename %s to %s", source, target);
203
+
204
+ return JS_undefined;
205
+ }
206
+ END
207
+
208
+ //FUNCTION(F_move, ARG_UTF8_CAST(source), ARG_UTF8_CAST(target))
209
+ //{
210
+ // ARG_COUNT(2);
211
+ //
212
+ // THROW("NYI move");
213
+ //
214
+ // return JS_undefined;
215
+ //}
216
+ //END
217
+
218
+ FUNCTION(F_remove, ARG_UTF8_CAST(path))
219
+ {
220
+ ARG_COUNT(1);
221
+
222
+ if (unlink(PATH_OR_DOT(path)) < 0)
223
+ THROW("failed to delete %s: %s", path, strerror(errno));
224
+
225
+ return JS_undefined;
226
+ }
227
+ END
228
+
229
+ FUNCTION(F_mkdir, ARG_UTF8_CAST(path))
230
+ {
231
+ ARG_COUNT(1);
232
+
233
+ if (mkdir(PATH_OR_DOT(path), 0777) < 0) {
234
+ // TODO: Should this fail
235
+ THROW("failed to make directory %s: %s", path, strerror(errno));
236
+ }
237
+
238
+ return JS_undefined;
239
+ }
240
+ END
241
+
242
+ //FUNCTION(F_mkdirs, ARG_UTF8_CAST(path))
243
+ //{
244
+ // ARG_COUNT(1);
245
+ //
246
+ // THROW("NYI mkdirs");
247
+ //
248
+ // return JS_undefined;
249
+ //}
250
+ //END
251
+
252
+ FUNCTION(F_rmdir, ARG_UTF8_CAST(path))
253
+ {
254
+ ARG_COUNT(1);
255
+
256
+ if (rmdir(PATH_OR_DOT(path)) < 0)
257
+ THROW("failed to remove the directory %s: %s", path, strerror(errno));
258
+
259
+ return JS_undefined;
260
+ }
261
+ END
262
+
263
+ FUNCTION(F_list, ARG_UTF8_CAST(path))
264
+ {
265
+ DIR *dp;
266
+ struct dirent *dirp;
267
+
268
+ if((dp = opendir(PATH_OR_DOT(path))) == NULL) {
269
+ THROW("%s", strerror(errno));
270
+ }
271
+
272
+ NWObject array = JS_array(0, NULL);
273
+
274
+ int index = 0;
275
+ while ((dirp = readdir(dp)) != NULL) {
276
+ if (strcmp(".", dirp->d_name) && strcmp("..", dirp->d_name))
277
+ SET_VALUE_AT_INDEX(array, index++, JS_str_utf8(dirp->d_name, dirp->d_namlen));
278
+ }
279
+ closedir(dp);
280
+
281
+ return array;
282
+ }
283
+ END
284
+
285
+ FUNCTION(F_touchImpl, ARG_UTF8_CAST(path), ARG_DOUBLE(mtime))
286
+ {
287
+ ARG_COUNT(2);
288
+
289
+ struct utimbuf times;
290
+ times.actime = (time_t)(mtime/1000.0);
291
+ times.modtime = (time_t)(mtime/1000.0);
292
+
293
+ if (utime(PATH_OR_DOT(path), &times) < 0)
294
+ THROW("unable to set mtime of %s: %s", path, strerror(errno));
295
+
296
+ return JS_undefined;
297
+ }
298
+ END
299
+
300
+ FUNCTION(F_FileIO, ARG_UTF8_CAST(path))
301
+ {
302
+ NWObject IO = GET_OBJECT(io, "IO");
303
+ NWObject mode_fn = GET_OBJECT(Exports, "mode");
304
+
305
+ ARGS_ARRAY(argv, ARGV(1));
306
+ NWObject modeObject = TO_OBJECT(CALL_AS_FUNCTION(mode_fn, JS_GLOBAL, 1, argv));
307
+
308
+ int readFlag = GET_BOOL(modeObject, "read");
309
+ int writeFlag = GET_BOOL(modeObject, "write");
310
+ int appendFlag = GET_BOOL(modeObject, "append");
311
+ int updateFlag = GET_BOOL(modeObject, "update");
312
+
313
+ DEBUG("path=%s readFlag=%d writeFlag=%d readFlag=%d readFlag=%d\n", path, readFlag, writeFlag, appendFlag, updateFlag);
314
+
315
+ int oflag = 0;
316
+
317
+ if (readFlag && writeFlag)
318
+ oflag = oflag | O_RDWR;
319
+ else if (writeFlag)
320
+ oflag = oflag | O_WRONLY;
321
+ else if (readFlag)
322
+ oflag = oflag | O_RDONLY;
323
+
324
+ if (appendFlag)
325
+ oflag = oflag | O_APPEND;
326
+ // if (!updateFlag)
327
+ // oflag = oflag | O_TRUNC;
328
+
329
+ if (updateFlag) {
330
+ THROW("Updating IO not yet implemented.");
331
+ } else if (writeFlag || appendFlag) {
332
+ oflag = oflag | O_CREAT;
333
+ if (!(oflag & O_APPEND))
334
+ oflag = oflag | O_TRUNC;
335
+
336
+ int fd = open(path, oflag, 0644);
337
+ DEBUG("fd=%d\n", fd);
338
+ if (fd < 0)
339
+ THROW("%s", strerror(errno));
340
+
341
+ ARGS_ARRAY(argv, JS_int(-1), JS_int(fd));
342
+ return TO_OBJECT(CALL_AS_CONSTRUCTOR(IO, 2, argv));
343
+ } else if (readFlag) {
344
+
345
+ int fd = open(path, oflag);
346
+ DEBUG("fd=%d\n", fd);
347
+ if (fd < 0)
348
+ THROW("%s", strerror(errno));
349
+
350
+ ARGS_ARRAY(argv, JS_int(fd), JS_int(-1));
351
+ return CALL_AS_CONSTRUCTOR(IO, 2, argv);
352
+ } else {
353
+ THROW("Files must be opened either for read, write, or update mode.");
354
+ }
355
+
356
+ return JS_undefined;
357
+ }
358
+ END
359
+
360
+
361
+ NARWHAL_MODULE(file_engine)
362
+ {
363
+ Exports = PROTECT_OBJECT(require("./file"));
364
+ io = PROTECT_OBJECT(require("io"));
365
+
366
+ EXPORTS("FileIO", JS_fn(F_FileIO));
367
+
368
+ EXPORTS("cwd", JS_fn(F_cwd));
369
+ //EXPORTS("canonical", JS_fn(F_canonical));
370
+ EXPORTS("canonicalImpl", JS_fn(F_canonicalImpl));
371
+ EXPORTS("mtime", JS_fn(F_mtime));
372
+ EXPORTS("size", JS_fn(F_size));
373
+ EXPORTS("stat", JS_fn(F_stat));
374
+ EXPORTS("exists", JS_fn(F_exists));
375
+ EXPORTS("linkExists", JS_fn(F_linkExists));
376
+ EXPORTS("isDirectory", JS_fn(F_isDirectory));
377
+ EXPORTS("isFile", JS_fn(F_isFile));
378
+ EXPORTS("isLink", JS_fn(F_isLink));
379
+ EXPORTS("isReadable", JS_fn(F_isReadable));
380
+ EXPORTS("isWritable", JS_fn(F_isWritable));
381
+ EXPORTS("chmod", JS_fn(F_chmod));
382
+ EXPORTS("chown", JS_fn(F_chown));
383
+ EXPORTS("link", JS_fn(F_link));
384
+ EXPORTS("symlink", JS_fn(F_symlink));
385
+ //EXPORTS("rename", JS_fn(F_rename));
386
+ //EXPORTS("move", JS_fn(F_rename));
387
+ EXPORTS("renameImpl", JS_fn(F_renameImpl));
388
+ //EXPORTS("move", JS_fn(F_move));
389
+ EXPORTS("remove", JS_fn(F_remove));
390
+ EXPORTS("mkdir", JS_fn(F_mkdir));
391
+ //EXPORTS("mkdirs", JS_fn(F_mkdirs));
392
+ EXPORTS("rmdir", JS_fn(F_rmdir));
393
+ EXPORTS("listImpl", JS_fn(F_list));
394
+ //EXPORTS("list", JS_fn(F_list));
395
+ EXPORTS("touchImpl", JS_fn(F_touchImpl));
396
+ //EXPORTS("touch", JS_fn(F_touch));
397
+
398
+ NWObject file_engine_js = require("file-engine.js");
399
+ EXPORTS("mkdirs", GET_VALUE(file_engine_js, "mkdirs"));
400
+ EXPORTS("touch", GET_VALUE(file_engine_js, "touch"));
401
+ EXPORTS("rename", GET_VALUE(file_engine_js, "rename"));
402
+ EXPORTS("move", GET_VALUE(file_engine_js, "rename"));
403
+ EXPORTS("canonical", GET_VALUE(file_engine_js, "canonical"));
404
+ }
405
+ END_NARWHAL_MODULE