Skip to content
Craig Minihan edited this page Dec 7, 2016 · 10 revisions

libjsapi introduces four new class types to make dealing with JSAPI a bit easier:

  • Value
  • Object
  • DynamicObject
  • DynamicArray

The last three of these classes creates a rs::jsapi::Value instance via a factory Create() function. The parameters for each of these functions can be broken down into:

  • getter callbacks
  • setter callbacks
  • enumerator callbacks (for DynamicObject)
  • finalizer callbacks
  • length callbacks (for DynamicArray)

The callbacks are implemented using std::function which means they accept C++ 11 lambdas with closures.

The signature of Object::Create looks like:

static bool Create(
        Context& cx, 
        std::initializer_list<const char*> properties,
        const GetCallback& getter, 
        const SetCallback& setter,
        std::initializer_list<Function> functions,
        const FinalizeCallback& finalizer,
        Value& obj);

The rs::jsapi::Value instance returned from Create can be passed to JSAPI functions or libjsapi methods, for example:

// create the new object
rs::jsapi::Value obj(cx);
rs::jsapi::Object::Create(/* ... */, obj);

// append the new object to an arguments instance
rs::jsapi::FunctionArguments args(cx);
args.Append(obj);

// call the JS function passing the new object via the args parameter
cx.Call("myfunc", args);

The getter and setter callbacks are invoked when JS code references the object. The finalizer is invoked when SpiderMonkey collects the object at the end of its life.

Clone this wiki locally