summaryrefslogtreecommitdiff
path: root/couchjs
diff options
context:
space:
mode:
authorRobert Newson <robert.newson@cloudant.com>2011-06-13 14:59:55 +0100
committerRobert Newson <robert.newson@cloudant.com>2011-06-13 14:59:55 +0100
commite9a5a6f90a021db1db8a7e55ec797a4c86edcad6 (patch)
tree06d0c28969cfcf54b2f137e7407b097f73aa0f21 /couchjs
parent266ba88ac6ded40087e0211ad9e75e4ce64e66cb (diff)
parent3c1a0d7e2c9adef4f8b20c9df205a86e5c0feefb (diff)
Merge CouchDB 1.1
Diffstat (limited to 'couchjs')
-rw-r--r--couchjs/js/json2.js3
-rw-r--r--couchjs/js/loop.js1
-rw-r--r--couchjs/js/render.js8
-rw-r--r--couchjs/js/state.js7
-rw-r--r--couchjs/js/util.js35
5 files changed, 36 insertions, 18 deletions
diff --git a/couchjs/js/json2.js b/couchjs/js/json2.js
index 39d8f370..a1a3b170 100644
--- a/couchjs/js/json2.js
+++ b/couchjs/js/json2.js
@@ -1,6 +1,6 @@
/*
http://www.JSON.org/json2.js
- 2009-09-29
+ 2010-03-20
Public Domain.
@@ -433,6 +433,7 @@ if (!this.JSON) {
// Unicode characters with escape sequences. JavaScript handles many characters
// incorrectly, either silently deleting them, or treating them as line endings.
+ text = String(text);
cx.lastIndex = 0;
if (cx.test(text)) {
text = text.replace(cx, function (a) {
diff --git a/couchjs/js/loop.js b/couchjs/js/loop.js
index a988684f..d2a07f61 100644
--- a/couchjs/js/loop.js
+++ b/couchjs/js/loop.js
@@ -101,6 +101,7 @@ var Loop = function() {
// "view" : Views.handler,
"reset" : State.reset,
"add_fun" : State.addFun,
+ "add_lib" : State.addLib,
"map_doc" : Views.mapDoc,
"reduce" : Views.reduce,
"rereduce" : Views.rereduce
diff --git a/couchjs/js/render.js b/couchjs/js/render.js
index 9dcfbcd6..d207db41 100644
--- a/couchjs/js/render.js
+++ b/couchjs/js/render.js
@@ -72,7 +72,7 @@ var Mime = (function() {
Mime.responseContentType = null;
};
- function runProvides(req) {
+ function runProvides(req, ddoc) {
var supportedMimes = [], bestFun, bestKey = null, accept = req.headers["Accept"];
if (req.query && req.query.format) {
bestKey = req.query.format;
@@ -103,7 +103,7 @@ var Mime = (function() {
};
if (bestFun) {
- return bestFun();
+ return bestFun.call(ddoc);
} else {
var supportedTypes = mimeFuns.map(function(mf) {return mimesByKey[mf[0]].join(', ') || mf[0]});
throw(["error","not_acceptable",
@@ -233,7 +233,7 @@ var Render = (function() {
}
if (Mime.providesUsed) {
- resp = Mime.runProvides(args[1]);
+ resp = Mime.runProvides(args[1], ddoc);
resp = applyContentType(maybeWrapResponse(resp), Mime.responseContentType);
}
@@ -287,7 +287,7 @@ var Render = (function() {
var tail = listFun.apply(ddoc, args);
if (Mime.providesUsed) {
- tail = Mime.runProvides(req);
+ tail = Mime.runProvides(req, ddoc);
}
if (!gotRow) getRow();
if (typeof tail != "undefined") {
diff --git a/couchjs/js/state.js b/couchjs/js/state.js
index 9af9e475..e6416382 100644
--- a/couchjs/js/state.js
+++ b/couchjs/js/state.js
@@ -14,6 +14,7 @@ var State = {
reset : function(config) {
// clear the globals and run gc
State.funs = [];
+ State.lib = null;
State.query_config = config || {};
init_sandbox();
gc();
@@ -21,7 +22,11 @@ var State = {
},
addFun : function(newFun) {
// Compile to a function and add it to funs array
- State.funs.push(Couch.compileFunction(newFun));
+ State.funs.push(Couch.compileFunction(newFun, {views : {lib : State.lib}}));
+ print("true");
+ },
+ addLib : function(lib) {
+ State.lib = lib;
print("true");
}
}
diff --git a/couchjs/js/util.js b/couchjs/js/util.js
index b55480b9..e4386701 100644
--- a/couchjs/js/util.js
+++ b/couchjs/js/util.js
@@ -31,16 +31,16 @@ var resolveModule = function(names, mod, root) {
}
return resolveModule(names, {
id : mod.id.slice(0, mod.id.lastIndexOf('/')),
- parent : mod.parent.parent.parent,
- current : mod.parent.parent.current
+ parent : mod.parent.parent,
+ current : mod.parent.current
});
} else if (n == '.') {
if (!mod.parent) {
throw ["error", "invalid_require_path", 'Object has no parent '+JSON.stringify(mod.current)];
}
return resolveModule(names, {
- parent : mod.parent.parent,
- current : mod.parent.current,
+ parent : mod.parent,
+ current : mod.current,
id : mod.id
});
} else if (root) {
@@ -66,17 +66,28 @@ var Couch = {
try {
if (sandbox) {
if (ddoc) {
+ if (!ddoc._module_cache) {
+ ddoc._module_cache = {};
+ }
var require = function(name, module) {
module = module || {};
- var newModule = resolveModule(name.split('/'), module, ddoc);
- var s = "function (module, exports, require) { " + newModule.current + " }";
- try {
- var func = sandbox ? evalcx(s, sandbox) : eval(s);
- func.apply(sandbox, [newModule, newModule.exports, function(name) {return require(name, newModule)}]);
- } catch(e) {
- throw ["error","compilation_error","Module require('"+name+"') raised error "+e.toSource()];
+ var newModule = resolveModule(name.split('/'), module.parent, ddoc);
+ if (!ddoc._module_cache.hasOwnProperty(newModule.id)) {
+ // create empty exports object before executing the module,
+ // stops circular requires from filling the stack
+ ddoc._module_cache[newModule.id] = {};
+ var s = "function (module, exports, require) { " + newModule.current + " }";
+ try {
+ var func = sandbox ? evalcx(s, sandbox) : eval(s);
+ func.apply(sandbox, [newModule, newModule.exports, function(name) {
+ return require(name, newModule);
+ }]);
+ } catch(e) {
+ throw ["error","compilation_error","Module require('"+name+"') raised error "+e.toSource()];
+ }
+ ddoc._module_cache[newModule.id] = newModule.exports;
}
- return newModule.exports;
+ return ddoc._module_cache[newModule.id];
}
sandbox.require = require;
}