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?

5
  • have you had a look at this: egachine.berlios.de/embedding-sm-best-practice/… ? Commented Jul 18, 2010 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. Commented Jul 18, 2010 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. Commented Jul 18, 2010 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. Commented Jul 18, 2010 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 crockford.com/javascript/inheritance.html Commented Jul 19, 2010 at 12:47

2 Answers 2

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).

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.