Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <v8.h>
- #include <iostream>
- #define JS_IMPL 1
- using namespace v8;
- __attribute__ ((noinline)) int test_function(int a, int b) {
- int result = 0;
- if(a == b) {
- result = 1;
- }
- return result;
- }
- int main(int argc, char* argv[]) {
- // Initialize V8.
- V8::InitializeICU();
- /*
- Platform* platform = platform::CreateDefaultPlatform();
- V8::InitializePlatform(platform);
- */
- V8::Initialize();
- // Create a new Isolate and make it the current one.
- Isolate* isolate = Isolate::New();
- {
- Isolate::Scope isolate_scope(isolate);
- // Create a stack-allocated handle scope.
- HandleScope handle_scope(isolate);
- // Create a new context.
- Local<Context> context = Context::New(isolate);
- // Enter the context for compiling and running the hello world script.
- Context::Scope context_scope(context);
- // Create a string containing the JavaScript source code.
- Local<String> source = String::NewFromUtf8(isolate, "function test_function() { var match = 0;if(arguments[0] == arguments[1]) { match = 1; } return match; };");
- // Compile the source code.
- Local<Script> script = Script::Compile(source);
- // Run the script to get the result.
- Local<Value> result = script->Run();
- Handle<v8::Object> global = context->Global();
- Handle<v8::Value> value = global->Get(String::NewFromUtf8(isolate, "test_function"));
- Handle<v8::Function> func = v8::Handle<v8::Function>::Cast(value);
- Handle<Value> args[2];
- Handle<Value> js_result;
- int final_result;
- int matched = 0;
- int not_matched = 0;
- for(int i = 0; i < 1000 * 1000 * 100; i++) {
- #if JS_IMPL
- args[0] = v8::Integer::New(isolate, i);
- args[1] = v8::Integer::New(isolate, i + (i % 2));
- js_result = func->Call(global, 2, args);
- final_result = js_result->ToInt32()->IntegerValue();
- #else
- final_result = test_function(i, i + (i % 2));
- #endif
- if(final_result == 1) {
- matched ++;
- } else {
- not_matched ++;
- }
- }
- std::cout << matched << " " << not_matched << std::endl;
- }
- // Dispose the isolate and tear down V8.
- isolate->Dispose();
- V8::Dispose();
- //V8::ShutdownPlatform();
- //delete platform;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement