Repository navigation
Expand file tree
/
Copy pathscript_state.cc
More file actions
57 lines (49 loc) · 1.47 KB
/
Copy pathscript_state.cc
File metadata and controls
57 lines (49 loc) · 1.47 KB
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
/*
* Copyright (C) 2019-2022 The Kraken authors. All rights reserved.
* Copyright (C) 2022-present The WebF authors. All rights reserved.
*/
#include "script_state.h"
#include "event_factory.h"
#include "html_element_factory.h"
#include "names_installer.h"
namespace webf {
JSRuntime* runtime_ = nullptr;
std::atomic<int32_t> runningContexts{0};
ScriptState::ScriptState() {
runningContexts++;
bool first_loaded = false;
if (runtime_ == nullptr) {
runtime_ = JS_NewRuntime();
first_loaded = true;
}
// Avoid stack overflow when running in multiple threads.
JS_UpdateStackTop(runtime_);
ctx_ = JS_NewContext(runtime_);
if (first_loaded) {
names_installer::Init(ctx_);
// Bump up the built-in classId. To make sure the created classId are larger than JS_CLASS_CUSTOM_CLASS_INIT_COUNT.
for (int i = 0; i < JS_CLASS_CUSTOM_CLASS_INIT_COUNT - JS_CLASS_GC_TRACKER + 2; i++) {
JSClassID id{0};
JS_NewClassID(&id);
}
}
}
JSRuntime* ScriptState::runtime() {
return runtime_;
}
ScriptState::~ScriptState() {
ctx_invalid_ = true;
JS_FreeContext(ctx_);
// Run GC to clean up remaining objects about m_ctx;
JS_RunGC(runtime_);
if (--runningContexts == 0) {
// Prebuilt strings stored in JSRuntime. Only needs to dispose when runtime disposed.
names_installer::Dispose();
HTMLElementFactory::Dispose();
EventFactory::Dispose();
JS_FreeRuntime(runtime_);
runtime_ = nullptr;
}
ctx_ = nullptr;
}
} // namespace webf