1 
2 /**
3  *    Copyright (C) 2018-present MongoDB, Inc.
4  *
5  *    This program is free software: you can redistribute it and/or modify
6  *    it under the terms of the Server Side Public License, version 1,
7  *    as published by MongoDB, Inc.
8  *
9  *    This program is distributed in the hope that it will be useful,
10  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *    Server Side Public License for more details.
13  *
14  *    You should have received a copy of the Server Side Public License
15  *    along with this program. If not, see
16  *    <http://www.mongodb.com/licensing/server-side-public-license>.
17  *
18  *    As a special exception, the copyright holders give permission to link the
19  *    code of portions of this program with the OpenSSL library under certain
20  *    conditions as described in each individual source file and distribute
21  *    linked combinations including the program with the OpenSSL library. You
22  *    must comply with the Server Side Public License in all respects for
23  *    all of the code used other than as permitted herein. If you modify file(s)
24  *    with this exception, you may extend this exception to your version of the
25  *    file(s), but you are not obligated to do so. If you do not wish to do so,
26  *    delete this exception statement from your version. If you delete this
27  *    exception statement from all source files in the program, then also delete
28  *    it in the license file.
29  */
30 
31 #pragma once
32 
33 #include <jsapi.h>
34 
35 #include "mongo/scripting/deadline_monitor.h"
36 #include "mongo/scripting/engine.h"
37 #include "mongo/stdx/mutex.h"
38 #include "mongo/stdx/unordered_map.h"
39 #include "mongo/util/concurrency/mutex.h"
40 
41 namespace mongo {
42 namespace mozjs {
43 
44 class MozJSImplScope;
45 
46 /**
47  * Implements the global ScriptEngine interface for MozJS.  The associated TU
48  * pulls this in for the polymorphic globalScriptEngine.
49  */
50 class MozJSScriptEngine final : public mongo::ScriptEngine {
51 public:
52     MozJSScriptEngine();
53     ~MozJSScriptEngine() override;
54 
55     mongo::Scope* createScope() override;
56     mongo::Scope* createScopeForCurrentThread() override;
57 
runTest()58     void runTest() override {}
59 
utf8Ok()60     bool utf8Ok() const override {
61         return true;
62     }
63 
64     void interrupt(unsigned opId) override;
65 
66     void interruptAll() override;
67 
68     void enableJIT(bool value) override;
69     bool isJITEnabled() const override;
70 
71     void enableJavaScriptProtection(bool value) override;
72     bool isJavaScriptProtectionEnabled() const override;
73 
74     int getJSHeapLimitMB() const override;
75     void setJSHeapLimitMB(int limit) override;
76 
77     void registerOperation(OperationContext* ctx, MozJSImplScope* scope);
78     void unregisterOperation(unsigned int opId);
79 
80     using ScopeCallback = void (*)(Scope&);
getScopeInitCallback()81     ScopeCallback getScopeInitCallback() {
82         return _scopeInitCallback;
83     };
84 
getDeadlineMonitor()85     DeadlineMonitor<MozJSImplScope>& getDeadlineMonitor() {
86         return _deadlineMonitor;
87     }
88 
89 private:
90     std::string printKnownOps_inlock();
91 
92     /**
93      * This mutex protects _opToScopeMap
94      */
95     stdx::mutex _globalInterruptLock;
96 
97     using OpIdToScopeMap = stdx::unordered_map<unsigned, MozJSImplScope*>;
98     OpIdToScopeMap _opToScopeMap;  // map of mongo op ids to scopes (protected by
99                                    // _globalInterruptLock).
100 
101     DeadlineMonitor<MozJSImplScope> _deadlineMonitor;
102 };
103 
104 }  // namespace mozjs
105 }  // namespace mongo
106