3

I want to use a JS engine such as v8 or rhino to perform a syntax check without actually executing the code. Is it possible with the command line versions, or with the corresponding libraries? Any helpful docs?

Alexei Averchenko
  • 1,706
  • 1
  • 16
  • 29
  • Have you ruled out the possibility of tools like [JSHint](http://jshint.com/) or [JSLint](http://jslint.com/)? – Jonathan Lonowski Aug 22 '13 at 07:38
  • @JonathanLonowski No, do they catch all syntactic errors or just some? I don't need a style enforcer because the code in question is machine generated. – Alexei Averchenko Aug 22 '13 at 07:39
  • 1
    Check: http://stackoverflow.com/questions/2554519/javascript-parser-in-javascript – jcubic Aug 22 '13 at 07:42
  • @jcubic I don't need a JS compiler written in JS, especially if it's not as well-tested as v8 or rhino. – Alexei Averchenko Aug 22 '13 at 09:32
  • @AlexeiAverchenko not js compiler but js parser. But if you want to use v8 then you need to read the code and find the parser there. I think it's simpler to use parser writen in javascript. – jcubic Aug 22 '13 at 09:39

1 Answers1

0

I had some limited success with 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.

Alexei Averchenko
  • 1,706
  • 1
  • 16
  • 29