关注开源代码的实际应用
在程序中嵌入虚拟机可以为程序带来更强大的功能、更好的灵活性,但是与其自己去开发一个虚拟机,还不如直接使用一个大公司开发好了的,何况还是开放源代码的。本文介绍如何在自己的程序中使用Google浏览器Chrome的Javascript虚拟机。
v8 developer page上面可以获取最新的V8代码和开发文档,当然要想在C++中嵌入Javascript虚拟机还需要一定的C++与Javascript知识。
一个简单范例:
- #include
- using namespace v8;
- int main(int argc, char* argv[]) {
- // Create a stack-allocated handle scope.
- HandleScope handle_scope;
- // Create a new context.
- Handle
context = Context::New(); - // Enter the created context for compiling and
- // running the hello world script.
- Context::Scope context_scope(context);
- // Create a string containing the JavaScript source code.
- Handle
source = String::New(“'Hello' + ', World!'”); - // Compile the source code.
- Handle