You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by be...@apache.org on 2014/01/12 13:40:15 UTC
[50/52] [partial] move src/* to the root
http://git-wip-us.apache.org/repos/asf/couchdb/blob/e62a4fc1/.gitignore
----------------------------------------------------------------------
diff --git a/.gitignore b/.gitignore
index fe5ded3..8153d2a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -17,152 +17,50 @@
INSTALL
Makefile.in
THANKS
-acinclude.m4
-aclocal.m4
-apache-couchdb-*
-autom4te.cache
-__pycache__
-bin/.deps/
-bin/couch-config
-bin/couch-config_dev
-bin/couchdb
-bin/couchdb.1
-bin/couchjs_dev
-bin/couchpw
-build-aux/config.*
-build-aux/depcomp
-build-aux/install-sh
-build-aux/ltmain.sh
-build-aux/missing
-build-aux/texinfo.tex
-config.h
-config.h.in
-config.log
-config.status
-configure
-couchdb.stderr
-couchdb.stdout
-cover/*
-erl_crash.dump
-etc/couchdb/default.ini
-etc/couchdb/default_dev.ini
-etc/couchdb/local_dev.ini
-etc/init/couchdb
-etc/launchd/org.apache.couchdb.plist
-etc/logrotate.d/couchdb
-libtool
-m4
-share/doc/build/CouchDB.log
-share/doc/build/doctrees
-share/doc/build/html
-share/doc/build/latex
-share/doc/build/texinfo
-share/server/main-coffee.js
-share/server/main.js
-src/couch_mrview/ebin/
-src/couch_plugins/ebin/
-src/couch_replicator/ebin/
-src/couchdb/.deps/*
-src/couchdb/.libs/*
-src/couchdb/couch.app
-src/couchdb/couchjs
-src/couchdb/edoc-info
-src/couchdb/erlang.png
-src/couchdb/priv/.deps/
-src/couchdb/priv/.libs/
-src/couchdb/priv/couch_icu_driver.la
-src/couchdb/priv/couchjs
-src/couchdb/priv/couchjs.1
-src/couchdb/priv/couchspawnkillable
-src/couchdb/priv/stat_descriptions.cfg
-src/couchdb/priv/stat_descriptions.cfg
-src/couchdb/stylesheet.css
-src/ejson/.deps/
-src/ejson/.libs/
-src/ejson/ejson.app
-src/ejson/priv
-src/erlang-oauth/oauth.app
-src/erlang-oauth/oauth.app
-src/fauxton/node_modules
-src/fauxton/dist
-src/fauxton/build
-src/fauxton/app/load_addons.js
-!src/fauxton/app/addons/
-src/fauxton/app/addons/*
-!src/fauxton/app/addons/activetasks
-!src/fauxton/app/addons/config
-!src/fauxton/app/addons/plugins
-!src/fauxton/app/addons/logs
-!src/fauxton/app/addons/stats
-!src/fauxton/app/addons/replication
-!src/fauxton/app/addons/contribute
-!src/fauxton/app/addons/auth
-!src/fauxton/app/addons/exampleAuth
-!src/fauxton/app/addons/permissions
-!src/fauxton/app/addons/verifyinstall
-!src/fauxton/app/addons/compaction
-src/fauxton/settings.json*
-!src/fauxton/settings.json.default
-src/ibrowse/ibrowse.app
-src/ibrowse/ibrowse.app
-src/mochiweb/mochiweb.app
-src/mochiweb/mochiweb.app
-!src/my-first-couchdb-plugin/.gitignore
-!src/my-first-couchdb-plugin/Makefile
-src/snappy/.deps/
-src/snappy/.libs/
-src/snappy/google-snappy/.deps/
-src/snappy/google-snappy/snappy-stubs-public.h
-src/snappy/google-snappy/stamp-h2
-src/snappy/priv
-src/snappy/snappy.app
-stamp-h1
-tmp
-utils/run
-src/fauxton/test/test.config.js
-src/fauxton/app/initialize.js
+__pycache__
+b
deps
rel/apache-couchdb
# rebar
-src/support/rebar/rebar
-src/support/rebar/inttest/rt.work
+support/rebar/rebar
+support/rebar/inttest/rt.work
rt.work
rebar
support/rebar/.test
# couch_collate
-src/apps/couch_collate/ebin
-src/apps/couch_collate/priv
-src/apps/couch_collate/c_src/*.o
-src/apps/couch_collate/.libs
-src/apps/couch_collate/.dists
+apps/couch_collate/ebin
+apps/couch_collate/priv
+apps/couch_collate/c_src/*.o
+apps/couch_collate/.libs
+apps/couch_collate/.dists
-src/apps/couch/ebin
-src/apps/couch_replicator/ebin
-src/apps/couch_mrview/ebin
-src/apps/couch_index/ebin
-src/apps/couch_dbupdates/ebin
-src/apps/couch_plugins/ebin
-src/apps/couch_rel/ebin/*.beam
+apps/couch/ebin
+apps/couch_replicator/ebin
+apps/couch_mrview/ebin
+apps/couch_index/ebin
+apps/couch_dbupdates/ebin
+apps/couch_plugins/ebin
+apps/couch_rel/ebin/*.beam
# couchjs
-src/apps/couch/c_src/couch_js/*.o
-src/apps/couch/c_src/couch_js/config.h
-src/apps/couch/priv/couchjs
-src/apps/couch/c_src/spawnkillable/*.o
-src/apps/couch/priv/couchspawnkillable
-src/apps/couch/.libs
-src/apps/couch/.dists
+apps/couch/c_src/couch_js/*.o
+apps/couch/c_src/couch_js/config.h
+apps/couch/priv/couchjs
+apps/couch/c_src/spawnkillable/*.o
+apps/couch/priv/couchspawnkillable
+apps/couch/.libs
+apps/couch/.dists
-src/share/server/main-coffee.js
-src/share/server/main.js
+share/server/main-coffee.js
+share/server/main.js
-src/test/etap/test_cfg_register
-src/test/out
-src/test/etap/*.beam
-src/test/etap/*.o
-src/test/etap/temp.*
+test/etap/test_cfg_register
+test/out
+test/etap/*.beam
+test/etap/*.o
+test/etap/temp.*
-src/share/doc/build
+share/doc/build
http://git-wip-us.apache.org/repos/asf/couchdb/blob/e62a4fc1/Makefile
----------------------------------------------------------------------
diff --git a/Makefile b/Makefile
index fe687b8..0169cc9 100644
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,5 @@
BASE_DIR = $(shell pwd)
-SUPPORT_DIR=$(BASE_DIR)/src/support
+SUPPORT_DIR=$(BASE_DIR)/support
ERLC ?= $(shell which erlc)
ESCRIPT ?= $(shell which escript)
OVERLAY_VARS ?=
@@ -53,11 +53,11 @@ check: test testjs
#
rebar:
- @(test ! -e $(BASE_DIR)/src/support/rebar/rebar && \
+ @(test ! -e $(BASE_DIR)/support/rebar/rebar && \
echo "==> build rebar" && \
- cd $(BASE_DIR)/src/support/rebar && \
+ cd $(BASE_DIR)/support/rebar && \
$(ESCRIPT) bootstrap || true)
- @cp $(BASE_DIR)/src/support/rebar/rebar $(BASE_DIR)/rebar
+ @cp $(BASE_DIR)/support/rebar/rebar $(BASE_DIR)/rebar
rebarclean:
@(cd $(BASE_DIR)/support/rebar/rebar && \
@@ -67,8 +67,8 @@ rebarclean:
# DOCS
#
-DOC_SRCDIR=$(BASE_DIR)/src/share/doc/src
-DOC_BUILDDIR=$(BASE_DIR)/src/share/doc/build
+DOC_SRCDIR=$(BASE_DIR)/share/doc/src
+DOC_BUILDDIR=$(BASE_DIR)/share/doc/build
DOC_RELDIR=$(RELDIR)/share/doc
SPHINXOPTS = -n -c $(DOC_SRCDIR) \
-A local=1 \
@@ -111,24 +111,24 @@ reldocclean:
#
# TESTS
#
-COUCHDB_ETAP_DIR=$(BASE_DIR)/src/test/etap
+COUCHDB_ETAP_DIR=$(BASE_DIR)/test/etap
export COUCHDB_ETAP_DIR
-ERL_LIBS=$(BASE_DIR)/src/deps:$(BASE_DIR)/src/apps:$(BASE_DIR)/src/test/etap
+ERL_LIBS=$(BASE_DIR)/deps:$(BASE_DIR)/apps:$(BASE_DIR)/test/etap
export ERL_LIBS
test: testbuild
prove $(COUCHDB_ETAP_DIR)/*.t
- prove $(BASE_DIR)/src/apps/couch_mrview/test/*.t
- prove $(BASE_DIR)/src/apps/couch_replicator/test/*.t
+ prove $(BASE_DIR)/apps/couch_mrview/test/*.t
+ prove $(BASE_DIR)/apps/couch_replicator/test/*.t
verbose-test: testbuild
prove -v $(COUCHDB_ETAP_DIR)/*.t
- prove -v $(BASE_DIR)/src/apps/couch_mrview/test/*.t
- prove -v $(BASE_DIR)/src/apps/couch_replicator/test/*.t
+ prove -v $(BASE_DIR)/apps/couch_mrview/test/*.t
+ prove -v $(BASE_DIR)/apps/couch_replicator/test/*.t
testjs: testbuild
- $(ESCRIPT) $(BASE_DIR)/src/test/javascript/test_js.escript
+ $(ESCRIPT) $(BASE_DIR)/test/javascript/test_js.escript
testbuild: testclean
$(ERLC) -v -o $(COUCHDB_ETAP_DIR) $(COUCHDB_ETAP_DIR)/etap.erl
@@ -137,18 +137,18 @@ testbuild: testclean
$(ERLC) -v -o $(COUCHDB_ETAP_DIR) $(COUCHDB_ETAP_DIR)/mustache.erl
cc -DBSD_SOURCE $(COUCHDB_ETAP_DIR)/test_cfg_register.c \
-o $(COUCHDB_ETAP_DIR)/test_cfg_register
- mkdir -p $(BASE_DIR)/src/test/out/data
- mkdir -p $(BASE_DIR)/src/test/out/bin
- mkdir -p $(BASE_DIR)/src/test/out/share
- mkdir -p $(BASE_DIR)/src/test/out/log
- cp $(BASE_DIR)/src/apps/couch/priv/couchjs $(BASE_DIR)/src/test/out/bin/
- cp -r $(BASE_DIR)/src/share/server $(BASE_DIR)/src/test/out/share
- cp -r $(BASE_DIR)/src/share/www $(BASE_DIR)/src/test/out/share
- cp $(BASE_DIR)/src/etc/couchdb/local.ini $(BASE_DIR)/src/test/out/
+ mkdir -p $(BASE_DIR)/test/out/data
+ mkdir -p $(BASE_DIR)/test/out/bin
+ mkdir -p $(BASE_DIR)/test/out/share
+ mkdir -p $(BASE_DIR)/test/out/log
+ cp $(BASE_DIR)/apps/couch/priv/couchjs $(BASE_DIR)/test/out/bin/
+ cp -r $(BASE_DIR)/share/server $(BASE_DIR)/test/out/share
+ cp -r $(BASE_DIR)/share/www $(BASE_DIR)/test/out/share
+ cp $(BASE_DIR)/etc/couchdb/local.ini $(BASE_DIR)/test/out/
testclean:
@rm -rf $(COUCHDB_ETAP_DIR)/*.beam
- @rm -rf $(BASE_DIR)/src/test/out
+ @rm -rf $(BASE_DIR)/test/out
@rm -rf $(COUCHDB_ETAP_DIR)/test_cfg_register
@rm -rf $(COUCHDB_ETAP_DIR)/*.o
http://git-wip-us.apache.org/repos/asf/couchdb/blob/e62a4fc1/addons/couchjs-node/README.md
----------------------------------------------------------------------
diff --git a/addons/couchjs-node/README.md b/addons/couchjs-node/README.md
new file mode 100644
index 0000000..551c3df
--- /dev/null
+++ b/addons/couchjs-node/README.md
@@ -0,0 +1,32 @@
+# CouchJS
+
+## Drop-in replacement JavaScript engine for Apache CouchDB
+
+CouchJS is a command-line Node.js program. It is 100% compatible with Apache CouchDB's built-in JavaScript system.
+
+By using CouchJS, you will get 100% CouchDB compatibility (the test suite completely passes) but your JavaScript environment is V8, or Node.js.
+
+See share/doc/src/experimental.rst for installation instructions.
+
+## Idea
+
+JavaScript is decoupled from the CouchDB core. To do JavaScript stuff, CouchDB runs a normal Unix subprocess, `couchjs`. This subprocess is just a read-eval-print loop on standard i/o. CouchDB passes `couchjs` a file name, and *that file* contains the view server implementation.
+
+This tool duplicates the "REPL" look and feel of `couchjs` and supports the exact same view server implementation.
+
+## Security
+
+I have no idea. I would not trust it for production use.
+
+## Log
+
+If you create a file, `/tmp/couchjs.log` then *couchjs* will output debugging messages there.
+
+## License
+
+Apache 2.0
+
+See the [Apache 2.0 license](named/blob/master/LICENSE).
+
+[tap]: https://github.com/isaacs/node-tap
+[def]: https://github.com/iriscouch/defaultable
http://git-wip-us.apache.org/repos/asf/couchdb/blob/e62a4fc1/addons/couchjs-node/cli.js
----------------------------------------------------------------------
diff --git a/addons/couchjs-node/cli.js b/addons/couchjs-node/cli.js
new file mode 100755
index 0000000..5447dd5
--- /dev/null
+++ b/addons/couchjs-node/cli.js
@@ -0,0 +1,89 @@
+#!/usr/bin/env node
+
+// Licensed under the Apache License, Version 2.0 (the "License"); you may not
+// use this file except in compliance with the License. You may obtain a copy of
+// the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
+
+var fs = require('fs');
+var Fiber = require('fibers');
+var optimist = require('optimist');
+
+var couchjs = require('./couchjs');
+var package_json = require('./package.json');
+var LineStream = require('./stream');
+var inspector = require('./inspector');
+var log = require('./console').log;
+
+
+var opts = optimist.boolean(['h', 'V', 'H'])
+ .describe({ 'h': 'display a short help message and exit',
+ 'V': 'display version information and exit',
+ 'H': 'enable couchjs cURL bindings (not implemented)'
+ })
+ .usage('$0 <path to main.js>');
+
+
+function main() {
+
+ 'use strict';
+
+ var main_js = opts.argv._[0];
+
+ if (!main_js) {
+ return console.error(opts.help());
+ }
+
+ log('couchjs/%s %s: %s', package_json.version, process.pid, main_js);
+
+ if (process.env.COUCHJS_DEBUG_PORT) {
+ inspector(+process.env.COUCHJS_DEBUG_PORT);
+ }
+
+ fs.readFile(main_js, 'utf8', function(er, body) {
+ if (er) {
+ throw er;
+ }
+
+ var stdin = new LineStream.v2();
+
+ stdin.on('readable', function() {
+ var buf = stdin.read();
+
+ if (buf) {
+ couchjs.stdin(buf);
+ }
+ });
+
+ stdin.on('end', function() {
+ log('Terminate; connection to parent closed');
+ process.exit(0);
+ });
+
+ process.stdin.setEncoding('utf8');
+ process.stdin.pipe(stdin);
+
+ var main_func = Function(['print', 'readline', 'evalcx', 'gc', 'quit'], body);
+
+ log('Call main');
+
+ new Fiber(function() {
+ main_func(couchjs.print, couchjs.readline, couchjs.evalcx, couchjs.gc);
+ }).run();
+ });
+
+ process.on('uncaughtException', function(er) {
+ log('Error:\n%s', er.stack);
+ });
+}
+
+if (require.main === module) {
+ main();
+}
http://git-wip-us.apache.org/repos/asf/couchdb/blob/e62a4fc1/addons/couchjs-node/console.js
----------------------------------------------------------------------
diff --git a/addons/couchjs-node/console.js b/addons/couchjs-node/console.js
new file mode 100644
index 0000000..18e7241
--- /dev/null
+++ b/addons/couchjs-node/console.js
@@ -0,0 +1,66 @@
+// Licensed under the Apache License, Version 2.0 (the "License"); you may not
+// use this file except in compliance with the License. You may obtain a copy of
+// the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
+
+var fs = require('fs');
+var util = require('util');
+
+var noop = function() {};
+
+module.exports = {};
+module.exports.log = noop;
+module.exports.debug = noop;
+module.exports.info = noop;
+module.exports.warn = noop;
+module.exports.error = noop;
+
+var LOG_PATH = '/tmp/couchjs.log';
+var stat = null;
+var LOG = null;
+
+try {
+ stat = fs.statSync(LOG_PATH);
+} catch(er) {}
+
+if (stat) {
+ LOG = fs.createWriteStream(LOG_PATH, {
+ 'flags':'a'
+ });
+
+ var log = function () {
+ var str = util.format.apply(this, arguments);
+ LOG.write(str + '\n');
+ };
+
+ var on_err = function (er) {
+ module.exports.error('Uncaught error:\n%s', er.stack || er.message || JSON.stringify(er));
+
+ if (er.stack) {
+ er = ['fatal', 'unknown_error', er.stack];
+ }
+
+ process.stdout.write(JSON.stringify(er) + '\n');
+ process.exit(1);
+ };
+
+ module.exports.log = log;
+ module.exports.debug = log;
+ module.exports.info = log;
+ module.exports.warn = log;
+ module.exports.error = log;
+
+ process.on('exit', function() {
+ module.exports.log('Exit %d', process.pid);
+ });
+
+ process.on('uncaughtException', on_err);
+}
+
http://git-wip-us.apache.org/repos/asf/couchdb/blob/e62a4fc1/addons/couchjs-node/couchdb.js
----------------------------------------------------------------------
diff --git a/addons/couchjs-node/couchdb.js b/addons/couchjs-node/couchdb.js
new file mode 100755
index 0000000..d420540
--- /dev/null
+++ b/addons/couchjs-node/couchdb.js
@@ -0,0 +1,29 @@
+// Licensed under the Apache License, Version 2.0 (the "License"); you may not
+// use this file except in compliance with the License. You may obtain a copy of
+// the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
+
+
+var http = require('http');
+
+function handler(req, res) {
+ res.writeHead(200);
+ res.end('Hello: ' + req.url + '\n');
+}
+
+if (require.main === module) {
+ var http = require('http');
+ var server = http.createServer(handler);
+
+ server.listen(3000);
+ console.log('Listening on :3000');
+}
+
+module.exports = handler;
http://git-wip-us.apache.org/repos/asf/couchdb/blob/e62a4fc1/addons/couchjs-node/couchjs.js
----------------------------------------------------------------------
diff --git a/addons/couchjs-node/couchjs.js b/addons/couchjs-node/couchjs.js
new file mode 100644
index 0000000..ccc1aa9
--- /dev/null
+++ b/addons/couchjs-node/couchjs.js
@@ -0,0 +1,133 @@
+// Licensed under the Apache License, Version 2.0 (the "License"); you may not
+// use this file except in compliance with the License. You may obtain a copy of
+// the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
+
+
+
+var vm = require('vm');
+var Fiber = require('fibers');
+var util = require('util');
+
+var XML = require('./xml');
+var log = require('./console').log;
+
+var INPUT = {
+ 'queue':[],
+ 'waiting':null
+};
+
+
+function print(line) {
+ log('STDOUT %s: %s', process.pid, line);
+ process.stdout.write(line + '\n');
+
+ try {
+ line = JSON.parse(line);
+ } catch(er) {
+ return;
+ }
+
+ if (line[0] === 'log') {
+ log('LOG: %s', line[1]);
+ }
+}
+
+function stdin (line) {
+ log('STDIN %s: %s', process.pid, line.trim());
+
+ if (INPUT.waiting) {
+ INPUT.waiting.run(line);
+ } else {
+ INPUT.queue.push(line);
+ }
+}
+
+function readline () {
+ var line = INPUT.queue.shift();
+
+ if (line) {
+ return line;
+ }
+
+ INPUT.waiting = Fiber.current;
+ line = Fiber.yield();
+ INPUT.waiting = null;
+
+ return line;
+}
+
+
+function evalcx (source, sandbox) {
+ sandbox = sandbox || {};
+ var func;
+ //log('evalcx in %j: %j', Object.keys(sandbox), source)
+
+ if (source === '') {
+ return sandbox;
+ }
+
+ // source might be "function(doc) { emit(doc._id, 1) }"
+ source = source.replace(/;+$/, '');
+
+ sandbox.XML = sandbox.XML || XML;
+ source = '(' + source + ')';
+
+ try {
+ var id = Math.floor(Math.random() * 1000*1000);
+ var filename = '_couchdb:' + id + '.js';
+ var script = vm.createScript(source, filename);
+ func = script.runInNewContext(sandbox);
+ } catch (er) {
+ log('Error making code: %s', er.stack);
+ return sandbox;
+ }
+
+ return func;
+}
+
+function quit(code) {
+ code = code || 1;
+ if (code < 0) {
+ code = -code;
+ }
+
+ process.exit(code);
+}
+
+function gc() { }
+
+
+function toSource() {
+ if (typeof this === 'function') {
+ return '' + this;
+ }
+
+ if (this instanceof Error) {
+ return this.stack;
+ }
+
+ return util.inspect(this);
+}
+
+Error.prototype.toSource = Error.prototype.toSource || toSource;
+Error.prototype.toString = Error.prototype.toString || toSource;
+Function.prototype.toSource = Function.prototype.toSource || toSource;
+Function.prototype.toString = Function.prototype.toString || toSource;
+
+module.exports = {
+ 'print': print,
+ 'readline': readline,
+ 'stdin': stdin,
+ 'evalcx': evalcx,
+ 'quit': quit,
+ 'gc': gc
+};
+
http://git-wip-us.apache.org/repos/asf/couchdb/blob/e62a4fc1/addons/couchjs-node/inspector.js
----------------------------------------------------------------------
diff --git a/addons/couchjs-node/inspector.js b/addons/couchjs-node/inspector.js
new file mode 100755
index 0000000..48182e5
--- /dev/null
+++ b/addons/couchjs-node/inspector.js
@@ -0,0 +1,101 @@
+#!/usr/bin/env node
+
+// Licensed under the Apache License, Version 2.0 (the "License"); you may not
+// use this file except in compliance with the License. You may obtain a copy of
+// the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
+
+
+
+var cp = require('child_process');
+var log = require('./console').log;
+
+function watchInspector(child) {
+
+ child.stderr.on('data', function(body) {
+ log('Inspector STDERR: %s', body);
+ });
+
+ child.stdout.on('data', function(body) {
+ log('Inspector STDOUT: %s', body);
+ });
+
+ child.on('exit', function(code, signal) {
+ log('Inspector exited %d signal=%j', code, signal);
+ process.exit(code);
+ });
+
+ process.on('exit', function() {
+ log('Kill inspector upon exit: %d', child.pid);
+ process.kill(child.pid, 'SIGTERM');
+ });
+
+}
+
+function start (debugPort) {
+
+ if (!debugPort || typeof debugPort !== 'number') {
+ throw new Error('Need a listen debugPort');
+ }
+
+ var webPort = debugPort + 1;
+ var cmd = __filename;
+ var args = [debugPort, webPort];
+ var opts = {
+ 'cwd': __dirname,
+ 'stdio': 'pipe',
+ 'detached': false
+ };
+
+ log('Start inspector: %s %j %j', cmd, args, opts);
+
+ var inspector = cp.spawn(cmd, args, opts);
+
+ watchInspector(inspector);
+
+ log('Enable remote debug pid=%d port=%d', process.pid, debugPort);
+
+ process.debugPort = debugPort;
+ process.kill(process.pid, 'SIGUSR1');
+}
+
+function main() {
+ var debugPort = +process.argv[2];
+ var webPort = +process.argv[3];
+
+ if (!debugPort || !webPort) {
+ throw new Error('Bad arguments: need debugPort and webPort');
+ }
+
+ console.log('Start inspector debugPort=%j webPort=%j', debugPort, webPort);
+
+ var DebugServer = require('node-inspector/lib/debug-server');
+ var server = new DebugServer();
+
+ server.on('close', function() {
+ console.log('Server closed');
+ process.exit(0);
+ });
+
+ server.start({
+ 'webPort':webPort,
+ 'debugPort':debugPort
+ });
+
+ process.on('uncaughtException', function(er) {
+ console.log('Error:\n%s', er.stack);
+ });
+}
+
+module.exports = start;
+
+if (require.main === module) {
+ main();
+}
http://git-wip-us.apache.org/repos/asf/couchdb/blob/e62a4fc1/addons/couchjs-node/package.json
----------------------------------------------------------------------
diff --git a/addons/couchjs-node/package.json b/addons/couchjs-node/package.json
new file mode 100644
index 0000000..445f3b9
--- /dev/null
+++ b/addons/couchjs-node/package.json
@@ -0,0 +1,40 @@
+{
+ "name": "couchjs",
+ "description": "Drop-in replacement for CouchDB JavaScript view server",
+ "keywords": [ "couchdb", "couchjs" ],
+ "version": "0.3.2",
+ "author": "Jason Smith <jh...@iriscouch.com> (http://www.iriscouch.com)",
+ "repository": {
+ "type":"git",
+ "url":"https://github.com/iriscouch/couchjs"
+ },
+ "engines": {
+ "node": ">= 0.8"
+ },
+ "main": "./couchjs.js",
+ "bin": {
+ "couchjs-node":"./cli.js"
+ },
+ "couchdb": {
+ "main":"./couchdb.js",
+ "vhosts": [
+ "127.0.0.1.xip.io"
+ ]
+ },
+ "bundledDependencies": [
+ "node-inspector"
+ ],
+ "dependencies": {
+ "optimist": "~0.3.4",
+ "async" : "~0.2.5",
+ "mkdirp" : "~0.3.4",
+ "fibers" : "~1.0.0",
+ "request" : "~2.9.203",
+ "pushover": "~1.2.1",
+ "defaultable": "~0.7.2",
+ "node-inspector": "git://github.com/iriscouch/node-inspector#couchjs"
+ },
+ "devDependencies": {
+ "tap": "~0.2.5"
+ }
+}
http://git-wip-us.apache.org/repos/asf/couchdb/blob/e62a4fc1/addons/couchjs-node/sandbox.js
----------------------------------------------------------------------
diff --git a/addons/couchjs-node/sandbox.js b/addons/couchjs-node/sandbox.js
new file mode 100644
index 0000000..5c18ef4
--- /dev/null
+++ b/addons/couchjs-node/sandbox.js
@@ -0,0 +1,47 @@
+// from https://github.com/KlausTrainer/sandbox.js
+exports.runInSandbox = function(src, ctx, whitelist) {
+ var vm = require('vm');
+ var sandbox;
+
+ if (ctx && ctx.require) {
+
+ whitelist = whitelist || [];
+
+ var insecureRequire = ctx.require;
+ var module = require('module');
+ var oldModulePrototype = module.prototype;
+
+ var secureRequire = function(moduleName) {
+
+ if (whitelist.indexOf(moduleName) === -1) {
+ module.prototype = oldModulePrototype;
+
+ throw new Error("'" + moduleName + "' is not whitelisted");
+
+ } else {
+ var requiredModule = insecureRequire(moduleName);
+
+ module.prototype = oldModulePrototype;
+
+ return requiredModule;
+ }
+ };
+
+ module.prototype = {
+ require: secureRequire,
+ load: module.prototype.load,
+ _compile: module.prototype._compile
+ };
+
+ module._cache = {};
+
+ ctx.require = secureRequire;
+ sandbox = Object.freeze(vm.createContext(ctx));
+ ctx.require = insecureRequire;
+
+ } else {
+ sandbox = Object.freeze(vm.createContext(ctx || {}));
+ }
+
+ return vm.createScript('(function() {"use strict"; return (' + src + ')()}())').runInContext(sandbox);
+};
http://git-wip-us.apache.org/repos/asf/couchdb/blob/e62a4fc1/addons/couchjs-node/stream.js
----------------------------------------------------------------------
diff --git a/addons/couchjs-node/stream.js b/addons/couchjs-node/stream.js
new file mode 100644
index 0000000..ebffc30
--- /dev/null
+++ b/addons/couchjs-node/stream.js
@@ -0,0 +1,115 @@
+// Licensed under the Apache License, Version 2.0 (the "License"); you may not
+// use this file except in compliance with the License. You may obtain a copy of
+// the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
+
+// Text line stream
+
+var stream = require('stream');
+var util = require('util');
+
+
+function LineStream() {
+
+ var self = this;
+ stream.call(self);
+
+ self.readable = true;
+ self.writable = true;
+
+ self.buffer = '';
+ self.downstream = null;
+
+ self.on('pipe', function(upstream) {
+ upstream.on('end', function(data, encoding) {
+ self.emit('end', data, encoding);
+ });
+ });
+}
+
+function LineStream2() {
+
+ if (!(this instanceof LineStream2)) {
+ return new LineStream2();
+ }
+
+ stream.Transform.call(this);
+ this.setEncoding('utf8');
+}
+
+util.inherits(LineStream2, stream.Transform);
+
+LineStream2.prototype._transform = function(message, encoding, done) {
+ var self = this;
+
+ message = message.toString(encoding);
+ var lines = message.split(/\n/);
+
+ // If the data ends in "\n" this will be ""; otherwise the final partial line.
+ var remainder = lines.pop();
+ if (remainder) {
+ this.unshift(remainder);
+ }
+
+ lines.forEach(function(line) {
+ self.push(line);
+ });
+
+ done();
+};
+
+util.inherits(LineStream, stream);
+
+
+
+LineStream.prototype.write = function(data) {
+ var self = this;
+
+ data = data || '';
+ if (typeof data !== 'string') {
+ return self.error(new Error('Data was not a string: ' + util.inspect(data)));
+ }
+
+ self.buffer += data;
+ var lines = self.buffer.split(/\n/);
+ self.buffer = lines.pop(); // If the data ended in "\n" this will be ""; otherwise the final partial line.
+
+ lines.forEach(function(line) {
+ self.emit('data', line);
+ });
+};
+
+
+LineStream.prototype.end = function(data) {
+ var self = this;
+
+ self.is_ending = true;
+ self.writable = false;
+
+ // Always call write, even with no data, so it can fire the "end" event.
+ self.write(data);
+};
+
+
+LineStream.prototype.error = function(er) {
+ var self = this;
+
+ self.readable = false;
+ self.writable = false;
+ self.emit('error', er);
+
+ // The write() method sometimes returns this value, so if there was an error, make write() return false.
+ return false;
+};
+
+
+module.exports = LineStream;
+module.exports.v2 = LineStream2;
+
http://git-wip-us.apache.org/repos/asf/couchdb/blob/e62a4fc1/addons/couchjs-node/test/experiment.js
----------------------------------------------------------------------
diff --git a/addons/couchjs-node/test/experiment.js b/addons/couchjs-node/test/experiment.js
new file mode 100644
index 0000000..9197ec6
--- /dev/null
+++ b/addons/couchjs-node/test/experiment.js
@@ -0,0 +1,125 @@
+// Licensed under the Apache License, Version 2.0 (the "License"); you may not
+// use this file except in compliance with the License. You may obtain a copy of
+// the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
+
+var vm = require('vm');
+var util = require('util');
+
+var STATE = 'wait';
+
+function main() {
+ process.debugPort = 5859;
+ process.kill(process.pid, 'SIGUSR1');
+
+ setTimeout(function() {
+ stuff(0);
+ }, 1000);
+}
+
+function stuff(count) {
+
+ console.log('Doing stuff: %d', count);
+ STATE = 'vm';
+ console.log('More stuff: %d', count);
+
+ if (STATE === 'done') {
+ console.log('Done');
+ } else if (STATE === 'code') {
+ setTimeout(code, 1000);
+ } else if(STATE === 'eval') {
+ test_eval();
+ } else if(STATE === 'vm') {
+ test_vm();
+ } else if(STATE === 'wait') {
+ setTimeout(function() {
+ stuff(count+1);
+ }, 1000);
+ } else {
+ throw new Error('Unknown state: ' + STATE);
+ }
+}
+
+function code() {
+ var code = [
+ 'var foo = "in the code"',
+ 'console.log("This is some code")',
+ 'debugger',
+ 'console.log("foo = " + foo)'
+ ].join('\n');
+
+ var runner = Function([], code);
+
+ console.log('Run runner in 1s');
+
+ setTimeout(run_runner, 1000)
+
+ function run_runner() {
+ console.log('About to run runner');
+ debugger;
+ runner();
+ console.log('Runner done');
+ }
+}
+
+function test_eval() {
+ console.log('Test eval in 1s');
+ setTimeout(run_eval, 1000);
+
+ var code = [
+ 'var foo = "in eval"',
+ 'console.log("This is eval")',
+ 'debugger',
+ 'console.log("foo = " + foo)'
+ ].join('\n');
+
+ function run_eval() {
+ console.log('Run eval now');
+ debugger;
+ eval(code);
+ }
+}
+
+function test_vm() {
+ console.log('Test vm');
+
+ var code = [
+ 'var i = 10',
+ 'setTimeout(hello, 1000)',
+ '',
+ 'function hello() {',
+ ' debugger',
+ ' console.log("Hello: " + i)',
+ ' if(--i)',
+ ' setTimeout(hello, 1000)',
+ '}'
+ ].join('\n');
+
+ console.log('Run vm now');
+ var filename = '_couchdb:code.js';
+
+ var sandbox = {};
+ var ok = ['console', 'setTimeout'];
+
+ ok.forEach(function(key) {
+ sandbox[key] = global[key];
+ });
+
+ var ctx = vm.createContext(sandbox);
+ var script = vm.createScript(code, filename);
+
+ var r = script.runInNewContext(sandbox);
+ console.log('Result:\n%s', util.inspect(r, false, 10));
+ return r;
+}
+
+if (require.main === module) {
+ main();
+}
http://git-wip-us.apache.org/repos/asf/couchdb/blob/e62a4fc1/addons/couchjs-node/xml.js
----------------------------------------------------------------------
diff --git a/addons/couchjs-node/xml.js b/addons/couchjs-node/xml.js
new file mode 100644
index 0000000..b64d221
--- /dev/null
+++ b/addons/couchjs-node/xml.js
@@ -0,0 +1,22 @@
+// Licensed under the Apache License, Version 2.0 (the "License"); you may not
+// use this file except in compliance with the License. You may obtain a copy of
+// the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
+
+
+function XML () {
+ this.foo = 'bar';
+}
+
+XML.prototype.toXMLString = function() {
+ return '<xml>\n <title>test</title>\n</xml>';
+};
+
+module.exports = XML;
http://git-wip-us.apache.org/repos/asf/couchdb/blob/e62a4fc1/apps/couch/build_spidermonkey.sh
----------------------------------------------------------------------
diff --git a/apps/couch/build_spidermonkey.sh b/apps/couch/build_spidermonkey.sh
new file mode 100755
index 0000000..95a15dd
--- /dev/null
+++ b/apps/couch/build_spidermonkey.sh
@@ -0,0 +1,251 @@
+#!/bin/sh
+
+CORE_TOP=`pwd`
+export CORE_TOP
+
+CURLBIN=`which curl`
+if ! test -n "CURLBIN"; then
+ display_error "Error: curl is required. Add it to 'PATH'"
+ exit 1
+fi
+
+GUNZIP=`which gunzip`
+UNZIP=`which unzip`
+TAR=`which tar`
+GNUMAKE=`which gmake 2>/dev/null || which make`
+PATCHES=$CORE_TOP/patches
+STATICLIBS=$CORE_TOP/.libs
+DISTDIR=$CORE_TOP/.dists
+
+# nspr sources
+NSPR_VER=4.8.8
+NSPR_DISTNAME=nspr-$NSPR_VER.tar.gz
+NSPR_SITE=http://dl.refuge.io
+
+# spidermonkey js sources
+JS_VER=185-1.0.0
+JS_REALVER=1.8.5
+JS_DISTNAME=js$JS_VER.tar.gz
+JS_SITE=http://dl.refuge.io
+JSDIR=$STATICLIBS/js-$JS_REALVER
+JS_LIBDIR=$STATICLIBS/js/lib
+JS_INCDIR=$STATICLIBS/js/include
+
+
+[ "$MACHINE" ] || MACHINE=`(uname -m) 2>/dev/null` || MACHINE="unknown"
+[ "$RELEASE" ] || RELEASE=`(uname -r) 2>/dev/null` || RELEASE="unknown"
+[ "$SYSTEM" ] || SYSTEM=`(uname -s) 2>/dev/null` || SYSTEM="unknown"
+[ "$BUILD" ] || VERSION=`(uname -v) 2>/dev/null` || VERSION="unknown"
+
+
+CFLAGS="-g -O2 -Wall"
+LDFLAGS="-lstdc++"
+ARCH=
+ISA64=
+GNUMAKE=make
+CC=gcc
+CXX=g++
+PATCH=patch
+case "$SYSTEM" in
+ Linux)
+ ARCH=`arch 2>/dev/null`
+ ;;
+ FreeBSD|OpenBSD|NetBSD)
+ ARCH=`(uname -p) 2>/dev/null`
+ GNUMAKE=gmake
+ ;;
+ Darwin)
+ ARCH=`(uname -p) 2>/dev/null`
+ ISA64=`(sysctl -n hw.optional.x86_64) 2>/dev/null`
+ ;;
+ Solaris)
+ ARCH=`(uname -p) 2>/dev/null`
+ GNUMAKE=gmake
+ PATCH=gpatch
+ ;;
+ *)
+ ARCH="unknown"
+ ;;
+esac
+
+
+# TODO: add mirror & signature validation support
+fetch()
+{
+ TARGET=$DISTDIR/$1
+ if ! test -f $TARGET; then
+ echo "==> Fetch $1 to $TARGET"
+ $CURLBIN --progress-bar -L $2/$1 -o $TARGET
+ fi
+}
+
+build_nspr()
+{
+ NSPR_CONFIGURE_ENV=""
+ case "$SYSTEM" in
+ Linux)
+ ARCH=`arch 2>/dev/null`
+ if [ "$ARCH" = "x86_64" ]; then
+ NSPR_CONFIGURE_ENV="--enable-64bit"
+ fi
+ CFLAGS="$CFLAGS -lpthread"
+ ;;
+ FreeBSD|OpenBSD|NetBSD)
+ ARCH=`(uname -p) 2>/dev/null`
+ if [ "$ARCH" = "x86_64" ] || [ "$ARCH" = "amd64" ]; then
+ NSPR_CONFIGURE_ENV+=--enable-64bit
+ fi
+ ;;
+ Darwin)
+ if [ "$ISA64" = "1" ]; then
+ NSPR_CONFIGURE_ENV+=--enable-64bit
+ fi
+ ;;
+ esac
+
+ fetch $NSPR_DISTNAME $NSPR_SITE
+
+ # clean the build
+ rm -rf $STATICLIBS/nspr*
+
+ echo "==> build nspr"
+ cd $STATICLIBS
+ $GUNZIP -c $DISTDIR/$NSPR_DISTNAME | $TAR xf -
+
+ cd $STATICLIBS/nspr-$NSPR_VER/mozilla/nsprpub
+ ./configure --disable-debug --enable-optimize \
+ --prefix=$STATICLIBS/nsprpub $NSPR_CONFIGURE_ENV
+
+ $GNUMAKE all
+ $GNUMAKE install
+}
+
+build_js()
+{
+
+ fetch $JS_DISTNAME $JS_SITE
+
+ # clean the build
+ rm -rf $STATICLIBS/js*
+
+ mkdir -p $JS_LIBDIR
+ mkdir -p $JS_INCDIR
+
+ cd $STATICLIBS
+ $GUNZIP -c $DISTDIR/$JS_DISTNAME | $TAR -xf -
+
+ echo "==> build js"
+ cd $JSDIR/js/src
+ $PATCH -p0 -i $PATCHES/js/patch-jsprf_cpp || echo "skipping patch"
+ $PATCH -p0 -i $PATCHES/js/patch-configure || echo "skipping patch"
+
+ env CFLAGS="$CFLAGS" LDFLAGS="$LDFLAGS" \
+ CPPFLAGS="-DXP_UNIX -DJS_C_STRINGS_ARE_UTF8" \
+ ./configure --prefix=$STATICLIBS/js \
+ --disable-debug \
+ --enable-optimize \
+ --enable-static \
+ --disable-shared-js \
+ --disable-tests \
+ --with-system-nspr \
+ --with-nspr-prefix=$STATICLIBS/nsprpub && \
+ $GNUMAKE all || exit 1
+
+ mkdir -p $JS_INCDIR/js
+ cp $JSDIR/js/src/*.h $JS_INCDIR
+ cp $JSDIR/js/src/*.tbl $JS_INCDIR
+ cp $JSDIR/js/src/libjs_static.a $JS_LIBDIR
+}
+
+
+do_setup()
+{
+ echo "==> spidermonkey (compile)"
+ mkdir -p $DISTDIR
+ mkdir -p $STATICLIBS
+}
+
+do_builddeps()
+{
+ if [ ! -f $STATICLIBS/nsprpub/lib/libnspr4.a ]; then
+ build_nspr
+ fi
+
+ if [ ! -f $STATICLIBS/js/lib/libjs_static.a ]; then
+ build_js
+ fi
+}
+
+
+clean()
+{
+ rm -rf $STATICLIBS
+ rm -rf $DISTDIR
+}
+
+
+
+usage()
+{
+ cat << EOF
+Usage: $basename [command] [OPTIONS]
+
+The $basename command compile Mozilla Spidermonkey and ICU statically
+for couch_core.
+
+Commands:
+
+ all: build couch_core static libs
+ clean: clean static libs
+ -?: display usage
+
+Report bugs at <https://github.com/refuge/couch_core>.
+EOF
+}
+
+
+if [ ! "x$COUCHDB_STATIC" = "x1" ]; then
+ exit 0
+fi
+
+if [ "x$1" = "x" ]; then
+ do_setup
+ do_builddeps
+ exit 0
+fi
+
+case "$1" in
+ all)
+ shift 1
+ do_setup
+ do_builddeps
+ ;;
+ clean)
+ shift 1
+ clean
+ ;;
+ js)
+ shift 1
+ do_setup
+ build_js
+ ;;
+ nspr)
+ shift 1
+ do_setup
+ build_nspr
+ ;;
+ help|--help|-h|-?)
+ usage
+ exit 0
+ ;;
+ *)
+ echo $basename: ERROR Unknown command $arg 1>&2
+ echo 1>&2
+ usage 1>&2
+ echo "### $basename: Exitting." 1>&2
+ exit 1;
+ ;;
+esac
+
+
+exit 0
http://git-wip-us.apache.org/repos/asf/couchdb/blob/e62a4fc1/apps/couch/c_src/couch_js/help.h
----------------------------------------------------------------------
diff --git a/apps/couch/c_src/couch_js/help.h b/apps/couch/c_src/couch_js/help.h
new file mode 100644
index 0000000..f4ddb24
--- /dev/null
+++ b/apps/couch/c_src/couch_js/help.h
@@ -0,0 +1,82 @@
+// Licensed under the Apache License, Version 2.0 (the "License"); you may not
+// use this file except in compliance with the License. You may obtain a copy of
+// the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
+
+#ifndef COUCHJS_HELP_H
+#define COUCHJS_HELP_H
+
+#include "config.h"
+
+static const char VERSION_TEMPLATE[] =
+ "%s - %s\n"
+ "\n"
+ "Licensed under the Apache License, Version 2.0 (the \"License\"); you may "
+ "not use\n"
+ "this file except in compliance with the License. You may obtain a copy of"
+ "the\n"
+ "License at\n"
+ "\n"
+ " http://www.apache.org/licenses/LICENSE-2.0\n"
+ "\n"
+ "Unless required by applicable law or agreed to in writing, software "
+ "distributed\n"
+ "under the License is distributed on an \"AS IS\" BASIS, WITHOUT "
+ "WARRANTIES OR\n"
+ "CONDITIONS OF ANY KIND, either express or implied. See the License "
+ "for the\n"
+ "specific language governing permissions and limitations under the "
+ "License.\n";
+
+static const char USAGE_TEMPLATE[] =
+ "Usage: %s [FILE]\n"
+ "\n"
+ "The %s command runs the %s JavaScript interpreter.\n"
+ "\n"
+ "The exit status is 0 for success or 1 for failure.\n"
+ "\n"
+ "Options:\n"
+ "\n"
+ " -h display a short help message and exit\n"
+ " -V display version information and exit\n"
+ " -H enable %s cURL bindings (only avaiable\n"
+ " if package was built with cURL available)\n"
+ " -S SIZE specify that the runtime should allow at\n"
+ " most SIZE bytes of memory to be allocated\n"
+ " -u FILE path to a .uri file containing the address\n"
+ " (or addresses) of one or more servers\n"
+ "\n"
+ "Report bugs at <%s>.\n";
+
+#define BASENAME COUCHJS_NAME
+
+#define couch_version(basename) \
+ fprintf( \
+ stdout, \
+ VERSION_TEMPLATE, \
+ basename, \
+ PACKAGE_STRING)
+
+#define DISPLAY_VERSION couch_version(BASENAME)
+
+
+#define couch_usage(basename) \
+ fprintf( \
+ stdout, \
+ USAGE_TEMPLATE, \
+ basename, \
+ basename, \
+ PACKAGE_NAME, \
+ basename, \
+ PACKAGE_BUGREPORT)
+
+#define DISPLAY_USAGE couch_usage(BASENAME)
+
+#endif // Included help.h
http://git-wip-us.apache.org/repos/asf/couchdb/blob/e62a4fc1/apps/couch/c_src/couch_js/http.c
----------------------------------------------------------------------
diff --git a/apps/couch/c_src/couch_js/http.c b/apps/couch/c_src/couch_js/http.c
new file mode 100644
index 0000000..3baa59d
--- /dev/null
+++ b/apps/couch/c_src/couch_js/http.c
@@ -0,0 +1,696 @@
+// Licensed under the Apache License, Version 2.0 (the "License"); you may not
+// use this file except in compliance with the License. You may obtain a copy of
+// the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <jsapi.h>
+#include "utf8.h"
+#include "util.h"
+
+// Soft dependency on cURL bindings because they're
+// only used when running the JS tests from the
+// command line which is rare.
+#ifdef WITHOUT_CURL
+
+void
+http_check_enabled()
+{
+ fprintf(stderr, "HTTP API was disabled at compile time.\n");
+ exit(3);
+}
+
+
+JSBool
+http_ctor(JSContext* cx, JSObject* req)
+{
+ return JS_FALSE;
+}
+
+
+JSBool
+http_dtor(JSContext* cx, JSObject* req)
+{
+ return JS_FALSE;
+}
+
+
+JSBool
+http_open(JSContext* cx, JSObject* req, jsval mth, jsval url, jsval snc)
+{
+ return JS_FALSE;
+}
+
+
+JSBool
+http_set_hdr(JSContext* cx, JSObject* req, jsval name, jsval val)
+{
+ return JS_FALSE;
+}
+
+
+JSBool
+http_send(JSContext* cx, JSObject* req, jsval body)
+{
+ return JS_FALSE;
+}
+
+
+int
+http_status(JSContext* cx, JSObject* req, jsval body)
+{
+ return -1;
+}
+
+JSBool
+http_uri(JSContext* cx, JSObject* req, couch_args* args, jsval* uri_val)
+{
+ return JS_FALSE;
+}
+
+
+#else
+#include <curl/curl.h>
+
+void
+http_check_enabled()
+{
+ return;
+}
+
+
+// Map some of the string function names to things which exist on Windows
+#ifdef XP_WIN
+#define strcasecmp _strcmpi
+#define strncasecmp _strnicmp
+#define snprintf _snprintf
+#endif
+
+
+typedef struct curl_slist CurlHeaders;
+
+
+typedef struct {
+ int method;
+ char* url;
+ CurlHeaders* req_headers;
+ jsint last_status;
+} HTTPData;
+
+
+char* METHODS[] = {"GET", "HEAD", "POST", "PUT", "DELETE", "COPY", "OPTIONS", NULL};
+
+
+#define GET 0
+#define HEAD 1
+#define POST 2
+#define PUT 3
+#define DELETE 4
+#define COPY 5
+#define OPTIONS 6
+
+
+static JSBool
+go(JSContext* cx, JSObject* obj, HTTPData* http, char* body, size_t blen);
+
+
+static JSString*
+str_from_binary(JSContext* cx, char* data, size_t length);
+
+
+JSBool
+http_ctor(JSContext* cx, JSObject* req)
+{
+ HTTPData* http = NULL;
+ JSBool ret = JS_FALSE;
+
+ http = (HTTPData*) malloc(sizeof(HTTPData));
+ if(!http)
+ {
+ JS_ReportError(cx, "Failed to create CouchHTTP instance.");
+ goto error;
+ }
+
+ http->method = -1;
+ http->url = NULL;
+ http->req_headers = NULL;
+ http->last_status = -1;
+
+ if(!JS_SetPrivate(cx, req, http))
+ {
+ JS_ReportError(cx, "Failed to set private CouchHTTP data.");
+ goto error;
+ }
+
+ ret = JS_TRUE;
+ goto success;
+
+error:
+ if(http) free(http);
+
+success:
+ return ret;
+}
+
+
+void
+http_dtor(JSContext* cx, JSObject* obj)
+{
+ HTTPData* http = (HTTPData*) JS_GetPrivate(cx, obj);
+ if(http) {
+ if(http->url) free(http->url);
+ if(http->req_headers) curl_slist_free_all(http->req_headers);
+ free(http);
+ }
+}
+
+
+JSBool
+http_open(JSContext* cx, JSObject* req, jsval mth, jsval url, jsval snc)
+{
+ HTTPData* http = (HTTPData*) JS_GetPrivate(cx, req);
+ char* method = NULL;
+ int methid;
+ JSBool ret = JS_FALSE;
+
+ if(!http) {
+ JS_ReportError(cx, "Invalid CouchHTTP instance.");
+ goto done;
+ }
+
+ if(JSVAL_IS_VOID(mth)) {
+ JS_ReportError(cx, "You must specify a method.");
+ goto done;
+ }
+
+ method = enc_string(cx, mth, NULL);
+ if(!method) {
+ JS_ReportError(cx, "Failed to encode method.");
+ goto done;
+ }
+
+ for(methid = 0; METHODS[methid] != NULL; methid++) {
+ if(strcasecmp(METHODS[methid], method) == 0) break;
+ }
+
+ if(methid > OPTIONS) {
+ JS_ReportError(cx, "Invalid method specified.");
+ goto done;
+ }
+
+ http->method = methid;
+
+ if(JSVAL_IS_VOID(url)) {
+ JS_ReportError(cx, "You must specify a URL.");
+ goto done;
+ }
+
+ if(http->url != NULL) {
+ free(http->url);
+ http->url = NULL;
+ }
+
+ http->url = enc_string(cx, url, NULL);
+ if(http->url == NULL) {
+ JS_ReportError(cx, "Failed to encode URL.");
+ goto done;
+ }
+
+ if(JSVAL_IS_BOOLEAN(snc) && JSVAL_TO_BOOLEAN(snc)) {
+ JS_ReportError(cx, "Synchronous flag must be false.");
+ goto done;
+ }
+
+ if(http->req_headers) {
+ curl_slist_free_all(http->req_headers);
+ http->req_headers = NULL;
+ }
+
+ // Disable Expect: 100-continue
+ http->req_headers = curl_slist_append(http->req_headers, "Expect:");
+
+ ret = JS_TRUE;
+
+done:
+ if(method) free(method);
+ return ret;
+}
+
+
+JSBool
+http_set_hdr(JSContext* cx, JSObject* req, jsval name, jsval val)
+{
+ HTTPData* http = (HTTPData*) JS_GetPrivate(cx, req);
+ char* keystr = NULL;
+ char* valstr = NULL;
+ char* hdrbuf = NULL;
+ size_t hdrlen = -1;
+ JSBool ret = JS_FALSE;
+
+ if(!http) {
+ JS_ReportError(cx, "Invalid CouchHTTP instance.");
+ goto done;
+ }
+
+ if(JSVAL_IS_VOID(name))
+ {
+ JS_ReportError(cx, "You must speciy a header name.");
+ goto done;
+ }
+
+ keystr = enc_string(cx, name, NULL);
+ if(!keystr)
+ {
+ JS_ReportError(cx, "Failed to encode header name.");
+ goto done;
+ }
+
+ if(JSVAL_IS_VOID(val))
+ {
+ JS_ReportError(cx, "You must specify a header value.");
+ goto done;
+ }
+
+ valstr = enc_string(cx, val, NULL);
+ if(!valstr)
+ {
+ JS_ReportError(cx, "Failed to encode header value.");
+ goto done;
+ }
+
+ hdrlen = strlen(keystr) + strlen(valstr) + 3;
+ hdrbuf = (char*) malloc(hdrlen * sizeof(char));
+ if(!hdrbuf) {
+ JS_ReportError(cx, "Failed to allocate header buffer.");
+ goto done;
+ }
+
+ snprintf(hdrbuf, hdrlen, "%s: %s", keystr, valstr);
+ http->req_headers = curl_slist_append(http->req_headers, hdrbuf);
+
+ ret = JS_TRUE;
+
+done:
+ if(keystr) free(keystr);
+ if(valstr) free(valstr);
+ if(hdrbuf) free(hdrbuf);
+ return ret;
+}
+
+JSBool
+http_send(JSContext* cx, JSObject* req, jsval body)
+{
+ HTTPData* http = (HTTPData*) JS_GetPrivate(cx, req);
+ char* bodystr = NULL;
+ size_t bodylen = 0;
+ JSBool ret = JS_FALSE;
+
+ if(!http) {
+ JS_ReportError(cx, "Invalid CouchHTTP instance.");
+ goto done;
+ }
+
+ if(!JSVAL_IS_VOID(body)) {
+ bodystr = enc_string(cx, body, &bodylen);
+ if(!bodystr) {
+ JS_ReportError(cx, "Failed to encode body.");
+ goto done;
+ }
+ }
+
+ ret = go(cx, req, http, bodystr, bodylen);
+
+done:
+ if(bodystr) free(bodystr);
+ return ret;
+}
+
+int
+http_status(JSContext* cx, JSObject* req)
+{
+ HTTPData* http = (HTTPData*) JS_GetPrivate(cx, req);
+
+ if(!http) {
+ JS_ReportError(cx, "Invalid CouchHTTP instance.");
+ return JS_FALSE;
+ }
+
+ return http->last_status;
+}
+
+JSBool
+http_uri(JSContext* cx, JSObject* req, couch_args* args, jsval* uri_val)
+{
+ FILE* uri_fp = NULL;
+ JSString* uri_str;
+
+ // Default is http://localhost:5984/ when no uri file is specified
+ if (!args->uri_file) {
+ uri_str = JS_InternString(cx, "http://localhost:5984/");
+ *uri_val = STRING_TO_JSVAL(uri_str);
+ return JS_TRUE;
+ }
+
+ // Else check to see if the base url is cached in a reserved slot
+ if (JS_GetReservedSlot(cx, req, 0, uri_val) && !JSVAL_IS_VOID(*uri_val)) {
+ return JS_TRUE;
+ }
+
+ // Read the first line of the couch.uri file.
+ if(!((uri_fp = fopen(args->uri_file, "r")) &&
+ (uri_str = couch_readline(cx, uri_fp)))) {
+ JS_ReportError(cx, "Failed to read couch.uri file.");
+ goto error;
+ }
+
+ fclose(uri_fp);
+ *uri_val = STRING_TO_JSVAL(uri_str);
+ JS_SetReservedSlot(cx, req, 0, *uri_val);
+ return JS_TRUE;
+
+error:
+ if(uri_fp) fclose(uri_fp);
+ return JS_FALSE;
+}
+
+
+// Curl Helpers
+
+typedef struct {
+ HTTPData* http;
+ JSContext* cx;
+ JSObject* resp_headers;
+ char* sendbuf;
+ size_t sendlen;
+ size_t sent;
+ int sent_once;
+ char* recvbuf;
+ size_t recvlen;
+ size_t read;
+} CurlState;
+
+/*
+ * I really hate doing this but this doesn't have to be
+ * uber awesome, it just has to work.
+ */
+CURL* HTTP_HANDLE = NULL;
+char ERRBUF[CURL_ERROR_SIZE];
+
+static size_t send_body(void *ptr, size_t size, size_t nmem, void *data);
+static int seek_body(void *ptr, curl_off_t offset, int origin);
+static size_t recv_body(void *ptr, size_t size, size_t nmem, void *data);
+static size_t recv_header(void *ptr, size_t size, size_t nmem, void *data);
+
+static JSBool
+go(JSContext* cx, JSObject* obj, HTTPData* http, char* body, size_t bodylen)
+{
+ CurlState state;
+ char* referer;
+ JSString* jsbody;
+ JSBool ret = JS_FALSE;
+ jsval tmp;
+
+ state.cx = cx;
+ state.http = http;
+
+ state.sendbuf = body;
+ state.sendlen = bodylen;
+ state.sent = 0;
+ state.sent_once = 0;
+
+ state.recvbuf = NULL;
+ state.recvlen = 0;
+ state.read = 0;
+
+ if(HTTP_HANDLE == NULL) {
+ HTTP_HANDLE = curl_easy_init();
+ curl_easy_setopt(HTTP_HANDLE, CURLOPT_READFUNCTION, send_body);
+ curl_easy_setopt(HTTP_HANDLE, CURLOPT_SEEKFUNCTION,
+ (curl_seek_callback) seek_body);
+ curl_easy_setopt(HTTP_HANDLE, CURLOPT_HEADERFUNCTION, recv_header);
+ curl_easy_setopt(HTTP_HANDLE, CURLOPT_WRITEFUNCTION, recv_body);
+ curl_easy_setopt(HTTP_HANDLE, CURLOPT_NOPROGRESS, 1);
+ curl_easy_setopt(HTTP_HANDLE, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
+ curl_easy_setopt(HTTP_HANDLE, CURLOPT_ERRORBUFFER, ERRBUF);
+ curl_easy_setopt(HTTP_HANDLE, CURLOPT_COOKIEFILE, "");
+ curl_easy_setopt(HTTP_HANDLE, CURLOPT_USERAGENT,
+ "CouchHTTP Client - Relax");
+ }
+
+ if(!HTTP_HANDLE) {
+ JS_ReportError(cx, "Failed to initialize cURL handle.");
+ goto done;
+ }
+
+ if(!JS_GetReservedSlot(cx, obj, 0, &tmp)) {
+ JS_ReportError(cx, "Failed to readreserved slot.");
+ goto done;
+ }
+
+ if(!(referer = enc_string(cx, tmp, NULL))) {
+ JS_ReportError(cx, "Failed to encode referer.");
+ goto done;
+ }
+ curl_easy_setopt(HTTP_HANDLE, CURLOPT_REFERER, referer);
+ free(referer);
+
+ if(http->method < 0 || http->method > OPTIONS) {
+ JS_ReportError(cx, "INTERNAL: Unknown method.");
+ goto done;
+ }
+
+ curl_easy_setopt(HTTP_HANDLE, CURLOPT_CUSTOMREQUEST, METHODS[http->method]);
+ curl_easy_setopt(HTTP_HANDLE, CURLOPT_NOBODY, 0);
+ curl_easy_setopt(HTTP_HANDLE, CURLOPT_FOLLOWLOCATION, 1);
+ curl_easy_setopt(HTTP_HANDLE, CURLOPT_UPLOAD, 0);
+
+ if(http->method == HEAD) {
+ curl_easy_setopt(HTTP_HANDLE, CURLOPT_NOBODY, 1);
+ curl_easy_setopt(HTTP_HANDLE, CURLOPT_FOLLOWLOCATION, 0);
+ } else if(http->method == POST || http->method == PUT) {
+ curl_easy_setopt(HTTP_HANDLE, CURLOPT_UPLOAD, 1);
+ curl_easy_setopt(HTTP_HANDLE, CURLOPT_FOLLOWLOCATION, 0);
+ }
+
+ if(body && bodylen) {
+ curl_easy_setopt(HTTP_HANDLE, CURLOPT_INFILESIZE, bodylen);
+ } else {
+ curl_easy_setopt(HTTP_HANDLE, CURLOPT_INFILESIZE, 0);
+ }
+
+ // curl_easy_setopt(HTTP_HANDLE, CURLOPT_VERBOSE, 1);
+
+ curl_easy_setopt(HTTP_HANDLE, CURLOPT_URL, http->url);
+ curl_easy_setopt(HTTP_HANDLE, CURLOPT_HTTPHEADER, http->req_headers);
+ curl_easy_setopt(HTTP_HANDLE, CURLOPT_READDATA, &state);
+ curl_easy_setopt(HTTP_HANDLE, CURLOPT_SEEKDATA, &state);
+ curl_easy_setopt(HTTP_HANDLE, CURLOPT_WRITEHEADER, &state);
+ curl_easy_setopt(HTTP_HANDLE, CURLOPT_WRITEDATA, &state);
+
+ if(curl_easy_perform(HTTP_HANDLE) != 0) {
+ JS_ReportError(cx, "Failed to execute HTTP request: %s", ERRBUF);
+ goto done;
+ }
+
+ if(!state.resp_headers) {
+ JS_ReportError(cx, "Failed to recieve HTTP headers.");
+ goto done;
+ }
+
+ tmp = OBJECT_TO_JSVAL(state.resp_headers);
+ if(!JS_DefineProperty(
+ cx, obj,
+ "_headers",
+ tmp,
+ NULL, NULL,
+ JSPROP_READONLY
+ )) {
+ JS_ReportError(cx, "INTERNAL: Failed to set response headers.");
+ goto done;
+ }
+
+ if(state.recvbuf) {
+ state.recvbuf[state.read] = '\0';
+ jsbody = dec_string(cx, state.recvbuf, state.read+1);
+ if(!jsbody) {
+ // If we can't decode the body as UTF-8 we forcefully
+ // convert it to a string by just forcing each byte
+ // to a jschar.
+ jsbody = str_from_binary(cx, state.recvbuf, state.read);
+ if(!jsbody) {
+ if(!JS_IsExceptionPending(cx)) {
+ JS_ReportError(cx, "INTERNAL: Failed to decode body.");
+ }
+ goto done;
+ }
+ }
+ tmp = STRING_TO_JSVAL(jsbody);
+ } else {
+ tmp = JS_GetEmptyStringValue(cx);
+ }
+
+ if(!JS_DefineProperty(
+ cx, obj,
+ "responseText",
+ tmp,
+ NULL, NULL,
+ JSPROP_READONLY
+ )) {
+ JS_ReportError(cx, "INTERNAL: Failed to set responseText.");
+ goto done;
+ }
+
+ ret = JS_TRUE;
+
+done:
+ if(state.recvbuf) JS_free(cx, state.recvbuf);
+ return ret;
+}
+
+static size_t
+send_body(void *ptr, size_t size, size_t nmem, void *data)
+{
+ CurlState* state = (CurlState*) data;
+ size_t length = size * nmem;
+ size_t towrite = state->sendlen - state->sent;
+
+ // Assume this is cURL trying to resend a request that
+ // failed.
+ if(towrite == 0 && state->sent_once == 0) {
+ state->sent_once = 1;
+ return 0;
+ } else if(towrite == 0) {
+ state->sent = 0;
+ state->sent_once = 0;
+ towrite = state->sendlen;
+ }
+
+ if(length < towrite) towrite = length;
+
+ memcpy(ptr, state->sendbuf + state->sent, towrite);
+ state->sent += towrite;
+
+ return towrite;
+}
+
+static int
+seek_body(void* ptr, curl_off_t offset, int origin)
+{
+ CurlState* state = (CurlState*) ptr;
+ if(origin != SEEK_SET) return -1;
+
+ state->sent = (size_t) offset;
+ return (int) state->sent;
+}
+
+static size_t
+recv_header(void *ptr, size_t size, size_t nmem, void *data)
+{
+ CurlState* state = (CurlState*) data;
+ char code[4];
+ char* header = (char*) ptr;
+ size_t length = size * nmem;
+ JSString* hdr = NULL;
+ jsuint hdrlen;
+ jsval hdrval;
+
+ if(length > 7 && strncasecmp(header, "HTTP/1.", 7) == 0) {
+ if(length < 12) {
+ return CURLE_WRITE_ERROR;
+ }
+
+ memcpy(code, header+9, 3*sizeof(char));
+ code[3] = '\0';
+ state->http->last_status = atoi(code);
+
+ state->resp_headers = JS_NewArrayObject(state->cx, 0, NULL);
+ if(!state->resp_headers) {
+ return CURLE_WRITE_ERROR;
+ }
+
+ return length;
+ }
+
+ // We get a notice at the \r\n\r\n after headers.
+ if(length <= 2) {
+ return length;
+ }
+
+ // Append the new header to our array.
+ hdr = dec_string(state->cx, header, length);
+ if(!hdr) {
+ return CURLE_WRITE_ERROR;
+ }
+
+ if(!JS_GetArrayLength(state->cx, state->resp_headers, &hdrlen)) {
+ return CURLE_WRITE_ERROR;
+ }
+
+ hdrval = STRING_TO_JSVAL(hdr);
+ if(!JS_SetElement(state->cx, state->resp_headers, hdrlen, &hdrval)) {
+ return CURLE_WRITE_ERROR;
+ }
+
+ return length;
+}
+
+static size_t
+recv_body(void *ptr, size_t size, size_t nmem, void *data)
+{
+ CurlState* state = (CurlState*) data;
+ size_t length = size * nmem;
+ char* tmp = NULL;
+
+ if(!state->recvbuf) {
+ state->recvlen = 4096;
+ state->read = 0;
+ state->recvbuf = JS_malloc(state->cx, state->recvlen);
+ }
+
+ if(!state->recvbuf) {
+ return CURLE_WRITE_ERROR;
+ }
+
+ // +1 so we can add '\0' back up in the go function.
+ while(length+1 > state->recvlen - state->read) state->recvlen *= 2;
+ tmp = JS_realloc(state->cx, state->recvbuf, state->recvlen);
+ if(!tmp) return CURLE_WRITE_ERROR;
+ state->recvbuf = tmp;
+
+ memcpy(state->recvbuf + state->read, ptr, length);
+ state->read += length;
+ return length;
+}
+
+JSString*
+str_from_binary(JSContext* cx, char* data, size_t length)
+{
+ jschar* conv = (jschar*) JS_malloc(cx, length * sizeof(jschar));
+ JSString* ret = NULL;
+ size_t i;
+
+ if(!conv) return NULL;
+
+ for(i = 0; i < length; i++) {
+ conv[i] = (jschar) data[i];
+ }
+
+ ret = JS_NewUCString(cx, conv, length);
+ if(!ret) JS_free(cx, conv);
+
+ return ret;
+}
+
+#endif /* HAVE_CURL */
http://git-wip-us.apache.org/repos/asf/couchdb/blob/e62a4fc1/apps/couch/c_src/couch_js/http.h
----------------------------------------------------------------------
diff --git a/apps/couch/c_src/couch_js/http.h b/apps/couch/c_src/couch_js/http.h
new file mode 100644
index 0000000..63d45bd
--- /dev/null
+++ b/apps/couch/c_src/couch_js/http.h
@@ -0,0 +1,27 @@
+// Licensed under the Apache License, Version 2.0 (the "License"); you may not
+// use this file except in compliance with the License. You may obtain a copy of
+// the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
+
+#ifndef COUCH_JS_HTTP_H
+#define COUCH_JS_HTTP_H
+
+#include "util.h"
+
+void http_check_enabled();
+JSBool http_ctor(JSContext* cx, JSObject* req);
+void http_dtor(JSContext* cx, JSObject* req);
+JSBool http_open(JSContext* cx, JSObject* req, jsval mth, jsval url, jsval snc);
+JSBool http_set_hdr(JSContext* cx, JSObject* req, jsval name, jsval val);
+JSBool http_send(JSContext* cx, JSObject* req, jsval body);
+int http_status(JSContext* cx, JSObject* req);
+JSBool http_uri(JSContext* cx, JSObject *req, couch_args* args, jsval* uri);
+
+#endif
http://git-wip-us.apache.org/repos/asf/couchdb/blob/e62a4fc1/apps/couch/c_src/couch_js/main.c
----------------------------------------------------------------------
diff --git a/apps/couch/c_src/couch_js/main.c b/apps/couch/c_src/couch_js/main.c
new file mode 100644
index 0000000..a0fc143
--- /dev/null
+++ b/apps/couch/c_src/couch_js/main.c
@@ -0,0 +1,431 @@
+// Licensed under the Apache License, Version 2.0 (the "License"); you may not
+// use this file except in compliance with the License. You may obtain a copy of
+// the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#include <jsapi.h>
+#include "http.h"
+#include "utf8.h"
+#include "util.h"
+
+
+#define SETUP_REQUEST(cx) \
+ JS_SetContextThread(cx); \
+ JS_BeginRequest(cx);
+#define FINISH_REQUEST(cx) \
+ JS_EndRequest(cx); \
+ JS_ClearContextThread(cx);
+
+
+static JSClass global_class = {
+ "GlobalClass",
+ JSCLASS_GLOBAL_FLAGS,
+ JS_PropertyStub,
+ JS_PropertyStub,
+ JS_PropertyStub,
+ JS_StrictPropertyStub,
+ JS_EnumerateStub,
+ JS_ResolveStub,
+ JS_ConvertStub,
+ JS_FinalizeStub,
+ JSCLASS_NO_OPTIONAL_MEMBERS
+};
+
+
+static JSBool
+req_ctor(JSContext* cx, uintN argc, jsval* vp)
+{
+ JSBool ret;
+ JSObject* obj = JS_NewObjectForConstructor(cx, vp);
+ if(!obj) {
+ JS_ReportError(cx, "Failed to create CouchHTTP instance.\n");
+ return JS_FALSE;
+ }
+ ret = http_ctor(cx, obj);
+ JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(obj));
+ return ret;
+}
+
+
+static void
+req_dtor(JSContext* cx, JSObject* obj)
+{
+ http_dtor(cx, obj);
+}
+
+
+static JSBool
+req_open(JSContext* cx, uintN argc, jsval* vp)
+{
+ JSObject* obj = JS_THIS_OBJECT(cx, vp);
+ jsval* argv = JS_ARGV(cx, vp);
+ JSBool ret = JS_FALSE;
+
+ if(argc == 2) {
+ ret = http_open(cx, obj, argv[0], argv[1], JSVAL_FALSE);
+ } else if(argc == 3) {
+ ret = http_open(cx, obj, argv[0], argv[1], argv[2]);
+ } else {
+ JS_ReportError(cx, "Invalid call to CouchHTTP.open");
+ }
+
+ JS_SET_RVAL(cx, vp, JSVAL_VOID);
+ return ret;
+}
+
+
+static JSBool
+req_set_hdr(JSContext* cx, uintN argc, jsval* vp)
+{
+ JSObject* obj = JS_THIS_OBJECT(cx, vp);
+ jsval* argv = JS_ARGV(cx, vp);
+ JSBool ret = JS_FALSE;
+
+ if(argc == 2) {
+ ret = http_set_hdr(cx, obj, argv[0], argv[1]);
+ } else {
+ JS_ReportError(cx, "Invalid call to CouchHTTP.set_header");
+ }
+
+ JS_SET_RVAL(cx, vp, JSVAL_VOID);
+ return ret;
+}
+
+
+static JSBool
+req_send(JSContext* cx, uintN argc, jsval* vp)
+{
+ JSObject* obj = JS_THIS_OBJECT(cx, vp);
+ jsval* argv = JS_ARGV(cx, vp);
+ JSBool ret = JS_FALSE;
+
+ if(argc == 1) {
+ ret = http_send(cx, obj, argv[0]);
+ } else {
+ JS_ReportError(cx, "Invalid call to CouchHTTP.send");
+ }
+
+ JS_SET_RVAL(cx, vp, JSVAL_VOID);
+ return ret;
+}
+
+
+static JSBool
+req_status(JSContext* cx, JSObject* obj, jsid pid, jsval* vp)
+{
+ int status = http_status(cx, obj);
+ if(status < 0)
+ return JS_FALSE;
+
+ JS_SET_RVAL(cx, vp, INT_TO_JSVAL(status));
+ return JS_TRUE;
+}
+
+
+static JSBool
+base_url(JSContext *cx, JSObject* obj, jsid pid, jsval* vp)
+{
+ couch_args *args = (couch_args*)JS_GetContextPrivate(cx);
+ return http_uri(cx, obj, args, &JS_RVAL(cx, vp));
+}
+
+
+static JSBool
+evalcx(JSContext *cx, uintN argc, jsval* vp)
+{
+ jsval* argv = JS_ARGV(cx, vp);
+ JSString* str;
+ JSObject* sandbox;
+ JSObject* global;
+ JSContext* subcx;
+ JSCrossCompartmentCall* call = NULL;
+ const jschar* src;
+ size_t srclen;
+ jsval rval;
+ JSBool ret = JS_FALSE;
+ char *name = NULL;
+
+ sandbox = NULL;
+ if(!JS_ConvertArguments(cx, argc, argv, "S / o", &str, &sandbox)) {
+ return JS_FALSE;
+ }
+
+ subcx = JS_NewContext(JS_GetRuntime(cx), 8L * 1024L);
+ if(!subcx) {
+ JS_ReportOutOfMemory(cx);
+ return JS_FALSE;
+ }
+
+ SETUP_REQUEST(subcx);
+
+ src = JS_GetStringCharsAndLength(cx, str, &srclen);
+
+ // Re-use the compartment associated with the main context,
+ // rather than creating a new compartment */
+ global = JS_GetGlobalObject(cx);
+ if(global == NULL) goto done;
+ call = JS_EnterCrossCompartmentCall(subcx, global);
+
+ if(!sandbox) {
+ sandbox = JS_NewGlobalObject(subcx, &global_class);
+ if(!sandbox || !JS_InitStandardClasses(subcx, sandbox)) {
+ goto done;
+ }
+ }
+
+ if(argc > 2) {
+ name = enc_string(cx, argv[2], NULL);
+ }
+
+ if(srclen == 0) {
+ JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(sandbox));
+ } else {
+ JS_EvaluateUCScript(subcx, sandbox, src, srclen, name, 1, &rval);
+ JS_SET_RVAL(cx, vp, rval);
+ }
+
+ ret = JS_TRUE;
+
+done:
+ if(name) JS_free(cx, name);
+ JS_LeaveCrossCompartmentCall(call);
+ FINISH_REQUEST(subcx);
+ JS_DestroyContext(subcx);
+ return ret;
+}
+
+
+static JSBool
+gc(JSContext* cx, uintN argc, jsval* vp)
+{
+ JS_GC(cx);
+ JS_SET_RVAL(cx, vp, JSVAL_VOID);
+ return JS_TRUE;
+}
+
+
+static JSBool
+print(JSContext* cx, uintN argc, jsval* vp)
+{
+ jsval* argv = JS_ARGV(cx, vp);
+ couch_print(cx, argc, argv);
+ JS_SET_RVAL(cx, vp, JSVAL_VOID);
+ return JS_TRUE;
+}
+
+
+static JSBool
+quit(JSContext* cx, uintN argc, jsval* vp)
+{
+ jsval* argv = JS_ARGV(cx, vp);
+ int exit_code = 0;
+ JS_ConvertArguments(cx, argc, argv, "/i", &exit_code);
+ exit(exit_code);
+}
+
+
+static JSBool
+readline(JSContext* cx, uintN argc, jsval* vp)
+{
+ JSString* line;
+
+ /* GC Occasionally */
+ JS_MaybeGC(cx);
+
+ line = couch_readline(cx, stdin);
+ if(line == NULL) return JS_FALSE;
+
+ JS_SET_RVAL(cx, vp, STRING_TO_JSVAL(line));
+ return JS_TRUE;
+}
+
+
+static JSBool
+seal(JSContext* cx, uintN argc, jsval* vp)
+{
+ jsval* argv = JS_ARGV(cx, vp);
+ JSObject *target;
+ JSBool deep = JS_FALSE;
+ JSBool ret;
+
+ if(!JS_ConvertArguments(cx, argc, argv, "o/b", &target, &deep))
+ return JS_FALSE;
+
+ if(!target) {
+ JS_SET_RVAL(cx, vp, JSVAL_VOID);
+ return JS_TRUE;
+ }
+
+
+ ret = deep ? JS_DeepFreezeObject(cx, target) : JS_FreezeObject(cx, target);
+ JS_SET_RVAL(cx, vp, JSVAL_VOID);
+ return ret;
+}
+
+
+JSClass CouchHTTPClass = {
+ "CouchHTTP",
+ JSCLASS_HAS_PRIVATE
+ | JSCLASS_CONSTRUCT_PROTOTYPE
+ | JSCLASS_HAS_RESERVED_SLOTS(2),
+ JS_PropertyStub,
+ JS_PropertyStub,
+ JS_PropertyStub,
+ JS_StrictPropertyStub,
+ JS_EnumerateStub,
+ JS_ResolveStub,
+ JS_ConvertStub,
+ req_dtor,
+ JSCLASS_NO_OPTIONAL_MEMBERS
+};
+
+
+JSPropertySpec CouchHTTPProperties[] = {
+ {"status", 0, JSPROP_READONLY, req_status, NULL},
+ {"base_url", 0, JSPROP_READONLY | JSPROP_SHARED, base_url, NULL},
+ {0, 0, 0, 0, 0}
+};
+
+
+JSFunctionSpec CouchHTTPFunctions[] = {
+ JS_FS("_open", req_open, 3, 0),
+ JS_FS("_setRequestHeader", req_set_hdr, 2, 0),
+ JS_FS("_send", req_send, 1, 0),
+ JS_FS_END
+};
+
+
+static JSFunctionSpec global_functions[] = {
+ JS_FS("evalcx", evalcx, 0, 0),
+ JS_FS("gc", gc, 0, 0),
+ JS_FS("print", print, 0, 0),
+ JS_FS("quit", quit, 0, 0),
+ JS_FS("readline", readline, 0, 0),
+ JS_FS("seal", seal, 0, 0),
+ JS_FS_END
+};
+
+
+int
+main(int argc, const char* argv[])
+{
+ JSRuntime* rt = NULL;
+ JSContext* cx = NULL;
+ JSObject* global = NULL;
+ JSCrossCompartmentCall *call = NULL;
+ JSObject* klass = NULL;
+ JSObject* script;
+ JSString* scriptsrc;
+ const jschar* schars;
+ size_t slen;
+ jsval sroot;
+ jsval result;
+ int i;
+
+ couch_args* args = couch_parse_args(argc, argv);
+
+ rt = JS_NewRuntime(args->stack_size);
+ if(rt == NULL)
+ return 1;
+
+ cx = JS_NewContext(rt, 8L * 1024L);
+ if(cx == NULL)
+ return 1;
+
+ JS_SetErrorReporter(cx, couch_error);
+ JS_ToggleOptions(cx, JSOPTION_XML);
+ JS_SetOptions(cx, JSOPTION_METHODJIT);
+#ifdef JSOPTION_TYPE_INFERENCE
+ JS_SetOptions(cx, JSOPTION_TYPE_INFERENCE);
+#endif
+ JS_SetContextPrivate(cx, args);
+
+ SETUP_REQUEST(cx);
+
+ global = JS_NewCompartmentAndGlobalObject(cx, &global_class, NULL);
+ if(global == NULL)
+ return 1;
+
+ call = JS_EnterCrossCompartmentCall(cx, global);
+
+ JS_SetGlobalObject(cx, global);
+
+ if(!JS_InitStandardClasses(cx, global))
+ return 1;
+
+ if(couch_load_funcs(cx, global, global_functions) != JS_TRUE)
+ return 1;
+
+ if(args->use_http) {
+ http_check_enabled();
+
+ klass = JS_InitClass(
+ cx, global,
+ NULL,
+ &CouchHTTPClass, req_ctor,
+ 0,
+ CouchHTTPProperties, CouchHTTPFunctions,
+ NULL, NULL
+ );
+
+ if(!klass)
+ {
+ fprintf(stderr, "Failed to initialize CouchHTTP class.\n");
+ exit(2);
+ }
+ }
+
+ for(i = 0 ; args->scripts[i] ; i++) {
+ // Convert script source to jschars.
+ scriptsrc = couch_readfile(cx, args->scripts[i]);
+ if(!scriptsrc)
+ return 1;
+
+ schars = JS_GetStringCharsAndLength(cx, scriptsrc, &slen);
+
+ // Root it so GC doesn't collect it.
+ sroot = STRING_TO_JSVAL(scriptsrc);
+ if(JS_AddValueRoot(cx, &sroot) != JS_TRUE) {
+ fprintf(stderr, "Internal root error.\n");
+ return 1;
+ }
+
+ // Compile and run
+ script = JS_CompileUCScript(cx, global, schars, slen,
+ args->scripts[i], 1);
+ if(!script) {
+ fprintf(stderr, "Failed to compile script.\n");
+ return 1;
+ }
+
+ if(JS_ExecuteScript(cx, global, script, &result) != JS_TRUE) {
+ fprintf(stderr, "Failed to execute script.\n");
+ return 1;
+ }
+
+ // Warning message if we don't remove it.
+ JS_RemoveValueRoot(cx, &sroot);
+
+ // Give the GC a chance to run.
+ JS_MaybeGC(cx);
+ }
+
+ JS_LeaveCrossCompartmentCall(call);
+ FINISH_REQUEST(cx);
+ JS_DestroyContext(cx);
+ JS_DestroyRuntime(rt);
+ JS_ShutDown();
+
+ return 0;
+}
http://git-wip-us.apache.org/repos/asf/couchdb/blob/e62a4fc1/apps/couch/c_src/couch_js/utf8.c
----------------------------------------------------------------------
diff --git a/apps/couch/c_src/couch_js/utf8.c b/apps/couch/c_src/couch_js/utf8.c
new file mode 100644
index 0000000..2b3735a
--- /dev/null
+++ b/apps/couch/c_src/couch_js/utf8.c
@@ -0,0 +1,288 @@
+// Licensed under the Apache License, Version 2.0 (the "License"); you may not
+// use this file except in compliance with the License. You may obtain a copy of
+// the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
+
+#include <jsapi.h>
+
+static int
+enc_char(uint8 *utf8Buffer, uint32 ucs4Char)
+{
+ int utf8Length = 1;
+
+ if (ucs4Char < 0x80)
+ {
+ *utf8Buffer = (uint8)ucs4Char;
+ }
+ else
+ {
+ int i;
+ uint32 a = ucs4Char >> 11;
+ utf8Length = 2;
+ while(a)
+ {
+ a >>= 5;
+ utf8Length++;
+ }
+ i = utf8Length;
+ while(--i)
+ {
+ utf8Buffer[i] = (uint8)((ucs4Char & 0x3F) | 0x80);
+ ucs4Char >>= 6;
+ }
+ *utf8Buffer = (uint8)(0x100 - (1 << (8-utf8Length)) + ucs4Char);
+ }
+
+ return utf8Length;
+}
+
+static JSBool
+enc_charbuf(const jschar* src, size_t srclen, char* dst, size_t* dstlenp)
+{
+ size_t i;
+ size_t utf8Len;
+ size_t dstlen = *dstlenp;
+ size_t origDstlen = dstlen;
+ jschar c;
+ jschar c2;
+ uint32 v;
+ uint8 utf8buf[6];
+
+ if(!dst)
+ {
+ dstlen = origDstlen = (size_t) -1;
+ }
+
+ while(srclen)
+ {
+ c = *src++;
+ srclen--;
+
+ if(c <= 0xD7FF || c >= 0xE000)
+ {
+ v = (uint32) c;
+ }
+ else if(c >= 0xD800 && c <= 0xDBFF)
+ {
+ if(srclen < 1) goto buffer_too_small;
+ c2 = *src++;
+ srclen--;
+ if(c2 >= 0xDC00 && c2 <= 0xDFFF)
+ {
+ v = (uint32) (((c - 0xD800) << 10) + (c2 - 0xDC00) + 0x10000);
+ }
+ else
+ {
+ // Invalid second half of surrogate pair
+ v = (uint32) 0xFFFD;
+ }
+ }
+ else
+ {
+ // Invalid first half surrogate pair
+ v = (uint32) 0xFFFD;
+ }
+
+ if(v < 0x0080)
+ {
+ /* no encoding necessary - performance hack */
+ if(!dstlen) goto buffer_too_small;
+ if(dst) *dst++ = (char) v;
+ utf8Len = 1;
+ }
+ else
+ {
+ utf8Len = enc_char(utf8buf, v);
+ if(utf8Len > dstlen) goto buffer_too_small;
+ if(dst)
+ {
+ for (i = 0; i < utf8Len; i++)
+ {
+ *dst++ = (char) utf8buf[i];
+ }
+ }
+ }
+ dstlen -= utf8Len;
+ }
+
+ *dstlenp = (origDstlen - dstlen);
+ return JS_TRUE;
+
+buffer_too_small:
+ *dstlenp = (origDstlen - dstlen);
+ return JS_FALSE;
+}
+
+char*
+enc_string(JSContext* cx, jsval arg, size_t* buflen)
+{
+ JSString* str = NULL;
+ const jschar* src = NULL;
+ char* bytes = NULL;
+ size_t srclen = 0;
+ size_t byteslen = 0;
+
+ str = JS_ValueToString(cx, arg);
+ if(!str) goto error;
+
+ src = JS_GetStringCharsAndLength(cx, str, &srclen);
+
+ if(!enc_charbuf(src, srclen, NULL, &byteslen)) goto error;
+
+ bytes = JS_malloc(cx, (byteslen) + 1);
+ bytes[byteslen] = 0;
+
+ if(!enc_charbuf(src, srclen, bytes, &byteslen)) goto error;
+
+ if(buflen) *buflen = byteslen;
+ goto success;
+
+error:
+ if(bytes != NULL) JS_free(cx, bytes);
+ bytes = NULL;
+
+success:
+ return bytes;
+}
+
+static uint32
+dec_char(const uint8 *utf8Buffer, int utf8Length)
+{
+ uint32 ucs4Char;
+ uint32 minucs4Char;
+
+ /* from Unicode 3.1, non-shortest form is illegal */
+ static const uint32 minucs4Table[] = {
+ 0x00000080, 0x00000800, 0x0001000, 0x0020000, 0x0400000
+ };
+
+ if (utf8Length == 1)
+ {
+ ucs4Char = *utf8Buffer;
+ }
+ else
+ {
+ ucs4Char = *utf8Buffer++ & ((1<<(7-utf8Length))-1);
+ minucs4Char = minucs4Table[utf8Length-2];
+ while(--utf8Length)
+ {
+ ucs4Char = ucs4Char<<6 | (*utf8Buffer++ & 0x3F);
+ }
+ if(ucs4Char < minucs4Char || ucs4Char == 0xFFFE || ucs4Char == 0xFFFF)
+ {
+ ucs4Char = 0xFFFD;
+ }
+ }
+
+ return ucs4Char;
+}
+
+static JSBool
+dec_charbuf(const char *src, size_t srclen, jschar *dst, size_t *dstlenp)
+{
+ uint32 v;
+ size_t offset = 0;
+ size_t j;
+ size_t n;
+ size_t dstlen = *dstlenp;
+ size_t origDstlen = dstlen;
+
+ if(!dst) dstlen = origDstlen = (size_t) -1;
+
+ while(srclen)
+ {
+ v = (uint8) *src;
+ n = 1;
+
+ if(v & 0x80)
+ {
+ while(v & (0x80 >> n))
+ {
+ n++;
+ }
+
+ if(n > srclen) goto buffer_too_small;
+ if(n == 1 || n > 6) goto bad_character;
+
+ for(j = 1; j < n; j++)
+ {
+ if((src[j] & 0xC0) != 0x80) goto bad_character;
+ }
+
+ v = dec_char((const uint8 *) src, n);
+ if(v >= 0x10000)
+ {
+ v -= 0x10000;
+
+ if(v > 0xFFFFF || dstlen < 2)
+ {
+ *dstlenp = (origDstlen - dstlen);
+ return JS_FALSE;
+ }
+
+ if(dstlen < 2) goto buffer_too_small;
+
+ if(dst)
+ {
+ *dst++ = (jschar)((v >> 10) + 0xD800);
+ v = (jschar)((v & 0x3FF) + 0xDC00);
+ }
+ dstlen--;
+ }
+ }
+
+ if(!dstlen) goto buffer_too_small;
+ if(dst) *dst++ = (jschar) v;
+
+ dstlen--;
+ offset += n;
+ src += n;
+ srclen -= n;
+ }
+
+ *dstlenp = (origDstlen - dstlen);
+ return JS_TRUE;
+
+bad_character:
+ *dstlenp = (origDstlen - dstlen);
+ return JS_FALSE;
+
+buffer_too_small:
+ *dstlenp = (origDstlen - dstlen);
+ return JS_FALSE;
+}
+
+JSString*
+dec_string(JSContext* cx, const char* bytes, size_t byteslen)
+{
+ JSString* str = NULL;
+ jschar* chars = NULL;
+ size_t charslen;
+
+ if(!dec_charbuf(bytes, byteslen, NULL, &charslen)) goto error;
+
+ chars = JS_malloc(cx, (charslen + 1) * sizeof(jschar));
+ if(!chars) return NULL;
+ chars[charslen] = 0;
+
+ if(!dec_charbuf(bytes, byteslen, chars, &charslen)) goto error;
+
+ str = JS_NewUCString(cx, chars, charslen - 1);
+ if(!str) goto error;
+
+ goto success;
+
+error:
+ if(chars != NULL) JS_free(cx, chars);
+ str = NULL;
+
+success:
+ return str;
+}
http://git-wip-us.apache.org/repos/asf/couchdb/blob/e62a4fc1/apps/couch/c_src/couch_js/utf8.h
----------------------------------------------------------------------
diff --git a/apps/couch/c_src/couch_js/utf8.h b/apps/couch/c_src/couch_js/utf8.h
new file mode 100644
index 0000000..c5cb86c
--- /dev/null
+++ b/apps/couch/c_src/couch_js/utf8.h
@@ -0,0 +1,19 @@
+// Licensed under the Apache License, Version 2.0 (the "License"); you may not
+// use this file except in compliance with the License. You may obtain a copy of
+// the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
+
+#ifndef COUCH_JS_UTF_8_H
+#define COUCH_JS_UTF_8_H
+
+char* enc_string(JSContext* cx, jsval arg, size_t* buflen);
+JSString* dec_string(JSContext* cx, const char* buf, size_t buflen);
+
+#endif
http://git-wip-us.apache.org/repos/asf/couchdb/blob/e62a4fc1/apps/couch/c_src/couch_js/util.c
----------------------------------------------------------------------
diff --git a/apps/couch/c_src/couch_js/util.c b/apps/couch/c_src/couch_js/util.c
new file mode 100644
index 0000000..9b46ceb
--- /dev/null
+++ b/apps/couch/c_src/couch_js/util.c
@@ -0,0 +1,288 @@
+// Licensed under the Apache License, Version 2.0 (the "License"); you may not
+// use this file except in compliance with the License. You may obtain a copy of
+// the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
+
+#include <stdlib.h>
+#include <string.h>
+
+#include <jsapi.h>
+
+#include "help.h"
+#include "util.h"
+#include "utf8.h"
+
+
+size_t
+slurp_file(const char* file, char** outbuf_p)
+{
+ FILE* fp;
+ char fbuf[16384];
+ char *buf = NULL;
+ char* tmp;
+ size_t nread = 0;
+ size_t buflen = 0;
+
+ if(strcmp(file, "-") == 0) {
+ fp = stdin;
+ } else {
+ fp = fopen(file, "r");
+ if(fp == NULL) {
+ fprintf(stderr, "Failed to read file: %s\n", file);
+ exit(3);
+ }
+ }
+
+ while((nread = fread(fbuf, 1, 16384, fp)) > 0) {
+ if(buf == NULL) {
+ buf = (char*) malloc(nread + 1);
+ if(buf == NULL) {
+ fprintf(stderr, "Out of memory.\n");
+ exit(3);
+ }
+ memcpy(buf, fbuf, nread);
+ } else {
+ tmp = (char*) malloc(buflen + nread + 1);
+ if(tmp == NULL) {
+ fprintf(stderr, "Out of memory.\n");
+ exit(3);
+ }
+ memcpy(tmp, buf, buflen);
+ memcpy(tmp+buflen, fbuf, nread);
+ free(buf);
+ buf = tmp;
+ }
+ buflen += nread;
+ buf[buflen] = '\0';
+ }
+ *outbuf_p = buf;
+ return buflen + 1;
+}
+
+couch_args*
+couch_parse_args(int argc, const char* argv[])
+{
+ couch_args* args;
+ int i = 1;
+
+ args = (couch_args*) malloc(sizeof(couch_args));
+ if(args == NULL)
+ return NULL;
+
+ memset(args, '\0', sizeof(couch_args));
+ args->stack_size = 64L * 1024L * 1024L;
+
+ while(i < argc) {
+ if(strcmp("-h", argv[i]) == 0) {
+ DISPLAY_USAGE;
+ exit(0);
+ } else if(strcmp("-V", argv[i]) == 0) {
+ DISPLAY_VERSION;
+ exit(0);
+ } else if(strcmp("-H", argv[i]) == 0) {
+ args->use_http = 1;
+ } else if(strcmp("-S", argv[i]) == 0) {
+ args->stack_size = atoi(argv[++i]);
+ if(args->stack_size <= 0) {
+ fprintf(stderr, "Invalid stack size.\n");
+ exit(2);
+ }
+ } else if(strcmp("-u", argv[i]) == 0) {
+ args->uri_file = argv[++i];
+ } else if(strcmp("--", argv[i]) == 0) {
+ i++;
+ break;
+ } else {
+ break;
+ }
+ i++;
+ }
+
+ if(i >= argc) {
+ DISPLAY_USAGE;
+ exit(3);
+ }
+ args->scripts = argv + i;
+
+ return args;
+}
+
+
+int
+couch_fgets(char* buf, int size, FILE* fp)
+{
+ int n, i, c;
+
+ if(size <= 0) return -1;
+ n = size - 1;
+
+ for(i = 0; i < n && (c = getc(fp)) != EOF; i++) {
+ buf[i] = c;
+ if(c == '\n') {
+ i++;
+ break;
+ }
+ }
+
+ buf[i] = '\0';
+ return i;
+}
+
+
+JSString*
+couch_readline(JSContext* cx, FILE* fp)
+{
+ JSString* str;
+ char* bytes = NULL;
+ char* tmp = NULL;
+ size_t used = 0;
+ size_t byteslen = 256;
+ size_t readlen = 0;
+
+ bytes = JS_malloc(cx, byteslen);
+ if(bytes == NULL) return NULL;
+
+ while((readlen = couch_fgets(bytes+used, byteslen-used, fp)) > 0) {
+ used += readlen;
+
+ if(bytes[used-1] == '\n') {
+ bytes[used-1] = '\0';
+ break;
+ }
+
+ // Double our buffer and read more.
+ byteslen *= 2;
+ tmp = JS_realloc(cx, bytes, byteslen);
+ if(!tmp) {
+ JS_free(cx, bytes);
+ return NULL;
+ }
+
+ bytes = tmp;
+ }
+
+ // Treat empty strings specially
+ if(used == 0) {
+ JS_free(cx, bytes);
+ return JSVAL_TO_STRING(JS_GetEmptyStringValue(cx));
+ }
+
+ // Shring the buffer to the actual data size
+ tmp = JS_realloc(cx, bytes, used);
+ if(!tmp) {
+ JS_free(cx, bytes);
+ return NULL;
+ }
+ bytes = tmp;
+ byteslen = used;
+
+ str = dec_string(cx, bytes, byteslen);
+ JS_free(cx, bytes);
+ return str;
+}
+
+
+JSString*
+couch_readfile(JSContext* cx, const char* filename)
+{
+ JSString *string;
+ size_t byteslen;
+ char *bytes;
+
+ if((byteslen = slurp_file(filename, &bytes))) {
+ string = dec_string(cx, bytes, byteslen);
+
+ free(bytes);
+ return string;
+ }
+ return NULL;
+}
+
+
+void
+couch_print(JSContext* cx, uintN argc, jsval* argv)
+{
+ char *bytes = NULL;
+ FILE *stream = stdout;
+
+ if (argc) {
+ if (argc > 1 && argv[1] == JSVAL_TRUE) {
+ stream = stderr;
+ }
+ bytes = enc_string(cx, argv[0], NULL);
+ if(!bytes) return;
+ fprintf(stream, "%s", bytes);
+ JS_free(cx, bytes);
+ }
+
+ fputc('\n', stream);
+ fflush(stream);
+}
+
+
+void
+couch_error(JSContext* cx, const char* mesg, JSErrorReport* report)
+{
+ jsval v, replace;
+ char* bytes;
+ JSObject* regexp, *stack;
+ jsval re_args[2];
+
+ if(!report || !JSREPORT_IS_WARNING(report->flags))
+ {
+ fprintf(stderr, "%s\n", mesg);
+
+ // Print a stack trace, if available.
+ if (JSREPORT_IS_EXCEPTION(report->flags) &&
+ JS_GetPendingException(cx, &v))
+ {
+ // Clear the exception before an JS method calls or the result is
+ // infinite, recursive error report generation.
+ JS_ClearPendingException(cx);
+
+ // Use JS regexp to indent the stack trace.
+ // If the regexp can't be created, don't JS_ReportError since it is
+ // probably not productive to wind up here again.
+ if(JS_GetProperty(cx, JSVAL_TO_OBJECT(v), "stack", &v) &&
+ (regexp = JS_NewRegExpObjectNoStatics(
+ cx, "^(?=.)", 6, JSREG_GLOB | JSREG_MULTILINE)))
+ {
+ // Set up the arguments to ``String.replace()``
+ re_args[0] = OBJECT_TO_JSVAL(regexp);
+ re_args[1] = STRING_TO_JSVAL(JS_InternString(cx, "\t"));
+
+ // Perform the replacement
+ if(JS_ValueToObject(cx, v, &stack) &&
+ JS_GetProperty(cx, stack, "replace", &replace) &&
+ JS_CallFunctionValue(cx, stack, replace, 2, re_args, &v))
+ {
+ // Print the result
+ bytes = enc_string(cx, v, NULL);
+ fprintf(stderr, "Stacktrace:\n%s", bytes);
+ JS_free(cx, bytes);
+ }
+ }
+ }
+ }
+}
+
+
+JSBool
+couch_load_funcs(JSContext* cx, JSObject* obj, JSFunctionSpec* funcs)
+{
+ JSFunctionSpec* f;
+ for(f = funcs; f->name != NULL; f++) {
+ if(!JS_DefineFunction(cx, obj, f->name, f->call, f->nargs, f->flags)) {
+ fprintf(stderr, "Failed to create function: %s\n", f->name);
+ return JS_FALSE;
+ }
+ }
+ return JS_TRUE;
+}
http://git-wip-us.apache.org/repos/asf/couchdb/blob/e62a4fc1/apps/couch/c_src/couch_js/util.h
----------------------------------------------------------------------
diff --git a/apps/couch/c_src/couch_js/util.h b/apps/couch/c_src/couch_js/util.h
new file mode 100644
index 0000000..65a2a06
--- /dev/null
+++ b/apps/couch/c_src/couch_js/util.h
@@ -0,0 +1,35 @@
+// Licensed under the Apache License, Version 2.0 (the "License"); you may not
+// use this file except in compliance with the License. You may obtain a copy of
+// the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
+
+#ifndef COUCHJS_UTIL_H
+#define COUCHJS_UTIL_H
+
+#include <jsapi.h>
+
+typedef struct {
+ int use_http;
+ int stack_size;
+ const char** scripts;
+ const char* uri_file;
+ JSString* uri;
+} couch_args;
+
+couch_args* couch_parse_args(int argc, const char* argv[]);
+int couch_fgets(char* buf, int size, FILE* fp);
+JSString* couch_readline(JSContext* cx, FILE* fp);
+JSString* couch_readfile(JSContext* cx, const char* filename);
+void couch_print(JSContext* cx, uintN argc, jsval* argv);
+void couch_error(JSContext* cx, const char* mesg, JSErrorReport* report);
+JSBool couch_load_funcs(JSContext* cx, JSObject* obj, JSFunctionSpec* funcs);
+
+
+#endif // Included util.h