Singleton objects

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Singleton objects

Mike Gran
Hello All,

I added Spidermonkey as a scripting host for a simple arcade
game I've been developing.  Getting it initialized, running
scripts, and calling simple global functions that I've defined
in C++ all went fairly painlessly. :-)

But, with respect to getting C++-defined objects working,
I'm afraid I'm a bit lost. One problem is this.  I have a
singleton static struct that I want to make usable as a JS
object.  Since it is static, its C++ storage should never be
garbage collected.  A simplified version might be like
this:

typedef struct eng_tag {
    double fps;
    bool   paused;
} eng_t;
static eng_t eng;

At the moment I do this with global JS functions called
getEngFps(), setEngFps(), getEngPause(), setEngPause(), and so
on.  This is fine, but, there are a lot of member variables in
my real 'eng' class, so I end up making a lot of global functions.

I'd rather there were an analagous global non-GC'd javascript
object 'eng' with the properties 'fps' and 'paused' where
reading or writing to them reads or writes to the C++ structure,
throwing an exception on writing if the JS type is wrong for
that property.

Can this be done? Thanks,

Mike Gran
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|

Re: Singleton objects

Terrence Cole-3
On 6/5/14, 2:42 PM, Mike Gran wrote:

> Hello All,
>
> I added Spidermonkey as a scripting host for a simple arcade
> game I've been developing.  Getting it initialized, running
> scripts, and calling simple global functions that I've defined
> in C++ all went fairly painlessly. :-)
>
> But, with respect to getting C++-defined objects working,
> I'm afraid I'm a bit lost. One problem is this.  I have a
> singleton static struct that I want to make usable as a JS
> object.  Since it is static, its C++ storage should never be
> garbage collected.  A simplified version might be like
> this:
>
> typedef struct eng_tag {
>      double fps;
>      bool   paused;
> } eng_t;
> static eng_t eng;
>
> At the moment I do this with global JS functions called
> getEngFps(), setEngFps(), getEngPause(), setEngPause(), and so
> on.  This is fine, but, there are a lot of member variables in
> my real 'eng' class, so I end up making a lot of global functions.
>
> I'd rather there were an analagous global non-GC'd javascript
> object 'eng' with the properties 'fps' and 'paused' where
> reading or writing to them reads or writes to the C++ structure,
> throwing an exception on writing if the JS type is wrong for
> that property.
>
> Can this be done? Thanks,

Yes, absolutely; Firefox is built around such dispatch.

Sadly, this isn't built-in to SpiderMonkey at the moment, but is a Gecko
infrastructure called webidl. Basically, you have your raw C++ class
like |eng| and an interface defined in webidl. There is a script called
Codegen.py that reads the webidl and spits out glue and sanitization
code to allow js to call C++ and the inverse. You could probably use
this infrastructure directly with less work than maintaining bindings
manually.

Good luck,
Terrence

> Mike Gran
> _______________________________________________
> dev-tech-js-engine mailing list
> [hidden email]
> https://lists.mozilla.org/listinfo/dev-tech-js-engine

_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|

Re: Singleton objects

Mike Gran
> Sadly, this isn't built-in to SpiderMonkey at the moment, but is a Gecko

> infrastructure called webidl. Basically, you have your raw C++ class
> like |eng| and an interface defined in webidl. There is a script called
> Codegen.py that reads the webidl and spits out glue and sanitization
> code to allow js to call C++ and the inverse. You could probably use
> this infrastructure directly with less work than maintaining bindings
> manually.

Well, how one would run Codegen.py by itself is not obvious.

What I ended up doing was this
- cloned the gecko-dev source tree from git
- added webidl files to the dom/webidl directory
- inserted the names of my webidl files into the 'dom/webidl/moz.build' file
  under the WEBIDL_FILES list, in alphabetical order
- then built from the top-level directory

And this resulted in skeleton interface classes found in the
'obj-i686-pc-linux-gnu/dom/bindings' directory, with headers in the
'obj-i686-pc-linux-gnu/dist/include/mozilla/dom' directory. But the
generated bindings have a lot of dependencies to various helper classes
and use a lot of internal mozilla clases that don't seem to be documented
as SpiderMonkey API.

It is useful.  It definitely gives me an outline of what I'm supposed to be
doing, even if I haven't yet worked out all the details.

Thanks,

Mike Gran

_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine