From 80ea020e4c9cdd505ec355ae2ca1495e1110e7ea Mon Sep 17 00:00:00 2001 From: "Jay Freeman (saurik)" Date: Fri, 14 Sep 2012 07:14:17 -0700 Subject: [PATCH] Provide a .typeOf() to get a Type of an ObjC Class. --- ObjectiveC/Library.mm | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/ObjectiveC/Library.mm b/ObjectiveC/Library.mm index 3976d54..379a1a2 100644 --- a/ObjectiveC/Library.mm +++ b/ObjectiveC/Library.mm @@ -2412,6 +2412,28 @@ static JSValueRef Instance_callAsFunction_toString(JSContextRef context, JSObjec } CYPoolCatch(NULL) } CYCatch return /*XXX*/ NULL; } +static JSValueRef Instance_callAsFunction_typeOf(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { + if (!CYJSValueIsNSObject(context, _this)) + return NULL; + + Instance *internal(reinterpret_cast(JSObjectGetPrivate(_this))); + id value(internal->GetValue()); + + if (!CYIsClass(value)) + CYThrow("non-Class object cannot be used as Type"); + + // XXX: this is a very silly implementation + + std::ostringstream type; + type << "@\""; + type << class_getName(value); + type << "\""; + + CYPoolTry { + return CYMakeType(context, type.str().c_str()); + } CYPoolCatch(NULL) +} CYCatch return /*XXX*/ NULL; } + static JSValueRef Selector_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { Selector_privateData *internal(reinterpret_cast(JSObjectGetPrivate(_this))); return CYCastJSValue(context, sel_getName(internal->GetValue())); @@ -2464,13 +2486,14 @@ static JSStaticValue Instance_staticValues[5] = { {NULL, NULL, NULL, 0} }; -static JSStaticFunction Instance_staticFunctions[7] = { +static JSStaticFunction Instance_staticFunctions[8] = { {"$cya", &CYValue_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {"toCYON", &Instance_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {"toJSON", &Instance_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {"valueOf", &Instance_callAsFunction_valueOf, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {"toPointer", &Instance_callAsFunction_toPointer, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {"toString", &Instance_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, + {"typeOf", &Instance_callAsFunction_typeOf, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {NULL, NULL, 0} }; -- 2.49.0