Merge 'remotes/trunk'
[0ad.git] / source / scriptinterface / ScriptRuntime.h
blob2f1e3496071bac3fe276098cca4f913753e590a1
1 /* Copyright (C) 2016 Wildfire Games.
2 * This file is part of 0 A.D.
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * 0 A.D. 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 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
18 #ifndef INCLUDED_SCRIPTRUNTIME
19 #define INCLUDED_SCRIPTRUNTIME
21 #include <sstream>
23 #include "ScriptTypes.h"
24 #include "ScriptExtraHeaders.h"
26 #define STACK_CHUNK_SIZE 8192
28 /**
29 * Abstraction around a SpiderMonkey JSRuntime.
30 * Each ScriptRuntime can be used to initialize several ScriptInterface
31 * contexts which can then share data, but a single ScriptRuntime should
32 * only be used on a single thread.
34 * (One means to share data between threads and runtimes is to create
35 * a ScriptInterface::StructuredClone.)
38 class ScriptRuntime
40 public:
41 ScriptRuntime(shared_ptr<ScriptRuntime> parentRuntime, int runtimeSize, int heapGrowthBytesGCTrigger);
42 ~ScriptRuntime();
44 /**
45 * MaybeIncrementalRuntimeGC tries to determine whether a runtime-wide garbage collection would free up enough memory to
46 * be worth the amount of time it would take. It does this with our own logic and NOT some predefined JSAPI logic because
47 * such functionality currently isn't available out of the box.
48 * It does incremental GC which means it will collect one slice each time it's called until the garbage collection is done.
49 * This can and should be called quite regularly. The delay parameter allows you to specify a minimum time since the last GC
50 * in seconds (the delay should be a fraction of a second in most cases though).
51 * It will only start a new incremental GC or another GC slice if this time is exceeded. The user of this function is
52 * responsible for ensuring that GC can run with a small enough delay to get done with the work.
54 void MaybeIncrementalGC(double delay);
55 void ShrinkingGC();
57 void RegisterContext(JSContext* cx);
58 void UnRegisterContext(JSContext* cx);
60 /**
61 * Registers an object to be freed/finalized by the ScriptRuntime. Freeing is
62 * guaranteed to happen after the next minor GC has completed, but might also
63 * happen a bit later. This is only needed in very special situations
64 * and you should only use it if you know exactly why you need it!
65 * Specify a deleter for the shared_ptr to free the void pointer correctly
66 * (by casting to the right type before calling delete for example).
68 void AddDeferredFinalizationObject(const std::shared_ptr<void>& obj);
70 JSRuntime* m_rt;
72 private:
74 void PrepareContextsForIncrementalGC();
75 void GCCallbackMember();
77 std::list<JSContext*> m_Contexts;
78 std::vector<std::shared_ptr<void> > m_FinalizationListObjectIdCache;
80 int m_RuntimeSize;
81 int m_HeapGrowthBytesGCTrigger;
82 int m_LastGCBytes;
83 double m_LastGCCheck;
85 static void GCCallback(JSRuntime *rt, JSGCStatus status, void *data);
88 #endif // INCLUDED_SCRIPTRUNTIME