Hi I am willing to embedded MathJax for an applicaton I am developing. I chose V8 as the JS engine but I am having problem loading MathJax.
The issue I get is that "Map" is undefined. Shouldn't this be a standard JavaScript built-in object?
#include <stdio.h>
#include <v8.h>
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
v8::Handle< v8::Value > include( const v8::Arguments & args ) {
for ( int i = 0; i < args.Length(); i++ )
{
v8::String::Utf8Value str( args[ i ] );
std::string js_file;
std::ifstream in_file( *str );
if ( in_file )
{
std::ostringstream ofs;
ofs << in_file.rdbuf();
js_file = ofs.str();
}
if( js_file.length() > 0 ) {
v8::Handle< v8::String > source = v8::String::New( js_file.c_str() );
v8::Handle< v8::Script > script = v8::Script::Compile( source );
return script->Run();
}
}
return v8::Undefined();
}
int main( int argc, char* argv[] ) {
v8::V8::Initialize();
v8::Isolate * isolate = v8::Isolate::New();
{
// Create a stack-allocated isolate scope.
v8::Isolate::Scope isolate_scope( isolate );
// Create a stack-allocated handle scope.
v8::HandleScope handle_scope;
v8::Handle< v8::ObjectTemplate > global = v8::ObjectTemplate::New();
global->Set( v8::String::New( "include" ), v8::FunctionTemplate::New( include ) );
// Create a new context.
v8::Handle< v8::Context > context = v8::Context::New( NULL, global );
// Enter the created context
v8::Context::Scope context_scope( context );
v8::Handle< v8::String > source = v8::String::New(
"include( \"/home/elias/mathjax/es5/tex-mml-svg.js\" )"
);
// Compile the source code.
v8::Handle< v8::Script > script = v8::Script::Compile( source );
// Run the script to get the result.
v8::Handle< v8::Value > result = script->Run();
// Convert the result to an ASCII string and print it.
v8::String::AsciiValue ascii( result );
printf( "%s\n", * ascii );
}
// Dispose the isolate and tear down V8.
isolate->Dispose();
v8::V8::Dispose();
return 0;
}
Above you can see the code I am using. I had even to implement the include() function, which seems to be present when scripts are run in browsers.
I was imagining that it could be possible to run any scrpit using V8, but it seems not to be the case.