【问题标题】:SpiderMonkey JS Engine C TroubleSpiderMonkey JS 引擎 C 故障
【发布时间】:2011-03-29 20:37:44
【问题描述】:

我是 C 的初学者,我正在尝试使用 SpiderMonkey JS 引擎。我不明白为什么它不起作用(mdc 上的示例不是很有帮助)

#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:在函数“myjs_rand”中: SpiderMonkeyFun.c:23:警告:传递“DOUBLE_TO_JSVAL”的参数 1 使指针从整数而不进行强制转换 SpiderMonkeyFun.c:23: 错误: 调用对象‘rand()’不是函数 SpiderMonkeyFun.c:在顶层: SpiderMonkeyFun.c:28:警告:从不兼容的指针类型初始化

【问题讨论】:

  • 什么“不起作用”?你到底遇到了什么问题?
  • 好吧,我收到了警告(在源代码的底部),当我尝试运行它时,我得到了这个:sh: line 1: 911 Segmentation fault ./a.out
  • 仅供参考,SpiderMonkey 嵌入并非旨在成为一个对新手友好的环境。许多正确的 API 使用责任被分流给了程序员,这通常是 C 的方式。

标签: javascript c spidermonkey javascript-engine


【解决方案1】:

您似乎忘记了rand() 后面的分号

编辑:看起来您也错误地使用了 API。正如文档解释的那样,DOUBLE_TO_JSVAL 需要 jsval。尝试(我自己没有构建这个来测试):

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

【讨论】:

  • 嗯,仍然收到警告:SpiderMonkeyFun.c:30: warning: initialization from incompatible pointer type
  • 哪个版本?我编辑了我的帖子几次。尝试将 r 转换为加倍。另外,我发现我拼错了 ctx 变量,它应该是 cx。 (即将编辑)
  • 仍然收到该警告,但现在在第 28 行:static JSFunctionSpec custom_global_functions[] = { JS_FS("rand", myjs_rand, 0, 0, 0), JS_FS_END };
  • 我认为你的意思是“JS_FN”..你真的应该检查你的代码并检查示例。很多这些东西都是谷歌查询。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-01
  • 2014-05-07
  • 2012-03-13
  • 1970-01-01
  • 1970-01-01
  • 2011-07-14
  • 1970-01-01
相关资源
最近更新 更多