]> git.mxchange.org Git - flightgear.git/blob - src/Scripting/NasalCanvas.cxx
dba2851476b818302d2608296f97865d270e29a8
[flightgear.git] / src / Scripting / NasalCanvas.cxx
1 // NasalCanvas.cxx -- expose Canvas classes to Nasal
2 //
3 // Written by James Turner, started 2012.
4 //
5 // Copyright (C) 2012 James Turner
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20
21 #ifdef HAVE_CONFIG_H
22 #  include "config.h"
23 #endif
24
25 #include <memory>
26 #include <string.h>
27
28 #include "NasalCanvas.hxx"
29 #include <Canvas/canvas_mgr.hxx>
30 #include <Main/globals.hxx>
31
32 //#include <boost/python.hpp>
33 #include <boost/foreach.hpp>
34 #include <boost/function.hpp>
35 #include <boost/utility/enable_if.hpp>
36 #include <boost/algorithm/string/case_conv.hpp>
37 #include <boost/make_shared.hpp>
38 #include <osgGA/GUIEventAdapter>
39
40 #include <simgear/sg_inlines.h>
41
42 #include <simgear/canvas/Canvas.hxx>
43 #include <simgear/canvas/elements/CanvasElement.hxx>
44
45 static naRef elementPrototype;
46 static naRef propsNodePrototype;
47
48 extern naRef propNodeGhostCreate(naContext c, SGPropertyNode* n);
49
50 static void hashset(naContext c, naRef hash, const char* key, naRef val)
51 {
52   naRef s = naNewString(c);
53   naStr_fromdata(s, (char*)key, strlen(key));
54   naHash_set(hash, s, val);
55 }
56
57 template<class T>
58 typename boost::enable_if<boost::is_arithmetic<T>, naRef>::type
59 convertToNasal(T val)
60 {
61   return naNum(val);
62 }
63
64 //void initCanvasPython()
65 //{
66 //  using namespace boost::python;
67 //  class_<simgear::canvas::Canvas>("Canvas");
68 //}
69
70 namespace sc = simgear::canvas;
71
72 naRef canvasGetTexture(naContext c, sc::Canvas* canvas)
73 {
74   //{ parents : [Node], _g : node }
75   naRef parents = naNewVector(c);
76   naVec_append(parents, propsNodePrototype);
77
78   naRef node = naNewHash(c);
79   hashset(c, node, "parents", parents);
80   hashset(c, node, "_g", propNodeGhostCreate(c, canvas->getProps()));
81
82   return node;
83 }
84
85 namespace nasal
86 {
87
88   template<class T>
89   struct class_traits
90   {
91     typedef boost::false_type::type is_shared;
92     typedef T raw_type;
93   };
94
95   template<class T>
96   struct class_traits<boost::shared_ptr<T> >
97   {
98     typedef boost::true_type::type is_shared;
99     typedef T raw_type;
100   };
101
102   template<class T>
103   struct class_traits<osg::ref_ptr<T> >
104   {
105     typedef boost::true_type::type is_shared;
106     typedef T raw_type;
107   };
108
109   template<class T>
110   struct class_traits<SGSharedPtr<T> >
111   {
112     typedef boost::true_type::type is_shared;
113     typedef T raw_type;
114   };
115
116   template<class T>
117   struct SharedPointerPolicy
118   {
119     typedef typename class_traits<T>::raw_type raw_type;
120
121     /**
122      * Create a shared pointer on the heap to handle the reference counting for
123      * the passed shared pointer while it is used in Nasal space.
124      */
125     static T* createInstance(const T& ptr)
126     {
127       return new T(ptr);
128     }
129
130     static raw_type* getRawPtr(void* ptr)
131     {
132       return static_cast<T*>(ptr)->get();
133     }
134   };
135
136   template<class T>
137   struct RawPointerPolicy
138   {
139     typedef typename class_traits<T>::raw_type raw_type;
140
141     static T* createInstance()
142     {
143       return new T();
144     }
145
146     static raw_type* getRawPtr(void* ptr)
147     {
148       BOOST_STATIC_ASSERT((boost::is_same<raw_type, T>::value));
149       return static_cast<T*>(ptr);
150     }
151   };
152
153   class class_metadata
154   {
155     public:
156       void addNasalBase(const naRef& parent)
157       {
158         assert( naIsHash(parent) );
159         _parents.push_back(parent);
160       }
161
162     protected:
163       const std::string             _name;
164       naGhostType                   _ghost_type;
165       std::vector<class_metadata*>  _base_classes;
166       std::vector<naRef>            _parents;
167
168       explicit class_metadata(const std::string& name):
169         _name(name)
170       {
171
172       }
173
174       void addBaseClass(class_metadata* base)
175       {
176         assert(base);
177         std::cout << _name << ": addBase(" << base->_name << ")" << std::endl;
178         _base_classes.push_back(base);
179       }
180
181       naRef getParents(naContext c)
182       {
183         naRef parents = naNewVector(c);
184         for(size_t i = 0; i < _parents.size(); ++i)
185           naVec_append(parents, _parents[i]);
186         return parents;
187       }
188   };
189
190   /**
191    * Class for exposing C++ objects to Nasal
192    */
193   template<class T>
194   class class_:
195     public class_metadata,
196     protected boost::mpl::if_< typename class_traits<T>::is_shared,
197                                SharedPointerPolicy<T>,
198                                RawPointerPolicy<T> >::type
199   {
200     private:
201       typedef typename class_traits<T>::raw_type raw_type;
202
203     public:
204
205       typedef boost::function<naRef(naContext c, raw_type*)> getter_t;
206       typedef std::map<std::string, getter_t> GetterMap;
207
208       static class_& init(const std::string& name)
209       {
210         getSingletonHolder().reset( new class_(name) );
211         return *getSingletonPtr();
212       }
213
214       static naRef f_create(naContext c, naRef me, int argc, naRef* args)
215       {
216         return create(c);
217       }
218
219       static naRef create( naContext c )
220       {
221         return makeGhost(c, class_::createInstance());
222       }
223
224       // TODO use variadic template when supporting C++11
225       template<class A1>
226       static naRef create( naContext c, const A1& a1 )
227       {
228         return makeGhost(c, class_::createInstance(a1));
229       }
230
231       class_& bases(const naRef& parent)
232       {
233         addNasalBase(parent);
234         return *this;
235       }
236
237       template<class Base>
238       typename boost::enable_if
239         < boost::is_convertible<Base, class_metadata>,
240           class_
241         >::type&
242       bases()
243       {
244         Base* base = Base::getSingletonPtr();
245         addBaseClass( base );
246
247         // Replace any getter that is not available in the current class.
248         // TODO check if this is the correct behavior of function overriding
249         const typename Base::GetterMap& base_getter = base->getGetter();
250         for( typename Base::GetterMap::const_iterator getter =
251                base_getter.begin();
252                getter != base_getter.end();
253              ++getter )
254         {
255           if( _getter.find(getter->first) == _getter.end() )
256             _getter.insert( *getter );
257         }
258
259         return *this;
260       }
261
262       template<class Type>
263       typename boost::enable_if_c
264         < !boost::is_convertible< Type, class_metadata>::value,
265           class_
266         >::type&
267       bases()
268       {
269         return bases< class_<Type> >();
270       }
271
272       template<class Var>
273       class_& def( const std::string& field,
274                    Var (raw_type::*getter)() const )
275       {
276         _getter[field] = boost::bind( &convertToNasal<Var>,
277                                       boost::bind(getter, _2) );
278         return *this;
279       }
280
281       class_& def_readonly( const std::string& field,
282                             getter_t getter )
283       {
284         _getter[field] = getter;
285         return *this;
286       }
287
288       /**
289        * Data member
290        */
291       template<class Var>
292       class_& def_readonly( const std::string& field,
293                             Var raw_type::*var )
294       {
295         _getter[field] = boost::bind( &convertToNasal<Var>,
296                                       boost::bind(var, _2) );
297         return *this;
298       }
299
300       static class_* getSingletonPtr()
301       {
302         return getSingletonHolder().get();
303       }
304
305       const GetterMap& getGetter() const
306       {
307         return _getter;
308       }
309
310     private:
311
312       typedef std::auto_ptr<class_> class_ptr;
313       GetterMap _getter;
314
315       explicit class_(const std::string& name):
316         class_metadata( name )
317       {
318         _ghost_type.destroy = &destroyGhost;
319         _ghost_type.name = _name.c_str();
320         _ghost_type.get_member = &getMember;
321         _ghost_type.set_member = 0;
322       }
323
324       static class_ptr& getSingletonHolder()
325       {
326         static class_ptr instance;
327         return instance;
328       }
329
330       static naRef makeGhost(naContext c, void *ptr)
331       {
332         std::cout << "makeGhost    " << ptr << std::endl;
333         return naNewGhost2(c, &getSingletonPtr()->_ghost_type, ptr);
334       }
335
336       static void destroyGhost(void *ptr)
337       {
338         std::cout << "destroyGhost " << ptr << std::endl;
339         delete (T*)ptr;
340       }
341
342       static const char* getMember(naContext c, void* g, naRef field, naRef* out)
343       {
344         const std::string key = naStr_data(field);
345         if( key == "parents" )
346         {
347           *out = getSingletonPtr()->getParents(c);
348           return "";
349         }
350
351         typename GetterMap::iterator getter =
352           getSingletonPtr()->_getter.find(key);
353
354         if( getter == getSingletonPtr()->_getter.end() )
355           return 0;
356
357         *out = getter->second(c, class_::getRawPtr(g));
358         return "";
359       }
360   };
361 }
362
363 struct Base
364 {
365   int getInt() const
366   {
367     return 8;
368   }
369 };
370
371 struct Test:
372   public Base
373 {
374   Test(): x(1) {}
375   int x;
376 };
377
378 typedef nasal::class_<sc::CanvasPtr> NasalCanvas;
379
380 void initCanvas()
381 {
382
383   NasalCanvas::init("Canvas")
384     .def_readonly("texture", &canvasGetTexture)
385     .def("size_x", &sc::Canvas::getSizeX)
386     .def("size_y", &sc::Canvas::getSizeY);
387   nasal::class_<sc::ElementPtr>::init("canvas.Element");
388   nasal::class_<sc::GroupPtr>::init("canvas.Group")
389     .bases<sc::ElementPtr>();
390
391   nasal::class_<Base>::init("BaseClass")
392     .def("int", &Base::getInt);
393   nasal::class_<Test>::init("TestClass")
394     .bases<Base>()
395     .def_readonly("x", &Test::x);
396 }
397
398 /**
399  * A Nasal Hash
400  */
401 class Hash
402 {
403   public:
404
405     /**
406      * Initialize from an existing Nasal Hash
407      *
408      * @param hash  Existing Nasal Hash
409      * @param c     Nasal context
410      */
411     Hash(const naRef& hash, naContext c):
412       _hash(hash),
413       _context(c)
414     {
415       assert( naIsHash(_hash) );
416     }
417
418     void set(const std::string& name, naRef val)
419     {
420       naHash_set(_hash, stringToNasal(name), val);
421     }
422
423     void set(const std::string& name, naCFunction func)
424     {
425       set(name, naNewFunc(_context, naNewCCode(_context, func)));
426     }
427
428     void set(const std::string& name, const std::string& str)
429     {
430       set(name, stringToNasal(str));
431     }
432
433     void set(const std::string& name, double num)
434     {
435       set(name, naNum(num));
436     }
437
438     /**
439      * Create a new child hash (module)
440      *
441      * @param name  Name of the new hash inside this hash
442      */
443     Hash createHash(const std::string& name)
444     {
445       naRef hash = naNewHash(_context);
446       set(name, hash);
447       return Hash(hash, _context);
448     }
449
450   protected:
451     naRef _hash;
452     naContext _context;
453
454     naRef stringToNasal(const std::string& str)
455     {
456       naRef s = naNewString(_context);
457       naStr_fromdata(s, str.c_str(), str.size());
458       return s;
459     }
460 };
461
462 /**
463  * Class for exposing C++ objects to Nasal
464  */
465 template<class T, class Derived>
466 class NasalObject
467 {
468   public:
469     // TODO use variadic template when supporting C++11
470     template<class A1>
471     static naRef create( naContext c, const A1& a1 )
472     {
473       return makeGhost(c, new T(a1));
474     }
475
476     template<class A1, class A2>
477     static naRef create( naContext c, const A1& a1,
478                                       const A2& a2 )
479     {
480       return makeGhost(c, new T(a1, a2));
481     }
482
483     template<class A1, class A2, class A3>
484     static naRef create( naContext c, const A1& a1,
485                                       const A2& a2,
486                                       const A3& a3 )
487     {
488       return makeGhost(c, new T(a1, a2, a3));
489     }
490
491     template<class A1, class A2, class A3, class A4>
492     static naRef create( naContext c, const A1& a1,
493                                       const A2& a2,
494                                       const A3& a3,
495                                       const A4& a4 )
496     {
497       return makeGhost(c, new T(a1, a2, a3, a4));
498     }
499
500     template<class A1, class A2, class A3, class A4, class A5>
501     static naRef create( naContext c, const A1& a1,
502                                       const A2& a2,
503                                       const A3& a3,
504                                       const A4& a4,
505                                       const A5& a5 )
506     {
507       return makeGhost(c, new T(a1, a2, a3, a4, a5));
508     }
509
510     // TODO If you need more arguments just do some copy&paste :)
511
512     static Derived& getInstance()
513     {
514       static Derived instance;
515       return instance;
516     }
517
518     void setParent(const naRef& parent)
519     {
520       // TODO check if we need to take care of reference counting/gc
521       _parents.resize(1);
522       _parents[0] = parent;
523     }
524
525   protected:
526
527     // TODO switch to boost::/std::function (with C++11 lambdas this can make
528     //      adding setters easier and shorter)
529     typedef naRef (Derived::*getter_t)(naContext, const T&);
530     typedef std::map<std::string, getter_t> GetterMap;
531
532     const std::string   _ghost_name;
533     std::vector<naRef>  _parents;
534     GetterMap           _getter;
535
536     NasalObject(const std::string& ghost_name):
537       _ghost_name( ghost_name )
538     {
539       _ghost_type.destroy = &destroyGhost;
540       _ghost_type.name = _ghost_name.c_str();
541       _ghost_type.get_member = &Derived::getMember;
542       _ghost_type.set_member = 0;
543
544       _getter["parents"] = &NasalObject::getParents;
545     }
546
547     naRef getParents(naContext c, const T&)
548     {
549       naRef parents = naNewVector(c);
550       for(size_t i = 0; i < _parents.size(); ++i)
551         naVec_append(parents, _parents[i]);
552       return parents;
553     }
554
555     static naRef makeGhost(naContext c, void *ptr)
556     {
557       std::cout << "create  " << ptr << std::endl;
558       return naNewGhost2(c, &(getInstance()._ghost_type), ptr);
559     }
560
561     static void destroyGhost(void *ptr)
562     {
563       std::cout << "destroy " << ptr << std::endl;
564       delete (T*)ptr;
565     }
566
567     static const char* getMember(naContext c, void* g, naRef field, naRef* out)
568     {
569       typename GetterMap::iterator getter =
570         getInstance()._getter.find(naStr_data(field));
571
572       if( getter == getInstance()._getter.end() )
573         return 0;
574
575       *out = (getInstance().*getter->second)(c, *static_cast<T*>(g));
576       return "";
577     }
578
579   private:
580
581     naGhostType _ghost_type;
582
583 };
584
585 static naRef stringToNasal(naContext c, const std::string& s)
586 {
587     return naStr_fromdata(naNewString(c),
588                    const_cast<char *>(s.c_str()),
589                    s.length());
590 }
591
592 typedef osg::ref_ptr<osgGA::GUIEventAdapter> GUIEventPtr;
593
594 class NasalCanvasEvent:
595   public NasalObject<GUIEventPtr, NasalCanvasEvent>
596 {
597   public:
598
599     NasalCanvasEvent():
600       NasalObject("CanvasEvent")
601     {
602       _getter["type"] = &NasalCanvasEvent::getEventType;
603     }
604
605     naRef getEventType(naContext c, const GUIEventPtr& event)
606     {
607 #define RET_EVENT_STR(type, str)\
608   case osgGA::GUIEventAdapter::type:\
609     return stringToNasal(c, str);
610
611       switch( event->getEventType() )
612       {
613         RET_EVENT_STR(PUSH,         "push");
614         RET_EVENT_STR(RELEASE,      "release");
615         RET_EVENT_STR(DOUBLECLICK,  "double-click");
616         RET_EVENT_STR(DRAG,         "drag");
617         RET_EVENT_STR(MOVE,         "move");
618         RET_EVENT_STR(SCROLL,       "scroll");
619         RET_EVENT_STR(KEYUP,        "key-up");
620         RET_EVENT_STR(KEYDOWN,      "key-down");
621
622 #undef RET_EVENT_STR
623
624         default:
625           return naNil();
626       }
627     }
628 };
629
630 //using simgear::canvas::CanvasPtr;
631 //
632 ///**
633 // * Expose Canvas to Nasal
634 // */
635 //class NasalCanvas:
636 //  public NasalObject<CanvasPtr, NasalCanvas>
637 //{
638 //  public:
639 //
640 //    NasalCanvas():
641 //      NasalObject("Canvas")
642 //    {
643 //      _getter["texture"] = &NasalCanvas::getTexture;
644 //      _getter["size_x"] = &NasalCanvas::getSizeX;
645 //      _getter["size_y"] = &NasalCanvas::getSizeY;
646 //    }
647 //
648 //    static naRef f_create(naContext c, naRef me, int argc, naRef* args)
649 //    {
650 //      std::cout << "NasalCanvas::create" << std::endl;
651 //
652 //      CanvasMgr* canvas_mgr =
653 //        static_cast<CanvasMgr*>(globals->get_subsystem("Canvas"));
654 //      if( !canvas_mgr )
655 //        return naNil();
656 //
657 //      return create(c, canvas_mgr->createCanvas());
658 //    }
659 //
660 //    static naRef f_setPrototype(naContext c, naRef me, int argc, naRef* args)
661 //    {
662 //      if( argc != 1 || !naIsHash(args[0]) )
663 //        naRuntimeError(c, "Invalid argument(s)");
664 //
665 //      getInstance().setParent(args[0]);
666 //
667 //      return naNil();
668 //    }
669 //
670 //    naRef getTexture(naContext c, const CanvasPtr& canvas)
671 //    {
672 //      //{ parents : [Node], _g : node }
673 //      naRef parents = naNewVector(c);
674 //      naVec_append(parents, propsNodePrototype);
675 //
676 //      naRef node = naNewHash(c);
677 //      hashset(c, node, "parents", parents);
678 //      hashset(c, node, "_g", propNodeGhostCreate(c, canvas->getProps()));
679 //
680 //      return node;
681 //    }
682 //
683 //    naRef getSizeX(naContext c, const CanvasPtr& canvas)
684 //    {
685 //      return naNum(canvas->getSizeX());
686 //    }
687 //
688 //    naRef getSizeY(naContext c, const CanvasPtr& canvas)
689 //    {
690 //      return naNum(canvas->getSizeY());
691 //    }
692 //};
693
694 static void elementGhostDestroy(void* g);
695
696 static const char* elementGhostGetMember(naContext c, void* g, naRef field, naRef* out);
697 static void elementGhostSetMember(naContext c, void* g, naRef field, naRef value);
698 naGhostType ElementGhostType = { elementGhostDestroy, "canvas.element", 
699     elementGhostGetMember, elementGhostSetMember };
700
701 static simgear::canvas::Element* elementGhost(naRef r)
702 {
703   if (naGhost_type(r) == &ElementGhostType)
704     return (simgear::canvas::Element*) naGhost_ptr(r);
705   return 0;
706 }
707
708 static void elementGhostDestroy(void* g)
709 {
710 }
711
712 static const char* eventGhostGetMember(naContext c, void* g, naRef field, naRef* out)
713 {
714   const char* fieldName = naStr_data(field);
715   osgGA::GUIEventAdapter* gea = (osgGA::GUIEventAdapter*) g;
716
717   if (!strcmp(fieldName, "windowX")) *out = naNum(gea->getWindowX());
718   else if (!strcmp(fieldName, "windowY")) *out = naNum(gea->getWindowY());
719   else if (!strcmp(fieldName, "time")) *out = naNum(gea->getTime());
720   else if (!strcmp(fieldName, "button")) *out = naNum(gea->getButton());
721   else {
722     return 0;
723   }
724   
725   return "";
726 }
727
728 static const char* elementGhostGetMember(naContext c, void* g, naRef field, naRef* out)
729 {
730   const char* fieldName = naStr_data(field);
731   simgear::canvas::Element* e = (simgear::canvas::Element*) g;
732   SG_UNUSED(e);
733   
734   if (!strcmp(fieldName, "parents")) {
735     *out = naNewVector(c);
736     naVec_append(*out, elementPrototype);
737   } else {
738     return 0;
739   }
740   
741   return "";
742 }
743
744 static void elementGhostSetMember(naContext c, void* g, naRef field, naRef value)
745 {
746   const char* fieldName = naStr_data(field);
747   simgear::canvas::Element* e = (simgear::canvas::Element*) g;
748   SG_UNUSED(fieldName);
749   SG_UNUSED(e);
750 }
751
752
753 static naRef f_canvas_getElement(naContext c, naRef me, int argc, naRef* args)
754 {
755 //  simgear::canvas::Canvas* cvs = canvasGhost(me);
756 //  if (!cvs) {
757 //    naRuntimeError(c, "canvas.getElement called on non-canvas object");
758 //  }
759   
760   return naNil();
761 }
762
763 static naRef f_element_addButtonCallback(naContext c, naRef me, int argc, naRef* args)
764 {
765   simgear::canvas::Element* e = elementGhost(me);
766   if (!e) {
767     naRuntimeError(c, "element.addButtonCallback called on non-canvas-element object");
768   }
769   
770   return naNil();
771 }
772
773 static naRef f_element_addDragCallback(naContext c, naRef me, int argc, naRef* args)
774 {
775   simgear::canvas::Element* e = elementGhost(me);
776   if (!e) {
777     naRuntimeError(c, "element.addDragCallback called on non-canvas-element object");
778   }
779   
780   return naNil();
781 }
782
783 static naRef f_element_addMoveCallback(naContext c, naRef me, int argc, naRef* args)
784 {
785   simgear::canvas::Element* e = elementGhost(me);
786   if (!e) {
787     naRuntimeError(c, "element.addMoveCallback called on non-canvas-element object");
788   }
789   
790   return naNil();
791 }
792
793 static naRef f_element_addScrollCallback(naContext c, naRef me, int argc, naRef* args)
794 {
795   simgear::canvas::Element* e = elementGhost(me);
796   if (!e) {
797     naRuntimeError(c, "element.addScrollCallback called on non-canvas-element object");
798   }
799   
800   return naNil();
801 }
802
803 static naRef f_createCanvas(naContext c, naRef me, int argc, naRef* args)
804 {
805   std::cout << "f_createCanvas" << std::endl;
806
807   CanvasMgr* canvas_mgr =
808     static_cast<CanvasMgr*>(globals->get_subsystem("Canvas"));
809   if( !canvas_mgr )
810     return naNil();
811
812   return NasalCanvas::create(c, canvas_mgr->createCanvas());
813 }
814
815 static naRef f_setCanvasPrototype(naContext c, naRef me, int argc, naRef* args)
816 {
817   if( argc != 1 || !naIsHash(args[0]) )
818     naRuntimeError(c, "Invalid argument(s)");
819
820   NasalCanvas::getSingletonPtr()->addNasalBase(args[0]);
821
822   return naNil();
823 }
824
825 naRef initNasalCanvas(naRef globals, naContext c, naRef gcSave)
826 {
827   naRef props_module = naHash_cget(globals, (char*)"props");
828   if( naIsNil(props_module) )
829     std::cerr << "No props module" << std::endl;
830
831   propsNodePrototype = naHash_cget(props_module, (char*)"Node");
832   if( naIsNil(propsNodePrototype) )
833     std::cerr << "Failed to get props.Node" << std::endl;
834
835       /*naNewHash(c);
836     hashset(c, gcSave, "canvasProto", canvasPrototype);
837   
838     hashset(c, canvasPrototype, "getElement", naNewFunc(c, naNewCCode(c, f_canvas_getElement)));*/
839     // set any event methods
840   
841     elementPrototype = naNewHash(c);
842     hashset(c, gcSave, "elementProto", elementPrototype);
843     
844     hashset(c, elementPrototype, "addButtonCallback", naNewFunc(c, naNewCCode(c, f_element_addButtonCallback)));
845     hashset(c, elementPrototype, "addDragCallback", naNewFunc(c, naNewCCode(c, f_element_addDragCallback)));
846     hashset(c, elementPrototype, "addMoveCallback", naNewFunc(c, naNewCCode(c, f_element_addMoveCallback)));
847     hashset(c, elementPrototype, "addScrollCallback", naNewFunc(c, naNewCCode(c, f_element_addScrollCallback)));
848
849   Hash globals_module(globals, c),
850        canvas_module = globals_module.createHash("canvas");
851
852   canvas_module.set("_new", f_createCanvas);
853   canvas_module.set("_setPrototype", f_setCanvasPrototype);
854   canvas_module.set("testClass", nasal::class_<Test>::f_create);
855
856   initCanvas();
857
858   return naNil();
859 }