Merge branch 'waywardmonkeys-remove-spurious-windows-linefeeds'

This commit is contained in:
Oliver Buchtala 2014-10-15 17:30:46 +02:00
commit 6531b16a56

View file

@ -1,16 +1,16 @@
#include <assert.h> #include <assert.h>
#include <fcntl.h> #include <fcntl.h>
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <v8.h> #include <v8.h>
#include <vector> #include <vector>
#include "js_shell.h" #include "js_shell.h"
typedef int (*V8ExtensionInitializer) (v8::Handle<v8::Object> module); typedef int (*V8ExtensionInitializer) (v8::Handle<v8::Object> module);
// Note: these typedefs and defines are used to deal with v8 API changes since version 3.19.00 // Note: these typedefs and defines are used to deal with v8 API changes since version 3.19.00
#if (SWIG_V8_VERSION < 0x031903) #if (SWIG_V8_VERSION < 0x031903)
@ -65,90 +65,90 @@ typedef v8::Persistent<v8::Context> SwigV8Context;
typedef v8::Local<v8::Context> SwigV8Context; typedef v8::Local<v8::Context> SwigV8Context;
#endif #endif
class V8Shell: public JSShell { class V8Shell: public JSShell {
public: public:
V8Shell(); V8Shell();
virtual ~V8Shell(); virtual ~V8Shell();
virtual bool RunScript(const std::string &scriptPath); virtual bool RunScript(const std::string &scriptPath);
virtual bool RunShell(); virtual bool RunShell();
protected: protected:
virtual bool InitializeEngine(); virtual bool InitializeEngine();
virtual bool ExecuteScript(const std::string &source, const std::string &scriptPath); virtual bool ExecuteScript(const std::string &source, const std::string &scriptPath);
virtual bool DisposeEngine(); virtual bool DisposeEngine();
private: private:
v8::Handle<v8::Value> Import(const std::string &moduleName); v8::Handle<v8::Value> Import(const std::string &moduleName);
SwigV8Context CreateShellContext(); SwigV8Context CreateShellContext();
void ReportException(v8::TryCatch *handler); void ReportException(v8::TryCatch *handler);
static SwigV8ReturnValue Print(const SwigV8Arguments &args); static SwigV8ReturnValue Print(const SwigV8Arguments &args);
static SwigV8ReturnValue Require(const SwigV8Arguments &args); static SwigV8ReturnValue Require(const SwigV8Arguments &args);
static SwigV8ReturnValue Quit(const SwigV8Arguments &args); static SwigV8ReturnValue Quit(const SwigV8Arguments &args);
static SwigV8ReturnValue Version(const SwigV8Arguments &args); static SwigV8ReturnValue Version(const SwigV8Arguments &args);
static const char* ToCString(const v8::String::Utf8Value &value); static const char* ToCString(const v8::String::Utf8Value &value);
}; };
#ifdef __GNUC__ #ifdef __GNUC__
#include <dlfcn.h> #include <dlfcn.h>
#define LOAD_SYMBOL(handle, name) dlsym(handle, name) #define LOAD_SYMBOL(handle, name) dlsym(handle, name)
#else #else
#error "implement dll loading" #error "implement dll loading"
#endif #endif
V8Shell::V8Shell() {} V8Shell::V8Shell() {}
V8Shell::~V8Shell() {} V8Shell::~V8Shell() {}
bool V8Shell::RunScript(const std::string &scriptPath) { bool V8Shell::RunScript(const std::string &scriptPath) {
std::string source = ReadFile(scriptPath); std::string source = ReadFile(scriptPath);
v8::Isolate *isolate = v8::Isolate::New(); v8::Isolate *isolate = v8::Isolate::New();
v8::Isolate::Scope isolate_scope(isolate); v8::Isolate::Scope isolate_scope(isolate);
SWIGV8_HANDLESCOPE(); SWIGV8_HANDLESCOPE();
SwigV8Context context = CreateShellContext(); SwigV8Context context = CreateShellContext();
if (context.IsEmpty()) {
printf("Could not create context.\n");
return false;
}
context->Enter();
// Store a pointer to this shell for later use
v8::Handle<v8::Object> global = context->Global(); if (context.IsEmpty()) {
printf("Could not create context.\n");
return false;
}
context->Enter();
// Store a pointer to this shell for later use
v8::Handle<v8::Object> global = context->Global();
v8::Local<v8::External> __shell__ = SWIGV8_EXTERNAL_NEW((void*) (long) this); v8::Local<v8::External> __shell__ = SWIGV8_EXTERNAL_NEW((void*) (long) this);
global->SetHiddenValue(SWIGV8_STRING_NEW("__shell__"), __shell__); global->SetHiddenValue(SWIGV8_STRING_NEW("__shell__"), __shell__);
// Node.js compatibility: make `print` available as `console.log()` // Node.js compatibility: make `print` available as `console.log()`
ExecuteScript("var console = {}; console.log = print;", "<console>"); ExecuteScript("var console = {}; console.log = print;", "<console>");
bool success = ExecuteScript(source, scriptPath); bool success = ExecuteScript(source, scriptPath);
// Cleanup // Cleanup
context->Exit(); context->Exit();
#if (SWIG_V8_VERSION < 0x031710) #if (SWIG_V8_VERSION < 0x031710)
context.Dispose(); context.Dispose();
#elif (SWIG_V8_VERSION < 0x031900) #elif (SWIG_V8_VERSION < 0x031900)
@ -166,32 +166,32 @@ bool V8Shell::RunShell() {
SWIGV8_HANDLESCOPE(); SWIGV8_HANDLESCOPE();
SwigV8Context context = CreateShellContext(); SwigV8Context context = CreateShellContext();
if (context.IsEmpty()) { if (context.IsEmpty()) {
printf("Could not create context.\n"); printf("Could not create context.\n");
return false; return false;
} }
context->Enter(); context->Enter();
v8::Context::Scope context_scope(context); v8::Context::Scope context_scope(context);
ExecuteScript("var console = {}; console.log = print;", "<console>"); ExecuteScript("var console = {}; console.log = print;", "<console>");
static const int kBufferSize = 1024; static const int kBufferSize = 1024;
while (true) { while (true) {
char buffer[kBufferSize]; char buffer[kBufferSize];
printf("> "); printf("> ");
char *str = fgets(buffer, kBufferSize, stdin); char *str = fgets(buffer, kBufferSize, stdin);
if (str == NULL) break; if (str == NULL) break;
std::string source(str); std::string source(str);
ExecuteScript(source, "(shell)"); ExecuteScript(source, "(shell)");
} }
printf("\n"); printf("\n");
// Cleanup // Cleanup
context->Exit(); context->Exit();
#if (SWIG_V8_VERSION < 0x031710) #if (SWIG_V8_VERSION < 0x031710)
context.Dispose(); context.Dispose();
@ -202,53 +202,53 @@ bool V8Shell::RunShell() {
#endif #endif
// v8::V8::Dispose(); // v8::V8::Dispose();
return true; return true;
} }
bool V8Shell::InitializeEngine() { bool V8Shell::InitializeEngine() {
return true; return true;
} }
bool V8Shell::ExecuteScript(const std::string &source, const std::string &name) { bool V8Shell::ExecuteScript(const std::string &source, const std::string &name) {
SWIGV8_HANDLESCOPE(); SWIGV8_HANDLESCOPE();
v8::TryCatch try_catch; v8::TryCatch try_catch;
v8::Handle<v8::Script> script = v8::Script::Compile(SWIGV8_STRING_NEW(source.c_str()), SWIGV8_STRING_NEW(name.c_str())); v8::Handle<v8::Script> script = v8::Script::Compile(SWIGV8_STRING_NEW(source.c_str()), SWIGV8_STRING_NEW(name.c_str()));
// Stop if script is empty // Stop if script is empty
if (script.IsEmpty()) { if (script.IsEmpty()) {
// Print errors that happened during compilation. // Print errors that happened during compilation.
ReportException(&try_catch); ReportException(&try_catch);
return false; return false;
} }
v8::Handle<v8::Value> result = script->Run(); v8::Handle<v8::Value> result = script->Run();
// Print errors that happened during execution. // Print errors that happened during execution.
if (try_catch.HasCaught()) { if (try_catch.HasCaught()) {
ReportException(&try_catch); ReportException(&try_catch);
return false; return false;
} else { } else {
return true; return true;
} }
} }
bool V8Shell::DisposeEngine() { bool V8Shell::DisposeEngine() {
return true; return true;
} }
SwigV8Context V8Shell::CreateShellContext() { SwigV8Context V8Shell::CreateShellContext() {
// Create a template for the global object. // Create a template for the global object.
v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New();
// Bind global functions // Bind global functions
global->Set(SWIGV8_STRING_NEW("print"), SWIGV8_FUNCTEMPLATE_NEW(V8Shell::Print)); global->Set(SWIGV8_STRING_NEW("print"), SWIGV8_FUNCTEMPLATE_NEW(V8Shell::Print));
global->Set(SWIGV8_STRING_NEW("quit"), SWIGV8_FUNCTEMPLATE_NEW(V8Shell::Quit)); global->Set(SWIGV8_STRING_NEW("quit"), SWIGV8_FUNCTEMPLATE_NEW(V8Shell::Quit));
global->Set(SWIGV8_STRING_NEW("require"), SWIGV8_FUNCTEMPLATE_NEW(V8Shell::Require)); global->Set(SWIGV8_STRING_NEW("require"), SWIGV8_FUNCTEMPLATE_NEW(V8Shell::Require));
global->Set(SWIGV8_STRING_NEW("version"), SWIGV8_FUNCTEMPLATE_NEW(V8Shell::Version)); global->Set(SWIGV8_STRING_NEW("version"), SWIGV8_FUNCTEMPLATE_NEW(V8Shell::Version));
#if (SWIG_V8_VERSION < 0x031900) #if (SWIG_V8_VERSION < 0x031900)
SwigV8Context context = v8::Context::New(NULL, global); SwigV8Context context = v8::Context::New(NULL, global);
return context; return context;
@ -256,133 +256,133 @@ SwigV8Context V8Shell::CreateShellContext() {
SwigV8Context context = v8::Context::New(v8::Isolate::GetCurrent(), NULL, global); SwigV8Context context = v8::Context::New(v8::Isolate::GetCurrent(), NULL, global);
return context; return context;
#endif #endif
} }
v8::Handle<v8::Value> V8Shell::Import(const std::string &module_path) v8::Handle<v8::Value> V8Shell::Import(const std::string &module_path)
{ {
SWIGV8_HANDLESCOPE_ESC(); SWIGV8_HANDLESCOPE_ESC();
HANDLE library; HANDLE library;
std::string module_name = LoadModule(module_path, &library); std::string module_name = LoadModule(module_path, &library);
std::string symname = std::string(module_name).append("_initialize"); std::string symname = std::string(module_name).append("_initialize");
V8ExtensionInitializer init_function = reinterpret_cast<V8ExtensionInitializer>((long) LOAD_SYMBOL(library, symname.c_str())); V8ExtensionInitializer init_function = reinterpret_cast<V8ExtensionInitializer>((long) LOAD_SYMBOL(library, symname.c_str()));
if(init_function == 0) { if(init_function == 0) {
printf("Could not find initializer function."); printf("Could not find initializer function.");
return SWIGV8_UNDEFINED(); return SWIGV8_UNDEFINED();
} }
v8::Local<v8::Object> module = SWIGV8_OBJECT_NEW(); v8::Local<v8::Object> module = SWIGV8_OBJECT_NEW();
init_function(module); init_function(module);
SWIGV8_ESCAPE(module); SWIGV8_ESCAPE(module);
} }
SwigV8ReturnValue V8Shell::Print(const SwigV8Arguments &args) { SwigV8ReturnValue V8Shell::Print(const SwigV8Arguments &args) {
SWIGV8_HANDLESCOPE(); SWIGV8_HANDLESCOPE();
bool first = true; bool first = true;
for (int i = 0; i < args.Length(); i++) { for (int i = 0; i < args.Length(); i++) {
if (first) { if (first) {
first = false; first = false;
} else { } else {
printf(" "); printf(" ");
} }
v8::String::Utf8Value str(args[i]); v8::String::Utf8Value str(args[i]);
const char *cstr = V8Shell::ToCString(str); const char *cstr = V8Shell::ToCString(str);
printf("%s", cstr); printf("%s", cstr);
} }
printf("\n"); printf("\n");
fflush(stdout); fflush(stdout);
SWIGV8_RETURN(SWIGV8_UNDEFINED()); SWIGV8_RETURN(SWIGV8_UNDEFINED());
} }
SwigV8ReturnValue V8Shell::Require(const SwigV8Arguments &args) { SwigV8ReturnValue V8Shell::Require(const SwigV8Arguments &args) {
SWIGV8_HANDLESCOPE(); SWIGV8_HANDLESCOPE();
if (args.Length() != 1) { if (args.Length() != 1) {
printf("Illegal arguments for `require`"); printf("Illegal arguments for `require`");
}; };
v8::String::Utf8Value str(args[0]); v8::String::Utf8Value str(args[0]);
const char *cstr = V8Shell::ToCString(str); const char *cstr = V8Shell::ToCString(str);
std::string moduleName(cstr); std::string moduleName(cstr);
v8::Local<v8::Object> global = SWIGV8_CURRENT_CONTEXT()->Global(); v8::Local<v8::Object> global = SWIGV8_CURRENT_CONTEXT()->Global();
v8::Local<v8::Value> hidden = global->GetHiddenValue(SWIGV8_STRING_NEW("__shell__")); v8::Local<v8::Value> hidden = global->GetHiddenValue(SWIGV8_STRING_NEW("__shell__"));
v8::Local<v8::External> __shell__ = v8::Local<v8::External>::Cast(hidden); v8::Local<v8::External> __shell__ = v8::Local<v8::External>::Cast(hidden);
V8Shell *_this = (V8Shell *) (long) __shell__->Value(); V8Shell *_this = (V8Shell *) (long) __shell__->Value();
v8::Handle<v8::Value> module = _this->Import(moduleName); v8::Handle<v8::Value> module = _this->Import(moduleName);
SWIGV8_RETURN(module); SWIGV8_RETURN(module);
} }
SwigV8ReturnValue V8Shell::Quit(const SwigV8Arguments &args) { SwigV8ReturnValue V8Shell::Quit(const SwigV8Arguments &args) {
SWIGV8_HANDLESCOPE(); SWIGV8_HANDLESCOPE();
int exit_code = args[0]->Int32Value(); int exit_code = args[0]->Int32Value();
fflush(stdout); fflush(stdout);
fflush(stderr); fflush(stderr);
exit(exit_code); exit(exit_code);
SWIGV8_RETURN(SWIGV8_UNDEFINED()); SWIGV8_RETURN(SWIGV8_UNDEFINED());
} }
SwigV8ReturnValue V8Shell::Version(const SwigV8Arguments &args) { SwigV8ReturnValue V8Shell::Version(const SwigV8Arguments &args) {
SWIGV8_HANDLESCOPE(); SWIGV8_HANDLESCOPE();
SWIGV8_RETURN(SWIGV8_STRING_NEW(v8::V8::GetVersion())); SWIGV8_RETURN(SWIGV8_STRING_NEW(v8::V8::GetVersion()));
} }
void V8Shell::ReportException(v8::TryCatch *try_catch) { void V8Shell::ReportException(v8::TryCatch *try_catch) {
SWIGV8_HANDLESCOPE(); SWIGV8_HANDLESCOPE();
v8::String::Utf8Value exception(try_catch->Exception()); v8::String::Utf8Value exception(try_catch->Exception());
const char *exception_string = V8Shell::ToCString(exception); const char *exception_string = V8Shell::ToCString(exception);
v8::Handle<v8::Message> message = try_catch->Message(); v8::Handle<v8::Message> message = try_catch->Message();
if (message.IsEmpty()) { if (message.IsEmpty()) {
// V8 didn't provide any extra information about this error; just // V8 didn't provide any extra information about this error; just
// print the exception. // print the exception.
printf("%s\n", exception_string); printf("%s\n", exception_string);
} else { } else {
// Print (filename):(line number): (message). // Print (filename):(line number): (message).
v8::String::Utf8Value filename(message->GetScriptResourceName()); v8::String::Utf8Value filename(message->GetScriptResourceName());
const char *filename_string = V8Shell::ToCString(filename); const char *filename_string = V8Shell::ToCString(filename);
int linenum = message->GetLineNumber(); int linenum = message->GetLineNumber();
printf("%s:%i: %s\n", filename_string, linenum, exception_string); printf("%s:%i: %s\n", filename_string, linenum, exception_string);
// Print line of source code. // Print line of source code.
v8::String::Utf8Value sourceline(message->GetSourceLine()); v8::String::Utf8Value sourceline(message->GetSourceLine());
const char *sourceline_string = V8Shell::ToCString(sourceline); const char *sourceline_string = V8Shell::ToCString(sourceline);
printf("%s\n", sourceline_string); printf("%s\n", sourceline_string);
// Print wavy underline (GetUnderline is deprecated). // Print wavy underline (GetUnderline is deprecated).
int start = message->GetStartColumn(); int start = message->GetStartColumn();
for (int i = 0; i < start; i++) { for (int i = 0; i < start; i++) {
printf(" "); printf(" ");
} }
int end = message->GetEndColumn(); int end = message->GetEndColumn();
for (int i = start; i < end; i++) { for (int i = start; i < end; i++) {
printf("^"); printf("^");
} }
printf("\n"); printf("\n");
v8::String::Utf8Value stack_trace(try_catch->StackTrace()); v8::String::Utf8Value stack_trace(try_catch->StackTrace());
if (stack_trace.length() > 0) { if (stack_trace.length() > 0) {
const char *stack_trace_string = V8Shell::ToCString(stack_trace); const char *stack_trace_string = V8Shell::ToCString(stack_trace);
printf("%s\n", stack_trace_string); printf("%s\n", stack_trace_string);
} }
} }
} }
// Extracts a C string from a V8 Utf8Value. // Extracts a C string from a V8 Utf8Value.
const char *V8Shell::ToCString(const v8::String::Utf8Value &value) { const char *V8Shell::ToCString(const v8::String::Utf8Value &value) {
return *value ? *value : "<string conversion failed>"; return *value ? *value : "<string conversion failed>";
} }
JSShell *V8Shell_Create() { JSShell *V8Shell_Create() {
return new V8Shell(); return new V8Shell();
} }