Skip to content

Commit c450010

Browse files
committed
fix compile issue for v8 version 7.4~13.0
1 parent 19a639c commit c450010

File tree

6 files changed

+192
-94
lines changed

6 files changed

+192
-94
lines changed

backend/V8/V8Engine.cc

Lines changed: 72 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,9 @@ Local<Value> V8Engine::eval(const Local<String>& script, const Local<Value>& sou
158158
throw Exception("can't eval script");
159159
}
160160
v8::ScriptOrigin origin(
161-
#if SCRIPTX_V8_VERSION_GE(9, 0)
161+
#if SCRIPTX_V8_VERSION_BETWEEN(9, 0, 12, 0)
162162
// V8 9.0 add isolate param for external API
163+
// V8 12.1 deprecated the isolate version, and introduced the one without isolation
163164
isolate_,
164165
#endif
165166
sourceFile.isNull() || !sourceFile.isString() ? v8::Local<v8::String>()
@@ -180,17 +181,18 @@ Local<Value> V8Engine::eval(const Local<String>& script) { return eval(script, {
180181
void V8Engine::registerNativeClassStatic(v8::Local<v8::FunctionTemplate> funcT,
181182
const internal::StaticDefine* staticDefine) {
182183
for (auto& prop : staticDefine->properties) {
184+
using PropDefPtr = internal::StaticDefine::PropertyDefine*;
185+
183186
StackFrameScope stack;
184187
auto name = String::newString(prop.name);
185188

186-
v8::AccessorGetterCallback getter = nullptr;
187-
v8::AccessorSetterCallback setter = nullptr;
189+
v8::AccessorNameGetterCallback getter = nullptr;
190+
v8::AccessorNameSetterCallback setter = nullptr;
188191

189192
if (prop.getter) {
190-
getter = [](v8::Local<v8::String> /*property*/,
193+
getter = [](v8::Local<v8::Name> /*property*/,
191194
const v8::PropertyCallbackInfo<v8::Value>& info) {
192-
auto ptr = static_cast<internal::StaticDefine::PropertyDefine*>(
193-
info.Data().As<v8::External>()->Value());
195+
auto ptr = static_cast<PropDefPtr>(info.Data().As<v8::External>()->Value());
194196
Tracer trace(EngineScope::currentEngine(), ptr->traceName);
195197
Local<Value> ret = ptr->getter();
196198
try {
@@ -202,10 +204,9 @@ void V8Engine::registerNativeClassStatic(v8::Local<v8::FunctionTemplate> funcT,
202204
}
203205

204206
if (prop.setter) {
205-
setter = [](v8::Local<v8::String> /*property*/, v8::Local<v8::Value> value,
207+
setter = [](v8::Local<v8::Name> /*property*/, v8::Local<v8::Value> value,
206208
const v8::PropertyCallbackInfo<void>& info) {
207-
auto ptr = static_cast<internal::StaticDefine::PropertyDefine*>(
208-
info.Data().As<v8::External>()->Value());
209+
auto ptr = static_cast<PropDefPtr>(info.Data().As<v8::External>()->Value());
209210
Tracer trace(EngineScope::currentEngine(), ptr->traceName);
210211
try {
211212
ptr->setter(make<Local<Value>>(value));
@@ -216,25 +217,26 @@ void V8Engine::registerNativeClassStatic(v8::Local<v8::FunctionTemplate> funcT,
216217
} else {
217218
// v8 requires setter to be present, otherwise, a real js set code with create a new
218219
// property...
219-
setter = [](v8::Local<v8::String> property, v8::Local<v8::Value> value,
220+
setter = [](v8::Local<v8::Name> property, v8::Local<v8::Value> value,
220221
const v8::PropertyCallbackInfo<void>& info) {};
221222
}
222223

223-
funcT->SetNativeDataProperty(
224-
toV8(isolate_, name), getter, setter,
225-
v8::External::New(isolate_, const_cast<internal::StaticDefine::PropertyDefine*>(&prop)),
226-
v8::PropertyAttribute::DontDelete);
224+
// SetNativeDataProperty with Local<String> and AccessControl is deprecated
225+
funcT->SetNativeDataProperty(v8::Local<v8::Name>::Cast(toV8(isolate_, name)), getter, setter,
226+
v8::External::New(isolate_, const_cast<PropDefPtr>(&prop)),
227+
v8::PropertyAttribute::DontDelete);
227228
}
228229

229230
for (auto& func : staticDefine->functions) {
231+
using FuncDefPtr = internal::StaticDefine::FunctionDefine*;
232+
230233
StackFrameScope stack;
231234
auto name = String::newString(func.name);
232235

233236
auto fn = v8::FunctionTemplate::New(
234237
isolate_,
235238
[](const v8::FunctionCallbackInfo<v8::Value>& info) {
236-
auto funcDef = reinterpret_cast<internal::StaticDefine::FunctionDefine*>(
237-
info.Data().As<v8::External>()->Value());
239+
auto funcDef = reinterpret_cast<FuncDefPtr>(info.Data().As<v8::External>()->Value());
238240
auto engine = v8_backend::currentEngine();
239241
Tracer trace(engine, funcDef->traceName);
240242

@@ -245,8 +247,8 @@ void V8Engine::registerNativeClassStatic(v8::Local<v8::FunctionTemplate> funcT,
245247
v8_backend::rethrowException(e);
246248
}
247249
},
248-
v8::External::New(isolate_, const_cast<internal::StaticDefine::FunctionDefine*>(&func)), {},
249-
0, v8::ConstructorBehavior::kThrow);
250+
v8::External::New(isolate_, const_cast<FuncDefPtr>(&func)), {}, 0,
251+
v8::ConstructorBehavior::kThrow);
250252
if (!fn.IsEmpty()) {
251253
funcT->Set(toV8(isolate_, name), fn, v8::PropertyAttribute::DontDelete);
252254
} else {
@@ -453,67 +455,67 @@ void V8Engine::registerNativeClassInstance(v8::Local<v8::FunctionTemplate> funcT
453455
// instance
454456
auto instanceT = funcT->PrototypeTemplate();
455457
auto signature = v8::Signature::New(isolate_, funcT);
458+
456459
for (auto& prop : classDefine->instanceDefine.properties) {
460+
// Template::SetAccessor is removed in 12.8
461+
// using Template::SetAccessorProperty is recommended
462+
463+
using PropDefPtr = typename internal::InstanceDefine::PropertyDefine*;
457464
StackFrameScope stack;
458465
auto name = String::newString(prop.name);
459-
460-
v8::AccessorGetterCallback getter = nullptr;
461-
v8::AccessorSetterCallback setter = nullptr;
466+
auto data = v8::External::New(isolate_, const_cast<PropDefPtr>(&prop));
467+
v8::Local<v8::FunctionTemplate> getter;
468+
v8::Local<v8::FunctionTemplate> setter;
462469

463470
if (prop.getter) {
464-
getter = [](v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& info) {
465-
auto ptr = static_cast<decltype(&prop)>(info.Data().As<v8::External>()->Value());
466-
auto thiz = static_cast<void*>(info.This()->GetAlignedPointerFromInternalField(
467-
kInstanceObjectAlignedPointer_PolymorphicPointer));
468-
auto scriptClass =
469-
static_cast<ScriptClass*>(info.This()->GetAlignedPointerFromInternalField(
470-
kInstanceObjectAlignedPointer_ScriptClass));
471-
auto& getter = ptr->getter;
472-
473-
Tracer trace(scriptClass->getScriptEngine(), ptr->traceName);
474-
475-
Local<Value> ret = (getter)(thiz);
476-
try {
477-
info.GetReturnValue().Set(toV8(info.GetIsolate(), ret));
478-
} catch (const Exception& e) {
479-
v8_backend::rethrowException(e);
480-
}
481-
};
471+
getter = v8::FunctionTemplate::New(
472+
isolate_,
473+
[](const v8::FunctionCallbackInfo<v8::Value>& info) {
474+
auto ptr = static_cast<PropDefPtr>(info.Data().As<v8::External>()->Value());
475+
auto thiz = static_cast<void*>(info.This()->GetAlignedPointerFromInternalField(
476+
kInstanceObjectAlignedPointer_PolymorphicPointer));
477+
auto scriptClass =
478+
static_cast<ScriptClass*>(info.This()->GetAlignedPointerFromInternalField(
479+
kInstanceObjectAlignedPointer_ScriptClass));
480+
auto& getter = ptr->getter;
481+
482+
Tracer trace(scriptClass->getScriptEngine(), ptr->traceName);
483+
484+
Local<Value> ret = (getter)(thiz);
485+
try {
486+
info.GetReturnValue().Set(toV8(info.GetIsolate(), ret));
487+
} catch (const Exception& e) {
488+
v8_backend::rethrowException(e);
489+
}
490+
},
491+
data, signature);
482492
}
483493

484494
if (prop.setter) {
485-
setter = [](v8::Local<v8::String> property, v8::Local<v8::Value> value,
486-
const v8::PropertyCallbackInfo<void>& info) {
487-
auto ptr = static_cast<decltype(&prop)>(info.Data().As<v8::External>()->Value());
488-
auto thiz = static_cast<void*>(info.This()->GetAlignedPointerFromInternalField(
489-
kInstanceObjectAlignedPointer_PolymorphicPointer));
490-
auto scriptClass =
491-
static_cast<ScriptClass*>(info.This()->GetAlignedPointerFromInternalField(
492-
kInstanceObjectAlignedPointer_ScriptClass));
493-
auto& setter = ptr->setter;
494-
495-
Tracer trace(scriptClass->getScriptEngine(), ptr->traceName);
496-
497-
try {
498-
(setter)(thiz, make<Local<Value>>(value));
499-
} catch (const Exception& e) {
500-
v8_backend::rethrowException(e);
501-
}
502-
};
495+
setter = v8::FunctionTemplate::New(
496+
isolate_,
497+
[](const v8::FunctionCallbackInfo<v8::Value>& info) {
498+
auto ptr = static_cast<PropDefPtr>(info.Data().As<v8::External>()->Value());
499+
auto thiz = static_cast<void*>(info.This()->GetAlignedPointerFromInternalField(
500+
kInstanceObjectAlignedPointer_PolymorphicPointer));
501+
auto scriptClass =
502+
static_cast<ScriptClass*>(info.This()->GetAlignedPointerFromInternalField(
503+
kInstanceObjectAlignedPointer_ScriptClass));
504+
auto& setter = ptr->setter;
505+
506+
Tracer trace(scriptClass->getScriptEngine(), ptr->traceName);
507+
508+
try {
509+
(setter)(thiz, make<Local<Value>>(info[0]));
510+
} catch (const Exception& e) {
511+
v8_backend::rethrowException(e);
512+
}
513+
},
514+
data, signature);
503515
}
504516

505-
auto v8Name = toV8(isolate_, name);
506-
auto data = v8::External::New(
507-
isolate_, const_cast<typename internal::InstanceDefine::PropertyDefine*>(&prop));
508-
509-
#if SCRIPTX_V8_VERSION_LE(10, 1) // SetAccessor AccessorSignature deprecated in 10.2 a8beac
510-
auto accessSignature = v8::AccessorSignature::New(isolate_, funcT);
511-
instanceT->SetAccessor(v8Name, getter, setter, data, v8::AccessControl::DEFAULT,
512-
v8::PropertyAttribute::DontDelete, accessSignature);
513-
#else
514-
instanceT->SetAccessor(v8Name, getter, setter, data, v8::AccessControl::DEFAULT,
515-
v8::PropertyAttribute::DontDelete);
516-
#endif
517+
instanceT->SetAccessorProperty(v8::Local<v8::Name>::Cast(toV8(isolate_, name)), getter, setter,
518+
v8::PropertyAttribute::DontDelete);
517519
}
518520

519521
for (auto& func : classDefine->instanceDefine.functions) {
@@ -541,7 +543,7 @@ void V8Engine::registerNativeClassInstance(v8::Local<v8::FunctionTemplate> funcT
541543
},
542544
v8::External::New(isolate_, const_cast<FuncDefPtr>(&func)), signature);
543545
if (!fn.IsEmpty()) {
544-
funcT->PrototypeTemplate()->Set(toV8(isolate_, name), fn, v8::PropertyAttribute::DontDelete);
546+
instanceT->Set(toV8(isolate_, name), fn, v8::PropertyAttribute::DontDelete);
545547
} else {
546548
throw Exception("can't create function for instance");
547549
}

backend/V8/V8Platform.cc

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
*/
1717

1818
#include "V8Platform.h"
19-
#include <libplatform/libplatform.h>
2019
#include <type_traits>
20+
#include <utility>
2121
#include "../../src/Utils.h"
2222
#include "V8Engine.h"
2323
#include "V8Helper.hpp"
@@ -52,6 +52,39 @@ class MessageQueueTaskRunner : public v8::TaskRunner {
5252

5353
void setEngine(V8Engine* engine) { engine_ = engine; }
5454

55+
#if SCRIPTX_V8_VERSION_GE(12, 4) // changed to PostTaskImpl
56+
protected:
57+
void PostTaskImpl(std::unique_ptr<v8::Task> task, const v8::SourceLocation& location) override {
58+
defaultTaskRunner_->PostTask(std::move(task));
59+
schedulePump();
60+
}
61+
62+
void PostDelayedTaskImpl(std::unique_ptr<v8::Task> task, double delay_in_seconds,
63+
const v8::SourceLocation& location) override {
64+
defaultTaskRunner_->PostDelayedTask(std::move(task), delay_in_seconds);
65+
schedulePump(delay_in_seconds);
66+
}
67+
68+
void PostIdleTaskImpl(std::unique_ptr<v8::IdleTask> task,
69+
const v8::SourceLocation& location) override {
70+
defaultTaskRunner_->PostIdleTask(std::move(task));
71+
schedulePump();
72+
}
73+
74+
void PostNonNestableTaskImpl(std::unique_ptr<v8::Task> task,
75+
const v8::SourceLocation& location) override {
76+
defaultTaskRunner_->PostNonNestableTask(std::move(task));
77+
schedulePump();
78+
}
79+
80+
void PostNonNestableDelayedTaskImpl(std::unique_ptr<v8::Task> task, double delay_in_seconds,
81+
const v8::SourceLocation& location) override {
82+
defaultTaskRunner_->PostNonNestableDelayedTask(std::move(task), delay_in_seconds);
83+
schedulePump(delay_in_seconds);
84+
}
85+
86+
public:
87+
#else
5588
void PostTask(std::unique_ptr<v8::Task> task) override {
5689
defaultTaskRunner_->PostTask(std::move(task));
5790
schedulePump();
@@ -67,12 +100,13 @@ class MessageQueueTaskRunner : public v8::TaskRunner {
67100
schedulePump();
68101
}
69102

70-
bool IdleTasksEnabled() override { return defaultTaskRunner_->IdleTasksEnabled(); }
71-
72103
void PostNonNestableTask(std::unique_ptr<v8::Task> task) override {
73104
defaultTaskRunner_->PostNonNestableTask(std::move(task));
74105
schedulePump();
75106
}
107+
#endif
108+
109+
bool IdleTasksEnabled() override { return defaultTaskRunner_->IdleTasksEnabled(); }
76110

77111
bool NonNestableTasksEnabled() const override {
78112
return defaultTaskRunner_->NonNestableTasksEnabled();
@@ -183,7 +217,12 @@ V8Platform::~V8Platform() {
183217
#endif
184218
}
185219

220+
#if SCRIPTX_V8_VERSION_GE(13, 0)
221+
std::shared_ptr<v8::TaskRunner> V8Platform::GetForegroundTaskRunner(v8::Isolate* isolate,
222+
v8::TaskPriority priority) {
223+
#else
186224
std::shared_ptr<v8::TaskRunner> V8Platform::GetForegroundTaskRunner(v8::Isolate* isolate) {
225+
#endif
187226
std::lock_guard<std::mutex> lock(lock_);
188227
auto queueRunner = engineMap_[isolate].messageQueueRunner;
189228
if (!queueRunner->hasRunner()) {

backend/V8/V8Platform.h

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "../../src/foundation.h"
2424

2525
SCRIPTX_BEGIN_INCLUDE_LIBRARY
26+
#include <libplatform/libplatform.h>
2627
#include <v8-platform.h>
2728
SCRIPTX_END_INCLUDE_LIBRARY
2829

@@ -44,7 +45,12 @@ class V8Platform : public v8::Platform {
4445

4546
public:
4647
// this method is used in v8 internally, we should handle it properly, since V8 7.1.1
48+
#if SCRIPTX_V8_VERSION_GE(13, 0)
49+
std::shared_ptr<v8::TaskRunner> GetForegroundTaskRunner(v8::Isolate* isolate,
50+
v8::TaskPriority priority) override;
51+
#else
4752
std::shared_ptr<v8::TaskRunner> GetForegroundTaskRunner(v8::Isolate* isolate) override;
53+
#endif
4854

4955
// call in V8Engine
5056
bool pumpMessageQueue(v8::Isolate* isolate);
@@ -57,17 +63,18 @@ class V8Platform : public v8::Platform {
5763
bool OnCriticalMemoryPressure(size_t length) override;
5864
#endif
5965

60-
public:
6166
// directly delegate to default platform
6267
int NumberOfWorkerThreads() override { return defaultPlatform_->NumberOfWorkerThreads(); }
6368

69+
#if SCRIPTX_V8_VERSION_LE(11, 3) // added default impl to call PostTaskOnWorkerThreadImpl in 11.4
6470
void CallOnWorkerThread(std::unique_ptr<v8::Task> task) override {
6571
return defaultPlatform_->CallOnWorkerThread(std::move(task));
6672
}
6773

6874
void CallDelayedOnWorkerThread(std::unique_ptr<v8::Task> task, double delay_in_seconds) override {
6975
return defaultPlatform_->CallDelayedOnWorkerThread(std::move(task), delay_in_seconds);
7076
}
77+
#endif
7178

7279
double MonotonicallyIncreasingTime() override {
7380
return defaultPlatform_->MonotonicallyIncreasingTime();
@@ -86,27 +93,50 @@ class V8Platform : public v8::Platform {
8693
// NOTE: not available in node 14.x (node.js modified v8 code...)
8794
// https://nodejs.org/en/download/releases/
8895
// and node 15.x uses v8 8.6+
89-
#if defined(BUILDING_NODE_EXTENSION) ? SCRIPTX_V8_VERSION_GE(8, 6) : SCRIPTX_V8_VERSION_GE(8, 4)
90-
96+
// V8 12.2 make it none-virtual by delegate to CreateJobImpl
97+
#if defined(BUILDING_NODE_EXTENSION) ? SCRIPTX_V8_VERSION_BETWEEN(8, 6, 12, 1) \
98+
: SCRIPTX_V8_VERSION_BETWEEN(8, 4, 12, 1)
9199
virtual std::unique_ptr<v8::JobHandle> PostJob(v8::TaskPriority priority,
92100
std::unique_ptr<v8::JobTask> job_task) override {
93101
return defaultPlatform_->PostJob(priority, std::move(job_task));
94102
}
95103

96104
#endif
97105

98-
#if SCRIPTX_V8_VERSION_GE(10, 5) // added in 10.5 1e0d18
106+
#if SCRIPTX_V8_VERSION_BETWEEN(10, 5, 11, 3)
107+
// added pure-virtual in 10.5 1e0d18
108+
// added default impl to CreateJobImpl in 11.4
99109
std::unique_ptr<v8::JobHandle> CreateJob(v8::TaskPriority priority,
100110
std::unique_ptr<v8::JobTask> job_task) override {
101111
return defaultPlatform_->CreateJob(priority, std::move(job_task));
102112
}
103113
#endif
104114

105-
#if SCRIPTX_V8_VERSION_BETWEEN(7, 9, 8, 1)
106-
// v8 7.9 added pure virtual function
107-
// v8 8.0 added default impl
108-
// v8 8.2 removed
115+
#if SCRIPTX_V8_VERSION_GE(11, 4)
116+
protected:
117+
virtual std::unique_ptr<v8::JobHandle> CreateJobImpl(
118+
v8::TaskPriority priority, std::unique_ptr<v8::JobTask> job_task,
119+
const v8::SourceLocation& location) override {
120+
return defaultPlatform_->CreateJob(priority, std::move(job_task));
121+
}
122+
123+
virtual void PostTaskOnWorkerThreadImpl(v8::TaskPriority priority, std::unique_ptr<v8::Task> task,
124+
const v8::SourceLocation& location) override {
125+
defaultPlatform_->CallOnWorkerThread(std::move(task)); // TODO
126+
}
127+
128+
virtual void PostDelayedTaskOnWorkerThreadImpl(v8::TaskPriority priority,
129+
std::unique_ptr<v8::Task> task,
130+
double delay_in_seconds,
131+
const v8::SourceLocation& location) override {
132+
// TODO
133+
defaultPlatform_->CallDelayedOnWorkerThread(std::move(task), delay_in_seconds);
134+
}
135+
136+
public:
137+
#endif
109138

139+
#if SCRIPTX_V8_VERSION_LE(8, 0) // removed in 8.1
110140
void CallOnForegroundThread(v8::Isolate* isolate, v8::Task* task) override {
111141
return GetForegroundTaskRunner(isolate)->PostTask(std::unique_ptr<v8::Task>(task));
112142
}

0 commit comments

Comments
 (0)