2011-09-05 3 views
3

iMonkey은 iOS 앱에 JS 런타임을 퍼가는 흥미로운 방법처럼 보이지만 실제로 일부 JS 코드를 실행하는 방법에 대한 예제는 찾을 수 없습니다.iOS 앱에서 iMonkey를 사용하는 방법

lib 디렉토리를 빌드하고 링크 할 수 있으며 src 디렉토리의 jsapi.h 헤더를 포함 할 수 있지만 예제 코드를 사용하면 다양한 링커 오류 ('undefined symbol for architecture ...')가 발생합니다 거미 원숭이 (아래 참조). 분명히 말하자면, 이것은 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으로 사용 가능) 멀티 스레딩와 함께 작동합니다.

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 
0

나는 링커와 비슷한 문제가 있었다. 이 문제를 피하기 위해 프로젝트에 libstdC++ 6을 추가하십시오.

관련 문제