2011-03-29 49 views
0

我是C的初學者,我試圖使用SpiderMonkey JS引擎。我不明白爲什麼它不工作(上mdc的例子不是非常有幫助)SpiderMonkey JS引擎C故障

#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

什麼「不工作」?你究竟有什麼問題? – 2011-03-29 20:40:40

+0

好吧,我得到了警告(在源代碼的底部),當我嘗試運行它時,我得到這個:'sh:line 1:911 Segmentation fault。/ a.out' – errorhandler 2011-03-29 20:43:48

+0

僅供參考,SpiderMonkey嵌入不是用於成爲一個適合新手的環境。許多正確的API使用責任被分流到程序員身上,通常是C方式。 – cdleary 2013-04-29 16:38:30

回答

1

貌似你之後rand()

編輯忘了一個分號:看起來你'也錯誤地使用API​​。按照文檔解釋,DOUBLE_TO_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 2011-03-29 20:51:46

+0

哪個版本?我編輯了我的文章幾次。試着將r加倍。另外,我看到我拼錯了ctx變量,它應該是cx。 (約編輯) – yan 2011-03-29 20:52:46

+0

仍然得到的警告,但現在在第28行:'靜態JSFunctionSpec custom_global_functions [] = { JS_FS( 「蘭特」,myjs_rand,0,0,0), JS_FS_END };' – errorhandler 2011-03-29 20:54:46