1

我将使用 Duktape 对 ARM 32/64 平台进行 javascript 评估。我只想为特定的平台和架构构建 Duktape,而不是针对所有范围。

看来,我可以成功构建它:

python tools/configure.py \
    --source-directory src-input \
    --platform linux \
    --architecture arm32 \
    --config-metadata config/ \
    --option-file arm32_config.yaml \
    --output-directory /tmp/arm32    

arm32_config.yaml

DUK_USE_32BIT_PTRS: true
DUK_USE_64BIT_OPS: false 
DUK_USE_FATAL_HANDLER: false

通常构建通行证。太好了!

在 Raspberry Pi 上(仅用于测试):

我有一个hello.c

 #include "duktape.h"

 int main(int argc, char *argv[]) {
   duk_context *ctx = duk_create_heap_default();
   duk_eval_string(ctx, "print('Hello world!');");
   duk_destroy_heap(ctx);
   return 0;
 }

Makefile.hello文件:

 DUKTAPE_SOURCES = src/arm32/duktape.c

 # Compiler options are quite flexible.  GCC versions have a significant impact
 # on the size of -Os code, e.g. gcc-4.6 is much worse than gcc-4.5.

 CC = gcc
 CCOPTS = -Os -pedantic -std=c99 -Wall -fstrict-aliasing -fomit-frame-pointer
 CCOPTS += -I./src/arm32  # for combined sources
 CCLIBS = -lm
 CCOPTS += -DUK_USE_32BIT_PTRS
 CCOPTS += -DUK_USE_64BIT_OPS
 CCOPTS += -DUK_USE_FATAL_HANDLER

 # For debugging, use -O0 -g -ggdb, and don't add -fomit-frame-pointer

 hello: $(DUKTAPE_SOURCES) hello.c
    $(CC) -o $@ $(DEFINES) $(CCOPTS) $(DUKTAPE_SOURCES) hello.c $(CCLIBS)

它也有效!

但是当我尝试启动程序时 ./hello我总是收到: 分段错误

请指出我的错误吗?我错过了什么?先感谢您!

ps:gcc版本4.9.2(Raspbian 4.9.2-10)

4

1 回答 1

1

您最可能遇到的主要问题是您正在使用不再提供内置“print()”绑定的 Duktape master(将成为 2.0.0 版本)工作。这会导致抛出错误。

该错误未被捕获,因为您没有包装 eval 调用的受保护调用,因此会发生致命错误。由于您没有提供致命错误处理程序(在 duk_create_heap() 中或通过 DUK_USE_FATAL_HANDLER),这会导致默认的致命错误行为,即导致故意的段错误(这是为了调试器可以附加)。

所以最简单的解决方法是定义一个 print() 绑定,并可能添加一个致命错误处理程序和/或对 eval 的受保护调用。这是一个添加 print() 绑定的示例(在执行 eval 之前):

static duk_ret_t native_print(duk_context *ctx) {
    duk_push_string(ctx, " ");
    duk_insert(ctx, 0);
    duk_join(ctx, duk_get_top(ctx) - 1);
    printf("%s\n", duk_safe_to_string(ctx, -1));
    return 0;
}

/* ... before doing eval(): */
duk_push_c_function(ctx, native_print, DUK_VARARGS);
duk_put_global_string(ctx, "print");

其他小评论:

  • Duktape duk_config.h 检测您正在构​​建的平台和目标;就生成的二进制文件而言,Duktape 从未真正为“所有目标”构建。因此,例如,在 duk_config.h 中拥有多个平台并不会使结果变得更大。
  • 您不需要 arm32_config.yaml 中的值。应自动检测到 ARM32。
  • 您使用的 Makefile.hello 来自 Duktape 1.x(改编自),而您正在编译的代码来自 Duktape master。Duktape master 将忽略 CCOPTS 选项(它们进入 tools/configure.py),并且它们的格式在示例中是错误的,例如-DUK_USE_FATAL_HANDLER定义预处理器值UK_USE_FATAL_HANDLER
于 2016-12-14T22:11:50.727 回答