1 // Copyright 2021 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 INCLUDE_V8_FUNCTION_H_ 6 #define INCLUDE_V8_FUNCTION_H_ 7 8 #include <stddef.h> 9 #include <stdint.h> 10 11 #include "v8-function-callback.h" // NOLINT(build/include_directory) 12 #include "v8-local-handle.h" // NOLINT(build/include_directory) 13 #include "v8-message.h" // NOLINT(build/include_directory) 14 #include "v8-object.h" // NOLINT(build/include_directory) 15 #include "v8-template.h" // NOLINT(build/include_directory) 16 #include "v8config.h" // NOLINT(build/include_directory) 17 18 namespace v8 { 19 20 class Context; 21 22 /** 23 * A JavaScript function object (ECMA-262, 15.3). 24 */ 25 class V8_EXPORT Function : public Object { 26 public: 27 /** 28 * Create a function in the current execution context 29 * for a given FunctionCallback. 30 */ 31 static MaybeLocal<Function> New( 32 Local<Context> context, FunctionCallback callback, 33 Local<Value> data = Local<Value>(), int length = 0, 34 ConstructorBehavior behavior = ConstructorBehavior::kAllow, 35 SideEffectType side_effect_type = SideEffectType::kHasSideEffect); 36 37 V8_WARN_UNUSED_RESULT MaybeLocal<Object> NewInstance( 38 Local<Context> context, int argc, Local<Value> argv[]) const; 39 NewInstance(Local<Context> context)40 V8_WARN_UNUSED_RESULT MaybeLocal<Object> NewInstance( 41 Local<Context> context) const { 42 return NewInstance(context, 0, nullptr); 43 } 44 45 /** 46 * When side effect checks are enabled, passing kHasNoSideEffect allows the 47 * constructor to be invoked without throwing. Calls made within the 48 * constructor are still checked. 49 */ 50 V8_WARN_UNUSED_RESULT MaybeLocal<Object> NewInstanceWithSideEffectType( 51 Local<Context> context, int argc, Local<Value> argv[], 52 SideEffectType side_effect_type = SideEffectType::kHasSideEffect) const; 53 54 V8_WARN_UNUSED_RESULT MaybeLocal<Value> Call(Local<Context> context, 55 Local<Value> recv, int argc, 56 Local<Value> argv[]); 57 58 void SetName(Local<String> name); 59 Local<Value> GetName() const; 60 61 /** 62 * Name inferred from variable or property assignment of this function. 63 * Used to facilitate debugging and profiling of JavaScript code written 64 * in an OO style, where many functions are anonymous but are assigned 65 * to object properties. 66 */ 67 Local<Value> GetInferredName() const; 68 69 /** 70 * displayName if it is set, otherwise name if it is configured, otherwise 71 * function name, otherwise inferred name. 72 */ 73 Local<Value> GetDebugName() const; 74 75 /** 76 * Returns zero based line number of function body and 77 * kLineOffsetNotFound if no information available. 78 */ 79 int GetScriptLineNumber() const; 80 /** 81 * Returns zero based column number of function body and 82 * kLineOffsetNotFound if no information available. 83 */ 84 int GetScriptColumnNumber() const; 85 86 /** 87 * Returns scriptId. 88 */ 89 int ScriptId() const; 90 91 /** 92 * Returns the original function if this function is bound, else returns 93 * v8::Undefined. 94 */ 95 Local<Value> GetBoundFunction() const; 96 97 /** 98 * Calls builtin Function.prototype.toString on this function. 99 * This is different from Value::ToString() that may call a user-defined 100 * toString() function, and different than Object::ObjectProtoToString() which 101 * always serializes "[object Function]". 102 */ 103 V8_WARN_UNUSED_RESULT MaybeLocal<String> FunctionProtoToString( 104 Local<Context> context); 105 106 ScriptOrigin GetScriptOrigin() const; Cast(Value * value)107 V8_INLINE static Function* Cast(Value* value) { 108 #ifdef V8_ENABLE_CHECKS 109 CheckCast(value); 110 #endif 111 return static_cast<Function*>(value); 112 } 113 114 static const int kLineOffsetNotFound; 115 116 private: 117 Function(); 118 static void CheckCast(Value* obj); 119 }; 120 } // namespace v8 121 122 #endif // INCLUDE_V8_FUNCTION_H_ 123