SpiderMonkey JS 引擎 C 问题

发布于 2024-10-28 06:26:43 字数 2804 浏览 1 评论 0原文

我是 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: 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

I'm a beginner at C and I was trying to use the SpiderMonkey JS Engine. I can't understand why it isn't working (the examples on mdc are not very helpfull)

#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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

东京女 2024-11-04 06:26:43

看起来您在 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);
}

Looks like you forgot a semi-colon after rand()

edit: looks like you're also using the API incorrectly. DOUBLE_TO_JSVAL expects a jsval, as the docs explain. Try (I didn't build this myself to test):

JSBool myjs_rand(JSContext *cx, uintN argc, jsval *vp)
{
    int r = rand();
    return JS_NewNumberValue(cx, (double)r, vp);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文