You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by ja...@apache.org on 2011/05/31 12:08:59 UTC

svn commit: r1129600 - in /couchdb/trunk: .gitignore LICENSE NOTICE etc/couchdb/default.ini.tpl.in license.skip share/Makefile.am share/server/coffee-script.js share/server/util.js share/www/script/couch_tests.js share/www/script/test/coffee.js

Author: jan
Date: Tue May 31 10:08:58 2011
New Revision: 1129600

URL: http://svn.apache.org/viewvc?rev=1129600&view=rev
Log:
Add CoffeeScript (http://coffeescript.org/) support to the JS view server.

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.

Added:
    couchdb/trunk/share/server/coffee-script.js
    couchdb/trunk/share/www/script/test/coffee.js
Modified:
    couchdb/trunk/.gitignore
    couchdb/trunk/LICENSE
    couchdb/trunk/NOTICE
    couchdb/trunk/etc/couchdb/default.ini.tpl.in
    couchdb/trunk/license.skip
    couchdb/trunk/share/Makefile.am
    couchdb/trunk/share/server/util.js
    couchdb/trunk/share/www/script/couch_tests.js

Modified: couchdb/trunk/.gitignore
URL: http://svn.apache.org/viewvc/couchdb/trunk/.gitignore?rev=1129600&r1=1129599&r2=1129600&view=diff
==============================================================================
--- couchdb/trunk/.gitignore (original)
+++ couchdb/trunk/.gitignore Tue May 31 10:08:58 2011
@@ -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
 

Modified: couchdb/trunk/LICENSE
URL: http://svn.apache.org/viewvc/couchdb/trunk/LICENSE?rev=1129600&r1=1129599&r2=1129600&view=diff
==============================================================================
--- couchdb/trunk/LICENSE (original)
+++ couchdb/trunk/LICENSE Tue May 31 10:08:58 2011
@@ -478,3 +478,28 @@ For the src/snappy/google-snappy compone
  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.

Modified: couchdb/trunk/NOTICE
URL: http://svn.apache.org/viewvc/couchdb/trunk/NOTICE?rev=1129600&r1=1129599&r2=1129600&view=diff
==============================================================================
--- couchdb/trunk/NOTICE (original)
+++ couchdb/trunk/NOTICE Tue May 31 10:08:58 2011
@@ -65,3 +65,7 @@ This product also includes the following
  * snappy-erlang-nif (https://github.com/fdmanana/snappy-erlang-nif)
 
   Copyright 2011, Filipe David Manana <fd...@apache.org>
+
+ * CoffeeScript (http://coffeescript.org/)
+
+  Copyright 2011, Jeremy Ashkenas

Modified: couchdb/trunk/etc/couchdb/default.ini.tpl.in
URL: http://svn.apache.org/viewvc/couchdb/trunk/etc/couchdb/default.ini.tpl.in?rev=1129600&r1=1129599&r2=1129600&view=diff
==============================================================================
--- couchdb/trunk/etc/couchdb/default.ini.tpl.in (original)
+++ couchdb/trunk/etc/couchdb/default.ini.tpl.in Tue May 31 10:08:58 2011
@@ -59,6 +59,8 @@ auth_cache_size = 50 ; size is number of
 
 [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,

Modified: couchdb/trunk/license.skip
URL: http://svn.apache.org/viewvc/couchdb/trunk/license.skip?rev=1129600&r1=1129599&r2=1129600&view=diff
==============================================================================
--- couchdb/trunk/license.skip (original)
+++ couchdb/trunk/license.skip Tue May 31 10:08:58 2011
@@ -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.*

Modified: couchdb/trunk/share/Makefile.am
URL: http://svn.apache.org/viewvc/couchdb/trunk/share/Makefile.am?rev=1129600&r1=1129599&r2=1129600&view=diff
==============================================================================
--- couchdb/trunk/share/Makefile.am (original)
+++ couchdb/trunk/share/Makefile.am Tue May 31 10:08:58 2011
@@ -30,12 +30,23 @@ $(JS_FILE): $(JS_FILE_COMPONENTS) $(JS_F
 	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 \

Added: couchdb/trunk/share/server/coffee-script.js
URL: http://svn.apache.org/viewvc/couchdb/trunk/share/server/coffee-script.js?rev=1129600&view=auto
==============================================================================
--- couchdb/trunk/share/server/coffee-script.js (added)
+++ couchdb/trunk/share/server/coffee-script.js Tue May 31 10:08:58 2011
@@ -0,0 +1,8 @@
+/**
+ * CoffeeScript Compiler v1.1.1
+ * http://coffeescript.org
+ *
+ * Copyright 2011, Jeremy Ashkenas
+ * Released under the MIT License
+ */

[... 3 lines stripped ...]
Modified: couchdb/trunk/share/server/util.js
URL: http://svn.apache.org/viewvc/couchdb/trunk/share/server/util.js?rev=1129600&r1=1129599&r2=1129600&view=diff
==============================================================================
--- couchdb/trunk/share/server/util.js (original)
+++ couchdb/trunk/share/server/util.js Tue May 31 10:08:58 2011
@@ -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 + ")"]);

Modified: couchdb/trunk/share/www/script/couch_tests.js
URL: http://svn.apache.org/viewvc/couchdb/trunk/share/www/script/couch_tests.js?rev=1129600&r1=1129599&r2=1129600&view=diff
==============================================================================
--- couchdb/trunk/share/www/script/couch_tests.js [utf-8] (original)
+++ couchdb/trunk/share/www/script/couch_tests.js [utf-8] Tue May 31 10:08:58 2011
@@ -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");

Added: couchdb/trunk/share/www/script/test/coffee.js
URL: http://svn.apache.org/viewvc/couchdb/trunk/share/www/script/test/coffee.js?rev=1129600&view=auto
==============================================================================
--- couchdb/trunk/share/www/script/test/coffee.js (added)
+++ couchdb/trunk/share/www/script/test/coffee.js Tue May 31 10:08:58 2011
@@ -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");
+};