Conflicts:

.gitignore
	.project
	.travis.yml
	Examples/Makefile.in
	Examples/test-suite/director_nspace.i
This commit is contained in:
Oliver Buchtala 2013-09-27 02:29:02 +02:00
commit 6dcbbf07db
134 changed files with 10011 additions and 15 deletions

View file

@ -0,0 +1,123 @@
/* -----------------------------------------------------------------------------
* arrays_javascript.i
*
* These typemaps give more natural support for arrays. The typemaps are not efficient
* as there is a lot of copying of the array values whenever the array is passed to C/C++
* from JavaScript and vice versa. The JavaScript array is expected to be the same size as the C array.
* An exception is thrown if they are not.
*
* Example usage:
* Wrapping:
*
* %include <arrays_javascript.i>
* %inline %{
* extern int FiddleSticks[3];
* %}
*
* Use from JavaScript like this:
*
* var fs = [10, 11, 12];
* example.FiddleSticks = fs;
* fs = example.FiddleSticks;
* ----------------------------------------------------------------------------- */
%fragment("SWIG_JSCGetIntProperty", "header", fragment=SWIG_AsVal_frag(int)) {}
%fragment("SWIG_JSCGetNumberProperty", "header", fragment=SWIG_AsVal_frag(double)) {}
%typemap(in, fragment="SWIG_JSCGetIntProperty") int[], int[ANY]
(int length = 0, JSObjectRef array, JSValueRef jsvalue, int i = 0, int res = 0, $*1_ltype temp) {
if (JSValueIsObject(context, $input))
{
// Convert into Array
array = JSValueToObject(context, $input, NULL);
length = $1_dim0;
$1 = ($*1_ltype *)malloc(sizeof($*1_ltype) * length);
// Get each element from array
for (i = 0; i < length; i++)
{
jsvalue = JSObjectGetPropertyAtIndex(context, array, i, NULL);
// Get primitive value from JSObject
res = SWIG_AsVal(int)(jsvalue, &temp);
if (!SWIG_IsOK(res))
{
SWIG_exception_fail(SWIG_ERROR, "Failed to convert $input to double");
}
arg$argnum[i] = temp;
}
}
else
{
SWIG_exception_fail(SWIG_ERROR, "$input is not JSObjectRef");
}
}
%typemap(freearg) int[], int[ANY] {
free($1);
}
%typemap(out, fragment=SWIG_From_frag(int)) int[], int[ANY] (int length = 0, int i = 0)
{
length = $1_dim0;
JSValueRef values[length];
for (i = 0; i < length; i++)
{
values[i] = SWIG_From(int)($1[i]);
}
$result = JSObjectMakeArray(context, length, values, NULL);
}
%typemap(in, fragment="SWIG_JSCGetNumberProperty") double[], double[ANY]
(int length = 0, JSObjectRef array, JSValueRef jsvalue, int i = 0, int res = 0, $*1_ltype temp) {
if (JSValueIsObject(context, $input))
{
// Convert into Array
array = JSValueToObject(context, $input, NULL);
length = $1_dim0;
$1 = ($*1_ltype *)malloc(sizeof($*1_ltype) * length);
// Get each element from array
for (i = 0; i < length; i++)
{
jsvalue = JSObjectGetPropertyAtIndex(context, array, i, NULL);
// Get primitive value from JSObject
res = SWIG_AsVal(double)(jsvalue, &temp);
if (!SWIG_IsOK(res))
{
SWIG_exception_fail(SWIG_ERROR, "Failed to convert $input to double");
}
arg$argnum[i] = temp;
}
}
else
{
SWIG_exception_fail(SWIG_ERROR, "$input is not JSObjectRef");
}
}
%typemap(freearg) double[], double[ANY] {
free($1);
}
%typemap(out, fragment=SWIG_From_frag(double)) double[], double[ANY] (int length = 0, int i = 0)
{
length = $1_dim0;
JSValueRef values[length];
for (i = 0; i < length; i++)
{
values[i] = SWIG_From(double)($1[i]);
}
$result = JSObjectMakeArray(context, length, values, NULL);
}

View file

@ -0,0 +1,26 @@
/* -----------------------------------------------------------------------------
* ccomplex.i
*
* C complex typemaps
* ISO C99: 7.3 Complex arithmetic <complex.h>
* ----------------------------------------------------------------------------- */
%include <javascriptcomplex.swg>
%{
#include <complex.h>
%}
/* C complex constructor */
#define CCplxConst(r, i) ((r) + I*(i))
%swig_cplxflt_convn(float complex, CCplxConst, creal, cimag);
%swig_cplxdbl_convn(double complex, CCplxConst, creal, cimag);
%swig_cplxdbl_convn(complex, CCplxConst, creal, cimag);
/* declaring the typemaps */
%typemaps_primitive(SWIG_TYPECHECK_CPLXFLT, float complex);
%typemaps_primitive(SWIG_TYPECHECK_CPLXDBL, double complex);
%typemaps_primitive(SWIG_TYPECHECK_CPLXDBL, complex);

View file

@ -0,0 +1 @@
%include <typemaps/cdata.swg>

View file

@ -0,0 +1,6 @@
#ifdef __cplusplus
%include <std_complex.i>
#else
%include <ccomplex.i>
#endif

View file

@ -0,0 +1 @@
%include <typemaps/exception.swg>

View file

@ -0,0 +1,19 @@
/* -----------------------------------------------------------------------------
* javascript.swg
*
* Javascript typemaps
* ----------------------------------------------------------------------------- */
%include <typemaps/swigmacros.swg>
%include <javascripttypemaps.swg>
%include <javascriptruntime.swg>
%include <javascripthelpers.swg>
%include <javascriptkw.swg>
%include <javascriptcode.swg>
%include <javascriptinit.swg>

View file

@ -0,0 +1,418 @@
/* -----------------------------------------------------------------------------
* 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")
%{
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_overloaded_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_overloaded_ctor", "templates")
%{
JSObjectRef $jswrapper(JSContextRef context, JSObjectRef thisObject, size_t argc, const JSValueRef argv[], JSValueRef* exception)
{
$jslocals
$jscode
return SWIG_JSC_NewPointerObj(context, result, SWIGTYPE_$jsmangledtype, SWIG_POINTER_OWN);
goto fail;
fail:
return NULL;
}
%}
/* -----------------------------------------------------------------------------
* 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)
{
SwigPrivData* t = (SwigPrivData*) JSObjectGetPrivate(thisObject);
if(t && t->swigCMemOwn) free (($jstype)t->swigCObject);
if(t) free(t);
}
%}
/* -----------------------------------------------------------------------------
* js_dtor: template for a destructor wrapper
* - $jsmangledname: mangled class name
* - $jstype: class type
* - ${destructor_action}: The custom destructor action to invoke.
* ----------------------------------------------------------------------------- */
%fragment ("js_dtoroverride", "templates")
%{
void $jswrapper(JSObjectRef thisObject)
{
SwigPrivData* t = (SwigPrivData*) JSObjectGetPrivate(thisObject);
if(t && t->swigCMemOwn) {
$jstype arg1 = ($jstype)t->swigCObject;
${destructor_action}
}
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;
$jscode
return jsresult;
goto fail;
fail:
return NULL;
}
%}
/* -----------------------------------------------------------------------------
* 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 $jswrapper(JSContextRef context, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef value, JSValueRef* exception)
{
$jslocals
$jscode
return true;
goto fail;
fail:
return false;
}
%}
/* -----------------------------------------------------------------------------
* 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)
{
$jslocals
JSValueRef jsresult;
if(argc != $jsargcount) SWIG_exception_fail(SWIG_ERROR, "Illegal number of arguments for $jswrapper.");
$jscode
return jsresult;
goto fail;
fail:
return NULL;
}
%}
/* -----------------------------------------------------------------------------
* 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)
{
$jslocals
JSValueRef jsresult;
int res;
$jscode
SWIG_exception_fail(SWIG_ERROR, "Illegal arguments for function $jsname.");
return jsresult;
goto fail;
fail:
return NULL;
}
%}
/* -----------------------------------------------------------------------------
* 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* p_result)
{
$jslocals
JSValueRef jsresult;
if(argc != $jsargcount) SWIG_exception_fail(SWIG_ERROR, "Illegal number of arguments for $jswrapper.");
$jscode
*p_result = jsresult;
return SWIG_OK;
goto fail;
fail:
return SWIG_TypeError;
}
%}
/* -----------------------------------------------------------------------------
* 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; }
}
%}
/* -----------------------------------------------------------------------------
* 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},
%}
/* -----------------------------------------------------------------------------
* 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;
JSClassDefinition $jsmangledname_objectDefinition;
JSClassRef $jsmangledname_classRef;
%}
/* -----------------------------------------------------------------------------
* 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
{ 0, 0, 0, 0 }
};
JSStaticFunction $jsmangledname_staticFunctions[] = {
$jsstaticclassfunctions
{ 0, 0, 0 }
};
JSStaticValue $jsmangledname_values[] = {
$jsclassvariables
{ 0, 0, 0, 0 }
};
JSStaticFunction $jsmangledname_functions[] = {
$jsclassfunctions
{ 0, 0, 0 }
};
%}
/* -----------------------------------------------------------------------------
* 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_classDefinition.finalize = $jsdtor;
$jsmangledname_objectDefinition.staticValues = $jsmangledname_values;
$jsmangledname_objectDefinition.staticFunctions = $jsmangledname_functions;
$jsclass_inheritance
JSClassRef $jsmangledname_classRef = JSClassCreate(&$jsmangledname_objectDefinition);
SWIGTYPE_$jsmangledtype->clientdata = $jsmangledname_classRef;
%}
%fragment ("jsc_class_inherit", templates)
%{
if (SWIGTYPE_p$jsbaseclassmangled != NULL) {
$jsmangledname_objectDefinition.parentClass = (JSClassRef) SWIGTYPE_p$jsbaseclassmangled->clientdata;
}
%}
%fragment ("jsc_class_noinherit", templates)
%{
$jsmangledname_objectDefinition.parentClass = _SwigObject_classRef;
%}
/* -----------------------------------------------------------------------------
* 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")
%{
JS_registerClass(context, $jsnspace_object, "$jsname", &$jsmangledname_classDefinition);
%}
/* -----------------------------------------------------------------------------
* 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")
%{
JSStaticValue $jsnspace_values[] = {
$jsglobalvariables
{ 0, 0, 0, 0 }
};
JSStaticFunction $jsnspace_functions[] = {
$jsglobalfunctions
{ 0, 0, 0 }
};
JSClassDefinition $jsnspace_classDefinition;
%}
/* -----------------------------------------------------------------------------
* 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")
%{
JS_registerNamespace(context, $jsmangledname_object, $jsparent_object, "$jsname");
%}

View file

@ -0,0 +1,146 @@
/*
Defines the As/From converters for double/float complex, you need to
provide complex Type, the Name you want to use in the converters,
the complex Constructor method, and the Real and Imag complex
accessor methods.
See the std_complex.i and ccomplex.i for concret examples.
*/
/* the common from converter */
%define %swig_fromcplx_conv(Type, Real, Imag)
%fragment(SWIG_From_frag(Type),"header",
fragment=SWIG_From_frag(double))
{
SWIGINTERNINLINE JSObjectRef
SWIG_From_dec(Type)(%ifcplusplus(const Type&, Type) c)
{
JSValueRef vals[2];
vals[0] = SWIG_From(double)(Real(c));
vals[1] = SWIG_From(double)(Imag(c));
return JSObjectMakeArray(context, 2, vals, NULL);
}
}
%enddef
/* the double case */
%define %swig_cplxdbl_conv(Type, Constructor, Real, Imag)
%fragment(SWIG_AsVal_frag(Type),"header",
fragment=SWIG_AsVal_frag(double))
{
SWIGINTERN int
SWIG_AsVal_dec(Type) (JSValueRef o, Type* val)
{
if (JSValueIsObject(context, o)) {
JSObjectRef array;
JSValueRef exception, js_re, js_im;
double re, im;
int res;
exception = 0;
res = 0;
array = JSValueToObject(context, o, &exception);
if(exception != 0)
return SWIG_TypeError;
js_re = JSObjectGetPropertyAtIndex(context, array, 0, &exception);
if(exception != 0)
return SWIG_TypeError;
js_im = JSObjectGetPropertyAtIndex(context, array, 1, &exception);
if(exception != 0)
return SWIG_TypeError;
res = SWIG_AsVal(double)(js_re, &re);
if(!SWIG_IsOK(res)) {
return SWIG_TypeError;
}
res = SWIG_AsVal(double)(js_im, &im);
if(!SWIG_IsOK(res)) {
return SWIG_TypeError;
}
if (val) *val = Constructor(re, im);
return SWIG_OK;
} else {
double d;
int res = SWIG_AddCast(SWIG_AsVal(double)(o, &d));
if (SWIG_IsOK(res)) {
if (val) *val = Constructor(d, 0.0);
return res;
}
}
return SWIG_TypeError;
}
}
%swig_fromcplx_conv(Type, Real, Imag);
%enddef
/* the float case */
%define %swig_cplxflt_conv(Type, Constructor, Real, Imag)
%fragment(SWIG_AsVal_frag(Type),"header",
fragment=SWIG_AsVal_frag(float)) {
SWIGINTERN int
SWIG_AsVal_dec(Type)(JSValueRef o, Type *val)
{
if (JSValueIsObject(context, o)) {
JSObjectRef array;
JSValueRef exception, js_re, js_im;
double re, im;
int res;
exception = 0;
res = 0;
array = JSValueToObject(context, o, &exception);
if(exception != 0)
return SWIG_TypeError;
js_re = JSObjectGetPropertyAtIndex(context, array, 0, &exception);
if(exception != 0)
return SWIG_TypeError;
js_im = JSObjectGetPropertyAtIndex(context, array, 1, &exception);
if(exception != 0)
return SWIG_TypeError;
res = SWIG_AsVal(double)(js_re, &re);
if(!SWIG_IsOK(res)) {
return SWIG_TypeError;
}
res = SWIG_AsVal(double)(js_im, &im);
if(!SWIG_IsOK(res)) {
return SWIG_TypeError;
}
if ((-FLT_MAX <= re && re <= FLT_MAX) && (-FLT_MAX <= im && im <= FLT_MAX)) {
if (val) *val = Constructor(%numeric_cast(re, float),
%numeric_cast(im, float));
return SWIG_OK;
} else {
return SWIG_OverflowError;
}
} else {
float re;
int res = SWIG_AddCast(SWIG_AsVal(float)(o, &re));
if (SWIG_IsOK(res)) {
if (val) *val = Constructor(re, 0.0);
return res;
}
}
return SWIG_TypeError;
}
}
%swig_fromcplx_conv(Type, Real, Imag);
%enddef
#define %swig_cplxflt_convn(Type, Constructor, Real, Imag) \
%swig_cplxflt_conv(Type, Constructor, Real, Imag)
#define %swig_cplxdbl_convn(Type, Constructor, Real, Imag) \
%swig_cplxdbl_conv(Type, Constructor, Real, Imag)

View file

@ -0,0 +1,23 @@
/*
Create a file with this name, 'javascriptfragments.swg', in your working
directory and add all the %fragments you want to take precedence
over the default ones defined by swig.
For example, if you add:
%fragment(SWIG_AsVal_frag(int),"header") {
SWIGINTERNINLINE int
SWIG_AsVal(int)(PyObject *obj, int *val)
{
<your code here>;
}
}
this will replace the code used to retrieve an integer value for all
the typemaps that need it, including:
int, std::vector<int>, std::list<std::pair<int,int> >, etc.
*/

View file

@ -0,0 +1,69 @@
%insert(wrapper) %{
bool JS_registerClass(JSGlobalContextRef context, JSObjectRef parentObject,
const char* className,
JSClassDefinition* definition) {
JSStringRef js_className = JSStringCreateWithUTF8CString(className);
JSObjectRef classObject = JSObjectMake(context, JSClassCreate(definition), NULL);
JSObjectSetProperty(context, parentObject,
js_className, classObject,
kJSPropertyAttributeNone, NULL);
JSStringRelease(js_className);
return true;
}
bool JS_registerNamespace(JSGlobalContextRef context,
JSObjectRef namespaceObj, JSObjectRef parentNamespace,
const char* name)
{
JSStringRef js_name = JSStringCreateWithUTF8CString(name);
JSObjectSetProperty(context, parentNamespace,
js_name, namespaceObj,
kJSPropertyAttributeNone, NULL);
JSStringRelease(js_name);
return true;
}
bool JS_registerFunction(JSGlobalContextRef context, JSObjectRef object,
const char* functionName, JSObjectCallAsFunctionCallback callback)
{
JSStringRef js_functionName = JSStringCreateWithUTF8CString(functionName);
JSObjectSetProperty(context, object, js_functionName,
JSObjectMakeFunctionWithCallback(context, js_functionName, callback),
kJSPropertyAttributeNone, NULL);
JSStringRelease(js_functionName);
return true;
}
bool JS_veto_set_variable(JSContextRef context, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef value, JSValueRef* exception)
{
char buffer[256];
char msg[512];
int res;
JSStringGetUTF8CString(propertyName, buffer, 256);
res = sprintf(msg, "Tried to write read-only variable: %s.", buffer);
if(res<0) {
SWIG_exception(SWIG_ERROR, "Tried to write read-only variable.");
} else {
SWIG_exception(SWIG_ERROR, msg);
}
return false;
}
JSValueRef JS_CharPtrToJSValue(JSContextRef context, char* cstr) {
JSValueRef val;
JSStringRef jsstring = JSStringCreateWithUTF8CString((char*) cstr);
val = JSValueMakeString(context, jsstring);
JSStringRelease(jsstring);
return val;
}
%}

View file

@ -0,0 +1,67 @@
%insert(init) %{
SWIGRUNTIME void
SWIG_JSC_SetModule(swig_module_info *swig_module) {}
SWIGRUNTIME swig_module_info *
SWIG_JSC_GetModule(void) {
return 0;
}
#define SWIG_GetModule(clientdata) SWIG_JSC_GetModule()
#define SWIG_SetModule(clientdata, pointer) SWIG_JSC_SetModule(pointer)
%}
%insert(init) "swiginit.swg"
%fragment ("js_initializer_define", "templates") %{
#define SWIGJSC_INIT $jsname_initialize
%}
// Open the initializer function
%insert(init)
%{
#ifdef __cplusplus
extern "C" {
#endif
bool SWIGJSC_INIT (JSGlobalContextRef context, JSObjectRef *exports) {
SWIG_InitializeModule(0);
%}
/* -----------------------------------------------------------------------------
* 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") %{
/* Initialize the base swig type object */
_SwigObject_objectDefinition.staticFunctions = _SwigObject_functions;
_SwigObject_objectDefinition.staticValues = _SwigObject_values;
_SwigObject_classRef = JSClassCreate(&_SwigObject_objectDefinition);
/* Initialize the PackedData class */
_SwigPackedData_objectDefinition.staticFunctions = _SwigPackedData_functions;
_SwigPackedData_objectDefinition.staticValues = _SwigPackedData_values;
_SwigPackedData_objectDefinition.finalize = _wrap_SwigPackedData_delete;
_SwigPackedData_classRef = JSClassCreate(&_SwigPackedData_objectDefinition);
/* Create objects for namespaces */
$jscreatenamespaces
/* Register classes */
$jsregisterclasses
/* Register namespaces */
$jsregisternamespaces
*exports = exports_object;
return true;
}
#ifdef __cplusplus
}
#endif
%}

View file

@ -0,0 +1,40 @@
#ifndef JAVASCRIPT_JAVASCRIPTKW_SWG_
#define JAVASCRIPT_JAVASCRIPTKW_SWG_
/* Warnings for Java keywords */
#define JAVASCRIPTKW(x) %keywordwarn("'" `x` "' is a javascript keyword, renaming to '_"`x`"'",rename="_%s") `x`
/* Taken from https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Reserved_Words */
JAVASCRIPTKW(break);
JAVASCRIPTKW(case);
JAVASCRIPTKW(catch);
JAVASCRIPTKW(continue);
JAVASCRIPTKW(default);
JAVASCRIPTKW(delete);
JAVASCRIPTKW(do);
JAVASCRIPTKW(else);
JAVASCRIPTKW(finally);
JAVASCRIPTKW(for);
JAVASCRIPTKW(function);
JAVASCRIPTKW(if);
JAVASCRIPTKW(in);
JAVASCRIPTKW(instanceof);
JAVASCRIPTKW(new);
JAVASCRIPTKW(return);
JAVASCRIPTKW(switch);
JAVASCRIPTKW(this);
JAVASCRIPTKW(throw);
JAVASCRIPTKW(try);
JAVASCRIPTKW(typeof);
JAVASCRIPTKW(var);
JAVASCRIPTKW(void);
JAVASCRIPTKW(while);
JAVASCRIPTKW(with);
/* others bad names if any*/
// for example %namewarn("321:clone() is a javascript bad method name") *::clone();
#undef JAVASCRIPTKW
#endif //JAVASCRIPT_JAVASCRIPTKW_SWG_

View file

@ -0,0 +1,182 @@
/* ------------------------------------------------------------
* Primitive Types
* ------------------------------------------------------------ */
/* boolean */
%fragment(SWIG_From_frag(bool),"header") {
SWIGINTERNINLINE
JSValueRef SWIG_From_dec(bool)(bool value)
{
return JSValueMakeBoolean(context, value);
}
}
%fragment(SWIG_AsVal_frag(bool),"header",
fragment=SWIG_AsVal_frag(long)) {
SWIGINTERN
int SWIG_AsVal_dec(bool)(JSValueRef obj, bool *val)
{
if(!JSValueIsBoolean(context, obj)) {
return SWIG_ERROR;
}
if (val) *val = JSValueToBoolean(context, obj);
return SWIG_OK;
}
}
/* int */
%fragment(SWIG_From_frag(int),"header") {
SWIGINTERNINLINE JSValueRef
SWIG_From_dec(int)(int value)
{
return JSValueMakeNumber(context, value);
}
}
/* long */
%fragment(SWIG_From_frag(long),"header") {
SWIGINTERNINLINE JSValueRef
SWIG_From_dec(long)(long value)
{
return JSValueMakeNumber(context, value);
}
}
%fragment(SWIG_AsVal_frag(long),"header",
fragment="SWIG_CanCastAsInteger") {
SWIGINTERN int
SWIG_AsVal_dec(long)(JSValueRef obj, long* val)
{
if (!JSValueIsNumber(context, obj)) {
return SWIG_TypeError;
}
if(val) *val = (long) JSValueToNumber(context, obj, NULL);
return SWIG_OK;
}
}
/* unsigned long */
%fragment(SWIG_From_frag(unsigned long),"header",
fragment=SWIG_From_frag(long)) {
SWIGINTERNINLINE JSValueRef
SWIG_From_dec(unsigned long)(unsigned long value)
{
return (value > LONG_MAX) ?
JSValueMakeNumber(context, value) : JSValueMakeNumber(context, %numeric_cast(value,long));
}
}
%fragment(SWIG_AsVal_frag(unsigned long),"header",
fragment="SWIG_CanCastAsInteger") {
SWIGINTERN int
SWIG_AsVal_dec(unsigned long)(JSValueRef obj, unsigned long *val)
{
if(!JSValueIsNumber(context, obj)) {
return SWIG_TypeError;
}
long longVal = (long) JSValueToNumber(context, obj, NULL);
if(longVal < 0) {
return SWIG_OverflowError;
}
if(val) *val = longVal;
return SWIG_OK;
}
}
/* long long */
// Note: these are copied from 'long' and probably need fixing
%fragment(SWIG_From_frag(long long),"header",
fragment=SWIG_From_frag(long),
fragment="<limits.h>") {
SWIGINTERNINLINE JSValueRef
SWIG_From_dec(long long)(long long value)
{
return JSValueMakeNumber(context, value);
}
}
%fragment(SWIG_AsVal_frag(long long),"header",
fragment=SWIG_AsVal_frag(long),
fragment="SWIG_CanCastAsInteger",
fragment="<limits.h>") {
SWIGINTERN int
SWIG_AsVal_dec(long long)(JSValueRef obj, long long* val)
{
if (!JSValueIsNumber(context, obj)) {
return SWIG_TypeError;
}
if(val) *val = (long long) JSValueToNumber(context, obj, NULL);
return SWIG_OK;
}
}
/* unsigned long long */
// Note: these are copied from 'unsigned long' and probably need fixing
%fragment(SWIG_From_frag(unsigned long long),"header",
fragment=SWIG_From_frag(long long),
fragment="<limits.h>") {
SWIGINTERN JSValueRef
SWIG_From_dec(unsigned long long)(unsigned long long value)
{
return (value > LONG_MAX) ?
JSValueMakeNumber(context, value) : JSValueMakeNumber(context, %numeric_cast(value,long));
}
}
%fragment(SWIG_AsVal_frag(unsigned long long),"header",
fragment=SWIG_AsVal_frag(unsigned long),
fragment="SWIG_CanCastAsInteger",
fragment="<limits.h>") {
SWIGINTERN int
SWIG_AsVal_dec(unsigned long long)(JSValueRef obj, unsigned long long *val)
{
if(!JSValueIsNumber(context, obj)) {
return SWIG_TypeError;
}
long long longVal = (unsigned long long) JSValueToNumber(context, obj, NULL);
if(longVal < 0) {
return SWIG_OverflowError;
}
if(val) *val = longVal;
return SWIG_OK;
}
}
/* double */
%fragment(SWIG_From_frag(double),"header") {
SWIGINTERN JSValueRef
SWIG_From_dec(double) (double val)
{
return JSValueMakeNumber(context, val);
}
}
%fragment(SWIG_AsVal_frag(double),"header") {
SWIGINTERN int
SWIG_AsVal_dec(double)(JSValueRef obj, double *val)
{
if(!JSValueIsNumber(context, obj)) {
return SWIG_TypeError;
}
if(val) *val = JSValueToNumber(context, obj, NULL);
return SWIG_OK;
}
}

View file

@ -0,0 +1,298 @@
/* ----------------------------------------------------------------------------
* Errors and exceptions
*
* ---------------------------------------------------------------------------*/
#define SWIG_Error(code, msg) SWIG_JSC_exception(context, exception, code, msg)
#define SWIG_exception(code, msg) SWIG_JSC_exception(context, exception, code, msg)
#define SWIG_fail goto fail
void SWIG_Javascript_Raise(JSContextRef context, JSValueRef *exception, const char* type) {
JSStringRef message = JSStringCreateWithUTF8CString(type);
*exception = JSValueMakeString(context, message);
JSStringRelease(message);
}
void SWIG_JSC_exception(JSContextRef context, JSValueRef *exception, int code, const char* msg) {
SWIG_Javascript_Raise(context, exception, msg);
}
/* ----------------------------------------------------------------------------
* The parent class of all Proxies
*
* ---------------------------------------------------------------------------*/
typedef struct {
bool swigCMemOwn;
void *swigCObject;
swig_type_info *info;
} SwigPrivData;
JSValueRef _wrap_SwigObject_disown(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argc, const JSValueRef argv[], JSValueRef* exception)
{
JSValueRef jsresult;
JSObjectRef obj = JSValueToObject(context, thisObject, NULL);
SwigPrivData *cdata = (SwigPrivData *) JSObjectGetPrivate(obj);
cdata->swigCMemOwn = false;
jsresult = JSValueMakeUndefined(context);
return jsresult;
}
JSValueRef _wrap_SwigObject_getCPtr(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argc, const JSValueRef argv[], JSValueRef* exception)
{
JSValueRef jsresult;
long result;
JSObjectRef obj = JSValueToObject(context, thisObject, NULL);
SwigPrivData *cdata = (SwigPrivData*) JSObjectGetPrivate(obj);
result = (long) cdata->swigCObject;
jsresult = JSValueMakeNumber(context, result);
return jsresult;
}
JSValueRef _wrap_SwigObject_equals(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argc, const JSValueRef argv[], JSValueRef* exception)
{
JSValueRef jsresult;
bool result;
JSObjectRef obj = JSValueToObject(context, thisObject, NULL);
SwigPrivData *cdata = (SwigPrivData*) JSObjectGetPrivate(obj);
JSObjectRef obj2 = JSValueToObject(context, argv[0], NULL);
SwigPrivData *cdata2 = (SwigPrivData*) JSObjectGetPrivate(obj2);
result = (cdata->swigCObject == cdata2->swigCObject);
jsresult = JSValueMakeBoolean(context, result);
return jsresult;
}
JSStaticValue _SwigObject_values[] = {
{
0, 0, 0, 0
}
};
JSStaticFunction _SwigObject_functions[] = {
{
"disown",_wrap_SwigObject_disown, kJSPropertyAttributeNone
},
{
"equals",_wrap_SwigObject_equals, kJSPropertyAttributeNone
},
{
"getCPtr",_wrap_SwigObject_getCPtr, kJSPropertyAttributeNone
},
{
0, 0, 0
}
};
JSClassDefinition _SwigObject_objectDefinition;
JSClassRef _SwigObject_classRef;
int SWIG_JSC_ConvertInstancePtr(JSContextRef context, JSObjectRef objRef, void** ptr, swig_type_info *info, int flags) {
SwigPrivData *cdata = (SwigPrivData *) JSObjectGetPrivate(objRef);
if(cdata == NULL) {
return SWIG_ERROR;
}
if(cdata->info != info) {
bool type_valid = false;
swig_cast_info *t = info->cast;
while(t != NULL) {
if(t->type == cdata->info) {
type_valid = true;
break;
}
t = t->next;
}
if(!type_valid) {
return SWIG_TypeError;
}
}
*ptr = cdata->swigCObject;
if(flags & SWIG_POINTER_DISOWN) {
cdata->swigCMemOwn = false;
}
return SWIG_OK;
}
int SWIG_JSC_ConvertPtr(JSContextRef context, JSValueRef valRef, void** ptr, swig_type_info *info, int flags) {
if(!JSValueIsObject(context, valRef)) {
return SWIG_TypeError;
}
JSObjectRef objRef = JSValueToObject(context, valRef, NULL);
if(objRef == NULL) {
return SWIG_ERROR;
}
return SWIG_JSC_ConvertInstancePtr(context, objRef, ptr, info, flags);
}
JSObjectRef SWIG_JSC_NewPointerObj(JSContextRef context, void *ptr, swig_type_info *info, int flags) {
JSClassRef classRef;
if(info->clientdata == NULL) {
classRef = _SwigObject_classRef;
} else {
classRef = (JSClassRef) info->clientdata;
}
JSObjectRef result = JSObjectMake(context, classRef, NULL);
SwigPrivData* cdata = (SwigPrivData*) malloc(sizeof(SwigPrivData));
cdata->swigCObject = ptr;
cdata->swigCMemOwn = (flags & SWIG_POINTER_OWN) ? 1 : 0;
cdata->info = info;
JSObjectSetPrivate(result, cdata);
return result;
}
#define SWIG_ConvertPtr(obj, ptr, info, flags) SWIG_JSC_ConvertPtr(context, obj, ptr, info, flags)
#define SWIG_NewPointerObj(ptr, info, flags) SWIG_JSC_NewPointerObj(context, ptr, info, flags)
#define SWIG_ConvertInstance(obj, pptr, type, flags) SWIG_JSC_ConvertInstancePtr(context, obj, pptr, type, flags)
#define SWIG_NewInstanceObj(thisvalue, type, flags) SWIG_JSC_NewPointerObj(context, thisvalue, type, flags)
#define SWIG_ConvertFunctionPtr(obj, pptr, type) SWIG_JSC_ConvertPtr(context, obj, pptr, type, 0)
#define SWIG_NewFunctionPtrObj(ptr, type) SWIG_JSC_NewPointerObj(context, ptr, type, 0)
/* ----------------------------------------------------------------------------
* A class for packed data
*
* ---------------------------------------------------------------------------*/
typedef struct {
void *data;
size_t size;
swig_type_info *type;
} SwigPackedData;
JSStaticValue _SwigPackedData_values[] = {
{
0, 0, 0, 0
}
};
JSStaticFunction _SwigPackedData_functions[] = {
{
0, 0, 0
}
};
JSClassDefinition _SwigPackedData_objectDefinition;
JSClassRef _SwigPackedData_classRef;
SWIGRUNTIMEINLINE
int SwigJSCPacked_Check(JSContextRef context, JSValueRef valRef) {
return JSValueIsObjectOfClass(context, valRef, _SwigPackedData_classRef);
}
SWIGRUNTIME
swig_type_info* SwigJSCPacked_UnpackData(JSContextRef context, JSValueRef valRef, void *ptr, size_t size) {
if (SwigJSCPacked_Check(context, valRef)) {
JSObjectRef objRef = JSValueToObject(context, valRef, NULL);
SwigPackedData *sobj = (SwigPackedData *) JSObjectGetPrivate(objRef);
if (sobj->size != size) return 0;
memcpy(ptr, sobj->data, size);
return sobj->type;
} else {
return 0;
}
}
SWIGRUNTIME
int SWIG_JSC_ConvertPacked(JSContextRef context, JSValueRef valRef, void *ptr, size_t sz, swig_type_info *ty) {
swig_type_info *to = SwigJSCPacked_UnpackData(context, valRef, ptr, sz);
if (!to) return SWIG_ERROR;
if (ty) {
if (to != ty) {
/* check type cast? */
swig_cast_info *tc = SWIG_TypeCheck(to->name,ty);
if (!tc) return SWIG_ERROR;
}
}
return SWIG_OK;
}
SWIGRUNTIME
JSValueRef SWIG_JSC_NewPackedObj(JSContextRef context, void *data, size_t size, swig_type_info *type) {
JSClassRef classRef = _SwigObject_classRef;
JSObjectRef result = JSObjectMake(context, classRef, NULL);
SwigPackedData* cdata = (SwigPackedData*) malloc(sizeof(SwigPackedData));
cdata->data = data;
cdata->size = size;
cdata->type = type;
JSObjectSetPrivate(result, cdata);
return result;
}
/* SwigPackedData wrappers */
void _wrap_SwigPackedData_delete(JSObjectRef obj)
{
SwigPackedData* cdata = (SwigPackedData*) JSObjectGetPrivate(obj);
if (cdata) {
free(cdata->data);
}
}
/* for C++ member pointers, ie, member methods */
#define SWIG_ConvertMember(obj, ptr, sz, ty) SWIG_JSC_ConvertPacked(context, obj, ptr, sz, ty)
#define SWIG_NewMemberObj(ptr, sz, type) SWIG_JSC_NewPackedObj(context, ptr, sz, type)
/* ---------------------------------------------------------------------------
* Support for IN/OUTPUT typemaps (see Lib/typemaps/inoutlist.swg)
*
* ---------------------------------------------------------------------------*/
unsigned int SWIGJSC_ArrayLength(JSContextRef context, JSObjectRef arr) {
static JSStringRef LENGTH = 0;
JSValueRef exception = NULL;
JSValueRef js_length;
double length;
if (LENGTH == 0) {
LENGTH = JSStringCreateWithUTF8CString("length");
}
js_length = JSObjectGetProperty(context, arr, LENGTH, &exception);
if (exception == 0 && JSValueIsNumber(context, js_length)) {
length = JSValueToNumber(context, js_length, 0);
return (unsigned int) length;
} else {
return 0;
}
}
SWIGRUNTIME
JSValueRef SWIGJSC_AppendOutput(JSContextRef context, JSValueRef value, JSValueRef obj) {
JSObjectRef arr;
unsigned int length;
if (JSValueIsUndefined(context, value)) {
arr = JSObjectMakeArray(context, 0, 0, 0);
} else {
arr = JSValueToObject(context, value, 0);
}
length = SWIGJSC_ArrayLength(context, arr);
JSObjectSetPropertyAtIndex(context, arr, length, obj, 0);
}

