How to get script's origin filename in V8
c++, c++11, javascript, v8
Solution
Solved by 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();
});
Problem
I want to get script's origin filename in global functions. I tried 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 script's origin filename?