Search code examples
objective-cioscocoa-touchjavascriptcore

How to transfer a JSValueRef into a JavaScript var from Objective-C


Using JavaScriptCore, if I have an NSString in Objective-C like this:

NSString *objcName = @"Kristof";

and a JSGlobalContextRef context called jsContextRef.

How do I transfer the Objective-C value of objcName into a named JavaScript variable in the jsContextRef? I was thinking along the lines of:

JSStringRef jsNameRef = JSStringCreateWithUTF8CString([objcName UTF8String]);
JSValueRef jsValueRef = JSValueMakeString(jsContextRef, jsNameRef);

Let's say the variable name will be "jsName". I need a couple more calls (or maybe even one call), something like:

// This part is pseudo-code for which I would like to have proper code:
JSValueStoreInVarWithName(jsContextRef,"jsName",jsValueRef);

so that in the end this JavaScript will evaluate correctly when called like this in Objective-C:

NSString *lJavaScriptScript = @"var jsUppercaseName = jsName.toUpperCase();";
JSStringRef scriptJS = JSStringCreateWithUTF8CString([lJavaScriptScript UTF8String]);
JSValueRef exception = NULL;
JSValueRef result = JSEvaluateScript(jsContextRef, scriptJS, NULL, NULL, 0, &exception);

Solution

  • I found the answer in the sample code for JavaScriptCoreHeadstart, more specifically the JSWrappers.m file. It has this method:

    /* -addGlobalStringProperty:withValue: adds a string with the given name to the
     global object of the JavaScriptContext.  After this call, scripts running in
     the context will be able to access the string using the name. */
    - (void)addGlobalStringProperty:(NSString *)name withValue:(NSString *)theValue {
        /* convert the name to a JavaScript string */
        JSStringRef propertyName = [name jsStringValue];
        if ( propertyName != NULL ) {
            /* convert the property value into a JavaScript string */
            JSStringRef propertyValue = [theValue jsStringValue];
            if ( propertyValue != NULL ) {            
                /* copy the property value into the JavaScript context */
                JSValueRef valueInContext = JSValueMakeString( [self JSContext], propertyValue );
                if ( valueInContext != NULL ) {                
                    /* add the property into the context's global object */
                    JSObjectSetProperty( [self JSContext], JSContextGetGlobalObject( [self JSContext] ),
                                    propertyName, valueInContext, kJSPropertyAttributeReadOnly, NULL );
                }
                /* done with our reference to the property value */
                JSStringRelease( propertyValue );
            }
            /* done with our reference to the property name */
            JSStringRelease( propertyName );
        }
    }
    

    which is exactly what I needed. The code for the jsStringValue method is in NSStringWrappers.m in the same project and is:

    /* return a new JavaScriptCore string value for the string */
    - (JSStringRef)jsStringValue {
        return JSStringCreateWithCFString( (__bridge CFStringRef) self );
    }
    

    This seems to work.