Pregunta

Uso de JavascriptCore, si tengo un NSString en Objective-C como este:

NSString *objcName = @"Kristof";

y un contexto de JSGLOBALCONTETREF llamado JSCONTEXTREF .

¿Cómo transfiero el valor objetivo-C de ObjcName a una variable de JavaScript nombrada en el JSCONTEXTREF?Estaba pensando en la línea de:

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

Digamos que el nombre de la variable será "jsname".Necesito un par de llamadas más (o tal vez incluso una llamada), algo así como:

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

para que al final este javascript evaluará correctamente cuando se llame así en 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);

¿Fue útil?

Solución

Encontré la respuesta en la código de muestra para JavascriptCoreHeerStart , más específicamente el archivo jswrappers.m.Tiene este método:

/* -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 );
    }
}

que es exactamente lo que necesitaba.El código para el método de JstringValue está en NSStringWraps.m en el mismo proyecto y es:

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

Esto parece funcionar.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top