Add initial support for argument marshalling using typemaps to v8 module.

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/oliverb-javascript-v8@13755 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Oliver Buchtala 2012-09-08 00:50:41 +00:00
commit 65d4769af8
4 changed files with 246 additions and 211 deletions

View file

@ -1,4 +1,3 @@
%fragment("v8_initializer", "templates") %{ %fragment("v8_initializer", "templates") %{
void ${MODULE}_Initialize(v8::Handle<v8::Context> context) void ${MODULE}_Initialize(v8::Handle<v8::Context> context)
{ {
@ -41,43 +40,34 @@ v8::Handle<v8::Value> ${NAME_MANGLED}_new(const v8::Arguments& args) {
v8::HandleScope scope; v8::HandleScope scope;
v8::Handle<v8::Object> self = args.Holder(); v8::Handle<v8::Object> self = args.Holder();
${LOCALS} ${LOCALS}
${MARSHAL_INPUT} ${CODE}
${ACTION} self->SetInternalField(0, v8::External::New(result));
self->SetInternalField(0, v8::External::New(ptr));
return self; return self;
} }%}
%}
%fragment("v8_getter", "templates") %{ %fragment("v8_getter", "templates") %{
v8::Handle<v8::Value> ${NAME_MANGLED}_get(v8::Local<v8::String> property, const v8::AccessorInfo& info) { v8::Handle<v8::Value> ${NAME_MANGLED}_get(v8::Local<v8::String> property, const v8::AccessorInfo& info) {
v8::HandleScope scope; v8::HandleScope scope;
v8::Handle<v8::Object> ret; v8::Handle<v8::Object> jsresult;
${LOCALS} ${LOCALS}
${ACTION} ${CODE}
${MARSHAL_OUTPUT} return scope.Close(jsresult);
return scope.Close(ret); }%}
}
%}
%fragment("v8_setter", "templates") %{ %fragment("v8_setter", "templates") %{
void ${NAME_MANGLED}_set(v8::Local<v8::String> property, v8::Local<v8::Value> value, const v8::AccessorInfo& info) { void ${NAME_MANGLED}_set(v8::Local<v8::String> property, v8::Local<v8::Value> value, const v8::AccessorInfo& info) {
${LOCALS} ${LOCALS}
${MARSHAL_INPUT} ${CODE}
${ACTION} }%}
}
%}
%fragment("v8_function", "templates") %{ %fragment("v8_function", "templates") %{
v8::Handle<v8::Value> wrap_${NAME_MANGLED}(const Arguments &args) { v8::Handle<v8::Value> wrap_${NAME_MANGLED}(const Arguments &args) {
v8::HandleScope scope; v8::HandleScope scope;
v8::Handle<v8::Object> ret; v8::Handle<v8::Object> jsresult;
${LOCALS} ${LOCALS}
${MARSHAL_INPUT} ${CODE}
${ACTION} return scope.Close(jsresult);
${MARSHAL_OUTPUT} }%}
return scope.Close(ret);
}
%}
%fragment("v8_create_namespace", "templates") %{ %fragment("v8_create_namespace", "templates") %{
v8::Handle<v8::ObjectTemplate> ${NAME_MANGLED} = v8::ObjectTemplate::New();%} v8::Handle<v8::ObjectTemplate> ${NAME_MANGLED} = v8::ObjectTemplate::New();%}
@ -96,3 +86,7 @@ SWIGV8_AddGlobalVariable(${CONTEXT}, "${NAME_UNQUALIFIED}", ${GETTER}, ${SETTER}
%fragment("v8_register_namespace", "templates") %{ %fragment("v8_register_namespace", "templates") %{
${CONTEXT}->Set(v8::String::NewSymbol("${NAME_UNQUALIFIED}", ${NAME_MANGLED}->NewInstance()));%} ${CONTEXT}->Set(v8::String::NewSymbol("${NAME_UNQUALIFIED}", ${NAME_MANGLED}->NewInstance()));%}
%fragment("v8_this_ptr", "templates") %{
arg1 = SWIGV8_UnwrapThisPointer<${TYPE}>(${ARG}.Holder());
%}

View file

@ -1,18 +1,25 @@
%typemap(in) bool
%{ $1 = ($1_ltype) $input->BooleanValue();%}
// Primitive types // Primitive types
%typemap(in) char, %typemap(in) char,
signed char, signed char,
unsigned char, unsigned char,
short, short,
unsigned short, unsigned short,
int, int
unsigned int, %{ $1 = ($1_ltype) $input->Int32Value();%}
%typemap(in) unsigned int,
long, long,
unsigned long, unsigned long,
long long, long long,
unsigned long long, unsigned long long
float, %{ $1 = ($1_ltype) $input->UInt32Value();%}
double
%{ $1 = ($1_ltype)JSValueToNumber(context, $input, NULL); %} %typemap(in) float, double
%{ $1 = ($1_ltype) $input->NumberValue();%}
%typemap(in) const bool &, bool &, %typemap(in) const bool &, bool &,
const char &, char &, const char &, char &,
@ -25,25 +32,33 @@
const long &, long &, const long &, long &,
const unsigned long &, unsigned long &, const unsigned long &, unsigned long &,
const long long &, long long &, const long long &, long long &,
const unsigned long long &,unsigned long long &, const unsigned long long &, unsigned long long &,
const float &, float &, const float &, float &,
const double &, double & const double &, double &
%{ $1 = ($1_ltype)&$input; %} %{
// TODO: typemap(in) const bool& at al
}
%typemap(out) bool
%{ $result = v8::Boolean::New($1);%}
%typemap(out) char, %typemap(out) char,
signed char, signed char,
unsigned char, unsigned char,
short, short,
unsigned short, unsigned short,
int, int
unsigned int, %{ $result = v8::Int32::New($1);%}
%typemap(out) unsigned int,
long, long,
unsigned long, unsigned long,
long long, long long,
unsigned long long, unsigned long long
float, %{ $result = v8::UInt32::New($1);%}
double
%{ $result = JSValueMakeNumber(context, $1); %} %typemap(out) float, double
%{ $result = v8::Number::New($1); %}
%typemap(out) const bool &, bool &, %typemap(out) const bool &, bool &,
const char &, char &, const char &, char &,
@ -51,16 +66,19 @@
const unsigned char &, unsigned char &, const unsigned char &, unsigned char &,
const short &, short &, const short &, short &,
const unsigned short &, unsigned short &, const unsigned short &, unsigned short &,
const int &, int &, const int &, int &
const unsigned int &, unsigned int &, %{ $result = v8::Int32::New((*$1);%}
%typemap(out) const unsigned int &, unsigned int &,
const long &, long &, const long &, long &,
const unsigned long &, unsigned long &, const unsigned long &, unsigned long &,
const long long &, long long &, const long long &, long long &,
const unsigned long long &,unsigned long long &, const unsigned long long &, unsigned long long &
const float &, float &, %{ $result = v8::UInt32::New(*$1);%}
const double &, double &
%{ $result = JSValueMakeNumber(context,*$1); %}
%typemap(out) const float &, float &,
const double &, double &
%{ $result = v8::Number::New(*$1);%}
%typemap(in) short *, %typemap(in) short *,
unsigned short *, unsigned short *,
@ -73,9 +91,7 @@
float *, float *,
double * double *
%{ %{
JSObjectRef o$1 = JSValueToObject(context,$input, NULL); // TODO: typemap(in): short* et al.
SWIG_PRV_DATA *$1_privatedata = (SWIG_PRV_DATA *)JSObjectGetPrivate(o$1);
$1 = ($1_ltype)$1_privatedata->swigCObject;
%} %}
@ -90,169 +106,88 @@
float *, float *,
double * double *
%{ %{
SWIG_PRV_DATA *privatedata = new SWIG_PRV_DATA(); // TODO: typemap(out) short* et al.
privatedata->swigCMemOwn = false;
privatedata->swigCObject = result;
$result = JSObjectMake(context, _wrap_swig_ptr_$*1_ltype_createJSClass(context), privatedata);
%}
%typemap(in) bool
%{
$1 = ($1_ltype)JSValueToBoolean(context, $input);
%}
%typemap(out) bool
%{
$result = JSValueMakeBoolean(context, $1);
%} %}
%typemap(out) void %typemap(out) void
%{ $result = JSValueMakeUndefined(context); %} %{ $result = v8::Undefined(); %}
%typemap(in) char * %typemap(in) char *
%{ %{
JSStringRef $1_str = JSValueToStringCopy(context, $input, NULL); // TODO: input typemap for char*
size_t $1_strsize = JSStringGetMaximumUTF8CStringSize($1_str);
$1 = (char *)malloc($1_strsize * sizeof(char));
JSStringGetUTF8CString($1_str, $1, $1_strsize);
%} %}
%typemap(out) char * %typemap(out) char *
%{ %{
JSStringRef jsstring = JSStringCreateWithUTF8CString($1); // TODO: output typemap for char*
$result = JSValueMakeString(context, jsstring);
JSStringRelease(jsstring);
%} %}
%typemap(arginit) char * ""
%typemap(in) char *& ($*1_ltype temp = 0) %{ %typemap(in) char *& ($*1_ltype temp = 0) %{
temp = ($*1_ltype)$input; // TODO: input typemap for char*&
JSStringRef $1_str = JSValueToStringCopy(context, $input, NULL);
size_t $1_strsize = JSStringGetMaximumUTF8CStringSize($1_str);
$1 = (char *)malloc($1_strsize * sizeof(char));
JSStringGetUTF8CString($1_str, $1, $1_strsize);
%} %}
%typemap(out) char *& %typemap(out) char *&
%{ %{
JSStringRef jsstring = JSStringCreateWithUTF8CString((const char *)*$1); // TODO: output typemap for char*&
$result = JSValueMakeString(context, jsstring);
JSStringRelease(jsstring);
%} %}
/* char arrays - treat as String */ /* char arrays - treat as String */
%typemap(in) char[ANY], char[] %{ %typemap(in) char[ANY], char[] %{
JSStringRef $1_str = JSValueToStringCopy(context, $input, NULL); // TODO: input typemap for char[]
size_t $1_strsize = JSStringGetMaximumUTF8CStringSize($1_str);
JSStringGetUTF8CString($1_str, $1, $1_strsize);
%} %}
%typemap(out) char[ANY], char[] %typemap(out) char[ANY], char[]
%{ %{
JSStringRef jsstring = JSStringCreateWithUTF8CString($1); // TODO: output typemap for char[]
$result = JSValueMakeString(context, jsstring);
JSStringRelease(jsstring);
%} %}
%typemap(freearg) char *, char *&, char[ANY], char[] //TODO: Not working: A memory leak %typemap(freearg) char *, char *&, char[ANY], char[] //TODO: Not working: A memory leak
%{ free($1); %} %{
// TODO: freearg char* et al
%}
/* Typemaps for composite types */ /* Typemaps for composite types */
%typemap(in) SWIGTYPE ($&1_type argp) // Objects passed by value, convert to a pointer %typemap(in) SWIGTYPE ($&1_type argp) // Objects passed by value, convert to a pointer
%{ %{
JSObjectRef o$1 = JSValueToObject(context,$input, NULL); // TODO: input typemap for composite types
SWIG_PRV_DATA *$1_privatedata = (SWIG_PRV_DATA *)JSObjectGetPrivate(o$1);
argp = ($&1_ltype)$1_privatedata->swigCObject;
$1 = *argp;
%} %}
%typemap(out) SWIGTYPE ($&1_type temp) %typemap(out) SWIGTYPE ($&1_type temp)
#ifdef __cplusplus
%{ %{
temp = new $1_ltype((const $1_ltype &)$1); // TODO: output typemap for composite types
SWIG_PRV_DATA *privatedata = new SWIG_PRV_DATA();
privatedata->swigCMemOwn = false;
privatedata->swigCObject = temp;
$result = JSObjectMake(context, _wrap_$1_type_createJSClass(context), privatedata);
//$result = JSObjectMake(context, _wrap_$1_basetype_createJSClass(context), privatedata);
//$result = JSObjectMake(context, _wrap_$objecttype_createJSClass(context), privatedata);
// $1_mangle
// $1_descriptor
%} %}
#else
{
$&1_ltype $1ptr = ($&1_ltype) malloc(sizeof($1_ltype));
memmove($1ptr, &$1, sizeof($1_type));
temp = $1ptr;
SWIG_PRV_DATA *privatedata = (SWIG_PRV_DATA *)malloc(sizeof(SWIG_PRV_DATA());
privatedata->swigCMemOwn = false;
privatedata->swigCObject = temp;
$result = JSObjectMake(context, _wrap_$1_ltype_createJSClass(context), privatedata);
//$result = JSObjectMake(context, _wrap_$1_basetype_createJSClass(context), privatedata);
//$result = JSObjectMake(context, _wrap_$objecttype_createJSClass(context), privatedata);
// $1_mangle
// $1_descriptor
}
#endif
%typemap(in) SWIGTYPE *, SWIGTYPE & %typemap(in) SWIGTYPE *, SWIGTYPE &
%{ %{
JSObjectRef o$1 = JSValueToObject(context,$input, NULL); // TODO: input typemap for ptr types
SWIG_PRV_DATA *$1_privatedata = (SWIG_PRV_DATA *)JSObjectGetPrivate(o$1);
$1 = ($1_ltype)$1_privatedata->swigCObject;
%} %}
%typemap(out) SWIGTYPE *, SWIGTYPE & %typemap(out) SWIGTYPE *, SWIGTYPE &
%{ %{
SWIG_PRV_DATA *privatedata = new SWIG_PRV_DATA(); // TODO: output typemap for ptr types
privatedata->swigCMemOwn = false;
privatedata->swigCObject = result;
$result = JSObjectMake(context, _wrap_$*1_ltype_createJSClass(context), privatedata);
//$result = JSObjectMake(context, _wrap_$1_basetype_createJSClass(context), privatedata);
//$result = JSObjectMake(context, _wrap_$objecttype_createJSClass(context), privatedata);
// $1_mangle
// $1_descriptor
%} %}
// TODO: sanity check?
%typemap(arginit) SWIGTYPE * %typemap(arginit) SWIGTYPE *
%{ %{%}
// Sanity check if the call is not on the global object
if (!JSValueIsEqual(context,
JSValueToObject(context,thisObject,NULL),
JSValueToObject(context,JSContextGetGlobalObject(context), NULL),
NULL)) {
SWIG_PRV_DATA* $1_swigprivatedata = (SWIG_PRV_DATA*)JSObjectGetPrivate(thisObject);
$1 = ($1_ltype)$1_swigprivatedata->swigCObject;
}
%}
%typemap(in) SWIGTYPE (CLASS::*) "" %typemap(in) SWIGTYPE (CLASS::*) ""
%typemap(out) SWIGTYPE (CLASS::*) %typemap(out) SWIGTYPE (CLASS::*)
%{ %{
// Class* out typemap // TODO: output typemap for CLASS::*
$result = JSObjectMake(context, _wrap_$*1_ltype_createJSClass(context), result);
%} %}
/* Typecheck typemaps - The purpose of these is merely to issue a warning for overloaded C++ functions
* that cannot be overloaded in Javascript as more than one C++ type maps to a single Javascript type */
// TODO
// Default array handling // Default array handling
%typemap(in) SWIGTYPE [] %{ $1 = ($1_ltype)$input; %} %typemap(in) SWIGTYPE [] %{
// TODO: typemap for arrays;
%}
%typemap(out) SWIGTYPE [] %{ $result = $1; %} %typemap(out) SWIGTYPE [] %{ $result = $1; %}
// Javascript specific directives
//TODO
// Some ANSI C typemaps */ // Some ANSI C typemaps */
%apply unsigned long { size_t }; %apply unsigned long { size_t };

View file

@ -22,6 +22,8 @@
#define V8_REGISTER_GLOBAL_VARIABLE "v8_register_global_variable" #define V8_REGISTER_GLOBAL_VARIABLE "v8_register_global_variable"
#define V8_CREATE_NAMESPACE "v8_create_namespace" #define V8_CREATE_NAMESPACE "v8_create_namespace"
#define V8_REGISTER_NAMESPACE "v8_register_namespace" #define V8_REGISTER_NAMESPACE "v8_register_namespace"
#define V8_THIS_PTR "v8_this_ptr"
// keywords used in templates // keywords used in templates
#define KW_MODULE_NAME "${MODULE}" #define KW_MODULE_NAME "${MODULE}"
@ -30,6 +32,8 @@
#define KW_CLASSNAME_MANGLED "${CLASSNAME_MANGLED}" #define KW_CLASSNAME_MANGLED "${CLASSNAME_MANGLED}"
#define KW_BASE_CLASS "${BASE_CLASS}" #define KW_BASE_CLASS "${BASE_CLASS}"
#define KW_CONTEXT "${CONTEXT}" #define KW_CONTEXT "${CONTEXT}"
#define KW_TYPE "${TYPE}"
#define KW_ARG "${ARG}"
#define KW_WRAPPER "${WRAPPER}" #define KW_WRAPPER "${WRAPPER}"
#define KW_GETTER "${GETTER}" #define KW_GETTER "${GETTER}"
#define KW_SETTER "${SETTER}" #define KW_SETTER "${SETTER}"
@ -42,6 +46,7 @@
#define KW_REGISTER_NS "${PART_REGISTER_NS}" #define KW_REGISTER_NS "${PART_REGISTER_NS}"
#define KW_LOCALS "${LOCALS}" #define KW_LOCALS "${LOCALS}"
#define KW_CODE "${CODE}"
#define KW_MARSHAL_INPUT "${MARSHAL_INPUT}" #define KW_MARSHAL_INPUT "${MARSHAL_INPUT}"
#define KW_ACTION "${ACTION}" #define KW_ACTION "${ACTION}"
#define KW_MARSHAL_OUTPUT "${MARSHAL_OUTPUT}" #define KW_MARSHAL_OUTPUT "${MARSHAL_OUTPUT}"
@ -196,6 +201,7 @@ int V8Emitter::EnterClass(Node *n)
{ {
current_classname_mangled = Swig_string_mangle(Getattr(n, "name")); current_classname_mangled = Swig_string_mangle(Getattr(n, "name"));
current_classname_unqualified = Swig_scopename_last(Getattr(n, "name")); current_classname_unqualified = Swig_scopename_last(Getattr(n, "name"));
current_class_type = Getattr(n, "classtype");
// emit declaration of a v8 class template // emit declaration of a v8 class template
Template t_decl_class(GetTemplate(V8_DECL_CLASSTEMPLATE)); Template t_decl_class(GetTemplate(V8_DECL_CLASSTEMPLATE));
@ -233,6 +239,7 @@ int V8Emitter::ExitClass(Node *n)
Delete(current_classname_unqualified); Delete(current_classname_unqualified);
current_classname_mangled = 0; current_classname_mangled = 0;
current_classname_unqualified = 0; current_classname_unqualified = 0;
current_class_type = 0;
return SWIG_OK; return SWIG_OK;
} }
@ -322,23 +329,19 @@ int V8Emitter::ExitFunction(Node* n)
int V8Emitter::EmitCtor(Node* n) int V8Emitter::EmitCtor(Node* n)
{ {
// TODO: // TODO: handle overloaded ctors using a dispatcher
// - handle overloaded ctors using a dispatcher
// - marshal inputs
Template t(GetTemplate(V8_CTOR_WRAPPER)); Template t(GetTemplate(V8_CTOR_WRAPPER));
ParmList *params = Getattr(n,"parms"); //HACK: manually add declaration of instance pointer
emit_parameter_variables(params, current_wrapper); Printf(current_wrapper->locals, "%sresult;", SwigType_str(Getattr(n, "type"),0));
emit_attach_parmmaps(params, current_wrapper);
String* action = Getattr(n, "wrap:action"); String* action = emit_action(n);
String* input = NewString(""); Printv(current_wrapper->code, action, 0);
t.Replace(KW_MANGLED_NAME, current_classname_mangled) t.Replace(KW_MANGLED_NAME, current_classname_mangled)
.Replace(KW_UNQUALIFIED_NAME, current_classname_unqualified) .Replace(KW_UNQUALIFIED_NAME, current_classname_unqualified)
.Replace(KW_LOCALS, current_wrapper->locals) .Replace(KW_LOCALS, current_wrapper->locals)
.Replace(KW_ACTION, action) .Replace(KW_CODE, current_wrapper->code);
.Replace(KW_MARSHAL_INPUT, input);
Wrapper_pretty_print(t.str(), f_wrapper); Wrapper_pretty_print(t.str(), f_wrapper);
@ -360,21 +363,21 @@ int V8Emitter::EmitGetter(Node *n, bool is_member) {
current_getter = Getattr(n,"wrap:name"); current_getter = Getattr(n,"wrap:name");
Printf(current_wrapper->locals, "%s result;\n", SwigType_str(Getattr(n, "type"), 0)); ParmList *params = Getattr(n,"parms");
emit_parameter_variables(params, current_wrapper);
emit_attach_parmmaps(params, current_wrapper);
int num_args = emit_num_arguments(params);
String* action = emit_action(n); String* action = emit_action(n);
String* output = NewString("// TODO: marshal output.\n ret = v8::Undefined();"); marshalInputArgs(n, params, num_args, current_wrapper);
marshalOutput(n, action, current_wrapper);
t_getter.Replace(KW_MANGLED_NAME, current_variable_mangled) t_getter.Replace(KW_MANGLED_NAME, current_variable_mangled)
.Replace(KW_LOCALS, current_wrapper->locals) .Replace(KW_LOCALS, current_wrapper->locals)
.Replace(KW_ACTION, action) .Replace(KW_CODE, current_wrapper->code);
.Replace(KW_MARSHAL_OUTPUT, output);
Wrapper_pretty_print(t_getter.str(), f_wrapper); Wrapper_pretty_print(t_getter.str(), f_wrapper);
// clean up
Delete(output);
return SWIG_OK; return SWIG_OK;
} }
@ -388,19 +391,17 @@ int V8Emitter::EmitSetter(Node* n, bool is_member)
emit_parameter_variables(params, current_wrapper); emit_parameter_variables(params, current_wrapper);
emit_attach_parmmaps(params, current_wrapper); emit_attach_parmmaps(params, current_wrapper);
int num_args = emit_num_arguments(params);
String* action = emit_action(n); String* action = emit_action(n);
String* input = NewString("// TODO: marshal input.\n"); marshalInputArgs(n, params, num_args, current_wrapper);
Printv(current_wrapper->code, action, 0);
t_setter.Replace(KW_MANGLED_NAME, current_variable_mangled) t_setter.Replace(KW_MANGLED_NAME, current_variable_mangled)
.Replace(KW_LOCALS, current_wrapper->locals) .Replace(KW_LOCALS, current_wrapper->locals)
.Replace(KW_ACTION, action) .Replace(KW_CODE, current_wrapper->code);
.Replace(KW_MARSHAL_INPUT, input);
Wrapper_pretty_print(t_setter.str(), f_wrapper); Wrapper_pretty_print(t_setter.str(), f_wrapper);
// clean up
Delete(input);
return SWIG_OK; return SWIG_OK;
} }
@ -416,26 +417,124 @@ int V8Emitter::EmitFunction(Node* n, bool is_member)
ParmList *params = Getattr(n,"parms"); ParmList *params = Getattr(n,"parms");
emit_parameter_variables(params, current_wrapper); emit_parameter_variables(params, current_wrapper);
emit_attach_parmmaps(params, current_wrapper); emit_attach_parmmaps(params, current_wrapper);
Printf(current_wrapper->locals, "%s result;\n", SwigType_str(Getattr(n, "type"), 0));
String* input = NewString("// TODO: marshal input"); int num_args = emit_num_arguments(params);
String* action = emit_action(n); String* action = emit_action(n);
String* output = NewString("// TODO: marshal output.\n ret = v8::Undefined();"); marshalInputArgs(n, params, num_args, current_wrapper);
marshalOutput(n, action, current_wrapper);
t_function.Replace(KW_MANGLED_NAME, current_function_mangled) t_function.Replace(KW_MANGLED_NAME, current_function_mangled)
.Replace(KW_LOCALS, current_wrapper->locals) .Replace(KW_LOCALS, current_wrapper->locals)
.Replace(KW_ACTION, action) .Replace(KW_CODE, current_wrapper->code);
.Replace(KW_MARSHAL_INPUT, input)
.Replace(KW_MARSHAL_OUTPUT, output);
Wrapper_pretty_print(t_function.str(), f_wrapper); Wrapper_pretty_print(t_function.str(), f_wrapper);
// clean up
Delete(input);
Delete(output);
return SWIG_OK; return SWIG_OK;
} }
void V8Emitter::marshalInputArgs(Node *n, ParmList *parms, int numarg, Wrapper *wrapper) {
String *tm;
Parm *p;
bool is_member = (current_class_type != 0);
bool is_setter = IsSetterMethod(n);
bool is_function = (current_function_mangled != 0);
int start_idx;
if(is_member) {
start_idx = 1;
} else {
start_idx = 0;
}
// retrieve this pointer for member functions
if(is_member) {
Template t_selfptr(GetTemplate(V8_THIS_PTR));
String *type_str = SwigType_strip_qualifiers(SwigType_str(current_class_type,0));
String *arg_str;
if(is_function) {
arg_str = NewString("args");
} else {
arg_str = NewString("info");
}
t_selfptr.Replace(KW_TYPE, type_str)
.Replace(KW_ARG, arg_str);
Printv(wrapper->code, t_selfptr.str(), 0);
Delete(type_str);
Delete(arg_str);
}
int i = 0;
for (i = 0, p = parms; i < numarg; i++)
{
p = skipIgnoredArgs(p);
SwigType *pt = Getattr(p, "type");
String *arg = NewString("");
if (i == 0) {
if(start_idx == 0) {
Printv(arg, is_setter?"value":"args[0]", 0);
} else {
p = Getattr(p, "tmap:in:next");
Delete(arg);
continue; // special case: skip the typemaps for the first argument
}
} else {
Printf(arg, is_setter?"value":"args[%d]", i - start_idx);
}
if ((tm = Getattr(p, "tmap:in"))) // Get typemap for this argument
{
Replaceall(tm, "$input", arg);
Setattr(p, "emit:input", arg);
Printf(wrapper->code, "%s\n", tm);
p = Getattr(p, "tmap:in:next");
} else {
Swig_warning(WARN_TYPEMAP_IN_UNDEF, input_file, line_number, "Unable to use type %s as a function argument.\n", SwigType_str(pt, 0));
p = nextSibling(p);
}
Delete(arg);
}
}
/* ---------------------------------------------------------------------
* marshalOutput()
*
* Process the return value of the C/C++ function call
* and convert them into the Javascript types using the
* supplied typemaps.
* --------------------------------------------------------------------- */
void V8Emitter::marshalOutput(Node *n, String *actioncode, Wrapper *wrapper) {
SwigType *type = Getattr(n, "type");
Setattr(n, "type", type);
String *tm;
if ((tm = Swig_typemap_lookup_out("out", n, "result", wrapper, actioncode)))
{
Replaceall(tm, "$result", "jsresult");
// TODO: May not be the correct way
Replaceall(tm, "$objecttype", Swig_scopename_last(SwigType_str(SwigType_strip_qualifiers(type), 0)));
Printf(wrapper->code, "%s", tm);
if (Len(tm))
Printf(wrapper->code, "\n");
} else {
Swig_warning(WARN_TYPEMAP_OUT_UNDEF, input_file, line_number, "Unable to use return type %s in function %s.\n", SwigType_str(type, 0), Getattr(n, "name"));
}
emit_return_variable(n, type, wrapper);
}
Parm* V8Emitter::skipIgnoredArgs(Parm *p) {
while (checkAttribute(p, "tmap:in:numinputs", "0")) {
p = Getattr(p, "tmap:in:next");
}
return p;
}
JSEmitter* create_v8_emitter() JSEmitter* create_v8_emitter()
{ {
return new V8Emitter(); return new V8Emitter();

View file

@ -45,6 +45,12 @@ protected:
virtual int EmitSetter(Node *n, bool is_member); virtual int EmitSetter(Node *n, bool is_member);
void marshalInputArgs(Node *n, ParmList *parms, int numarg, Wrapper *wrapper);
void marshalOutput(Node *n, String *actioncode, Wrapper *wrapper);
Parm *skipIgnoredArgs(Parm *p);
private: private:
File *f_runtime; File *f_runtime;
@ -64,6 +70,7 @@ private:
// state variables // state variables
String* current_context; String* current_context;
String* current_class_type;
String* current_classname_mangled; String* current_classname_mangled;
String* current_classname_unqualified; String* current_classname_unqualified;
String* current_variable_mangled; String* current_variable_mangled;