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,23 @@
|
|
1
|
+
#ifndef __IO_ENGINE__
|
2
|
+
#define __IO_ENGINE__
|
3
|
+
|
4
|
+
#include <iconv.h>
|
5
|
+
|
6
|
+
typedef struct __IOPrivate {
|
7
|
+
int input;
|
8
|
+
int output;
|
9
|
+
} IOPrivate;
|
10
|
+
|
11
|
+
typedef struct __TextInputStreamPrivate {
|
12
|
+
int input;
|
13
|
+
iconv_t cd;
|
14
|
+
|
15
|
+
char *inBuffer;
|
16
|
+
size_t inBufferSize;
|
17
|
+
size_t inBufferUsed;
|
18
|
+
} TextInputStreamPrivate;
|
19
|
+
|
20
|
+
extern "C" JSClassRef IO_class(JSContextRef _context);
|
21
|
+
extern "C" JSClassRef TextInputStream_class(JSContextRef _context);
|
22
|
+
|
23
|
+
#endif
|
@@ -0,0 +1,427 @@
|
|
1
|
+
#ifndef __NARWHAL__
|
2
|
+
#define __NARWHAL__
|
3
|
+
|
4
|
+
// platform name
|
5
|
+
#define NARWHAL_JSC
|
6
|
+
#define NARWHAL_VERSION "0.2a"
|
7
|
+
|
8
|
+
#include <JavaScriptCore/JavaScriptCore.h>
|
9
|
+
#include <pthread.h>
|
10
|
+
|
11
|
+
JSObjectRef JSObjectMakeDate(JSContextRef, size_t, const JSValueRef[], JSValueRef*);
|
12
|
+
JSValueRef JSValueMakeStringWithUTF8CString(JSContextRef, const char *);
|
13
|
+
|
14
|
+
// You never know what sorts of CPUs we'll want to run Narwhal on...
|
15
|
+
#if defined(__hppa__) || \
|
16
|
+
defined(__m68k__) || defined(mc68000) || defined(_M_M68K) || \
|
17
|
+
(defined(__MIPS__) && defined(__MISPEB__)) || \
|
18
|
+
defined(__ppc__) || defined(__POWERPC__) || defined(_M_PPC) || \
|
19
|
+
defined(__sparc__)
|
20
|
+
#define UTF_16_ENCODING "UTF-16BE"
|
21
|
+
#else
|
22
|
+
#define UTF_16_ENCODING "UTF-16LE"
|
23
|
+
#endif
|
24
|
+
|
25
|
+
//#define DEBUG_ON
|
26
|
+
#ifdef DEBUG_ON
|
27
|
+
#define DEBUG(...) LOG(__VA_ARGS__)
|
28
|
+
#define THROW_DEBUG " (%s:%d)\n"
|
29
|
+
#else
|
30
|
+
#define NOTRACE
|
31
|
+
#define DEBUG(...)
|
32
|
+
#define THROW_DEBUG
|
33
|
+
#endif
|
34
|
+
|
35
|
+
// "#define NOTRACE" at the top of files you don't want to enable tracing on
|
36
|
+
#ifdef NOTRACE
|
37
|
+
#define TRACE(...)
|
38
|
+
#else
|
39
|
+
#define TRACE(...) LOG(__VA_ARGS__);
|
40
|
+
#endif
|
41
|
+
|
42
|
+
#define ERROR(...) LOG(__VA_ARGS__)
|
43
|
+
#define LOG(...) fprintf(stderr, __VA_ARGS__);
|
44
|
+
|
45
|
+
#define THROW(format, ...) \
|
46
|
+
{ char msg[1024]; snprintf(msg, 1024, format THROW_DEBUG, ##__VA_ARGS__, __FILE__, __LINE__); \
|
47
|
+
DEBUG("THROWING: %s", msg); \
|
48
|
+
*_exception = JSValueMakeStringWithUTF8CString(_context, msg); \
|
49
|
+
return NULL;/*JSValueMakeUndefined(_context);*/ }
|
50
|
+
|
51
|
+
#define HANDLE_EXCEPTION_BLOCK(block) if (*_exception) block;
|
52
|
+
#define HANDLE_EXCEPTION_PRINT() HANDLE_EXCEPTION_BLOCK({ JS_Print(*_exception); });
|
53
|
+
#define HANDLE_EXCEPTION_RETURN(...) HANDLE_EXCEPTION_BLOCK({ return __VA_ARGS__; });
|
54
|
+
|
55
|
+
#define HANDLE_EXCEPTION(shouldPrint, shouldReturn) \
|
56
|
+
HANDLE_EXCEPTION_BLOCK({ \
|
57
|
+
if (shouldPrint) {fprintf(stderr, "ERROR:" THROW_DEBUG, __FILE__, __LINE__); JS_Print(*_exception);} \
|
58
|
+
if (shouldReturn) return NULL; \
|
59
|
+
})
|
60
|
+
|
61
|
+
JSValueRef JSValueMakeStringWithUTF8CString(JSContextRef _context, const char *string)
|
62
|
+
{
|
63
|
+
JSStringRef stringRef = JSStringCreateWithUTF8CString(string);
|
64
|
+
if (!stringRef)
|
65
|
+
return NULL;
|
66
|
+
JSValueRef valueRef = JSValueMakeString(_context, stringRef);
|
67
|
+
JSStringRelease(stringRef);
|
68
|
+
return valueRef;
|
69
|
+
}
|
70
|
+
|
71
|
+
JSValueRef JSValueMakeStringWithUTF16(JSContextRef _context, JSChar *string, size_t len)
|
72
|
+
{
|
73
|
+
JSStringRef stringRef = JSStringCreateWithCharacters(string, len);
|
74
|
+
if (!stringRef)
|
75
|
+
return NULL;
|
76
|
+
JSValueRef valueRef = JSValueMakeString(_context, stringRef);
|
77
|
+
JSStringRelease(stringRef);
|
78
|
+
return valueRef;
|
79
|
+
}
|
80
|
+
|
81
|
+
void JSValuePrint(JSContextRef _context, JSValueRef *_exception, JSValueRef value)
|
82
|
+
{
|
83
|
+
JSValueRef tmpException = NULL;
|
84
|
+
|
85
|
+
if (!value) return;
|
86
|
+
|
87
|
+
JSStringRef string = JSValueToStringCopy(_context, value, &tmpException);
|
88
|
+
if (!string || tmpException) {
|
89
|
+
if (_exception)
|
90
|
+
*_exception = tmpException;
|
91
|
+
return;
|
92
|
+
}
|
93
|
+
|
94
|
+
size_t length = JSStringGetMaximumUTF8CStringSize(string);
|
95
|
+
char buffer[length];
|
96
|
+
|
97
|
+
JSStringGetUTF8CString(string, buffer, length);
|
98
|
+
JSStringRelease(string);
|
99
|
+
|
100
|
+
puts(buffer);
|
101
|
+
}
|
102
|
+
|
103
|
+
#define STRINGIZE2(s) #s
|
104
|
+
#define STRINGIZE(s) STRINGIZE2(s)
|
105
|
+
|
106
|
+
#define NWObject JSObjectRef
|
107
|
+
#define NWValue JSValueRef
|
108
|
+
#define NWString JSValueRef
|
109
|
+
#define NWDate JSObjectRef
|
110
|
+
#define NWArray JSObjectRef
|
111
|
+
typedef JSChar NWChar;
|
112
|
+
|
113
|
+
#define THIS _this
|
114
|
+
#define JS_GLOBAL JSContextGetGlobalObject(_context)
|
115
|
+
|
116
|
+
#define ARGC _argc
|
117
|
+
#define ARGV(index) _argv[index]
|
118
|
+
#define ARG_COUNT(c) if ( ARGC != c ) { THROW("Insufficient arguments"); }
|
119
|
+
|
120
|
+
#define IS_NUMBER(value) JSValueIsNumber(_context, value)
|
121
|
+
#define IS_OBJECT(value) JSValueIsObject(_context, value)
|
122
|
+
#define IS_FUNCTION(value) JSObjectIsFunction(_context, value)
|
123
|
+
#define IS_STRING(value) JSValueIsString(_context, value)
|
124
|
+
|
125
|
+
#define TO_OBJECT(value) JSValueToObject(_context, value, _exception)
|
126
|
+
|
127
|
+
#define TO_STRING(value) _TO_STRING(_context, _exception, value)
|
128
|
+
JSValueRef _TO_STRING(JSContextRef _context, JSValueRef *_exception, JSValueRef value) {
|
129
|
+
JSValueRef tmpException = NULL;
|
130
|
+
|
131
|
+
if (!value) {
|
132
|
+
THROW("NULL toString!");
|
133
|
+
}
|
134
|
+
|
135
|
+
JSStringRef string = JSValueToStringCopy(_context, value, &tmpException);
|
136
|
+
if (tmpException) {
|
137
|
+
if (_exception)
|
138
|
+
*_exception = tmpException;
|
139
|
+
return NULL;
|
140
|
+
}
|
141
|
+
|
142
|
+
JSValueRef stringValue = JSValueMakeString(_context, string);
|
143
|
+
|
144
|
+
JSStringRelease(string);
|
145
|
+
|
146
|
+
return stringValue;
|
147
|
+
}
|
148
|
+
|
149
|
+
#define ARGN_INT(variable, index) \
|
150
|
+
if (index >= ARGC || !IS_NUMBER(ARGV(index))) THROW("Argument %d must be a number.", index) \
|
151
|
+
int variable = (int)JSValueToNumber(_context, ARGV(index), _exception); \
|
152
|
+
if (*_exception) { return NULL; };
|
153
|
+
|
154
|
+
#define ARGN_DOUBLE(variable, index) \
|
155
|
+
if (index >= ARGC || !IS_NUMBER(ARGV(index))) THROW("Argument %d must be a number.", index) \
|
156
|
+
double variable = JSValueToNumber(_context, ARGV(index), _exception); \
|
157
|
+
if (*_exception) { return NULL; };
|
158
|
+
|
159
|
+
#define ARGN_OBJ(variable, index) \
|
160
|
+
if (index >= ARGC || !IS_OBJECT(ARGV(index))) THROW("Argument %d must be an object.", index) \
|
161
|
+
JSObjectRef variable = JSValueToObject(_context, ARGV(index), _exception); \
|
162
|
+
if (*_exception) { return NULL; };
|
163
|
+
|
164
|
+
#define ARGN_FN(variable, index) \
|
165
|
+
ARGN_OBJ(variable, index); \
|
166
|
+
if (!IS_FUNCTION(variable)) { THROW("Argument %d must be a function.", index); } \
|
167
|
+
|
168
|
+
#define ARGN_STR(variable, index) \
|
169
|
+
if (index >= ARGC || !IS_STRING(ARGV(index))) THROW("Argument %d must be a string.", index) \
|
170
|
+
JSValueRef variable = ARGV(index);
|
171
|
+
|
172
|
+
#define ARGN_STR_OR_NULL(variable, index) \
|
173
|
+
JSValueRef variable = ((index < ARGC) && IS_STRING(ARGV(index))) ? ARGV(index) : NULL;
|
174
|
+
|
175
|
+
|
176
|
+
#define GET_UTF8(variable, value) \
|
177
|
+
{_tmpStr = JSValueToStringCopy(_context, value, _exception);\
|
178
|
+
if (*_exception) { return NULL; }\
|
179
|
+
_tmpSz = JSStringGetMaximumUTF8CStringSize(_tmpStr); }\
|
180
|
+
char variable[_tmpSz]; \
|
181
|
+
{JSStringGetUTF8CString(_tmpStr, variable, _tmpSz);\
|
182
|
+
JSStringRelease(_tmpStr);}
|
183
|
+
|
184
|
+
#define ARGN_UTF8_CAST(variable, index) \
|
185
|
+
if (index >= ARGC) THROW("Argument %d must be a string.", index) \
|
186
|
+
GET_UTF8(variable, ARGV(index));
|
187
|
+
|
188
|
+
#define ARGN_UTF8(variable, index) \
|
189
|
+
if (index >= ARGC && !IS_STRING(ARGV(index))) THROW("Argument %d must be a string.", index) \
|
190
|
+
ARGN_UTF8_CAST(variable, index)
|
191
|
+
|
192
|
+
#define ARG_INT(variable) 0; ARGN_INT(variable, _argn); _argn++; 0
|
193
|
+
#define ARG_DOUBLE(variable) 0; ARGN_DOUBLE(variable, _argn); _argn++; 0
|
194
|
+
#define ARG_OBJ(variable) 0; ARGN_OBJ(variable, _argn); _argn++; 0
|
195
|
+
#define ARG_FN(variable) 0; ARGN_FN(variable, _argn); _argn++; 0
|
196
|
+
#define ARG_STR(variable) 0; ARGN_STR(variable, _argn); _argn++; 0
|
197
|
+
#define ARG_UTF8(variable) 0; ARGN_UTF8(variable, _argn); _argn++; 0
|
198
|
+
#define ARG_UTF8_CAST(variable) 0; ARGN_UTF8_CAST(variable, _argn); _argn++; 0
|
199
|
+
|
200
|
+
#define JS_undefined JSValueMakeUndefined(_context)
|
201
|
+
#define JS_null JSValueMakeNull(_context)
|
202
|
+
#define JS_int(number) JSValueMakeNumber(_context, (double)number)
|
203
|
+
#define JS_bool(b) JSValueMakeBoolean(_context, (int)b)
|
204
|
+
#define JS_str_utf8(str, len) JSValueMakeStringWithUTF8CString(_context, str)
|
205
|
+
#define JS_str_utf16(str, len) JSValueMakeStringWithUTF16(_context, (JSChar*)str, (len)/sizeof(JSChar))
|
206
|
+
|
207
|
+
//#define JS_obj(value) JSValueToObject(_context, value, _exception)
|
208
|
+
#define JS_fn(f) JSObjectMakeFunctionWithCallback(_context, NULL, f)
|
209
|
+
#define JS_array(count, array) JSObjectMakeArray(_context, count, array, _exception)
|
210
|
+
|
211
|
+
#define JS_date(ms) _JS_date(_context, _exception, ms)
|
212
|
+
NWValue _JS_date(JSContextRef _context, JSValueRef* _exception, long long usec) {
|
213
|
+
JSValueRef argv[] = { JS_int(usec) };
|
214
|
+
return JSObjectMakeDate(_context, 1, argv, _exception);
|
215
|
+
}
|
216
|
+
|
217
|
+
#define GET_VALUE(object, name) _GET_VALUE(_context, _exception, object, name)
|
218
|
+
JSValueRef _GET_VALUE(JSContextRef _context, JSValueRef *_exception, JSObjectRef object, const char *name) {
|
219
|
+
JSStringRef nameStr = JSStringCreateWithUTF8CString(name);
|
220
|
+
JSValueRef value = JSObjectGetProperty(_context, object, nameStr, _exception);
|
221
|
+
JSStringRelease(nameStr);
|
222
|
+
return value;
|
223
|
+
}
|
224
|
+
|
225
|
+
#define SET_VALUE(object, name, value) \
|
226
|
+
{JSStringRef nameStr = JSStringCreateWithUTF8CString(name); \
|
227
|
+
JSObjectSetProperty(_context, object, nameStr, value, kJSPropertyAttributeNone, _exception); \
|
228
|
+
JSStringRelease(nameStr); \
|
229
|
+
if (*_exception) { return NULL; }}
|
230
|
+
|
231
|
+
#define SET_VALUE_AT_INDEX(object, index, value) \
|
232
|
+
JSObjectSetPropertyAtIndex(_context, object, index, value, _exception); \
|
233
|
+
if (*_exception) { return NULL; }
|
234
|
+
|
235
|
+
#define GET_OBJECT(object, name) \
|
236
|
+
JSValueToObject(_context, GET_VALUE(object, name), _exception)
|
237
|
+
|
238
|
+
#define GET_INT(object, name) \
|
239
|
+
((int)JSValueToNumber(_context, GET_VALUE(object, name), _exception))
|
240
|
+
|
241
|
+
#define GET_BOOL(object, name) \
|
242
|
+
JSValueToBoolean(_context, GET_VALUE(object, name))
|
243
|
+
|
244
|
+
#define HAS_PROPERTY(object, property) _HAS_PROPERTY(_context, object, property)
|
245
|
+
bool _HAS_PROPERTY(JSContextRef _context, JSObjectRef object, const char *property) {
|
246
|
+
JSStringRef propertyName = JSStringCreateWithUTF8CString(property);
|
247
|
+
bool has = JSObjectHasProperty(_context, object, propertyName);
|
248
|
+
JSStringRelease(propertyName);
|
249
|
+
return has;
|
250
|
+
}
|
251
|
+
|
252
|
+
#define EXPORTS(name, object) SET_VALUE(Exports, name, object);
|
253
|
+
|
254
|
+
#define FUNC_HEADER(f) \
|
255
|
+
JSValueRef f( \
|
256
|
+
JSContextRef _ctx, \
|
257
|
+
JSObjectRef function, \
|
258
|
+
JSObjectRef _this, \
|
259
|
+
size_t _argc, \
|
260
|
+
const JSValueRef _argv[], \
|
261
|
+
JSValueRef *_exception) \
|
262
|
+
|
263
|
+
#define FUNCTION(f,...) \
|
264
|
+
FUNC_HEADER(f) \
|
265
|
+
{ \
|
266
|
+
TRACE(" *** F: %s\n", STRINGIZE(f)) \
|
267
|
+
JSContextRef _context = _ctx; \
|
268
|
+
int _argn = 0; \
|
269
|
+
JSStringRef _tmpStr; \
|
270
|
+
size_t _tmpSz; \
|
271
|
+
__VA_ARGS__; \
|
272
|
+
|
273
|
+
#define CONSTRUCTOR(f,...) \
|
274
|
+
JSObjectRef f( \
|
275
|
+
JSContextRef _context, \
|
276
|
+
JSObjectRef constructor, \
|
277
|
+
size_t _argc, \
|
278
|
+
const JSValueRef _argv[], \
|
279
|
+
JSValueRef *_exception) \
|
280
|
+
{ \
|
281
|
+
TRACE(" *** C: %s\n", STRINGIZE(f)) \
|
282
|
+
int _argn = 0; \
|
283
|
+
JSStringRef _tmpStr; \
|
284
|
+
size_t _tmpSz; \
|
285
|
+
__VA_ARGS__; \
|
286
|
+
|
287
|
+
#define END \
|
288
|
+
}
|
289
|
+
|
290
|
+
#define NARWHAL_MODULE(MODULE_NAME) \
|
291
|
+
JSObjectRef Require; \
|
292
|
+
JSObjectRef Exports; \
|
293
|
+
JSObjectRef Module; \
|
294
|
+
JSObjectRef System; \
|
295
|
+
JSObjectRef Print; \
|
296
|
+
JSContextRef _context; \
|
297
|
+
NarwhalContext narwhal_context; \
|
298
|
+
\
|
299
|
+
void print(const char * string)\
|
300
|
+
{\
|
301
|
+
JSValueRef argv[] = { JS_str_utf8(string, strlen(string)) }; \
|
302
|
+
JSObjectCallAsFunction(_context, Print, NULL, 1, argv, NULL); \
|
303
|
+
}\
|
304
|
+
JSObjectRef require(const char *id) {\
|
305
|
+
JSValueRef argv[] = { JS_str_utf8(id, strlen(id)) }; \
|
306
|
+
return JSValueToObject(_context, JSObjectCallAsFunction(_context, Require, NULL, 1, argv, NULL), NULL); \
|
307
|
+
}\
|
308
|
+
\
|
309
|
+
const char *moduleName = STRINGIZE(MODULE_NAME);\
|
310
|
+
extern "C" const char * narwhalModuleInit(NarwhalContext *_narwhal_context) { \
|
311
|
+
narwhal_context = *_narwhal_context; \
|
312
|
+
return moduleName; \
|
313
|
+
}\
|
314
|
+
\
|
315
|
+
extern "C" FUNC_HEADER(MODULE_NAME) \
|
316
|
+
{ _context = _ctx; \
|
317
|
+
ARG_COUNT(5); \
|
318
|
+
Require = JSValueToObject(_context, ARGV(0), _exception); \
|
319
|
+
if (*_exception) { return NULL; }; \
|
320
|
+
if (!JSObjectIsFunction(_context, Require)) { THROW("Argument 0 must be a function."); } \
|
321
|
+
Exports = JSValueToObject(_context, ARGV(1), _exception); \
|
322
|
+
if (*_exception) { return NULL; }; \
|
323
|
+
Module = JSValueToObject(_context, ARGV(2), _exception); \
|
324
|
+
if (*_exception) { return NULL; }; \
|
325
|
+
System = JSValueToObject(_context, ARGV(3), _exception); \
|
326
|
+
if (*_exception) { return NULL; }; \
|
327
|
+
Print = JSValueToObject(_context, ARGV(4), _exception); \
|
328
|
+
if (*_exception) { return NULL; }; \
|
329
|
+
if (!JSObjectIsFunction(_context, Print)) { THROW("Argument 4 must be a function."); } \
|
330
|
+
|
331
|
+
#define END_NARWHAL_MODULE \
|
332
|
+
return JS_undefined; }\
|
333
|
+
|
334
|
+
#define GET_INTERNAL(type, name, object) \
|
335
|
+
type name = (type) JSObjectGetPrivate(object)
|
336
|
+
#define SET_INTERNAL(object, data) \
|
337
|
+
JSObjectSetPrivate(object, (void *)data)
|
338
|
+
|
339
|
+
#define DESTRUCTOR(f) void f(JSObjectRef object) \
|
340
|
+
{ \
|
341
|
+
|
342
|
+
#define ARGS_ARRAY(name, ...) JSValueRef name[] = { __VA_ARGS__ };
|
343
|
+
|
344
|
+
#define CALL_AS_FUNCTION(object, thisObject, argc, argv) \
|
345
|
+
JSObjectCallAsFunction(_context, object, thisObject, argc, argv, _exception)
|
346
|
+
|
347
|
+
#define CALL_AS_CONSTRUCTOR(object, argc, argv) \
|
348
|
+
JSObjectCallAsConstructor(_context, object, argc, argv, _exception)
|
349
|
+
|
350
|
+
#define GET_UTF16(string, buffer, length) _GET_UTF16(_context, _exception, string, (JSChar**)buffer, length)
|
351
|
+
int _GET_UTF16(JSContextRef _context, JSValueRef *_exception, JSValueRef str, JSChar **buffer, size_t *length) {
|
352
|
+
JSStringRef string = JSValueToStringCopy(_context, str, _exception);
|
353
|
+
|
354
|
+
*length = (size_t)(JSStringGetLength(string) * sizeof(JSChar));
|
355
|
+
*buffer = (JSChar*)malloc(*length);
|
356
|
+
|
357
|
+
if (!*buffer)
|
358
|
+
return 0;
|
359
|
+
|
360
|
+
memcpy(*buffer, JSStringGetCharactersPtr(string), *length);
|
361
|
+
|
362
|
+
JSStringRelease(string);
|
363
|
+
|
364
|
+
return 1;
|
365
|
+
}
|
366
|
+
|
367
|
+
#define CALL(f, ...) f(_context, _exception, __VA_ARGS__)
|
368
|
+
|
369
|
+
#define JS_Print(value) CALL(JSValuePrint, value)
|
370
|
+
|
371
|
+
typedef struct __NarwhalContext NarwhalContext;
|
372
|
+
struct __NarwhalContext {
|
373
|
+
pthread_mutex_t *mutex;
|
374
|
+
JSContextRef context;
|
375
|
+
};
|
376
|
+
|
377
|
+
extern NarwhalContext narwhal_context;
|
378
|
+
|
379
|
+
#define LOCK() \
|
380
|
+
{DEBUG("locking %p (%lu)\n", narwhal_context.mutex, (unsigned long)pthread_self()) \
|
381
|
+
int ret = pthread_mutex_lock(narwhal_context.mutex); \
|
382
|
+
if (ret) fprintf(stderr, "pthread_mutex_lock error: %d", ret); \
|
383
|
+
else {DEBUG("locked %p (%lu)\n", narwhal_context.mutex, (unsigned long)pthread_self())} };
|
384
|
+
|
385
|
+
#define UNLOCK() \
|
386
|
+
{DEBUG("unlocking %p (%lu)\n", narwhal_context.mutex, (unsigned long)pthread_self()) \
|
387
|
+
int ret = pthread_mutex_unlock(narwhal_context.mutex); \
|
388
|
+
if (ret) fprintf(stderr, "pthread_mutex_unlock error: %d", ret); \
|
389
|
+
else {DEBUG("unlocked %p (%lu)\n", narwhal_context.mutex, (unsigned long)pthread_self())}};
|
390
|
+
|
391
|
+
JSClassRef Custom_class(JSContextRef _context)
|
392
|
+
{
|
393
|
+
static JSClassRef jsClass;
|
394
|
+
if (!jsClass)
|
395
|
+
{
|
396
|
+
JSClassDefinition definition = kJSClassDefinitionEmpty;
|
397
|
+
jsClass = JSClassCreate(&definition);
|
398
|
+
}
|
399
|
+
return jsClass;
|
400
|
+
}
|
401
|
+
|
402
|
+
extern JSObjectRef Require;
|
403
|
+
extern JSObjectRef Exports;
|
404
|
+
extern JSObjectRef Module;
|
405
|
+
extern JSObjectRef System;
|
406
|
+
extern JSObjectRef Print;
|
407
|
+
extern JSContextRef _context;
|
408
|
+
extern NarwhalContext narwhal_context;
|
409
|
+
extern void print(const char * string);
|
410
|
+
extern JSObjectRef require(const char *id);
|
411
|
+
|
412
|
+
JSObjectRef JSObjectMakeArray(JSContextRef _context, size_t argc, const JSValueRef argv[], JSValueRef* _exception);
|
413
|
+
JSObjectRef JSObjectMakeDate(JSContextRef _context, size_t argc, const JSValueRef argv[], JSValueRef* _exception);
|
414
|
+
JSObjectRef JSObjectMakeError(JSContextRef _context, size_t argc, const JSValueRef argv[], JSValueRef* _exception);
|
415
|
+
JSObjectRef JSObjectMakeRegExp(JSContextRef _context, size_t argc, const JSValueRef argv[], JSValueRef* _exception);
|
416
|
+
|
417
|
+
#define CLASS(NAME) JSObjectMakeConstructor(_context, NAME ## _class(_context), NAME ## _constructor)
|
418
|
+
|
419
|
+
#define PROTECT(value) _PROTECT(_context, value)
|
420
|
+
JSValueRef _PROTECT(JSContextRef _context, JSValueRef value) {
|
421
|
+
JSValueProtect(_context, value);
|
422
|
+
return value;
|
423
|
+
}
|
424
|
+
|
425
|
+
#define PROTECT_OBJECT(value) ((NWObject)PROTECT(value))
|
426
|
+
|
427
|
+
#endif
|