<commit_before>/*
 * v8cgi app file. Loosely based on V8's "shell" sample app.
 */

#include <sstream>
#include <vector>
#include <stdlib.h>
#include <string.h>
#include <v8.h>

#ifdef FASTCGI
#  include <fcgi_stdio.h>
#endif

#include "js_app.h"
#include "js_system.h"
#include "js_io.h"
#include "js_socket.h"
#include "js_macros.h"
#include "js_cache.h"
#include "js_path.h"

#ifndef windows
#   include <dlfcn.h>
#	define DLSYM dlsym
#else
#   include <windows.h>
#	define DLSYM GetProcAddress
#   define dlsym(x,y) (void*)GetProcAddress((HMODULE)x,y)
#endif

JS_METHOD(_include) {
	v8cgi_App * app = APP_PTR;
	v8::String::Utf8Value file(args[0]);
	v8::Handle<v8::Value> result;
	try {
		result = app->include(*file, true, true); /* populate, wrap */
	} catch (std::string e) {
		return JS_EXCEPTION(e.c_str());
	}
	return result;
}

JS_METHOD(_require) {
	v8cgi_App * app = APP_PTR;
	v8::String::Utf8Value file(args[0]);
	v8::Handle<v8::Value> result;
	try {
		result = app->include(*file, false, true); /* do not populate, wrap */
	} catch (std::string e) {
		return JS_EXCEPTION(e.c_str());
	}
	return result;
}

JS_METHOD(_onexit) {
	v8cgi_App * app = APP_PTR;

	v8::Persistent<v8::Function> fun  = v8::Persistent<v8::Function>::New(v8::Handle<v8::Function>::Cast(args[0]));
	app->onexit.push_back(fun);
	return v8::Undefined();
}

JS_METHOD(_exit) {
	v8::Context::GetCurrent()->Exit();
	return v8::Undefined();
}

// Format for command line arguments
//
// as you can see if you wish to pass any arguments to v8, you MUST
// put a -- surrounded by whitespace after all the v8 arguments
//
// any arguments after the v8_args but before the program_file are
// used by v8cgi.
static const char * v8cgi_usage = "v8cgi [v8_args --] [-c path] program_file [argument ...]";

int v8cgi_App::init(int argc, char ** argv) {
	this->cfgfile = STRING(CONFIG_PATH);

	try {
		this->process_args(argc, argv);
	} catch (std::string e) {
		this->error(e.c_str(), __FILE__, __LINE__); /* initialization error -> goes to stderr */
		return 1;
	}

	return 0;
}

int v8cgi_App::execute(char ** envp, bool change) {
	v8::HandleScope handle_scope;
	v8::Handle<v8::ObjectTemplate> globaltemplate = v8::ObjectTemplate::New();
	globaltemplate->SetInternalFieldCount(2);
	v8::Handle<v8::Context> context = v8::Context::New(NULL, globaltemplate);
	v8::Context::Scope context_scope(context);

	try {
		this->prepare(envp); /* prepare JS environment */
	} catch (std::string e) {
		this->error(e.c_str(), __FILE__, __LINE__); /* error with config file or default libs -> goes to stderr */
		this->finish();
		return 1;
	}

	try { 
		this->findmain();  /* try to locate main file */
	}  catch (std::string e) {
		this->error(e.c_str(), __FILE__, __LINE__); /* cannot locate main file -> goes to stderr */
		this->finish();
		return 1;
	}
	
	if (change) { path_chdir(path_dirname(this->mainfile)); } /* if requested, chdir */
	std::string current = path_getcwd(); /* add current path to stack */
	this->paths.push(current);
	this->http(); /* setup builtin request and response, if running as CGI */

	try {
		this->include(this->mainfile, false, false); /* do not populate, do not wrap */
	} catch (std::string e) {
//		this->error(e.c_str(), __FILE__, __LINE__); /* error when executing main file -> goes to ??? */
		this->js_error(e.c_str()); /* error when executing main file -> goes to ??? */
		this->finish();
		return 1;
	}
	
	this->finish();
	return 0;
}
<commit_msg>error reporting fix<commit_after>/*
 * v8cgi app file. Loosely based on V8's "shell" sample app.
 */

