Add support for type-based dispatching of overloaded functions.
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/oliverb-javascript-v8@13779 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
eff094ef39
commit
07d5ec24ce
2 changed files with 54 additions and 23 deletions
|
|
@ -58,21 +58,37 @@ JSValueRef ${functionname}(JSContextRef context, JSObjectRef function, JSObjectR
|
||||||
}
|
}
|
||||||
%}
|
%}
|
||||||
|
|
||||||
/**************************************************************************************
|
%fragment ("JS_functionwrapper_overload", "templates")
|
||||||
function_dispatch_case: This template is used for the function which is overloaded
|
%{
|
||||||
***************************************************************************************/
|
int ${functionname}(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argc, const JSValueRef argv[], JSValueRef* exception, JSValueRef* result)
|
||||||
|
{
|
||||||
|
${LOCALS}
|
||||||
|
${CODE}
|
||||||
|
*result = jsresult;
|
||||||
|
return SWIG_OK;
|
||||||
|
|
||||||
|
goto fail;
|
||||||
|
fail:
|
||||||
|
return SWIG_TypeError;
|
||||||
|
}
|
||||||
|
%}
|
||||||
|
|
||||||
|
/***********************************************************************
|
||||||
|
* JS_function_dispatch_case:
|
||||||
|
* This template is used to create a branch for dispatching
|
||||||
|
* to an overloaded function.
|
||||||
|
***********************************************************************/
|
||||||
|
|
||||||
%fragment ("JS_function_dispatch_case", "templates")
|
%fragment ("JS_function_dispatch_case", "templates")
|
||||||
%{if(argc == ${argcount}) {
|
%{if(argc == ${argcount}) {
|
||||||
jsresult = ${functionwrapper}(context, function, thisObject, argc, argv, exception);
|
res = ${functionwrapper}(context, function, thisObject, argc, argv, exception, &jsresult);
|
||||||
} else %}
|
if(res == SWIG_OK) { *exception = 0; return jsresult; }
|
||||||
|
}
|
||||||
|
%}
|
||||||
|
|
||||||
%fragment ("JS_function_dispatch_case_default", "templates")
|
%fragment ("JS_function_dispatch_case_default", "templates")
|
||||||
%{
|
%{
|
||||||
{
|
SWIG_exception_fail(SWIG_ERROR, "Illegal arguments for function ${functionname}.");
|
||||||
// TODO: throw JS exception
|
|
||||||
throw "Invalid function arguments.";
|
|
||||||
}
|
|
||||||
%}
|
%}
|
||||||
|
|
||||||
/* Added template for function declaration */
|
/* Added template for function declaration */
|
||||||
|
|
@ -173,18 +189,16 @@ void _wrap_${classname_mangled}_finalize(JSObjectRef thisObject)
|
||||||
JSObjectRef _wrap_create_${classname_mangled}(JSContextRef context, JSObjectRef ctorObject,
|
JSObjectRef _wrap_create_${classname_mangled}(JSContextRef context, JSObjectRef ctorObject,
|
||||||
size_t argc, const JSValueRef argv[], JSValueRef* exception)
|
size_t argc, const JSValueRef argv[], JSValueRef* exception)
|
||||||
{
|
{
|
||||||
JSObjectRef thisObject;
|
JSObjectRef thisObject = NULL;
|
||||||
|
|
||||||
|
// switch all cases by means of series of if-returns.
|
||||||
${DISPATCH_CASES}
|
${DISPATCH_CASES}
|
||||||
{
|
|
||||||
// TODO: handle illegal arguments
|
|
||||||
SWIG_exception_fail(SWIG_ERROR, "Illegal arguments for contruction of ${classname_mangled}");
|
|
||||||
}
|
|
||||||
|
|
||||||
return thisObject;
|
// default:
|
||||||
|
SWIG_exception_fail(SWIG_ERROR, "Illegal arguments for contruction of ${classname_mangled}");
|
||||||
|
|
||||||
fail:
|
fail:
|
||||||
return NULL;
|
return thisObject;
|
||||||
}
|
}
|
||||||
%}
|
%}
|
||||||
|
|
||||||
|
|
@ -195,7 +209,9 @@ ctor_dispatch_case: This template is used for the constructor which is overloade
|
||||||
%fragment ("JS_ctor_dispatch_case", "templates")
|
%fragment ("JS_ctor_dispatch_case", "templates")
|
||||||
%{if(argc == ${argcount}) {
|
%{if(argc == ${argcount}) {
|
||||||
thisObject = _wrap_create_${classname_mangled}${overloadext}(context, NULL, argc, argv, exception);
|
thisObject = _wrap_create_${classname_mangled}${overloadext}(context, NULL, argc, argv, exception);
|
||||||
} else %}
|
if(thisObject != NULL) { *exception=0; return thisObject; } /* reset exception and return */
|
||||||
|
}
|
||||||
|
%}
|
||||||
|
|
||||||
|
|
||||||
%fragment ("JS_ctordefn", "templates")
|
%fragment ("JS_ctordefn", "templates")
|
||||||
|
|
|
||||||
|
|
@ -71,6 +71,8 @@ public:
|
||||||
|
|
||||||
Template& pretty_print(DOH *doh);
|
Template& pretty_print(DOH *doh);
|
||||||
|
|
||||||
|
void operator=(const Template& t);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
String *code;
|
String *code;
|
||||||
|
|
@ -1019,8 +1021,8 @@ int JSCEmitter::enterFunction(Node *n) {
|
||||||
|
|
||||||
/* Initialize DOH for collecting function dispatchers */
|
/* Initialize DOH for collecting function dispatchers */
|
||||||
bool is_overloaded = GetFlag(n, "sym:overloaded");
|
bool is_overloaded = GetFlag(n, "sym:overloaded");
|
||||||
if (is_overloaded && state.function(FUNCTION_DISPATCHERS) == 0) {
|
if (is_overloaded && state.global(FUNCTION_DISPATCHERS) == 0) {
|
||||||
state.function(FUNCTION_DISPATCHERS, NewString(""));
|
state.global(FUNCTION_DISPATCHERS, NewString(""));
|
||||||
}
|
}
|
||||||
|
|
||||||
return SWIG_OK;
|
return SWIG_OK;
|
||||||
|
|
@ -1333,6 +1335,7 @@ int JSEmitter::emitFunction(Node *n, bool is_member, bool is_static) {
|
||||||
String *wrap_name = Swig_name_wrapper(Getattr(n, "sym:name"));
|
String *wrap_name = Swig_name_wrapper(Getattr(n, "sym:name"));
|
||||||
if (is_overloaded) {
|
if (is_overloaded) {
|
||||||
Append(wrap_name, Getattr(n, "sym:overname"));
|
Append(wrap_name, Getattr(n, "sym:overname"));
|
||||||
|
t_function = getTemplate("JS_functionwrapper_overload");
|
||||||
}
|
}
|
||||||
Setattr(n, "wrap:name", wrap_name);
|
Setattr(n, "wrap:name", wrap_name);
|
||||||
state.function(WRAPPER_NAME, wrap_name);
|
state.function(WRAPPER_NAME, wrap_name);
|
||||||
|
|
@ -1364,7 +1367,7 @@ int JSEmitter::emitFunction(Node *n, bool is_member, bool is_static) {
|
||||||
t_dispatch_case.replace("${functionwrapper}", wrap_name)
|
t_dispatch_case.replace("${functionwrapper}", wrap_name)
|
||||||
.replace("${argcount}", argcount);
|
.replace("${argcount}", argcount);
|
||||||
|
|
||||||
Append(state.function(FUNCTION_DISPATCHERS), t_dispatch_case.str());
|
Append(state.global(FUNCTION_DISPATCHERS), t_dispatch_case.str());
|
||||||
|
|
||||||
Delete(argcount);
|
Delete(argcount);
|
||||||
}
|
}
|
||||||
|
|
@ -1382,16 +1385,21 @@ int JSEmitter::emitFunctionDispatcher(Node *n, bool /*is_member */ ) {
|
||||||
String *wrap_name = Swig_name_wrapper(Getattr(n, "name"));
|
String *wrap_name = Swig_name_wrapper(Getattr(n, "name"));
|
||||||
Setattr(n, "wrap:name", wrap_name);
|
Setattr(n, "wrap:name", wrap_name);
|
||||||
|
|
||||||
|
Wrapper_add_local(wrapper, "res", "int res");
|
||||||
Wrapper_add_local(wrapper, "jsresult", "JSValueRef jsresult");
|
Wrapper_add_local(wrapper, "jsresult", "JSValueRef jsresult");
|
||||||
|
|
||||||
Append(wrapper->code, state.function(FUNCTION_DISPATCHERS));
|
Append(wrapper->code, state.global(FUNCTION_DISPATCHERS));
|
||||||
Append(wrapper->code, getTemplate("JS_function_dispatch_case_default").str());
|
Append(wrapper->code, getTemplate("JS_function_dispatch_case_default").str());
|
||||||
|
|
||||||
|
t_function.replace("${LOCALS}", wrapper->locals)
|
||||||
|
.replace("${CODE}", wrapper->code);
|
||||||
|
|
||||||
|
// call this here, to replace all variables
|
||||||
t_function.replace("${functionname}", wrap_name)
|
t_function.replace("${functionname}", wrap_name)
|
||||||
.replace("${LOCALS}", wrapper->locals)
|
|
||||||
.replace("${CODE}", wrapper->code)
|
|
||||||
.pretty_print(f_wrappers);
|
.pretty_print(f_wrappers);
|
||||||
|
|
||||||
|
// Delete the state variable
|
||||||
|
state.global(FUNCTION_DISPATCHERS, 0);
|
||||||
DelWrapper(wrapper);
|
DelWrapper(wrapper);
|
||||||
|
|
||||||
return SWIG_OK;
|
return SWIG_OK;
|
||||||
|
|
@ -2052,3 +2060,10 @@ Template& Template::pretty_print(DOH *doh) {
|
||||||
Wrapper_pretty_print(str(), doh);
|
Wrapper_pretty_print(str(), doh);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Template::operator=(const Template& t) {
|
||||||
|
Delete(code);
|
||||||
|
Delete(templateName);
|
||||||
|
code = NewString(t.code);
|
||||||
|
templateName = NewString(t.templateName);
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue