D8 on Windows

The D8 console runner includes the ability to execute system shell commands on Posix systems. Unfortunately, this ability is absent from the Windows version of the D8 console runner.

To accommodate this requires some small changes to the V8 source code before building the solution on Windows.


d8.cc (line 875)

We need to change this compiler directive to allow OS Methods to be added in Windows compilations:

From: #if !defined(V8_SHARED) && !defined(_WIN32) && !defined(_WIN64) To: #if !defined(V8_SHARED)


d8-windows.cc (line 29-39)

Now, we need to provide an implementation of the OS Shell Methods for Windows. Change the implementation of this class:

From: #include "d8.h" #include "d8-debug.h" #include "debug.h" #include "api.h" namespace v8 { void Shell::AddOSMethods(Handle os_templ) { } To: #include "d8.h" #include "d8-debug.h" #include "debug.h" #include "api.h" #include "string" #include "iostream" #include "stdio.h" namespace v8 { void Shell::AddOSMethods(Handle os_templ) { os_templ->Set(String::New("system"), FunctionTemplate::New(System)); } void Shell::System(const FunctionCallbackInfo& args) { std::string command = ""; for (int i = 0; i < args.Length(); i++) { std::string str(*v8::String::Utf8Value(args[i])); command += " "; command += str; } args.GetReturnValue().SetUndefined(); if (!command.empty()) { command = "cmd.exe /C \"" + command + "\"2>&1"; std::string result = ""; FILE* pipe = NULL; pipe = _popen(command.c_str(), "r"); if (!pipe) { v8::Local error = String::New("Error running command."); args.GetReturnValue().Set(ThrowException(error)); } else { char buffer[128]; while (!feof(pipe)) { if (fgets(buffer, 128, pipe) != NULL) { result += buffer; } } int status = _pclose(pipe); if (status != 0) { v8::Local output = v8::String::New(result.c_str()); args.GetReturnValue().Set(ThrowException(output)); } else { args.GetReturnValue().Set(v8::String::New(result.c_str())); } } } }

If you prefer not to make these changes yourself, you can download a compiled version of the Windows d8 executable with these changes already integrated from the Supported Host Engines page.