2011-03-29 2 views
0

저는 C에서 초보자이며 SpiderMonkey JS 엔진을 사용하려고했습니다. 그것이 작동하지 않는 이유는 rand()SpiderMonkey JS Engine C Trouble

편집 후 세미콜론을 잊어처럼

#define XP_UNIX 
#include <stdio.h> 
#include <string.h> 
#include "jsapi.h" 

/* The class of the global object. */ 
#ifndef JSCLASS_GLOBAL_FLAGS 
#define JSCLSAS_GLOBAL_FLAGS 0 
#endif 

static JSClass global_class = { 
    "global", JSCLASS_GLOBAL_FLAGS, 
    JS_PropertyStub, JS_PropertyStub, 
    JS_PropertyStub, JS_PropertyStub, 
    JS_EnumerateStub, JS_ResolveStub, 
    JS_ConvertStub, JS_FinalizeStub, 
    JSCLASS_NO_OPTIONAL_MEMBERS  
}; 

JSBool myjs_rand(JSContext *cx, uintN argc, jsval *vp) 
{ 
    int r = rand(); 
    JS_SET_RVAL(cx, vp, DOUBLE_TO_JSVAL(r)); 
    return JS_TRUE; 
} 

static JSFunctionSpec custom_global_functions[] = { 
    JS_FS("rand", myjs_rand, 0, 0, 0), 
    JS_FS_END 
}; 

/* The error reporter callback. */ 
void reportError(JSContext *cx, const char *message, JSErrorReport *report) 
{ 
    fprintf(stderr, "%s:%u:%s\n", 
      report->filename ? report->filename : "<no filename>", 
      (unsigned int) report->lineno, 
      message); 
} 

int main(int argc, const char *argv[]) 
{ 
    /* JS variables. */ 
    JSRuntime *rt; 
    JSContext *cx; 
    JSObject *global; 

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

    /* Create a context. */ 
    cx = JS_NewContext(rt, 8192); 
    if (cx == NULL) 
     return 1; 
    JS_SetOptions(cx, JSOPTION_VAROBJFIX | JSOPTION_JIT); 
    JS_SetVersion(cx, JSVERSION_LATEST); 
    JS_SetErrorReporter(cx, reportError); 

    /* Create the global object in a new compartment. */ 
    global = JS_NewObject(cx, &global_class, 0, 0); 
    if (global == NULL) 
     return 1; 

    /* Populate the global object with the standard globals, 
     like Object and Array. */ 
    if (!JS_InitStandardClasses(cx, global)) 
     return 1; 

    /* Add custom methods like log */ 
    if (!JS_DefineFunctions(cx, global, custom_global_functions)) 
     return JS_FALSE; 

    /* Run Script */ 

    char *filename; 
    uintN lineno; 

    jsval rval; 
    JSBool ok; 

    char *source = "rand()"; 

    ok = JS_EvaluateScript(cx, global, source, strlen(source), filename, lineno, &rval); 

    if (ok) { 
     // do stuff 
    } 


    JS_DestroyContext(cx); 
    JS_DestroyRuntime(rt); 
    JS_ShutDown(); 
    return 0; 
} 
 
SpiderMonkeyFun.c: In function ‘myjs_rand’: 
SpiderMonkeyFun.c:23: warning: passing argument 1 of ‘DOUBLE_TO_JSVAL’ makes pointer from integer without a cast 
SpiderMonkeyFun.c:23: error: called object ‘rand()’ is not a function 
SpiderMonkeyFun.c: At top level: 
SpiderMonkeyFun.c:28: warning: initialization from incompatible pointer type 
+0

"작동하지 않음"이란 무엇입니까? 정확히 어떤 문제가 있습니까? –

+0

나는 소스 코드의 하단에있는 경고 메시지를 얻었고 실행하려고 할 때 다음과 같이 표시된다.'sh : line 1 : 911 Segmentation fault./a.out' – errorhandler

+0

참고로, SpiderMonkey 임베딩은 의도하지 않았다. 초보자 친화적 인 환경. 많은 C API가 그렇듯이 올바른 API 사용에 대한 많은 책임이 프로그래머에게 배제됩니다. – cdleary

답변

1

이 보이는 (mdc의 예는 매우 helpfull되지 않습니다) 이해할 수 없다 : 당신처럼 보이는 또한 API를 잘못 사용하고 있습니다. DOUBLE_TO_JSVAL은 문서가 설명하는대로 jsval을 기대합니다. 시도해보십시오 (테스트를 위해 직접 작성하지 않았습니다).

JSBool myjs_rand(JSContext *cx, uintN argc, jsval *vp) 
{ 
    int r = rand(); 
    return JS_NewNumberValue(cx, (double)r, vp); 
} 
+0

흠, 경고 메시지가 계속 표시됨 : 'SpiderMonkeyFun.c : 30 : 경고 : 호환되지 않는 포인터 유형에서 초기화 됨' – errorhandler

+0

어떤 버전입니까? 나는 내 게시물을 몇 번 편집했다. r을 double 형으로 변환 해보십시오. 또한, 나는 ctx 변수의 오정렬을 보았다. 그것은 cx 여야한다. – yan

+0

아직도 그 경고를하지만 (편집에 대한) 지금 28 행에서'정적 JSFunctionSpec의 custom_global_functions는 [] = { JS_FS ("랜드", myjs_rand, 0, 0, 0), JS_FS_END }' – errorhandler