Architecture: Using the JsVar suite within C

Posted on
  • Howdy folks,
    Am studying the use of the JsVar suite within C code. My first question is ... is there an architecture or programmers guide on using JsVars? There seems to be a ton of functions including concepts such as "locking" and "references" and I'm looking for a primer on such. If nothing like that exists, if anyone wishes to post their theory or experiences, it will be most welcome.

    The next set of questions will all be on "usage patterns". For example, how to create a Java Script object, what does typing "mean", how to populate an object's fields, how to invoke a variable that represents a function etc etc.

    Anything and everything is welcomed here ... and I suspect this thread may grow along this area.

    Neil

  • Afraid I'm off on holiday for this week, so won't be able to help out. Best bet is to take a look at other functions that do something similar to what you want though.

  • Bits are starting to come together. We can create a new object instance using jspNewObject() and add properties to the object using jsvObjectSetChild(). I think one of the things that has been confusing me is the notion of "children" which I believe are what I consider to be "properties" in JavaScript. I also found a thread which talks about jsvLock() as a mechanism to prevent the garbage collector from cleaning up references. This means that deep testing will be required to ensure that objects created by the code are not left locked or we will run out of memory.

  • We can create a new object instance using jspNewObject()

    Yes, although for most functions I'd suggest returning basic objects, made with jsvNewWithFlags(JSV_OBJECT)

    the notion of "children" which I believe are what I consider to be "properties" in JavaScript

    Yes, spot on

    jsvLock()

    Yes. Basically to get a pointer from a reference you jsvLock and when you're done with it you jsvUnLock - but you don't unlock if you're returning the pointer from a function. When setting stuff in an object, you might do something like:

    jsvUnLock(jsvObjectSetChild(obj, "foo", jsvNewFromInteger(42)));
    

    See here for an example of how process.env does it.

    deep testing will be required to ensure that objects created by the code are not left locked or we will run out of memory.

    If you compile without RELEASE=1 a load of assertions are left in that catch multiple unlocks. You can call multiple times and then check with process.memory().usage to see if memory usage increases too.

    When compiling for Linux, if you run the tests then each test checks for unfreed memory after execution and fails if there is any, so you can catch stuff that way too.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Architecture: Using the JsVar suite within C

Posted by Avatar for Kolban @Kolban

Actions