I've tried to use the class I shared. I noticed that the second code occured an error "Access violation executing location." at below.
void Core::Run(const char* source_code) { v8::Isolate::Scope isolate_scope(isolate_); { v8::HandleScope handle_scope(isolate_); { // The error is happened here. v8::Local<v8::Context> context = v8::Context::New(isolate_); I captured the error. [image: error-sample.png] I'm wondered why the error was occured. I also checked isolate_ is not null. 2022年5月4日水曜日 15:46:20 UTC+9 Kazuya Hiruma: > Hi, V8 folks. > > I'm confused why running a script is failed. > > I'm creating V8 api class like below. > > It's the header. > > #pragma once > namespace v8_api > { > class Core > { > private: > v8::Isolate* isolate_; > > static void LogCallback(const v8::FunctionCallbackInfo<v8::Value>& > args) > { > if (args.Length() < 1) return; > > v8::HandleScope scope(args.GetIsolate()); > > v8::String::Utf8Value value(args.GetIsolate(), args[0]); > std::string str(*value); > > std::cout << str; > } > > public: > v8::Isolate* GetIsolte(); > > void Initialize(); > > void Run(const char* source_code); > }; > } > > The implemenation. > > #include <iostream> > > #include "include/v8.h" > #include "include/libplatform/libplatform.h" > #include "v8api.h" > > namespace v8_api > { > v8::Isolate* Core::GetIsolte() > { > return isolate_; > } > > void Core::Initialize() > { > std::cout << "Initializing v8_api::Core\n"; > > std::unique_ptr<v8::Platform> 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(); > > isolate_ = v8::Isolate::New(create_params); > v8::Isolate::Scope isolate_scope(isolate_); > > const char* src = "function test() { return \"from internal > calling.\\n\"; } test();"; > Run(src); > } > > void Core::Run(const char* source_code) > { > v8::Isolate::Scope isolate_scope(isolate_); > { > v8::HandleScope handle_scope(isolate_); > { > v8::Local<v8::Context> context = > v8::Context::New(isolate_); > v8::Context::Scope context_scope(context); > { > v8::MaybeLocal<v8::String> m_source = > v8::String::NewFromUtf8(isolate_, source_code); > v8::Local<v8::String> source = > m_source.ToLocalChecked(); > v8::Local<v8::Script> script = > v8::Script::Compile(context, source).ToLocalChecked(); > v8::Local<v8::Value> result = > script->Run(isolate_->GetCurrentContext()).ToLocalChecked(); > v8::String::Utf8Value resultStr(isolate_, result); > std::cout << *resultStr; > } > } > } > } > } > > Lastly, it's a client code. > > #include <iostream> > #include <thread> > #include "v8api.h" > > int main(int argc, char* argv[]) > { > std::cout << "Hello World!\n"; > > std::unique_ptr<v8_api::Core> core(new v8_api::Core()); > core->Initialize(); > > const char* src = "function test() { return \"from external > calling.\\n\"; } test();"; > core->Run(src); > > std::this_thread::sleep_for(std::chrono::milliseconds(1000)); > > return 0; > } > > Above codes are working well but if I comment out invoking `Run` method in > the `Initialize` method then the invoking second `Run` will fail. > > The commented out code is like below. > > void Core::Initialize() > { > std::cout << "Initializing v8_api::Core\n"; > > std::unique_ptr<v8::Platform> 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(); > > isolate_ = v8::Isolate::New(create_params); > v8::Isolate::Scope isolate_scope(isolate_); > > // Comment out here. > // const char* src = "function test() { return \"from internal > calling.\\n\"; } test();"; > // Run(src); > } > > -------------------- > > First one will show me like this. > > D:\C++Projects\v8client\x64\Release>v8client.exe > Hello World! > Initializing v8_api::Core > from internal calling. > from external calling. > > But second one is like this. > > D:\C++Projects\v8client\x64\Release>v8client.exe > Hello World! > Initializing v8_api::Core > > Nothing anymore. > > ------------------- > > What's my mistake? Why first one can get result? > -- -- v8-users mailing list v8-users@googlegroups.com http://groups.google.com/group/v8-users --- You received this message because you are subscribed to the Google Groups "v8-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to v8-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/v8-users/ac73a0b2-abb1-49aa-82e0-ca52b7222b8fn%40googlegroups.com.