Created
October 25, 2013 07:04
-
-
Save wyyqyl/7150497 to your computer and use it in GitHub Desktop.
shows the difference between PrototypeTemplate and InstanceTemplate
This file contains 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
#include <v8.h> | |
#include <vld.h> | |
#include <iostream> | |
void InvokeCallback(const v8::FunctionCallbackInfo<v8::Value>& args) { | |
std::cout << "InvokeCallback" << std::endl; | |
} | |
void InstanceAccessorCallback(v8::Local<v8::String> property, | |
const v8::PropertyCallbackInfo<v8::Value>& args) { | |
std::cout << "InstanceAccessorCallback" << std::endl; | |
} | |
int main() { | |
auto isolate = v8::Isolate::GetCurrent(); | |
v8::HandleScope handle_scope(isolate); | |
auto context = v8::Context::New(isolate); | |
auto t = v8::FunctionTemplate::New(); | |
t->Set("func_property", v8::Number::New(1)); | |
v8::Local<v8::Template> proto_t = t->PrototypeTemplate(); | |
proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback)); | |
proto_t->Set("proto_const", v8::Number::New(2)); | |
auto instance_t = t->InstanceTemplate(); | |
instance_t->SetAccessor(v8::String::New("instance_accessor"), | |
InstanceAccessorCallback); | |
instance_t->Set("instance_property", v8::Number::New(3)); | |
context->Enter(); | |
context->Global()->Set(v8::String::New("demo"), t->GetFunction()); | |
auto source = v8::String::New("demo.prototype.proto_method();" | |
"var instance = new demo();" | |
"instance.InstanceAccessorCallback();"); | |
auto script = v8::Script::Compile(source); | |
auto result = script->Run(); | |
context->Exit(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
what?