1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2  * vim: set ts=8 sts=2 et sw=2 tw=80:
3  *
4  * Copyright 2016 Mozilla Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #ifndef wasm_realm_h
20 #define wasm_realm_h
21 
22 #include "wasm/WasmJS.h"
23 
24 namespace js {
25 namespace wasm {
26 
27 // wasm::Realm lives in JS::Realm and contains the wasm-related per-realm state.
28 // wasm::Realm tracks every live instance in the realm and must be notified, via
29 // registerInstance(), of any new WasmInstanceObject.
30 
31 class Realm {
32   JSRuntime* runtime_;
33   InstanceVector instances_;
34 
35  public:
36   explicit Realm(JSRuntime* rt);
37   ~Realm();
38 
39   // Before a WasmInstanceObject can be considered fully constructed and
40   // valid, it must be registered with the Realm. If this method fails,
41   // an error has been reported and the instance object must be abandoned.
42   // After a successful registration, an Instance must call
43   // unregisterInstance() before being destroyed.
44 
45   bool registerInstance(JSContext* cx, HandleWasmInstanceObject instanceObj);
46   void unregisterInstance(Instance& instance);
47 
48   // Return a vector of all live instances in the realm. The lifetime of
49   // these Instances is determined by their owning WasmInstanceObject.
50   // Note that accessing instances()[i]->object() triggers a read barrier
51   // since instances() is effectively a weak list.
52 
instances()53   const InstanceVector& instances() const { return instances_; }
54 
55   // Ensure all Instances in this Realm have profiling labels created.
56 
57   void ensureProfilingLabels(bool profilingEnabled);
58 
59   // about:memory reporting
60 
61   void addSizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf,
62                               size_t* realmTables);
63 };
64 
65 // Interrupt all running wasm Instances that have been registered with
66 // wasm::Realms in the given JSContext.
67 
68 extern void InterruptRunningCode(JSContext* cx);
69 
70 // After a wasm Instance sees an interrupt request and calls
71 // CheckForInterrupt(), it should call RunningCodeInterrupted() to clear the
72 // interrupt request for all wasm Instances to avoid spurious trapping.
73 
74 void ResetInterruptState(JSContext* cx);
75 
76 }  // namespace wasm
77 }  // namespace js
78 
79 #endif  // wasm_realm_h
80