1 // Copyright 2019 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_CPUTRACEMARK_EXTENSION_H_ 6 #define V8_EXTENSIONS_CPUTRACEMARK_EXTENSION_H_ 7 8 #include "include/v8-extension.h" 9 #include "src/base/strings.h" 10 11 namespace v8 { 12 13 template <typename T> 14 class FunctionCallbackInfo; 15 16 namespace internal { 17 18 class CpuTraceMarkExtension : public v8::Extension { 19 public: CpuTraceMarkExtension(const char * fun_name)20 explicit CpuTraceMarkExtension(const char* fun_name) 21 : v8::Extension("v8/cpumark", 22 BuildSource(buffer_, sizeof(buffer_), fun_name)) {} 23 24 v8::Local<v8::FunctionTemplate> GetNativeFunctionTemplate( 25 v8::Isolate* isolate, v8::Local<v8::String> name) override; 26 27 private: 28 static void Mark(const v8::FunctionCallbackInfo<v8::Value>& args); 29 BuildSource(char * buf,size_t size,const char * fun_name)30 static const char* BuildSource(char* buf, size_t size, const char* fun_name) { 31 base::SNPrintF(base::Vector<char>(buf, static_cast<int>(size)), 32 "native function %s();", fun_name); 33 return buf; 34 } 35 36 char buffer_[50]; 37 }; 38 39 } // namespace internal 40 } // namespace v8 41 42 #endif // V8_EXTENSIONS_CPUTRACEMARK_EXTENSION_H_ 43