Fixed getting current script name

This commit is contained in:
ksuprynowicz 2022-12-17 15:41:15 +01:00
parent 6301d23c48
commit da458ccef9
3 changed files with 22 additions and 13 deletions

View file

@ -114,7 +114,7 @@ ScriptEnginePointer ScriptContextV8Wrapper::engine() const {
}
ScriptFunctionContextPointer ScriptContextV8Wrapper::functionContext() const {
return std::make_shared<ScriptFunctionContextV8Wrapper>(_engine->getContext());
return std::make_shared<ScriptFunctionContextV8Wrapper>(_engine);
}
ScriptContextPointer ScriptContextV8Wrapper::parentContext() const {
@ -176,15 +176,21 @@ ScriptValue ScriptContextV8Wrapper::throwValue(const ScriptValue& value) {
QString ScriptFunctionContextV8Wrapper::fileName() const {
//V8TODO
//return _value.fileName();
return QString("");
//V8TODO: It's not exactly like in QtScript, because there's no such context object in V8, let's return the current one for now
//Maybe fetch data on creation or store stack frame?
v8::Local<v8::String> name = v8::StackTrace::CurrentScriptNameOrSourceURL(_engine->getIsolate());
v8::String::Utf8Value nameUTF(_engine->getIsolate(), name);
return QString(*nameUTF);
}
QString ScriptFunctionContextV8Wrapper::functionName() const {
//V8TODO
//return _value.functionName();
return QString("");
//V8TODO: It's not exactly like in QtScript, because there's no such context object in V8, let's return the current one for now
//Maybe fetch data on creation?
v8::Local<v8::StackTrace> stackTrace = v8::StackTrace::CurrentStackTrace(_engine->getIsolate(), 1);
v8::Local<v8::StackFrame> stackFrame = stackTrace->GetFrame(_engine->getIsolate(), 0);
v8::Local<v8::String> name = stackFrame->GetFunctionName();
v8::String::Utf8Value nameUTF(_engine->getIsolate(), name);
return QString(*nameUTF);
}
ScriptFunctionContext::FunctionType ScriptFunctionContextV8Wrapper::functionType() const {
@ -194,7 +200,9 @@ ScriptFunctionContext::FunctionType ScriptFunctionContextV8Wrapper::functionType
}
int ScriptFunctionContextV8Wrapper::lineNumber() const {
//V8TODO
//return _value.lineNumber();
return 0;
//V8TODO: It's not exactly like in QtScript, because there's no such context object in V8, let's return the current one for now
//Maybe fetch data on creation?
v8::Local<v8::StackTrace> stackTrace = v8::StackTrace::CurrentStackTrace(_engine->getIsolate(), 1);
v8::Local<v8::StackFrame> stackFrame = stackTrace->GetFrame(_engine->getIsolate(), 0);
return stackFrame->GetLineNumber();
}

View file

@ -58,7 +58,7 @@ private: // storage
class ScriptFunctionContextV8Wrapper final : public ScriptFunctionContext {
public: // construction
//V8TODO
inline ScriptFunctionContextV8Wrapper(v8::Local<v8::Context> context) { }
inline ScriptFunctionContextV8Wrapper(ScriptEngineV8* engine) : _engine(engine) { }
public: // ScriptFunctionContext implementation
virtual QString fileName() const override;
@ -66,7 +66,8 @@ public: // ScriptFunctionContext implementation
virtual FunctionType functionType() const override;
virtual int lineNumber() const override;
//private: // storage
private: // storage
ScriptEngineV8* _engine;
//V8ScriptContextInfo _value;
};

View file

@ -376,7 +376,7 @@ ScriptValue::PropertyFlags ScriptObjectV8Proxy::propertyFlags(const V8ScriptValu
void ScriptObjectV8Proxy::v8Get(v8::Local<v8::Name> name, const v8::PropertyCallbackInfo<v8::Value>& info) {
v8::HandleScope handleScope(info.GetIsolate());
v8::String::Utf8Value utf8Value(info.GetIsolate(), name);
qDebug(scriptengine) << "Get: " << *utf8Value;
//qDebug(scriptengine) << "Get: " << *utf8Value;
V8ScriptValue object(info.GetIsolate(), info.This());
ScriptObjectV8Proxy *proxy = ScriptObjectV8Proxy::unwrapProxy(object);
if (!proxy) {