Advertisement
Guest User

Untitled

a guest
Mar 4th, 2015
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <v8.h>
  2. #include <iostream>
  3.  
  4. #define JS_IMPL 1
  5.  
  6. using namespace v8;
  7.  
  8. __attribute__ ((noinline)) int  test_function(int a, int b) {
  9.     int result = 0;
  10.     if(a == b) {
  11.         result = 1;
  12.     }
  13.     return result;
  14. }
  15.  
  16. int main(int argc, char* argv[]) {
  17.     // Initialize V8.
  18.     V8::InitializeICU();
  19.     /*
  20.     Platform* platform = platform::CreateDefaultPlatform();
  21.     V8::InitializePlatform(platform);
  22.     */
  23.     V8::Initialize();
  24.  
  25.     // Create a new Isolate and make it the current one.
  26.     Isolate* isolate = Isolate::New();
  27.     {
  28.         Isolate::Scope isolate_scope(isolate);
  29.  
  30.         // Create a stack-allocated handle scope.
  31.         HandleScope handle_scope(isolate);
  32.  
  33.         // Create a new context.
  34.         Local<Context> context = Context::New(isolate);
  35.  
  36.         // Enter the context for compiling and running the hello world script.
  37.         Context::Scope context_scope(context);
  38.  
  39.         // Create a string containing the JavaScript source code.
  40.         Local<String> source = String::NewFromUtf8(isolate, "function test_function() { var match = 0;if(arguments[0] == arguments[1]) { match = 1; } return match; };");
  41.  
  42.         // Compile the source code.
  43.         Local<Script> script = Script::Compile(source);
  44.  
  45.         // Run the script to get the result.
  46.         Local<Value> result = script->Run();
  47.  
  48.         Handle<v8::Object> global = context->Global();
  49.         Handle<v8::Value> value = global->Get(String::NewFromUtf8(isolate, "test_function"));
  50.         Handle<v8::Function> func = v8::Handle<v8::Function>::Cast(value);
  51.         Handle<Value> args[2];
  52.         Handle<Value> js_result;
  53.         int final_result;
  54.  
  55.         int matched = 0;
  56.         int not_matched = 0;
  57.  
  58.         for(int i = 0; i < 1000 * 1000 * 100; i++) {
  59. #if JS_IMPL
  60.             args[0] = v8::Integer::New(isolate, i);
  61.             args[1] = v8::Integer::New(isolate, i + (i % 2));
  62.  
  63.             js_result = func->Call(global, 2, args);
  64.  
  65.             final_result = js_result->ToInt32()->IntegerValue();
  66. #else
  67.             final_result = test_function(i, i + (i % 2));
  68. #endif
  69.             if(final_result == 1) {
  70.  
  71.                 matched ++;
  72.  
  73.             } else {
  74.  
  75.                 not_matched ++;
  76.  
  77.             }
  78.         }
  79.         std::cout << matched << " " << not_matched << std::endl;
  80.     }
  81.  
  82.     // Dispose the isolate and tear down V8.
  83.     isolate->Dispose();
  84.     V8::Dispose();
  85.     //V8::ShutdownPlatform();
  86.     //delete platform;
  87.     return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement