-1

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.

Elias
  • 234
  • 2
  • 9

1 Answers1

1

Seeing #include <v8.h> make me guess that you're using the libv8-dev package on Ubuntu, which for a long time was V8's 3.14 branch, dating back to the year 2012. Map was added to the standard in EcmaScript 2015 (aka ES6) and shipped in V8's 3.28 branch in 2014.

I recommend that you get a recent V8 version (8.3 would be a good choice right now) from v8.dev.

jmrk
  • 34,271
  • 7
  • 59
  • 74
  • You are right, this was the problem. I wasn't expecting it to be that outdated. I had to redo a large part of the code, but it now knows what Map is and MathJax seems to be loading properly. – Elias Jun 02 '20 at 17:47