Hi All,
I have posted the same doubt earlier in this group.
Actually i want to execute an external java script file from v8 code.
I saw lot of code,normally executing javascript inside v8.But didn't
get the
solution for executing an external javascript file by putting file
name inside v8 code in C++.I am still waiting for the solution.What i
did earlier is,i read the content of the file
character by character and assigned it to a v8::String as source then
compile ans run it.
But not getting the output exactly from javascript.Can anyone please
help me out.
//////////////here is the v8 c++ file////////////
#include <v8.h>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std ;
void RunShell(v8::Handle<v8::Context> context);
v8::Handle<v8::String> ReadFile(const char* name);
v8::Handle<v8::Value> Load(const v8::Arguments& args);
v8::Handle<v8::Value> Plus(const v8::Arguments& args);
bool ExecuteJavaScript(v8::Handle<v8::String> source,
v8::Handle<v8::Value> name,
bool print_result,
bool report_exceptions);
void ReportException(v8::TryCatch* handler);
FILE* file;
v8::Handle<v8::String> result;
v8::Handle<v8::String> src1;
char *fileName ;
char *str ="add.js";
int main(int argc, char* argv[])
{
v8::V8::SetFlagsFromCommandLine(&argc, argv, true);
v8::HandleScope handle_scope;
v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New();
v8::Handle<v8::Context> context = v8::Context::New(NULL, global);
v8::Context::Scope context_scope(context);
bool run_shell = (argc == 1);
for (int i = 1; i < argc; i++)
{
const char* str = argv[i];//b4
//const char* str = "add.js";//now
printf("%s", *str);
if (strcmp(str, "--shell") == 0)
{
run_shell = true;
}
else if (strcmp(str, "-f") == 0)
{
continue;
}
else if (strncmp(str, "--", 2) == 0)
{
printf("Warning: unknown flag %s.\n", str);
}
else if (strcmp(str, "-e") == 0 && i + 1 < argc)
{
v8::HandleScope handle_scope;
v8::Handle<v8::String> file_name = v8::String::New("dumy");
// v8::Handle<v8::String> source = v8::String::New(argv[i +
1]);
v8::Handle<v8::String> source = v8::String::New(str);
//Load(*str);
if (!ExecuteJavaScript(source, file_name, false, true))
{
printf("JavaScript not executed.");
return 1;
i++;
}
}
else
{
// Use all other arguments as names of files to load
and run.
v8::HandleScope handle_scope;
v8::Handle<v8::String> file_name = v8::String::New(str);
v8::Handle<v8::String> source = ReadFile(str);
//Load(v8::Arguments *str);
if (source.IsEmpty())
{
printf("Error reading '%s'\n", str);
return 1;
}
if (!ExecuteJavaScript(source, file_name, false, true))
{
printf("Java Script Not Executed .");
return 1;
}
}
}
if (run_shell)
{
RunShell(context);
}
return 0;
}
v8::Handle<v8::Value> Plus(const v8::Arguments& args)
{
unsigned int A = args[0]->Uint32Value();
unsigned int B = args[1]->Uint32Value();
unsigned int C = args[0]->Uint32Value() +args[1]->Uint32Value();
//printf("Value of c is:%d",C);
}
void RunShell(v8::Handle<v8::Context> context)
{
static const int kBufferSize = 256;
char buffer[kBufferSize];
if (str == NULL)
{
printf("Enter a file to Read.");
//break;
}
ReadFile(str);
v8::HandleScope handle_scope;
printf("\n");
}
v8::Handle<v8::String> ReadFile(const char *name)
{
file = fopen(name, "rb");
if (file == NULL)
{
return v8::Handle<v8::String>();
printf("File is empty.");
}
else
{
// printf("\nFile Reading starts....\n");
}
fseek(file, 0, SEEK_END);//sends file pointer to end of the file.
int size = ftell(file);//counts the size of the file in character.
printf("The size of file is:%d\n",size);
rewind(file);
char* chars = new char[size + 1];
chars[size] = '\0';
for (int i = 0; i < size;)
{
int read = fread(&chars[i], 1, size - i, file);//
i += read;
}
fclose(file);
//v8::Handle<v8::String> result;
result = v8::String::New(chars, size);
v8::String::Utf8Value str1(result);
printf("\nRead File is like bellow...\n\n%s",*str1);
ExecuteJavaScript(v8::String::New(str),v8::String::New("dumy
"),true,true);
delete[] chars;
return result;
}
bool ExecuteJavaScript(v8::Handle<v8::String> source,
v8::Handle<v8::Value> name,
bool print_result,
bool report_exceptions)
{
v8::HandleScope handle_scope;
v8::TryCatch try_catch;
printf("\nsource File Name is ->%s\n",str);
v8::Handle<v8::Script> script = v8::Script::Compile(result,
name);
//v8::String::Utf8Value str(result);
//printf("Result",*str);
if (script.IsEmpty())
{
printf("Script is Empty.\n");
if (report_exceptions)
ReportException(&try_catch);
return false;
}
else
{
v8::Handle<v8::Value> result1 = script->Run();
if (!result.IsEmpty())
{
v8::String::Utf8Value str(result1);
printf("Result is not empty.\n%s",*str);
}
if (result.IsEmpty())
{
printf("Result is empty.\n");
if (report_exceptions)
ReportException(&try_catch);
return false;
}
else if (result->IsUndefined())
{
v8::String::Utf8Value str(result1);
printf("\nResult After Execution - undefined\n
%s",*str);
}
else
{
//else if (print_result && !result->IsUndefined
())
if (print_result && !result->IsUndefined())
{
v8::String::Utf8Value str(result1);
printf(" \nResult after executing the File is::\n\n%s",
*str);
}
return true;
}
}
}
void ReportException(v8::TryCatch* try_catch)
{
v8::HandleScope handle_scope;
v8::String::Utf8Value exception(try_catch->Exception());
v8::Handle<v8::Message> message = try_catch->Message();
if (message.IsEmpty())
{
printf("Try_Catch Exception::%s\n", *exception);
}
else
{
v8::String::Utf8Value filename(message->GetScriptResourceName());
int linenum = message->GetLineNumber();
printf("The File Name is: %s\n Line Number: %i\n Exception: %s\n",
*filename, linenum, *exception);
v8::String::Utf8Value sourceline(message->GetSourceLine());
printf("Source Line:%s\n", *sourceline);
int start = message->GetStartColumn();
for (int i = 0; i < start; i++)
{
printf(" ");
}
int end = message->GetEndColumn();
for (int i = start; i < end; i++)
{
printf("^");
}
printf("\n");
}
}
////////////////////Here is the javascript file/////////////////
function add(a,b){
return (a+b);
}
var a = add(3,4);
//////Here is the result after execution////////
source File Name is ->add.js
Result is not empty.
undefined
Result after executing the File is::
undefined
///////////////////////////
Thanks in advance.I think there is a silly mistake anywhere.
Can any one please help me out.
--~--~---------~--~----~------------~-------~--~----~
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users
-~----------~----~----~----~------~----~------~--~---