Running V8 Javascript Engine Standalone

前端 未结 9 1877
悲&欢浪女
悲&欢浪女 2020-11-27 09:05

I want to run a Javascript console on top of V8. How do I do this?

9条回答
  •  执念已碎
    2020-11-27 09:51

    V8 is easy to build and does not come with the Java VM overhead from Mozilla's standalone Javascript interpreter. Luckily, V8 ships with code for building a console. Here is how to build this:

    $> svn co http://v8.googlecode.com/svn/trunk v8-trunk
    ...
    $> cd v8-trunk
    $> scons
    $> g++ ./samples/shell.cc -o v8-shell -I include libv8.a 
    

    Now, we have a standalone binary called v8-shell.

    Running the console:

    $> ./v8-shell 
    V8 version 2.0.2
    > var x = 10;
    > x
    10
    > function foo(x) { return x * x; }
    > foo
    function foo(x) { return x * x; }
    > quit()
    

    Executing Javascript from the command line:

    $> ./v8-shell -e 'print("10*10 = " + 10*10)'
    10*10 = 100
    

    Many more features are documented in the help:

    $> ./v8-shell --help
    Usage:
    ...
    

提交回复
热议问题