Quantcast
Viewing latest article 7
Browse Latest Browse All 47

Extended C++ interface support in Sciter

If C++ would have reflection life will be a bit easier.  It could be a breeze to add native functionality to Sciter using it – in particular to expose native objects to script. But no reflection in C++.

But situation is not that grim with the new mechanism that I am adding now.

If you have some methods or properties of a class that need to be exposed to script then you will need to provide so called “passport” – definition of items that need to be exposed:

class frame: public sciter::window {
public:
  frame() : window(SW_TITLEBAR | SW_RESIZEABLE | SW_CONTROLS | SW_MAIN | SW_GLASSY | SW_ENABLE_DEBUG) {}

     // methods for script
     int foo() { return p + 42; }
     int bar(int p1, int p2) { return p1 + p2; }
     std::vector<int> vec() { return {1,2,3}; }

     const bool uSciter = true;
           bool busy = false;

  // the passport
  SOM_PASSPORT_BEGIN(frame)
    SOM_FUNCS(SOM_FUNC(foo), 
              SOM_FUNC(bar), 
              SOM_FUNC(vec))
    SOM_PROPS(SOM_RO_PROP(uSciter),
              SOM_PROP(busy))
  SOM_PASSPORT_END

};

SOM_PASSPORT_BEGIN/END above contain helper definitions that allow Sciter’s script to call those methods and access properties.

So if in your script code you will have something like this:

if(view.frame.uSciter) // access to native property
{
  var numbers =  view.vec();
  debug log: numbers; 
}

then output will have numbers:[1,2,3].

The SOM_PASSPORT feature will allow to annotate not just native behaviors as the above but arbitrary classes that you will want to expose to script from your app. I will rewrite SQLite wrapper using this mechanism.

SOM_PASSPORT provides unified access to native code from script – it makes ugly `tiscript.h` and quite heavy `vfunc()` mechanism obsolete at great extent, at some point they will be removed. SOM_PASSPORT transparently converts all basic types: bool, int, float, double, std::string, std::wstring, std::vector<T> and custom objects.

Yet, SOM_PASSPORT is not just for C++, the mechanism is based on plain C API so can be available for other native languages Rust, Delphi, etc.


Viewing latest article 7
Browse Latest Browse All 47

Trending Articles