Repository navigation
Expand file tree
/
Copy pathjsr.h
More file actions
205 lines (170 loc) · 5.37 KB
/
Copy pathjsr.h
File metadata and controls
205 lines (170 loc) · 5.37 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
//
// Created by Ammar Ahmed on 01/12/2024.
//
#ifndef TEST_APP_JSR_H
#define TEST_APP_JSR_H
#include <memory>
#ifdef __ANDROID__
#include "JEnv.h"
#endif
#include "SimpleAllocator.h"
#include "jsr_common.h"
#include "libplatform/libplatform.h"
#include "v8-api.h"
class JSR {
public:
JSR();
v8::Isolate* isolate;
static std::unique_ptr<v8::Platform> platform;
std::recursive_mutex js_mutex;
void lock() { js_mutex.lock(); }
void unlock() { js_mutex.unlock(); }
static JSR* ForEnv(napi_env env);
static void RegisterEnv(napi_env env, JSR* runtime);
static void UnregisterEnv(napi_env env);
private:
static std::once_flag initialization_once;
static std::mutex env_cache_mutex;
static std::unordered_map<napi_env, JSR*> env_to_jsr_cache;
};
class NapiScope {
public:
explicit NapiScope(napi_env env, bool openHandle = true)
: env_(env),
locker_(env->isolate),
isolate_scope_(env->isolate),
context_scope_(env->context())
// handle_scope_(env->isolate)
{
if (openHandle) {
napi_open_handle_scope(env_, &napiHandleScope_);
} else {
napiHandleScope_ = nullptr;
}
}
~NapiScope() {
if (napiHandleScope_) {
napi_close_handle_scope(env_, napiHandleScope_);
}
}
public:
napi_env env_;
v8::Locker locker_;
v8::Isolate::Scope isolate_scope_;
v8::Context::Scope context_scope_;
// v8::HandleScope handle_scope_;
napi_handle_scope napiHandleScope_;
};
class NapiEscapableScope {
public:
explicit NapiEscapableScope(napi_env env)
: env_(env),
locker_(env->isolate),
isolate_scope_(env->isolate),
context_scope_(env->context()),
handle_scope_(env->isolate) {
napi_open_escapable_handle_scope(env_, &napiHandleScope_);
}
~NapiEscapableScope() {
napi_close_escapable_handle_scope(env_, napiHandleScope_);
}
napi_env env_;
v8::Locker locker_;
v8::Isolate::Scope isolate_scope_;
v8::Context::Scope context_scope_;
v8::HandleScope handle_scope_;
napi_escapable_handle_scope napiHandleScope_;
};
#define JSEnterScope \
v8::Locker locker(env->isolate); \
v8::HandleScope handle_scope(env->isolate);
namespace tns {
#ifdef __ANDROID__
inline static v8::Local<v8::String> ConvertToV8String(v8::Isolate* isolate,
const jchar* data,
int length) {
return v8::String::NewFromTwoByte(isolate, (const uint16_t*)data,
v8::NewStringType::kNormal, length)
.ToLocalChecked();
}
#endif
inline static v8::Local<v8::String> ConvertToV8String(v8::Isolate* isolate,
const std::string& s) {
return v8::String::NewFromUtf8(isolate, s.c_str(), v8::NewStringType::kNormal,
static_cast<int>(s.length()))
.ToLocalChecked();
}
inline static v8::Local<v8::String> ConvertToV8String(v8::Isolate* isolate,
const char* data,
int length) {
return v8::String::NewFromUtf8(isolate, (const char*)data,
v8::NewStringType::kNormal, length)
.ToLocalChecked();
}
inline static v8::Local<v8::String> ConvertToV8UTF16String(
v8::Isolate* isolate, const std::u16string& utf16string) {
return v8::String::NewFromTwoByte(isolate,
((const uint16_t*)utf16string.data()))
.ToLocalChecked();
}
inline static v8::Local<v8::String> ToV8String(v8::Isolate* isolate,
const std::string& value) {
return v8::String::NewFromUtf8(isolate, value.c_str(),
v8::NewStringType::kNormal,
(int)value.length())
.ToLocalChecked();
}
inline static std::string ConvertToString(const v8::Local<v8::String>& s) {
if (s.IsEmpty()) {
return {};
} else {
auto isolate = v8::Isolate::GetCurrent();
v8::String::Utf8Value str(isolate, s);
return {*str};
}
}
#ifdef __ANDROID__
static v8::Local<v8::Value> jstringToV8String(v8::Isolate* isolate,
jstring value) {
if (value == nullptr) {
return Null(isolate);
}
JEnv env;
auto chars = env.GetStringChars(value, NULL);
auto length = env.GetStringLength(value);
auto v8String = tns::ConvertToV8String(isolate, chars, length);
env.ReleaseStringChars(value, chars);
return v8String;
}
#endif
inline static std::string ToString(v8::Isolate* isolate,
const v8::Local<v8::Value>& value) {
if (value.IsEmpty()) {
return std::string();
}
if (value->IsStringObject()) {
v8::Local<v8::String> obj = value.As<v8::StringObject>()->ValueOf();
return ToString(isolate, obj);
}
v8::String::Utf8Value result(isolate, value);
const char* val = *result;
if (val == nullptr) {
return std::string();
}
return std::string(*result, result.length());
}
#ifdef __ANDROID__
static std::string jstringToString(jstring value) {
if (value == nullptr) {
return {};
}
JEnv env;
jboolean f = JNI_FALSE;
auto chars = env.GetStringUTFChars(value, &f);
std::string s(chars);
env.ReleaseStringUTFChars(value, chars);
return s;
}
#endif
} // namespace tns
#endif // TEST_APP_JSR_H