View file

@ -0,0 +1,19 @@
/* -----------------------------------------------------------------------------
* javascriptruntime.swg
*
* Javascript support code
* ----------------------------------------------------------------------------- */
%insert(runtime) %{
#include <JavaScriptCore/JavaScript.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
%}
%insert(runtime) "swigrun.swg"; /* SWIG API */
%insert(runtime) "swigerrors.swg"; /* SWIG errors */
%insert(runtime) "javascriptrun.swg"; /* SWIG errors */

View file

@ -0,0 +1,182 @@
/* ------------------------------------------------------------
* utility methods for char strings
* ------------------------------------------------------------ */
%fragment("SWIG_AsCharPtrAndSize","header",fragment="SWIG_pchar_descriptor") {
SWIGINTERN int
SWIG_JSC_AsCharPtrAndSize(JSContextRef context, JSValueRef valRef, char** cptr, size_t* psize, int *alloc)
{
if(JSValueIsString(context, valRef)) {
JSStringRef js_str = JSValueToStringCopy(context, valRef, NULL);
size_t len = JSStringGetMaximumUTF8CStringSize(js_str);
char* cstr = (char*) malloc(len * sizeof(char));
/* JSStringGetUTF8CString returns the length including 0-terminator */
len = JSStringGetUTF8CString(js_str, cstr, len);
if(alloc) *alloc = SWIG_NEWOBJ;
if(psize) *psize = len;
if(cptr) *cptr = cstr;
return SWIG_OK;
} else {
if(JSValueIsObject(context, valRef)) {
JSObjectRef obj = JSValueToObject(context, valRef, NULL);
// try if the object is a wrapped char[]
swig_type_info* pchar_descriptor = SWIG_pchar_descriptor();
if (pchar_descriptor) {
void* vptr = 0;
if (SWIG_ConvertPtr(obj, &vptr, pchar_descriptor, 0) == SWIG_OK) {
if (cptr) *cptr = (char *) vptr;
if (psize) *psize = vptr ? (strlen((char *)vptr) + 1) : 0;
if (alloc) *alloc = SWIG_OLDOBJ;
return SWIG_OK;
}
}
return SWIG_TypeError;
} else {
return SWIG_TypeError;
}
}
}
}
%fragment("SWIG_FromCharPtrAndSize","header",fragment="SWIG_pchar_descriptor") {
SWIGINTERNINLINE JSValueRef
SWIG_JSC_FromCharPtrAndSize(JSContextRef context, const char* carray, size_t size)
{
if (carray) {
if (size > INT_MAX) {
// TODO: handle extra long strings
//swig_type_info* pchar_descriptor = SWIG_pchar_descriptor();
//return pchar_descriptor ?
// SWIG_InternalNewPointerObj(%const_cast(carray,char *), pchar_descriptor, 0) : SWIG_Py_Void();
return JSValueMakeUndefined(context);
} else {
JSStringRef jsstring;
if(size < 2) {
char c[size+1];
int i;
for(i=0;i<size;++i) {
c[i] = carray[i];
}
c[size] = 0;
jsstring = JSStringCreateWithUTF8CString(c);
} else {
jsstring = JSStringCreateWithUTF8CString(carray);
}
JSValueRef result = JSValueMakeString(context, jsstring);
JSStringRelease(jsstring);
return result;
}
} else {
return JSValueMakeUndefined(context);
}
}
}
%define %_typemap2_string(StringCode, CharCode,
Char, CharName,
SWIG_AsCharPtrAndSize,
SWIG_FromCharPtrAndSize,
SWIG_CharPtrLen,
SWIG_NewCopyCharArray,
SWIG_DeleteCharArray,
FragLimits, CHAR_MIN, CHAR_MAX)
%fragment("SWIG_From"#CharName"Ptr","header",fragment=#SWIG_FromCharPtrAndSize) {
SWIGINTERNINLINE SWIG_Object
SWIG_JSC_From##CharName##Ptr(JSContextRef context, const Char *cptr)
{
return SWIG_JSC_FromCharPtrAndSize(context, cptr, (cptr ? SWIG_CharPtrLen(cptr) : 0));
}
}
%fragment("SWIG_From"#CharName"Array","header",fragment=#SWIG_FromCharPtrAndSize) {
SWIGINTERNINLINE SWIG_Object
SWIG_JSC_From##CharName##Array(JSContextRef context, const Char *cptr, size_t size)
{
return SWIG_JSC_FromCharPtrAndSize(context, cptr, size);
}
}
%fragment("SWIG_As" #CharName "Ptr","header",fragment=#SWIG_AsCharPtrAndSize) {
%define_as(SWIG_As##CharName##Ptr(obj, val, alloc), SWIG_JSC_AsCharPtrAndSize(context, obj, val, NULL, alloc))
}
%fragment("SWIG_As" #CharName "Array","header",fragment=#SWIG_AsCharPtrAndSize) {
SWIGINTERN int
SWIG_JSC_As##CharName##Array(JSContextRef context, SWIG_Object obj, Char *val, size_t size)
{
Char* cptr = 0; size_t csize = 0; int alloc = SWIG_OLDOBJ;
int res = SWIG_JSC_AsCharPtrAndSize(context, obj, &cptr, &csize, &alloc);
if (SWIG_IsOK(res)) {
if ((csize == size + 1) && cptr && !(cptr[csize-1])) --csize;
if (csize <= size) {
if (val) {
if (csize) memcpy(val, cptr, csize*sizeof(Char));
if (csize < size) memset(val + csize, 0, (size - csize)*sizeof(Char));
}
if (alloc == SWIG_NEWOBJ) {
SWIG_DeleteCharArray(cptr);
res = SWIG_DelNewMask(res);
}
return res;
}
if (alloc == SWIG_NEWOBJ) SWIG_DeleteCharArray(cptr);
}
return SWIG_TypeError;
}
#define SWIG_As##CharName##Array(obj, val, size) SWIG_JSC_As##CharName##Array(context, obj, val, size)
}
/* Char */
%fragment(SWIG_From_frag(Char),"header",fragment=#SWIG_FromCharPtrAndSize) {
SWIGINTERNINLINE SWIG_Object
SWIG_From_dec(Char)(Char c)
{
return SWIG_JSC_FromCharPtrAndSize(context, &c,1);
}
}
%fragment(SWIG_AsVal_frag(Char),"header",
fragment="SWIG_As"#CharName"Array",
fragment=FragLimits,
fragment=SWIG_AsVal_frag(long)) {
SWIGINTERN int
SWIG_AsVal_dec(Char)(SWIG_Object obj, Char *val)
{
int res = SWIG_As##CharName##Array(obj, val, 1);
if (!SWIG_IsOK(res)) {
long v;
res = SWIG_AddCast(SWIG_AsVal(long)(obj, &v));
if (SWIG_IsOK(res)) {
if ((CHAR_MIN <= v) && (v <= CHAR_MAX)) {
if (val) *val = %numeric_cast(v, Char);
} else {
res = SWIG_OverflowError;
}
}
}
return res;
}
}
%_typemap_string(StringCode,
Char,
SWIG_AsCharPtrAndSize,
SWIG_FromCharPtrAndSize,
SWIG_CharPtrLen,
SWIG_As##CharName##Ptr,
SWIG_From##CharName##Ptr,
SWIG_As##CharName##Array,
SWIG_NewCopyCharArray,
SWIG_DeleteCharArray)
%enddef
%insert(runtime) %{
#define SWIG_AsCharPtrAndSize(val, cptr, psize, alloc) SWIG_JSC_AsCharPtrAndSize(context, val, cptr, psize, alloc)
#define SWIG_FromCharPtrAndSize(cptr, size) SWIG_JSC_FromCharPtrAndSize(context, cptr, size)
#define SWIG_FromCharPtr(cptr) SWIG_JSC_FromCharPtr(context, cptr)
%}

View file

@ -0,0 +1,54 @@
/* ------------------------------------------------------------
* Typemap specializations for Javascript
* ------------------------------------------------------------ */
/* ------------------------------------------------------------
* Fragment section
* ------------------------------------------------------------ */
/* These macros are necessary to provide an extra parameter
to SWIG_AsVal_dec functions (JSContextRef context).
They must be defined before including `typemaps/fragments.swg`
*/
#define SWIG_FROM_DECL_ARGS SWIG_JSC_FROM_DECL_ARGS
#define SWIG_FROM_CALL_ARGS SWIG_JSC_FROM_CALL_ARGS
#define SWIG_AS_DECL_ARGS SWIG_JSC_AS_DECL_ARGS
#define SWIG_AS_CALL_ARGS SWIG_JSC_AS_CALL_ARGS
/* Include fundamental fragemt definitions */
%include <typemaps/fragments.swg>
/* Look for user fragments file. */
%include <javascriptfragments.swg>
/* Javascript fragments for fundamental types */
%include <javascriptprimtypes.swg>
/* Javascript fragments for char* strings */
%include <javascriptstrings.swg>
/* ------------------------------------------------------------
* Unified typemap section
* ------------------------------------------------------------ */
#define SWIG_Object JSValueRef
#define VOID_Object JSValueMakeUndefined(context)
/* append output */
#define SWIG_AppendOutput(result, obj) SWIGJSC_AppendOutput(context, result, obj)
/* set constant */
#define SWIG_SetConstant(name, obj)
/* raise */
#define SWIG_Raise(obj, type, desc) SWIG_Javascript_Raise(context, exception, type)
%insert("runtime") %{
#define SWIG_JSC_FROM_DECL_ARGS(arg1) (JSContextRef context, arg1)
#define SWIG_JSC_FROM_CALL_ARGS(arg1) (context, arg1)
#define SWIG_JSC_AS_DECL_ARGS(arg1, arg2) (JSContextRef context, arg1, arg2)
#define SWIG_JSC_AS_CALL_ARGS(arg1, arg2) (context, arg1, arg2)
%}
/* Include the unified typemap library */
%include <typemaps/swigtypemaps.swg>

View file

@ -0,0 +1,5 @@
%include <std_except.i>
%apply size_t { std::size_t };
%apply const size_t& { const std::size_t& };

View file

@ -0,0 +1,19 @@
/*
* STD C++ complex typemaps
*/
%include <javascriptcomplex.swg>
%{
#include <complex>
%}
/* defining the complex as/from converters */
%swig_cplxdbl_convn(std::complex<double>, std::complex<double>, std::real, std::imag)
%swig_cplxflt_convn(std::complex<float>, std::complex<float>, std::real, std::imag)
/* defining the typemaps */
%typemaps_primitive(%checkcode(CPLXDBL), std::complex<double>);
%typemaps_primitive(%checkcode(CPLXFLT), std::complex<float>);

View file

@ -0,0 +1 @@
%include <std/_std_deque.i>

View file

@ -0,0 +1 @@
%include <typemaps/std_except.swg>

74
Lib/javascript/jsc/std_map.i Executable file
View file

@ -0,0 +1,74 @@
/* -----------------------------------------------------------------------------
* std_map.i
*
* SWIG typemaps for std::map
* ----------------------------------------------------------------------------- */
%include <std_common.i>
// ------------------------------------------------------------------------
// std::map
// ------------------------------------------------------------------------
%{
#include <map>
#include <algorithm>
#include <stdexcept>
%}
// exported class
namespace std {
template<class K, class T> class map {
// add typemaps here
public:
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef K key_type;
typedef T mapped_type;
map();
map(const map<K,T> &);
unsigned int size() const;
bool empty() const;
void clear();
%extend {
const T& get(const K& key) throw (std::out_of_range) {
std::map<K,T >::iterator i = self->find(key);
if (i != self->end())
return i->second;
else
throw std::out_of_range("key not found");
}
void set(const K& key, const T& x) {
(*self)[key] = x;
}
void del(const K& key) throw (std::out_of_range) {
std::map<K,T >::iterator i = self->find(key);
if (i != self->end())
self->erase(i);
else
throw std::out_of_range("key not found");
}
bool has_key(const K& key) {
std::map<K,T >::iterator i = self->find(key);
return i != self->end();
}
}
};
// Legacy macros (deprecated)
%define specialize_std_map_on_key(K,CHECK,CONVERT_FROM,CONVERT_TO)
#warning "specialize_std_map_on_key ignored - macro is deprecated and no longer necessary"
%enddef
%define specialize_std_map_on_value(T,CHECK,CONVERT_FROM,CONVERT_TO)
#warning "specialize_std_map_on_value ignored - macro is deprecated and no longer necessary"
%enddef
%define specialize_std_map_on_both(K,CHECK_K,CONVERT_K_FROM,CONVERT_K_TO, T,CHECK_T,CONVERT_T_FROM,CONVERT_T_TO)
#warning "specialize_std_map_on_both ignored - macro is deprecated and no longer necessary"
%enddef
}

34
Lib/javascript/jsc/std_pair.i Executable file
View file

@ -0,0 +1,34 @@
/* -----------------------------------------------------------------------------
* std_pair.i
*
* SWIG typemaps for std::pair
* ----------------------------------------------------------------------------- */
%include <std_common.i>
%include <exception.i>
// ------------------------------------------------------------------------
// std::pair
// ------------------------------------------------------------------------
%{
#include <utility>
%}
namespace std {
template<class T, class U> struct pair {
pair();
pair(T first, U second);
pair(const pair& p);
template <class U1, class U2> pair(const pair<U1, U2> &p);
T first;
U second;
};
// add specializations here
}

69
Lib/javascript/jsc/std_string.i Executable file
View file

@ -0,0 +1,69 @@
/* -----------------------------------------------------------------------------
* std_string.i
*
* Typemaps for const std::string&.
* To use non-const std::string references use the following %apply:
* %apply const std::string & {std::string &};
*
* ----------------------------------------------------------------------------- */
%{
#include <string>
std::string SWIGJSC_valueToString(JSContextRef context, JSValueRef value) {
JSStringRef jsstring = JSValueToStringCopy(context, value, /* JSValueRef *exception */ 0);
unsigned int length = JSStringGetLength(jsstring);
char *cstr = new char[length + 1];
JSStringGetUTF8CString(jsstring, cstr, length + 1);
// create a copy
std::string result(cstr);
JSStringRelease(jsstring);
delete[] cstr;
return result;
}
JSValueRef SWIGJSC_stringToValue(JSContextRef context, const std::string& s)
{
JSValueRef result;
JSStringRef jsstring = JSStringCreateWithUTF8CString(s.c_str());
result = JSValueMakeString(context, jsstring);
JSStringRelease(jsstring);
return result;
}
%}
namespace std {
%naturalvar string;
class string;
%typemap(in) string
%{
$1 = SWIGJSC_valueToString(context, $input);
%}
%typemap(in) const string &
%{
$1 = new std::string(SWIGJSC_valueToString(context, $input));
%}
%typemap(freearg) const string &
%{
delete $1;
%}
%typemap(out) string
%{
$result = SWIGJSC_stringToValue(context, $1);
%}
%typemap(out) const string &
%{
$result = SWIGJSC_stringToValue(context, *$1);
%}
}

85
Lib/javascript/jsc/std_vector.i Executable file
View file

@ -0,0 +1,85 @@
/* -----------------------------------------------------------------------------
* std_vector.i
* ----------------------------------------------------------------------------- */
%include <std_common.i>
%{
#include <vector>
#include <stdexcept>
%}
namespace std {
template<class T> class vector {
public:
typedef size_t size_type;
typedef T value_type;
typedef const value_type& const_reference;
vector();
vector(size_type n);
size_type size() const;
size_type capacity() const;
void reserve(size_type n);
%rename(isEmpty) empty;
bool empty() const;
void clear();
%rename(add) push_back;
void push_back(const value_type& x);
%extend {
const_reference get(int i) throw (std::out_of_range) {
int size = int(self->size());
if (i>=0 && i<size)
return (*self)[i];
else
throw std::out_of_range("vector index out of range");
}
void set(int i, const value_type& val) throw (std::out_of_range) {
int size = int(self->size());
if (i>=0 && i<size)
(*self)[i] = val;
else
throw std::out_of_range("vector index out of range");
}
}
};
// bool specialization
template<> class vector<bool> {
public:
typedef size_t size_type;
typedef bool value_type;
typedef bool const_reference;
vector();
vector(size_type n);
size_type size() const;
size_type capacity() const;
void reserve(size_type n);
%rename(isEmpty) empty;
bool empty() const;
void clear();
%rename(add) push_back;
void push_back(const value_type& x);
%extend {
const_reference get(int i) throw (std::out_of_range) {
int size = int(self->size());
if (i>=0 && i<size)
return (*self)[i];
else
throw std::out_of_range("vector index out of range");
}
void set(int i, const value_type& val) throw (std::out_of_range) {
int size = int(self->size());
if (i>=0 && i<size)
(*self)[i] = val;
else
throw std::out_of_range("vector index out of range");
}
}
};
}
%define specialize_std_vector(T)
#warning "specialize_std_vector - specialization for type T no longer needed"
%enddef

10
Lib/javascript/jsc/stl.i Executable file
View file

@ -0,0 +1,10 @@
/* -----------------------------------------------------------------------------
* stl.i
* ----------------------------------------------------------------------------- */
%include <std_common.i>
%include <std_string.i>
%include <std_vector.i>
%include <std_map.i>
%include <std_pair.i>

View file

@ -0,0 +1,148 @@
/* -----------------------------------------------------------------------------
* typemaps.i
*
* Pointer handling
* These mappings provide support for input/output arguments and common
* uses for C/C++ pointers.
* ----------------------------------------------------------------------------- */
// INPUT typemaps.
// These remap a C pointer to be an "INPUT" value which is passed by value
// instead of reference.
/*
The following methods can be applied to turn a pointer into a simple
"input" value. That is, instead of passing a pointer to an object,
you would use a real value instead.
int *INPUT
short *INPUT
long *INPUT
long long *INPUT
unsigned int *INPUT
unsigned short *INPUT
unsigned long *INPUT
unsigned long long *INPUT
unsigned char *INPUT
bool *INPUT
float *INPUT
double *INPUT
To use these, suppose you had a C function like this :
double fadd(double *a, double *b) {
return *a+*b;
}
You could wrap it with SWIG as follows :
%include <typemaps.i>
double fadd(double *INPUT, double *INPUT);
or you can use the %apply directive :
%include <typemaps.i>
%apply double *INPUT { double *a, double *b };
double fadd(double *a, double *b);
*/
// OUTPUT typemaps. These typemaps are used for parameters that
// are output only. The output value is appended to the result as
// a list element.
/*
The following methods can be applied to turn a pointer into an "output"
value. When calling a function, no input value would be given for
a parameter, but an output value would be returned. In the case of
multiple output values, they are returned in the form of a Python tuple.
int *OUTPUT
short *OUTPUT
long *OUTPUT
long long *OUTPUT
unsigned int *OUTPUT
unsigned short *OUTPUT
unsigned long *OUTPUT
unsigned long long *OUTPUT
unsigned char *OUTPUT
bool *OUTPUT
float *OUTPUT
double *OUTPUT
For example, suppose you were trying to wrap the modf() function in the
C math library which splits x into integral and fractional parts (and
returns the integer part in one of its parameters).K:
double modf(double x, double *ip);
You could wrap it with SWIG as follows :
%include <typemaps.i>
double modf(double x, double *OUTPUT);
or you can use the %apply directive :
%include <typemaps.i>
%apply double *OUTPUT { double *ip };
double modf(double x, double *ip);
The Python output of the function would be a tuple containing both
output values.
*/
// INOUT
// Mappings for an argument that is both an input and output
// parameter
/*
The following methods can be applied to make a function parameter both
an input and output value. This combines the behavior of both the
"INPUT" and "OUTPUT" methods described earlier. Output values are
returned in the form of a Python tuple.
int *INOUT
short *INOUT
long *INOUT
long long *INOUT
unsigned int *INOUT
unsigned short *INOUT
unsigned long *INOUT
unsigned long long *INOUT
unsigned char *INOUT
bool *INOUT
float *INOUT
double *INOUT
For example, suppose you were trying to wrap the following function :
void neg(double *x) {
*x = -(*x);
}
You could wrap it with SWIG as follows :
%include <typemaps.i>
void neg(double *INOUT);
or you can use the %apply directive :
%include <typemaps.i>
%apply double *INOUT { double *x };
void neg(double *x);
Unlike C, this mapping does not directly modify the input value (since
this makes no sense in Python). Rather, the modified input value shows
up as the return value of the function. Thus, to apply this function
to a Python variable you might do this :
x = neg(x)
Note : previous versions of SWIG used the symbol 'BOTH' to mark
input/output arguments. This is still supported, but will be slowly
phased out in future releases.
*/
%include <typemaps/typemaps.swg>

View file

@ -0,0 +1,125 @@
/* -----------------------------------------------------------------------------
* arrays_javascript.i
*
* These typemaps give more natural support for arrays. The typemaps are not efficient
* as there is a lot of copying of the array values whenever the array is passed to C/C++
* from JavaScript and vice versa. The JavaScript array is expected to be the same size as the C array.
* An exception is thrown if they are not.
*
* Example usage:
* Wrapping:
*
* %include <arrays_javascript.i>
* %inline %{
* extern int FiddleSticks[3];
* %}
*
* Use from JavaScript like this:
*
* var fs = [10, 11, 12];
* example.FiddleSticks = fs;
* fs = example.FiddleSticks;
* ----------------------------------------------------------------------------- */
%fragment("SWIG_JSCGetIntProperty", "header", fragment=SWIG_AsVal_frag(int)) {}
%fragment("SWIG_JSCGetNumberProperty", "header", fragment=SWIG_AsVal_frag(double)) {}
%typemap(in, fragment="SWIG_JSCGetIntProperty") int[], int[ANY]
(int length = 0, v8::Local<v8::Array> array, v8::Local<v8::Value> jsvalue, int i = 0, int res = 0, $*1_ltype temp) {
if ($input->IsArray())
{
// Convert into Array
array = v8::Local<v8::Array>::Cast($input);
length = $1_dim0;
$1 = ($*1_ltype *)malloc(sizeof($*1_ltype) * length);
// Get each element from array
for (i = 0; i < length; i++)
{
jsvalue = array->Get(i);
// Get primitive value from JSObject
res = SWIG_AsVal(int)(jsvalue, &temp);
if (!SWIG_IsOK(res))
{
SWIG_exception_fail(SWIG_ERROR, "Failed to convert $input to double");
}
arg$argnum[i] = temp;
}
}
else
{
SWIG_exception_fail(SWIG_ERROR, "$input is not JSObjectRef");
}
}
%typemap(freearg) int[], int[ANY] {
free($1);
}
%typemap(out, fragment=SWIG_From_frag(int)) int[], int[ANY] (int length = 0, int i = 0)
{
length = $1_dim0;
v8::Local<v8::Array> array = v8::Array::New(length);
for (i = 0; i < length; i++)
{
array->Set(i, SWIG_From(int)($1[i]));
}
$result = array;
}
%typemap(in, fragment="SWIG_JSCGetNumberProperty") double[], double[ANY]
(int length = 0, v8::Local<v8::Array> array, v8::Local<v8::Value> jsvalue, int i = 0, int res = 0, $*1_ltype temp) {
if ($input->IsArray())
{
// Convert into Array
array = v8::Local<v8::Array>::Cast($input);
length = $1_dim0;
$1 = ($*1_ltype *)malloc(sizeof($*1_ltype) * length);
// Get each element from array
for (i = 0; i < length; i++)
{
jsvalue = array->Get(i);
// Get primitive value from JSObject
res = SWIG_AsVal(double)(jsvalue, &temp);
if (!SWIG_IsOK(res))
{
SWIG_exception_fail(SWIG_ERROR, "Failed to convert $input to double");
}
arg$argnum[i] = temp;
}
}
else
{
SWIG_exception_fail(SWIG_ERROR, "$input is not JSObjectRef");
}
}
%typemap(freearg) double[], double[ANY] {
free($1);
}
%typemap(out, fragment=SWIG_From_frag(double)) double[], double[ANY] (int length = 0, int i = 0)
{
length = $1_dim0;
v8::Local<v8::Array> array = v8::Array::New(length);
for (i = 0; i < length; i++)
{
array->Set(i, SWIG_From(double)($1[i]));
}
$result = array;
}

View file

@ -0,0 +1,26 @@
/* -----------------------------------------------------------------------------
* ccomplex.i
*
* C complex typemaps
* ISO C99: 7.3 Complex arithmetic <complex.h>
* ----------------------------------------------------------------------------- */
%include <javscriptcomplex.swg>
%{
#include <complex.h>
%}
/* C complex constructor */
#define CCplxConst(r, i) ((r) + I*(i))
%swig_cplxflt_convn(float complex, CCplxConst, creal, cimag);
%swig_cplxdbl_convn(double complex, CCplxConst, creal, cimag);
%swig_cplxdbl_convn(complex, CCplxConst, creal, cimag);
/* declaring the typemaps */
%typemaps_primitive(SWIG_TYPECHECK_CPLXFLT, float complex);
%typemaps_primitive(SWIG_TYPECHECK_CPLXDBL, double complex);
%typemaps_primitive(SWIG_TYPECHECK_CPLXDBL, complex);

View file

@ -0,0 +1 @@
%include <typemaps/cdata.swg>

View file

@ -0,0 +1,6 @@
#ifdef __cplusplus
%include <std_complex.i>
#else
%include <ccomplex.i>
#endif

View file

@ -0,0 +1 @@
%include <typemaps/exception.swg>

View file

@ -0,0 +1,19 @@
/* -----------------------------------------------------------------------------
* javascript.swg
*
* Javascript typemaps
* ----------------------------------------------------------------------------- */
%include <typemaps/swigmacros.swg>
%include <javascripttypemaps.swg>
%include <javascriptruntime.swg>
%include <javascripthelpers.swg>
%include <javascriptkw.swg>
%include <javascriptcode.swg>
%include <javascriptinit.swg>

View file

@ -0,0 +1,468 @@
/* -----------------------------------------------------------------------------
* 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") %{
SwigV8ReturnValue $jswrapper(const SwigV8Arguments& args) {
v8::HandleScope scope;
v8::Handle<v8::Object> self = args.Holder();
$jslocals
if(args.Length() != $jsargcount) SWIG_exception_fail(SWIG_ERROR, "Illegal number of arguments for $jswrapper.");
$jscode
SWIGV8_SetPrivateData(self, result, SWIGTYPE_$jsmangledtype, SWIG_POINTER_OWN);
SWIGV8_RETURN(self);
goto fail;
fail:
SWIGV8_RETURN(v8::Undefined());
}
%}
/* -----------------------------------------------------------------------------
* js_veto_ctor: a vetoing ctor for abstract classes
* - $jswrapper: name of wrapper
* - $jsname: class name
* ----------------------------------------------------------------------------- */
%fragment ("js_veto_ctor", "templates")
%{
SwigV8ReturnValue $jswrapper(const SwigV8Arguments& args) {
v8::HandleScope scope;
SWIG_exception(SWIG_ERROR, "Class $jsname can not be instantiated");
SWIGV8_RETURN(v8::Undefined());
}
%}
/* -----------------------------------------------------------------------------
* 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")
%{
SwigV8ReturnValue $jswrapper(const SwigV8Arguments& args) {
v8::HandleScope scope;
OverloadErrorHandler errorHandler;
v8::Handle<v8::Value> self;
// switch all cases by means of series of if-returns.
$jsdispatchcases
// default:
SWIG_exception_fail(SWIG_ERROR, "Illegal arguments for contruction of $jsmangledname");
fail:
SWIGV8_RETURN(v8::Undefined());
}
%}
/* -----------------------------------------------------------------------------
* js_overloaded_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_overloaded_ctor", "templates") %{
SwigV8ReturnValue $jswrapper(const SwigV8Arguments& args, V8ErrorHandler& SWIGV8_ErrorHandler) {
v8::HandleScope scope;
v8::Handle<v8::Object> self = args.Holder();
$jslocals
if(args.Length() != $jsargcount) SWIG_exception_fail(SWIG_ERROR, "Illegal number of arguments for $jswrapper.");
$jscode
SWIGV8_SetPrivateData(self, result, SWIGTYPE_$jsmangledtype, SWIG_POINTER_OWN);
SWIGV8_RETURN(self);
goto fail;
fail:
SWIGV8_RETURN(v8::Undefined());
}
%}
/* -----------------------------------------------------------------------------
* 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(args.Length() == $jsargcount) {
errorHandler.err.Clear();
#if SWIG_V8_VERSION < 0x031900
self = $jswrapper(args, errorHandler);
if(errorHandler.err.IsEmpty()) {
return scope.Close(self);
}
#else
$jswrapper(args, errorHandler);
if(errorHandler.err.IsEmpty()) {
return;
}
#endif
}
%}
/* -----------------------------------------------------------------------------
* js_dtor: template for a destructor wrapper
* - $jsmangledname: mangled class name
* - $jstype: class type
* ----------------------------------------------------------------------------- */
%fragment ("js_dtor", "templates")
%{
#if (SWIG_V8_VERSION < 0x031900)
void $jswrapper(v8::Persistent< v8::Value > object, void *parameter)
{
SWIGV8_Proxy *proxy = static_cast<SWIGV8_Proxy *>(parameter);
#else
void $jswrapper(v8::Isolate *isolate, v8::Persistent< v8::Object > * object, SWIGV8_Proxy *proxy)
{
#endif
if(proxy->swigCMemOwn && proxy->swigCObject) {
#ifdef SWIGRUNTIME_DEBUG
printf("Deleting wrapped instance: %s\n", proxy->info->name);
#endif
$jsfree proxy->swigCObject;
}
delete proxy;
object.Clear();
#if (SWIG_V8_VERSION < 0x031900)
object.Dispose();
#elif (SWIG_V8_VERSION < 0x032100)
object->Dispose(isolate);
#else
object->Dispose();
#endif
}
%}
/* -----------------------------------------------------------------------------
* js_dtoroverride: template for a destructor wrapper
* - $jsmangledname: mangled class name
* - $jstype: class type
* - ${destructor_action}: The custom destructor action to invoke.
* ----------------------------------------------------------------------------- */
%fragment ("js_dtoroverride", "templates")
%{
#if (SWIG_V8_VERSION < 0x031900)
void $jswrapper(v8::Persistent< v8::Value > object, void *parameter)
{
SWIGV8_Proxy *proxy = static_cast<SWIGV8_Proxy *>(parameter);
#else
void $jswrapper(v8::Isolate *isolate, v8::Persistent< v8::Object > * object, SWIGV8_Proxy *proxy)
{
#endif
if(proxy->swigCMemOwn && proxy->swigCObject) {
$jstype arg1 = ($jstype)proxy->swigCObject;
${destructor_action}
}
delete proxy;
#if (SWIG_V8_VERSION < 0x031900)
object.Dispose();
#elif (SWIG_V8_VERSION < 0x032100)
object->Dispose(isolate);
#else
object->Dispose();
#endif
}
%}
/* -----------------------------------------------------------------------------
* 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")
%{
SwigV8ReturnValue $jswrapper(v8::Local<v8::String> property, const SwigV8PropertyCallbackInfo& info) {
v8::HandleScope scope;
v8::Handle<v8::Value> jsresult;
$jslocals
$jscode
SWIGV8_RETURN_INFO(jsresult, info);
goto fail;
fail:
SWIGV8_RETURN_INFO(v8::Undefined(), info);
}
%}
/* -----------------------------------------------------------------------------
* 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")
%{
void $jswrapper(v8::Local<v8::String> property, v8::Local<v8::Value> value,
const SwigV8PropertyCallbackInfoVoid& info) {
v8::HandleScope scope;
$jslocals
$jscode
goto fail;
fail:
return;
}
%}
/* -----------------------------------------------------------------------------
* js_function: template for function wrappers
* - $jswrapper: wrapper function name
* - $jslocals: locals part of wrapper
* - $jscode: code part of wrapper
* ----------------------------------------------------------------------------- */
%fragment("js_function", "templates")
%{
SwigV8ReturnValue $jswrapper(const SwigV8Arguments& args) {
v8::HandleScope scope;
v8::Handle<v8::Value> jsresult;
$jslocals
if(args.Length() != $jsargcount) SWIG_exception_fail(SWIG_ERROR, "Illegal number of arguments for $jswrapper.");
$jscode
SWIGV8_RETURN(jsresult);
goto fail;
fail:
SWIGV8_RETURN(v8::Undefined());
}
%}
/* -----------------------------------------------------------------------------
* 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")
%{
SwigV8ReturnValue $jswrapper(const SwigV8Arguments& args) {
v8::HandleScope scope;
v8::Handle<v8::Value> jsresult;
OverloadErrorHandler errorHandler;
$jscode
SWIG_exception_fail(SWIG_ERROR, "Illegal arguments for function $jsname.");
goto fail;
fail:
SWIGV8_RETURN(v8::Undefined());
}
%}
/* -----------------------------------------------------------------------------
* 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")
%{
SwigV8ReturnValue $jswrapper(const SwigV8Arguments& args, V8ErrorHandler& SWIGV8_ErrorHandler)
{
v8::HandleScope scope;
v8::Handle<v8::Value> jsresult;
$jslocals
$jscode
SWIGV8_RETURN(jsresult);
goto fail;
fail:
SWIGV8_RETURN(v8::Undefined());
}
%}
/* -----------------------------------------------------------------------------
* 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(args.Length() == $jsargcount) {
errorHandler.err.Clear();
#if (SWIG_V8_VERSION < 0x031900)
jsresult = $jswrapper(args, errorHandler);
if(errorHandler.err.IsEmpty()) {
return scope.Close(jsresult);
}
#else
$jswrapper(args, errorHandler);
if(errorHandler.err.IsEmpty()) {
return;
}
#endif
}
%}
/* -----------------------------------------------------------------------------
* jsv8_declare_class_template: template for a class template declaration.
* - $jsmangledname: mangled class name
* ----------------------------------------------------------------------------- */
%fragment("jsv8_declare_class_template", "templates")
%{
SWIGV8_ClientData $jsmangledname_clientData;
%}
/* -----------------------------------------------------------------------------
* jsv8_define_class_template: template for a class template definition.
* - $jsmangledname: mangled class name
* - $jsmangledtype: mangled class type
* - $jsdtor: the dtor wrapper
* ----------------------------------------------------------------------------- */
%fragment("jsv8_define_class_template", "templates")
%{
v8::Handle<v8::FunctionTemplate> $jsmangledname_class = SWIGV8_CreateClassTemplate("$jsmangledname");
#if (SWIG_V8_VERSION < 0x031900)
$jsmangledname_clientData.class_templ = v8::Persistent<v8::FunctionTemplate>::New($jsmangledname_class);
#else
$jsmangledname_clientData.class_templ.Reset(v8::Isolate::GetCurrent(), $jsmangledname_class);
#endif
$jsmangledname_clientData.dtor = $jsdtor;
if (SWIGTYPE_$jsmangledtype->clientdata == 0) {
SWIGTYPE_$jsmangledtype->clientdata = &$jsmangledname_clientData;
}
%}
/* -----------------------------------------------------------------------------
* jsv8_inherit: template for an class inherit statement.
* - $jsmangledname: mangled class name
* - $jsbaseclass: mangled name of the base class
* ----------------------------------------------------------------------------- */
%fragment("jsv8_inherit", "templates")
%{
if (SWIGTYPE_p$jsbaseclass->clientdata && !(static_cast<SWIGV8_ClientData *>(SWIGTYPE_p$jsbaseclass->clientdata)->class_templ.IsEmpty()))
{
#if (SWIG_V8_VERSION < 0x031900)
$jsmangledname_class->Inherit(static_cast<SWIGV8_ClientData *>(SWIGTYPE_p$jsbaseclass->clientdata)->class_templ);
#else
$jsmangledname_class->Inherit(
v8::Handle<v8::FunctionTemplate>::New(
v8::Isolate::GetCurrent(),
static_cast<SWIGV8_ClientData *>(SWIGTYPE_p$jsbaseclass->clientdata)->class_templ)
);
#endif
#ifdef SWIGRUNTIME_DEBUG
printf("Inheritance successful $jsmangledname $jsbaseclass\n");
#endif
} else {
#ifdef SWIGRUNTIME_DEBUG
printf("Unable to inherit baseclass, it didn't exist $jsmangledname $jsbaseclass\n");
#endif
}
%}
/* -----------------------------------------------------------------------------
* jsv8_create_class_instance: template for creating an class object.
* - $jsname: class name
* - $jsmangledname: mangled class name
* ----------------------------------------------------------------------------- */
%fragment("jsv8_create_class_instance", "templates")
%{
v8::Handle<v8::FunctionTemplate> $jsmangledname_class_0 = SWIGV8_CreateClassTemplate("$jsname");
$jsmangledname_class_0->SetCallHandler($jsctor);
$jsmangledname_class_0->Inherit($jsmangledname_class);
$jsmangledname_class_0->SetHiddenPrototype(true);
v8::Handle<v8::Object> $jsmangledname_obj = $jsmangledname_class_0->GetFunction();
%}
/* -----------------------------------------------------------------------------
* jsv8_register_class: template for a statement that registers a class in a parent namespace.
* - $jsname: class name
* - $jsmangledname: mangled class name
* - $jsparent: mangled name of parent namespace
* ----------------------------------------------------------------------------- */
%fragment("jsv8_register_class", "templates")
%{
$jsparent_obj->Set(v8::String::NewSymbol("$jsname"), $jsmangledname_obj);
%}
/* -----------------------------------------------------------------------------
* jsv8_create_namespace: template for a statement that creates a namespace object.
* - $jsmangledname: mangled namespace name
* ----------------------------------------------------------------------------- */
%fragment("jsv8_create_namespace", "templates")
%{
v8::Handle<v8::Object> $jsmangledname_obj = v8::Object::New();
%}
/* -----------------------------------------------------------------------------
* jsv8_register_namespace: template for a statement that registers a namespace in a parent namespace.
* - $jsname: name of namespace
* - $jsmangledname: mangled name of namespace
* - $jsparent: mangled name of parent namespace
* ----------------------------------------------------------------------------- */
%fragment("jsv8_register_namespace", "templates")
%{
$jsparent_obj->Set(v8::String::NewSymbol("$jsname"), $jsmangledname_obj);
%}
/* -----------------------------------------------------------------------------
* jsv8_register_member_function: template for a statement that registers a member function.
* - $jsmangledname: mangled class name
* - $jsname: name of the function
* - $jswrapper: wrapper of the member function
* ----------------------------------------------------------------------------- */
%fragment("jsv8_register_member_function", "templates")
%{
SWIGV8_AddMemberFunction($jsmangledname_class, "$jsname", $jswrapper);
%}
/* -----------------------------------------------------------------------------
* jsv8_register_member_variable: template for a statement that registers a member variable.
* - $jsmangledname: mangled class name
* - $jsname: name of the function
* - $jsgetter: wrapper of the getter function
* - $jssetter: wrapper of the setter function
* ----------------------------------------------------------------------------- */
%fragment("jsv8_register_member_variable", "templates")
%{
SWIGV8_AddMemberVariable($jsmangledname_class, "$jsname", $jsgetter, $jssetter);
%}
/* -----------------------------------------------------------------------------
* jsv8_register_static_function: template for a statement that registers a static class function.
* - $jsname: function name
* - $jswrapper: wrapper of the function
* - $jsparent: mangled name of parent namespace
*
* Note: this template is also used for global functions.
* ----------------------------------------------------------------------------- */
%fragment("jsv8_register_static_function", "templates")
%{
SWIGV8_AddStaticFunction($jsparent_obj, "$jsname", $jswrapper);
%}
/* -----------------------------------------------------------------------------
* jsv8_register_static_variable: template for a statement that registers a static variable.
* - $jsname: variable name
* - $jsparent: mangled name of parent namespace
* - $jsgetter: wrapper of the getter function
* - $jssetter: wrapper of the setter function
*
* Note: this template is also used for global variables.
* ----------------------------------------------------------------------------- */
%fragment("jsv8_register_static_variable", "templates")
%{
SWIGV8_AddStaticVariable($jsparent_obj, "$jsname", $jsgetter, $jssetter);
%}

View file

@ -0,0 +1,123 @@
/*
Defines the As/From converters for double/float complex, you need to
provide complex Type, the Name you want to use in the converters,
the complex Constructor method, and the Real and Imag complex
accessor methods.
See the std_complex.i and ccomplex.i for concret examples.
*/
/* the common from converter */
%define %swig_fromcplx_conv(Type, Real, Imag)
%fragment(SWIG_From_frag(Type),"header",
fragment=SWIG_From_frag(double))
{
SWIGINTERNINLINE v8::Handle<v8::Value>
SWIG_From_dec(Type)(%ifcplusplus(const Type&, Type) c)
{
v8::HandleScope scope;
v8::Local<v8::Array> vals = v8::Array::New(2);
vals->Set(0, SWIG_From(double)(Real(c)));
vals->Set(1, SWIG_From(double)(Imag(c)));
return scope.Close(vals);
}
}
%enddef
/* the double case */
%define %swig_cplxdbl_conv(Type, Constructor, Real, Imag)
%fragment(SWIG_AsVal_frag(Type),"header",
fragment=SWIG_AsVal_frag(double))
{
SWIGINTERN int
SWIG_AsVal_dec(Type) (v8::Handle<v8::Value> o, Type* val)
{
v8::HandleScope scope;
if (o->IsArray()) {
v8::Handle<v8::Array> array = v8::Handle<v8::Array>::Cast(o);
if(array->Length() != 2) SWIG_Error(SWIG_TypeError, "Illegal argument for complex: must be array[2].");
double re, im;
int res;
res = SWIG_AsVal(double)(array->Get(0), &re);
if(!SWIG_IsOK(res)) {
return SWIG_TypeError;
}
res = SWIG_AsVal(double)(array->Get(1), &im);
if(!SWIG_IsOK(res)) {
return SWIG_TypeError;
}
if (val) *val = Constructor(re, im);
return SWIG_OK;
} else if(o->IsNumber()){
double d;
int res = SWIG_AddCast(SWIG_AsVal(double)(o, &d));
if (SWIG_IsOK(res)) {
if (val) *val = Constructor(d, 0.0);
return res;
}
}
return SWIG_TypeError;
}
}
%swig_fromcplx_conv(Type, Real, Imag);
%enddef
/* the float case */
%define %swig_cplxflt_conv(Type, Constructor, Real, Imag)
%fragment(SWIG_AsVal_frag(Type),"header",
fragment=SWIG_AsVal_frag(float)) {
SWIGINTERN int
SWIG_AsVal_dec(Type) (v8::Handle<v8::Value> o, Type* val)
{
v8::HandleScope scope;
if (o->IsArray()) {
v8::Handle<v8::Array> array = v8::Handle<v8::Array>::Cast(o);
if(array->Length() != 2) SWIG_Error(SWIG_TypeError, "Illegal argument for complex: must be array[2].");
double re, im;
int res;
res = SWIG_AsVal(double)(array->Get(0), &re);
if(!SWIG_IsOK(res)) {
return SWIG_TypeError;
}
res = SWIG_AsVal(double)(array->Get(1), &im);
if(!SWIG_IsOK(res)) {
return SWIG_TypeError;
}
if ((-FLT_MAX <= re && re <= FLT_MAX) && (-FLT_MAX <= im && im <= FLT_MAX)) {
if (val) *val = Constructor(%numeric_cast(re, float),
%numeric_cast(im, float));
return SWIG_OK;
} else {
return SWIG_OverflowError;
}
} else if(o->IsNumber()){
float re;
int res = SWIG_AddCast(SWIG_AsVal(float)(o, &re));
if (SWIG_IsOK(res)) {
if (val) *val = Constructor(re, 0.0);
return res;
}
}
return SWIG_TypeError;
}
}
%swig_fromcplx_conv(Type, Real, Imag);
%enddef
#define %swig_cplxflt_convn(Type, Constructor, Real, Imag) \
%swig_cplxflt_conv(Type, Constructor, Real, Imag)
#define %swig_cplxdbl_convn(Type, Constructor, Real, Imag) \
%swig_cplxdbl_conv(Type, Constructor, Real, Imag)

View file

@ -0,0 +1,23 @@
/*
Create a file with this name, 'javascriptfragments.swg', in your working
directory and add all the %fragments you want to take precedence
over the default ones defined by swig.
For example, if you add:
%fragment(SWIG_AsVal_frag(int),"header") {
SWIGINTERNINLINE int
SWIG_AsVal(int)(PyObject *obj, int *val)
{
<your code here>;
}
}
this will replace the code used to retrieve an integer value for all
the typemaps that need it, including:
int, std::vector<int>, std::list<std::pair<int,int> >, etc.
*/

View file

@ -0,0 +1,87 @@
%insert(runtime) %{
// Note: since 3.19 there are new CallBack types, since 03.21.9 the old ones have been removed
#if SWIG_V8_VERSION < 0x031900
typedef v8::InvocationCallback SwigV8FunctionCallback;
typedef v8::AccessorGetter SwigV8AccessorGetterCallback;
typedef v8::AccessorSetter SwigV8AccessorSetterCallback;
typedef v8::AccessorInfo SwigV8PropertyCallbackInfoVoid;
#else
typedef v8::FunctionCallback SwigV8FunctionCallback;
typedef v8::AccessorGetterCallback SwigV8AccessorGetterCallback;
typedef v8::AccessorSetterCallback SwigV8AccessorSetterCallback;
typedef v8::PropertyCallbackInfo<void> SwigV8PropertyCallbackInfoVoid;
#endif
/**
* Creates a class template for a class with specified initialization function.
*/
v8::Handle<v8::FunctionTemplate> SWIGV8_CreateClassTemplate(const char* symbol) {
v8::HandleScope scope;
v8::Local<v8::FunctionTemplate> class_templ = v8::FunctionTemplate::New();
class_templ->SetClassName(v8::String::NewSymbol(symbol));
v8::Handle<v8::ObjectTemplate> inst_templ = class_templ->InstanceTemplate();
inst_templ->SetInternalFieldCount(1);
v8::Handle<v8::ObjectTemplate> equals_templ = class_templ->PrototypeTemplate();
equals_templ->Set(v8::String::NewSymbol("equals"), v8::FunctionTemplate::New(_SWIGV8_wrap_equals));
v8::Handle<v8::ObjectTemplate> cptr_templ = class_templ->PrototypeTemplate();
cptr_templ->Set(v8::String::NewSymbol("getCPtr"), v8::FunctionTemplate::New(_wrap_getCPtr));
return scope.Close(class_templ);
}
/**
* Registers a class method with given name for a given class template.
*/
void SWIGV8_AddMemberFunction(v8::Handle<v8::FunctionTemplate> class_templ, const char* symbol,
SwigV8FunctionCallback _func) {
v8::Handle<v8::ObjectTemplate> proto_templ = class_templ->PrototypeTemplate();
proto_templ->Set(v8::String::NewSymbol(symbol), v8::FunctionTemplate::New(_func));
}
/**
* Registers a class property with given name for a given class template.
*/
void SWIGV8_AddMemberVariable(v8::Handle<v8::FunctionTemplate> class_templ, const char* symbol,
SwigV8AccessorGetterCallback getter, SwigV8AccessorSetterCallback setter) {
v8::Handle<v8::ObjectTemplate> proto_templ = class_templ->InstanceTemplate();
proto_templ->SetAccessor(v8::String::NewSymbol(symbol), getter, setter);
}
/**
* Registers a class method with given name for a given object.
*/
void SWIGV8_AddStaticFunction(v8::Handle<v8::Object> obj, const char* symbol,
const SwigV8FunctionCallback& _func) {
obj->Set(v8::String::NewSymbol(symbol), v8::FunctionTemplate::New(_func)->GetFunction());
}
/**
* Registers a class method with given name for a given object.
*/
void SWIGV8_AddStaticVariable(v8::Handle<v8::Object> obj, const char* symbol,
SwigV8AccessorGetterCallback getter, SwigV8AccessorSetterCallback setter) {
obj->SetAccessor(v8::String::NewSymbol(symbol), getter, setter);
}
void JS_veto_set_variable(v8::Local<v8::String> property, v8::Local<v8::Value> value,
const SwigV8PropertyCallbackInfoVoid& info)
{
char buffer[256];
char msg[512];
int res;
property->WriteUtf8(buffer, 256);
res = sprintf(msg, "Tried to write read-only variable: %s.", buffer);
if(res<0) {
SWIG_exception(SWIG_ERROR, "Tried to write read-only variable.");
} else {
SWIG_exception(SWIG_ERROR, msg);
}
}
%} // v8_helper_functions

View file

@ -0,0 +1,118 @@
%insert(init) %{
#include <assert.h>
SWIGRUNTIME void
SWIG_V8_SetModule(void *, swig_module_info *swig_module) {
v8::Local<v8::Object> global_obj = v8::Context::GetCurrent()->Global();
v8::Local<v8::External> mod = v8::External::New(swig_module);
assert(!mod.IsEmpty());
global_obj->SetHiddenValue(v8::String::New("swig_module_info_data"), mod);
}
SWIGRUNTIME swig_module_info *
SWIG_V8_GetModule(void *) {
v8::Local<v8::Object> global_obj = v8::Context::GetCurrent()->Global();
v8::Local<v8::Value> moduleinfo = global_obj->GetHiddenValue(v8::String::New("swig_module_info_data"));
if (moduleinfo.IsEmpty())
{
// It's not yet loaded
return 0;
}
v8::Local<v8::External> moduleinfo_extern = v8::Local<v8::External>::Cast(moduleinfo);
if (moduleinfo_extern.IsEmpty())
{
// Something's not right
return 0;
}
void *ptr = moduleinfo_extern->Value();
assert(ptr);
swig_module_info *retptr = static_cast<swig_module_info *>(ptr);
assert(retptr);
return retptr;
}
#define SWIG_GetModule(clientdata) SWIG_V8_GetModule(clientdata)
#define SWIG_SetModule(clientdata, pointer) SWIG_V8_SetModule(clientdata, pointer)
%}
%insert(init) "swiginit.swg"
// Open the initializer function definition here
%fragment ("js_initializer_define", "templates") %{
#define SWIGV8_INIT $jsname_initialize
%}
%insert(init) %{
// Note: 'extern "C"'' disables name mangling which makes it easier to load the symbol manually
// TODO: is it ok to do that?
extern "C"
#if (NODE_MODULE_VERSION < 0x000C)
void SWIGV8_INIT (v8::Handle<v8::Object> exports)
#else
void SWIGV8_INIT (v8::Handle<v8::Object> exports, v8::Handle<v8::Object> /*module*/)
#endif
{
SWIG_InitializeModule(static_cast<void *>(&exports));
v8::HandleScope scope;
v8::Handle<v8::Object> exports_obj = exports;
%}
/* -----------------------------------------------------------------------------
* js_initializer: template for the module initializer function
* - $jsname: module name
* - $jsv8nspaces: part with code creating namespace objects
* - $jsv8classtemplates: part with code creating class templates
* - $jsv8wrappers: part with code that registers wrapper functions
* - $jsv8inheritance: part with inherit statements
* - $jsv8classinstances: part with code creating class objects
* - $jsv8staticwrappers: part with code adding static functions to class objects
* - $jsv8registerclasses: part with code that registers class objects in namespaces
* - $jsv8registernspaces: part with code that registers namespaces in parent namespaces
* ----------------------------------------------------------------------------- */
%fragment("js_initializer", "templates")
%{
// a class template for creating proxies of undefined types
#if (SWIG_V8_VERSION < 0x031900)
SWIGV8_SWIGTYPE_Proxy_class_templ = v8::Persistent<v8::FunctionTemplate>::New(SWIGV8_CreateClassTemplate("SwigProxy"));
#else
SWIGV8_SWIGTYPE_Proxy_class_templ.Reset(v8::Isolate::GetCurrent(), SWIGV8_CreateClassTemplate("SwigProxy"));
#endif
/* create objects for namespaces */
$jsv8nspaces
/* create class templates */
$jsv8classtemplates
/* register wrapper functions */
$jsv8wrappers
/* setup inheritances */
$jsv8inheritance
/* class instances */
$jsv8classinstances
/* add static class functions and variables */
$jsv8staticwrappers
/* register classes */
$jsv8registerclasses
/* create and register namespace objects */
$jsv8registernspaces
}
#if defined(BUILDING_NODE_EXTENSION)
NODE_MODULE($jsname, $jsname_initialize);
#endif
%}

View file

@ -0,0 +1,40 @@
#ifndef JAVASCRIPT_JAVASCRIPTKW_SWG_
#define JAVASCRIPT_JAVASCRIPTKW_SWG_
/* Warnings for Java keywords */
#define JAVASCRIPTKW(x) %keywordwarn("'" `x` "' is a javascript keyword, renaming to '_"`x`"'",rename="_%s") `x`
/* Taken from https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Reserved_Words */
JAVASCRIPTKW(break);
JAVASCRIPTKW(case);
JAVASCRIPTKW(catch);
JAVASCRIPTKW(continue);
JAVASCRIPTKW(default);
JAVASCRIPTKW(delete);
JAVASCRIPTKW(do);
JAVASCRIPTKW(else);
JAVASCRIPTKW(finally);
JAVASCRIPTKW(for);
JAVASCRIPTKW(function);
JAVASCRIPTKW(if);
JAVASCRIPTKW(in);
JAVASCRIPTKW(instanceof);
JAVASCRIPTKW(new);
JAVASCRIPTKW(return);
JAVASCRIPTKW(switch);
JAVASCRIPTKW(this);
JAVASCRIPTKW(throw);
JAVASCRIPTKW(try);
JAVASCRIPTKW(typeof);
JAVASCRIPTKW(var);
JAVASCRIPTKW(void);
JAVASCRIPTKW(while);
JAVASCRIPTKW(with);
/* others bad names if any*/
// for example %namewarn("321:clone() is a javascript bad method name") *::clone();
#undef JAVASCRIPTKW
#endif //JAVASCRIPT_JAVASCRIPTKW_SWG_

View file

@ -0,0 +1,198 @@
/* ------------------------------------------------------------
* Primitive Types
* ------------------------------------------------------------ */
/* boolean */
%fragment(SWIG_From_frag(bool),"header") {
SWIGINTERNINLINE
v8::Handle<v8::Value>
SWIG_From_dec(bool)(bool value)
{
return v8::Boolean::New(value);
}
}
%fragment(SWIG_AsVal_frag(bool),"header",
fragment=SWIG_AsVal_frag(long)) {
SWIGINTERN
int SWIG_AsVal_dec(bool)(v8::Handle<v8::Value> obj, bool *val)
{
if(!obj->IsBoolean()) {
return SWIG_ERROR;
}
if (val) *val = obj->BooleanValue();
return SWIG_OK;
}
}
/* int */
%fragment(SWIG_From_frag(int),"header") {
SWIGINTERNINLINE
v8::Handle<v8::Value> SWIG_From_dec(int)(int value)
{
return v8::Int32::New(value);
}
}
%fragment(SWIG_AsVal_frag(int),"header") {
SWIGINTERN
int SWIG_AsVal_dec(int)(v8::Handle<v8::Value> valRef, int* val)
{
if (!valRef->IsNumber()) {
return SWIG_TypeError;
}
if(val) *val = valRef->IntegerValue();
return SWIG_OK;
}
}
/* long */
%fragment(SWIG_From_frag(long),"header") {
SWIGINTERNINLINE
v8::Handle<v8::Value> SWIG_From_dec(long)(long value)
{
return v8::Number::New(value);
}
}
%fragment(SWIG_AsVal_frag(long),"header",
fragment="SWIG_CanCastAsInteger") {
SWIGINTERN
int SWIG_AsVal_dec(long)(v8::Handle<v8::Value> obj, long* val)
{
if (!obj->IsNumber()) {
return SWIG_TypeError;
}
if(val) *val = (long) obj->IntegerValue();
return SWIG_OK;
}
}
/* unsigned long */
%fragment(SWIG_From_frag(unsigned long),"header",
fragment=SWIG_From_frag(long)) {
SWIGINTERNINLINE
v8::Handle<v8::Value> SWIG_From_dec(unsigned long)(unsigned long value)
{
return (value > LONG_MAX) ?
v8::Integer::NewFromUnsigned(value) : v8::Integer::New(%numeric_cast(value,long));
}
}
%fragment(SWIG_AsVal_frag(unsigned long),"header",
fragment="SWIG_CanCastAsInteger") {
SWIGINTERN
int SWIG_AsVal_dec(unsigned long)(v8::Handle<v8::Value> obj, unsigned long *val)
{
if(!obj->IsNumber()) {
return SWIG_TypeError;
}
long longVal = (long) obj->NumberValue();
if(longVal < 0) {
return SWIG_OverflowError;
}
if(val) *val = longVal;
return SWIG_OK;
}
}
/* long long */
// Note: these are copied from 'long' and probably need fixing
%fragment(SWIG_From_frag(long long),"header",
fragment=SWIG_From_frag(long),
fragment="<limits.h>") {
SWIGINTERNINLINE
v8::Handle<v8::Value> SWIG_From_dec(long long)(long long value)
{
return v8::Number::New(value);
}
}
%fragment(SWIG_AsVal_frag(long long),"header",
fragment=SWIG_AsVal_frag(long),
fragment="SWIG_CanCastAsInteger",
fragment="<limits.h>") {
SWIGINTERN
int SWIG_AsVal_dec(long long)(v8::Handle<v8::Value> obj, long long* val)
{
if (!obj->IsNumber()) {
return SWIG_TypeError;
}
if(val) *val = (long long) obj->IntegerValue();
return SWIG_OK;
}
}
/* unsigned long long */
// Note: these are copied from 'unsigned long' and probably need fixing
%fragment(SWIG_From_frag(unsigned long long),"header",
fragment=SWIG_From_frag(long long),
fragment="<limits.h>") {
SWIGINTERNINLINE
v8::Handle<v8::Value> SWIG_From_dec(unsigned long long)(unsigned long long value)
{
return (value > LONG_MAX) ?
v8::Integer::NewFromUnsigned(value) : v8::Integer::New(%numeric_cast(value,long));
}
}
%fragment(SWIG_AsVal_frag(unsigned long long),"header",
fragment=SWIG_AsVal_frag(unsigned long),
fragment="SWIG_CanCastAsInteger",
fragment="<limits.h>") {
SWIGINTERN
int SWIG_AsVal_dec(unsigned long long)(v8::Handle<v8::Value> obj, unsigned long long *val)
{
if(!obj->IsNumber()) {
return SWIG_TypeError;
}
long long longVal = (long long) obj->NumberValue();
if(longVal < 0) {
return SWIG_OverflowError;
}
if(val) *val = longVal;
return SWIG_OK;
}
}
/* double */
%fragment(SWIG_From_frag(double),"header") {
SWIGINTERN
v8::Handle<v8::Value> SWIG_From_dec(double) (double val)
{
return v8::Number::New(val);
}
}
%fragment(SWIG_AsVal_frag(double),"header") {
SWIGINTERN
int SWIG_AsVal_dec(double)(v8::Handle<v8::Value> obj, double *val)
{
if(!obj->IsNumber()) {
return SWIG_TypeError;
}
if(val) *val = obj->NumberValue();
return SWIG_OK;
}
}

View file

@ -0,0 +1,462 @@
/* ---------------------------------------------------------------------------
* Error handling
*
* ---------------------------------------------------------------------------*/
#define SWIG_Error(code, msg) SWIGV8_ErrorHandler.error(code, msg)
#define SWIG_exception(code, msg) SWIGV8_ErrorHandler.error(code, msg)
#define SWIG_fail goto fail
#define SWIGV8_OVERLOAD false
void SWIG_V8_Raise(const char* msg) {
v8::ThrowException(v8::Exception::Error(v8::String::New(msg)));
}
/*
Note: There are two contexts for handling errors.
A static V8ErrorHandler is used in not overloaded methods.
For overloaded methods the throwing type checking mechanism is used
during dispatching. As V8 exceptions can not be resetted properly
the trick is to use a dynamic ErrorHandler with same local name as the global
one.
- See defintion of SWIG_Error above.
- See code templates 'JS_function_dispatcher', 'JS_functionwrapper_overload',
and 'JS_function_dispatch_case' in javascriptcode.swg
*/
class V8ErrorHandler {
public:
virtual ~V8ErrorHandler() {}
virtual void error(int code, const char* msg) {
SWIG_V8_Raise(msg);
}
};
// this is used in usually
V8ErrorHandler SWIGV8_ErrorHandler;
// instances of this are used in overloaded functions
class OverloadErrorHandler: public V8ErrorHandler {
public:
virtual void error(int code, const char* msg) {
err = v8::Exception::Error(v8::String::New(msg));
if(code != SWIG_TypeError) {
v8::ThrowException(err);
}
}
v8::Handle<v8::Value> err;
};
// Note: these typedefs and defines are used to deal with v8 API changes since version 3.19.00
#if (SWIG_V8_VERSION < 0x031900)
typedef v8::Handle<v8::Value> SwigV8ReturnValue;
typedef v8::Arguments SwigV8Arguments;
typedef v8::AccessorInfo SwigV8PropertyCallbackInfo;
#define SWIGV8_RETURN(val) return scope.Close(val)
#define SWIGV8_RETURN_INFO(val, info) return scope.Close(val)
#else
typedef void SwigV8ReturnValue;
typedef v8::FunctionCallbackInfo<v8::Value> SwigV8Arguments;
typedef v8::PropertyCallbackInfo<v8::Value> SwigV8PropertyCallbackInfo;
#define SWIGV8_RETURN(val) args.GetReturnValue().Set(val); return
#define SWIGV8_RETURN_INFO(val, info) info.GetReturnValue().Set(val); return
#endif
/* ---------------------------------------------------------------------------
* Basic Proxy object
*
* ---------------------------------------------------------------------------*/
// Note: to trigger the v8 gc more often one can tell v8 about the memory consumption
// TODO: we could add a v8 specific parameter to control this value
#define SWIGV8_AVG_OBJ_SIZE 1000
class SWIGV8_Proxy {
public:
SWIGV8_Proxy(): swigCMemOwn(false), swigCObject(0), info(0) {
v8::V8::AdjustAmountOfExternalAllocatedMemory(SWIGV8_AVG_OBJ_SIZE);
};
~SWIGV8_Proxy() {
#if (SWIG_V8_VERSION < 0x031900 || SWIG_V8_VERSION >= 0x032100)
handle.ClearWeak();
handle.Dispose();
#else
handle.ClearWeak(v8::Isolate::GetCurrent());
handle.Dispose(v8::Isolate::GetCurrent());
#endif
handle.Clear();
v8::V8::AdjustAmountOfExternalAllocatedMemory(-SWIGV8_AVG_OBJ_SIZE);
}
bool swigCMemOwn;
void *swigCObject;
swig_type_info *info;
v8::Persistent<v8::Object> handle;
};
class SWIGV8_ClientData {
public:
v8::Persistent<v8::FunctionTemplate> class_templ;
#if (SWIG_V8_VERSION < 0x031900)
void (*dtor) (v8::Persistent< v8::Value> object, void *parameter);
#else
void (*dtor) (v8::Isolate *isolate, v8::Persistent< v8::Object > *object, SWIGV8_Proxy *proxy);
#endif
};
v8::Persistent<v8::FunctionTemplate> SWIGV8_SWIGTYPE_Proxy_class_templ;
int SWIG_V8_ConvertInstancePtr(v8::Handle<v8::Object> objRef, void** ptr, swig_type_info *info, int flags) {
v8::HandleScope scope;
if(objRef->InternalFieldCount() < 1) return SWIG_ERROR;
#if (SWIG_V8_VERSION < 0x031900)
v8::Handle<v8::Value> cdataRef = objRef->GetInternalField(0);
SWIGV8_Proxy *cdata = static_cast<SWIGV8_Proxy *>(v8::External::Unwrap(cdataRef));
#else
SWIGV8_Proxy *cdata = static_cast<SWIGV8_Proxy *>(objRef->GetAlignedPointerFromInternalField(0));
#endif
if(cdata == NULL) {
return SWIG_ERROR;
}
if(cdata->info != info) {
swig_cast_info *tc = SWIG_TypeCheckStruct(cdata->info, info);
if (!tc && cdata->info->name) {
tc = SWIG_TypeCheck(cdata->info->name, info);
}
bool type_valid = tc != 0;
if(!type_valid) {
return SWIG_TypeError;
}
}
*ptr = cdata->swigCObject;
if(flags & SWIG_POINTER_DISOWN) {
cdata->swigCMemOwn = false;
}
return SWIG_OK;
}
#if (SWIG_V8_VERSION < 0x031900)
void SWIGV8_Proxy_DefaultDtor(v8::Persistent< v8::Value > object, void *parameter)
#else
void SWIGV8_Proxy_DefaultDtor(v8::Isolate *, v8::Persistent< v8::Object > *object, SWIGV8_Proxy *proxy)
#endif
{
#if (SWIG_V8_VERSION < 0x031900)
SWIGV8_Proxy *proxy = static_cast<SWIGV8_Proxy *>(parameter);
#endif
delete proxy;
}
int SWIG_V8_GetInstancePtr(v8::Handle<v8::Value> valRef, void** ptr) {
if(!valRef->IsObject()) {
return SWIG_TypeError;
}
v8::Handle<v8::Object> objRef = valRef->ToObject();
if(objRef->InternalFieldCount() < 1) return SWIG_ERROR;
#if (SWIG_V8_VERSION < 0x031900)
v8::Handle<v8::Value> cdataRef = objRef->GetInternalField(0);
SWIGV8_Proxy *cdata = static_cast<SWIGV8_Proxy *>(v8::External::Unwrap(cdataRef));
#else
SWIGV8_Proxy *cdata = static_cast<SWIGV8_Proxy *>(objRef->GetAlignedPointerFromInternalField(0));
#endif
if(cdata == NULL) {
return SWIG_ERROR;
}
*ptr = cdata->swigCObject;
return SWIG_OK;
}
void SWIGV8_SetPrivateData(v8::Handle<v8::Object> obj, void* ptr, swig_type_info *info, int flags) {
SWIGV8_Proxy* cdata = new SWIGV8_Proxy();
cdata->swigCObject = ptr;
cdata->swigCMemOwn = (flags & SWIG_POINTER_OWN) ? 1 : 0;
cdata->info = info;
#if (SWIG_V8_VERSION < 0x031900)
obj->SetPointerInInternalField(0, cdata);
cdata->handle = v8::Persistent<v8::Object>::New(obj);
#else
obj->SetAlignedPointerInInternalField(0, cdata);
cdata->handle.Reset(v8::Isolate::GetCurrent(), obj);
#endif
#if (SWIG_V8_VERSION < 0x031900)
// clientdata must be set for owned data as we need to register the dtor
if(cdata->swigCMemOwn && (SWIGV8_ClientData*)info->clientdata) {
cdata->handle.MakeWeak(cdata, ((SWIGV8_ClientData*)info->clientdata)->dtor);
} else {
cdata->handle.MakeWeak(cdata, SWIGV8_Proxy_DefaultDtor);
}
cdata->handle.MarkIndependent();
#else
if(cdata->swigCMemOwn && (SWIGV8_ClientData*)info->clientdata) {
cdata->handle.MakeWeak(v8::Isolate::GetCurrent(), cdata, ((SWIGV8_ClientData*)info->clientdata)->dtor);
} else {
cdata->handle.MakeWeak(v8::Isolate::GetCurrent(), cdata, SWIGV8_Proxy_DefaultDtor);
}
#if (SWIG_V8_VERSION < 0x032100)
cdata->handle.MarkIndependent(v8::Isolate::GetCurrent());
#else
cdata->handle.MarkIndependent();
#endif
#endif
}
int SWIG_V8_ConvertPtr(v8::Handle<v8::Value> valRef, void** ptr, swig_type_info *info, int flags) {
v8::HandleScope scope;
if(!valRef->IsObject()) {
return SWIG_TypeError;
}
v8::Handle<v8::Object> objRef = valRef->ToObject();
return SWIG_V8_ConvertInstancePtr(objRef, ptr, info, flags);
}
v8::Handle<v8::Object> SWIG_V8_NewPointerObj(void *ptr, swig_type_info *info, int flags) {
v8::HandleScope scope;
v8::Handle<v8::FunctionTemplate> class_templ;
#if (SWIG_V8_VERSION < 0x031900)
if(info->clientdata != 0) {
class_templ = ((SWIGV8_ClientData*) info->clientdata)->class_templ;
} else {
class_templ = SWIGV8_SWIGTYPE_Proxy_class_templ;
}
#else
v8::Isolate *iso = v8::Isolate::GetCurrent();
if(info->clientdata != 0) {
class_templ = v8::Handle<v8::FunctionTemplate>::New(iso, ((SWIGV8_ClientData*) info->clientdata)->class_templ);
} else {
class_templ = v8::Handle<v8::FunctionTemplate>::New(iso, SWIGV8_SWIGTYPE_Proxy_class_templ);
}
#endif
v8::Handle<v8::Object> result = class_templ->InstanceTemplate()->NewInstance();
SWIGV8_SetPrivateData(result, ptr, info, flags);
return scope.Close(result);
}
#define SWIG_ConvertPtr(obj, ptr, info, flags) SWIG_V8_ConvertPtr(obj, ptr, info, flags)
#define SWIG_NewPointerObj(ptr, info, flags) SWIG_V8_NewPointerObj(ptr, info, flags)
#define SWIG_ConvertInstance(obj, pptr, type, flags) SWIG_V8_ConvertInstancePtr(obj, pptr, type, flags)
#define SWIG_NewInstanceObj(thisvalue, type, flags) SWIG_V8_NewPointerObj(thisvalue, type, flags)
#define SWIG_ConvertFunctionPtr(obj, pptr, type) SWIG_V8_ConvertPtr(obj, pptr, type, 0)
#define SWIG_NewFunctionPtrObj(ptr, type) SWIG_V8_NewPointerObj(ptr, type, 0)
#define SWIG_GetInstancePtr(obj, ptr) SWIG_V8_GetInstancePtr(obj, ptr)
#if (SWIG_V8_VERSION < 0x031900)
v8::Handle<v8::Value> _SWIGV8_wrap_equals(const v8::Arguments &args) {
#else
void _SWIGV8_wrap_equals(const v8::FunctionCallbackInfo<v8::Value>& args) {
#endif
v8::HandleScope scope;
v8::Handle<v8::Value> jsresult;
void *arg1 = (void *) 0 ;
void *arg2 = (void *) 0 ;
bool result;
int res1;
int res2;
if(args.Length() != 1) SWIG_exception_fail(SWIG_ERROR, "Illegal number of arguments for equals.");
res1 = SWIG_GetInstancePtr(args.Holder(), &arg1);
if (!SWIG_IsOK(res1)) {
SWIG_exception_fail(SWIG_ERROR, "Could not get pointer from 'this' object for equals.");
}
res2 = SWIG_GetInstancePtr(args[0], &arg2);
if (!SWIG_IsOK(res2)) {
SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "equals" "', argument " "1"" of type '" "void *""'");
}
result = (bool)(arg1 == arg2);
jsresult = v8::Boolean::New(result);
SWIGV8_RETURN(jsresult);
goto fail;
fail:
SWIGV8_RETURN(v8::Undefined());
}
#if (SWIG_V8_VERSION < 0x031900)
v8::Handle<v8::Value> _wrap_getCPtr(const v8::Arguments &args) {
#else
void _wrap_getCPtr(const v8::FunctionCallbackInfo<v8::Value>& args) {
#endif
v8::HandleScope scope;
v8::Handle<v8::Value> jsresult;
void *arg1 = (void *) 0 ;
long result;
int res1;
res1 = SWIG_GetInstancePtr(args.Holder(), &arg1);
if (!SWIG_IsOK(res1)) {
SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "getCPtr" "', argument " "1"" of type '" "void *""'");
}
result = (long)arg1;
jsresult = v8::Number::New(result);
SWIGV8_RETURN(jsresult);
goto fail;
fail:
SWIGV8_RETURN(v8::Undefined());
}
/* ---------------------------------------------------------------------------
* PackedData object
*
* ---------------------------------------------------------------------------*/
class SwigV8PackedData {
public:
SwigV8PackedData(void *data, size_t size, swig_type_info *type): data(data), size(size), type(type) {};
~SwigV8PackedData() {
};
void* data;
size_t size;
swig_type_info *type;
v8::Persistent<v8::Object> handle;
};
SWIGRUNTIMEINLINE
int SwigV8Packed_Check(v8::Handle<v8::Value> valRef) {
v8::HandleScope scope;
v8::Handle<v8::Object> objRef = valRef->ToObject();
if(objRef->InternalFieldCount() < 1) return false;
v8::Handle<v8::Value> flag = objRef->GetHiddenValue(v8::String::New("__swig__packed_data__"));
return (flag->IsBoolean() && flag->BooleanValue());
}
SWIGRUNTIME
swig_type_info* SwigV8Packed_UnpackData(v8::Handle<v8::Value> valRef, void *ptr, size_t size) {
if (SwigV8Packed_Check(valRef)) {
v8::HandleScope scope;
SwigV8PackedData *sobj;
v8::Handle<v8::Object> objRef = valRef->ToObject();
#if (SWIG_V8_VERSION < 0x031900)
v8::Handle<v8::Value> cdataRef = objRef->GetInternalField(0);
sobj = static_cast<SwigV8PackedData*>(v8::External::Unwrap(cdataRef));
#else
sobj = static_cast<SwigV8PackedData*>(objRef->GetAlignedPointerFromInternalField(0));
#endif
if (sobj == NULL || sobj->size != size) return 0;
memcpy(ptr, sobj->data, size);
return sobj->type;
} else {
return 0;
}
}
SWIGRUNTIME
int SWIGV8_ConvertPacked(v8::Handle<v8::Value> valRef, void *ptr, size_t sz, swig_type_info *ty) {
swig_type_info *to = SwigV8Packed_UnpackData(valRef, ptr, sz);
if (!to) return SWIG_ERROR;
if (ty) {
if (to != ty) {
/* check type cast? */
swig_cast_info *tc = SWIG_TypeCheck(to->name,ty);
if (!tc) return SWIG_ERROR;
}
}
return SWIG_OK;
}
#if (SWIG_V8_VERSION < 0x031900)
void _wrap_SwigV8PackedData_delete(v8::Persistent< v8::Value > object, void *parameter)
{
SwigV8PackedData *cdata = static_cast<SwigV8PackedData *>(parameter);
#else
void _wrap_SwigV8PackedData_delete(v8::Isolate *isolate, v8::Persistent< v8::Object > * object, SwigV8PackedData *cdata)
{
#endif
delete cdata;
#if (SWIG_V8_VERSION < 0x031900)
object.Clear();
object.Dispose();
#elif (SWIG_V8_VERSION < 0x032100)
object->Dispose(isolate);
#else
object->Dispose();
#endif
}
SWIGRUNTIME
v8::Handle<v8::Value> SWIGV8_NewPackedObj(void *data, size_t size, swig_type_info *type) {
v8::HandleScope scope;
SwigV8PackedData* cdata = new SwigV8PackedData(data, size, type);
v8::Handle<v8::Object> obj = v8::Object::New();
obj->SetHiddenValue(v8::String::New("__swig__packed_data__"), v8::Boolean::New(true));
#if (SWIG_V8_VERSION < 0x031900)
obj->SetPointerInInternalField(0, cdata);
cdata->handle = v8::Persistent<v8::Object>::New(obj);
#else
obj->SetAlignedPointerInInternalField(0, cdata);
cdata->handle.Reset(v8::Isolate::GetCurrent(), obj);
#endif
#if (SWIG_V8_VERSION < 0x031900)
cdata->handle.MakeWeak(cdata, _wrap_SwigV8PackedData_delete);
cdata->handle.MarkIndependent();
#else
cdata->handle.MakeWeak(v8::Isolate::GetCurrent(), cdata, _wrap_SwigV8PackedData_delete);
# if (SWIG_V8_VERSION < 0x032100)
cdata->handle.MarkIndependent(v8::Isolate::GetCurrent());
# else
cdata->handle.MarkIndependent();
# endif
#endif
return scope.Close(obj);
}
#define SWIG_ConvertMember(obj, ptr, sz, ty) SWIGV8_ConvertPacked(obj, ptr, sz, ty)
#define SWIG_NewMemberObj(ptr, sz, type) SWIGV8_NewPackedObj(ptr, sz, type)
/* ---------------------------------------------------------------------------
* Support for IN/OUTPUT typemaps (see Lib/typemaps/inoutlist.swg)
*
* ---------------------------------------------------------------------------*/
SWIGRUNTIME
v8::Handle<v8::Value> SWIGV8_AppendOutput(v8::Handle<v8::Value> result, v8::Handle<v8::Value> obj) {
v8::HandleScope scope;
if (result->IsUndefined()) {
result = v8::Array::New();
}
v8::Handle<v8::Array> arr = v8::Handle<v8::Array>::Cast(result);
arr->Set(arr->Length(), obj);
return scope.Close(arr);
}

View file

@ -0,0 +1,39 @@
/* -----------------------------------------------------------------------------
* javascriptruntime.swg
*
* ----------------------------------------------------------------------------- */
%define %swig_v8_define_version(version)
%insert("runtime") %{
#ifndef SWIG_V8_VERSION
#define SWIG_V8_VERSION version
#endif
%}
%enddef
#ifdef V8_VERSION
%swig_v8_define_version(V8_VERSION)
#else
// HACK: defining a default version
%swig_v8_define_version(0x031110)
#endif
#ifdef BUILDING_NODE_EXTENSION
%insert("runtime") %{
#include <node.h>
%}
#endif
%insert(runtime) %{
#include <v8.h>
#include <errno.h>
#include <limits.h>
#include <stdlib.h>
%}
%insert(runtime) "swigrun.swg"; /* SWIG API */
%insert(runtime) "swigerrors.swg"; /* SWIG errors */
%insert(runtime) "javascriptrun.swg"

View file

@ -0,0 +1,59 @@
/* ------------------------------------------------------------
* utility methods for char strings
* ------------------------------------------------------------ */
%fragment("SWIG_AsCharPtrAndSize", "header", fragment="SWIG_pchar_descriptor") {
SWIGINTERN int
SWIG_AsCharPtrAndSize(v8::Handle<v8::Value> valRef, char** cptr, size_t* psize, int *alloc)
{
if(valRef->IsString()) {
v8::Handle<v8::String> js_str = valRef->ToString();
size_t len = js_str->Utf8Length() + 1;
char* cstr = new char[len];
js_str->WriteUtf8(cstr, len);
if(alloc) *alloc = SWIG_NEWOBJ;
if(psize) *psize = len;
if(cptr) *cptr = cstr;
return SWIG_OK;
} else {
if(valRef->IsObject()) {
v8::Handle<v8::Object> obj = valRef->ToObject();
// try if the object is a wrapped char[]
swig_type_info* pchar_descriptor = SWIG_pchar_descriptor();
if (pchar_descriptor) {
void* vptr = 0;
if (SWIG_ConvertPtr(obj, &vptr, pchar_descriptor, 0) == SWIG_OK) {
if (cptr) *cptr = (char *) vptr;
if (psize) *psize = vptr ? (strlen((char *)vptr) + 1) : 0;
if (alloc) *alloc = SWIG_OLDOBJ;
return SWIG_OK;
}
}
return SWIG_TypeError;
} else {
return SWIG_TypeError;
}
}
}
}
%fragment("SWIG_FromCharPtrAndSize","header",fragment="SWIG_pchar_descriptor") {
SWIGINTERNINLINE v8::Handle<v8::Value>
SWIG_FromCharPtrAndSize(const char* carray, size_t size)
{
if (carray) {
if (size > INT_MAX) {
// TODO: handle extra long strings
return v8::Undefined();
} else {
v8::Handle<v8::String> js_str = v8::String::New(carray, size);
return js_str;
}
} else {
return v8::Undefined();
}
}
}

View file

@ -0,0 +1,43 @@
/* ------------------------------------------------------------
* Typemap specializations for Javascript
* ------------------------------------------------------------ */
/* ------------------------------------------------------------
* Fragment section
* ------------------------------------------------------------ */
/* Include fundamental fragemt definitions */
%include <typemaps/fragments.swg>
/* Look for user fragments file. */
%include <javascriptfragments.swg>
/* Javascript fragments for fundamental types */
%include <javascriptprimtypes.swg>
/* Javascript fragments for char* strings */
%include <javascriptstrings.swg>
/* ------------------------------------------------------------
* Unified typemap section
* ------------------------------------------------------------ */
/* Javascript types */
#define SWIG_Object v8::Handle<v8::Value>
#define VOID_Object v8::Undefined()
/* Overload of the output/constant/exception/dirout handling */
/* append output */
#define SWIG_AppendOutput(result, obj) SWIGV8_AppendOutput(result, obj)
/* set constant */
#define SWIG_SetConstant(name, obj)
/* raise */
#define SWIG_Raise(obj, type, desc) SWIG_V8_Raise(type)
/* Include the unified typemap library */
%include <typemaps/swigtypemaps.swg>

12
Lib/javascript/v8/node.i Normal file
View file

@ -0,0 +1,12 @@
%insert("begin") %{
#ifndef BUILDING_NODE_EXTENSION
#define BUILDING_NODE_EXTENSION
#endif
%}
%insert("runtime") %{
// we are including relative to the src folder because of issues
// with other files which might be named "node.h"
#include <src/node.h>
%}

5
Lib/javascript/v8/std_common.i Executable file
View file

@ -0,0 +1,5 @@
%include <std_except.i>
%apply size_t { std::size_t };
%apply const size_t& { const std::size_t& };

View file

@ -0,0 +1,19 @@
/*
* STD C++ complex typemaps
*/
%include <javascriptcomplex.swg>
%{
#include <complex>
%}
/* defining the complex as/from converters */
%swig_cplxdbl_convn(std::complex<double>, std::complex<double>, std::real, std::imag)
%swig_cplxflt_convn(std::complex<float>, std::complex<float>, std::real, std::imag)
/* defining the typemaps */
%typemaps_primitive(%checkcode(CPLXDBL), std::complex<double>);
%typemaps_primitive(%checkcode(CPLXFLT), std::complex<float>);

View file

@ -0,0 +1 @@
%include <std/_std_deque.i>

View file

@ -0,0 +1 @@
%include <typemaps/std_except.swg>

74
Lib/javascript/v8/std_map.i Executable file
View file

@ -0,0 +1,74 @@
/* -----------------------------------------------------------------------------
* std_map.i
*
* SWIG typemaps for std::map
* ----------------------------------------------------------------------------- */
%include <std_common.i>
// ------------------------------------------------------------------------
// std::map
// ------------------------------------------------------------------------
%{
#include <map>
#include <algorithm>
#include <stdexcept>
%}
// exported class
namespace std {
template<class K, class T> class map {
// add typemaps here
public:
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef K key_type;
typedef T mapped_type;
map();
map(const map<K,T> &);
unsigned int size() const;
bool empty() const;
void clear();
%extend {
const T& get(const K& key) throw (std::out_of_range) {
std::map<K,T >::iterator i = self->find(key);
if (i != self->end())
return i->second;
else
throw std::out_of_range("key not found");
}
void set(const K& key, const T& x) {
(*self)[key] = x;
}
void del(const K& key) throw (std::out_of_range) {
std::map<K,T >::iterator i = self->find(key);
if (i != self->end())
self->erase(i);
else
throw std::out_of_range("key not found");
}
bool has_key(const K& key) {
std::map<K,T >::iterator i = self->find(key);
return i != self->end();
}
}
};
// Legacy macros (deprecated)
%define specialize_std_map_on_key(K,CHECK,CONVERT_FROM,CONVERT_TO)
#warning "specialize_std_map_on_key ignored - macro is deprecated and no longer necessary"
%enddef
%define specialize_std_map_on_value(T,CHECK,CONVERT_FROM,CONVERT_TO)
#warning "specialize_std_map_on_value ignored - macro is deprecated and no longer necessary"
%enddef
%define specialize_std_map_on_both(K,CHECK_K,CONVERT_K_FROM,CONVERT_K_TO, T,CHECK_T,CONVERT_T_FROM,CONVERT_T_TO)
#warning "specialize_std_map_on_both ignored - macro is deprecated and no longer necessary"
%enddef
}

33
Lib/javascript/v8/std_pair.i Executable file
View file

@ -0,0 +1,33 @@
/* -----------------------------------------------------------------------------
* std_pair.i
*
* SWIG typemaps for std::pair
* ----------------------------------------------------------------------------- */
%include <std_common.i>
// ------------------------------------------------------------------------
// std::pair
// ------------------------------------------------------------------------
%{
#include <utility>
%}
namespace std {
template<class T, class U> struct pair {
pair();
pair(T first, U second);
pair(const pair& p);
template <class U1, class U2> pair(const pair<U1, U2> &p);
T first;
U second;
};
// add specializations here
}

79
Lib/javascript/v8/std_string.i Executable file
View file

@ -0,0 +1,79 @@
/* -----------------------------------------------------------------------------
* std_string.i
*
* Typemaps for std::string and const std::string&.
*
* To use non-const std::string references use the following %apply:
* %apply const std::string & {std::string &};
*
* ----------------------------------------------------------------------------- */
%{
#include <string>
%}
%fragment("SWIGV8_valueToString", "header", fragment="SWIG_AsCharPtrAndSize") {
std::string* SWIGV8_valueToStringPtr(v8::Handle<v8::Value> val) {
if (!val->IsString()) return 0;
int alloc;
size_t size;
char* chars;
int res = SWIG_AsCharPtrAndSize(val, &chars, &size, &alloc);
if(res != SWIG_OK) {
v8::ThrowException(v8::Exception::TypeError(v8::String::New("Could not convert to string.")));
return 0;
}
// copies the data (again)
std::string *str = new std::string(chars);
if (alloc) delete[] chars;
return str;
}
}
%fragment("SWIGV8_stringToValue", "header", fragment="SWIG_FromCharPtrAndSize") {
v8::Handle<v8::Value> SWIGV8_stringToValue(const std::string &str) {
return SWIG_FromCharPtrAndSize(str.c_str(), str.length());
}
}
namespace std {
%naturalvar string;
class string;
%typemap(in, fragment="SWIGV8_valueToString") string (std::string* tmp)
%{
tmp = SWIGV8_valueToStringPtr($input);
$1 = *tmp;
if (tmp == 0) { v8::ThrowException(v8::Exception::TypeError(v8::String::New("Null pointer."))); goto fail; }
if (tmp) delete tmp;
%}
%typemap(in, fragment="SWIGV8_valueToString") const string &
%{
$1 = SWIGV8_valueToStringPtr($input);
if ($1 == 0) { v8::ThrowException(v8::Exception::TypeError(v8::String::New("Null pointer."))); goto fail; }
%}
%typemap(freearg) const string &
%{
if ($1) delete $1;
%}
%typemap(out, fragment="SWIGV8_stringToValue") string
%{
$result = SWIGV8_stringToValue($1);
%}
%typemap(out, fragment="SWIGV8_stringToValue") const string &
%{
$result = SWIGV8_stringToValue(*$1);
%}
}

85
Lib/javascript/v8/std_vector.i Executable file
View file

@ -0,0 +1,85 @@
/* -----------------------------------------------------------------------------
* std_vector.i
* ----------------------------------------------------------------------------- */
%include <std_common.i>
%{
#include <vector>
#include <stdexcept>
%}
namespace std {
template<class T> class vector {
public:
typedef size_t size_type;
typedef T value_type;
typedef const value_type& const_reference;
vector();
vector(size_type n);
size_type size() const;
size_type capacity() const;
void reserve(size_type n);
%rename(isEmpty) empty;
bool empty() const;
void clear();
%rename(add) push_back;
void push_back(const value_type& x);
%extend {
const_reference get(int i) throw (std::out_of_range) {
int size = int(self->size());
if (i>=0 && i<size)
return (*self)[i];
else
throw std::out_of_range("vector index out of range");
}
void set(int i, const value_type& val) throw (std::out_of_range) {
int size = int(self->size());
if (i>=0 && i<size)
(*self)[i] = val;
else
throw std::out_of_range("vector index out of range");
}
}
};
// bool specialization
template<> class vector<bool> {
public:
typedef size_t size_type;
typedef bool value_type;
typedef bool const_reference;
vector();
vector(size_type n);
size_type size() const;
size_type capacity() const;
void reserve(size_type n);
%rename(isEmpty) empty;
bool empty() const;
void clear();
%rename(add) push_back;
void push_back(const value_type& x);
%extend {
const_reference get(int i) throw (std::out_of_range) {
int size = int(self->size());
if (i>=0 && i<size)
return (*self)[i];
else
throw std::out_of_range("vector index out of range");
}
void set(int i, const value_type& val) throw (std::out_of_range) {
int size = int(self->size());
if (i>=0 && i<size)
(*self)[i] = val;
else
throw std::out_of_range("vector index out of range");
}
}
};
}
%define specialize_std_vector(T)
#warning "specialize_std_vector - specialization for type T no longer needed"
%enddef

10
Lib/javascript/v8/stl.i Executable file
View file

@ -0,0 +1,10 @@
/* -----------------------------------------------------------------------------
* stl.i
* ----------------------------------------------------------------------------- */
%include <std_common.i>
%include <std_string.i>
%include <std_vector.i>
%include <std_map.i>
%include <std_pair.i>

View file

@ -0,0 +1,148 @@
/* -----------------------------------------------------------------------------
* typemaps.i
*
* Pointer handling
* These mappings provide support for input/output arguments and common
* uses for C/C++ pointers.
* ----------------------------------------------------------------------------- */
// INPUT typemaps.
// These remap a C pointer to be an "INPUT" value which is passed by value
// instead of reference.
/*
The following methods can be applied to turn a pointer into a simple
"input" value. That is, instead of passing a pointer to an object,
you would use a real value instead.
int *INPUT
short *INPUT
long *INPUT
long long *INPUT
unsigned int *INPUT
unsigned short *INPUT
unsigned long *INPUT
unsigned long long *INPUT
unsigned char *INPUT
bool *INPUT
float *INPUT
double *INPUT
To use these, suppose you had a C function like this :
double fadd(double *a, double *b) {
return *a+*b;
}
You could wrap it with SWIG as follows :
%include <typemaps.i>
double fadd(double *INPUT, double *INPUT);
or you can use the %apply directive :
%include <typemaps.i>
%apply double *INPUT { double *a, double *b };
double fadd(double *a, double *b);
*/
// OUTPUT typemaps. These typemaps are used for parameters that
// are output only. The output value is appended to the result as
// a list element.
/*
The following methods can be applied to turn a pointer into an "output"
value. When calling a function, no input value would be given for
a parameter, but an output value would be returned. In the case of
multiple output values, they are returned in the form of a Python tuple.
int *OUTPUT
short *OUTPUT
long *OUTPUT
long long *OUTPUT
unsigned int *OUTPUT
unsigned short *OUTPUT
unsigned long *OUTPUT
unsigned long long *OUTPUT
unsigned char *OUTPUT
bool *OUTPUT
float *OUTPUT
double *OUTPUT
For example, suppose you were trying to wrap the modf() function in the
C math library which splits x into integral and fractional parts (and
returns the integer part in one of its parameters).K:
double modf(double x, double *ip);
You could wrap it with SWIG as follows :
%include <typemaps.i>
double modf(double x, double *OUTPUT);
or you can use the %apply directive :
%include <typemaps.i>
%apply double *OUTPUT { double *ip };
double modf(double x, double *ip);
The Python output of the function would be a tuple containing both
output values.
*/
// INOUT
// Mappings for an argument that is both an input and output
// parameter
/*
The following methods can be applied to make a function parameter both
an input and output value. This combines the behavior of both the
"INPUT" and "OUTPUT" methods described earlier. Output values are
returned in the form of a Python tuple.
int *INOUT
short *INOUT
long *INOUT
long long *INOUT
unsigned int *INOUT
unsigned short *INOUT
unsigned long *INOUT
unsigned long long *INOUT
unsigned char *INOUT
bool *INOUT
float *INOUT
double *INOUT
For example, suppose you were trying to wrap the following function :
void neg(double *x) {
*x = -(*x);
}
You could wrap it with SWIG as follows :
%include <typemaps.i>
void neg(double *INOUT);
or you can use the %apply directive :
%include <typemaps.i>
%apply double *INOUT { double *x };
void neg(double *x);
Unlike C, this mapping does not directly modify the input value (since
this makes no sense in Python). Rather, the modified input value shows
up as the return value of the function. Thus, to apply this function
to a Python variable you might do this :
x = neg(x)
Note : previous versions of SWIG used the symbol 'BOTH' to mark
input/output arguments. This is still supported, but will be slowly
phased out in future releases.
*/
%include <typemaps/typemaps.swg>

View file

@ -493,7 +493,7 @@
* --- String fragment methods ---
* ------------------------------------------------------------ */
#ifndef %_typemap2_string
%define %_typemap2_string(StringCode, CharCode,
Char, CharName,
SWIG_AsCharPtrAndSize,
@ -593,7 +593,7 @@ SWIG_AsVal_dec(Char)(SWIG_Object obj, Char *val)
SWIG_DeleteCharArray)
%enddef
#endif
/* ------------------------------------------------------------
* String typemaps and fragments, with default allocators