diff --git a/Lib/javascript/v8/javascriptcode.swg b/Lib/javascript/v8/javascriptcode.swg index c8dc0a109..e5a1f1eba 100644 --- a/Lib/javascript/v8/javascriptcode.swg +++ b/Lib/javascript/v8/javascriptcode.swg @@ -16,6 +16,12 @@ void ${MODULE}_Initialize(v8::Handle context) /* setup inheritances */ ${PART_INHERITANCE} + /* class instances */ + ${PART_CLASS_INSTANCES} + + /* add static class functions and variables */ + ${PART_STATIC_WRAPPERS} + /* register classes */ ${PART_REGISTER_CLASSES} @@ -29,11 +35,14 @@ v8::Persistent SWIGV8_${NAME_MANGLED};%} %fragment("v8_define_class_template", "templates") %{ SWIGV8_${NAME_MANGLED} = SWIGV8_CreateClassTemplate("${NAME_UNQUALIFIED}" , ${NAME_MANGLED}_new);%} +%fragment("v8_create_class_instance", "templates") %{ +v8::Handle class_${NAME_MANGLED} = SWIGV8_${NAME_MANGLED}->GetFunction();%} + %fragment("v8_inherit", "templates") %{ SWIGV8_${NAME_MANGLED}->Inherit(SWIGV8_${BASE_CLASS});%} %fragment("v8_register_class", "templates") %{ -${CONTEXT}->Set(v8::String::NewSymbol("${NAME_UNQUALIFIED}", SWIGV8_${NAME_MANGLED}->GetFunction()));%} +${CONTEXT}->Set(v8::String::NewSymbol("${NAME_UNQUALIFIED}"), class_${NAME_MANGLED});%} %fragment("v8_ctor_wrapper", "templates") %{ v8::Handle ${NAME_MANGLED}_new(const v8::Arguments& args) { diff --git a/Source/Modules/javascript_v8.cxx b/Source/Modules/javascript_v8.cxx index 7d6e9a327..cacceb214 100644 --- a/Source/Modules/javascript_v8.cxx +++ b/Source/Modules/javascript_v8.cxx @@ -9,6 +9,7 @@ #define V8_INITIALIZER "v8_initializer" #define V8_DECL_CLASSTEMPLATE "v8_declare_class_template" #define V8_DEFINE_CLASSTEMPLATE "v8_define_class_template" +#define V8_CREATE_CLASS_INSTANCE "v8_create_class_instance" #define V8_INHERIT "v8_inherit" #define V8_REGISTER_CLASS "v8_register_class" #define V8_CTOR_WRAPPER "v8_ctor_wrapper" @@ -42,6 +43,8 @@ #define KW_CLASS_TEMPLATES "${PART_CLASS_TEMPLATES}" #define KW_WRAPPERS "${PART_WRAPPERS}" #define KW_INHERITANCE "${PART_INHERITANCE}" +#define KW_CLASS_INSTANCES "${PART_CLASS_INSTANCES}" +#define KW_STATIC_WRAPPERS "${PART_STATIC_WRAPPERS}" #define KW_REGISTER_CLASSES "${PART_REGISTER_CLASSES}" #define KW_REGISTER_NS "${PART_REGISTER_NS}" @@ -86,6 +89,8 @@ int V8Emitter::Initialize(Node *n) f_init_class_templates = NewString(""); f_init_wrappers = NewString(""); f_init_inheritance = NewString(""); + f_init_class_instances = NewString(""); + f_init_static_wrappers = NewString(""); f_init_register_classes = NewString(""); f_init_register_namespaces = NewString(""); @@ -116,6 +121,8 @@ int V8Emitter::Dump(Node *n) .Replace(KW_CLASS_TEMPLATES, f_init_class_templates) .Replace(KW_WRAPPERS, f_init_wrappers) .Replace(KW_INHERITANCE, f_init_inheritance) + .Replace(KW_CLASS_INSTANCES, f_init_class_instances) + .Replace(KW_STATIC_WRAPPERS, f_init_static_wrappers) .Replace(KW_REGISTER_CLASSES, f_init_register_classes) .Replace(KW_REGISTER_NS, f_init_register_namespaces); Wrapper_pretty_print(initializer.str(), f_wrap_cpp); @@ -125,6 +132,7 @@ int V8Emitter::Dump(Node *n) int V8Emitter::Close() { + /* strings */ Delete(f_runtime); Delete(f_header); Delete(f_class_templates); @@ -133,8 +141,12 @@ int V8Emitter::Close() Delete(f_init_class_templates); Delete(f_init_wrappers); Delete(f_init_inheritance); + Delete(f_init_class_instances); + Delete(f_init_static_wrappers); Delete(f_init_register_classes); Delete(f_init_register_namespaces); + + /* files */ ::Close(f_wrap_cpp); Delete(f_wrap_cpp); @@ -210,9 +222,13 @@ int V8Emitter::EnterClass(Node *n) // emit definition of v8 class template Template t_def_class(GetTemplate(V8_DEFINE_CLASSTEMPLATE)); - t_def_class.Replace(KW_MANGLED_NAME, current_classname_mangled) + t_def_class.Replace(KW_MANGLED_NAME, current_classname_mangled) .Replace(KW_UNQUALIFIED_NAME, current_classname_unqualified); Printv(f_init_class_templates, t_def_class.str(), 0); + + Template t_class_instance(GetTemplate(V8_CREATE_CLASS_INSTANCE)); + t_class_instance.Replace(KW_MANGLED_NAME, current_classname_mangled); + Printv(f_init_class_instances, t_class_instance.str(), 0); return SWIG_OK; } @@ -262,14 +278,25 @@ int V8Emitter::EnterVariable(Node* n) int V8Emitter::ExitVariable(Node* n) { - if(GetFlag(n, "ismember")) { - Template t_register(GetTemplate(V8_REGISTER_MEMBER_VARIABLE)); - t_register.Replace(KW_CLASSNAME_MANGLED, current_classname_mangled) - .Replace(KW_UNQUALIFIED_NAME, current_variable_unqualified) - .Replace(KW_GETTER, current_getter) - .Replace(KW_SETTER, current_setter); - Printv(f_init_wrappers, t_register.str(), 0); + if(Equal(Getattr(n, "storage"), "static")) { + Template t_register(GetTemplate(V8_REGISTER_GLOBAL_VARIABLE)); + String *class_instance = NewString(""); + Printf(class_instance, "class_%s", current_classname_mangled); + t_register.Replace(KW_CONTEXT, class_instance) + .Replace(KW_UNQUALIFIED_NAME, current_variable_unqualified) + .Replace(KW_GETTER, current_getter) + .Replace(KW_SETTER, current_setter); + Printv(f_init_static_wrappers, t_register.str(), 0); + Delete(class_instance); + } else { + Template t_register(GetTemplate(V8_REGISTER_MEMBER_VARIABLE)); + t_register.Replace(KW_CLASSNAME_MANGLED, current_classname_mangled) + .Replace(KW_UNQUALIFIED_NAME, current_variable_unqualified) + .Replace(KW_GETTER, current_getter) + .Replace(KW_SETTER, current_setter); + Printv(f_init_wrappers, t_register.str(), 0); + } } else { Template t_register(GetTemplate(V8_REGISTER_GLOBAL_VARIABLE)); t_register.Replace(KW_CONTEXT, current_context) @@ -305,11 +332,22 @@ int V8Emitter::ExitFunction(Node* n) { // register the function at the specific context if(GetFlag(n, "ismember")) { - Template t_register(GetTemplate(V8_REGISTER_MEMBER_FUNCTION)); - t_register.Replace(KW_CLASSNAME_MANGLED, current_classname_mangled) - .Replace(KW_UNQUALIFIED_NAME, current_function_unqualified) - .Replace(KW_MANGLED_NAME, Getattr(n, "wrap:name")); - Printv(f_init_wrappers, t_register.str(), "\n", 0); + if(Equal(Getattr(n, "storage"), "static")) { + Template t_register(GetTemplate(V8_REGISTER_GLOBAL_FUNCTION)); + String *class_instance = NewString(""); + Printf(class_instance, "class_%s", current_classname_mangled); + t_register.Replace(KW_CONTEXT, class_instance) + .Replace(KW_UNQUALIFIED_NAME, current_function_unqualified) + .Replace(KW_MANGLED_NAME, Getattr(n, "wrap:name")); + Printv(f_init_static_wrappers, t_register.str(), 0); + Delete(class_instance); + } else { + Template t_register(GetTemplate(V8_REGISTER_MEMBER_FUNCTION)); + t_register.Replace(KW_CLASSNAME_MANGLED, current_classname_mangled) + .Replace(KW_UNQUALIFIED_NAME, current_function_unqualified) + .Replace(KW_MANGLED_NAME, Getattr(n, "wrap:name")); + Printv(f_init_wrappers, t_register.str(), "\n", 0); + } } else { Template t_register(GetTemplate(V8_REGISTER_GLOBAL_FUNCTION)); t_register.Replace(KW_CONTEXT, current_context) diff --git a/Source/Modules/javascript_v8.h b/Source/Modules/javascript_v8.h index 7ff11f136..8d9e6d0b0 100644 --- a/Source/Modules/javascript_v8.h +++ b/Source/Modules/javascript_v8.h @@ -62,6 +62,8 @@ private: File *f_init_class_templates; File *f_init_wrappers; File *f_init_inheritance; + File *f_init_class_instances; + File *f_init_static_wrappers; File *f_init_register_classes; File *f_init_register_namespaces;