From 21d5f610b933e87b7110d629daddeb8f333dcec9 Mon Sep 17 00:00:00 2001 From: "Jay Freeman (saurik)" Date: Sat, 15 Sep 2012 01:38:22 -0700 Subject: [PATCH] Allow Type objects to have associated identifiers. --- Cycript.yy.in | 8 +++++++- Execute.cpp | 24 ++++++++++++++++++++++-- sig/types.hpp | 2 +- todo.txt | 2 ++ 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/Cycript.yy.in b/Cycript.yy.in index 92d57f6..b6f355c 100644 --- a/Cycript.yy.in +++ b/Cycript.yy.in @@ -478,6 +478,7 @@ int cylex(YYSTYPE *, cy::location *, void *); %type MessageParameterListOpt %type MessageScope %type ModifiedType +%type NamedTypeOpt %type PrefixedType %type SelectorCall_ %type SelectorCall @@ -1339,8 +1340,13 @@ ProgramBodyOpt @begin ObjectiveC /* Cycript (Objective-C): Type Encoding {{{ */ +NamedTypeOpt + : Identifier { $$ = CYNew CYEncodedPart(NULL, "withName", CYNew CYArgument(CYNew CYString($1->word_))); } + | { $$ = NULL; } + ; + SuffixedType - : { $$ = NULL; } + : NamedTypeOpt { $$ = $1; } | "(" PrefixedType ")" { $$ = $2; } | SuffixedType "[" NumericLiteral "]" { $$ = CYNew CYEncodedPart($1, "arrayOf", CYNew CYArgument($3)); } ; diff --git a/Execute.cpp b/Execute.cpp index 601ed9f..70cceae 100644 --- a/Execute.cpp +++ b/Execute.cpp @@ -1097,6 +1097,19 @@ static JSValueRef Type_callAsFunction_pointerTo(JSContextRef context, JSObjectRe return CYMakeType(context, &type); } CYCatch } +static JSValueRef Type_callAsFunction_withName(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { + if (count != 1) + throw CYJSError(context, "incorrect number of arguments to Type.withName"); + Type_privateData *internal(reinterpret_cast(JSObjectGetPrivate(_this))); + + CYPool pool; + const char *name(CYPoolCString(pool, context, arguments[0])); + + sig::Type type(*internal->type_); + type.name = name; + return CYMakeType(context, &type); +} CYCatch } + static JSValueRef Type_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { if (count != 1) throw CYJSError(context, "incorrect number of arguments to type cast function"); @@ -1172,6 +1185,11 @@ static JSValueRef Type_getProperty_alignment(JSContextRef context, JSObjectRef o return CYCastJSValue(context, internal->GetFFI()->alignment); } +static JSValueRef Type_getProperty_name(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { + Type_privateData *internal(reinterpret_cast(JSObjectGetPrivate(object))); + return CYCastJSValue(context, internal->type_->name); +} + static JSValueRef Type_getProperty_size(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { Type_privateData *internal(reinterpret_cast(JSObjectGetPrivate(object))); return CYCastJSValue(context, internal->GetFFI()->size); @@ -1225,16 +1243,18 @@ namespace cy { JSStaticFunction const * const Functor::StaticFunctions = Functor_staticFunctions; } -static JSStaticValue Type_staticValues[3] = { +static JSStaticValue Type_staticValues[4] = { {"alignment", &Type_getProperty_alignment, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, + {"name", &Type_getProperty_name, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {"size", &Type_getProperty_size, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {NULL, NULL, NULL, 0} }; -static JSStaticFunction Type_staticFunctions[7] = { +static JSStaticFunction Type_staticFunctions[8] = { {"arrayOf", &Type_callAsFunction_arrayOf, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {"constant", &Type_callAsFunction_constant, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {"pointerTo", &Type_callAsFunction_pointerTo, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, + {"withName", &Type_callAsFunction_withName, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {"toCYON", &Type_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {"toJSON", &Type_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {"toString", &Type_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, diff --git a/sig/types.hpp b/sig/types.hpp index e6240a1..54da645 100644 --- a/sig/types.hpp +++ b/sig/types.hpp @@ -74,7 +74,7 @@ struct Signature { struct Type { enum Primitive primitive; - char *name; + const char *name; uint8_t flags; union { diff --git a/todo.txt b/todo.txt index 626b4b5..4df4d2d 100644 --- a/todo.txt +++ b/todo.txt @@ -111,3 +111,5 @@ constant flags on types are encoded using something horribly wrong http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtTypeEncodings.html http://gcc.gnu.org/onlinedocs/gcc/Type-encoding.html http://gcc.gnu.org/onlinedocs/gcc/Legacy-type-encoding.html + +using .withName from the grammar is a horrible hack that makes other uses impossible -- 2.47.2