#include <sstream>
#include <vector>
#include <stdlib.h>
#include <string.h>
#include <v8.h>

#ifdef FASTCGI
#  include <fcgi_stdio.h>
#endif

#include "js_app.h"
#include "js_system.h"
#include "js_io.h"
#include "js_socket.h"
#include "js_macros.h"
#include "js_cache.h"
#include "js_path.h"

#ifndef windows
#   include <dlfcn.h>
#	define DLSYM dlsym
#else
#   include <windows.h>
#	define DLSYM GetProcAddress
#   define dlsym(x,y) (void*)GetProcAddress((HMODULE)x,y)
#endif

JS_METHOD(_include) {
	v8cgi_App * app = APP_PTR;
	v8::String::Utf8Value file(args[0]);
	v8::Handle<v8::Value> result;
	try {
		result = app->include(*file, true, true); /* populate, wrap */
	} catch (std::string e) {
		return JS_EXCEPTION(e.c_str());
	}
	return result;
}

JS_METHOD(_require) {
	v8cgi_App * app = APP_PTR;
	v8::String::Utf8Value file(args[0]);
	v8::Handle<v8::Value> result;
	try {
		result = app->include(*file, false, true); /* do not populate, wrap */
	} catch (std::string e) {
		return JS_EXCEPTION(e.c_str());
	}
	return result;
}

JS_METHOD(_onexit) {
	v8cgi_App * app = APP_PTR;

	v8::Persistent<v8::Function> fun  = v8::Persistent<v8::Function>::New(v8::Handle<v8::Function>::Cast(args[0]));
	app->onexit.push_back(fun);
	return v8::Undefined();
}

JS_METHOD(_exit) {
	v8::Context::GetCurrent()->Exit();
	return v8::Undefined();
}

// Format for command line arguments
//
// as you can see if you wish to pass any arguments to v8, you MUST
// put a -- surrounded by whitespace after all the v8 arguments
//
// any arguments after the v8_args but before the program_file are
// used by v8cgi.
static const char * v8cgi_usage = "v8cgi [v8_args --] [-c path] program_file [argument ...]";

int v8cgi_App::init(int argc, char ** argv) {
	this->cfgfile = STRING(CONFIG_PATH);

	try {
		this->process_args(argc, argv);
	} catch (std::string e) {
		this->error(e.c_str(), __FILE__, __LINE__); /* initialization error -> goes to stderr */
		return 1;
	}

	return 0;
}

int v8cgi_App::execute(char ** envp, bool change) {
	v8::HandleScope handle_scope;
	v8::Handle<v8::ObjectTemplate> globaltemplate = v8::ObjectTemplate::New();
	globaltemplate->SetInternalFieldCount(2);
	v8::Handle<v8::Context> context = v8::Context::New(NULL, globaltemplate);
	v8::Context::Scope context_scope(context);

	try {
		this->prepare(envp); /* prepare JS environment */
	} catch (std::string e) {
		this->error(e.c_str(), __FILE__, __LINE__); /* error with config file or default libs -> goes to stderr */
		this->finish();
		return 1;
	}

	try { 
		this->findmain();  /* try to locate main file */
	}  catch (std::string e) {
		this->error(e.c_str(), __FILE__, __LINE__); /* cannot locate main file -> goes to stderr */
		this->finish();
		return 1;
	}
	
	if (change) { path_chdir(path_dirname(this->mainfile)); } /* if requested, chdir */
	std::string current = path_getcwd(); /* add current path to stack */
	this->paths.push(current);
	this->http(); /* setup builtin request and response, if running as CGI */

	try {
		this->include(this->mainfile, false, false); /* do not populate, do not wrap */
	} catch (std::string e) {
		this->error(e.c_str(), __FILE__, __LINE__); /* error when executing main file -> goes to ??? */
//		this->js_error(e.c_str()); /* error when executing main file -> goes to ??? */
		this->finish();
		return 1;
	}
	
	this->finish();
	return 0;
}

{'fraction_non_alphanumeric': 0.17047756874095513, 'fraction_numerical': 0.01447178002894356, 'mean_word_length': 4.2957854406130265, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 89, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7097910', 'n_tokens_mistral': 2647, 'n_tokens_neox': 2550, 'n_words': 646}