1
我剛剛安裝了最新版本的SpiderMonkey,並試圖運行嵌入JS引擎的示例「Hello World」程序。示例程序如下所示:SpiderMonkey編譯錯誤
#include "jsapi.h"
/* The class of the global object. */
static JSClass global_class = {
"global",
JSCLASS_GLOBAL_FLAGS,
JS_PropertyStub,
JS_DeletePropertyStub,
JS_PropertyStub,
JS_StrictPropertyStub,
JS_EnumerateStub,
JS_ResolveStub,
JS_ConvertStub
};
int main(int argc, const char *argv[])
{
JS_Init();
JSRuntime *rt = JS_NewRuntime(8L * 1024 * 1024, JS_NO_HELPER_THREADS);
if (!rt)
return 1;
JSContext *cx = JS_NewContext(rt, 8192);
if (!cx)
return 1;
JS::RootedObject global(cx, JS_NewGlobalObject(cx, &global_class, nullptr, JS::DontFireOnNewGlobalHook));
if (!global)
return 1;
JS::Value rval;
bool ok;
{
JSAutoCompartment ac(cx, *global);
JS_InitStandardClasses(cx, *global);
const char *script = "'hello'+'world, it is '+new Date()";
const char *filename = "noname";
int lineno = 0;
ok = JS_EvaluateScript(cx, *global, script, strlen(script), filename, lineno, &rval);
if (rval.isNull() | rval.isFalse())
return 1;
}
JSString *str = rval.toString();
printf("%s\n", JS_EncodeString(cx, str));
JS_DestroyContext(cx);
JS_DestroyRuntime(rt);
JS_ShutDown();
return 0;
}
不過,我收到以下錯誤(當我使用g ++編譯的終端):
js/src/helloworld.cpp:21:53: error: use of undeclared identifier 'JS_NO_HELPER_THREADS'
js/src/helloworld.cpp:36:25: error: no matching constructor for initialization of 'JSAutoCompartment'
js/src/helloworld.cpp:37:7: error: no matching function for call to 'JS_InitStandardClasses'
js/src/helloworld.cpp:42:12: error: no matching function for call to 'JS_EvaluateScript'
誰能告訴我什麼,我做錯了什麼?