Skip to content

Instantly share code, notes, and snippets.

@nkuln
Created August 6, 2012 08:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nkuln/3272323 to your computer and use it in GitHub Desktop.
Save nkuln/3272323 to your computer and use it in GitHub Desktop.
V8 Code Sample
// UpaV8PerfTest.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
using namespace v8;
std::string readSourceFile(std::string fileName)
{
std::ifstream t(fileName);
std::string content((std::istreambuf_iterator<char>(t)),
std::istreambuf_iterator<char>());
return content;
}
void printObjectProperties(Handle<Object> obj)
{
String::AsciiValue objascii(obj);
printf("%s\n", *objascii);
Handle<Array> properties = obj->GetPropertyNames();
int len = properties->Length();
printf("Number of properties = %d:\n", len);
for(int i = 0 ; i < len ; ++i)
{
const Handle<Value> key = properties->Get(i);
String::AsciiValue str(key);
printf("\t%d. %s\n", (i + 1), *str);
}
}
void printValue(Handle<Value> value)
{
if(value->IsNumber()){
if(value->IsUint32()){
printf("Uint32: %d", value->Uint32Value());
}else if(value->IsInt32()){
printf("Int32: %d", value->Int32Value());
}else{
printf("Long: %I64d", value->IntegerValue());
}
}else if(value->IsBoolean()){
printf("Bool: %d", value->BooleanValue());
}else if(value->IsObject()){
printf("Object:");
printObjectProperties(Handle<Object>::Cast(value));
}
printf("\n");
}
int _tmain(int argc, _TCHAR* argv[])
{
// Create a stack-allocated handle scope.
HandleScope handle_scope;
// Create a new context.
Persistent<Context> context = Context::New();
// Enter the created context for compiling and
// running the hello world script.
Context::Scope context_scope(context);
std::string Dict_js = readSourceFile("Dict.js");
std::string EnumDict_js = readSourceFile("EnumDict.js");
std::string upa_js = readSourceFile("upa.js");
std::string bench_load_js = readSourceFile("bench_load.js");
std::string all_js;
all_js.append(Dict_js);
all_js.append(EnumDict_js);
all_js.append(upa_js);
all_js.append(bench_load_js);
// Create a string containing the JavaScript source code.
Handle<String> source = String::New(all_js.c_str());
Handle<Script> script = Script::Compile(source);
Handle<Value> result = script->Run();
// Convert the result to an ASCII string and print it.
//String::AsciiValue ascii(result);
//printf("%s\n", *ascii);
//Handle<Array> arr = Array::New(4);
//Handle<Value> val = Integer::New(1);
//arr->Set(Integer::New (0), val);
//arr->Set(Integer::New (1), val);
//arr->Set(Integer::New (2), val);
//arr->Set(Integer::New (3), val);
//Handle<Value> args[1];
//args[0] = arr;
// Start the benchmark
Handle<Function> func = Handle<Function>::Cast(context->Global()->Get(String::New("runBenchMark")));
Handle<Value> value = func->Call(func, 0, 0);
printValue(value);
//Handle<Value> value = context->Global()->Get(String::New("f"));
//printf("==== is value number = %d ====\n", value->IsNumber());
//printf("==== is value integer = %d ====\n", value->IsInt32());
//printf("==== %I64d ===", value->IntegerValue());
//Handle<Object> obj = Handle<Object>::Cast(context->Global()->Get(String::New("BinaryReader")));
// Create a string containing the JavaScript source code.
Handle<String> createReader = String::New("var b = BinaryReader.reader([0,0,1,1]); var f = b.int();");
// Compile the source code.
Handle<Script> compiledCreateReader = Script::Compile(createReader);
compiledCreateReader->Run();
//Handle<Object> obj = Handle<Object>::Cast(compiledCreateReader->Run());
Handle<Object> obj = Handle<Object>::Cast(context->Global()->Get(String::New("b")));
printObjectProperties(obj);
// Dispose the persistent context.
context.Dispose();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment