diff --git a/CHANGES.current b/CHANGES.current index 18ab3ba45..8ab562781 100644 --- a/CHANGES.current +++ b/CHANGES.current @@ -5,6 +5,11 @@ See the RELEASENOTES file for a summary of changes in each release. Version 3.0.9 (in progress) =========================== +2016-04-06: smarchetto + [Scilab] #552 Make Scilab runtime keep track of pointer types + Instead of a Scilab pointer which has no type, SWIG Scilab maps a + pointer to a structure tlist containing the pointer adress and its type. + 2016-04-02: ahnolds [Python] Apply #598. Fix misleading error message when attempting to read a non-existent attribute. The previous cryptic error message: @@ -13,7 +18,7 @@ Version 3.0.9 (in progress) AttributeError: 'Foo' object has no attribute 'bar' 2016-04-02: derkuci - [Python] Patch #610 to fix #607. + [Python] Patch #610 to fix #607. Fix single arguments when using python -builtin -O with %feature("compactdefaultargs") 2016-03-31: wsfulton @@ -79,7 +84,7 @@ Version 3.0.9 (in progress) strips the symbol's suffix instead of the prefix. The example below will rename SomeThingCls to SomeThing and AnotherThingCls to AnotherThing: - %rename("%(rstrip:[Cls])s") ""; + %rename("%(rstrip:[Cls])s") ""; class SomeThingCls {}; struct AnotherThingCls {}; diff --git a/Doc/Manual/Scilab.html b/Doc/Manual/Scilab.html index 3e9e1c1a2..aa4a3e99f 100644 --- a/Doc/Manual/Scilab.html +++ b/Doc/Manual/Scilab.html @@ -45,6 +45,7 @@
  • Structures
  • C++ classes
  • C++ inheritance +
  • C++ overloading
  • Pointers, references, values, and arrays
  • C++ templates
  • C++ operators @@ -752,11 +753,25 @@ typedef enum { RED, BLUE, GREEN } color;

    -C/C++ pointers are fully supported by SWIG. They are mapped to the Scilab pointer type ("pointer", type ID: 128). +Pointers are supported by SWIG. A pointer can be returned from a wrapped C/C++ function, stored in a Scilab variable, and used in input argument of another C/C++ function. +

    +

    +Also, thanks to the SWIG runtime which stores informations about types, pointer types are tracked between exchanges Scilab and the native code. Indeed pointer types are stored alongside the pointer adress. +A pointer is mapped to a Scilab structure (tlist), which contains as fields the pointer address and the pointer type (in fact a pointer to the type information structure in the SWIG runtime). +
    +Why a native pointer is not mapped to a Scilab pointer (type name: "pointer", type ID: 128) ? The big advantage of mapping to a tlist is that it exposes a new type for the pointer in Scilab, type which can be acessed in Scilab with the typeof function, and manipulated using the overloading mechanism.

    -Given a wrapping of some of the C file functions: +Notes: +

    +

    + +

    +Following is an example of the wrapping of the C FILE* pointer:

    @@ -772,20 +787,26 @@ int fclose(FILE *);
     

    -These functions can be used in a natural way from Scilab: +These functions can be used the same way as in C from Scilab:

    +--> example_Init();
    +
     --> f = fopen("junk", "w");
     --> typeof(f)
      ans  =
     
    -  pointer
    +  _p_FILE
     
     --> fputs("Hello World", f);
     --> fclose(f);
     
    +

    +Note: the type name _p_FILE which means "pointer to FILE". +

    +

    The user of a pointer is responsible for freeing it or, like in the example, closing any resources associated with it (just as is required in a C program).

    @@ -794,7 +815,7 @@ The user of a pointer is responsible for freeing it or, like in the example, clo

    -Most of time pointer manipulation is not needed in a scripting language such as Scilab. +As a scripting language, Scilab does not provide functions to manipulate pointers. However, in some cases it can be useful, such as for testing or debugging.

    @@ -806,7 +827,11 @@ SWIG comes with two pointer utility functions:
  • SWIG_ptr(): creates a pointer from an address value
  • -

    Following illustrates their use on the last example:

    +

    +Note: a pointer created by SWIG_ptr() does not have any type and is mapped as a Scilab pointer. +

    + +

    Following we use the utility functions on the previous example:

     --> f = fopen("junk", "w");
    @@ -817,15 +842,20 @@ SWIG comes with two pointer utility functions:
         8219088.
     
     --> p = SWIG_ptr(addr);
    +--> typeof(p)
    +ans  =
    +
    +  pointer
    +
     --> fputs(" World", p);
     --> fclose(f);
     
    -

    39.3.6.2 Null pointers

    +

    39.3.6.2 Null pointers:

    -

    By default, Scilab does not provide a way to test or create null pointers. -But it is possible to have a null pointer by using the previous functions SWIG_this() and SWIG_ptr(), like this: +

    +Using the previous SWIG_this() and SWIG_ptr(), it is possible to create and check null pointers:

    @@ -873,7 +903,7 @@ Several functions are generated:
     
  • a destructor function delete_Foo() to release the struct pointer.
  • -

    + Usage example:

    @@ -931,6 +961,19 @@ ans = 20.
    +

    +Note: the pointer to the struct works as described in Pointers. For example, the type of the struct pointer can be get with typeof, as following: +

    + +

    +--> example_Init();
    +--> b = new_Bar();
    +--> typeof(b)
    + ans  =
    +
    +    _p_Bar
    +--> delete_Bar(b);
    +

    39.3.8 C++ classes

    @@ -982,6 +1025,24 @@ ans = --> delete_Point(p2); +

    +Note: like structs, class pointers are mapped as described in Pointers. Let's give an example which shows that each class pointer type is a new type in Scilab that can be used for example (through overloading) to implement a custom print for the Point class: +

    + +

    +--> function %_p_Point_p(p)
    +-->     mprintf('[%d, %d]\n', Point_x_get(p), Point_y_get(p));
    +--> endfunction
    +
    +--> example_Init();
    +--> p = new_Point(1, 2)
    + p  =
    +
    +[1, 2]
    +
    +--> delete_Point(p);
    +
    +

    39.3.9 C++ inheritance

    @@ -1057,7 +1118,49 @@ But we can use either use the get_perimeter() function of the parent cl 18.84 -

    39.3.10 Pointers, references, values, and arrays

    +

    39.3.10 C++ overloading

    + +

    +As explained in 6.15 SWIG provides support for overloaded functions and constructors. +

    + +

    As SWIG knows pointer types, the overloading works also with pointer types, here is is an example with a function magnify overloaded for the previous classes Shape and Circle: + +

    +

    +%module example
    +
    +void magnify(Square *square, double factor) {
    +    square->size *= factor;
    +};
    +
    +void magnify(Circle *circle, double factor) {
    +    square->radius *= factor;
    +};
    +
    +

    + +

    +

    +--> example_Init();
    +--> c = new_Circle(3);
    +--> s = new_Square(2);
    +
    +--> magnify(c, 10);
    +--> Circle_get_radius(c)
    + ans  =
    +
    +   30;
    +--> magnify(s, 10);
    +--> Square_get_size(s)
    + ans  =
    +
    +   20;
    +
    +

    + + +

    39.3.11 Pointers, references, values, and arrays

    @@ -1115,7 +1218,7 @@ All these functions will return a pointer to an instance of Foo. As the function spam7 returns a value, new instance of Foo has to be allocated, and a pointer on this instance is returned.

    -

    39.3.11 C++ templates

    +

    39.3.12 C++ templates

    @@ -1123,8 +1226,7 @@ As in other languages, function and class templates are supported in SWIG Scilab

    -You have to tell SWIG to create wrappers for a particular -template instantiation. The %template directive is used for this purpose. +You have to tell SWIG to create wrappers for a particular template instantiation. The %template directive is used for this purpose. For example:

    @@ -1175,7 +1277,7 @@ Then in Scilab: More details on template support can be found in the templates documentation.

    -

    39.3.12 C++ operators

    +

    39.3.13 C++ operators

    @@ -1228,7 +1330,7 @@ private: -

    39.3.13 C++ namespaces

    +

    39.3.14 C++ namespaces

    @@ -1306,7 +1408,7 @@ Note: the nspace feature is not supp

    -

    39.3.14 C++ exceptions

    +

    39.3.15 C++ exceptions

    @@ -1389,7 +1491,7 @@ More complex or custom exception types require specific exception typemaps to be See the SWIG C++ documentation for more details.

    -

    39.3.15 C++ STL

    +

    39.3.16 C++ STL

    @@ -1448,15 +1550,9 @@ The default behaviour is for SWIG to generate code that will give a runtime erro -

    39.4.2 Default type mappings for non-primitive types

    -

    -The default mapped type for C/C++ non-primitive types is the Scilab pointer, for example for C structs, C++ classes, etc... -

    - - -

    39.4.3 Arrays

    +

    39.4.2 Arrays

    @@ -1511,7 +1607,7 @@ void printArray(int values[], int len) { [ 0 1 2 3 ] -

    39.4.4 Pointer-to-pointers

    +

    39.4.3 Pointer-to-pointers

    @@ -1584,7 +1680,7 @@ void print_matrix(double **M, int nbRows, int nbCols) { -

    39.4.5 Matrices

    +

    39.4.4 Matrices

    @@ -1677,7 +1773,7 @@ The remarks made earlier for arrays also apply here:

  • There is no control while converting double values to integers, double values are truncated without any checking or warning.
  • -

    39.4.6 STL

    +

    39.4.5 STL

    @@ -1884,8 +1980,8 @@ ans = The wrapped module contains an initialization function to:

    diff --git a/Lib/scilab/scirun.swg b/Lib/scilab/scirun.swg index c59ad61dd..5625b5298 100644 --- a/Lib/scilab/scirun.swg +++ b/Lib/scilab/scirun.swg @@ -245,7 +245,7 @@ SwigScilabPtrFromObject(void *pvApiCtx, int iVarOut, void *pvObj, swig_type_info return SWIG_ERROR; } - pstString = SWIG_TypePrettyName(descriptor); + pstString = SWIG_TypeName(descriptor); sciErr = createMatrixOfStringInList(pvApiCtx, SWIG_NbInputArgument(pvApiCtx) + iVarOut, piTListAddr, 1, 1, 1, &pstString); if (sciErr.iErr) { printError(&sciErr, 0);