Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add CoffeeScript (http://coffeescript.org/) support to the JS view se…

…rver.

This allows you to write all query server functions in CoffeeScript.

The implementation re-uses the JS query server and adds an additional,
optional compilation step. To make the implementation easier for now,
a new main-coffee.js file is generated that includes the CoffeeScript
compiler, but I think we can do away with that in a future optimisation
and just use a single main.js that can handle both.

The patch includes basic tests for the JS test suite that I tested
in Firefox and Chrome. I'm no expert in CoffeeScript, so I don't
know if more complex script will break the implementation. `make
distcheck` passes.

Futon picks up the "coffeescript" design document lanugage as expected
and view definition interaction works as usual.

List functions don't deal well with the implicit return generated by
CoffeeScript, but J Chris says, this will be easy to fix.

The patch adds CoffeeScript 1.1.1 which is MIT licensed and updates
the NOTICE, LICENSE, license.skip and .gitignore files as well as the
build system accordingly.

git-svn-id: https://svn.apache.org/repos/asf/couchdb/trunk@1129600 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information...
commit 972da5ab71af3eaeb939314a973398b6fe3235a9 1 parent 2cd5139
@janl janl authored
View
1  .gitignore
@@ -83,6 +83,7 @@ test/etap/test_cfg_register
test/etap/test_util.erl
test/javascript/run
share/server/main.js
+share/server/main-coffee.js
# for make dev
View
25 LICENSE
@@ -478,3 +478,28 @@ For the src/snappy/google-snappy component
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+For the share/server/coffee-script.js file
+
+ Copyright (c) 2011 Jeremy Ashkenas
+
+ Permission is hereby granted, free of charge, to any person
+ obtaining a copy of this software and associated documentation
+ files (the "Software"), to deal in the Software without
+ restriction, including without limitation the rights to use,
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the
+ Software is furnished to do so, subject to the following
+ conditions:
+
+ The above copyright notice and this permission notice shall be
+ included in all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ OTHER DEALINGS IN THE SOFTWARE.
View
4 NOTICE
@@ -65,3 +65,7 @@ This product also includes the following third-party components:
* snappy-erlang-nif (https://github.com/fdmanana/snappy-erlang-nif)
Copyright 2011, Filipe David Manana <fdmanana@apache.org>
+
+ * CoffeeScript (http://coffeescript.org/)
+
+ Copyright 2011, Jeremy Ashkenas
View
2  etc/couchdb/default.ini.tpl.in
@@ -59,6 +59,8 @@ auth_cache_size = 50 ; size is number of cache entries
[query_servers]
javascript = %bindir%/%couchjs_command_name% %localbuilddatadir%/server/main.js
+coffeescript = %bindir%/%couchjs_command_name% %localbuilddatadir%/server/main-coffee.js
+
; Changing reduce_limit to false will disable reduce_limit.
; If you think you're hitting reduce_limit with a "good" reduce function,
View
1  license.skip
@@ -58,6 +58,7 @@
^share/Makefile.in
^share/server/json2.js
^share/server/mimeparse.js
+^share/server/coffee-script.js
^share/www/favicon.ico
^share/www/image/*
^share/www/script/jquery.*
View
17 share/Makefile.am
@@ -30,12 +30,23 @@ $(JS_FILE): $(JS_FILE_COMPONENTS) $(JS_FILE_COMPONENTS_LAST)
echo >> $@
cat $^ >> $@
-CLEANFILES = $(JS_FILE)
+COFFEE_FILE = server/main-coffee.js
-EXTRA_DIST = $(JS_FILE_COMPONENTS) $(JS_FILE_COMPONENTS_LAST)
+COFFEE_FILE_COMPONENTS_LAST = server/coffee-script.js server/loop.js
+
+$(COFFEE_FILE): $(JS_FILE_COMPONENTS) $(COFFEE_FILE_COMPONENTS_LAST)
+ mkdir -p `dirname $(COFFEE_FILE)`
+ echo "// DO NOT EDIT THIS FILE BY HAND" > $@
+ echo >> $@
+ cat $^ >> $@
+
+CLEANFILES = $(JS_FILE) $(COFFEE_FILE)
+
+EXTRA_DIST = $(JS_FILE_COMPONENTS) $(JS_FILE_COMPONENTS_LAST) $(COFFEE_FILE_COMPONENTS_LAST)
nobase_localdata_SCRIPTS = \
- $(JS_FILE)
+ $(JS_FILE) \
+ $(COFFEE_FILE)
nobase_dist_localdata_DATA = \
www/config.html \
View
8 share/server/coffee-script.js
8 additions, 0 deletions not shown
View
15 share/server/util.js
@@ -63,6 +63,17 @@ var Couch = {
},
compileFunction : function(source, ddoc) {
if (!source) throw(["error","not_found","missing function"]);
+
+ var evaluate_function_source = function(source, evalFunction, sandbox) {
+ sandbox = sandbox || {};
+ if(typeof CoffeeScript === "undefined") {
+ return evalFunction(source, sandbox);
+ } else {
+ coffee = CoffeeScript.compile(source, {bare: true});
+ return evalFunction(coffee, sandbox);
+ }
+ }
+
try {
if (sandbox) {
if (ddoc) {
@@ -91,9 +102,9 @@ var Couch = {
};
sandbox.require = require;
}
- var functionObject = evalcx(source, sandbox);
+ var functionObject = evaluate_function_source(source, evalcx, sandbox);
} else {
- var functionObject = eval(source);
+ var functionObject = evaluate_function_source(source, eval);
}
} catch (err) {
throw(["error", "compilation_error", err.toSource() + " (" + source + ")"]);
View
1  share/www/script/couch_tests.js
@@ -42,6 +42,7 @@ loadTest("auth_cache.js");
loadTest("batch_save.js");
loadTest("bulk_docs.js");
loadTest("changes.js");
+loadTest("coffee.js");
loadTest("compact.js");
loadTest("config.js");
loadTest("conflicts.js");
View
67 share/www/script/test/coffee.js
@@ -0,0 +1,67 @@
+// 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.
+
+// test basic coffeescript functionality
+couchTests.coffee = function(debug) {
+ var db = new CouchDB("test_suite_db", {"X-Couch-Full-Commit":"false"});
+ db.deleteDb();
+ db.createDb();
+ if (debug) debugger;
+
+ var ddoc = {
+ _id: "_design/coffee",
+ language: "coffeescript",
+ views: {
+ myview: {
+ map: '(doc) -> if doc.foo\n emit(doc.foo, 1)',
+ reduce: '(keys, values, rereduce) ->\n sum = 0\n for x in values\n sum = sum + x\n sum'
+ }
+ },
+ shows: {
+ myshow: '(doc) ->\n "Foo #{doc.foo}"'
+ },
+ lists: {
+ mylist: '(head, req) ->\n while row = getRow()\n send("Foo #{row.value}")\n return "Foo"'
+ },
+ filters: {
+ filter: "(doc) ->\n doc.foo"
+ }
+ };
+
+ db.save(ddoc);
+
+ var docs = [
+ {_id:"a", foo: 100},
+ {foo:1},
+ {foo:1},
+ {foo:2},
+ {foo:2},
+ {bar:1},
+ {bar:1},
+ {bar:2},
+ {bar:2}
+ ];
+
+ db.bulkSave(docs);
+
+ var res = db.view("coffee/myview");
+ TEquals(5, res.rows[0].value, "should sum up values");
+
+ var res = CouchDB.request("GET", "/" + db.name + "/_design/coffee/_show/myshow/a");
+ TEquals("Foo 100", res.responseText, "should show 100");
+
+ var res = CouchDB.request("GET", "/" + db.name + "/_design/coffee/_list/mylist/myview");
+ TEquals("Foo 5Foo", res.responseText, "should list");
+
+ var changes = db.changes({filter: "coffee/filter"});
+ TEquals(5, changes.results.length, "should have changes");
+};
Please sign in to comment.
Something went wrong with that request. Please try again.