Como utilizar o iMonkey numa aplicação iOS

IMonkey parece ser uma maneira interessante de incorporar um tempo de execução JS em um aplicativo iOS, mas não consigo encontrar nenhum exemplo de como realmente executar algum código JS.

posso construir / ligar a lib, incluir a jsapi.h header( do diretório src), mas ele tropeça sobre vários erros de linker ('símbolo indefinido para arquitetura...') quando tento um código de exemplo do Macaco-Aranha (ver abaixo). Para ser claro, este é praticamente uma copy-paste de uma publicação relacionada com Mac em outro site, Eu sou Não tenho a certeza se é assim que deve ser feito. Tenho a certeza que tenho a biblioteca estática certa (universal) para a minha arquitetura (simulador).

Alguém sabe como fazer isto?

#include "jsapi.h"

.....

JSRuntime *rt;
JSContext *cx;
JSObject  *global;

/* Create a JS runtime. */
rt = JS_NewRuntime(8L * 1024L * 1024L);

/* Create a context. */
cx = JS_NewContext(rt, 8192);
JS_SetOptions(cx, JSOPTION_VAROBJFIX);

/* Create the global object. */
global = JS_NewObject(cx, &global_class, NULL, NULL);

/* Populate the global object with the standard globals,
 like Object and Array. */
if (!JS_InitStandardClasses(cx, global))
    @throw [[NSException alloc]initWithName:@"JSerror" reason:@"jserrpr" userInfo:nil];

/* Cleanup. */
JS_DestroyContext(cx);
JS_DestroyRuntime(rt);
JS_ShutDown();
Author: beno1604, 2011-09-05

2 answers

Aqui está um exemplo vagamente baseado nos exemplos encontrados no Macaco-Aranha original.

Http://egachine.berlios.de/embedding-sm-best-practice/embedding-sm-best-practice.html

Eu modifiquei para que funcione com multi-threading (que já está activa pelo iMonkey).

Https://developer.mozilla.org/En/SpiderMonkey/Internals/Thread_Safety

//
//  JSEngine.mm
//  POC_JS
//
//  Created by Quoc Le on 7/12/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "JSEngine.h"

/* get SpiderMonkey API declarations */
#include <jsapi.h>
/* EXIT_FAILURE and EXIT_SUCCESS */
#include <stdlib.h>  
/* strlen */
#include <string.h>


@implementation JSEngine

+ (int) run
{
    /* pointer to our runtime object */
    JSRuntime *runtime=NULL;
    /* pointer to our context */
    JSContext *context=NULL;
    /* pointer to our global JavaScript object */
    JSObject  *global=NULL;

    /* script to run (should return 100) */
    char *script="var x=10;x*x;";
    /* JavaScript value to store the result of the script */
    jsval rval;

    /* create new runtime, new context, global object */
    if (    (!(runtime = JS_NewRuntime (1024L*1024L)))
        || (!(context = JS_NewContext (runtime, 8192)))
        ) return EXIT_FAILURE;

    JS_SetContextThread(context);
    JS_BeginRequest(context);

    //        || (!(global  = JS_NewObject  (context, NULL, NULL, NULL)))

    global  = JS_NewObject  (context, NULL, NULL, NULL);

    /* set global object of context and initialize standard ECMAScript
     objects (Math, Date, ...) within this global object scope */
    if (!JS_InitStandardClasses(context, global)) return EXIT_FAILURE;

    /* now we are ready to run our script */
    if (!JS_EvaluateScript(context, global,script, strlen(script),
                           "script", 1, &rval))
        return EXIT_FAILURE;
    /* check if the result is really 100 */
    NSLog(@"RSVAL %d", JSVAL_TO_INT(rval));
    if (!(JSVAL_IS_INT(rval)&&(JSVAL_TO_INT(rval)==100)))
        return EXIT_FAILURE;

    JS_EndRequest(context);
    JS_ClearContextThread(context);

    /* clean up */
    //JS_DestroyContext(context);
    //JS_DestroyRuntime(runtime);
    //JS_ShutDown();
    return EXIT_SUCCESS;
}

@end
 1
Author: qle, 2012-07-12 08:38:51
Tive problemas semelhantes com o linker. basta adicionar a libstdc++6 ao seu projecto para evitar este problema
 0
Author: stalk, 2012-08-08 14:01:53