-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjsr.h
More file actions
154 lines (122 loc) · 4.34 KB
/
jsr.h
File metadata and controls
154 lines (122 loc) · 4.34 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
//
// Created by Ammar Ahmed on 01/12/2024.
//
#ifndef TEST_APP_JSR_H
#define TEST_APP_JSR_H
#include "v8-api.h"
#include "jsr_common.h"
#include "libplatform/libplatform.h"
#include "SimpleAllocator.h"
#include "JEnv.h"
typedef struct napi_runtime__ *napi_runtime;
class JSR {
public:
JSR();
v8::Isolate* isolate;
static bool s_mainThreadInitialized;
static std::unique_ptr<v8::Platform> platform;
std::recursive_mutex js_mutex;
void lock() {
js_mutex.lock();
}
void unlock() {
js_mutex.unlock();
}
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_);
}
}
private:
napi_env env_;
v8::Locker locker_;
v8::Isolate::Scope isolate_scope_;
v8::Context::Scope context_scope_;
// v8::HandleScope handle_scope_;
napi_handle_scope napiHandleScope_;
};
#define JSEnterScope \
v8::Locker locker(env->isolate); \
v8::HandleScope handle_scope(env->isolate);
namespace tns {
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();
}
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, 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};
}
}
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;
}
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());
}
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 //TEST_APP_JSR_H