Extend and rename v8 helper functions.
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/oliverb-javascript-v8@13756 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
65d4769af8
commit
badf090cb5
2 changed files with 47 additions and 33 deletions
|
|
@ -279,8 +279,9 @@ A lot of boiler-plate code can be shifted into static helper functions:
|
|||
/**
|
||||
* Creates a class template for a class without extra initialization function.
|
||||
*/
|
||||
v8::Persistent<v8::FunctionTemplate> SWIGV8_CreateClassTemplate(const char* symbol) {
|
||||
v8::Local<v8::FunctionTemplate> class_templ = v8::FunctionTemplate::New();
|
||||
v8::Persistent<v8::FunctionTemplate> SWIGV8_CreateClassTemplate(const char* symbol, v8::InvocationCallback func)
|
||||
{
|
||||
v8::Local<v8::FunctionTemplate> class_templ = v8::FunctionTemplate::New(func);
|
||||
class_templ->SetClassName(v8::String::NewSymbol(symbol));
|
||||
|
||||
v8::Handle<v8::ObjectTemplate> inst_templ = class_templ->InstanceTemplate();
|
||||
|
|
@ -289,47 +290,60 @@ v8::Persistent<v8::FunctionTemplate> SWIGV8_CreateClassTemplate(const char* symb
|
|||
return v8::Persistent<v8::FunctionTemplate>::New(class_templ);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a class template for a class with specified initialization function.
|
||||
*/
|
||||
v8::Persistent<v8::FunctionTemplate> SWIGV8_CreateClassTemplate(const char* symbol, v8::InvocationCallback _func) {
|
||||
v8::Local<v8::FunctionTemplate> class_templ = v8::FunctionTemplate::New(_func);
|
||||
class_templ->SetClassName(v8::String::NewSymbol(symbol));
|
||||
|
||||
v8::Handle<v8::ObjectTemplate> inst_templ = class_templ->InstanceTemplate();
|
||||
inst_templ->SetInternalFieldCount(1);
|
||||
|
||||
return v8::Persistent<v8::FunctionTemplate>::New(class_templ);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the pimpl data of a V8 class.
|
||||
*/
|
||||
v8::Handle<v8::Value> V8GeneratorUtils::SetInstance(const v8::Arguments& args, void* data) {
|
||||
v8::HandleScope scope;
|
||||
|
||||
v8::Handle<v8::Object> self = args.Holder();
|
||||
self->SetInternalField(0, v8::External::New(data));
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a class method with given name for a given class template.
|
||||
*/
|
||||
void V8GeneratorUtils::AddClassMethod(v8::Handle<v8::FunctionTemplate> class_templ, const char* symbol, v8::InvocationCallback _func) {
|
||||
void SWIGV8_AddMemberFunction(v8::Handle<v8::FunctionTemplate> class_templ,
|
||||
const char* symbol,
|
||||
v8::InvocationCallback func)
|
||||
{
|
||||
v8::Handle<v8::ObjectTemplate> proto_templ = class_templ->PrototypeTemplate();
|
||||
proto_templ->Set(v8::String::NewSymbol(symbol), v8::FunctionTemplate::New(_func));
|
||||
proto_templ->Set(v8::String::NewSymbol(symbol), v8::FunctionTemplate::New(func));
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a class property with given name for a given class template.
|
||||
*/
|
||||
void V8GeneratorUtils::AddProperty(v8::Handle<v8::FunctionTemplate> class_templ, const char* varname, v8::AccessorGetter getter, v8::AccessorSetter setter) {
|
||||
void SWIGV8_AddMemberVariable(v8::Handle<v8::FunctionTemplate> class_templ,
|
||||
const char* varname,
|
||||
v8::AccessorGetter getter,
|
||||
v8::AccessorSetter setter)
|
||||
{
|
||||
v8::Handle<v8::ObjectTemplate> proto_templ = class_templ->InstanceTemplate();
|
||||
proto_templ->SetAccessor(v8::String::New(varname), getter, setter);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Adds a property with given name to a given context.
|
||||
*/
|
||||
void SWIGV8_AddGlobalVariable(v8::Handle<v8::Object> context,
|
||||
const char* varname,
|
||||
v8::AccessorGetter getter,
|
||||
v8::AccessorSetter setter)
|
||||
{
|
||||
context->SetAccessor(v8::String::NewSymbol(varname), getter, setter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a function with given name to a given context.
|
||||
*/
|
||||
void SWIGV8_AddGlobalFunction(v8::Handle<v8::Object> context,
|
||||
const char* symbol,
|
||||
v8::InvocationCallback func)
|
||||
{
|
||||
context->Set(v8::String::NewSymbol(symbol), v8::FunctionTemplate::New(func)->GetFunction());
|
||||
}
|
||||
|
||||
template <class T>
|
||||
static T* SWIGV8_UnwrapThisPointer (v8::Handle<v8::Object> handle)
|
||||
{
|
||||
// assert(!handle.IsEmpty());
|
||||
// assert(handle->InternalFieldCount() > 0);
|
||||
v8::Local<v8::External> wrap = v8::Local<v8::External>::Cast(handle->GetInternalField(0));
|
||||
return static_cast<T*>(wrap->Value());
|
||||
}
|
||||
|
||||
~~~~
|
||||
|
||||
-------------------------
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue