libv8 6.2.414.42.1-universal-darwin-16 → 8.4.255.0-universal-darwin-16
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/ext/libv8/paths.rb +5 -18
- data/lib/libv8/version.rb +1 -1
- data/vendor/v8/include/cppgc/allocation.h +124 -0
- data/vendor/v8/include/cppgc/garbage-collected.h +192 -0
- data/vendor/v8/include/cppgc/heap.h +50 -0
- data/vendor/v8/include/cppgc/internal/accessors.h +26 -0
- data/vendor/v8/include/cppgc/internal/api-constants.h +44 -0
- data/vendor/v8/include/cppgc/internal/compiler-specific.h +26 -0
- data/vendor/v8/include/cppgc/internal/finalizer-trait.h +90 -0
- data/vendor/v8/include/cppgc/internal/gc-info.h +43 -0
- data/vendor/v8/include/cppgc/internal/logging.h +50 -0
- data/vendor/v8/include/cppgc/internal/persistent-node.h +109 -0
- data/vendor/v8/include/cppgc/internal/pointer-policies.h +133 -0
- data/vendor/v8/include/cppgc/internal/prefinalizer-handler.h +31 -0
- data/vendor/v8/include/cppgc/liveness-broker.h +50 -0
- data/vendor/v8/include/cppgc/macros.h +26 -0
- data/vendor/v8/include/cppgc/member.h +206 -0
- data/vendor/v8/include/cppgc/persistent.h +304 -0
- data/vendor/v8/include/cppgc/platform.h +31 -0
- data/vendor/v8/include/cppgc/prefinalizer.h +54 -0
- data/vendor/v8/include/cppgc/source-location.h +59 -0
- data/vendor/v8/include/cppgc/trace-trait.h +67 -0
- data/vendor/v8/include/cppgc/type-traits.h +109 -0
- data/vendor/v8/include/cppgc/visitor.h +137 -0
- data/vendor/v8/include/libplatform/libplatform.h +15 -13
- data/vendor/v8/include/libplatform/v8-tracing.h +69 -21
- data/vendor/v8/include/v8-fast-api-calls.h +412 -0
- data/vendor/v8/include/v8-inspector-protocol.h +4 -4
- data/vendor/v8/include/v8-inspector.h +95 -35
- data/vendor/v8/include/v8-internal.h +389 -0
- data/vendor/v8/include/v8-platform.h +388 -52
- data/vendor/v8/include/v8-profiler.h +363 -213
- data/vendor/v8/include/v8-util.h +33 -36
- data/vendor/v8/include/v8-version-string.h +10 -5
- data/vendor/v8/include/v8-version.h +4 -4
- data/vendor/v8/include/v8-wasm-trap-handler-posix.h +31 -0
- data/vendor/v8/include/v8-wasm-trap-handler-win.h +28 -0
- data/vendor/v8/include/v8.h +3566 -1925
- data/vendor/v8/include/v8config.h +162 -114
- data/vendor/v8/out.gn/libv8/obj/libv8_libbase.a +0 -0
- data/vendor/v8/out.gn/libv8/obj/libv8_libplatform.a +0 -0
- data/vendor/v8/out.gn/libv8/obj/libv8_monolith.a +0 -0
- data/vendor/v8/out.gn/libv8/obj/third_party/icu/libicui18n.a +0 -0
- data/vendor/v8/out.gn/libv8/obj/third_party/icu/libicuuc.a +0 -0
- data/vendor/v8/out.gn/libv8/obj/third_party/zlib/google/libcompression_utils_portable.a +0 -0
- data/vendor/v8/out.gn/libv8/obj/third_party/zlib/libchrome_zlib.a +0 -0
- metadata +51 -30
- data/ext/libv8/arch.rb +0 -20
- data/vendor/v8/include/v8-debug.h +0 -255
- data/vendor/v8/include/v8-testing.h +0 -48
- data/vendor/v8/out/x64.release/libv8_base.a +0 -0
- data/vendor/v8/out/x64.release/libv8_builtins_generators.a +0 -0
- data/vendor/v8/out/x64.release/libv8_builtins_setup.a +0 -0
- data/vendor/v8/out/x64.release/libv8_libbase.a +0 -0
- data/vendor/v8/out/x64.release/libv8_libplatform.a +0 -0
- data/vendor/v8/out/x64.release/libv8_libsampler.a +0 -0
- data/vendor/v8/out/x64.release/libv8_nosnapshot.a +0 -0
- data/vendor/v8/out/x64.release/libv8_snapshot.a +0 -0
@@ -5,9 +5,12 @@
|
|
5
5
|
#ifndef V8_LIBPLATFORM_LIBPLATFORM_H_
|
6
6
|
#define V8_LIBPLATFORM_LIBPLATFORM_H_
|
7
7
|
|
8
|
+
#include <memory>
|
9
|
+
|
8
10
|
#include "libplatform/libplatform-export.h"
|
9
11
|
#include "libplatform/v8-tracing.h"
|
10
|
-
#include "v8-platform.h" // NOLINT(build/
|
12
|
+
#include "v8-platform.h" // NOLINT(build/include_directory)
|
13
|
+
#include "v8config.h" // NOLINT(build/include_directory)
|
11
14
|
|
12
15
|
namespace v8 {
|
13
16
|
namespace platform {
|
@@ -33,34 +36,33 @@ enum class MessageLoopBehavior : bool {
|
|
33
36
|
* If |tracing_controller| is nullptr, the default platform will create a
|
34
37
|
* v8::platform::TracingController instance and use it.
|
35
38
|
*/
|
36
|
-
V8_PLATFORM_EXPORT v8::Platform
|
39
|
+
V8_PLATFORM_EXPORT std::unique_ptr<v8::Platform> NewDefaultPlatform(
|
37
40
|
int thread_pool_size = 0,
|
38
41
|
IdleTaskSupport idle_task_support = IdleTaskSupport::kDisabled,
|
39
42
|
InProcessStackDumping in_process_stack_dumping =
|
40
|
-
InProcessStackDumping::
|
41
|
-
v8::TracingController
|
43
|
+
InProcessStackDumping::kDisabled,
|
44
|
+
std::unique_ptr<v8::TracingController> tracing_controller = {});
|
42
45
|
|
43
46
|
/**
|
44
47
|
* Pumps the message loop for the given isolate.
|
45
48
|
*
|
46
49
|
* The caller has to make sure that this is called from the right thread.
|
47
|
-
* Returns true if a task was executed, and false otherwise.
|
48
|
-
*
|
49
|
-
*
|
50
|
+
* Returns true if a task was executed, and false otherwise. If the call to
|
51
|
+
* PumpMessageLoop is nested within another call to PumpMessageLoop, only
|
52
|
+
* nestable tasks may run. Otherwise, any task may run. Unless requested through
|
53
|
+
* the |behavior| parameter, this call does not block if no task is pending. The
|
54
|
+
* |platform| has to be created using |NewDefaultPlatform|.
|
50
55
|
*/
|
51
56
|
V8_PLATFORM_EXPORT bool PumpMessageLoop(
|
52
57
|
v8::Platform* platform, v8::Isolate* isolate,
|
53
58
|
MessageLoopBehavior behavior = MessageLoopBehavior::kDoNotWait);
|
54
59
|
|
55
|
-
V8_PLATFORM_EXPORT void EnsureEventLoopInitialized(v8::Platform* platform,
|
56
|
-
v8::Isolate* isolate);
|
57
|
-
|
58
60
|
/**
|
59
61
|
* Runs pending idle tasks for at most |idle_time_in_seconds| seconds.
|
60
62
|
*
|
61
63
|
* The caller has to make sure that this is called from the right thread.
|
62
64
|
* This call does not block if no task is pending. The |platform| has to be
|
63
|
-
* created using |
|
65
|
+
* created using |NewDefaultPlatform|.
|
64
66
|
*/
|
65
67
|
V8_PLATFORM_EXPORT void RunIdleTasks(v8::Platform* platform,
|
66
68
|
v8::Isolate* isolate,
|
@@ -69,10 +71,10 @@ V8_PLATFORM_EXPORT void RunIdleTasks(v8::Platform* platform,
|
|
69
71
|
/**
|
70
72
|
* Attempts to set the tracing controller for the given platform.
|
71
73
|
*
|
72
|
-
* The |platform| has to be created using |
|
74
|
+
* The |platform| has to be created using |NewDefaultPlatform|.
|
73
75
|
*
|
74
|
-
* DEPRECATED: Will be removed soon.
|
75
76
|
*/
|
77
|
+
V8_DEPRECATE_SOON("Access the DefaultPlatform directly")
|
76
78
|
V8_PLATFORM_EXPORT void SetTracingController(
|
77
79
|
v8::Platform* platform,
|
78
80
|
v8::platform::tracing::TracingController* tracing_controller);
|
@@ -5,13 +5,21 @@
|
|
5
5
|
#ifndef V8_LIBPLATFORM_V8_TRACING_H_
|
6
6
|
#define V8_LIBPLATFORM_V8_TRACING_H_
|
7
7
|
|
8
|
+
#include <atomic>
|
8
9
|
#include <fstream>
|
9
10
|
#include <memory>
|
10
11
|
#include <unordered_set>
|
11
12
|
#include <vector>
|
12
13
|
|
13
14
|
#include "libplatform/libplatform-export.h"
|
14
|
-
#include "v8-platform.h" // NOLINT(build/
|
15
|
+
#include "v8-platform.h" // NOLINT(build/include_directory)
|
16
|
+
|
17
|
+
namespace perfetto {
|
18
|
+
namespace trace_processor {
|
19
|
+
class TraceProcessorStorage;
|
20
|
+
}
|
21
|
+
class TracingSession;
|
22
|
+
}
|
15
23
|
|
16
24
|
namespace v8 {
|
17
25
|
|
@@ -22,12 +30,14 @@ class Mutex;
|
|
22
30
|
namespace platform {
|
23
31
|
namespace tracing {
|
24
32
|
|
33
|
+
class TraceEventListener;
|
34
|
+
|
25
35
|
const int kTraceMaxNumArgs = 2;
|
26
36
|
|
27
37
|
class V8_PLATFORM_EXPORT TraceObject {
|
28
38
|
public:
|
29
39
|
union ArgValue {
|
30
|
-
bool as_bool;
|
40
|
+
V8_DEPRECATED("use as_uint ? true : false") bool as_bool;
|
31
41
|
uint64_t as_uint;
|
32
42
|
int64_t as_int;
|
33
43
|
double as_double;
|
@@ -35,7 +45,7 @@ class V8_PLATFORM_EXPORT TraceObject {
|
|
35
45
|
const char* as_string;
|
36
46
|
};
|
37
47
|
|
38
|
-
TraceObject()
|
48
|
+
TraceObject() = default;
|
39
49
|
~TraceObject();
|
40
50
|
void Initialize(
|
41
51
|
char phase, const uint8_t* category_enabled_flag, const char* name,
|
@@ -43,8 +53,8 @@ class V8_PLATFORM_EXPORT TraceObject {
|
|
43
53
|
const char** arg_names, const uint8_t* arg_types,
|
44
54
|
const uint64_t* arg_values,
|
45
55
|
std::unique_ptr<v8::ConvertableToTraceFormat>* arg_convertables,
|
46
|
-
unsigned int flags);
|
47
|
-
void UpdateDuration();
|
56
|
+
unsigned int flags, int64_t timestamp, int64_t cpu_timestamp);
|
57
|
+
void UpdateDuration(int64_t timestamp, int64_t cpu_timestamp);
|
48
58
|
void InitializeForTesting(
|
49
59
|
char phase, const uint8_t* category_enabled_flag, const char* name,
|
50
60
|
const char* scope, uint64_t id, uint64_t bind_id, int num_args,
|
@@ -106,12 +116,14 @@ class V8_PLATFORM_EXPORT TraceObject {
|
|
106
116
|
|
107
117
|
class V8_PLATFORM_EXPORT TraceWriter {
|
108
118
|
public:
|
109
|
-
TraceWriter()
|
110
|
-
virtual ~TraceWriter()
|
119
|
+
TraceWriter() = default;
|
120
|
+
virtual ~TraceWriter() = default;
|
111
121
|
virtual void AppendTraceEvent(TraceObject* trace_event) = 0;
|
112
122
|
virtual void Flush() = 0;
|
113
123
|
|
114
124
|
static TraceWriter* CreateJSONTraceWriter(std::ostream& stream);
|
125
|
+
static TraceWriter* CreateJSONTraceWriter(std::ostream& stream,
|
126
|
+
const std::string& tag);
|
115
127
|
|
116
128
|
private:
|
117
129
|
// Disallow copy and assign
|
@@ -145,8 +157,8 @@ class V8_PLATFORM_EXPORT TraceBufferChunk {
|
|
145
157
|
|
146
158
|
class V8_PLATFORM_EXPORT TraceBuffer {
|
147
159
|
public:
|
148
|
-
TraceBuffer()
|
149
|
-
virtual ~TraceBuffer()
|
160
|
+
TraceBuffer() = default;
|
161
|
+
virtual ~TraceBuffer() = default;
|
150
162
|
|
151
163
|
virtual TraceObject* AddTraceEvent(uint64_t* handle) = 0;
|
152
164
|
virtual TraceObject* GetEventByHandle(uint64_t handle) = 0;
|
@@ -187,6 +199,9 @@ class V8_PLATFORM_EXPORT TraceConfig {
|
|
187
199
|
|
188
200
|
TraceConfig() : enable_systrace_(false), enable_argument_filter_(false) {}
|
189
201
|
TraceRecordMode GetTraceRecordMode() const { return record_mode_; }
|
202
|
+
const StringList& GetEnabledCategories() const {
|
203
|
+
return included_categories_;
|
204
|
+
}
|
190
205
|
bool IsSystraceEnabled() const { return enable_systrace_; }
|
191
206
|
bool IsArgumentFilterEnabled() const { return enable_argument_filter_; }
|
192
207
|
|
@@ -219,12 +234,21 @@ class V8_PLATFORM_EXPORT TraceConfig {
|
|
219
234
|
class V8_PLATFORM_EXPORT TracingController
|
220
235
|
: public V8_PLATFORM_NON_EXPORTED_BASE(v8::TracingController) {
|
221
236
|
public:
|
222
|
-
|
237
|
+
TracingController();
|
238
|
+
~TracingController() override;
|
223
239
|
|
224
|
-
|
225
|
-
//
|
226
|
-
//
|
227
|
-
|
240
|
+
#if defined(V8_USE_PERFETTO)
|
241
|
+
// Must be called before StartTracing() if V8_USE_PERFETTO is true. Provides
|
242
|
+
// the output stream for the JSON trace data.
|
243
|
+
void InitializeForPerfetto(std::ostream* output_stream);
|
244
|
+
// Provide an optional listener for testing that will receive trace events.
|
245
|
+
// Must be called before StartTracing().
|
246
|
+
void SetTraceEventListenerForTesting(TraceEventListener* listener);
|
247
|
+
#else // defined(V8_USE_PERFETTO)
|
248
|
+
// The pointer returned from GetCategoryGroupEnabled() points to a value with
|
249
|
+
// zero or more of the following bits. Used in this class only. The
|
250
|
+
// TRACE_EVENT macros should only use the value as a bool. These values must
|
251
|
+
// be in sync with macro values in TraceEvent.h in Blink.
|
228
252
|
enum CategoryGroupEnabledFlags {
|
229
253
|
// Category group enabled for the recording mode.
|
230
254
|
ENABLED_FOR_RECORDING = 1 << 0,
|
@@ -234,8 +258,7 @@ class V8_PLATFORM_EXPORT TracingController
|
|
234
258
|
ENABLED_FOR_ETW_EXPORT = 1 << 3
|
235
259
|
};
|
236
260
|
|
237
|
-
|
238
|
-
~TracingController() override;
|
261
|
+
// Takes ownership of |trace_buffer|.
|
239
262
|
void Initialize(TraceBuffer* trace_buffer);
|
240
263
|
|
241
264
|
// v8::TracingController implementation.
|
@@ -247,8 +270,19 @@ class V8_PLATFORM_EXPORT TracingController
|
|
247
270
|
const uint64_t* arg_values,
|
248
271
|
std::unique_ptr<v8::ConvertableToTraceFormat>* arg_convertables,
|
249
272
|
unsigned int flags) override;
|
273
|
+
uint64_t AddTraceEventWithTimestamp(
|
274
|
+
char phase, const uint8_t* category_enabled_flag, const char* name,
|
275
|
+
const char* scope, uint64_t id, uint64_t bind_id, int32_t num_args,
|
276
|
+
const char** arg_names, const uint8_t* arg_types,
|
277
|
+
const uint64_t* arg_values,
|
278
|
+
std::unique_ptr<v8::ConvertableToTraceFormat>* arg_convertables,
|
279
|
+
unsigned int flags, int64_t timestamp) override;
|
250
280
|
void UpdateTraceEventDuration(const uint8_t* category_enabled_flag,
|
251
281
|
const char* name, uint64_t handle) override;
|
282
|
+
|
283
|
+
static const char* GetCategoryGroupName(const uint8_t* category_enabled_flag);
|
284
|
+
#endif // !defined(V8_USE_PERFETTO)
|
285
|
+
|
252
286
|
void AddTraceStateObserver(
|
253
287
|
v8::TracingController::TraceStateObserver* observer) override;
|
254
288
|
void RemoveTraceStateObserver(
|
@@ -257,18 +291,32 @@ class V8_PLATFORM_EXPORT TracingController
|
|
257
291
|
void StartTracing(TraceConfig* trace_config);
|
258
292
|
void StopTracing();
|
259
293
|
|
260
|
-
|
294
|
+
protected:
|
295
|
+
#if !defined(V8_USE_PERFETTO)
|
296
|
+
virtual int64_t CurrentTimestampMicroseconds();
|
297
|
+
virtual int64_t CurrentCpuTimestampMicroseconds();
|
298
|
+
#endif // !defined(V8_USE_PERFETTO)
|
261
299
|
|
262
300
|
private:
|
263
|
-
|
301
|
+
#if !defined(V8_USE_PERFETTO)
|
264
302
|
void UpdateCategoryGroupEnabledFlag(size_t category_index);
|
265
303
|
void UpdateCategoryGroupEnabledFlags();
|
304
|
+
#endif // !defined(V8_USE_PERFETTO)
|
266
305
|
|
267
|
-
std::unique_ptr<TraceBuffer> trace_buffer_;
|
268
|
-
std::unique_ptr<TraceConfig> trace_config_;
|
269
306
|
std::unique_ptr<base::Mutex> mutex_;
|
307
|
+
std::unique_ptr<TraceConfig> trace_config_;
|
308
|
+
std::atomic_bool recording_{false};
|
270
309
|
std::unordered_set<v8::TracingController::TraceStateObserver*> observers_;
|
271
|
-
|
310
|
+
|
311
|
+
#if defined(V8_USE_PERFETTO)
|
312
|
+
std::ostream* output_stream_ = nullptr;
|
313
|
+
std::unique_ptr<perfetto::trace_processor::TraceProcessorStorage>
|
314
|
+
trace_processor_;
|
315
|
+
TraceEventListener* listener_for_testing_ = nullptr;
|
316
|
+
std::unique_ptr<perfetto::TracingSession> tracing_session_;
|
317
|
+
#else // !defined(V8_USE_PERFETTO)
|
318
|
+
std::unique_ptr<TraceBuffer> trace_buffer_;
|
319
|
+
#endif // !defined(V8_USE_PERFETTO)
|
272
320
|
|
273
321
|
// Disallow copy and assign
|
274
322
|
TracingController(const TracingController&) = delete;
|
@@ -0,0 +1,412 @@
|
|
1
|
+
// Copyright 2020 the V8 project authors. All rights reserved.
|
2
|
+
// Use of this source code is governed by a BSD-style license that can be
|
3
|
+
// found in the LICENSE file.
|
4
|
+
|
5
|
+
/**
|
6
|
+
* This file provides additional API on top of the default one for making
|
7
|
+
* API calls, which come from embedder C++ functions. The functions are being
|
8
|
+
* called directly from optimized code, doing all the necessary typechecks
|
9
|
+
* in the compiler itself, instead of on the embedder side. Hence the "fast"
|
10
|
+
* in the name. Example usage might look like:
|
11
|
+
*
|
12
|
+
* \code
|
13
|
+
* void FastMethod(int param, bool another_param);
|
14
|
+
*
|
15
|
+
* v8::FunctionTemplate::New(isolate, SlowCallback, data,
|
16
|
+
* signature, length, constructor_behavior
|
17
|
+
* side_effect_type,
|
18
|
+
* &v8::CFunction::Make(FastMethod));
|
19
|
+
* \endcode
|
20
|
+
*
|
21
|
+
* An example for custom embedder type support might employ a way to wrap/
|
22
|
+
* unwrap various C++ types in JSObject instances, e.g:
|
23
|
+
*
|
24
|
+
* \code
|
25
|
+
*
|
26
|
+
* // Represents the way this type system maps C++ and JS values.
|
27
|
+
* struct WrapperTypeInfo {
|
28
|
+
* // Store e.g. a method to map from exposed C++ types to the already
|
29
|
+
* // created v8::FunctionTemplate's for instantiating them.
|
30
|
+
* };
|
31
|
+
*
|
32
|
+
* // Helper method with a sanity check.
|
33
|
+
* template <typename T, int offset>
|
34
|
+
* inline T* GetInternalField(v8::Local<v8::Object> wrapper) {
|
35
|
+
* assert(offset < wrapper->InternalFieldCount());
|
36
|
+
* return reinterpret_cast<T*>(
|
37
|
+
* wrapper->GetAlignedPointerFromInternalField(offset));
|
38
|
+
* }
|
39
|
+
*
|
40
|
+
* // Returns the type info from a wrapper JS object.
|
41
|
+
* inline const WrapperTypeInfo* ToWrapperTypeInfo(
|
42
|
+
* v8::Local<v8::Object> wrapper) {
|
43
|
+
* return GetInternalField<WrapperTypeInfo,
|
44
|
+
* kV8EmbedderWrapperTypeIndex>(wrapper);
|
45
|
+
* }
|
46
|
+
*
|
47
|
+
* class CustomEmbedderType {
|
48
|
+
* public:
|
49
|
+
* static constexpr const WrapperTypeInfo* GetWrapperTypeInfo() {
|
50
|
+
* return &custom_type_wrapper_type_info;
|
51
|
+
* }
|
52
|
+
* // Returns the raw C object from a wrapper JS object.
|
53
|
+
* static CustomEmbedderType* Unwrap(v8::Local<v8::Object> wrapper) {
|
54
|
+
* return GetInternalField<CustomEmbedderType,
|
55
|
+
* kV8EmbedderWrapperObjectIndex>(wrapper);
|
56
|
+
* }
|
57
|
+
* static void FastMethod(CustomEmbedderType* receiver, int param) {
|
58
|
+
* assert(receiver != nullptr);
|
59
|
+
* // Type checks are already done by the optimized code.
|
60
|
+
* // Then call some performance-critical method like:
|
61
|
+
* // receiver->Method(param);
|
62
|
+
* }
|
63
|
+
*
|
64
|
+
* static void SlowMethod(
|
65
|
+
* const v8::FunctionCallbackInfo<v8::Value>& info) {
|
66
|
+
* v8::Local<v8::Object> instance =
|
67
|
+
* v8::Local<v8::Object>::Cast(info.Holder());
|
68
|
+
* CustomEmbedderType* receiver = Unwrap(instance);
|
69
|
+
* // TODO: Do type checks and extract {param}.
|
70
|
+
* FastMethod(receiver, param);
|
71
|
+
* }
|
72
|
+
*
|
73
|
+
* private:
|
74
|
+
* static const WrapperTypeInfo custom_type_wrapper_type_info;
|
75
|
+
* };
|
76
|
+
*
|
77
|
+
* // Support for custom embedder types via specialization of WrapperTraits.
|
78
|
+
* namespace v8 {
|
79
|
+
* template <>
|
80
|
+
* class WrapperTraits<CustomEmbedderType> {
|
81
|
+
* public:
|
82
|
+
* static const void* GetTypeInfo() {
|
83
|
+
* // We use the already defined machinery for the custom type.
|
84
|
+
* return CustomEmbedderType::GetWrapperTypeInfo();
|
85
|
+
* }
|
86
|
+
* };
|
87
|
+
* } // namespace v8
|
88
|
+
*
|
89
|
+
* // The constants kV8EmbedderWrapperTypeIndex and
|
90
|
+
* // kV8EmbedderWrapperObjectIndex describe the offsets for the type info
|
91
|
+
* // struct (the one returned by WrapperTraits::GetTypeInfo) and the
|
92
|
+
* // native object, when expressed as internal field indices within a
|
93
|
+
* // JSObject. The existance of this helper function assumes that all
|
94
|
+
* // embedder objects have their JSObject-side type info at the same
|
95
|
+
* // offset, but this is not a limitation of the API itself. For a detailed
|
96
|
+
* // use case, see the third example.
|
97
|
+
* static constexpr int kV8EmbedderWrapperTypeIndex = 0;
|
98
|
+
* static constexpr int kV8EmbedderWrapperObjectIndex = 1;
|
99
|
+
*
|
100
|
+
* // The following setup function can be templatized based on
|
101
|
+
* // the {embedder_object} argument.
|
102
|
+
* void SetupCustomEmbedderObject(v8::Isolate* isolate,
|
103
|
+
* v8::Local<v8::Context> context,
|
104
|
+
* CustomEmbedderType* embedder_object) {
|
105
|
+
* isolate->set_embedder_wrapper_type_index(
|
106
|
+
* kV8EmbedderWrapperTypeIndex);
|
107
|
+
* isolate->set_embedder_wrapper_object_index(
|
108
|
+
* kV8EmbedderWrapperObjectIndex);
|
109
|
+
*
|
110
|
+
* v8::CFunction c_func =
|
111
|
+
* MakeV8CFunction(CustomEmbedderType::FastMethod);
|
112
|
+
*
|
113
|
+
* Local<v8::FunctionTemplate> method_template =
|
114
|
+
* v8::FunctionTemplate::New(
|
115
|
+
* isolate, CustomEmbedderType::SlowMethod, v8::Local<v8::Value>(),
|
116
|
+
* v8::Local<v8::Signature>(), 1, v8::ConstructorBehavior::kAllow,
|
117
|
+
* v8::SideEffectType::kHasSideEffect, &c_func);
|
118
|
+
*
|
119
|
+
* v8::Local<v8::ObjectTemplate> object_template =
|
120
|
+
* v8::ObjectTemplate::New(isolate);
|
121
|
+
* object_template->SetInternalFieldCount(
|
122
|
+
* kV8EmbedderWrapperObjectIndex + 1);
|
123
|
+
* object_template->Set(
|
124
|
+
v8::String::NewFromUtf8Literal(isolate, "method"), method_template);
|
125
|
+
*
|
126
|
+
* // Instantiate the wrapper JS object.
|
127
|
+
* v8::Local<v8::Object> object =
|
128
|
+
* object_template->NewInstance(context).ToLocalChecked();
|
129
|
+
* object->SetAlignedPointerInInternalField(
|
130
|
+
* kV8EmbedderWrapperObjectIndex,
|
131
|
+
* reinterpret_cast<void*>(embedder_object));
|
132
|
+
*
|
133
|
+
* // TODO: Expose {object} where it's necessary.
|
134
|
+
* }
|
135
|
+
* \endcode
|
136
|
+
*
|
137
|
+
* For instance if {object} is exposed via a global "obj" variable,
|
138
|
+
* one could write in JS:
|
139
|
+
* function hot_func() {
|
140
|
+
* obj.method(42);
|
141
|
+
* }
|
142
|
+
* and once {hot_func} gets optimized, CustomEmbedderType::FastMethod
|
143
|
+
* will be called instead of the slow version, with the following arguments:
|
144
|
+
* receiver := the {embedder_object} from above
|
145
|
+
* param := 42
|
146
|
+
*
|
147
|
+
* Currently only void return types are supported.
|
148
|
+
* Currently supported argument types:
|
149
|
+
* - pointer to an embedder type
|
150
|
+
* - bool
|
151
|
+
* - int32_t
|
152
|
+
* - uint32_t
|
153
|
+
* To be supported types:
|
154
|
+
* - int64_t
|
155
|
+
* - uint64_t
|
156
|
+
* - float32_t
|
157
|
+
* - float64_t
|
158
|
+
* - arrays of C types
|
159
|
+
* - arrays of embedder types
|
160
|
+
*/
|
161
|
+
|
162
|
+
#ifndef INCLUDE_V8_FAST_API_CALLS_H_
|
163
|
+
#define INCLUDE_V8_FAST_API_CALLS_H_
|
164
|
+
|
165
|
+
#include <stddef.h>
|
166
|
+
#include <stdint.h>
|
167
|
+
|
168
|
+
#include "v8config.h" // NOLINT(build/include_directory)
|
169
|
+
|
170
|
+
namespace v8 {
|
171
|
+
|
172
|
+
class CTypeInfo {
|
173
|
+
public:
|
174
|
+
enum class Type : char {
|
175
|
+
kVoid,
|
176
|
+
kBool,
|
177
|
+
kInt32,
|
178
|
+
kUint32,
|
179
|
+
kInt64,
|
180
|
+
kUint64,
|
181
|
+
kFloat32,
|
182
|
+
kFloat64,
|
183
|
+
kUnwrappedApiObject,
|
184
|
+
};
|
185
|
+
|
186
|
+
enum class ArgFlags : uint8_t {
|
187
|
+
kNone = 0,
|
188
|
+
kIsArrayBit = 1 << 0, // This argument is first in an array of values.
|
189
|
+
};
|
190
|
+
|
191
|
+
static CTypeInfo FromWrapperType(const void* wrapper_type_info,
|
192
|
+
ArgFlags flags = ArgFlags::kNone) {
|
193
|
+
uintptr_t wrapper_type_info_ptr =
|
194
|
+
reinterpret_cast<uintptr_t>(wrapper_type_info);
|
195
|
+
// Check that the lower kIsWrapperTypeBit bits are 0's.
|
196
|
+
CHECK_EQ(
|
197
|
+
wrapper_type_info_ptr & ~(static_cast<uintptr_t>(~0)
|
198
|
+
<< static_cast<uintptr_t>(kIsWrapperTypeBit)),
|
199
|
+
0u);
|
200
|
+
// TODO(mslekova): Refactor the manual bit manipulations to use
|
201
|
+
// PointerWithPayload instead.
|
202
|
+
return CTypeInfo(wrapper_type_info_ptr | static_cast<int>(flags) |
|
203
|
+
kIsWrapperTypeBit);
|
204
|
+
}
|
205
|
+
|
206
|
+
static constexpr CTypeInfo FromCType(Type ctype,
|
207
|
+
ArgFlags flags = ArgFlags::kNone) {
|
208
|
+
// ctype cannot be Type::kUnwrappedApiObject.
|
209
|
+
return CTypeInfo(
|
210
|
+
((static_cast<uintptr_t>(ctype) << kTypeOffset) & kTypeMask) |
|
211
|
+
static_cast<int>(flags));
|
212
|
+
}
|
213
|
+
|
214
|
+
const void* GetWrapperInfo() const;
|
215
|
+
|
216
|
+
constexpr Type GetType() const {
|
217
|
+
if (payload_ & kIsWrapperTypeBit) {
|
218
|
+
return Type::kUnwrappedApiObject;
|
219
|
+
}
|
220
|
+
return static_cast<Type>((payload_ & kTypeMask) >> kTypeOffset);
|
221
|
+
}
|
222
|
+
|
223
|
+
constexpr bool IsArray() const {
|
224
|
+
return payload_ & static_cast<int>(ArgFlags::kIsArrayBit);
|
225
|
+
}
|
226
|
+
|
227
|
+
private:
|
228
|
+
explicit constexpr CTypeInfo(uintptr_t payload) : payload_(payload) {}
|
229
|
+
|
230
|
+
// That must be the last bit after ArgFlags.
|
231
|
+
static constexpr uintptr_t kIsWrapperTypeBit = 1 << 1;
|
232
|
+
static constexpr uintptr_t kWrapperTypeInfoMask = static_cast<uintptr_t>(~0)
|
233
|
+
<< 2;
|
234
|
+
|
235
|
+
static constexpr unsigned int kTypeOffset = kIsWrapperTypeBit;
|
236
|
+
static constexpr unsigned int kTypeSize = 8 - kTypeOffset;
|
237
|
+
static constexpr uintptr_t kTypeMask =
|
238
|
+
(~(static_cast<uintptr_t>(~0) << kTypeSize)) << kTypeOffset;
|
239
|
+
|
240
|
+
const uintptr_t payload_;
|
241
|
+
};
|
242
|
+
|
243
|
+
class CFunctionInfo {
|
244
|
+
public:
|
245
|
+
virtual const CTypeInfo& ReturnInfo() const = 0;
|
246
|
+
virtual unsigned int ArgumentCount() const = 0;
|
247
|
+
virtual const CTypeInfo& ArgumentInfo(unsigned int index) const = 0;
|
248
|
+
};
|
249
|
+
|
250
|
+
template <typename T>
|
251
|
+
class WrapperTraits {
|
252
|
+
public:
|
253
|
+
static const void* GetTypeInfo() {
|
254
|
+
static_assert(sizeof(T) != sizeof(T),
|
255
|
+
"WrapperTraits must be specialized for this type.");
|
256
|
+
return nullptr;
|
257
|
+
}
|
258
|
+
};
|
259
|
+
|
260
|
+
namespace internal {
|
261
|
+
|
262
|
+
template <typename T>
|
263
|
+
struct GetCType {
|
264
|
+
static_assert(sizeof(T) != sizeof(T), "Unsupported CType");
|
265
|
+
};
|
266
|
+
|
267
|
+
#define SPECIALIZE_GET_C_TYPE_FOR(ctype, ctypeinfo) \
|
268
|
+
template <> \
|
269
|
+
struct GetCType<ctype> { \
|
270
|
+
static constexpr CTypeInfo Get() { \
|
271
|
+
return CTypeInfo::FromCType(CTypeInfo::Type::ctypeinfo); \
|
272
|
+
} \
|
273
|
+
};
|
274
|
+
|
275
|
+
#define SUPPORTED_C_TYPES(V) \
|
276
|
+
V(void, kVoid) \
|
277
|
+
V(bool, kBool) \
|
278
|
+
V(int32_t, kInt32) \
|
279
|
+
V(uint32_t, kUint32) \
|
280
|
+
V(int64_t, kInt64) \
|
281
|
+
V(uint64_t, kUint64) \
|
282
|
+
V(float, kFloat32) \
|
283
|
+
V(double, kFloat64)
|
284
|
+
|
285
|
+
SUPPORTED_C_TYPES(SPECIALIZE_GET_C_TYPE_FOR)
|
286
|
+
|
287
|
+
template <typename T, typename = void>
|
288
|
+
struct EnableIfHasWrapperTypeInfo {};
|
289
|
+
|
290
|
+
template <typename T>
|
291
|
+
struct EnableIfHasWrapperTypeInfo<T, decltype(WrapperTraits<T>::GetTypeInfo(),
|
292
|
+
void())> {
|
293
|
+
typedef void type;
|
294
|
+
};
|
295
|
+
|
296
|
+
// T* where T is a primitive (array of primitives).
|
297
|
+
template <typename T, typename = void>
|
298
|
+
struct GetCTypePointerImpl {
|
299
|
+
static constexpr CTypeInfo Get() {
|
300
|
+
return CTypeInfo::FromCType(GetCType<T>::Get().GetType(),
|
301
|
+
CTypeInfo::ArgFlags::kIsArrayBit);
|
302
|
+
}
|
303
|
+
};
|
304
|
+
|
305
|
+
// T* where T is an API object.
|
306
|
+
template <typename T>
|
307
|
+
struct GetCTypePointerImpl<T, typename EnableIfHasWrapperTypeInfo<T>::type> {
|
308
|
+
static constexpr CTypeInfo Get() {
|
309
|
+
return CTypeInfo::FromWrapperType(WrapperTraits<T>::GetTypeInfo());
|
310
|
+
}
|
311
|
+
};
|
312
|
+
|
313
|
+
// T** where T is a primitive. Not allowed.
|
314
|
+
template <typename T, typename = void>
|
315
|
+
struct GetCTypePointerPointerImpl {
|
316
|
+
static_assert(sizeof(T**) != sizeof(T**), "Unsupported type");
|
317
|
+
};
|
318
|
+
|
319
|
+
// T** where T is an API object (array of API objects).
|
320
|
+
template <typename T>
|
321
|
+
struct GetCTypePointerPointerImpl<
|
322
|
+
T, typename EnableIfHasWrapperTypeInfo<T>::type> {
|
323
|
+
static constexpr CTypeInfo Get() {
|
324
|
+
return CTypeInfo::FromWrapperType(WrapperTraits<T>::GetTypeInfo(),
|
325
|
+
CTypeInfo::ArgFlags::kIsArrayBit);
|
326
|
+
}
|
327
|
+
};
|
328
|
+
|
329
|
+
template <typename T>
|
330
|
+
struct GetCType<T**> : public GetCTypePointerPointerImpl<T> {};
|
331
|
+
|
332
|
+
template <typename T>
|
333
|
+
struct GetCType<T*> : public GetCTypePointerImpl<T> {};
|
334
|
+
|
335
|
+
template <typename R, typename... Args>
|
336
|
+
class CFunctionInfoImpl : public CFunctionInfo {
|
337
|
+
public:
|
338
|
+
CFunctionInfoImpl()
|
339
|
+
: return_info_(internal::GetCType<R>::Get()),
|
340
|
+
arg_count_(sizeof...(Args)),
|
341
|
+
arg_info_{internal::GetCType<Args>::Get()...} {
|
342
|
+
static_assert(
|
343
|
+
internal::GetCType<R>::Get().GetType() == CTypeInfo::Type::kVoid,
|
344
|
+
"Only void return types are currently supported.");
|
345
|
+
}
|
346
|
+
|
347
|
+
const CTypeInfo& ReturnInfo() const override { return return_info_; }
|
348
|
+
unsigned int ArgumentCount() const override { return arg_count_; }
|
349
|
+
const CTypeInfo& ArgumentInfo(unsigned int index) const override {
|
350
|
+
CHECK_LT(index, ArgumentCount());
|
351
|
+
return arg_info_[index];
|
352
|
+
}
|
353
|
+
|
354
|
+
private:
|
355
|
+
CTypeInfo return_info_;
|
356
|
+
const unsigned int arg_count_;
|
357
|
+
CTypeInfo arg_info_[sizeof...(Args)];
|
358
|
+
};
|
359
|
+
|
360
|
+
} // namespace internal
|
361
|
+
|
362
|
+
class V8_EXPORT CFunction {
|
363
|
+
public:
|
364
|
+
constexpr CFunction() : address_(nullptr), type_info_(nullptr) {}
|
365
|
+
|
366
|
+
const CTypeInfo& ReturnInfo() const { return type_info_->ReturnInfo(); }
|
367
|
+
|
368
|
+
const CTypeInfo& ArgumentInfo(unsigned int index) const {
|
369
|
+
return type_info_->ArgumentInfo(index);
|
370
|
+
}
|
371
|
+
|
372
|
+
unsigned int ArgumentCount() const { return type_info_->ArgumentCount(); }
|
373
|
+
|
374
|
+
const void* GetAddress() const { return address_; }
|
375
|
+
const CFunctionInfo* GetTypeInfo() const { return type_info_; }
|
376
|
+
|
377
|
+
template <typename F>
|
378
|
+
static CFunction Make(F* func) {
|
379
|
+
return ArgUnwrap<F*>::Make(func);
|
380
|
+
}
|
381
|
+
|
382
|
+
private:
|
383
|
+
const void* address_;
|
384
|
+
const CFunctionInfo* type_info_;
|
385
|
+
|
386
|
+
CFunction(const void* address, const CFunctionInfo* type_info);
|
387
|
+
|
388
|
+
template <typename R, typename... Args>
|
389
|
+
static CFunctionInfo* GetCFunctionInfo() {
|
390
|
+
static internal::CFunctionInfoImpl<R, Args...> instance;
|
391
|
+
return &instance;
|
392
|
+
}
|
393
|
+
|
394
|
+
template <typename F>
|
395
|
+
class ArgUnwrap {
|
396
|
+
static_assert(sizeof(F) != sizeof(F),
|
397
|
+
"CFunction must be created from a function pointer.");
|
398
|
+
};
|
399
|
+
|
400
|
+
template <typename R, typename... Args>
|
401
|
+
class ArgUnwrap<R (*)(Args...)> {
|
402
|
+
public:
|
403
|
+
static CFunction Make(R (*func)(Args...)) {
|
404
|
+
return CFunction(reinterpret_cast<const void*>(func),
|
405
|
+
GetCFunctionInfo<R, Args...>());
|
406
|
+
}
|
407
|
+
};
|
408
|
+
};
|
409
|
+
|
410
|
+
} // namespace v8
|
411
|
+
|
412
|
+
#endif // INCLUDE_V8_FAST_API_CALLS_H_
|