Created
January 9, 2011 05:51
-
-
Save getify/771459 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| v8::HandleScope handle_scope; | |
| v8::Context::Scope scope(my_context); | |
| v8::Handle<v8::Function> func1; | |
| { | |
| v8::TryCatch try_catch; | |
| v8::Handle<v8::Script> func1_script = v8::Script::Compile(v8::String::New("func1"), v8::String::New("inline::code")); | |
| v8::Handle<v8::Value> func1_res = func1_script->Run(); | |
| func1 = v8::Handle<v8::Function>::Cast(func1_res); | |
| } | |
| v8::Handle<v8::Function> func2; | |
| { | |
| v8::TryCatch try_catch; | |
| v8::Handle<v8::Script> func2_script = v8::Script::Compile(v8::String::New("func2"), v8::String::New("inline::code")); | |
| v8::Handle<v8::Value> func2_res = func2_script->Run(); | |
| func2 = v8::Handle<v8::Function>::Cast(func2_res); | |
| } | |
| v8::Handle<v8::Value> func1_args[2] = {v8::String::New("foo"), v8::String::New("bar")}; | |
| v8::Handle<v8::Value> val1 = func1->Call(my_context->Global(), 2, func1_args); | |
| v8::Handle<v8::Value> func2_args[1] = {v8::String::New("baz")}; | |
| v8::Handle<v8::Value> val2 = func2->Call(my_context->Global(), 1, func2_args); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| v8::HandleScope handle_scope; | |
| v8::Context::Scope scope(my_context); | |
| v8::Handle<v8::Value> execstr(v8::Handle<v8::String> str) { | |
| v8::HandleScope handle_scope; | |
| v8::Context::Scope scope(my_context); | |
| v8::TryCatch try_catch; | |
| v8::Handle<v8::Script> script = v8::Script::Compile(str, v8::String::New("inline::code")); | |
| return script->Run(); | |
| } | |
| // ..... | |
| v8::Handle<v8::Function> func1 = v8::Handle<v8::Function>::Cast(execstr(v8::String::New("func1"))); | |
| v8::Handle<v8::Function> func2 = v8::Handle<v8::Function>::Cast(execstr(v8::String::New("func2"))); | |
| v8::Handle<v8::Value> func1_args[2] = {v8::String::New("foo"), v8::String::New("bar")}; | |
| v8::Handle<v8::Value> val1 = func1->Call(my_context->Global(), 2, func1_args); | |
| // usually this fails, or it segfaults at this point | |
| // but if i were to comment out the line above that assigns `func2`, then it works fine. :( | |
| v8::Handle<v8::Value> func2_args[1] = {v8::String::New("baz")}; | |
| v8::Handle<v8::Value> val2 = func2->Call(my_context->Global(), 1, func2_args); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| v8::HandleScope handle_scope; | |
| v8::Context::Scope scope(my_context); | |
| v8::Handle<v8::Function> func3; | |
| const char* ToCString(const v8::String::Utf8Value& value) { | |
| return *value ? *value : "<string conversion failed>"; | |
| } | |
| v8::Handle<v8::Object> getObj() { | |
| v8::HandleScope handle_scope; | |
| v8::Context::Scope scope(my_context); | |
| v8::TryCatch try_catch; | |
| v8::Handle<v8::Script> func3_script = v8::Script::Compile(v8::String::New("func3"), v8::String::New("inline::code")); | |
| v8::Handle<v8::Value> func3_res = func3_script->Run(); | |
| func3 = v8::Handle<v8::Function>::Cast(func3_res); | |
| v8::Handle<v8::Value> func3_args[0] = {}; | |
| v8::Handle<v8::Value> val3 = func3->Call(my_context->Global(), 0, func3_args); | |
| return v8::Handle<v8::Object>::Cast(val3); | |
| } | |
| // ..... | |
| { | |
| v8::Handle<v8::Object> obj3 = getObj(); | |
| v8::Handle<v8::Value> v3_1 = obj3->Get(v8::String::New("prop3_1")); | |
| std::string s3_1 = ToCString(v8::String::Utf8Value(v3_1)); | |
| printf("%s\n",s3_1.c_str()); | |
| // ^^^^^ this works fine. | |
| v8::Handle<v8::Value> v3_2 = obj3->Get(v8::String::New("prop3_2")); | |
| std::string s3_2 = ToCString(v8::String::Utf8Value(v3_2)); | |
| printf("$s\n",s3_2.c_str()); | |
| // ^^^^^ this works fine. | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| v8::HandleScope handle_scope; | |
| v8::Context::Scope scope(my_context); | |
| v8::Handle<v8::Function> func3; | |
| const char* ToCString(const v8::String::Utf8Value& value) { | |
| return *value ? *value : "<string conversion failed>"; | |
| } | |
| v8::Handle<v8::Object> getObj() { | |
| v8::HandleScope handle_scope; | |
| v8::Context::Scope scope(my_context); | |
| v8::Handle<v8::Value> func3_args[0] = {}; | |
| v8::Handle<v8::Value> val3 = func3->Call(my_context->Global(), 0, func3_args); | |
| return v8::Handle<v8::Object>::Cast(val3); | |
| } | |
| // ..... | |
| { | |
| v8::TryCatch try_catch; | |
| v8::Handle<v8::Script> func3_script = v8::Script::Compile(v8::String::New("func3"), v8::String::New("inline::code")); | |
| v8::Handle<v8::Value> func3_res = func3_script->Run(); | |
| func3 = v8::Handle<v8::Function>::Cast(func3_res); | |
| } | |
| { | |
| v8::Handle<v8::Object> obj3 = getObj(); | |
| v8::Handle<v8::Value> v3_1 = obj3->Get(v8::String::New("prop3_1")); | |
| std::string s3_1 = ToCString(v8::String::Utf8Value(v3_1)); | |
| printf("%s\n",s3_1.c_str()); | |
| // ^^^^^ this works fine. | |
| v8::Handle<v8::Value> v3_2 = obj3->Get(v8::String::New("prop3_2")); | |
| std::string s3_2 = ToCString(v8::String::Utf8Value(v3_2)); | |
| printf("$s\n",s3_2.c_str()); | |
| // ^^^^^ this fails, prints out "undefined" | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment