16

I am embedding SpiderMonkey to make my C++ library scriptable. To make it extendable, I need it possible to define a new class (JavaScript) inheriting one C++ base class of the library.

Is there an example showing me how to do that using SpiderMonkey?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ims
  • 161
  • 3
  • have you had a look at this: http://egachine.berlios.de/embedding-sm-best-practice/embedding-sm-best-practice.html#id2510183 ? – smerlin Jul 18 '10 at 22:43
  • Thanks for your comment. I have read that article before, but I didn't find the solution to my question. My question is not to map Mapping inheritance of existing C++ classes. In my case, I wish I could sub-class existing C++ class in my Javascript code and use it in my C++ program. Many thanks. – ims Jul 18 '10 at 23:13
  • 3
    Err... don't do that. One probably should not be inheriting from library classes; instead, make your class usable through composition. – Billy ONeal Jul 18 '10 at 23:20
  • Yes I was thinking about just providing a C++ scriptable base class which can call Javascript callback functions. Not sure if it's exactly what you meant :) Many thanks. BTW, any example is welcome. – ims Jul 18 '10 at 23:42
  • 4
    The concept of inheritance between the two languages is not the same so I am not really sure what you mean...Anyhow, check this http://www.crockford.com/javascript/inheritance.html – Fraser Jul 19 '10 at 12:47

2 Answers2

3

I'd think you cannot subclass a C++ class in SpiderMonkey/JavaScript and use it transparently in the rest of your C++ code. However, you could create a wrapping class in C++ that delegates its work to a couple of JavaScript classes/functions. If you make your C++ class configurable enough that should work for your situation. Both calling JavaScript code from C and C code from JavaScript is explaind in the JSAPI User Guide page (I think you've already read that one).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Simon Groenewolt
  • 10,607
  • 1
  • 36
  • 64
1

I don't know it's possible, but a feeling says you're better off writing a wrapper class in JavaScript for the class in the C++ library.

Community
  • 1
  • 1
Gorkem Pacaci
  • 1,741
  • 1
  • 11
  • 9