Improve names and comments of code templates for javascript emitters.

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/oliverb-javascript-v8@13826 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Oliver Buchtala 2012-09-08 01:15:51 +00:00
commit 131a106204
3 changed files with 428 additions and 276 deletions

View file

@ -1,12 +1,108 @@
/*********************************************************************
*getproperty: This template gives name to generated wrapper for the getproperty
*{LOCALS}: declarations for input arguments
*{CODE}: contains input marshalling, and the action
*********************************************************************/
%fragment ("JS_getproperty", "templates")
/* -----------------------------------------------------------------------------
* js_ctor: template for wrapping a ctor.
* - $jswrapper: wrapper of called ctor
* - $jslocals: locals part of wrapper
* - $jscode: code part of wrapper
* - $jsargcount: number of arguments
* - $jsmangledtype: mangled type of class
* ----------------------------------------------------------------------------- */
%fragment ("js_ctor", "templates")
%{
JSValueRef $jsgetter(JSContextRef context, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef* exception)
JSObjectRef $jswrapper(JSContextRef context, JSObjectRef thisObject, size_t argc, const JSValueRef argv[], JSValueRef* exception)
{
$jslocals
if(argc != $jsargcount) SWIG_exception_fail(SWIG_ERROR, "Illegal number of arguments for $jswrapper.");
$jscode
return SWIG_JSC_NewPointerObj(context, result, SWIGTYPE_$jsmangledtype, SWIG_POINTER_OWN);
goto fail;
fail:
return NULL;
}
%}
/* -----------------------------------------------------------------------------
* js_veto_ctor: a vetoing ctor for abstract classes
* - $jswrapper: name of wrapper
* - $jsname: class name
* ----------------------------------------------------------------------------- */
%fragment ("js_veto_ctor", "templates")
%{
JSObjectRef $jswrapper(JSContextRef context, JSObjectRef ctorObject,
size_t argc, const JSValueRef argv[], JSValueRef* exception)
{
SWIG_exception(SWIG_ERROR, "Class $jsname can not be instantiated");
return 0;
}
%}
/* -----------------------------------------------------------------------------
* js_ctor_dispatcher: dispatcher for overloaded constructors
* - $jswrapper: name of wrapper
* - $jsname: class name
* - $jsdispatchcases: part containing code for dispatching
* ----------------------------------------------------------------------------- */
%fragment ("js_ctor_dispatcher", "templates")
%{
JSObjectRef $jswrapper(JSContextRef context, JSObjectRef ctorObject,
size_t argc, const JSValueRef argv[], JSValueRef* exception)
{
JSObjectRef thisObject = NULL;
// switch all cases by means of series of if-returns.
$jsdispatchcases
// default:
SWIG_exception_fail(SWIG_ERROR, "Illegal arguments for contruction of $jsname");
fail:
return thisObject;
}
%}
/* -----------------------------------------------------------------------------
* js_ctor_dispatch_case: template for a dispatch case for calling an overloaded ctor.
* - $jsargcount: number of arguments of called ctor
* - $jswrapper: wrapper of called ctor
*
* Note: a try-catch-like mechanism is used to switch cases
* ----------------------------------------------------------------------------- */
%fragment ("js_ctor_dispatch_case", "templates")
%{
if(argc == $jsargcount) {
thisObject = $jswrapper(context, NULL, argc, argv, exception);
if(thisObject != NULL) { *exception=0; return thisObject; } /* reset exception and return */
}
%}
/* -----------------------------------------------------------------------------
* js_dtor: template for a destructor wrapper
* - $jsmangledname: mangled class name
* - $jstype: class type
* ----------------------------------------------------------------------------- */
%fragment ("js_dtor", "templates")
%{
void $jswrapper(JSObjectRef thisObject)
{
SWIG_PRV_DATA* t = (SWIG_PRV_DATA*) JSObjectGetPrivate(thisObject);
if(t && t->swigCMemOwn) free (($jstype*)t->swigCObject);
if(t) free(t);
}
%}
/* -----------------------------------------------------------------------------
* js_getter: template for getter function wrappers
* - $jswrapper: wrapper function name
* - $jslocals: locals part of wrapper
* - $jscode: code part of wrapper
* ----------------------------------------------------------------------------- */
%fragment ("js_getter", "templates")
%{
JSValueRef $jswrapper(JSContextRef context, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef* exception)
{
$jslocals
JSValueRef jsresult;
@ -20,15 +116,15 @@ JSValueRef $jsgetter(JSContextRef context, JSObjectRef thisObject, JSStringRef p
}
%}
/**********************************************************************
*setproperty: This template gives name to generated wrapper for the setproperty
*{LOCALS}: declarations for input arguments
*{CODE}: contains input marshalling, and the action
**********************************************************************/
%fragment ("JS_setproperty", "templates")
/* -----------------------------------------------------------------------------
* js_setter: template for setter function wrappers
* - $jswrapper: wrapper function name
* - $jslocals: locals part of wrapper
* - $jscode: code part of wrapper
* ----------------------------------------------------------------------------- */
%fragment ("js_setter", "templates")
%{
bool $jssetter(JSContextRef context, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef value, JSValueRef* exception)
bool $jswrapper(JSContextRef context, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef value, JSValueRef* exception)
{
$jslocals
$jscode
@ -41,12 +137,13 @@ bool $jssetter(JSContextRef context, JSObjectRef thisObject, JSStringRef propert
}
%}
/************************************************************************************
*functionwrapper: This template gives name to generated wrapper for the function
*{LOCALS}: declarations for input arguments
*{CODE} contains input marshalling, and the action
************************************************************************************/
%fragment ("JS_functionwrapper", "templates")
/* -----------------------------------------------------------------------------
* js_function: template for function wrappers
* - $jswrapper: wrapper function name
* - $jslocals: locals part of wrapper
* - $jscode: code part of wrapper
* ----------------------------------------------------------------------------- */
%fragment ("js_function", "templates")
%{
JSValueRef $jswrapper(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argc, const JSValueRef argv[], JSValueRef* exception)
{
@ -64,7 +161,14 @@ JSValueRef $jswrapper(JSContextRef context, JSObjectRef function, JSObjectRef th
}
%}
%fragment ("JS_function_dispatcher", "templates")
/* -----------------------------------------------------------------------------
* js_function_dispatcher: template for a function dispatcher for overloaded functions
* - $jswrapper: wrapper function name
* - $jsname: name of the wrapped function
* - $jslocals: locals part of wrapper
* - $jscode: code part of wrapper
* ----------------------------------------------------------------------------- */
%fragment ("js_function_dispatcher", "templates")
%{
JSValueRef $jswrapper(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argc, const JSValueRef argv[], JSValueRef* exception)
{
@ -82,7 +186,13 @@ JSValueRef $jswrapper(JSContextRef context, JSObjectRef function, JSObjectRef th
}
%}
%fragment ("JS_functionwrapper_overload", "templates")
/* -----------------------------------------------------------------------------
* js_overloaded_function: template for a overloaded function
* - $jswrapper: wrapper function name
* - $jslocals: locals part of wrapper
* - $jscode: code part of wrapper
* ----------------------------------------------------------------------------- */
%fragment ("js_overloaded_function", "templates")
%{
int $jswrapper(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argc, const JSValueRef argv[], JSValueRef* exception, JSValueRef* result)
{
@ -101,51 +211,87 @@ int $jswrapper(JSContextRef context, JSObjectRef function, JSObjectRef thisObjec
}
%}
/***********************************************************************
* JS_function_dispatch_case:
* This template is used to create a branch for dispatching
* to an overloaded function.
***********************************************************************/
%fragment ("JS_function_dispatch_case", "templates")
%{if(argc == $jsargcount) {
/* -----------------------------------------------------------------------------
* js_function_dispatch_case: template for a case used in the function dispatcher
* - $jswrapper: wrapper function name
* - $jsargcount: number of arguments of overloaded function
* - $jscode: code part of wrapper
* ----------------------------------------------------------------------------- */
%fragment ("js_function_dispatch_case", "templates")
%{
if(argc == $jsargcount) {
res = $jswrapper(context, function, thisObject, argc, argv, exception, &jsresult);
if(res == SWIG_OK) { *exception = 0; return jsresult; }
}
%}
/* Added template for function declaration */
/* -----------------------------------------------------------------------------
* js_initializer: template for the module initializer function
* - $jsname: module name
* - $jscreatenamespaces: part with code for creating namespace objects
* - $jscreateclasses: part with code for creating classes
* - $jsregisternamespaces: part with code for registration of namespaces
* ----------------------------------------------------------------------------- */
%fragment ("js_initializer", "templates") %{
#ifdef __cplusplus
extern "C" {
#endif
%fragment ("JS_variabledecl", "templates")
%{{"$jsname", $jsgetter, $jssetter, kJSPropertyAttributeNone},%}
bool $jsname_initialize(JSGlobalContextRef context) {
SWIG_InitializeModule(0);
JSObjectRef global_object = JSContextGetGlobalObject(context);
/* Initialize the base swig type object */
_SwigObject_objectDefinition.staticFunctions = _SwigObject_functions;
_SwigObject_objectDefinition.staticValues = _SwigObject_values;
_SwigObject_classRef = JSClassCreate(&_SwigObject_objectDefinition);
/* Create objects for namespaces */
$jscreatenamespaces
/* Added template for function declaration */
/* Register classes */
$jsregisterclasses
%fragment ("JS_functiondecl", "templates")
%{{"$jsname",$jswrapper, kJSPropertyAttributeNone},%}
/* Register namespaces */
$jsregisternamespaces
%fragment ("JS_globaldefn", "templates")
%{
JSStaticValue $jsnspace_values[] = {
$jsglobalvariables
{ 0, 0, 0, 0 }
};
return true;
}
JSStaticFunction $jsnspace_functions[] = {
$jsglobalfunctions
{ 0, 0, 0 }
};
#ifdef __cplusplus
}
#endif
JSClassDefinition $jsnspace_classDefinition;
%}
/***********************************************************************
* class_definition:
* declarations of javascript class definition objects.
***********************************************************************/
/* -----------------------------------------------------------------------------
* jsc_variable_declaration: template for a variable table entry
* - $jsname: name of the variable
* - $jsgetter: wrapper of getter function
* - $jssetter: wrapper of setter function
* ----------------------------------------------------------------------------- */
%fragment ("jsc_variable_declaration", "templates")
%{
{"$jsname", $jsgetter, $jssetter, kJSPropertyAttributeNone},
%}
%fragment ("JS_class_definition", "templates")
/* -----------------------------------------------------------------------------
* jsc_function_declaration: template for a function table entry
* - $jsname: name of the variable
* - $jswrapper: wrapper function
* ----------------------------------------------------------------------------- */
%fragment ("jsc_function_declaration", "templates")
%{
{"$jsname", $jswrapper, kJSPropertyAttributeNone},
%}
/* -----------------------------------------------------------------------------
* jsc_classtemplate_declaration: template for a namespace declaration
* - $jsmangledname: mangled class name
* ----------------------------------------------------------------------------- */
%fragment ("jsc_class_declaration", "templates")
%{
JSClassDefinition $jsmangledname_classDefinition;
@ -154,12 +300,15 @@ JSClassDefinition $jsmangledname_objectDefinition;
JSClassRef $jsmangledname_classRef;
%}
/***********************************************************************
* class_table:
* function and variable tables for class and object creation.
***********************************************************************/
%fragment ("JS_class_tables", "templates")
/* -----------------------------------------------------------------------------
* jsc_class_tables: template for a namespace declaration
* - $jsmangledname: mangled class name
* - $jsstaticclassvariables: list of static variable entries
* - $jsstaticclassfunctions: list of static function entries
* - $jsclassvariables: list of member variable entries
* - $jsclassfunctions: list of member function entries
* ----------------------------------------------------------------------------- */
%fragment ("jsc_class_tables", "templates")
%{
JSStaticValue $jsmangledname_staticValues[] = {
$jsstaticclassvariables
@ -182,154 +331,76 @@ JSStaticFunction $jsmangledname_functions[] = {
};
%}
/*********************************************************************
* destructordefn:
* This code template is used to adds the destructor wrapper function
***********************************************************************/
/* -----------------------------------------------------------------------------
* jsc_define_class_template: template for defining a class template
* - $jsmangledname: mangled class name
* - $jsmangledtype: mangled class type
* - $jsctor: wrapper of ctor
* - $jsbaseclass: mangled name of base class
* ----------------------------------------------------------------------------- */
%fragment ("jsc_class_definition", "templates")
%{
$jsmangledname_classDefinition.staticFunctions = $jsmangledname_staticFunctions;
$jsmangledname_classDefinition.staticValues = $jsmangledname_staticValues;
$jsmangledname_classDefinition.callAsConstructor = $jsctor;
$jsmangledname_objectDefinition.staticValues = $jsmangledname_values;
$jsmangledname_objectDefinition.staticFunctions = $jsmangledname_functions;
$jsmangledname_objectDefinition.parentClass = $jsbaseclass_classRef;
JSClassRef $jsmangledname_classRef = JSClassCreate(&$jsmangledname_objectDefinition);
SWIGTYPE_$jsmangledtype->clientdata = $jsmangledname_classRef;
%}
%fragment ("JS_destructordefn", "templates")
/* -----------------------------------------------------------------------------
* jsc_register_class: template for registration of a class
* - $jsname: class name
* - $jsmangledname: mangled class name
* - $jsnspace: mangled name of namespace
* ----------------------------------------------------------------------------- */
%fragment ("jsc_class_registration", "templates")
%{
void _wrap_$jsmangledname_finalize(JSObjectRef thisObject)
{
SWIG_PRV_DATA* t = (SWIG_PRV_DATA*)JSObjectGetPrivate(thisObject);
if(t && t->swigCMemOwn) free (($jstype*)t->swigCObject);
if(t) free(t);
}
JS_registerClass(context, $jsnspace_object, "$jsname", &$jsmangledname_classDefinition);
%}
/*********************************************************************
* constructor_definition:
* This code template is used to adds the main constructor wrapper function
***********************************************************************/
%fragment ("JS_mainctordefn", "templates")
/* -----------------------------------------------------------------------------
* jsc_nspace_declaration: template for a namespace declaration
* - $jsnspace: mangled name of the namespace
* - $jsglobalvariables: list of variable entries
* - $jsglobalfunctions: list if fuction entries
* ----------------------------------------------------------------------------- */
%fragment ("jsc_nspace_declaration", "templates")
%{
JSObjectRef $jswrapper(JSContextRef context, JSObjectRef ctorObject,
size_t argc, const JSValueRef argv[], JSValueRef* exception)
{
JSObjectRef thisObject = NULL;
// switch all cases by means of series of if-returns.
$jsdispatchcases
JSStaticValue $jsnspace_values[] = {
$jsglobalvariables
{ 0, 0, 0, 0 }
};
// default:
SWIG_exception_fail(SWIG_ERROR, "Illegal arguments for contruction of $jsmangledname");
fail:
return thisObject;
}
JSStaticFunction $jsnspace_functions[] = {
$jsglobalfunctions
{ 0, 0, 0 }
};
JSClassDefinition $jsnspace_classDefinition;
%}
%fragment ("JS_veto_ctor", "templates")
/* -----------------------------------------------------------------------------
* jsc_nspace_definition: template for definition of a namespace object
* - $jsmangledname: mangled name of namespace
* ----------------------------------------------------------------------------- */
%fragment ("jsc_nspace_definition", "templates")
%{
$jsmangledname_classDefinition.staticFunctions = $jsmangledname_functions;
$jsmangledname_classDefinition.staticValues = $jsmangledname_values;
JSObjectRef $jsmangledname_object = JSObjectMake(context, JSClassCreate(&$jsmangledname_classDefinition), NULL);
%}
/* -----------------------------------------------------------------------------
* jsc_nspace_registration: template for registration of a namespace object
* - $jsname: name of namespace
* - $jsmangledname: mangled name of namespace
* - $jsparent: mangled name of parent namespace
* ----------------------------------------------------------------------------- */
%fragment ("jsc_nspace_registration", "templates")
%{
JSObjectRef $jsctor(JSContextRef context, JSObjectRef ctorObject,
size_t argc, const JSValueRef argv[], JSValueRef* exception)
{
SWIG_exception(SWIG_ERROR, "Class $jsname can not be instantiated");
return 0;
}
JS_registerNamespace(context, $jsmangledname_object, $jsparent_object, "$jsname");
%}
/**************************************************************************************
ctor_dispatch_case: This template is used for the constructor which is overloaded
***************************************************************************************/
%fragment ("JS_ctor_dispatch_case", "templates")
%{if(argc == $jsargcount) {
thisObject = $jswrapper(context, NULL, argc, argv, exception);
if(thisObject != NULL) { *exception=0; return thisObject; } /* reset exception and return */
}
%}
%fragment ("JS_ctordefn", "templates")
%{
JSObjectRef $jswrapper(JSContextRef context, JSObjectRef thisObject, size_t argc, const JSValueRef argv[], JSValueRef* exception)
{
$jslocals
if(argc != $jsargcount) SWIG_exception_fail(SWIG_ERROR, "Illegal number of arguments for $jswrapper.");
$jscode
return SWIG_JSC_NewPointerObj(context, result, SWIGTYPE_$jsmangledtype, SWIG_POINTER_OWN);
goto fail;
fail:
return NULL;
}
%}
/**********************************************************************
initializer:This template is dynamic growing and aggregates everything
**********************************************************************/
%fragment ("JS_initializer", "templates") %{
#ifdef __cplusplus
extern "C" {
#endif
bool $jsname_initialize(JSGlobalContextRef context) {
SWIG_InitializeModule(0);
JSObjectRef global_object = JSContextGetGlobalObject(context);
/* Initialize the base swig type object */
_SwigObject_objectDefinition.staticFunctions = _SwigObject_functions;
_SwigObject_objectDefinition.staticValues = _SwigObject_values;
_SwigObject_classRef = JSClassCreate(&_SwigObject_objectDefinition);
/* Create objects for namespaces */
$jscreatenamespaces
/* Create classes */
$jsinitializercode
/* Register namespaces */
$jsregisternamespaces
return true;
}
#ifdef __cplusplus
}
#endif
%}
/*****************************************************************************************
*create_class_template:
*This template is used to add a Static references to class templates.
*****************************************************************************************/
%fragment ("JS_create_class_template", "templates")
%{ $jsmangledname_classDefinition.staticFunctions = $jsmangledname_staticFunctions;
$jsmangledname_classDefinition.staticValues = $jsmangledname_staticValues;
$jsmangledname_classDefinition.callAsConstructor = $jsctor;
$jsmangledname_objectDefinition.staticValues = $jsmangledname_values;
$jsmangledname_objectDefinition.staticFunctions = $jsmangledname_functions;
$jsmangledname_objectDefinition.parentClass = $jsbaseclass_classRef;
JSClassRef $jsmangledname_classRef = JSClassCreate(&$jsmangledname_objectDefinition);
SWIGTYPE_$jsmangledtype->clientdata = $jsmangledname_classRef;%}
/*****************************************************************************************
*register_class:
* This template is used to adds a class registration statement to initializer function
*****************************************************************************************/
%fragment ("JS_register_class", "templates")
%{JS_registerClass(context, $jsnspace_object, "$jsname", &$jsmangledname_classDefinition);%}
/* create and register namespaces */
%fragment ("JS_create_namespace", "templates")
%{ $jsmangledname_classDefinition.staticFunctions = $jsmangledname_functions;
$jsmangledname_classDefinition.staticValues = $jsmangledname_values;
JSObjectRef $jsmangledname_object = JSObjectMake(context, JSClassCreate(&$jsmangledname_classDefinition), NULL);
%}
%fragment ("JS_register_namespace", "templates")
%{
JS_registerNamespace(context, $jsmangledname_object, $jsparent_object, "$jsname"); %}