Implement namespace support for v8 generator.
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/oliverb-javascript-v8@13748 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
90bbc6430b
commit
886f17c343
3 changed files with 53 additions and 13 deletions
|
|
@ -3,19 +3,25 @@
|
||||||
void ${MODULE}_Initialize(v8::Handle<v8::Context> context)
|
void ${MODULE}_Initialize(v8::Handle<v8::Context> context)
|
||||||
{
|
{
|
||||||
v8::HandleScope scope;
|
v8::HandleScope scope;
|
||||||
|
|
||||||
// register the module in globale context
|
|
||||||
v8::Local<v8::Object> global = context->Global();
|
v8::Local<v8::Object> global = context->Global();
|
||||||
|
|
||||||
|
/* create object templates for namespaces */
|
||||||
${PART_NAMESPACES}
|
${PART_NAMESPACES}
|
||||||
|
|
||||||
|
/* create class templates */
|
||||||
${PART_CLASS_TEMPLATES}
|
${PART_CLASS_TEMPLATES}
|
||||||
|
|
||||||
|
/* register wrapper functions */
|
||||||
${PART_WRAPPERS}
|
${PART_WRAPPERS}
|
||||||
|
|
||||||
|
/* setup inheritances */
|
||||||
${PART_INHERITANCE}
|
${PART_INHERITANCE}
|
||||||
|
|
||||||
|
/* some registration TODO: what specifically?*/
|
||||||
${PART_REGISTER}
|
${PART_REGISTER}
|
||||||
|
|
||||||
|
/* create and register namespace objects */
|
||||||
|
${PART_REGISTER_NS}
|
||||||
}%}
|
}%}
|
||||||
|
|
||||||
%fragment("v8_declare_class_template", "templates") %{
|
%fragment("v8_declare_class_template", "templates") %{
|
||||||
|
|
@ -74,5 +80,10 @@ v8::Handle<v8::Value> ${NAME_MANGLED}(const Arguments &args) {
|
||||||
%}
|
%}
|
||||||
|
|
||||||
%fragment("v8_register_member_function", "templates") %{
|
%fragment("v8_register_member_function", "templates") %{
|
||||||
SWIGV8_AddClassMethod(SWIGV8_${CLASSNAME_MANGLED}, "${NAME_UNQUALIFIED}", ${WRAPPER});
|
SWIGV8_AddClassMethod(SWIGV8_${CLASSNAME_MANGLED}, "${NAME_UNQUALIFIED}", ${WRAPPER});%}
|
||||||
%}
|
|
||||||
|
%fragment("v8_create_namespace", "templates") %{
|
||||||
|
v8::Handle<v8::ObjectTemplate> ${NAME_MANGLED} = v8::ObjectTemplate::New();%}
|
||||||
|
|
||||||
|
%fragment("v8_register_namespace", "templates") %{
|
||||||
|
${CONTEXT}->Set(v8::String::NewSymbol("${NAME_UNQUALIFIED}", ${NAME_MANGLED}->NewInstance()));%}
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,9 @@
|
||||||
#define V8_FUNCTION "v8_function"
|
#define V8_FUNCTION "v8_function"
|
||||||
#define V8_RETRIEVE_THIS "v8_retrieve_this"
|
#define V8_RETRIEVE_THIS "v8_retrieve_this"
|
||||||
#define V8_REGISTER_MEMBER_FUNCTION "v8_register_member_function"
|
#define V8_REGISTER_MEMBER_FUNCTION "v8_register_member_function"
|
||||||
|
#define V8_CREATE_NAMESPACE "v8_create_namespace"
|
||||||
|
#define V8_REGISTER_NAMESPACE "v8_register_namespace"
|
||||||
|
|
||||||
|
|
||||||
// keywords used in templates
|
// keywords used in templates
|
||||||
#define KW_MODULE_NAME "${MODULE}"
|
#define KW_MODULE_NAME "${MODULE}"
|
||||||
|
|
@ -32,6 +35,7 @@
|
||||||
#define KW_WRAPPERS "${PART_WRAPPERS}"
|
#define KW_WRAPPERS "${PART_WRAPPERS}"
|
||||||
#define KW_INHERITANCE "${PART_INHERITANCE}"
|
#define KW_INHERITANCE "${PART_INHERITANCE}"
|
||||||
#define KW_REGISTER "${PART_REGISTER}"
|
#define KW_REGISTER "${PART_REGISTER}"
|
||||||
|
#define KW_REGISTER_NS "${PART_REGISTER_NS}"
|
||||||
|
|
||||||
#define KW_LOCALS "${LOCALS}"
|
#define KW_LOCALS "${LOCALS}"
|
||||||
#define KW_MARSHAL_INPUT "${MARSHAL_INPUT}"
|
#define KW_MARSHAL_INPUT "${MARSHAL_INPUT}"
|
||||||
|
|
@ -72,6 +76,7 @@ int V8Emitter::Initialize(Node *n)
|
||||||
f_init_wrappers = NewString("");
|
f_init_wrappers = NewString("");
|
||||||
f_init_inheritance = NewString("");
|
f_init_inheritance = NewString("");
|
||||||
f_init_register = NewString("");
|
f_init_register = NewString("");
|
||||||
|
f_init_register_namespaces = NewString("");
|
||||||
|
|
||||||
// note: this is necessary for built-in generation of swig runtime code
|
// note: this is necessary for built-in generation of swig runtime code
|
||||||
Swig_register_filebyname("runtime", f_runtime);
|
Swig_register_filebyname("runtime", f_runtime);
|
||||||
|
|
@ -100,7 +105,8 @@ int V8Emitter::Dump(Node *n)
|
||||||
.Replace(KW_CLASS_TEMPLATES, f_init_class_templates)
|
.Replace(KW_CLASS_TEMPLATES, f_init_class_templates)
|
||||||
.Replace(KW_WRAPPERS, f_init_wrappers)
|
.Replace(KW_WRAPPERS, f_init_wrappers)
|
||||||
.Replace(KW_INHERITANCE, f_init_inheritance)
|
.Replace(KW_INHERITANCE, f_init_inheritance)
|
||||||
.Replace(KW_REGISTER, f_init_register);
|
.Replace(KW_REGISTER, f_init_register)
|
||||||
|
.Replace(KW_REGISTER_NS, f_init_register_namespaces);
|
||||||
Wrapper_pretty_print(initializer.str(), f_wrap_cpp);
|
Wrapper_pretty_print(initializer.str(), f_wrap_cpp);
|
||||||
|
|
||||||
return SWIG_OK;
|
return SWIG_OK;
|
||||||
|
|
@ -117,6 +123,7 @@ int V8Emitter::Close()
|
||||||
Delete(f_init_wrappers);
|
Delete(f_init_wrappers);
|
||||||
Delete(f_init_inheritance);
|
Delete(f_init_inheritance);
|
||||||
Delete(f_init_register);
|
Delete(f_init_register);
|
||||||
|
Delete(f_init_register_namespaces);
|
||||||
::Close(f_wrap_cpp);
|
::Close(f_wrap_cpp);
|
||||||
Delete(f_wrap_cpp);
|
Delete(f_wrap_cpp);
|
||||||
|
|
||||||
|
|
@ -143,17 +150,38 @@ int V8Emitter::SwitchContext(Node *n)
|
||||||
|
|
||||||
int V8Emitter::CreateNamespace(String* scope) {
|
int V8Emitter::CreateNamespace(String* scope) {
|
||||||
String* parent_scope = Swig_scopename_prefix(scope);
|
String* parent_scope = Swig_scopename_prefix(scope);
|
||||||
|
String* parent_scope_mangled = 0;
|
||||||
|
|
||||||
|
if(!parent_scope) {
|
||||||
|
parent_scope_mangled = NewString("global");
|
||||||
|
} else {
|
||||||
|
parent_scope_mangled = Swig_name_mangle(parent_scope);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
if (parent_scope && !Getattr(namespaces, parent_scope)) {
|
if (parent_scope && !Getattr(namespaces, parent_scope)) {
|
||||||
CreateNamespace(parent_scope);
|
CreateNamespace(parent_scope);
|
||||||
}
|
}
|
||||||
|
|
||||||
String* ns = Swig_string_mangle(scope);
|
String* scope_mangled = Swig_string_mangle(scope);
|
||||||
Setattr(namespaces, scope, ns);
|
String* scope_unqualified = Swig_scopename_last(scope);
|
||||||
|
Setattr(namespaces, scope, scope_mangled);
|
||||||
|
|
||||||
// TODO: create namespace object and register it to the parent scope
|
// create namespace object and register it to the parent scope
|
||||||
Printf(f_init_namespaces, "create_ns(%s);\n", ns);
|
Template t_create_ns(GetTemplate(V8_CREATE_NAMESPACE));
|
||||||
|
t_create_ns.Replace(KW_MANGLED_NAME, scope_mangled);
|
||||||
|
Template t_register_ns(GetTemplate(V8_REGISTER_NAMESPACE));
|
||||||
|
t_register_ns.Replace(KW_MANGLED_NAME, scope_mangled)
|
||||||
|
.Replace(KW_CONTEXT, parent_scope_mangled)
|
||||||
|
.Replace(KW_UNQUALIFIED_NAME, scope_unqualified);
|
||||||
|
|
||||||
|
Printv(f_init_namespaces, t_create_ns.str(), 0);
|
||||||
|
// prepend in order to achieve reversed order of registration statements
|
||||||
|
Insert(f_init_register_namespaces, 0, t_register_ns.str());
|
||||||
|
|
||||||
|
Delete(parent_scope);
|
||||||
|
Delete(parent_scope_mangled);
|
||||||
|
Delete(scope_unqualified);
|
||||||
return SWIG_OK;
|
return SWIG_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -57,6 +57,7 @@ private:
|
||||||
File *f_init_wrappers;
|
File *f_init_wrappers;
|
||||||
File *f_init_inheritance;
|
File *f_init_inheritance;
|
||||||
File *f_init_register;
|
File *f_init_register;
|
||||||
|
File *f_init_register_namespaces;
|
||||||
|
|
||||||
// the output cpp file
|
// the output cpp file
|
||||||
File *f_wrap_cpp;
|
File *f_wrap_cpp;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue