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.h" 9 #include "src/utils/utils.h" 10 11 namespace v8 { 12 namespace internal { 13 14 // Provides garbage collection on invoking |fun_name|(options), where 15 // - options is a dictionary like object. See supported properties below. 16 // - no parameter refers to options: 17 // {type: 'major', execution: 'sync'}. 18 // - truthy parameter that is not setting any options: 19 // {type: 'minor', execution: 'sync'}. 20 // 21 // Supported options: 22 // - type: 'major' or 'minor' for full GC and Scavenge, respectively. 23 // - execution: 'sync' or 'async' for synchronous and asynchronous execution, 24 // respectively. 25 // - Defaults to {type: 'major', execution: 'sync'}. 26 // 27 // Returns a Promise that resolves when GC is done when asynchronous execution 28 // is requested, and undefined otherwise. 29 class GCExtension : public v8::Extension { 30 public: GCExtension(const char * fun_name)31 explicit GCExtension(const char* fun_name) 32 : v8::Extension("v8/gc", 33 BuildSource(buffer_, sizeof(buffer_), fun_name)) {} 34 v8::Local<v8::FunctionTemplate> GetNativeFunctionTemplate( 35 v8::Isolate* isolate, v8::Local<v8::String> name) override; 36 static void GC(const v8::FunctionCallbackInfo<v8::Value>& args); 37 38 private: BuildSource(char * buf,size_t size,const char * fun_name)39 static const char* BuildSource(char* buf, size_t size, const char* fun_name) { 40 SNPrintF(Vector<char>(buf, static_cast<int>(size)), 41 "native function %s();", fun_name); 42 return buf; 43 } 44 45 char buffer_[50]; 46 }; 47 48 } // namespace internal 49 } // namespace v8 50 51 #endif // V8_EXTENSIONS_GC_EXTENSION_H_ 52