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
8#include "include/v8-extension.h"
9#include "include/v8-local-handle.h"
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 refers to options:
22//   {type: 'major', execution: 'sync'}.
23// - truthy parameter that is not setting any options:
24//   {type: 'minor', execution: 'sync'}.
25//
26// Supported options:
27// - type: 'major' or 'minor' for full GC and Scavenge, respectively.
28// - execution: 'sync' or 'async' for synchronous and asynchronous execution,
29// respectively.
30// - Defaults to {type: 'major', execution: 'sync'}.
31//
32// Returns a Promise that resolves when GC is done when asynchronous execution
33// is requested, and undefined otherwise.
34class GCExtension : public v8::Extension {
35 public:
36  explicit GCExtension(const char* fun_name)
37      : v8::Extension("v8/gc",
38                      BuildSource(buffer_, sizeof(buffer_), fun_name)) {}
39  v8::Local<v8::FunctionTemplate> GetNativeFunctionTemplate(
40      v8::Isolate* isolate, v8::Local<v8::String> name) override;
41  static void GC(const v8::FunctionCallbackInfo<v8::Value>& args);
42
43 private:
44  static const char* BuildSource(char* buf, size_t size, const char* fun_name) {
45    base::SNPrintF(base::Vector<char>(buf, static_cast<int>(size)),
46                   "native function %s();", fun_name);
47    return buf;
48  }
49
50  char buffer_[50];
51};
52
53}  // namespace internal
54}  // namespace v8
55
56#endif  // V8_EXTENSIONS_GC_EXTENSION_H_
57