Shaka Player Embedded
js_engine.h
Go to the documentation of this file.
1 // Copyright 2016 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef SHAKA_EMBEDDED_MAPPING_JS_ENGINE_H_
16 #define SHAKA_EMBEDDED_MAPPING_JS_ENGINE_H_
17 
18 #include <glog/logging.h>
19 
20 #include <functional>
21 #include <memory>
22 #include <thread>
23 #include <unordered_map>
24 
28 
29 namespace shaka {
30 
38 class JsEngine : public PseudoSingleton<JsEngine> {
39  public:
40  JsEngine();
41  ~JsEngine();
42 
43  Handle<JsObject> global_handle();
44  ReturnVal<JsValue> global_value();
45 
46 #if defined(USING_V8)
47  void OnPromiseReject(v8::PromiseRejectMessage message);
48  void AddDestructor(void* object, std::function<void(void*)> destruct);
49  v8::Isolate* isolate() const {
50  // Verify this thread can use the isolate.
51  DCHECK(isolate_);
52  DCHECK(v8::Locker::IsLocked(isolate_));
53  return isolate_;
54  }
55 #elif defined(USING_JSC)
56  JSContextRef context() const;
57 #endif // USING_JSC
58 
59  struct SetupContext {
60  SetupContext();
61  ~SetupContext();
62 
63  private:
64 #if defined(USING_V8)
65  v8::Locker locker;
66  v8::HandleScope handles;
67  v8::Isolate::Scope isolate_scope;
68  v8::Context::Scope context_scope;
69 #endif
70  };
71 
72  private:
73 #if defined(USING_V8)
74  class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
75  public:
76  void* Allocate(size_t length) override;
77  void* AllocateUninitialized(size_t length) override;
78  void Free(void* data, size_t) override;
79  };
80 
81  v8::Isolate* CreateIsolate();
82  v8::Global<v8::Context> CreateContext();
83 
84  ArrayBufferAllocator allocator_;
85  std::unordered_map<void*, std::function<void(void*)>> destructors_;
86  v8::Isolate* isolate_;
87  v8::Global<v8::Context> context_;
88 #elif defined(USING_JSC)
89  JSGlobalContextRef context_;
90  std::thread::id thread_id_;
91 #endif
92 
93  RejectedPromiseHandler promise_handler_;
94 };
95 
96 } // namespace shaka
97 
98 #endif // SHAKA_EMBEDDED_MAPPING_JS_ENGINE_H_
Handle< JsObject > global_handle()
ReturnVal< JsValue > global_value()
const char * message