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