summaryrefslogtreecommitdiff
path: root/src/mongo/scripting/engine_spidermonkey.h
blob: 3fe3e586793f23c1586bb838f5d83b1a6d0f02ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// engine_spidermonkey.h

/*    Copyright 2009 10gen Inc.
 *
 *    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.
 */

#pragma once

#include "mongo/scripting/engine.h"

// START inc hacking

#ifdef WIN32
#include <third_party/js-1.7/jstypes.h>
#undef JS_PUBLIC_API
#undef JS_PUBLIC_DATA
#define JS_PUBLIC_API(t)    t __cdecl
#define JS_PUBLIC_DATA(t)   t
#endif

#include <third_party/js-1.7/jsapi.h>

// END inc hacking

// -- SM 1.6 hacks ---
#ifndef JSCLASS_GLOBAL_FLAGS
#error old version of spider monkey ( probably 1.6 ) you should upgrade to at least 1.7
#endif
// -- END SM 1.6 hacks ---

#ifdef JSVAL_IS_TRACEABLE
#define SM18
#endif

#ifdef XULRUNNER
#define SM181
#endif

namespace mongo {
namespace spidermonkey {

    class SMScope;
    class Convertor;

    extern JSClass bson_class;
    extern JSClass bson_ro_class;

    extern JSClass object_id_class;
    extern JSClass dbpointer_class;
    extern JSClass dbref_class;
    extern JSClass bindata_class;
    extern JSClass timestamp_class;
    extern JSClass numberlong_class;
    extern JSClass numberint_class;
    extern JSClass minkey_class;
    extern JSClass maxkey_class;

    // internal things
    inline void dontDeleteScope( SMScope * s ) {}
    void errorReporter( JSContext *cx, const char *message, JSErrorReport *report );
    extern boost::thread_specific_ptr<SMScope> currentScope;

    // bson
    JSBool resolveBSONField( JSContext *cx, JSObject *obj, jsval id, uintN flags, JSObject **objp );

    // mongo
    void initMongoJS( SMScope * scope , JSContext * cx , JSObject * global , bool local );
    bool appendSpecialDBObject( Convertor * c , BSONObjBuilder& b , const string& name , jsval val , JSObject * o );

#define JSVAL_IS_OID(v) ( JSVAL_IS_OBJECT( v ) && JS_InstanceOf( cx , JSVAL_TO_OBJECT( v ) , &object_id_class , 0 ) )

    bool isDate( JSContext * cx , JSObject * o );

    // JS private data must be 2byte aligned, so we use a holder to refer to an unaligned pointer.
    struct BinDataHolder {
        BinDataHolder( const char *c, int copyLen = -1 ) :
            c_( const_cast< char * >( c ) ),
            iFree_( copyLen != -1 ) {
            if ( copyLen != -1 ) {
                c_ = (char*)malloc( copyLen );
                memcpy( c_, c, copyLen );
            }
        }
        ~BinDataHolder() {
            if ( iFree_ )
                free( c_ );
        }
        char *c_;
        bool iFree_;
    };

}  // namespace spidermonkey
}  // namespace mongo