Skip to content

Instantly share code, notes, and snippets.

@evanxg852000
Created August 3, 2017 20:19
Show Gist options
  • Save evanxg852000/dd8bf5e9d9bbde9afbe3b51fbd02a893 to your computer and use it in GitHub Desktop.
Save evanxg852000/dd8bf5e9d9bbde9afbe3b51fbd02a893 to your computer and use it in GitHub Desktop.
Embeding V8 Demo
#include "stdafx.h"
#include <v8.h>
#include <string>
#include <iostream>
#include <fstream>
v8::Handle<v8::ObjectTemplate> global; //global object -> window in our web plannette
v8::HandleScope handle_scope;
v8::Handle<v8::Context> context;
static char* readFile(char *file) {
std::ifstream ifs (file);
std::string file_content;
std::getline(ifs, file_content, (char) ifs.eof());
return const_cast<char*>(file_content.c_str());
}
//convert unsigned int to value
static v8::Local<v8::Value> v8_uint32(unsigned int x) {
return v8::Uint32::New(x);
}
// Add two numbers
v8::Handle<v8::Value> Plus(const v8::Arguments& args)
{
unsigned int A = args[0]->Uint32Value();
unsigned int B = args[1]->Uint32Value();
return v8_uint32(A + B);
}
// The callback that is invoked by v8 whenever the JavaScript 'print'
// function is called. Prints its arguments on stdout separated by
// spaces and ending with a newline.
v8::Handle<v8::Value> Print(const v8::Arguments& args) {
bool first = true;
for (int i = 0; i < args.Length(); i++) {
v8::HandleScope handle_scope;
if (first){
first = false;
} else {
printf(" ");
}
//convert the args[i] type to normal char* string
v8::String::AsciiValue str(args[i]);
printf("%s", *str);
}
printf("\n");
//returning Undefined is the same as returning void...
return v8::Undefined();
}
// Executes a string within the current v8 context.
bool ExecuteString(v8::Handle<v8::String> source,v8::Handle<v8::String> name) {
//access global context within this scope
v8::Context::Scope context_scope(context);
//exception handler
v8::TryCatch try_catch;
//compile script to binary code - JIT
v8::Handle<v8::Script> script = v8::Script::Compile(source, name);
bool print_result = true;
//check if we got problems on compilation
if (script.IsEmpty()) {
// Print errors that happened during compilation.
v8::String::AsciiValue error(try_catch.Exception());
printf("%s\n", *error);
return false;
} else {
//no errors , let's continue
v8::Handle<v8::Value> result = script->Run();
//check if execution ended with errors
if(result.IsEmpty()) {
// Print errors that happened during execution.
v8::String::AsciiValue error(try_catch.Exception());
printf("%s\n", *error);
return false;
} else {
if (print_result && !result->IsUndefined()) {
// If all went well and the result wasn't undefined then print
// the returned value.
v8::String::AsciiValue str(result);
printf("script result [%s]\n", *str);
}
return true;
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
char *script = readFile("main.js");
char *script_name = "internal_script";
//convert the string with the script to a v8 string
v8::Handle<v8::String> source = v8::String::New(script, strlen(script));
//each script name must be unique , for this demo I just run one embedded script, so the name can be fixed
v8::Handle<v8::String> name = v8::String::New(script_name,strlen(script_name));
// Create a template for the global object.
global = v8::ObjectTemplate::New();
//associates "plus" on script to the Plus function
global->Set(v8::String::New("plus"), v8::FunctionTemplate::New(Plus));
//associates "print" on script to the Print function
global->Set(v8::String::New("print"), v8::FunctionTemplate::New(Print));
//create context for the script
context = v8::Context::New(NULL, global);
//simple javascritp to test
ExecuteString(source, name);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment