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") %{
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::Handle<v8::Object> self = args.Holder();
${LOCALS}
${MARSHAL_INPUT}
${ACTION}
self->SetInternalField(0, v8::External::New(ptr));
${CODE}
self->SetInternalField(0, v8::External::New(result));
return self;
}
%}
}%}
%fragment("v8_getter", "templates") %{
v8::Handle<v8::Value> ${NAME_MANGLED}_get(v8::Local<v8::String> property, const v8::AccessorInfo& info) {
v8::HandleScope scope;
v8::Handle<v8::Object> ret;
v8::Handle<v8::Object> jsresult;
${LOCALS}
${ACTION}
${MARSHAL_OUTPUT}
return scope.Close(ret);
}
%}
${CODE}
return scope.Close(jsresult);
}%}
%fragment("v8_setter", "templates") %{
void ${NAME_MANGLED}_set(v8::Local<v8::String> property, v8::Local<v8::Value> value, const v8::AccessorInfo& info) {
${LOCALS}
${MARSHAL_INPUT}
${ACTION}
}
%}
${CODE}
}%}
%fragment("v8_function", "templates") %{
v8::Handle<v8::Value> wrap_${NAME_MANGLED}(const Arguments &args) {
v8::HandleScope scope;
v8::Handle<v8::Object> ret;
v8::Handle<v8::Object> jsresult;
${LOCALS}
${MARSHAL_INPUT}
${ACTION}
${MARSHAL_OUTPUT}
return scope.Close(ret);
}
%}
${CODE}
return scope.Close(jsresult);
}%}
%fragment("v8_create_namespace", "templates") %{
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") %{
${CONTEXT}->Set(v8::String::NewSymbol("${NAME_UNQUALIFIED}", ${NAME_MANGLED}->NewInstance()));%}
%fragment("v8_this_ptr", "templates") %{
arg1 = SWIGV8_UnwrapThisPointer<${TYPE}>(${ARG}.Holder());
%}

View file

@ -1,66 +1,84 @@
%typemap(in) bool
%{ $1 = ($1_ltype) $input->BooleanValue();%}
// Primitive types
%typemap(in) char,
signed char,
unsigned char,
short,
short,
unsigned short,
int,
unsigned int,
int
%{ $1 = ($1_ltype) $input->Int32Value();%}
%typemap(in) unsigned int,
long,
unsigned long,
long long,
unsigned long long,
float,
double
%{ $1 = ($1_ltype)JSValueToNumber(context, $input, NULL); %}
unsigned long long
%{ $1 = ($1_ltype) $input->UInt32Value();%}
%typemap(in) const bool &, bool &,
const char &, char &,
const signed char &, signed char &,
const unsigned char &, unsigned char &,
const short &, short &,
const unsigned short &, unsigned short &,
const int &, int &,
const unsigned int &, unsigned int &,
const long &, long &,
const unsigned long &, unsigned long &,
const long long &, long long &,
const unsigned long long &,unsigned long long &,
const float &, float &,
const double &, double &
%{ $1 = ($1_ltype)&$input; %}
%typemap(in) float, double
%{ $1 = ($1_ltype) $input->NumberValue();%}
%typemap(in) const bool &, bool &,
const char &, char &,
const signed char &, signed char &,
const unsigned char &, unsigned char &,
const short &, short &,
const unsigned short &, unsigned short &,
const int &, int &,
const unsigned int &, unsigned int &,
const long &, long &,
const unsigned long &, unsigned long &,
const long long &, long long &,
const unsigned long long &, unsigned long long &,
const float &, float &,
const double &, double &
%{
// TODO: typemap(in) const bool& at al
}
%typemap(out) bool
%{ $result = v8::Boolean::New($1);%}
%typemap(out) char,
signed char,
unsigned char,
short,
short,
unsigned short,
int,
unsigned int,
int
%{ $result = v8::Int32::New($1);%}
%typemap(out) unsigned int,
long,
unsigned long,
long long,
unsigned long long,
float,
double
%{ $result = JSValueMakeNumber(context, $1); %}
long long,
unsigned long long
%{ $result = v8::UInt32::New($1);%}
%typemap(out) const bool &, bool &,
const char &, char &,
const signed char &, signed char &,
const unsigned char &, unsigned char &,
const short &, short &,
const unsigned short &, unsigned short &,
const int &, int &,
const unsigned int &, unsigned int &,
const long &, long &,
const unsigned long &, unsigned long &,
const long long &, long long &,
const unsigned long long &,unsigned long long &,
const float &, float &,
const double &, double &
%{ $result = JSValueMakeNumber(context,*$1); %}
%typemap(out) float, double
%{ $result = v8::Number::New($1); %}
%typemap(out) const bool &, bool &,
const char &, char &,
const signed char &, signed char &,
const unsigned char &, unsigned char &,
const short &, short &,
const unsigned short &, unsigned short &,
const int &, int &
%{ $result = v8::Int32::New((*$1);%}
%typemap(out) const unsigned int &, unsigned int &,
const long &, long &,
const unsigned long &, unsigned long &,
const long long &, long long &,
const unsigned long long &, unsigned long long &
%{ $result = v8::UInt32::New(*$1);%}
%typemap(out) const float &, float &,
const double &, double &
%{ $result = v8::Number::New(*$1);%}
%typemap(in) short *,
unsigned short *,
@ -73,9 +91,7 @@
float *,
double *
%{
JSObjectRef o$1 = JSValueToObject(context,$input, NULL);
SWIG_PRV_DATA *$1_privatedata = (SWIG_PRV_DATA *)JSObjectGetPrivate(o$1);
$1 = ($1_ltype)$1_privatedata->swigCObject;
// TODO: typemap(in): short* et al.
%}
@ -90,169 +106,88 @@
float *,
double *
%{
SWIG_PRV_DATA *privatedata = new SWIG_PRV_DATA();
privatedata->swigCMemOwn = false;
privatedata->swigCObject = result;
$result = JSObjectMake(context, _wrap_swig_ptr_$*1_ltype_createJSClass(context), privatedata);
// TODO: typemap(out) short* et al.
%}
%typemap(in) bool
%{
$1 = ($1_ltype)JSValueToBoolean(context, $input);
%}
%typemap(out) bool
%{
$result = JSValueMakeBoolean(context, $1);
%}
%typemap(out) void
%{ $result = JSValueMakeUndefined(context); %}
%{ $result = v8::Undefined(); %}
%typemap(in) 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);
// TODO: input typemap for char*
%}
%typemap(out) char *
%{
JSStringRef jsstring = JSStringCreateWithUTF8CString($1);
$result = JSValueMakeString(context, jsstring);
JSStringRelease(jsstring);
// TODO: output typemap for char*
%}
%typemap(arginit) char * ""
%typemap(in) char *& ($*1_ltype temp = 0) %{
temp = ($*1_ltype)$input;
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);
// TODO: input typemap for char*&
%}
%typemap(out) char *&
%{
JSStringRef jsstring = JSStringCreateWithUTF8CString((const char *)*$1);
$result = JSValueMakeString(context, jsstring);
JSStringRelease(jsstring);
// TODO: output typemap for char*&
%}
/* char arrays - treat as String */
%typemap(in) char[ANY], char[] %{
JSStringRef $1_str = JSValueToStringCopy(context, $input, NULL);
size_t $1_strsize = JSStringGetMaximumUTF8CStringSize($1_str);
JSStringGetUTF8CString($1_str, $1, $1_strsize);
// TODO: input typemap for char[]
%}
%typemap(out) char[ANY], char[]
%{
JSStringRef jsstring = JSStringCreateWithUTF8CString($1);
$result = JSValueMakeString(context, jsstring);
JSStringRelease(jsstring);
// TODO: output typemap for char[]
%}
%typemap(freearg) char *, char *&, char[ANY], char[] //TODO: Not working: A memory leak
%{ free($1); %}
%{
// TODO: freearg char* et al
%}
/* Typemaps for composite types */
%typemap(in) SWIGTYPE ($&1_type argp) // Objects passed by value, convert to a pointer
%{
JSObjectRef o$1 = JSValueToObject(context,$input, NULL);
SWIG_PRV_DATA *$1_privatedata = (SWIG_PRV_DATA *)JSObjectGetPrivate(o$1);
argp = ($&1_ltype)$1_privatedata->swigCObject;
$1 = *argp;
// TODO: input typemap for composite types
%}
%typemap(out) SWIGTYPE ($&1_type temp)
#ifdef __cplusplus
%{
temp = new $1_ltype((const $1_ltype &)$1);
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
// TODO: output typemap for composite types
%}
#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 &
%{
JSObjectRef o$1 = JSValueToObject(context,$input, NULL);
SWIG_PRV_DATA *$1_privatedata = (SWIG_PRV_DATA *)JSObjectGetPrivate(o$1);
$1 = ($1_ltype)$1_privatedata->swigCObject;
// TODO: input typemap for ptr types
%}
%typemap(out) SWIGTYPE *, SWIGTYPE &
%{
SWIG_PRV_DATA *privatedata = new SWIG_PRV_DATA();
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: output typemap for ptr types
%}
// TODO: sanity check?
%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(out) SWIGTYPE (CLASS::*)
%{
// Class* out typemap
$result = JSObjectMake(context, _wrap_$*1_ltype_createJSClass(context), result);
// TODO: output typemap for CLASS::*
%}
/* 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
%typemap(in) SWIGTYPE [] %{ $1 = ($1_ltype)$input; %}
%typemap(in) SWIGTYPE [] %{
// TODO: typemap for arrays;
%}
%typemap(out) SWIGTYPE [] %{ $result = $1; %}
// Javascript specific directives
//TODO
// Some ANSI C typemaps */
%apply unsigned long { size_t };