v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
gc-extension.h
Go to the documentation of this file.
1// Copyright 2010 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#ifndef V8_EXTENSIONS_GC_EXTENSION_H_
6#define V8_EXTENSIONS_GC_EXTENSION_H_
7
10#include "src/base/strings.h"
11
12namespace v8 {
13
14template <typename T>
15class FunctionCallbackInfo;
16
17namespace internal {
18
19// Provides garbage collection on invoking |fun_name|(options), where
20// - options is a dictionary like object. See supported properties below.
21// - no parameter defaults to options:
22// {type: 'major', execution: 'sync'}.
23// - truthy parameter that is not setting any options:
24// {type: 'minor', execution: 'sync'}.
25//
26// Exceptions that occur during parsing the options bag are preserved and result
27// in skipping the GC call.
28//
29// Supported options:
30// - type:
31// - 'major': Full GC.
32// - 'minor': Young generation GC.
33// - 'major-snapshot': Full GC with taking a heap snapshot at the same time.
34// - execution: 'sync' or 'async' for synchronous and asynchronous
35// execution, respectively.
36// - flavor:
37// - 'regular': A regular GC.
38// - 'last-resort': A last resort GC.
39// - filename: Filename for the snapshot in case the type was
40// 'major-snapshot'.
41//
42// Returns a Promise that resolves when GC is done when asynchronous execution
43// is requested, and undefined otherwise.
44//
45// Frequent use cases (assuming --expose-gc):
46// 1. Just perform a GC to check whether things improve: `gc()`
47// 2. Test that certain objects indeed are reclaimed:
48// `await gc({type:'major', execution:'async'})`
49// 3. Same as 2. but with checking why things did go wrong in a snapshot:
50// `await gc({type:'major-snapshot', execution:'async'})`
51// 4. Synchronous last resort GC:
52// `gc({type:'major',execution:'sync',flavor:'last-resort'})`
53class GCExtension : public v8::Extension {
54 public:
55 explicit GCExtension(const char* fun_name)
56 : v8::Extension("v8/gc",
57 BuildSource(buffer_, sizeof(buffer_), fun_name)) {}
59 v8::Isolate* isolate, v8::Local<v8::String> name) override;
60 static void GC(const v8::FunctionCallbackInfo<v8::Value>& info);
61
62 private:
63 static const char* BuildSource(char* buf, size_t size, const char* fun_name) {
64 base::SNPrintF(base::VectorOf(buf, size), "native function %s();",
65 fun_name);
66 return buf;
67 }
68
69 char buffer_[50];
70};
71
72} // namespace internal
73} // namespace v8
74
75#endif // V8_EXTENSIONS_GC_EXTENSION_H_
static const char * BuildSource(char *buf, size_t size, const char *fun_name)
v8::Local< v8::FunctionTemplate > GetNativeFunctionTemplate(v8::Isolate *isolate, v8::Local< v8::String > name) override
GCExtension(const char *fun_name)
int SNPrintF(Vector< char > str, const char *format,...)
Definition strings.cc:20
constexpr Vector< T > VectorOf(T *start, size_t size)
Definition vector.h:360
@ GC
Definition v8-unwinder.h:38