Joy Online Manual
Data Types
Table of Contents |
Introduction | ||
id, Class | ||
SEL | ||
JSVal | ||
BOOL | ||
Numerical Types | ||
Pointers and Arrays | ||
Structures and Unions |
Introduction
JavaScript is a loosely typed language. Objective-C is a strongly typed language. This means that Joy has to extend JavaScript with language features for data types and declarations. Joy borrows all of those language features from C, so you don't have to learn any new syntax. All Objective-C data types are supported, including pointers, structs, unions, bitfields, function pointers, and long long integers. Joy treats automatic conversion between data types (sometimes called coercion, e.g. when a JavaScript string is passed to a method expecting an integer) differently from explicit conversion via the type-cast operator. Automatic conversions follow the JavaScript philosophy of maximum programmer-friendliness, while type casts follow C semantics as far as possible. E.g., in the case of a JavaScript string being passed as an integer, Joy would try to interpret the string numerically and report an error message on failure, while type-casting the same string to int would result in a number corresponding to the string's memory address. If you assign any JavaScript value to a variable or property with a known type, the result of the assignment expression will be the auto-converted value, not necessarily the value from the right hand side of the assignment! Joy automatically converts between Java and Objective-C object types. To force a conversion, use the Java.toObjC and Java.toJava commands.
|
Objective-C instances and class objects are reflected by JavaScript objects of the Id class. To guard your program from fatal run-time errors, Joy keeps a list of "known" Objective-C id's. Any access to an object that is not "known" to Joy will fail with an error message. By setting the boolean property ObjC.registerIdsOnAlloc to true you can cause Joy to register new id's at the moment they are allocated - this has a certain impact on application performance, but can be useful for debugging or exploration purposes, when you need information about all objects created by the application, not just those that pass through Joy.
All class and metaclass objects known to the Objective-C run-time environment are automatically known to Joy, too. Instance objects can become "known" through either of the following events:
An object is deleted from the list of "known" objects when its memory is freed by the Objective-C run-time system or when you call the ItkObjC_UnregisterId function on it from C code. |
AUTOMATIC CONVERSIONS |
null | nil | |
Id Object | corresponding Objective-C id | |
java.lang.Object | result of passing the object across the Java/Objective-C bridge | |
Array Object | NSArray containing converted elements | |
Other Object | [ITKJSVal jsObject: value] | |
String | [NSString stringWithJSVal: value] | |
Integer | [NSNumber numberWithInt: value] | |
Double | [NSNumber numberWithDouble: value] | |
Boolean | [NSNumber numberWithBool: value] |
EXAMPLES |
id
Class
id <NSCopying>
NSButton *
SEL
DESCRIPTION |
Objective-C message selectors are reflected by their numerical value. On automatic conversion from a JavaScript value to an Objective-C selector, Joy will alternatively accept the selector name as a string. To get the numerical value of a selector at compile time you can use @selector(name). |
AUTOMATIC CONVERSIONS |
null | (SEL)0 | |
String | NSSelectorFromString(value) | |
Integer | (SEL)value |
JSVal
DESCRIPTION |
The special data type JSVal allows you to pass JavaScript values between Objective-C and JavaScript directly, without any conversion. Any value expressible in JavaScript is a valid JSVal.
In Objective-C, JSVal is implemented as a typedef equivalent to the jsval data type from jsapi.h, which is again a typedef for a 4-byte integer. To be able to distinguish between normal integers and jsval's in Objective-C method signatures, Joy adds the JSVal typedef. This is a union containing the jsval as the only changeable field, and a few other fields that are present only to mark that union actually is a JSVal. |
AUTOMATIC CONVERSIONS |
Anything | JSVal(value) |
Numerical Types
DESCRIPTION |
The numerical types (including char) are represented as JavaScript numbers (either 30-bit integers or double precision floating point numbers), with the exception of long long and unsigned long long which are reflected by JavaScript objects of the LongLong or UnsignedLongLong class. Joy JavaScript understands char literals and size letters after numeric constants. |
AUTOMATIC CONVERSIONS |
null | (type)0 | |
Object | numerical interpretation, or error | |
String | numerical interpretation, or error | |
Integer | (type)value | |
Double | (type)value | |
Boolean | (type)value |
EXAMPLES |
char ch = 'A'
char z = '\0'
unsigned int x = 4000000000
double pi = M_PI
u = 1234567890123456789ULL
BOOL |
If you have used the Tcl version of Joy, you may remember the problem regarding the equivalence between BOOL and char. There is no such problem in Joy JavaScript, because char is now a numerical type. |
Pointers
DESCRIPTION |
Joy understands all pointer data types that exist in C, including function pointers, and pointers to arrays. Both pointers and arrays are represented as JavaScript objects of the Pointer class. Also, any id or Class data value can be used exactly like a pointer to a struct containing all its instance variables.
You use pointers like you do in C. Joy even supports pointer arithmetic. Joy 2.0 had a problem with comparison operators not working correctly on pointers: This has been fixed in Joy 2.1. |
AUTOMATIC CONVERSIONS |
null | (type *)NULL | |
Pointer Object | corresponding Objective-C pointer | |
Id Object | address of corresponding Objective-C object | |
String | address of value as a C-string, valid until value is garbage-collected or current NSAutoreleasePool is emptied, whatever happens later |
EXAMPLES |
const char *str = "xyz"
void (*fp)(char *, int **)
char *(*b)[20]
Arrays
DESCRIPTION |
Both arrays and pointers are represented as JavaScript objects of the Pointer class. |
AUTOMATIC CONVERSIONS |
Array Object | first n elements converted to binary, undefined elements are converted to binary 0 | |
String | only for char arrays, first n characters, 0-padded if less than n characters |
EXAMPLES |
void *a[10] = [null]
char x[256] = "string"
Structures and Unions
DESCRIPTION |
Joy allows you to define structs and unions with tag names, and refer to them later using struct tag or union tag, like C (of course you can also use typedefs). Bitfields are supported inside structs and unions using standard C syntax. Also, you can use the Objective-C @defs keyword to convert any Objective-C class into a struct.
C structs and unions are reflected by JavaScript objects of the Struct and Union class, respectively. Be aware that these objects internally maintain only a pointer to the real location of the data (otherwise s.u.x[0] = 5; would not have any effect on s). This means you have to be a little careful with assignments. The following assignment will always work: |
struct mystruct s = somestruct;
But this might not work as expected: |
var s2 = s;
Because the first assignment will copy the contents of somestruct, while the second one will just setup s2 as an alias for s. Especially if the memory pointed to by s might become invalid (such as when s is a local variable, and s2 is global), you are asking for trouble. So, always declare variables holding structs or unions, unless you know what you are doing. Another way to make the second assignment work is: |
var s2 = Struct (s);
This will create a new Struct object which has its own private memory, and is initialized to the contents of s. This private memory will be freed automatically when the Struct object is garbage-collected.
When Joy converts from a JavaScript value to a C struct, it will accept any object that has the correct properties, not just Struct objects. For each field of the struct, it will first try object[index], where index is the index of the field (0 is the first entry). If that fails, but the struct definition does include a name for the field, it will try to get the property object.name. This means that you can pass JavaScript arrays or object literals for C structs. For unions there is no such convenience; declare and initialize them explicitly. |
AUTOMATIC CONVERSIONS |
Object | see above |
EXAMPLES |
struct mystruct {
int x, y;
short a : 3, : 3, b : 10;
union {
int x [20];
double d [10];
} u;
}
struct _NSButton { @defs(NSButton) }
Index |