How to get the original filename of a script in V8

I want to get the original script filename in global functions.

I have tried the following code, but filename.IsEmpty () returns true.

using namespace v8;

HandleScope handle_scope;

// Define Global Function 'func'
Handle<ObjectTemplate> global = ObjectTemplate::New();

auto func_name = v8::String::New("func");
auto func = v8::FunctionTemplate::New(
        [](const v8::Arguments& args) -> v8::Handle<v8::Value>{

            // I want to get Filename here.
            auto filename = args.Callee()->GetScriptOrigin().ResourceName();
            std::cout << filename.IsEmpty() << std::endl;

            return v8::Undefined();
        });

global->Set(func_name, func);

auto context = Context::New(nullptr, global);
Context::Scope context_scope(context);

auto source = String::New("func()");

// Set Filename
auto filename = String::New("abc.js");
auto script = v8::Script::Compile(source, filename);
script->Run();

context.Dispose();

      

Is there a correct way to access the original filename of the script?

+3


source to share


1 answer


I decided myself:



auto func = v8::FunctionTemplate::New(
    [](const v8::Arguments& args) -> v8::Handle<v8::Value>{

          // Get Filename
          auto filename = v8::StackTrace::CurrentStackTrace(1,v8::StackTrace::kScriptName)
                                ->GetFrame(0)->GetScriptName();
          std::cout << *v8::String::AsciiValue(filename) << std::endl;

        return v8::Undefined();
    });

      

+3


source







All Articles