代码之家  ›  专栏  ›  技术社区  ›  Alexei Averchenko

如何使用v8或rhino执行JS语法检查?

  •  3
  • Alexei Averchenko  · 技术社区  · 12 年前

    我想使用一个JS引擎,比如v8或rhino来执行语法检查,而不需要实际执行代码。是否可以使用命令行版本,或者使用相应的库?有什么有用的文档吗?

    1 回复  |  直到 11 年前
        1
  •  0
  •   Alexei Averchenko    12 年前

    我在v8上取得了一些有限的成功:

    /*
     * main.cpp
     *
     *  Created on: Aug 22, 2013
     *      Author: kallikanzarid
     */
    
    #include <v8.h>
    #include <iostream>
    
    int main() {
        using namespace std;
        using namespace v8;
    
        //
        // See https://developers.google.com/v8/get_started
        //
    
        // Create a stack-allocated handle scope.
        HandleScope handle_scope;
    
        // Create a new context.
        Handle<Context> context = Context::New();
    
        // Here's how you could create a Persistent handle to the context, if needed.
        Persistent<Context> persistent_context(context);
    
        // Enter the created context for compiling and
        // running the hello world script.
        Context::Scope context_scope(context);
    
        Local<String> source = String::New("function foo(x { console.log(x); }; foo('bar')");
        Local<Script> script = Script::New(source);
    
        persistent_context.Dispose();
    
        return 0;
    }
    

    我希望你们能打败这个本质上是二进制语法检查器。如果没有更好的结果,我可能会尝试自己通过捕捉异常来改进它,并尝试使输出具有机器可读性。