History

Initial V8 support, not working yet

ksuprynowicz committed 4 years ago

Commit 7e9ad391 parent df507a740 files · +4040 −3087Browse files

Showing the changes to libraries/script-engine/src/v8/ScriptProgramV8Wrapper.cpp (1 of 40 files). Show all

Unified · Split

libraries/script-engine/src/v8/ScriptProgramV8Wrapper.cpp+64−0
@@ -0,0 +1,64 @@
1+//
2+// ScriptProgramV8Wrapper.cpp
3+// libraries/script-engine/src/v8
4+//
5+// Created by Heather Anderson on 8/24/21.
6+// Modified for V8 by dr Karol Suprynowicz on 2022/10/08
7+// Copyright 2021 Vircadia contributors.
8+// Copyright 2022 Overte e.V.
9+//
10+// Distributed under the Apache License, Version 2.0.
11+// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
12+//
13+
14+#include "ScriptProgramV8Wrapper.h"
15+
16+#include "ScriptEngineV8.h"
17+#include "ScriptValueV8Wrapper.h"
18+
19+ScriptProgramV8Wrapper* ScriptProgramV8Wrapper::unwrap(ScriptProgramPointer val) {
20+ if (!val) {
21+ return nullptr;
22+ }
23+
24+ return dynamic_cast<ScriptProgramV8Wrapper*>(val.get());
25+}
26+
27+ScriptSyntaxCheckResultPointer ScriptProgramV8Wrapper::checkSyntax() {
28+ if (!_isCompiled) {
29+ compile();
30+ }
31+ return std::make_shared<ScriptSyntaxCheckResultV8Wrapper>(_compileResult);
32+}
33+
34+bool ScriptProgramV8Wrapper::compile() {
35+ int errorColumnNumber = 0;
36+ int errorLineNumber = 0;
37+ QString errorMessage = "";
38+ QString errorBacktrace = "";
39+ ScriptSyntaxCheckResult::State state;
40+ v8::TryCatch tryCatch(_engine->getIsolate());
41+ v8::ScriptOrigin scriptOrigin(_engine->getIsolate(), v8::String::NewFromUtf8(_engine->getIsolate(), _url.toStdString().c_str()).ToLocalChecked());
42+ v8::Local<v8::Script> script;
43+ if (v8::Script::Compile(_engine->getContext(), v8::String::NewFromUtf8(_engine->getIsolate(), _source.toStdString().c_str()).ToLocalChecked(), &scriptOrigin).ToLocal(&script)) {
44+ _compileResult = ScriptSyntaxCheckResultV8Wrapper(ScriptSyntaxCheckResult::Valid);
45+ _value = V8ScriptProgram(_engine->getIsolate(), script);
46+ return true;
47+ }
48+ v8::String::Utf8Value utf8Value(_engine->getIsolate(), tryCatch.Exception());
49+ errorMessage = QString(*utf8Value);
50+ v8::Local<v8::Message> exceptionMessage = tryCatch.Message();
51+ if (!exceptionMessage.IsEmpty()) {
52+ errorLineNumber = exceptionMessage->GetLineNumber(_engine->getContext()).FromJust();
53+ errorColumnNumber = exceptionMessage->GetStartColumn(_engine->getContext()).FromJust();
54+ v8::Local<v8::Value> backtraceV8String;
55+ if (tryCatch.StackTrace(_engine->getContext()).ToLocal(&backtraceV8String) && backtraceV8String->IsString() &&
56+ v8::Local<v8::String>::Cast(backtraceV8String)->Length() > 0) {
57+ v8::String::Utf8Value backtraceUtf8Value(_engine->getIsolate(), backtraceV8String);
58+ errorBacktrace = *backtraceUtf8Value;
59+ }
60+ }
61+ //V8TODO
62+ _compileResult = ScriptSyntaxCheckResultV8Wrapper(ScriptSyntaxCheckResult::Error, errorColumnNumber, errorLineNumber, errorMessage, errorBacktrace);
63+ return false;
64+}