+static JSValueRef CYJSValueInContext(id self, SEL _cmd, JSContextRef context) {
+ // XXX: the offset of this Ivar could be recomputed and stored in some form of closure during $objc_registerClassPair
+
+ JSObjectRef value;
+ object_getInstanceVariable(self, "cy$value_", reinterpret_cast<void **>(&value));
+
+ if (value == NULL) {
+ value = CYMakeInstance(context, self, false);
+ object_setInstanceVariable(self, "cy$value_", value);
+ }
+
+ return value;
+}
+
+static JSValueRef $objc_registerClassPair(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+ CYTry {
+ CYPool pool;
+
+ Class _class(CYCastNSObject(pool, context, object));
+ if (class_getInstanceMethod(_class, @selector(cy$JSValueInContext:)) == NULL) {
+ class_addIvar(_class, "cy$value_", sizeof(JSObjectRef), log2(sizeof(JSObjectRef)), "^v");
+ class_addMethod(_class, @selector(cy$JSValueInContext:), reinterpret_cast<IMP>(&CYJSValueInContext), "^v12@0:4^v8");
+ }
+
+ objc_registerClassPair(_class);
+ return CYJSUndefined(context);
+ } CYCatch
+}
+