Skip to content
Browse files

Merge pull request #11 from ccgus/master

patch for calling JS functions on NSStrings
  • Loading branch information...
2 parents ea10980 + 0bfa8f9 commit d580d8a7cee4d675f60bc93c9aa21bdf44c16ca1 @logancollins committed Feb 4, 2013
Showing with 50 additions and 0 deletions.
  1. +44 −0 Mocha/MochaRuntime.m
  2. +6 −0 Mocha/Utilities/MOUtilities.m
View
44 Mocha/MochaRuntime.m
@@ -61,6 +61,7 @@
static JSValueRef MOFunction_callAsFunction(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef *exception);
+static JSValueRef MOStringPrototypeFunction(JSContextRef ctx, NSString *name);
NSString * const MORuntimeException = @"MORuntimeException";
NSString * const MOJavaScriptException = @"MOJavaScriptException";
@@ -1200,6 +1201,14 @@ static bool MOBoxedObject_hasProperty(JSContextRef ctx, JSObjectRef objectJS, JS
return YES;
}
+ if ([object isKindOfClass:[NSString class]]) {
+ // special case bridging of NSString w/ JS string functions
+
+ if (MOStringPrototypeFunction(ctx, propertyName)) {
+ return YES;
+ }
+ }
+
return NO;
}
@@ -1322,6 +1331,16 @@ static JSValueRef MOBoxedObject_getProperty(JSContextRef ctx, JSObjectRef object
}
}
+ if ([object isKindOfClass:[NSString class]]) {
+ // special case bridging of NSString w/ JS string functions
+
+ JSValueRef jsPropertyValue = MOStringPrototypeFunction(ctx, propertyName);
+
+ if (jsPropertyValue) {
+ return jsPropertyValue;
+ }
+ }
+
// if (exception != NULL) {
// NSString *reason = nil;
// if (object == objectClass) {
@@ -1528,3 +1547,28 @@ static JSValueRef MOFunction_callAsFunction(JSContextRef ctx, JSObjectRef functi
return value;
}
+
+static JSValueRef MOStringPrototypeFunction(JSContextRef ctx, NSString *name) {
+
+ JSValueRef exception = nil;
+ JSStringRef jsPropertyName = JSStringCreateWithUTF8CString("String");
+ JSValueRef jsPropertyValue = JSObjectGetProperty(ctx, JSContextGetGlobalObject(ctx), jsPropertyName, &exception);
+ JSStringRelease(jsPropertyName);
+
+ jsPropertyName = JSStringCreateWithUTF8CString("prototype");
+ jsPropertyValue = JSObjectGetProperty(ctx, JSValueToObject(ctx, jsPropertyValue, nil), jsPropertyName, &exception);
+ JSStringRelease(jsPropertyName);
+
+ jsPropertyName = JSStringCreateWithUTF8CString([name UTF8String]);
+ jsPropertyValue = JSObjectGetProperty(ctx, JSValueToObject(ctx, jsPropertyValue, nil), jsPropertyName, &exception);
+ JSStringRelease(jsPropertyName);
+
+ if (jsPropertyValue && JSValueGetType(ctx, jsPropertyValue) == kJSTypeObject) {
+ // OK, there's a JS String method with the same name as propertyName. Let's use that.
+ return jsPropertyValue;
+ }
+
+ return nil;
+}
+
+
View
6 Mocha/Utilities/MOUtilities.m
@@ -306,6 +306,12 @@ JSValueRef MOFunctionInvoke(id function, JSContextRef ctx, size_t argumentCount,
target = [[target objectClass] alloc];
}
+ // Make sure autorelease is ignored, since we do our own reference counting.
+ if (selector == NSSelectorFromString(@"autorelease")) {
+ NSLog(@"Ignoring autorelease call on %@", target);
+ return [runtime JSValueForObject:target];
+ }
+
Method method = NULL;
BOOL classMethod = (target == klass);

0 comments on commit d580d8a

Please sign in to comment.
Something went wrong with that request. Please try again.