黑客
我已经想到,由于头文件中的条件定义以及分散的库和头文件位置,链接失败。好吧...我试图在命令行上定义 JS_HAS_CTYPES 但如果它确实有效,那肯定是不够的。
我决定,因为 SpiderMonkey shell 有自己独特的 makefile,并且已经可以访问我试图捕获的功能,只需将 js.cpp 重命名为 js.cpp.tmp 并让我的代码站在它的位置,几乎可以工作.
该文件编译良好,应用程序执行时没有引发运行时链接错误,但代码('JSNativeObject' ctypes)几乎完全失败了 JS_InitCTypesClass。看到我的链接错误早已被遗忘,我立即查看了 make 的输出,看看我是否可以“刷”编译代码,然后......我们有一个宾果游戏!
汇编
将 shell/js.cpp 恢复到其原始目标后,我将 hello.cpp 移动到 spidermonkey 的根源目录并开始更正由 makefile 创建的相对路径以及执行明显不适合的构造的删除我的应用程序的存在或相关性。
虽然以下命令似乎呈现了一个可操作的二进制文件,但作者无法给出此列表的正确性或完整性的相关性。
c++ -o hello.o -c -Idist/system_wrappers_js -include config/gcc_hidden.h \
-DEXPORT_JS_API -DOSTYPE=\"Linux3.2\" -DOSARCH=Linux -I. -Idist/include \
-Idist/include/nsprpub -I/usr/include/nspr -fPIC -fno-rtti \
-fno-exceptions -Wall -Wpointer-arith -Woverloaded-virtual -Wsynth \
-Wno-ctor-dtor-privacy -Wno-non-virtual-dtor -Wcast-align \
-Wno-invalid-offsetof -Wno-variadic-macros -Werror=return-type -pedantic \
-Wno-long-long -fno-strict-aliasing -pthread -pipe -DNDEBUG -DTRIMMED -Os \
-freorder-blocks -fomit-frame-pointer -DJS_HAS_CTYPES -DMOZILLA_CLIENT \
-include js-confdefs.h -MD -MF .deps/hello.pp hello.cpp;
c++ -o hello -fno-rtti -fno-exceptions -Wall -Wpointer-arith \
-Woverloaded-virtual -Wsynth -Wno-ctor-dtor-privacy \
-Wno-non-virtual-dtor -Wcast-align -Wno-invalid-offsetof \
-Wno-variadic-macros -Werror=return-type -pedantic \
-Wno-long-long -fno-strict-aliasing -pthread -pipe -DNDEBUG \
-DTRIMMED -Os -freorder-blocks -fomit-frame-pointer hello.o \
-lpthread -Wl,-rpath-link,/bin -Wl,-rpath-link,/usr/local/lib \
-Ldist/bin -Ldist/lib -L/usr/lib -lplds4 -lplc4 -lnspr4 \
-lpthread -ldl editline/libeditline.a libjs_static.a -ldl;
上面列出的两个命令被放入一个名为“mkhello”的可执行 shell 脚本中,该脚本保存在根源目录中。
据我所知,这是一种两阶段的编译方法。出于什么原因我不确定,但解释似乎很有教育意义。想法?
编辑请参阅下面的评论以了解“两阶段编译方法”的解释。
代码:hello.cpp
/*
* This define is for Windows only, it is a work-around for bug 661663.
*/
#ifdef _MSC_VER
# define XP_WIN
#endif
/* Include the JSAPI header file to get access to SpiderMonkey. */
#include "jsapi.h"
/* The class of the global object. */
static JSClass global_class = {
"global", JSCLASS_GLOBAL_FLAGS,
JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_StrictPropertyStub,
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub,
JSCLASS_NO_OPTIONAL_MEMBERS
};
/* 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=\"filename\">",
(unsigned int) report->lineno,
message);
}
int main(int argc, const char *argv[])
{
/* JSAPI variables. */
JSRuntime *rt;
JSContext *cx;
JSObject *global;
/* Create a JS runtime. You always need at least one runtime per process. */
rt = JS_NewRuntime(8 * 1024 * 1024);
if (rt == NULL)
return 1;
/*
* Create a context. You always need a context per thread.
* Note that this program is not multi-threaded.
*/
cx = JS_NewContext(rt, 8192);
if (cx == NULL)
return 1;
JS_SetOptions(cx, JSOPTION_VAROBJFIX | JSOPTION_JIT | JSOPTION_METHODJIT);
JS_SetVersion(cx, JSVERSION_LATEST);
JS_SetErrorReporter(cx, reportError);
/*
* Create the global object in a new compartment.
* You always need a global object per context.
*/
global = JS_NewCompartmentAndGlobalObject(cx, &global_class, NULL);
if (global == NULL)
return 1;
/*
* Populate the global object with the standard JavaScript
* function and object classes, such as Object, Array, Date.
*/
if (!JS_InitStandardClasses(cx, global))
return 1;
/* Populate the global object with the ctypes object. */
if (!JS_InitCTypesClass(cx, global))
return NULL;
/*
/* Your application code here. This may include JSAPI calls
* to create your own custom JavaScript objects and to run scripts.
*
* The following example code creates a literal JavaScript script,
* evaluates it, and prints the result to stdout.
*
* Errors are conventionally saved in a JSBool variable named ok.
*/
char *script = "ctypes.open";
jsval rval;
JSString *str;
JSBool ok;
const char *filename = "noname";
uintN lineno = 0;
ok = JS_EvaluateScript(cx, global, script, strlen(script),
filename, lineno, &rval);
if (rval == NULL | rval == JS_FALSE)
return 1;
str = JS_ValueToString(cx, rval);
printf("%s\n", JS_EncodeString(cx, str));
/* End of your application code */
/* Clean things up and shut down SpiderMonkey. */
JS_DestroyContext(cx);
JS_DestroyRuntime(rt);
JS_ShutDown();
return 0;
}
结论
$ ./mkhello
# ...
# error free garbage scrolls....
$ ./hello
function open() {
[native code]
}
按照此示例为 SpiderMonkey 嵌入提供 js-ctypes。您可能需要也可能不需要按顺序重新创建这些步骤,但从我目前的角度来看,强烈建议您这样做。