]> git.saurik.com Git - cycript.git/blobdiff - Internal.hpp
Do not output an empty comment for an empty stack.
[cycript.git] / Internal.hpp
index 464fe936d1782e8038cafa4a36f12e80f1e25b62..e0193128ef5e0e8d0d9faad80e839aeefa4cc328 100644 (file)
@@ -39,15 +39,19 @@ sig::Type *Structor_(CYPool &pool, sig::Aggregate *aggregate);
 
 extern JSClassRef Functor_;
 
-template <typename Internal_>
-struct CYPrivate :
+struct CYRoot :
     CYData
 {
-    static JSClassRef Class_;
-
     _finline JSValueRef GetPrototype(JSContextRef context) const {
         return NULL;
     }
+};
+
+template <typename Internal_, typename Base_ = CYRoot>
+struct CYPrivateOld :
+    Base_
+{
+    static JSClassRef Class_;
 
     template <typename... Args_>
     _finline static JSClassRef GetClass(Args_ &&... args) {
@@ -69,8 +73,33 @@ struct CYPrivate :
     }
 };
 
+template <typename Internal_, typename Base_>
+JSClassRef CYPrivateOld<Internal_, Base_>::Class_;
+
+template <typename Internal_>
+struct CYPrivate {
+    static JSClassRef Class_;
+
+    template <typename... Args_>
+    static JSObjectRef Make(JSContextRef context, Args_ &&... args) {
+        Internal_ *internal(new Internal_(cy::Forward<Args_>(args)...));
+        JSObjectRef object(JSObjectMake(context, Class_, internal));
+        if (JSValueRef prototype = internal->GetPrototype(context))
+            CYSetPrototype(context, object, prototype);
+        return object;
+    }
+
+    static Internal_ *Get(JSContextRef context, JSObjectRef object) {
+        _assert(JSValueIsObjectOfClass(context, object, Class_));
+        return static_cast<Internal_ *>(JSObjectGetPrivate(object));
+    }
+};
+
+template <typename Internal_>
+JSClassRef CYPrivate<Internal_>::Class_;
+
 struct Type_privateData :
-    CYPrivate<Type_privateData>
+    CYRoot
 {
     ffi_type *ffi_;
     sig::Type *type_;
@@ -117,29 +146,6 @@ struct Type_privateData :
     }
 };
 
-template <typename Internal_, typename Value_>
-struct CYValue :
-    CYPrivate<Internal_>
-{
-    Value_ value_;
-
-    CYValue() {
-    }
-
-    CYValue(const Value_ &value) :
-        value_(value)
-    {
-    }
-
-    CYValue(const CYValue &rhs) :
-        value_(rhs.value_)
-    {
-    }
-};
-
-template <typename Internal_>
-JSClassRef CYPrivate<Internal_>::Class_;
-
 struct CYProtect {
   private:
     JSGlobalContextRef context_;
@@ -176,7 +182,7 @@ struct CYProtect {
 
 namespace cy {
 struct Functor :
-    CYValue<Functor, void (*)()>
+    CYRoot
 {
   private:
     void set() {
@@ -184,12 +190,13 @@ struct Functor :
     }
 
   public:
+    void (*value_)();
     bool variadic_;
     sig::Signature signature_;
     ffi_cif cif_;
 
     Functor(void (*value)(), bool variadic, const sig::Signature &signature) :
-        CYValue(value),
+        value_(value),
         variadic_(variadic)
     {
         sig::Copy(*pool_, signature_, signature);
@@ -197,7 +204,7 @@ struct Functor :
     }
 
     Functor(void (*value)(), const char *encoding) :
-        CYValue(value),
+        value_(value),
         variadic_(false)
     {
         sig::Parse(*pool_, &signature_, encoding, &Structor_);