diff --git a/Doc/Devel/Javascript/V8_CodeGeneratorSpecification.md b/Doc/Devel/Javascript/V8_CodeGeneratorSpecification.md index fcd581755..2265e6a5d 100644 --- a/Doc/Devel/Javascript/V8_CodeGeneratorSpecification.md +++ b/Doc/Devel/Javascript/V8_CodeGeneratorSpecification.md @@ -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 SWIGV8_CreateClassTemplate(const char* symbol) { - v8::Local class_templ = v8::FunctionTemplate::New(); +v8::Persistent SWIGV8_CreateClassTemplate(const char* symbol, v8::InvocationCallback func) +{ + v8::Local class_templ = v8::FunctionTemplate::New(func); class_templ->SetClassName(v8::String::NewSymbol(symbol)); v8::Handle inst_templ = class_templ->InstanceTemplate(); @@ -289,47 +290,60 @@ v8::Persistent SWIGV8_CreateClassTemplate(const char* symb return v8::Persistent::New(class_templ); } -/** - * Creates a class template for a class with specified initialization function. - */ -v8::Persistent SWIGV8_CreateClassTemplate(const char* symbol, v8::InvocationCallback _func) { - v8::Local class_templ = v8::FunctionTemplate::New(_func); - class_templ->SetClassName(v8::String::NewSymbol(symbol)); - - v8::Handle inst_templ = class_templ->InstanceTemplate(); - inst_templ->SetInternalFieldCount(1); - - return v8::Persistent::New(class_templ); -} - -/** - * Sets the pimpl data of a V8 class. - */ -v8::Handle V8GeneratorUtils::SetInstance(const v8::Arguments& args, void* data) { - v8::HandleScope scope; - - v8::Handle 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 class_templ, const char* symbol, v8::InvocationCallback _func) { +void SWIGV8_AddMemberFunction(v8::Handle class_templ, + const char* symbol, + v8::InvocationCallback func) +{ v8::Handle 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 class_templ, const char* varname, v8::AccessorGetter getter, v8::AccessorSetter setter) { +void SWIGV8_AddMemberVariable(v8::Handle class_templ, + const char* varname, + v8::AccessorGetter getter, + v8::AccessorSetter setter) +{ v8::Handle 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 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 context, + const char* symbol, + v8::InvocationCallback func) +{ + context->Set(v8::String::NewSymbol(symbol), v8::FunctionTemplate::New(func)->GetFunction()); +} + +template +static T* SWIGV8_UnwrapThisPointer (v8::Handle handle) +{ + // assert(!handle.IsEmpty()); + // assert(handle->InternalFieldCount() > 0); + v8::Local wrap = v8::Local::Cast(handle->GetInternalField(0)); + return static_cast(wrap->Value()); +} + ~~~~ ------------------------- diff --git a/Lib/javascript/v8/javascriptcode.swg b/Lib/javascript/v8/javascriptcode.swg index 855f72284..c8dc0a109 100644 --- a/Lib/javascript/v8/javascriptcode.swg +++ b/Lib/javascript/v8/javascriptcode.swg @@ -73,13 +73,13 @@ v8::Handle wrap_${NAME_MANGLED}(const Arguments &args) { v8::Handle ${NAME_MANGLED} = v8::ObjectTemplate::New();%} %fragment("v8_register_member_function", "templates") %{ -SWIGV8_AddClassMethod(SWIGV8_${CLASSNAME_MANGLED}, "${NAME_UNQUALIFIED}", wrap_${NAME_MANGLED});%} +SWIGV8_AddMemberFunction(SWIGV8_${CLASSNAME_MANGLED}, "${NAME_UNQUALIFIED}", wrap_${NAME_MANGLED});%} %fragment("v8_register_global_function", "templates") %{ -SWIGV8_AddGlobalMethod(${CONTEXT}, "${NAME_UNQUALIFIED}", wrap_${NAME_MANGLED});%} +SWIGV8_AddGlobalFunction(${CONTEXT}, "${NAME_UNQUALIFIED}", wrap_${NAME_MANGLED});%} %fragment("v8_register_member_variable", "templates") %{ -SWIGV8_AddClassVariable(SWIGV8_${CLASSNAME_MANGLED}, "${NAME_UNQUALIFIED}", ${GETTER}, ${SETTER});%} +SWIGV8_AddMemberVariable(SWIGV8_${CLASSNAME_MANGLED}, "${NAME_UNQUALIFIED}", ${GETTER}, ${SETTER});%} %fragment("v8_register_global_variable", "templates") %{ SWIGV8_AddGlobalVariable(${CONTEXT}, "${NAME_UNQUALIFIED}", ${GETTER}, ${SETTER});%}