I am new to v8.
I am trying to execute a JavaScript function written in an external file.I
am trying to call the function through another java script function written
in v8 and pass the argument from the main program.But i have no clue how to
retrieve the function like a script. we are only able to retrieve it like
a string.The program I am trying is given below
#include <v8.h> #include <stdio.h> #include <stdlib.h>> #include <iostream>
#include <fstream> #include <string> using namespace v8; int main(int argc,
char* argv[]) { // Create a stack-allocated handle scope. HandleScope
handle_scope; // Create a new context. Persistent<Context> context =
Context::New(); //context->AllowCodeGenerationFromStrings(true); // Enter
the created context for compiling and // running the hello world script.
Context::Scope context_scope(context); Local<String> source; Local<Script>
script; Local<Value> result; // Create a string containing the JavaScript
source code. source = String::New("function test_function() { var
ans=arguments[0];return ans;}"); // Compile the source code. script =
Script::Compile(source); // Run the script to get the result. result =
script->Run(); // Dispose the persistent context. context.Dispose(); //
Convert the result to an ASCII string and print it. //String::AsciiValue
ascii(result); //printf("%s\n", *ascii); Handle<v8::Object> global =
context->Global(); Handle<v8::Value> value =
global->Get(String::New("test_function")); Handle<v8::Function> func =
v8::Handle<v8::Function>::Cast(value); Handle<Value> args[1]; Handle<Value>
js_result; // Code to load a file into a string char c[1000]; FILE *fptr;
if ((fptr=fopen("sum.js","r"))==NULL) { std::printf("Error! opening file");
//exit(1); /* Program exits if file pointer returns NULL. */ }
fscanf(fptr,"%[^\n]",c); std::printf("Data from file: This is the out put
of CODE TO LOAD A FILE INTO A STRING\n%s\n\n",c); fclose(fptr); // End of
code to load a file into a string int a,b; std::cout<<"Enter first:\n";
std::cin>>a; std::cout<<"Enter Second:\n"; std::cin>>b; args[0] =
v8::String::New(c); //args[3] = v8::Integer::New(line); std::string
final_result; js_result = func->Call(global, 1, args); String::AsciiValue
ascii(js_result); final_result = atoi(*ascii); Handle<Context> context1 =
Context::New(); Context::Scope context_scope1(context1); //Local<String>
source1; Local<Script> script1; Local<Value> result1; // Create a string
containing the JavaScript source code. //source = String::New("function
test_function() { var ans=arguments[0];return ans;}"); // Compile the
source code. script1 = Script::Compile(final_result); // Run the script to
get the result. result1 = script1->Run(); // Dispose the persistent
context. //context1.Dispose(); // Convert the result to an ASCII string and
print it. //String::AsciiValue ascii(result); //printf("%s\n", *ascii);
Handle<v8::Object> global1 = context1->Global(); Handle<v8::Value> value1 =
global1->Get(String::New("test_function1")); Handle<v8::Function> func1 =
v8::Handle<v8::Function>::Cast(value1); Handle<Value> arg[2]; Handle<Value>
js_result1; int final_result1; arg[0] = v8::Integer::New(a); arg[1] =
v8::Integer::New(b); js_result1= func1->Call(global1, 2, arg); // Convert
the result to an ASCII string and print it. String::AsciiValue
ascii1(js_result1); //printf("This is the out put of JAVASCRIPT
FUNCTION:%s\n\n", *ascii); //String::AsciiValue ascii(js_result);
final_result1 = atoi(*ascii1); std::cout <<"SUM: "<<final_result<<" \n";
return 0; }
The external function is
function test_function1() { var match3 = 0; match3 =
parseInt(arguments[0])+parseInt(arguments[1])+; return match3; }
--
--
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users
---
You received this message because you are subscribed to the Google Groups
"v8-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.