diff V8_tests/Two_threads.cpp @ 6:c82791ba5ce1

added two-context test, from botton of pdf, plus pdf
author Sean Halle <seanhalle@yahoo.com>
date Sat, 01 Feb 2014 14:26:10 -0800
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/V8_tests/Two_threads.cpp	Sat Feb 01 14:26:10 2014 -0800
     1.3 @@ -0,0 +1,68 @@
     1.4 +#include <v8.h>
     1.5 +#include <pthread.h>
     1.6 +using namespace v8;
     1.7 +Isolate* isolate;
     1.8 +void* threadFunction(void*)
     1.9 + {
    1.10 +   //Isolate* isolate1 = Isolate::New();
    1.11 +   //{
    1.12 +   if(isolate == NULL)
    1.13 +    {
    1.14 +      printf("Error1");
    1.15 +      return 0;
    1.16 +    }
    1.17 +
    1.18 +   Locker locker1(isolate);
    1.19 +   //isolate->Enter();
    1.20 +   Isolate::Scope isolate_scope1(isolate);
    1.21 +   HandleScope handle_scope1(isolate);
    1.22 +   Handle<Context> context1 = Context::New(isolate);
    1.23 +   Context::Scope context_scope1(context1);
    1.24 +   Handle<String> source1 = String::NewFromUtf8(isolate, "'Hi' + ', Sara'");
    1.25 +   Handle<Script> script1 = Script::Compile(source1);
    1.26 +   Handle<Value> result1 = script1->Run();
    1.27 +   String::Utf8Value utf81(result1);
    1.28 +   printf("%s\n", *utf81);
    1.29 +   //isolate->Exit();
    1.30 +   Unlocker unlocker1(isolate);
    1.31 +
    1.32 +   return 0;
    1.33 + }
    1.34 + 
    1.35 +int main(int argc, char* argv[]) 
    1.36 + {
    1.37 +   // Get the default Isolate created at startup.
    1.38 +   isolate =  Isolate::New();
    1.39 +   if(isolate == NULL)
    1.40 +    {
    1.41 +      printf("Error");
    1.42 +      return 0;
    1.43 +    }
    1.44 +   //Sara: the "locker" looks like it's purpose is to make sure only a single
    1.45 +   // thread is modifying the isolate..  this goes against the purpose, which
    1.46 +   // is for multiple scripts to be running at the same time..
    1.47 +   Locker locker(isolate);
    1.48 +   Isolate::Scope isolate_scope(isolate);
    1.49 +   // Create a stack-allocated handle scope.
    1.50 +   HandleScope handle_scope(isolate);
    1.51 +   // Create a new context.
    1.52 +   Handle<Context> context = Context::New(isolate);
    1.53 +   // Enter the context for compiling and running the hello world script.
    1.54 +   Context::Scope context_scope(context);
    1.55 +   // Create a string containing the JavaScript source code.
    1.56 +   //isolate->Exit();
    1.57 +   Unlocker unlocker(isolate);
    1.58 +   pthread_t thread_id;
    1.59 +   pthread_create(&thread_id, NULL, &threadFunction,NULL);
    1.60 +   //isolate->Enter();
    1.61 +   Handle<String> source = String::NewFromUtf8(isolate, "'Hello' + ', World!'");
    1.62 +   // Compile the source code.
    1.63 +   Handle<Script> script = Script::Compile(source);
    1.64 +   // Run the script to get the result.
    1.65 +   Handle<Value> result = script->Run();
    1.66 +   // Convert the result to an UTF8 string and print it.
    1.67 +   String::Utf8Value utf8(result);
    1.68 +   printf("%s\n", *utf8);
    1.69 +   return 0;
    1.70 +   //isolate->Dispose();
    1.71 + }