Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

patch for calling JS functions on NSStrings #11

Merged
merged 2 commits into from

2 participants

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
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);
Something went wrong with that request. Please try again.