X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/dea834b05466e2e8894197360ec38b84ff206f23..953647c120fbe1d8b7c59941ef5df5d35031c2af:/Library.mm?ds=sidebyside diff --git a/Library.mm b/Library.mm index 0dceaea..4b6b7bc 100644 --- a/Library.mm +++ b/Library.mm @@ -60,6 +60,7 @@ #include #include #include +#include #include #include @@ -81,13 +82,31 @@ CFLog(kCFLogLevelNotice, CFSTR("_trace():%u"), __LINE__); \ } while (false) - -static JSContextRef Context_; +#define CYPoolTry { \ + id _saved(nil); \ + NSAutoreleasePool *_pool([[NSAutoreleasePool alloc] init]); \ + @try +#define CYPoolCatch(value) \ + @catch (NSException *error) { \ + _saved = [error retain]; \ + @throw; \ + return value; \ + } @finally { \ + [_pool release]; \ + if (_saved != nil) \ + [_saved autorelease]; \ + } \ +} + +static JSGlobalContextRef Context_; +static JSObjectRef System_; static JSClassRef Functor_; static JSClassRef Instance_; static JSClassRef Pointer_; +static JSClassRef Runtime_; static JSClassRef Selector_; +static JSClassRef Struct_; static JSObjectRef Array_; static JSObjectRef Function_; @@ -98,15 +117,141 @@ static JSStringRef length_; static Class NSCFBoolean_; -static NSMutableDictionary *Bridge_; +static NSArray *Bridge_; struct Client { CFHTTPMessageRef message_; CFSocketRef socket_; }; -JSObjectRef CYMakeObject(JSContextRef context, id object) { - return JSObjectMake(context, Instance_, [object retain]); +struct CYData { + apr_pool_t *pool_; + + virtual ~CYData() { + } + + void *operator new(size_t size) { + apr_pool_t *pool; + apr_pool_create(&pool, NULL); + void *data(apr_palloc(pool, size)); + reinterpret_cast(data)->pool_ = pool; + return data;; + } + + static void Finalize(JSObjectRef object) { + CYData *data(reinterpret_cast(JSObjectGetPrivate(object))); + data->~CYData(); + apr_pool_destroy(data->pool_); + } +}; + +struct Pointer_privateData : + CYData +{ + void *value_; + sig::Type type_; + + Pointer_privateData(void *value) : + value_(value) + { + } +}; + +struct Functor_privateData : Pointer_privateData { + sig::Signature signature_; + ffi_cif cif_; + + Functor_privateData(const char *type, void (*value)()) : + Pointer_privateData(reinterpret_cast(value)) + { + sig::Parse(pool_, &signature_, type); + sig::sig_ffi_cif(pool_, &sig::ObjectiveC, &signature_, &cif_); + } +}; + +struct ffoData : Functor_privateData { + JSContextRef context_; + JSObjectRef function_; + + ffoData(const char *type) : + Functor_privateData(type, NULL) + { + } +}; + +struct Selector_privateData : Pointer_privateData { + Selector_privateData(SEL value) : + Pointer_privateData(value) + { + } + + SEL GetValue() const { + return reinterpret_cast(value_); + } +}; + +struct Instance_privateData : Pointer_privateData { + bool transient_; + + Instance_privateData(id value, bool transient) : + Pointer_privateData(value) + { + } + + virtual ~Instance_privateData() { + if (!transient_) + [GetValue() release]; + } + + id GetValue() const { + return reinterpret_cast(value_); + } +}; + +struct Struct_privateData : CYData { +}; + +JSObjectRef CYMakeInstance(JSContextRef context, id object, bool transient) { + if (!transient) + object = [object retain]; + Instance_privateData *data(new Instance_privateData(object, transient)); + return JSObjectMake(context, Instance_, data); +} + +const char *CYPoolCString(apr_pool_t *pool, NSString *value) { + if (pool == NULL) + return [value UTF8String]; + else { + size_t size([value maximumLengthOfBytesUsingEncoding:NSUTF8StringEncoding] + 1); + char *string(new(pool) char[size]); + if (![value getCString:string maxLength:size encoding:NSUTF8StringEncoding]) + @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"[NSString getCString:maxLength:encoding:] == NO" userInfo:nil]; + return string; + } +} + +JSValueRef CYCastJSValue(JSContextRef context, bool value) { + return JSValueMakeBoolean(context, value); +} + +JSValueRef CYCastJSValue(JSContextRef context, double value) { + return JSValueMakeNumber(context, value); +} + +#define CYCastJSValue_(Type_) \ + JSValueRef CYCastJSValue(JSContextRef context, Type_ value) { \ + return JSValueMakeNumber(context, static_cast(value)); \ + } + +CYCastJSValue_(int) +CYCastJSValue_(unsigned int) +CYCastJSValue_(long int) +CYCastJSValue_(long unsigned int) +CYCastJSValue_(long long int) +CYCastJSValue_(long long unsigned int) + +JSValueRef CYJSUndefined(JSContextRef context) { + return JSValueMakeUndefined(context); } @interface NSMethodSignature (Cycript) @@ -116,7 +261,10 @@ JSObjectRef CYMakeObject(JSContextRef context, id object) { @interface NSObject (Cycript) - (bool) cy$isUndefined; - (NSString *) cy$toJSON; -- (JSValueRef) cy$JSValueInContext:(JSContextRef)context; +- (JSValueRef) cy$JSValueInContext:(JSContextRef)context transient:(bool)transient; +- (NSObject *) cy$getProperty:(NSString *)name; +- (bool) cy$setProperty:(NSString *)name to:(NSObject *)value; +- (bool) cy$deleteProperty:(NSString *)name; @end @interface NSString (Cycript) @@ -137,8 +285,24 @@ JSObjectRef CYMakeObject(JSContextRef context, id object) { return [self description]; } -- (JSValueRef) cy$JSValueInContext:(JSContextRef)context { - return CYMakeObject(context, self); +- (JSValueRef) cy$JSValueInContext:(JSContextRef)context transient:(bool)transient { + return CYMakeInstance(context, self, transient); +} + +- (NSObject *) cy$getProperty:(NSString *)name { + if (![name isEqualToString:@"prototype"]) + NSLog(@"get:%@", name); + return nil; +} + +- (bool) cy$setProperty:(NSString *)name to:(NSObject *)value { + NSLog(@"set:%@", name); + return false; +} + +- (bool) cy$deleteProperty:(NSString *)name { + NSLog(@"delete:%@", name); + return false; } @end @@ -153,8 +317,16 @@ JSObjectRef CYMakeObject(JSContextRef context, id object) { return @"undefined"; } -- (JSValueRef) cy$JSValueInContext:(JSContextRef)context { - return JSValueMakeUndefined(context); +- (JSValueRef) cy$JSValueInContext:(JSContextRef)context transient:(bool)transient { + return CYJSUndefined(context); +} + +@end + +@implementation NSNull (Cycript) + +- (NSString *) cy$toJSON { + return @"null"; } @end @@ -183,6 +355,38 @@ JSObjectRef CYMakeObject(JSContextRef context, id object) { return json; } +- (NSObject *) cy$getProperty:(NSString *)name { + int index([name intValue]); + if (index < 0 || index >= static_cast([self count])) + return [super cy$getProperty:name]; + else + return [self objectAtIndex:index]; +} + +@end + +@implementation NSMutableArray (Cycript) + +- (bool) cy$setProperty:(NSString *)name to:(NSObject *)value { + int index([name intValue]); + if (index < 0 || index >= static_cast([self count])) + return [super cy$setProperty:name to:value]; + else { + [self replaceObjectAtIndex:index withObject:(value ?: [NSNull null])]; + return true; + } +} + +- (bool) cy$deleteProperty:(NSString *)name { + int index([name intValue]); + if (index < 0 || index >= static_cast([self count])) + return [super cy$deleteProperty:name]; + else { + [self removeObjectAtIndex:index]; + return true; + } +} + @end @implementation NSDictionary (Cycript) @@ -207,6 +411,28 @@ JSObjectRef CYMakeObject(JSContextRef context, id object) { return json; } +- (NSObject *) cy$getProperty:(NSString *)name { + return [self objectForKey:name]; +} + +@end + +@implementation NSMutableDictionary (Cycript) + +- (bool) cy$setProperty:(NSString *)name to:(NSObject *)value { + [self setObject:(value ?: [NSNull null]) forKey:name]; + return true; +} + +- (bool) cy$deleteProperty:(NSString *)name { + if ([self objectForKey:name] == nil) + return false; + else { + [self removeObjectForKey:name]; + return true; + } +} + @end @implementation NSNumber (Cycript) @@ -215,8 +441,8 @@ JSObjectRef CYMakeObject(JSContextRef context, id object) { return [self class] != NSCFBoolean_ ? [self stringValue] : [self boolValue] ? @"true" : @"false"; } -- (JSValueRef) cy$JSValueInContext:(JSContextRef)context { - return [self class] != NSCFBoolean_ ? JSValueMakeNumber(context, [self doubleValue]) : JSValueMakeBoolean(context, [self boolValue]); +- (JSValueRef) cy$JSValueInContext:(JSContextRef)context transient:(bool)transient { + return [self class] != NSCFBoolean_ ? CYCastJSValue(context, [self doubleValue]) : CYCastJSValue(context, [self boolValue]); } - (void *) cy$symbol { @@ -243,7 +469,8 @@ JSObjectRef CYMakeObject(JSContextRef context, id object) { } - (void *) cy$symbol { - return dlsym(RTLD_DEFAULT, [self UTF8String]); + CYPool pool; + return dlsym(RTLD_DEFAULT, CYPoolCString(pool, self)); } @end @@ -278,66 +505,113 @@ JSObjectRef CYMakeObject(JSContextRef context, id object) { CYRange WordStartRange_(0x1000000000LLU,0x7fffffe87fffffeLLU); // A-Za-z_$ CYRange WordEndRange_(0x3ff001000000000LLU,0x7fffffe87fffffeLLU); // A-Za-z_$0-9 -JSContextRef CYGetJSContext() { +JSGlobalContextRef CYGetJSContext() { return Context_; } +#define CYTry \ + @try #define CYCatch \ @catch (id error) { \ + NSLog(@"e:%@", error); \ CYThrow(context, error, exception); \ return NULL; \ } void CYThrow(JSContextRef context, JSValueRef value); -id CYCastNSObject(JSContextRef context, JSObjectRef object) { - if (JSValueIsObjectOfClass(context, object, Instance_)) - return reinterpret_cast(JSObjectGetPrivate(object)); +apr_status_t CYPoolRelease_(void *data) { + id object(reinterpret_cast(data)); + [object release]; + return APR_SUCCESS; +} + +id CYPoolRelease(apr_pool_t *pool, id object) { + if (pool == NULL) + return [object autorelease]; + else { + apr_pool_cleanup_register(pool, object, &CYPoolRelease_, &apr_pool_cleanup_null); + return object; + } +} + +CFTypeRef CYPoolRelease(apr_pool_t *pool, CFTypeRef object) { + return (CFTypeRef) CYPoolRelease(pool, (id) object); +} + +id CYCastNSObject(apr_pool_t *pool, JSContextRef context, JSObjectRef object) { + if (JSValueIsObjectOfClass(context, object, Instance_)) { + Instance_privateData *data(reinterpret_cast(JSObjectGetPrivate(object))); + return data->GetValue(); + } + JSValueRef exception(NULL); bool array(JSValueIsInstanceOfConstructor(context, object, Array_, &exception)); CYThrow(context, exception); - if (array) - return [[[CYJSArray alloc] initWithJSObject:object inContext:context] autorelease]; - return [[[CYJSObject alloc] initWithJSObject:object inContext:context] autorelease]; + id value(array ? [CYJSArray alloc] : [CYJSObject alloc]); + return CYPoolRelease(pool, [value initWithJSObject:object inContext:context]); } JSStringRef CYCopyJSString(id value) { - return JSStringCreateWithCFString(reinterpret_cast([value description])); + return value == NULL ? NULL : JSStringCreateWithCFString(reinterpret_cast([value description])); } JSStringRef CYCopyJSString(const char *value) { - return JSStringCreateWithUTF8CString(value); + return value == NULL ? NULL : JSStringCreateWithUTF8CString(value); } JSStringRef CYCopyJSString(JSStringRef value) { - return JSStringRetain(value); + return value == NULL ? NULL : JSStringRetain(value); } JSStringRef CYCopyJSString(JSContextRef context, JSValueRef value) { + if (JSValueIsNull(context, value)) + return NULL; JSValueRef exception(NULL); JSStringRef string(JSValueToStringCopy(context, value, &exception)); CYThrow(context, exception); return string; } -// XXX: this is not a safe handle class CYJSString { private: JSStringRef string_; + void Clear_() { + JSStringRelease(string_); + } + public: + CYJSString(const CYJSString &rhs) : + string_(CYCopyJSString(rhs.string_)) + { + } + template - CYJSString(Arg0_ arg0) { - string_ = CYCopyJSString(arg0); + CYJSString(Arg0_ arg0) : + string_(CYCopyJSString(arg0)) + { } template - CYJSString(Arg0_ arg0, Arg1_ arg1) { - string_ = CYCopyJSString(arg0, arg1); + CYJSString(Arg0_ arg0, Arg1_ arg1) : + string_(CYCopyJSString(arg0, arg1)) + { + } + + CYJSString &operator =(const CYJSString &rhs) { + Clear_(); + string_ = CYCopyJSString(rhs.string_); + return *this; } ~CYJSString() { - JSStringRelease(string_); + Clear_(); + } + + void Clear() { + Clear_(); + string_ = NULL; } operator JSStringRef() const { @@ -365,50 +639,111 @@ CFNumberRef CYCopyCFNumber(JSContextRef context, JSValueRef value) { return CFNumberCreate(kCFAllocatorDefault, kCFNumberDoubleType, &number); } -NSString *CYCastNSString(JSStringRef value) { - return [reinterpret_cast(CYCopyCFString(value)) autorelease]; +CFStringRef CYCopyCFString(const char *value) { + return CFStringCreateWithCString(kCFAllocatorDefault, value, kCFStringEncodingUTF8); } -CFTypeRef CYCopyCFType(JSContextRef context, JSValueRef value) { +NSString *CYCastNSString(apr_pool_t *pool, const char *value) { + return (NSString *) CYPoolRelease(pool, CYCopyCFString(value)); +} + +NSString *CYCastNSString(apr_pool_t *pool, JSStringRef value) { + return (NSString *) CYPoolRelease(pool, CYCopyCFString(value)); +} + +bool CYCastBool(JSContextRef context, JSValueRef value) { + return JSValueToBoolean(context, value); +} + +CFTypeRef CYCFType(apr_pool_t *pool, JSContextRef context, JSValueRef value, bool cast) { + CFTypeRef object; + bool copy; + switch (JSType type = JSValueGetType(context, value)) { case kJSTypeUndefined: - return CFRetain([WebUndefined undefined]); + object = [WebUndefined undefined]; + copy = false; + break; + case kJSTypeNull: - return nil; + return NULL; + break; + case kJSTypeBoolean: - return CFRetain(JSValueToBoolean(context, value) ? kCFBooleanTrue : kCFBooleanFalse); + object = CYCastBool(context, value) ? kCFBooleanTrue : kCFBooleanFalse; + copy = false; + break; + case kJSTypeNumber: - return CYCopyCFNumber(context, value); + object = CYCopyCFNumber(context, value); + copy = true; + break; + case kJSTypeString: - return CYCopyCFString(context, value); + object = CYCopyCFString(context, value); + copy = true; + break; + case kJSTypeObject: - return CFRetain((CFTypeRef) CYCastNSObject(context, (JSObjectRef) value)); + // XXX: this might could be more efficient + object = (CFTypeRef) CYCastNSObject(pool, context, (JSObjectRef) value); + copy = false; + break; + default: @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:[NSString stringWithFormat:@"JSValueGetType() == 0x%x", type] userInfo:nil]; + break; } + + if (cast != copy) + return object; + else if (copy) + return CYPoolRelease(pool, object); + else + return CFRetain(object); +} + +CFTypeRef CYCastCFType(apr_pool_t *pool, JSContextRef context, JSValueRef value) { + return CYCFType(pool, context, value, true); +} + +CFTypeRef CYCopyCFType(apr_pool_t *pool, JSContextRef context, JSValueRef value) { + return CYCFType(pool, context, value, false); } NSArray *CYCastNSArray(JSPropertyNameArrayRef names) { + CYPool pool; size_t size(JSPropertyNameArrayGetCount(names)); NSMutableArray *array([NSMutableArray arrayWithCapacity:size]); for (size_t index(0); index != size; ++index) - [array addObject:CYCastNSString(JSPropertyNameArrayGetNameAtIndex(names, index))]; + [array addObject:CYCastNSString(pool, JSPropertyNameArrayGetNameAtIndex(names, index))]; return array; } -id CYCastNSObject(JSContextRef context, JSValueRef value) { - const NSObject *object(reinterpret_cast(CYCopyCFType(context, value))); - return object == nil ? nil : [object autorelease]; +id CYCastNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef value) { + return reinterpret_cast(CYCastCFType(pool, context, value)); } void CYThrow(JSContextRef context, JSValueRef value) { if (value == NULL) return; - @throw CYCastNSObject(context, value); + @throw CYCastNSObject(NULL, context, value); +} + +JSValueRef CYJSNull(JSContextRef context) { + return JSValueMakeNull(context); +} + +JSValueRef CYCastJSValue(JSContextRef context, JSStringRef value) { + return value == NULL ? CYJSNull(context) : JSValueMakeString(context, value); } -JSValueRef CYCastJSValue(JSContextRef context, id value) { - return value == nil ? JSValueMakeNull(context) : [value cy$JSValueInContext:context]; +JSValueRef CYCastJSValue(JSContextRef context, const char *value) { + return CYCastJSValue(context, CYJSString(value)); +} + +JSValueRef CYCastJSValue(JSContextRef context, id value, bool transient = true) { + return value == nil ? CYJSNull(context) : [value cy$JSValueInContext:context transient:transient]; } JSObjectRef CYCastJSObject(JSContextRef context, JSValueRef value) { @@ -432,6 +767,8 @@ void CYSetProperty(JSContextRef context, JSObjectRef object, JSStringRef name, J } void CYThrow(JSContextRef context, id error, JSValueRef *exception) { + if (exception == NULL) + throw error; *exception = CYCastJSValue(context, error); } @@ -452,7 +789,7 @@ void CYThrow(JSContextRef context, id error, JSValueRef *exception) { } - (id) objectForKey:(id)key { - return CYCastNSObject(context_, CYGetProperty(context_, object_, CYJSString(key))); + return CYCastNSObject(NULL, context_, CYGetProperty(context_, object_, CYJSString(key))) ?: [NSNull null]; } - (NSEnumerator *) keyEnumerator { @@ -492,15 +829,26 @@ void CYThrow(JSContextRef context, id error, JSValueRef *exception) { JSValueRef exception(NULL); JSValueRef value(JSObjectGetPropertyAtIndex(context_, object_, index, &exception)); CYThrow(context_, exception); - id object(CYCastNSObject(context_, value)); - return object == nil ? [NSNull null] : object; + return CYCastNSObject(NULL, context_, value) ?: [NSNull null]; } @end -CFStringRef CYCopyJSONString(JSContextRef context, JSValueRef value) { - id object(CYCastNSObject(context, value)); - return reinterpret_cast([(object == nil ? @"null" : [object cy$toJSON]) retain]); +CFStringRef CYCopyJSONString(JSContextRef context, JSValueRef value, JSValueRef *exception) { + CYTry { + CYPoolTry { + id object(CYCastNSObject(NULL, context, value)); + return reinterpret_cast([(object == nil ? @"null" : [object cy$toJSON]) retain]); + } CYPoolCatch(NULL) + } CYCatch +} + +const char *CYPoolJSONString(apr_pool_t *pool, JSContextRef context, JSValueRef value, JSValueRef *exception) { + if (NSString *json = (NSString *) CYCopyJSONString(context, value, exception)) { + const char *string(CYPoolCString(pool, json)); + [json release]; + return string; + } else return NULL; } static void OnData(CFSocketRef socket, CFSocketCallBackType type, CFDataRef address, const void *value, void *info) { @@ -532,7 +880,7 @@ static void OnData(CFSocketRef socket, CFSocketCallBackType type, CFDataRef addr CFHTTPMessageRef response(CFHTTPMessageCreateResponse(kCFAllocatorDefault, 200, NULL, kCFHTTPVersion1_1)); CFHTTPMessageSetHeaderFieldValue(response, CFSTR("Content-Type"), CFSTR("application/json; charset=utf-8")); - CFStringRef json(CYCopyJSONString(CYGetJSContext(), result)); + CFStringRef json(CYCopyJSONString(CYGetJSContext(), result, NULL)); CFDataRef body(CFStringCreateExternalRepresentation(kCFAllocatorDefault, json, kCFStringEncodingUTF8, NULL)); CFRelease(json); @@ -576,109 +924,86 @@ static void OnAccept(CFSocketRef socket, CFSocketCallBackType type, CFDataRef ad } } -static JSValueRef Instance_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { _pooled - @try { - NSString *name(CYCastNSString(property)); - NSLog(@"%@", name); - return NULL; +static JSValueRef Instance_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { + CYTry { + CYPool pool; + NSString *self(CYCastNSObject(pool, context, object)); + NSString *name(CYCastNSString(pool, property)); + NSObject *data([self cy$getProperty:name]); + return data == nil ? NULL : CYCastJSValue(context, data); } CYCatch } -typedef id jocData; - -static JSObjectRef Instance_callAsConstructor(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { _pooled - @try { - id data(reinterpret_cast(JSObjectGetPrivate(object))); - return CYMakeObject(context, [[data alloc] autorelease]); +static bool Instance_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { + CYTry { + CYPool pool; + NSString *self(CYCastNSObject(pool, context, object)); + NSString *name(CYCastNSString(pool, property)); + NSString *data(CYCastNSObject(pool, context, value)); + return [self cy$setProperty:name to:data]; } CYCatch } -struct ptrData { - apr_pool_t *pool_; - void *value_; - sig::Type type_; - - void *operator new(size_t size) { - apr_pool_t *pool; - apr_pool_create(&pool, NULL); - void *data(apr_palloc(pool, size)); - reinterpret_cast(data)->pool_ = pool; - return data;; - } - - ptrData(void *value) : - value_(value) - { - } -}; - -struct ffiData : ptrData { - sig::Signature signature_; - ffi_cif cif_; - - ffiData(void (*value)(), const char *type) : - ptrData(reinterpret_cast(value)) - { - sig::Parse(pool_, &signature_, type); - sig::sig_ffi_cif(pool_, &sig::ObjectiveC, &signature_, &cif_); - } -}; +static bool Instance_deleteProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { + CYTry { + CYPool pool; + NSString *self(CYCastNSObject(pool, context, object)); + NSString *name(CYCastNSString(pool, property)); + return [self cy$deleteProperty:name]; + } CYCatch +} -struct selData : ptrData { - selData(SEL value) : - ptrData(value) - { - } -}; +static JSObjectRef Instance_callAsConstructor(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { + CYTry { + Instance_privateData *data(reinterpret_cast(JSObjectGetPrivate(object))); + return CYMakeInstance(context, [data->GetValue() alloc], true); + } CYCatch +} JSObjectRef CYMakeSelector(JSContextRef context, SEL sel) { - selData *data(new selData(sel)); + Selector_privateData *data(new Selector_privateData(sel)); return JSObjectMake(context, Selector_, data); } JSObjectRef CYMakePointer(JSContextRef context, void *pointer) { - ptrData *data(new ptrData(pointer)); + Pointer_privateData *data(new Pointer_privateData(pointer)); return JSObjectMake(context, Pointer_, data); } -static void Pointer_finalize(JSObjectRef object) { - ptrData *data(reinterpret_cast(JSObjectGetPrivate(object))); - apr_pool_destroy(data->pool_); -} - -static void Instance_finalize(JSObjectRef object) { - id data(reinterpret_cast(JSObjectGetPrivate(object))); - [data release]; -} - -JSObjectRef CYMakeFunction(JSContextRef context, void (*function)(), const char *type) { - ffiData *data(new ffiData(function, type)); +JSObjectRef CYMakeFunctor(JSContextRef context, void (*function)(), const char *type) { + Functor_privateData *data(new Functor_privateData(type, function)); return JSObjectMake(context, Functor_, data); } -JSObjectRef CYMakeFunction(JSContextRef context, void *function, const char *type) { - return CYMakeFunction(context, reinterpret_cast(function), type); -} - -char *CYPoolCString(apr_pool_t *pool, JSStringRef value) { - size_t size(JSStringGetMaximumUTF8CStringSize(value)); - char *string(new(pool) char[size]); - JSStringGetUTF8CString(value, string, size); - JSStringRelease(value); - return string; +const char *CYPoolCString(apr_pool_t *pool, JSStringRef value) { + if (pool == NULL) + return [CYCastNSString(NULL, value) UTF8String]; + else { + size_t size(JSStringGetMaximumUTF8CStringSize(value)); + char *string(new(pool) char[size]); + JSStringGetUTF8CString(value, string, size); + return string; + } } -char *CYPoolCString(apr_pool_t *pool, JSContextRef context, JSValueRef value) { +const char *CYPoolCString(apr_pool_t *pool, JSContextRef context, JSValueRef value) { + if (JSValueIsNull(context, value)) + return NULL; return CYPoolCString(pool, CYJSString(context, value)); } // XXX: this macro is unhygenic #define CYCastCString(context, value) ({ \ - JSStringRef string(CYCopyJSString(context, value)); \ - size_t size(JSStringGetMaximumUTF8CStringSize(string)); \ - char *utf8(reinterpret_cast(alloca(size))); \ - JSStringGetUTF8CString(string, utf8, size); \ - JSStringRelease(string); \ + char *utf8; \ + if (value == NULL) \ + utf8 = NULL; \ + else if (JSStringRef string = CYCopyJSString(context, value)) { \ + size_t size(JSStringGetMaximumUTF8CStringSize(string)); \ + utf8 = reinterpret_cast(alloca(size)); \ + JSStringGetUTF8CString(string, utf8, size); \ + JSStringRelease(string); \ + } else \ + utf8 = NULL; \ utf8; \ }) @@ -686,28 +1011,36 @@ SEL CYCastSEL(JSContextRef context, JSValueRef value) { if (JSValueIsNull(context, value)) return NULL; else if (JSValueIsObjectOfClass(context, value, Selector_)) { - selData *data(reinterpret_cast(JSObjectGetPrivate((JSObjectRef) value))); + Selector_privateData *data(reinterpret_cast(JSObjectGetPrivate((JSObjectRef) value))); return reinterpret_cast(data->value_); } else return sel_registerName(CYCastCString(context, value)); } -void *CYCastPointer(JSContextRef context, JSValueRef value) { +void *CYCastPointer_(JSContextRef context, JSValueRef value) { switch (JSValueGetType(context, value)) { case kJSTypeNull: return NULL; - case kJSTypeString: + /*case kJSTypeString: return dlsym(RTLD_DEFAULT, CYCastCString(context, value)); case kJSTypeObject: if (JSValueIsObjectOfClass(context, value, Pointer_)) { - ptrData *data(reinterpret_cast(JSObjectGetPrivate((JSObjectRef) value))); + Pointer_privateData *data(reinterpret_cast(JSObjectGetPrivate((JSObjectRef) value))); return data->value_; - } + }*/ default: - return reinterpret_cast(static_cast(CYCastDouble(context, value))); + double number(CYCastDouble(context, value)); + if (isnan(number)) + @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"cannot convert value to pointer" userInfo:nil]; + return reinterpret_cast(static_cast(static_cast(number))); } } +template +_finline Type_ CYCastPointer(JSContextRef context, JSValueRef value) { + return reinterpret_cast(CYCastPointer_(context, value)); +} + void CYPoolFFI(apr_pool_t *pool, JSContextRef context, sig::Type *type, void *data, JSValueRef value) { switch (type->primitive) { case sig::boolean_P: @@ -734,7 +1067,7 @@ void CYPoolFFI(apr_pool_t *pool, JSContextRef context, sig::Type *type, void *da case sig::object_P: case sig::typename_P: - *reinterpret_cast(data) = CYCastNSObject(context, value); + *reinterpret_cast(data) = CYCastNSObject(pool, context, value); break; case sig::selector_P: @@ -742,11 +1075,11 @@ void CYPoolFFI(apr_pool_t *pool, JSContextRef context, sig::Type *type, void *da break; case sig::pointer_P: - *reinterpret_cast(data) = CYCastPointer(context, value); + *reinterpret_cast(data) = CYCastPointer(context, value); break; case sig::string_P: - *reinterpret_cast(data) = CYPoolCString(pool, context, value); + *reinterpret_cast(data) = CYPoolCString(pool, context, value); break; case sig::struct_P: @@ -766,12 +1099,12 @@ JSValueRef CYFromFFI(JSContextRef context, sig::Type *type, void *data) { switch (type->primitive) { case sig::boolean_P: - value = JSValueMakeBoolean(context, *reinterpret_cast(data)); + value = CYCastJSValue(context, *reinterpret_cast(data)); break; #define CYFromFFI_(primitive, native) \ case sig::primitive ## _P: \ - value = JSValueMakeNumber(context, *reinterpret_cast(data)); \ + value = CYCastJSValue(context, *reinterpret_cast(data)); \ break; CYFromFFI_(uchar, unsigned char) @@ -788,10 +1121,13 @@ JSValueRef CYFromFFI(JSContextRef context, sig::Type *type, void *data) { CYFromFFI_(double, double) case sig::object_P: - case sig::typename_P: value = CYCastJSValue(context, *reinterpret_cast(data)); break; + case sig::typename_P: + value = CYMakeInstance(context, *reinterpret_cast(data), true); + break; + case sig::selector_P: if (SEL sel = *reinterpret_cast(data)) value = CYMakeSelector(context, sel); @@ -806,7 +1142,7 @@ JSValueRef CYFromFFI(JSContextRef context, sig::Type *type, void *data) { case sig::string_P: if (char *utf8 = *reinterpret_cast(data)) - value = JSValueMakeString(context, CYJSString(utf8)); + value = CYCastJSValue(context, utf8); else goto null; break; @@ -814,11 +1150,11 @@ JSValueRef CYFromFFI(JSContextRef context, sig::Type *type, void *data) { goto fail; case sig::void_P: - value = JSValueMakeUndefined(context); + value = CYJSUndefined(context); break; null: - value = JSValueMakeNull(context); + value = CYJSNull(context); break; default: fail: @@ -829,8 +1165,8 @@ JSValueRef CYFromFFI(JSContextRef context, sig::Type *type, void *data) { return value; } -static JSValueRef CYCallFunction(JSContextRef context, size_t count, const JSValueRef *arguments, JSValueRef *exception, sig::Signature *signature, ffi_cif *cif, void (*function)()) { _pooled - @try { +static JSValueRef CYCallFunction(JSContextRef context, size_t count, const JSValueRef *arguments, JSValueRef *exception, sig::Signature *signature, ffi_cif *cif, void (*function)()) { + CYTry { if (count != signature->count - 1) @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to ffi function" userInfo:nil]; @@ -851,21 +1187,63 @@ static JSValueRef CYCallFunction(JSContextRef context, size_t count, const JSVal } CYCatch } -static JSValueRef Global_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { _pooled - @try { - NSString *name(CYCastNSString(property)); +void Closure_(ffi_cif *cif, void *result, void **arguments, void *arg) { + ffoData *data(reinterpret_cast(arg)); + + JSContextRef context(data->context_); + + size_t count(data->cif_.nargs); + JSValueRef values[count]; + + for (size_t index(0); index != count; ++index) + values[index] = CYFromFFI(context, data->signature_.elements[1 + index].type, arguments[index]); + + JSValueRef exception(NULL); + JSValueRef value(JSObjectCallAsFunction(context, data->function_, NULL, count, values, &exception)); + CYThrow(context, exception); + + CYPoolFFI(NULL, context, data->signature_.elements[0].type, result, value); +} + +JSObjectRef CYMakeFunctor(JSContextRef context, JSObjectRef function, const char *type) { + // XXX: in case of exceptions this will leak + ffoData *data(new ffoData(type)); + + ffi_closure *closure; + _syscall(closure = (ffi_closure *) mmap( + NULL, sizeof(ffi_closure), + PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, + -1, 0 + )); + + ffi_status status(ffi_prep_closure(closure, &data->cif_, &Closure_, data)); + _assert(status == FFI_OK); + + _syscall(mprotect(closure, sizeof(*closure), PROT_READ | PROT_EXEC)); + + data->value_ = closure; + + data->context_ = CYGetJSContext(); + data->function_ = function; + + return JSObjectMake(context, Functor_, data); +} + +static JSValueRef Runtime_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { + CYTry { + CYPool pool; + NSString *name(CYCastNSString(pool, property)); if (Class _class = NSClassFromString(name)) - return CYMakeObject(context, _class); - if (NSMutableArray *entry = [Bridge_ objectForKey:name]) + return CYMakeInstance(context, _class, true); + if (NSMutableArray *entry = [[Bridge_ objectAtIndex:0] objectForKey:name]) switch ([[entry objectAtIndex:0] intValue]) { case 0: return JSEvaluateScript(CYGetJSContext(), CYJSString([entry objectAtIndex:1]), NULL, NULL, 0, NULL); case 1: - return CYMakeFunction(context, [name cy$symbol], [[entry objectAtIndex:1] UTF8String]); + return CYMakeFunctor(context, reinterpret_cast([name cy$symbol]), CYPoolCString(pool, [entry objectAtIndex:1])); case 2: - CYPool pool; sig::Signature signature; - sig::Parse(pool, &signature, [[entry objectAtIndex:1] UTF8String]); + sig::Parse(pool, &signature, CYPoolCString(pool, [entry objectAtIndex:1])); return CYFromFFI(context, signature.elements[0].type, [name cy$symbol]); } return NULL; @@ -879,27 +1257,60 @@ bool stret(ffi_type *ffi_type) { ); } -static JSValueRef $objc_msgSend(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { _pooled +extern "C" { + int *_NSGetArgc(void); + char ***_NSGetArgv(void); + int UIApplicationMain(int argc, char *argv[], NSString *principalClassName, NSString *delegateClassName); +} + +static JSValueRef System_print(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { + CYTry { + NSLog(@"%s", CYCastCString(context, arguments[0])); + return CYJSUndefined(context); + } CYCatch +} + +static JSValueRef CYApplicationMain(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { + CYTry { + CYPool pool; + NSString *name(CYCastNSObject(pool, context, arguments[0])); + int argc(*_NSGetArgc()); + char **argv(*_NSGetArgv()); + for (int i(0); i != argc; ++i) + NSLog(@"argv[%i]=%s", i, argv[i]); + _pooled + return CYCastJSValue(context, UIApplicationMain(argc, argv, name, name)); + } CYCatch +} + +static JSValueRef $objc_msgSend(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { const char *type; - @try { + CYPool pool; + + CYTry { if (count < 2) @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"too few arguments to objc_msgSend" userInfo:nil]; - id self(CYCastNSObject(context, arguments[0])); + id self(CYCastNSObject(pool, context, arguments[0])); if (self == nil) - return JSValueMakeNull(context); + return CYJSNull(context); SEL _cmd(CYCastSEL(context, arguments[1])); - NSMethodSignature *method([self methodSignatureForSelector:_cmd]); - if (method == nil) - @throw [NSException exceptionWithName:NSInvalidArgumentException reason:[NSString stringWithFormat:@"unrecognized selector %s sent to object %p", sel_getName(_cmd), self] userInfo:nil]; - type = [[method _typeString] UTF8String]; + Class _class(object_getClass(self)); + if (Method method = class_getInstanceMethod(_class, _cmd)) + type = method_getTypeEncoding(method); + else { + CYPoolTry { + NSMethodSignature *method([self methodSignatureForSelector:_cmd]); + if (method == nil) + @throw [NSException exceptionWithName:NSInvalidArgumentException reason:[NSString stringWithFormat:@"unrecognized selector %s sent to object %p", sel_getName(_cmd), self] userInfo:nil]; + type = CYPoolCString(pool, [method _typeString]); + } CYPoolCatch(NULL) + } } CYCatch - CYPool pool; - sig::Signature signature; sig::Parse(pool, &signature, type); @@ -919,12 +1330,12 @@ static JSValueRef Selector_callAsFunction(JSContextRef context, JSObjectRef obje } static JSValueRef Functor_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { - ffiData *data(reinterpret_cast(JSObjectGetPrivate(object))); + Functor_privateData *data(reinterpret_cast(JSObjectGetPrivate(object))); return CYCallFunction(context, count, arguments, exception, &data->signature_, &data->cif_, reinterpret_cast(data->value_)); } -JSObjectRef sel(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { - @try { +JSObjectRef Selector_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { + CYTry { if (count != 1) @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to Selector constructor" userInfo:nil]; const char *name(CYCastCString(context, arguments[0])); @@ -932,35 +1343,101 @@ JSObjectRef sel(JSContextRef context, JSObjectRef object, size_t count, const JS } CYCatch } -JSObjectRef ffi(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { - @try { +JSObjectRef Functor_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { + CYTry { if (count != 2) @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to Functor constructor" userInfo:nil]; - void *function(CYCastPointer(context, arguments[0])); const char *type(CYCastCString(context, arguments[1])); - return CYMakeFunction(context, function, type); + JSValueRef exception(NULL); + if (JSValueIsInstanceOfConstructor(context, arguments[0], Function_, &exception)) { + JSObjectRef function(CYCastJSObject(context, arguments[0])); + return CYMakeFunctor(context, function, type); + } else if (exception != NULL) { + return NULL; + } else { + void (*function)()(CYCastPointer(context, arguments[0])); + return CYMakeFunctor(context, function, type); + } } CYCatch } JSValueRef Pointer_getProperty_value(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { - ptrData *data(reinterpret_cast(JSObjectGetPrivate(object))); - return JSValueMakeNumber(context, reinterpret_cast(data->value_)); + Pointer_privateData *data(reinterpret_cast(JSObjectGetPrivate(object))); + return CYCastJSValue(context, reinterpret_cast(data->value_)); } JSValueRef Selector_getProperty_prototype(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { return Function_; } +static JSValueRef Pointer_callAsFunction_valueOf(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { + CYTry { + Pointer_privateData *data(reinterpret_cast(JSObjectGetPrivate(_this))); + return CYCastJSValue(context, reinterpret_cast(data->value_)); + } CYCatch +} + +static JSValueRef Instance_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { + CYTry { + Instance_privateData *data(reinterpret_cast(JSObjectGetPrivate(_this))); + NSString *description; CYPoolTry { + description = [data->GetValue() description]; + } CYPoolCatch(NULL) + return CYCastJSValue(context, CYJSString(description)); + } CYCatch +} + +static JSValueRef Selector_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { + CYTry { + Selector_privateData *data(reinterpret_cast(JSObjectGetPrivate(_this))); + return CYCastJSValue(context, sel_getName(data->GetValue())); + } CYCatch +} + +static JSValueRef Selector_callAsFunction_type(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { + CYTry { + if (count != 2) + @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to Selector.type" userInfo:nil]; + CYPool pool; + Selector_privateData *data(reinterpret_cast(JSObjectGetPrivate(_this))); + Class _class(CYCastNSObject(pool, context, arguments[0])); + bool instance(CYCastBool(context, arguments[1])); + SEL sel(data->GetValue()); + if (Method method = (*(instance ? &class_getInstanceMethod : class_getClassMethod))(_class, sel)) + return CYCastJSValue(context, method_getTypeEncoding(method)); + else if (NSString *type = [[Bridge_ objectAtIndex:1] objectForKey:CYCastNSString(pool, sel_getName(sel))]) + return CYCastJSValue(context, CYJSString(type)); + else + return CYJSNull(context); + } CYCatch +} + static JSStaticValue Pointer_staticValues[2] = { {"value", &Pointer_getProperty_value, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete}, {NULL, NULL, NULL, 0} }; +static JSStaticFunction Pointer_staticFunctions[2] = { + {"valueOf", &Pointer_callAsFunction_valueOf, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, + {NULL, NULL, 0} +}; + /*static JSStaticValue Selector_staticValues[2] = { {"prototype", &Selector_getProperty_prototype, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete}, {NULL, NULL, NULL, 0} };*/ +static JSStaticFunction Instance_staticFunctions[2] = { + {"toString", &Instance_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, + {NULL, NULL, 0} +}; + +static JSStaticFunction Selector_staticFunctions[3] = { + {"toString", &Selector_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, + {"type", &Selector_callAsFunction_type, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, + {NULL, NULL, 0} +}; + CYDriver::CYDriver(const std::string &filename) : state_(CYClear), data_(NULL), @@ -982,9 +1459,22 @@ void cy::parser::error(const cy::parser::location_type &location, const std::str driver.errors_.push_back(error); } +void CYSetArgs(int argc, const char *argv[]) { + JSContextRef context(CYGetJSContext()); + JSValueRef args[argc]; + for (int i(0); i != argc; ++i) + args[i] = CYCastJSValue(context, argv[i]); + JSValueRef exception(NULL); + JSObjectRef array(JSObjectMakeArray(context, argc, args, &exception)); + CYThrow(context, exception); + CYSetProperty(context, System_, CYJSString("args"), array); +} + MSInitialize { _pooled apr_initialize(); + Bridge_ = [[NSMutableArray arrayWithContentsOfFile:@"/usr/lib/libcycript.plist"] retain]; + NSCFBoolean_ = objc_getClass("NSCFBoolean"); pid_t pid(getpid()); @@ -1011,44 +1501,74 @@ MSInitialize { _pooled definition = kJSClassDefinitionEmpty; definition.className = "Pointer"; definition.staticValues = Pointer_staticValues; - definition.finalize = &Pointer_finalize; + definition.staticFunctions = Pointer_staticFunctions; + definition.finalize = &CYData::Finalize; Pointer_ = JSClassCreate(&definition); definition = kJSClassDefinitionEmpty; definition.className = "Functor"; - definition.parentClass = Pointer_; + definition.staticValues = Pointer_staticValues; + definition.staticFunctions = Pointer_staticFunctions; definition.callAsFunction = &Functor_callAsFunction; + definition.finalize = &CYData::Finalize; Functor_ = JSClassCreate(&definition); + definition = kJSClassDefinitionEmpty; + definition.className = "Struct"; + //definition.getProperty = &Struct_getProperty; + //definition.setProperty = &Struct_setProperty; + definition.finalize = &CYData::Finalize; + Struct_ = JSClassCreate(&definition); + definition = kJSClassDefinitionEmpty; definition.className = "Selector"; - definition.parentClass = Pointer_; + definition.staticValues = Pointer_staticValues; //definition.staticValues = Selector_staticValues; + definition.staticFunctions = Selector_staticFunctions; definition.callAsFunction = &Selector_callAsFunction; + definition.finalize = &CYData::Finalize; Selector_ = JSClassCreate(&definition); definition = kJSClassDefinitionEmpty; - definition.className = "Instance_"; + definition.className = "Instance"; + definition.staticValues = Pointer_staticValues; + definition.staticFunctions = Instance_staticFunctions; definition.getProperty = &Instance_getProperty; + definition.setProperty = &Instance_setProperty; + definition.deleteProperty = &Instance_deleteProperty; definition.callAsConstructor = &Instance_callAsConstructor; - definition.finalize = &Instance_finalize; + definition.finalize = &CYData::Finalize; Instance_ = JSClassCreate(&definition); definition = kJSClassDefinitionEmpty; - definition.getProperty = &Global_getProperty; + definition.className = "Runtime"; + definition.getProperty = &Runtime_getProperty; + Runtime_ = JSClassCreate(&definition); + + definition = kJSClassDefinitionEmpty; + //definition.getProperty = &Global_getProperty; JSClassRef Global(JSClassCreate(&definition)); - JSContextRef context(JSGlobalContextCreate(Global)); + JSGlobalContextRef context(JSGlobalContextCreate(Global)); Context_ = context; JSObjectRef global(JSContextGetGlobalObject(context)); - CYSetProperty(context, global, CYJSString("SEL"), JSObjectMakeConstructor(context, Selector_, &sel)); - CYSetProperty(context, global, CYJSString("ffi"), JSObjectMakeConstructor(context, Functor_, &ffi)); + JSObjectSetPrototype(context, global, JSObjectMake(context, Runtime_, NULL)); + CYSetProperty(context, global, CYJSString("obc"), JSObjectMake(context, Runtime_, NULL)); + CYSetProperty(context, global, CYJSString("Selector"), JSObjectMakeConstructor(context, Selector_, &Selector_new)); + CYSetProperty(context, global, CYJSString("Functor"), JSObjectMakeConstructor(context, Functor_, &Functor_new)); + + CYSetProperty(context, global, CYJSString("CYApplicationMain"), JSObjectMakeFunctionWithCallback(context, CYJSString("CYApplicationMain"), &CYApplicationMain)); CYSetProperty(context, global, CYJSString("objc_msgSend"), JSObjectMakeFunctionWithCallback(context, CYJSString("objc_msgSend"), &$objc_msgSend)); - Bridge_ = [[NSMutableDictionary dictionaryWithContentsOfFile:@"/usr/lib/libcycript.plist"] retain]; + System_ = JSObjectMake(context, NULL, NULL); + CYSetProperty(context, global, CYJSString("system"), System_); + CYSetProperty(context, System_, CYJSString("args"), CYJSNull(context)); + CYSetProperty(context, System_, CYJSString("global"), global); + + CYSetProperty(context, System_, CYJSString("print"), JSObjectMakeFunctionWithCallback(context, CYJSString("print"), &System_print)); name_ = JSStringCreateWithUTF8CString("name"); message_ = JSStringCreateWithUTF8CString("message");