Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #14 from ccgus/master

Add support for calling length() on NSArray as well as calling JS functions such as forEach() on NSArray
  • Loading branch information...
commit f549f6579f98fd2f24f926485c50164b94d466de 2 parents d580d8a + a0262dc
@logancollins authored
Showing with 38 additions and 10 deletions.
  1. +38 −10 Mocha/MochaRuntime.m
View
48 Mocha/MochaRuntime.m
@@ -61,7 +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);
+static JSValueRef MOJSPrototypeFunctionForOBJCInstance(JSContextRef ctx, id instance, NSString *functionName);
NSString * const MORuntimeException = @"MORuntimeException";
NSString * const MOJavaScriptException = @"MOJavaScriptException";
@@ -1201,12 +1201,20 @@ 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 ([object isKindOfClass:[NSString class]] || [object isKindOfClass:[NSArray class]]) {
+ // special case bridging of NSString & NSArray w/ JS functions
- if (MOStringPrototypeFunction(ctx, propertyName)) {
+ if (MOJSPrototypeFunctionForOBJCInstance(ctx, object, propertyName)) {
return YES;
}
+
+ if ([object isKindOfClass:[NSArray class]]) {
+ // if we're calling length on an NSArray (which will happen from inside JS'a Array.forEach), we need to make sure
+ // to catch and do that right. We could also add a category to NSArray I suppose, but that feels a little dirty.
+ if ([propertyName isEqualToString:@"length"]) {
+ return YES;
+ }
+ }
}
return NO;
@@ -1331,14 +1339,22 @@ 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 ([object isKindOfClass:[NSString class]] || [object isKindOfClass:[NSArray class]]) {
+ // special case bridging of NSString & NSArray w/ JS functions
+ JSValueRef jsPropertyValue = MOJSPrototypeFunctionForOBJCInstance(ctx, object, propertyName);
if (jsPropertyValue) {
return jsPropertyValue;
}
+
+ if ([object isKindOfClass:[NSArray class]]) {
+
+ // see the notes in MOBoxedObject_hasProperty
+ if ([propertyName isEqualToString:@"length"]) {
+ MOMethod *method = [MOMethod methodWithTarget:object selector:@selector(count)];
+ return MOFunctionInvoke(method, ctx, 0, NULL, exception);
+ }
+ }
}
// if (exception != NULL) {
@@ -1548,10 +1564,22 @@ static JSValueRef MOFunction_callAsFunction(JSContextRef ctx, JSObjectRef functi
return value;
}
-static JSValueRef MOStringPrototypeFunction(JSContextRef ctx, NSString *name) {
+static JSValueRef MOJSPrototypeFunctionForOBJCInstance(JSContextRef ctx, id instance, NSString *name) {
+
+ char *propName = nil;
+ if ([instance isKindOfClass:[NSString class]]) {
+ propName = "String";
+ }
+ else if ([instance isKindOfClass:[NSArray class]]) {
+ propName = "Array";
+ }
+
+ if (!propName) {
+ return NO;
+ }
JSValueRef exception = nil;
- JSStringRef jsPropertyName = JSStringCreateWithUTF8CString("String");
+ JSStringRef jsPropertyName = JSStringCreateWithUTF8CString(propName);
JSValueRef jsPropertyValue = JSObjectGetProperty(ctx, JSContextGetGlobalObject(ctx), jsPropertyName, &exception);
JSStringRelease(jsPropertyName);
Please sign in to comment.
Something went wrong with that request. Please try again.