From: <ne...@us...> - 2012-07-02 21:21:59
|
Revision: 13241 http://swig.svn.sourceforge.net/swig/?rev=13241&view=rev Author: neha1 Date: 2012-07-02 21:21:53 +0000 (Mon, 02 Jul 2012) Log Message: ----------- Improved class emitter implementation. Studying the JavascriptCore reference revealed that creating a class object and a class instance should be done by using two different JSClassDefinitions. One for the static part and one for the member variables and functions. Modified Paths: -------------- branches/gsoc2012-javascript/Lib/javascript/jsc/javascriptcode.swg branches/gsoc2012-javascript/Lib/javascript/jsc/javascripthelpers.swg branches/gsoc2012-javascript/Source/Modules/javascript_jsc.cxx branches/gsoc2012-javascript/Source/Modules/javascript_jsc.h Modified: branches/gsoc2012-javascript/Lib/javascript/jsc/javascriptcode.swg =================================================================== --- branches/gsoc2012-javascript/Lib/javascript/jsc/javascriptcode.swg 2012-07-02 21:18:39 UTC (rev 13240) +++ branches/gsoc2012-javascript/Lib/javascript/jsc/javascriptcode.swg 2012-07-02 21:21:53 UTC (rev 13241) @@ -4,32 +4,6 @@ * Additional Javascript typemaps for generating code for classes, constants and variables * ----------------------------------------------------------------------------- ----------------*/ -%fragment ("jsc_initializer", "templates") %{ -bool ${modulename}_initialize(JSGlobalContextRef context) { - JSObjectRef globalObject = JSContextGetGlobalObject(context); - - global_classDefinition.staticValues = global_values; - global_classDefinition.staticFunctions = global_functions; - jsc_registerClass(context, globalObject, "cvar", &global_classDefinition); - - ${initializercode} - - return true; -} -%} - -%fragment ("create_class_template", "templates") -%{ ${classname}_classDefinition.staticFunctions = ${classname}_functions; - ${classname}_classDefinition.staticValues = ${classname}_values; -%} - -%fragment ("register_class", "templates") -%{jsc_registerClass(context, globalObject, "${classname}", &${classname}_classDefinition, _wrap_create_${classname});%} - -%fragment ("jsc_register_global_function", "templates") -%{jsc_registerFunction(${context}, ${context_object}, "${functionname}", ${functionwrapper});%} - -/* Additional typemaps for the class code needed by the access layer. This code is copy-pasted into the wrapper header and source file */ %fragment ("getproperty", "templates") %{ JSValueRef ${getname}(JSContextRef context, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef* exception) @@ -82,6 +56,16 @@ %fragment ("classdefn", "templates") %{ +JSStaticValue ${classname}_staticValues[] = { + ${jsstaticclassvariables} + { 0, 0, 0, 0 } +}; + +JSStaticFunction ${classname}_staticFunctions[] = { + ${jsstaticclassfunctions} + { 0, 0, 0 } +}; + JSStaticValue ${classname}_values[] = { ${jsclassvariables} { 0, 0, 0, 0 } @@ -93,6 +77,10 @@ }; JSClassDefinition ${classname}_classDefinition; + +JSClassDefinition ${classname}_objectDefinition; + +JSClassRef ${classname}_classRef; %} %fragment ("destructordefn", "templates") @@ -108,42 +96,67 @@ %fragment ("mainctordefn", "templates") %{ JSObjectRef _wrap_create_${classname}(JSContextRef context, JSObjectRef ctorObject, - size_t argc, const JSValueRef argv[],JSValueRef* exception) + size_t argc, const JSValueRef argv[], JSValueRef* exception) { - JSObjectRef jsresult = 0; - ${type}* result = 0; + JSObjectRef thisObject = JSObjectMake(context, ${classname}_classRef, NULL); ${DISPATCH_CASES} { // TODO: handle illegal arguments - jsresult = JSValueToObject(context, JSValueMakeUndefined(context), exception); + thisObject = JSValueToObject(context, JSValueMakeUndefined(context), exception); } - return jsresult; + return thisObject; } %} %fragment ("ctor_dispatch_case", "templates") %{if(argc == ${argcount}) { - jsresult = _wrap_create_${classname}${overloadext}(context, ctorObject, argc, argv, exception); + thisObject = _wrap_create_${classname}${overloadext}(context, thisObject, argc, argv, exception); } else %} %fragment ("ctordefn", "templates") %{ -JSObjectRef _wrap_create_${classname}${overloadext}(JSContextRef context, JSObjectRef thisObject, - size_t argc, const JSValueRef argv[], JSValueRef* exception) +JSObjectRef _wrap_create_${classname}${overloadext}(JSContextRef context, JSObjectRef thisObject, size_t argc, const JSValueRef argv[], JSValueRef* exception) { ${LOCALS} ${CODE} - - JSClassRef classTemplate = JSClassCreate(&${classname}_classDefinition); SWIG_PRV_DATA *swigprivatedata = new SWIG_PRV_DATA(); swigprivatedata->swigCMemOwn = true; swigprivatedata->swigCObject = result; - JSObjectRef jsobject = JSObjectMake(context, classTemplate, swigprivatedata); + JSObjectSetPrivate(thisObject, swigprivatedata); - return jsobject; + return thisObject; } %} + +%fragment ("jsc_initializer", "templates") %{ +bool ${modulename}_initialize(JSGlobalContextRef context) { + JSObjectRef globalObject = JSContextGetGlobalObject(context); + + global_classDefinition.staticValues = global_values; + global_classDefinition.staticFunctions = global_functions; + jsc_registerClass(context, globalObject, "cvar", &global_classDefinition); + + ${initializercode} + + return true; +} +%} + +%fragment ("create_class_template", "templates") +%{ + ${classname}_classDefinition.staticFunctions = ${classname}_staticFunctions; + ${classname}_classDefinition.staticValues = ${classname}_staticValues; + ${classname}_classDefinition.callAsConstructor = _wrap_create_${classname}; + ${classname}_objectDefinition.staticValues = ${classname}_values; + ${classname}_objectDefinition.staticFunctions = ${classname}_functions; + ${classname}_classRef = JSClassCreate(&${classname}_objectDefinition);%} + +%fragment ("register_class", "templates") +%{jsc_registerClass(context, globalObject, "${classname}", &${classname}_classDefinition);%} + +%fragment ("jsc_register_global_function", "templates") +%{jsc_registerFunction(${context}, ${context_object}, "${functionname}", ${functionwrapper});%} Modified: branches/gsoc2012-javascript/Lib/javascript/jsc/javascripthelpers.swg =================================================================== --- branches/gsoc2012-javascript/Lib/javascript/jsc/javascripthelpers.swg 2012-07-02 21:18:39 UTC (rev 13240) +++ branches/gsoc2012-javascript/Lib/javascript/jsc/javascripthelpers.swg 2012-07-02 21:21:53 UTC (rev 13241) @@ -1,24 +1,18 @@ %insert(runtime) %{ - bool jsc_registerClass(JSGlobalContextRef& context, JSObjectRef& parentObject, const char* className, - JSClassDefinition* definition, - JSObjectCallAsConstructorCallback ctor = NULL) { + JSClassDefinition* definition) { JSStringRef js_className = JSStringCreateWithUTF8CString(className); - JSObjectRef classObject; - if(ctor == NULL) { - classObject = JSObjectMake(context, JSClassCreate(definition), NULL); - } else { - classObject = JSObjectMakeConstructor(context, JSClassCreate(definition), ctor); - } + JSObjectRef classObject = JSObjectMake(context, JSClassCreate(definition), NULL); JSObjectSetProperty(context, parentObject, js_className, classObject, kJSPropertyAttributeNone, NULL); JSStringRelease(js_className); + return true; } Modified: branches/gsoc2012-javascript/Source/Modules/javascript_jsc.cxx =================================================================== --- branches/gsoc2012-javascript/Source/Modules/javascript_jsc.cxx 2012-07-02 21:18:39 UTC (rev 13240) +++ branches/gsoc2012-javascript/Source/Modules/javascript_jsc.cxx 2012-07-02 21:21:53 UTC (rev 13241) @@ -201,7 +201,13 @@ .Replace("${functionwrapper}", current_functionwrapper); if(GetFlag(n, "ismember")) { - Printv(js_class_functions_code, t_function.str(), 0); + if(Equal(Getattr(n, "storage"), "static")) { + Printv(js_class_static_functions_code, t_function.str(), 0); + + } else { + Printv(js_class_functions_code, t_function.str(), 0); + } + } else { Printv(js_global_functions_code, t_function.str(), 0); } @@ -224,16 +230,19 @@ t_variable.Replace("${setname}", current_setter) .Replace("${getname}", current_getter) .Replace("${propertyname}", current_propertyname); - if(GetFlag(n, "ismember")) - { - Printv(js_class_variables_code, t_variable.str(), 0); - } - else - { + + if(GetFlag(n, "ismember")) { + if(Equal(Getattr(n, "storage"), "static")) { + Printv(js_class_static_variables_code, t_variable.str(), 0); + + } else { + Printv(js_class_variables_code, t_variable.str(), 0); + } + } else { Printv(js_global_variables_code, t_variable.str(), 0); - } + } - return SWIG_OK; + return SWIG_OK; } int JSCEmitter::EnterClass(Node *n) @@ -242,6 +251,9 @@ js_class_variables_code = NewString(""); js_class_functions_code = NewString(""); + js_class_static_variables_code = NewString(""); + js_class_static_functions_code = NewString(""); + js_ctor_wrappers = NewString(""); js_ctor_dispatcher_code = NewString(""); @@ -251,9 +263,11 @@ int JSCEmitter::ExitClass(Node*) { Template t_class(GetTemplate("classdefn")); - t_class.Replace("${jsclassvariables}", js_class_variables_code) - .Replace("${jsclassfunctions}", js_class_functions_code) - .Replace("${classname}", current_classname); + t_class.Replace("${classname}", current_classname) + .Replace("${jsclassvariables}", js_class_variables_code) + .Replace("${jsclassfunctions}", js_class_functions_code) + .Replace("${jsstaticclassfunctions}", js_class_static_functions_code) + .Replace("${jsstaticclassvariables}", js_class_static_variables_code); Wrapper_pretty_print(t_class.str(), f_wrappers); /* @@ -269,7 +283,7 @@ .Replace("${DISPATCH_CASES}", js_ctor_dispatcher_code); Wrapper_pretty_print(t_mainctor.str(), f_wrappers); - + /* adds the dtor wrapper */ Template t_dtor(GetTemplate("destructordefn")); t_dtor.Replace("${classname}", current_classname) .Replace("${type}", current_classname); @@ -285,12 +299,17 @@ t_registerclass.Replace("${classname}", current_classname); Wrapper_pretty_print(t_registerclass.str(), js_initializer_code); + /* clean up all DOHs */ Delete(js_class_variables_code); Delete(js_class_functions_code); + Delete(js_class_static_variables_code); + Delete(js_class_static_functions_code); Delete(js_ctor_wrappers); Delete(js_ctor_dispatcher_code); js_class_variables_code = 0; js_class_functions_code = 0; + js_class_static_variables_code = 0; + js_class_static_functions_code = 0; js_ctor_wrappers = 0; js_ctor_dispatcher_code = 0; Modified: branches/gsoc2012-javascript/Source/Modules/javascript_jsc.h =================================================================== --- branches/gsoc2012-javascript/Source/Modules/javascript_jsc.h 2012-07-02 21:18:39 UTC (rev 13240) +++ branches/gsoc2012-javascript/Source/Modules/javascript_jsc.h 2012-07-02 21:21:53 UTC (rev 13241) @@ -66,22 +66,30 @@ File *f_wrappers; File *f_init; - // state variables + String* NULL_STR; - String* current_propertyname; - String* current_getter; - String* current_setter; - String* current_classname; - String* NULL_STR; + // dynamically filled code parts String *js_global_functions_code; String *js_global_variables_code; + String *js_class_functions_code; String *js_class_variables_code; - String *js_initializer_code; + String *js_class_static_functions_code; + String *js_class_static_variables_code; + String *js_ctor_wrappers; String *js_ctor_dispatcher_code; + + String *js_initializer_code; + + // state variables + String* current_propertyname; + String* current_getter; + String* current_setter; + String* current_classname; String *current_functionwrapper; String *current_functionname; + }; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |