From 500df331fc8b5a93992231e92308981ce897452f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=8A=B2=E9=B9=8F?= Date: Mon, 22 Mar 2021 20:35:45 +0800 Subject: [PATCH] add v8 release bin --- doric-Qt/doric/doric.pro | 19 ++++++ doric-Qt/doric/engine/DoricNativeJSE.cpp | 83 ++++++++++++++++++++++++ doric-Qt/doric/engine/DoricNativeJSE.h | 4 ++ 3 files changed, 106 insertions(+) diff --git a/doric-Qt/doric/doric.pro b/doric-Qt/doric/doric.pro index 4afb7243..baff2f7b 100644 --- a/doric-Qt/doric/doric.pro +++ b/doric-Qt/doric/doric.pro @@ -7,6 +7,7 @@ CONFIG += c++11 # depend on your compiler). Refer to the documentation for the # deprecated API to know how to port your code away from it. DEFINES += QT_DEPRECATED_WARNINGS +DEFINES += V8_COMPRESS_POINTERS # You can also make your code fail to compile if it uses deprecated APIs. # In order to do so, uncomment the following line. @@ -130,3 +131,21 @@ HEADERS += \ yoga/internal/experiments-inl.h \ yoga/internal/experiments.h \ yoga/log.h + +LIBS += -lwinmm +LIBS += -lAdvapi32 +LIBS += -lDbghelp + +INCLUDEPATH += $$PWD/v8 + +LIBS += -L$$PWD/library/v8/release/ +LIBS += -lv8_monolith + +win32:CONFIG(debug, debug|release): { + QMAKE_CFLAGS_DEBUG += -MT + QMAKE_CXXFLAGS_DEBUG += -MT +} +else:win32:CONFIG(release, debug|release): { + QMAKE_CFLAGS_RELEASE += -MT + QMAKE_CXXFLAGS_RELEASE += -MT +} diff --git a/doric-Qt/doric/engine/DoricNativeJSE.cpp b/doric-Qt/doric/engine/DoricNativeJSE.cpp index 63bc2c6d..5751d923 100644 --- a/doric-Qt/doric/engine/DoricNativeJSE.cpp +++ b/doric-Qt/doric/engine/DoricNativeJSE.cpp @@ -6,6 +6,89 @@ DoricNativeJSE::DoricNativeJSE() { mJSEngine.installExtensions(QJSEngine::AllExtensions); + + std::unique_ptr platform = v8::platform::NewDefaultPlatform(); + v8::V8::InitializePlatform(platform.get()); + v8::V8::Initialize(); + + v8::Isolate::CreateParams create_params; + create_params.array_buffer_allocator = + v8::ArrayBuffer::Allocator::NewDefaultAllocator(); + v8::Isolate *isolate = v8::Isolate::New(create_params); + + { + v8::Isolate::Scope isolate_scope(isolate); + + // Create a stack-allocated handle scope. + v8::HandleScope handle_scope(isolate); + + // Create a new context. + v8::Local context = v8::Context::New(isolate); + + // Enter the context for compiling and running the hello world script. + v8::Context::Scope context_scope(context); + + { + // Create a string containing the JavaScript source code. + v8::Local source = + v8::String::NewFromUtf8Literal(isolate, "'Hello' + ', World!'"); + + // Compile the source code. + v8::Local script = + v8::Script::Compile(context, source).ToLocalChecked(); + + // Run the script to get the result. + v8::Local result = script->Run(context).ToLocalChecked(); + + // Convert the result to an UTF8 string and print it. + v8::String::Utf8Value utf8(isolate, result); + printf("%s\n", *utf8); + } + + { + // Use the JavaScript API to generate a WebAssembly module. + // + // |bytes| contains the binary format for the following module: + // + // (func (export "add") (param i32 i32) (result i32) + // get_local 0 + // get_local 1 + // i32.add) + // + const char csource[] = R"( + let bytes = new Uint8Array([ + 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x07, 0x01, + 0x60, 0x02, 0x7f, 0x7f, 0x01, 0x7f, 0x03, 0x02, 0x01, 0x00, 0x07, + 0x07, 0x01, 0x03, 0x61, 0x64, 0x64, 0x00, 0x00, 0x0a, 0x09, 0x01, + 0x07, 0x00, 0x20, 0x00, 0x20, 0x01, 0x6a, 0x0b + ]); + let module = new WebAssembly.Module(bytes); + let instance = new WebAssembly.Instance(module); + instance.exports.add(3, 4); + )"; + + // Create a string containing the JavaScript source code. + v8::Local source = + v8::String::NewFromUtf8Literal(isolate, csource); + + // Compile the source code. + v8::Local script = + v8::Script::Compile(context, source).ToLocalChecked(); + + // Run the script to get the result. + v8::Local result = script->Run(context).ToLocalChecked(); + + // Convert the result to a uint32 and print it. + uint32_t number = result->Uint32Value(context).ToChecked(); + printf("3 + 4 = %u\n", number); + } + } + + // Dispose the isolate and tear down V8. + isolate->Dispose(); + v8::V8::Dispose(); + v8::V8::ShutdownPlatform(); + delete create_params.array_buffer_allocator; } QString DoricNativeJSE::loadJS(QString script, QString source) { diff --git a/doric-Qt/doric/engine/DoricNativeJSE.h b/doric-Qt/doric/engine/DoricNativeJSE.h index 564fad55..a06defd5 100644 --- a/doric-Qt/doric/engine/DoricNativeJSE.h +++ b/doric-Qt/doric/engine/DoricNativeJSE.h @@ -3,6 +3,10 @@ #include "DoricInterfaceJSE.h" #include +#include + +#include "libplatform/libplatform.h" +#include "v8.h" class DoricNativeJSE : public DoricInterfaceJSE { private: