summaryrefslogtreecommitdiff
path: root/scripting/engine_spidermonkey.h
blob: 4617b5d878f4d91d576ed9c4ade2e45d2ca3a1c6 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// 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 "engine.h"

// START inc hacking

#if defined( MOZJS )

#define MOZILLA_1_8_BRANCH

#include "mozjs/jsapi.h"
#include "mozjs/jsdate.h"
#include "mozjs/jsregexp.h"

#warning if you are using an ubuntu version of spider monkey, we recommend installing spider monkey from source

#elif defined( OLDJS )

#ifdef WIN32
#include "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 "jsapi.h"
#include "jsobj.h"
#include "jsdate.h"
#include "jsregexp.h"

#else

#include "js/jsapi.h"
#include "js/jsobj.h"
#include "js/jsdate.h"
#include "js/jsregexp.h"

#endif

// END inc hacking

// -- SM 1.6 hacks ---
#ifndef JSCLASS_GLOBAL_FLAGS

#warning old version of spider monkey ( probably 1.6 ) you should upgrade to at least 1.7

#define JSCLASS_GLOBAL_FLAGS 0

JSBool JS_CStringsAreUTF8(){
    return false;
}

#define SM16

#endif
// -- END SM 1.6 hacks ---

#ifdef JSVAL_IS_TRACEABLE
#define SM18
#endif

#ifdef XULRUNNER
#define SM181
#endif

namespace mongo {

    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 minkey_class;
    extern JSClass maxkey_class;

    // internal things
    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_;
    };
}