Skip to content

Commit

Permalink
v8 version command line support
Browse files Browse the repository at this point in the history
64bits compilation doc
  • Loading branch information
Nicolas Ferrero authored and einars committed Oct 14, 2010
1 parent e35f114 commit 31c3c10
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 30 deletions.
17 changes: 12 additions & 5 deletions v8/README.txt
Expand Up @@ -16,11 +16,18 @@ i.e. Python and scons.

Build steps:

svn checkout http://v8.googlecode.com/svn/trunk lib
cd lib && scons mode=release
cd ..
g++ -o jsbeautify jsbeautify.cpp -Ilib/include/ -lv8 -Llib -m32

For 32 Bits:
svn checkout http://v8.googlecode.com/svn/trunk lib
cd lib && scons mode=release
cd ..
g++ -o jsbeautify jsbeautify.cpp -Ilib/include/ -lv8 -Llib -m32

For 64 Bits:
svn checkout http://v8.googlecode.com/svn/trunk lib
cd lib && scons mode=release arch=x64
cd ..
g++ -o jsbeautify jsbeautify.cpp -Ilib/include/ -lv8 -Llib -lpthread

How to use:

./jsbeautify somefile.js
Expand Down
119 changes: 94 additions & 25 deletions v8/jsbeautify.cpp
@@ -1,7 +1,8 @@
/*
Copyright (C) 2010 Ariya Hidayat <ariya.hidayat@gmail.com>
Copyright (c) 2010 Ariya Hidayat <ariya.hidayat@gmail.com>
Copyright (c) 2009 Einar Lielmanis
Copyright (c) 2010 Nicolas Ferrero <ferrero.nicolas@gmail.com>
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
Expand All @@ -26,54 +27,122 @@

#include <v8.h>

#include <string.h>

#include "beautify.h"

using namespace v8;

int main(int argc, char* argv[])
{
if (argc < 2) {
printf("Usage: jsbeautify filename\n\n");
return 0;
Handle<String> readFile(const char* name) {
FILE* file = fopen(name, "rb");

if (file == NULL) {
return Handle<String>();
}

FILE* f = fopen(argv[1], "r");
if (!f) {
printf("Error: unable to open file %s\n", argv[1]);
return 0;
}
int len = 0;
while (!feof(f)) {
fgetc(f);
++len;
}
rewind(f);

fseek(file, 0, SEEK_END);
int len = ftell(file);
rewind(file);

char* buf = new char[len + 1];
fread(buf, 1, len, f);
fclose(f);
buf[len] = '\0';

fread(buf, 1, len, file);

fclose(file);

Handle<String> result = String::New(buf);

delete[] buf;

return result;
}


static void usage(char* progname)
{
printf("Usage: %s [options] source-file\n", progname);
printf("[options]\n");
printf(" --indent-size or -s: Indentation size. (default 4)\n");
printf(" --indent-char or -c: Character to indent with. (default space)\n");
printf(" --disable-preserve-newlines or -d: Do not preserve existing line breaks.\n");
printf(" --indent-level or -l: Initial indentation level, you probably won't need this ever. (default 0)\n");
printf(" --space-after-anon-function or -f: Space is added between \"function ()\", otherwise the common \"function()\" output is used.\n");
printf(" --braces-on-own-line or -b: ANSI / Allman brace style, each opening/closing brace gets its own line.\n");
printf(" --keep-array-indentation or -k: Keep array indentation.\n");
printf(" --help or -h: Prints this help statement.\n");
}

int main(int argc, char* argv[])
{
Handle<String> source;
HandleScope handle_scope;

Handle<ObjectTemplate> global = ObjectTemplate::New();
Handle<ObjectTemplate> options = ObjectTemplate::New();

for (int argpos = 1; argpos < argc; ++argpos) {
if (argv[argpos][0] != '-') {
source = readFile(argv[argpos]);

} else if (strcmp(argv[argpos], "--indent-size") == 0 ||
strcmp(argv[argpos], "-s") == 0) {
options->Set("indent_size", String::New(argv[argpos+1]));

} else if (strcmp(argv[argpos], "--indent-char") == 0 ||
strcmp(argv[argpos], "-c") == 0) {
options->Set("indent_char", String::New(argv[argpos+1]));

} else if (strcmp(argv[argpos], "--disable-preserve-newlines") == 0 ||
strcmp(argv[argpos], "-d") == 0) {
options->Set("preserve_newlines", Boolean::New(false));

} else if (strcmp(argv[argpos], "--indent-level") == 0 ||
strcmp(argv[argpos], "-l") == 0) {
options->Set("indent_level", String::New(argv[argpos+1]));

} else if (strcmp(argv[argpos], "--space-after-anon-function") == 0 ||
strcmp(argv[argpos], "-f") == 0) {
options->Set("space_after_anon_function", Boolean::New(true));

} else if (strcmp(argv[argpos], "--braces-on-own-line") == 0 ||
strcmp(argv[argpos], "-b") == 0) {
options->Set("braces_on_own_line", Boolean::New(true));

} else if (strcmp(argv[argpos], "--keep-array-indentation") == 0 ||
strcmp(argv[argpos], "-k") == 0) {
options->Set("keep_array_indentation", Boolean::New(true));

} else if (strcmp(argv[argpos], "--help") == 0 ||
strcmp(argv[argpos], "-h") == 0) {
usage(argv[0]);
return -1;
}
}

if (source.IsEmpty()) {
usage(argv[0]);
return -1;
}

global->Set("code", String::New(buf, len));

global->Set("source", source);
global->Set("options", options);

Handle<Context> context = Context::New(NULL, global);

Context::Scope context_scope(context);

Handle<Script> beautifyScript = Script::Compile(String::New(beautify_code));
beautifyScript->Run();

Handle<Script> runnerScript = Script::Compile(String::New("js_beautify(code)"));
Handle<Script> runnerScript = Script::Compile(String::New("js_beautify(source, options);"));
Handle<Value> result = runnerScript->Run();

if (!result.IsEmpty() && !result->IsUndefined()) {
String::Utf8Value str(result);
printf("%s\n", *str);
}

delete [] buf;
return 0;
}

0 comments on commit 31c3c10

Please sign in to comment.