couchdb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jason Smith <>
Subject Re: [jira] [Commented] (COUCHDB-1397) Function expressions, evals in SpiderMonkey
Date Mon, 06 Feb 2012 01:04:31 GMT
Hi, Marcello and Alexander. Perhaps we can move this to the mailing
list and off the JIRA ticket?

On Sun, Feb 5, 2012 at 11:21 PM, Marcello Nuccio (Commented) (JIRA)
<> wrote:
>    [
> Marcello Nuccio commented on COUCHDB-1397:
> ------------------------------------------
> @Alexander,
> this isn't as off-topic as it seems at first, because software design is heavily influenced
by ease of testability.

Firstly, I'm unsure if you noticed but I made a typo which gave my
statement the opposite meaning.

I agree that emit(), log(), etc. should NOT be global.

However, I disagree that they should be passed to map(), simply
because it burdens the programmer. Plus, if we add new functions in
the future, it would change the call signature of map functions. On
the other hand, we get the same benefit by passing the functions but
using a closure to put them in scope of map(), reduce(), etc.

The map function might be built like so (the example is imperfect but
I'm aiming for clarity for now):

Given: = "module.exports = function(doc) {
emit(, 1) }"

var make_map = Function("require, module, exports, emit, log",
var map = make_map(require, module, exports, emit, log)

map({name:"Bob"}) // Runs emit("Bob", 1)

## Unit testing

It's imperfect for unit testing; however I think a hypothetical NPM
package would be fine. It could convert the ddoc functions to
JavaScript functions in a similar fashion. (Below is the first API
that popped into my head, no doubt we could make better.)

var assert = require('assert')
  , couch = require('mock-couchdb')

var ddoc = couch.load_ddoc({views: {people: {map: "function(doc) {
emit(, 1) }"}}})

var emitted
ddoc.on('emit', function(key, val) { emitted = key }){name:"Bob"})
assert.equal(emitted, "Bob")

Iris Couch

View raw message