简体   繁体   English

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

[英]How to perform JS syntax check with v8 or rhino?

I want to use a JS engine such as v8 or rhino to perform a syntax check without actually executing the code. 我想使用诸如v8或rhino之类的JS引擎来执行语法检查而不实际执行代码。 Is it possible with the command line versions, or with the corresponding libraries? 是否可以使用命令行版本或相应的库? Any helpful docs? 任何有用的文档?

I had some limited success with v8: 我在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;
}

I hope you guys can beat this essentially binary syntax checker. 我希望你们能打败这个本质上是二进制的语法检查器。 I may try to improve upon it myself by catching exceptions and trying to make the output machine-readable if nothing better comes up. 我可以尝试通过捕获异常并尝试使输出机器可读,如果没有更好的方法来改进它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM