mNo edit summary |
m (107 -> 115.) |
||
Line 2: | Line 2: | ||
|developer=saurik | |developer=saurik | ||
|package=cycript | |package=cycript | ||
|version=0.9. | |version=0.9.115-1 | ||
}} | }} | ||
Line 77: | Line 77: | ||
| ''var''<tt>.$cya()</tt> | | ''var''<tt>.$cya()</tt> | ||
|- | |- | ||
| <tt>@class </tt>''classname''<tt> : </tt>''superclass''<tt> {}<br />+ </tt>''methodname''<tt> {</tt> ''function body'' <tt>}<br />- </tt>''methodname''<tt> {</tt> ''function body'' <tt>}<br />...<br />@end</tt> | |||
| colspan="2" | Declare an Objective-C class. The ''classname'' can be omitted, where an anonymous class will be declared. | | colspan="2" | Declare an Objective-C class. The ''classname'' can be omitted, where an anonymous class will be declared. | ||
|- | |- | ||
Line 85: | Line 85: | ||
| <tt>new </tt>''classname'' | | <tt>new </tt>''classname'' | ||
| colspan="2" | Although not exactly a new syntax, this construction has a new meaning for Objective-C classes. This is similar to <tt>[</tt>''classname''<tt> alloc]</tt>, but the resource will be managed by JavaScriptCore's garbage collector. To fully initialize the class, you need to call <tt>[new </tt>''classname''<tt> initWithFoo:...]</tt>. | | colspan="2" | Although not exactly a new syntax, this construction has a new meaning for Objective-C classes. This is similar to <tt>[</tt>''classname''<tt> alloc]</tt>, but the resource will be managed by JavaScriptCore's garbage collector. To fully initialize the class, you need to call <tt>[new </tt>''classname''<tt> initWithFoo:...]</tt>. | ||
|- | |||
| <tt>for each(</tt>''i''<tt> in </tt>''array''<tt>) ...</tt> | |||
| colspan="2" | Like the traditional [https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Statements/for...in <tt>for/in</tt>], but ''i'' will iterate over all values instead of all keys. | |||
|- | |||
| style="white-space:nowrap;" | <tt>[</tt>''func''<tt> for ''each'' (</tt>''i''<tt> in </tt>''list''<tt>) if (</tt>''condition''<tt>)]</tt> | |||
| colspan="2" | List comprehension, similar to that in Python. The <tt>if</tt> part can be omitted if the list is to be evaluated unconditionally.<br />Example:<tt>[i*i for each (i in [2,3,5,7,11,13,15,17]) if (i % 3 != 0)]</tt> → <tt>[4,25,49,121,169,289]</tt> | |||
|} | |} | ||
Line 152: | Line 158: | ||
| ''obj''<tt>.value</tt> | | ''obj''<tt>.value</tt> | ||
| For some objects, returns the address. | | For some objects, returns the address. | ||
|- | |||
| ''class''<tt>.messages</tt> | |||
| Contains an associative array of messages in the class. The keys are the selector names and the values are implementations (<tt>function</tt>s). | |||
|- | |- | ||
| <tt>system.print(</tt>''string''<tt>)</tt> | | <tt>system.print(</tt>''string''<tt>)</tt> |
Revision as of 16:05, 19 October 2009
Cycript | |
Cydia Package | |
---|---|
Developer | saurik |
Package ID | cycript |
Latest Version | 0.9.115-1 |
Cycript is a Javascript interpreter which also understands Objective-C syntax. The cycript binary is also a REPL for this language.
Besides evaluating scripts, Cycript can also hook into a running process (using cycript -p pid) and modify its property.
Currently Cycript is in beta, so information on this page may change over time.
JS/ObjC Object Bridging
Some native Javascript types are bridged to the corresponding Objective-C types for convenient, so you can use
[[41,"foo",true,[8,6],{a:12,b:46},36] indexOfObject:"foo"]
instead of
[[NSArray arrayWithObjects:
[NSNumber numberWithInt:41],
"foo",
[NSNumber numberWithBool:YES],
[NSArray arrayWithObjects:[NSNumber numberWithInt:8], [NSNumber numberWithInt:6], nil],
[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:12], "a",
[NSNumber numberWithInt:46], "b",
nil],
[NSNumber numberWithInt:36],
nil] indexOfObject:"foo"]
JS type | ObjC type |
---|---|
number | NSNumber (CFNumber) |
boolean | NSNumber (CFBoolean) |
string | NSString |
Array | NSArray |
object (Associative array) | NSDictionary |
null in Cycript is equivalent to nil in Objective-C. Additionally, nil, YES and NO are also defined in Cycript.
Additional syntax
New Syntax | Meaning | "Desugared" representation |
---|---|---|
[obj msg:var] | Sends msg to obj with parameters var. This is the Objective-C's message sending syntax. | objc_msgSend(obj, @selector(msg).value, var ...) |
@selector(selname) | Returns the selector named selname with Objective-C syntax. | new Selector("selname"); |
obj->ivar | Obtain the instant variable ivar of an Objective-C object obj. | obj.$cyi.ivar |
*ptr | Dereference the pointer, or list all ivars of an object (so that you can access them using (*obj).ivar). | ptr.$cyi |
&var | Takes the address of a variable. Only instances of ObjC class can have addresses. | var.$cya() |
@class classname : superclass {} + methodname { function body } - methodname { function body } ... @end |
Declare an Objective-C class. The classname can be omitted, where an anonymous class will be declared. | |
@class existingclass + methodname { function body } - methodname { function body } ... @end |
Insert extra methods to an existing class. The existingclass itself can be an expression e.g. @class ([obj class]) .... | |
new classname | Although not exactly a new syntax, this construction has a new meaning for Objective-C classes. This is similar to [classname alloc], but the resource will be managed by JavaScriptCore's garbage collector. To fully initialize the class, you need to call [new classname initWithFoo:...]. | |
for each(i in array) ... | Like the traditional for/in, but i will iterate over all values instead of all keys. | |
[func for each (i in list) if (condition)] | List comprehension, similar to that in Python. The if part can be omitted if the list is to be evaluated unconditionally. Example:[i*i for each (i in [2,3,5,7,11,13,15,17]) if (i % 3 != 0)] → [4,25,49,121,169,289] |
REPL-only additions
These are used for debugging.
Line | Usage |
---|---|
?debug | Toggles debug output. |
?bypass | Bypass syntax error pretty-printing. |
?expand | Toggles whether to display the line break characters, etc. as really a line break or just \n. |
Additional types
Type/Constructor | Usage |
---|---|
Selector(selname) | Declare a selector. |
Functor(function body, type encoding) | Associate an Objective-C type encoding to a function, e.g. new Functor(function(x,y){return (x+y).toString(16);}, "*dd"); to declare a (double, double) → char* function. |
Pointer(address, type encoding) | Treat the input number as a pointer. Like C pointers, the result can be dereferenced using * and subscripted using [i], but pointer arithmetic is not directly supported. |
Type(type encoding) | Create a type. The resulting value can be new-ed to get a Pointer, e.g. var p = new new Type("d");. To deallocate the pointer, use the free() function. |
Instance(address) | Treat the address as an instance of Objective-C object. |
Additional variables and methods
Variable | Meaning |
---|---|
_ | Last evaluated value (REPL only). |
ObjectiveC.images | An associative array, with keys beings the path of loaded libraries, and value is the classes of this library. |
ObjectiveC.classes | An associative array of classes. The keys are class names and the values are the classes themselves. |
ObjectiveC.protocols | An associative array of protocols. The keys are protocol names and the values are the protocols themselves. |
obj.toJSON() | Convert the object to JSON. |
obj.toCYON() | Convert the object to CYON (Cycript object notation). |
obj.value | For some objects, returns the address. |
class.messages | Contains an associative array of messages in the class. The keys are the selector names and the values are implementations (functions). |
system.print(string) | Print the string to syslog. |
system.args | Parameters of the executable |
References
- Official website: http://www.cycript.org/
- SVN trunk: http://svn.saurik.com/repos/cycript/trunk/
|