Mercurial > cgi-bin > hgwebdir.cgi > PR > PR_Integrations > javascript > SpiderMonkey
changeset 7:524090d5a407
this is the 2nd tets
| author | Sara |
|---|---|
| date | Sat, 15 Feb 2014 11:16:09 -0800 |
| parents | c82791ba5ce1 |
| children | 1e2d1d978e4a |
| files | V8_tests/Passing_Context_and_compiledScript_to_threads/first_strategy.cpp |
| diffstat | 1 files changed, 115 insertions(+), 0 deletions(-) [+] |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/V8_tests/Passing_Context_and_compiledScript_to_threads/first_strategy.cpp Sat Feb 15 11:16:09 2014 -0800 1.3 @@ -0,0 +1,115 @@ 1.4 +#include <v8.h> 1.5 +#include <pthread.h> 1.6 +using namespace v8; 1.7 +Isolate* isolate; 1.8 + 1.9 + 1.10 +struct threadParam{ 1.11 + Handle<Context> context; 1.12 + Handle<Script> compiledScript; 1.13 +}; 1.14 + 1.15 + 1.16 +void* threadFunction(void* param){ 1.17 + 1.18 + struct threadParam* p = (struct threadParam*) param; 1.19 + p->context->Enter(); 1.20 + Locker locker1(isolate); 1.21 + 1.22 + Isolate::Scope isolate_scope1(isolate); 1.23 + HandleScope handle_scope1(isolate); 1.24 + Context::Scope context_scope1(p->context); 1.25 + Handle<Value> result = p->compiledScript->Run(); 1.26 + String::Utf8Value utf8(result); 1.27 + printf("%s\n", *utf8); 1.28 + Unlocker unlocker1(isolate); 1.29 + return NULL; 1.30 + 1.31 +} 1.32 + 1.33 +int main(int argc, char* argv[]) { 1.34 + 1.35 + isolate = Isolate::New(); 1.36 + { 1.37 + if(isolate == NULL){ 1.38 + printf("Error"); 1.39 + return 0; 1.40 + } 1.41 + Locker locker(isolate); 1.42 + Isolate::Scope isolate_scope(isolate); 1.43 + // Create a stack-allocated handle scope. 1.44 + HandleScope handle_scope(isolate); 1.45 + //struct threadParam testStruct1; 1.46 + //pthread_t thread1_id; 1.47 + // Create a new context. 1.48 + Handle<Context> context1 = Context::New(isolate); 1.49 + // Enter the context for compiling and running the hello world script. 1.50 + Context::Scope context_scope1(context1); 1.51 + // Create a string containing the JavaScript source code. 1.52 + Handle<String>source1 = String::NewFromUtf8(isolate, "'Hello' + ', World!'"); 1.53 + // Compile the source code. 1.54 + Handle<Script> script1 = Script::Compile(source1); 1.55 + // Run the script to get the result. 1.56 + Handle<Value> result1 = script1->Run(); 1.57 + // Convert the result to an UTF8 string and print it. 1.58 + 1.59 + String::Utf8Value utf81(result1); 1.60 + printf("%s\n", *utf81); 1.61 + 1.62 + 1.63 + 1.64 + //create the 2nd context 1.65 + Handle<Context> context2 = Context::New(isolate); 1.66 + // Enter the context for compiling and running the hello world script. 1.67 + // context2->Enter(); 1.68 + //Note--- Here the first context is still the current context---- 1.69 + Context::Scope context_scope2(context2); 1.70 + //Note---here the 2nd context is the current context----using the following test 1.71 + Local<Context> temp = isolate->GetCurrentContext(); 1.72 + if(temp == context1) 1.73 + printf("Yes the first context"); 1.74 + else if(temp == context2) 1.75 + printf("No this is context 2"); 1.76 + //-------End of the test---// 1.77 + 1.78 + // Create a string containing the JavaScript source code. 1.79 + Handle<String> source2 = String::NewFromUtf8(isolate, "'Hi' + ', Dr Sean'"); 1.80 + // Compile the source code. 1.81 + Handle<Script> script2 = Script::Compile(source2); 1.82 + // Run the script to get the result. 1.83 + Handle<Value> result2 = script2->Run(); 1.84 + // Convert the result to an UTF8 string and print it. 1.85 + String::Utf8Value utf82(result2); 1.86 + printf("%s\n", *utf82); 1.87 + 1.88 + //Here we will work with threads 1.89 + //context2->Exit(); 1.90 + //context1->Exit(); 1.91 + 1.92 + 1.93 + context1->Enter(); 1.94 + struct threadParam testStruct1; 1.95 + pthread_t thread1_id; 1.96 + testStruct1.compiledScript = script1; 1.97 + testStruct1.context = context1; 1.98 + pthread_create(&thread1_id, NULL, &threadFunction, &testStruct1 ); 1.99 + pthread_join(thread1_id, NULL); 1.100 + context1->Exit(); 1.101 + context2->Enter(); 1.102 + struct threadParam testStruct2; 1.103 + pthread_t thread2_id; 1.104 + testStruct2.compiledScript = script2; 1.105 + testStruct2.context = context2; 1.106 + pthread_create(&thread2_id, NULL, &threadFunction, &testStruct2 ); 1.107 + pthread_join(thread2_id, NULL); 1.108 + context2->Exit(); 1.109 + //context1->Enter(); 1.110 + //context1->Exit(); 1.111 + // pthread_create(&thread1_id, NULL, &threadFunction, &testStruct1 ); 1.112 + // pthread_create(&thread2_id, NULL, &threadFunction, &testStruct2 ); 1.113 + // pthread_join(thread1_id, NULL); 1.114 + //pthread_join(thread2_id, NULL); 1.115 + 1.116 + return 0; 1.117 + } 1.118 + }
