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 18namespace v8 { 19 20class Context; 21class UnboundScript; 22 23/** 24 * A JavaScript function object (ECMA-262, 15.3). 25 */ 26class 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 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 V8_DEPRECATED("No direct replacement") 63 MaybeLocal<UnboundScript> GetUnboundScript() const; 64 65 /** 66 * Name inferred from variable or property assignment of this function. 67 * Used to facilitate debugging and profiling of JavaScript code written 68 * in an OO style, where many functions are anonymous but are assigned 69 * to object properties. 70 */ 71 Local<Value> GetInferredName() const; 72 73 /** 74 * displayName if it is set, otherwise name if it is configured, otherwise 75 * function name, otherwise inferred name. 76 */ 77 Local<Value> GetDebugName() const; 78 79 /** 80 * Returns zero based line number of function body and 81 * kLineOffsetNotFound if no information available. 82 */ 83 int GetScriptLineNumber() const; 84 /** 85 * Returns zero based column number of function body and 86 * kLineOffsetNotFound if no information available. 87 */ 88 int GetScriptColumnNumber() const; 89 90 /** 91 * Returns scriptId. 92 */ 93 int ScriptId() const; 94 95 /** 96 * Returns the original function if this function is bound, else returns 97 * v8::Undefined. 98 */ 99 Local<Value> GetBoundFunction() const; 100 101 /** 102 * Calls builtin Function.prototype.toString on this function. 103 * This is different from Value::ToString() that may call a user-defined 104 * toString() function, and different than Object::ObjectProtoToString() which 105 * always serializes "[object Function]". 106 */ 107 V8_WARN_UNUSED_RESULT MaybeLocal<String> FunctionProtoToString( 108 Local<Context> context); 109 110 /** 111 * Returns true if the function does nothing. 112 * The function returns false on error. 113 * Note that this function is experimental. Embedders should not rely on 114 * this existing. We may remove this function in the future. 115 */ 116 V8_WARN_UNUSED_RESULT bool Experimental_IsNopFunction() const; 117 118 ScriptOrigin GetScriptOrigin() const; 119 V8_INLINE static Function* Cast(Value* value) { 120#ifdef V8_ENABLE_CHECKS 121 CheckCast(value); 122#endif 123 return static_cast<Function*>(value); 124 } 125 126 static const int kLineOffsetNotFound; 127 128 private: 129 Function(); 130 static void CheckCast(Value* obj); 131}; 132} // namespace v8 133 134#endif // INCLUDE_V8_FUNCTION_H_ 135