Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Usual non-virtual method of IGUIObject, or you may
- // split it out and pass GUI* as an argument.
- template<typename InterfaceType, typename ObjectType>
- JS::PersistentRootedObject IGUIObject::CreateJSObject(
- ObjectType* object, const char* name)
- {
- JSContext* cx = GetGUI()->GetScriptInterface()->GetContext();
- JSAutoRequest rq(cx);
- JS::PersistentRootedObject JSObject;
- if (!JSObject.initialized())
- {
- JSObject.init(
- cx, GetGUI()->GetScriptInterface()->CreateCustomObject(name));
- InterfaceType<ObjectType>::RegisterScriptFunctions(cx, JSObject);
- JS_SetPrivate(JSObject.get(), object);
- }
- return JSObject;
- }
- // IGUIObject interface object
- template<typename ObjectType>
- class IGUIObjectInterface
- {
- public:
- static void RegisterScriptFunctions(JSContext* cx, JS::HandleObject obj)
- {
- JS_DefineFunctions(cx, obj, Methods);
- // ...
- }
- static bool JSI_IGUIObject::toString(
- JSContext* cx, uint UNUSED(argc), JS::Value* vp)
- {
- JSAutoRequest rq(cx);
- JS::CallReceiver rec = JS::CallReceiverFromVp(vp);
- JS::RootedObject thisObj(cx, JS_THIS_OBJECT(cx, vp));
- // Here we have a derived/final object type. That means
- // we don't have to do downcast, only upcast. Which is simple.
- ObjectType* object = reinterpret_cast<ObjectType*>(
- JS_GetInstancePrivate(
- cx, thisObj, &JSI_IGUIObject::JSI_class, NULL));
- if (!object)
- return false;
- ScriptInterface::ToJSVal(
- cx, rec.rval(), "[GUIObject: " + object->GetName() + "]");
- return true;
- }
- private:
- static JSFunctionSpec Methods;
- };
- template<typename ObjectType>
- JSFunctionSpec IGUIObjectInterface<ObjectType>::Methods = {
- JS_FN("toString", IGUIObjectInterface<ObjectType>::ToString, 0, 0),
- JS_FS_END
- };
- // Another interface object, it can derive or not, it doesn't matter,
- // it also allows to use virtual inheritance.
- template<typename ObjectType>
- class IGUITextOwnerInterface : public IGUIObjectInterface<ObjectType>
- {
- public:
- static void RegisterScriptFunctions(JSContext* cx, JS::HandleObject obj)
- {
- IGUIObjectInterface<ObjectType>::RegisterScriptFunctions(cx, obj);
- // ...
- }
- private:
- static JSFunctionSpec Methods;
- };
- // All this objects can be a part of origin GUI objects.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement