<commit_before>#pragma once


#include "include/libplatform/libplatform.h"
#include "include/v8.h"



template<class CALLBACK_FUNCTION>
v8::Local<v8::FunctionTemplate> make_function_template(v8::Isolate * isolate, CALLBACK_FUNCTION function)
{		
	return v8::FunctionTemplate::New(isolate, [](const v8::FunctionCallbackInfo<v8::Value>& args){
		(*(CALLBACK_FUNCTION *)v8::External::Cast(*(args.Data()))->Value())(args);
	}, v8::External::New(isolate, (void*)&function));
}

template<class CALLBACK_FUNCTION>
void add_function(v8::Isolate * isolate, v8::Handle<v8::ObjectTemplate> & object_template, const char * name, CALLBACK_FUNCTION function) {
	object_template->Set(isolate, name, make_function_template(isolate, function));
}




#include <boost/format.hpp>
static void print_helper(const v8::FunctionCallbackInfo<v8::Value>& args, bool append_newline) {
	if (args.Length() > 0) {
		auto string = *v8::String::Utf8Value(args[0]);
		auto format = boost::format(string);
		
		for (int i = 1; i < args.Length(); i++) {
			format % *v8::String::Utf8Value(args[i]);
		}
		std::cout << ">>> ";
		std::cout << format;	
		if (append_newline) {
			std::cout << std::endl;
		}
	}
}



void printobj(const v8::FunctionCallbackInfo<v8::Value>& args) {
	for (int i = 0; i < args.Length(); i++) {
		auto object = v8::Object::Cast(*args[i]);
		if(object->InternalFieldCount() > 0) {
			v8::Local<v8::External> wrap = v8::Local<v8::External>::Cast(object->GetInternalField(0));
			printf(">>> Object %p: %s\n", wrap->Value(), *v8::String::Utf8Value(args[i]));
		} else {
			printf(">>> Object does not appear to be a wrapped c++ class (no internal fields): %s\n", *v8::String::Utf8Value(args[i]));
		}
	}
	// v8::Local<v8::Object> self = info.Holder();
	// v8::Local<v8::External> wrap = v8::Local<v8::External>::Cast(self->GetInternalField(0));
	// T * cpp_object = static_cast<T *>(wrap->Value());
}

// call this to add a function called "print" to whatever object template you pass in (probably the global one)
void add_print(v8::Isolate * isolate, v8::Local<v8::ObjectTemplate> global_template )
{
	v8::HandleScope hs(isolate);
	// auto print_template = v8::FunctionTemplate::New(isolate, &print_callback);
	add_function(isolate, global_template, "print", [&](const v8::FunctionCallbackInfo<v8::Value>& args){print_helper(args, false);});
	add_function(isolate, global_template, "println", [&](const v8::FunctionCallbackInfo<v8::Value>& args){print_helper(args, true);});
	add_function(isolate, global_template, "printobj", [&](const v8::FunctionCallbackInfo<v8::Value>& args){printobj(args);});
	
}


<commit_msg>creating v8_toolbox - non-class-wrapping helper stuff and moved print methods out to that.  created helpers for making functiontempaltes and adding them to templates in toolbox<commit_after>#pragma once


#include "include/libplatform/libplatform.h"
#include "include/v8.h"



template<class CALLBACK_FUNCTION>
v8::Local<v8::FunctionTemplate> make_function_template(v8::Isolate * isolate, CALLBACK_FUNCTION function)
{		
	return v8::FunctionTemplate::New(isolate, [](const v8::FunctionCallbackInfo<v8::Value>& args){
		(*(CALLBACK_FUNCTION *)v8::External::Cast(*(args.Data()))->Value())(args);
	}, v8::External::New(isolate, (void*)&function));
}

template<class CALLBACK_FUNCTION>
void add_function(v8::Isolate * isolate, v8::Handle<v8::ObjectTemplate> & object_template, const char * name, CALLBACK_FUNCTION function) {
	object_template->Set(isolate, name, make_function_template(isolate, function));
}




#include <boost/format.hpp>
// takes a format string and some javascript objects and does a printf-style print using boost::format
static void print_helper(const v8::FunctionCallbackInfo<v8::Value>& args, bool append_newline) {
	if (args.Length() > 0) {
		auto string = *v8::String::Utf8Value(args[0]);
		auto format = boost::format(string);
		
		for (int i = 1; i < args.Length(); i++) {
			format % *v8::String::Utf8Value(args[i]);
		}
		std::cout << ">>> ";
		std::cout << format;	
		if (append_newline) {
			std::cout << std::endl;
		}
	}
}

// prints out information about the guts of an object
void printobj(const v8::FunctionCallbackInfo<v8::Value>& args) {
	for (int i = 0; i < args.Length(); i++) {
		auto object = v8::Object::Cast(*args[i]);
		if(object->InternalFieldCount() > 0) {
			v8::Local<v8::External> wrap = v8::Local<v8::External>::Cast(object->GetInternalField(0));
			printf(">>> Object %p: %s\n", wrap->Value(), *v8::String::Utf8Value(args[i]));
		} else {
			printf(">>> Object does not appear to be a wrapped c++ class (no internal fields): %s\n", *v8::String::Utf8Value(args[i]));
		}
	}
}

// call this to add a function called "print" to whatever object template you pass in (probably the global one)
void add_print(v8::Isolate * isolate, v8::Local<v8::ObjectTemplate> global_template )
{
	add_function(isolate, global_template, "print", [&](const v8::FunctionCallbackInfo<v8::Value>& args){print_helper(args, false);});
	add_function(isolate, global_template, "println", [&](const v8::FunctionCallbackInfo<v8::Value>& args){print_helper(args, true);});
	add_function(isolate, global_template, "printobj", [&](const v8::FunctionCallbackInfo<v8::Value>& args){printobj(args);});	
}



{'fraction_non_alphanumeric': 0.1788788980294624, 'fraction_numerical': 0.01874880428544098, 'mean_word_length': 6.500717360114778, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 69, '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': '3572233', 'n_tokens_mistral': 1860, 'n_tokens_neox': 1764, 'n_words': 379}