嗨,我愿意为我正在开发的应用程序嵌入MathJax。我选择V8作为JS引擎,但是在加载MathJax时遇到问题。
我得到的问题是“地图”未定义。这不是标准的JavaScript内置对象吗?
#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;
}
上面您可以看到我正在使用的代码。我什至必须实现include()函数,该函数似乎在浏览器中运行脚本时出现。
我以为使用V8可以运行任何scrpit,但事实并非如此。
[看到#include <v8.h>
让我猜想您在Ubuntu上使用的是libv8-dev软件包,长期以来,这是V8的3.14分支,可追溯到2012年。Map
已添加到EcmaScript 2015中的标准中(aka ES6),并于2014年在V8的3.28分支中发货。
我建议您从would be a good choice right now获取最新的V8版本(8.3 v8.dev)。