On Wed, Apr 8, 2009 at 8:49 AM, Yohei Sasaki <[email protected]> wrote: > Hi all, > > I came across a problem in my view script with the "instanceof" operation. > Can anyone explain the cause of my problem duplicated by following steps? > > === > 1. Add a document with an Array member into a database. > > { > "_id": "oh_my_array", > "_rev": "2-3397837489", > "array_member": [ > 1, > 2, > 3 > ] > } > > 2. Run the following map-only view. > > function(doc) { > emit("0_raw_data", doc.array_member); > emit("1_instanceof", (doc.array_member instanceof Array)); > emit("2_constructor.name", doc.array_member.constructor.name); > emit("3_typeof", (typeof doc.array_member)); > } > > 3. Get the strange value in "1_instanceof" in Futon. > > (key, value) -> > ("0_raw_data", [1,2,3]) > ("1_instanceof", false); <-- why not true? > ("2_constructor.name", "Array"); > ("3_typeof", "object"); > === > # As above, "constructor.name" is an workaround instead of instanceof. > > So I don't know why "instanceof" operation returns false for the array > value. > # This means we cannot use the instanceof operation for condtion expressions > such as if( a instanceof Array){ ... } > > BTW, I use CouchDB 0.9.0 (installed from the tarball on Apache) with JS > 1.7(installed from MacPorts). > And on couchjs standalone mode, the expression, 'instanceof Array' > returns true as follows: > > === > mac:~ yssk22$ cat a.js > var doc = { > "_id": "oh_my_array", > "_rev": "2-3397837489", > "array_member": [ > 1, > 2, > 3 > ] > }; > print(doc.array_member instanceof Array); > > mac:~ yssk22$ /usr/local/bin/couchjs a.js > true > === > > Thanks. > > -- > Yohei SASAKI > http://github.com/yssk22/ >
My first guess is that this is an artifact of the sandbox that view functions run in. Since the JS_Context* that is used for running the functions is never initialized with JS_InitStandardClasses the Array class isn't defined and as such you can't test if the object is an instance. IIRC, JS_InitStandardClasses has a fairly pricey runtime cost associated with it so I doubt calling it on all sand box contexts would be the way to go. Perhaps with some refactoring of couchjs to make sure that its called optionally, and only once per view update wouldn't be out of the question. Patches welcome. HTH, Paul Davis
