c++node.jsv8

How does FunctionTemplate::GetFunction may return something?


In V8 code there is a function that receives a function

MaybeLocal<v8::Function> FunctionTemplate::GetFunction(Local<Context> context) {
  PREPARE_FOR_EXECUTION(context, FunctionTemplate, GetFunction);
  auto self = Utils::OpenHandle(this);
  Local<Function> result;
  has_exception =
      !ToLocal<Function>(i::ApiNatives::InstantiateFunction(
                             i_isolate, i_isolate->native_context(), self),
                         &result);
  RETURN_ON_FAILED_EXECUTION(Function);
  RETURN_ESCAPED(result);
}

Or maybe I don't know something, but how can this function return something? There is no return statement.

The node.js V8 documentation says this:

Returns the unique function instance in the current execution context.

How does this work?


Solution

  • deps/v8/src/api/api-macros.h:106-112

    #define RETURN_ON_FAILED_EXECUTION(T) \
      if (has_exception) return MaybeLocal<T>();
    
    #define RETURN_ESCAPED(value) return handle_scope.Escape(value);
    

    Results in

    MaybeLocal<v8::Function> FunctionTemplate::GetFunction(Local<Context> context) {
      PREPARE_FOR_EXECUTION(context, FunctionTemplate, GetFunction);
      auto self = Utils::OpenHandle(this);
      Local<Function> result;
      has_exception =
          !ToLocal<Function>(i::ApiNatives::InstantiateFunction(
                                 i_isolate, i_isolate->native_context(), self),
                             &result);
      if (has_exception) return MaybeLocal<Function>();
      return handle_scope.Escape(result);
    }