import source from 1.3.40
[external/swig.git] / Lib / octave / octruntime.swg
1 %insert(runtime) %{
2 #include <octave/oct.h>
3 #include <octave/parse.h>
4 #include <octave/ov-fcn-handle.h>
5 #include <octave/Cell.h>
6 #include <octave/oct-map.h>
7 %}
8
9 %insert(runtime) "swigrun.swg";
10 %insert(runtime) "swigerrors.swg";
11 %insert(runtime) "octrun.swg";
12
13 %insert(initbeforefunc) "swiginit.swg"
14
15 %insert(initbeforefunc) %{
16
17 static void SWIG_init_user(octave_swig_type* module_ns);
18
19 DEFUN_DLD (SWIG_name,args,nargout,SWIG_name_d) {
20   static bool already_init=false;
21   if (already_init)
22     return octave_value_list();
23   already_init=true;
24
25   octave_swig_ref::register_type();
26   octave_swig_packed::register_type();
27   SWIG_InitializeModule(0);
28   SWIG_PropagateClientData();
29   
30   install_builtin_function(swig_type,"swig_type",std::string());
31   install_builtin_function(swig_typequery,"swig_typequery",std::string());
32   install_builtin_function(swig_this,"swig_this",std::string());
33   install_builtin_function(swig_subclass,"subclass",std::string());
34
35   bool global_option=true; // * swig cli option should control this default
36   for (int j=0;j<args.length();++j)
37     if (args(j).is_string()&&args(j).string_value()=="noglobal")
38       global_option=true;
39     else if (args(j).is_string()&&args(j).string_value()=="noglobal")
40       global_option=false;
41
42   octave_swig_type* cvar_ns=new octave_swig_type;
43   for (int j=0;swig_globals[j].name;++j)
44     if (swig_globals[j].get_method)
45       cvar_ns->assign(swig_globals[j].name,&swig_globals[j]);
46
47   octave_swig_type* module_ns=new octave_swig_type(0, 0, 0, true);
48   module_ns->assign("cvar",Swig::swig_value_ref(cvar_ns));
49   for (int j=0;swig_globals[j].name;++j)
50     if (swig_globals[j].method)
51       module_ns->assign(swig_globals[j].name,&swig_globals[j]);
52
53   // * need better solution here; swig_type -> octave_class mapping is 
54   // * really n-to-1, in some cases such as template partial spec, etc. 
55   // * see failing tests.
56   for (int j=0;swig_types[j];++j)
57     if (swig_types[j]->clientdata) {
58       swig_octave_class* c=(swig_octave_class*)swig_types[j]->clientdata;
59       module_ns->assign(c->name,
60                         Swig::swig_value_ref
61                         (new octave_swig_type(0,swig_types[j])));
62     }
63
64   SWIG_init_user(module_ns);
65
66   SWIG_InstallOps(octave_swig_ref::static_type_id());
67
68   // the incref is necessary so install_global doesn't destroy module_ns,
69   // as it would if it installed something with the same name as the module.
70   module_ns->incref();
71   if (global_option)
72     module_ns->install_global();
73   module_ns->decref();
74
75 #if USE_OCTAVE_API_VERSION<37
76   link_to_global_variable(curr_sym_tab->lookup(SWIG_name_d,true));
77 #else
78   symbol_table::varref(SWIG_name_d);
79   symbol_table::mark_global(SWIG_name_d);
80 #endif
81   set_global_value(SWIG_name_d,Swig::swig_value_ref(module_ns));
82
83 #if USE_OCTAVE_API_VERSION>=37
84   mlock();
85 #endif
86
87   return octave_value_list();
88 }
89
90 // workaround bug in octave where installing global variable of custom type and then
91 // exiting without explicitly clearing the variable causes octave to segfault.
92 #if USE_OCTAVE_API_VERSION>=37
93 struct oct_file_unload {
94   ~oct_file_unload() {
95     string_vector vars = symbol_table::global_variable_names();
96     for (int i = 0; i < vars.length(); i++)
97       symbol_table::clear_global(vars[i]);
98   }
99 };
100 static oct_file_unload __unload;
101 #endif
102
103 %}
104