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_; v8::Local<v8::Context> context_; 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 <fstream> #include <sstream> #include <string> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <thread> #include "include/v8.h" #include "include/libplatform/libplatform.h" #include "v8api.h" void ReadFile(const char* filePath, std::string& contents) { std::ifstream f(filePath); std::stringstream buffer; buffer << f.rdbuf(); contents = buffer.str(); } static void LogCallback(const v8::FunctionCallbackInfo<v8::Value>& args) { if (args.Length() < 1) return; v8::Isolate* isolate = args.GetIsolate(); v8::HandleScope scope(isolate); v8::Local<v8::Value> arg = args[0]; v8::String::Utf8Value value(isolate, arg); std::string str(*value); std::cout << str; } 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 shows me like this. D:\C++Projects\v8client\x64\Release>v8client.exe Hello World! Initializing v8_api::Core I supposed to see "from external calling". but 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/5e3c2196-616d-400f-af8e-7600dca53091n%40googlegroups.com.