#262 Octave global load behaviour / "cvar" name

closed-accepted
None
5
2011-05-21
2011-03-23
Karl Wette
No

This patch (against SWIG v2.0.2) is intended to address some small deficiencies
in the Octave language module:

1) It's currently not possible to tell SWIG whether or not symbols should be
loaded into the global namespace by default. In fact, it's not even possible
to make the generated .oct module *not* load all symbols globally, due to a
bug in Lib/octave/octruntime.swg (line 37: "noglobal" should be "global").

2) It's not possible to change the name of the symbol used to access
global variables/constants from the default "cvar".

This patch adds 3 Octave-specific command-line options:

* -global/-noglobal tell SWIG whether the generated .oct module should load
symbols into the global namespace by default. The default option is -global to
preserve existing behaviour.

* -globals <name> sets the name of the symbol used to access global
variables/constants. It is set to "cvar" by default.

These options are parsed by OCTAVE::main() in Source/Modules/octave.cxx, which
sets global variables global_load (bool) and global_name (String*). In
OCTAVE::top() these variables are output to the generated wrapping code as
#defines SWIG_global_load and SWIG_global_name.

In Lib/octave/octruntime.swg, the %insert(initbeforefunc) block containing the
Octave entry point function DEFUN_DLD now contains a expanded input argument
parser, which uses the same command-line arguments (-global/-noglobal, -globals)
as can be passed to SWIG itself. In this way, the user loading the .oct module
can change any of the global options the module was originally compiled
with. The parser checks for non-string and unrecognised arguments, and also
checks that global_name is a valid Octave identifier. A -help option is also
included, which generated a short usage message.

Example usage using an example module:

------------------------------
octave:1> example -help
usage: example [-global|-noglobal] [-globals <name>]
octave:2> example(4)
error: example: unrecognised non-string argument.
usage: example [-global|-noglobal] [-globals <name>]
octave:3> example 4
error: example: unrecognised argument '4'.
usage: example [-global|-noglobal] [-globals <name>]
octave:4> example
octave:5> example
example =
{
cvar
somefunc (global method)
}
octave:6> somefunc
Not implemented :)
ans = 0
octave:7> cvar.somevar
ans = 12345
------------------------------

and

------------------------------
octave:1> example -noglobal -globals mycvar
octave:2> somefunc()
error: `somefunc' undefined near line 2 column 1
octave:2> example.somefunc()
Not implemented :)
ans = 0
octave:3> example.mycvar.somevar
ans = 12345
------------------------------

This patch does break the current Octave module behaviour as described in
section 29.3.1 of the SWIG 2.0 documentation. However, since the "noglobal"
option (which does not seem to be mentioned in the documentation) is currently
broken anyway, hopefully this is not a major issue :)

I would be very happy to discuss any issues with / improvements to this patch.

Cheers,
Karl

Discussion

  • Karl Wette
    Karl Wette
    2011-03-23

    Patch against SWIG v2.0.2

     
  • William Fulton
    William Fulton
    2011-05-21

    Patch applied by Xavier for swig-2.0.4, thanks.

     
  • William Fulton
    William Fulton
    2011-05-21

    • assigned_to: nobody --> xavier98
    • status: open --> closed-accepted