2011-09-05 51 views
3

iMonkey看起來像是一種在iOS應用程序中嵌入JS運行時的有趣方式,但我找不到任何有關如何實際運行某些JS代碼的示例。如何在iOS應用程序中使用iMonkey

我可以建立/鏈接lib,包含jsapi.h頭文件(來自src目錄),但是當我嘗試一些示例代碼時,它會跳過各種鏈接器錯誤('未定義的架構符號...')蜘蛛猴(見下文)。要清楚的是,這幾乎是來自另一個網站上Mac相關帖子的複製粘貼,我完全不知道是否應該這樣做。我確信我的架構(模擬器)具有正確的靜態庫(通用)。

任何人都知道如何做到這一點?

#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(); 

回答

1

下面是一個鬆散地基於原始蜘蛛猴中的例子的例子。

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

我修改,以便它具有多線程(這已被iMonkey啓用)的作品。

​​

// 
// 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 
0

我曾與連接器類似的問題。只需將libstdC++ 6添加到您的項目中即可避免此問題

相關問題