从C调用球拍功能

时间:2016-03-29 14:14:20

标签: c racket embedding

我有一个球拍模块hw.rkt:

#lang racket/base

(provide hw)

(define (hw) (displayln "Hello, world!"))

我想编写一个嵌入Racket运行时的C程序并应用(hw)过程。

示例代码here演示了如何嵌入Racket运行时并应用racket/base中的过程,或者读取和评估S表达式,但我已经有了没有运气修改此代码以允许访问(hw)程序。

This page似乎是说可能通过首先使用raco ctool --c-mods将hw.rkt编译为hw.c来做我想做的事情,这只是工作原理我尝试时很好,但我仍然无法访问(hw)程序。

如果某人可以发布完整的示例程序,或者只是描述要使用哪些C函数,我将非常感激。从那里我可以弄清楚剩下的。

编辑以提供我尝试过的事情的示例。

我修改了示例程序以摆脱" evaluate命令行参数"咬并直接跳到REPL以便我可以试验。因此(使用" hw.c"运行raco ctool --c-mods hw.c ++libs racket/base hw.rkt的结果):

#define MZ_PRECISE_GC
#include "scheme.h"

#include "hw.c"

static int run(Scheme_Env *e, int argc, char *argv[])
{
  Scheme_Object *curout = NULL, *v = NULL, *a[2] = {NULL, NULL};
  Scheme_Config *config = NULL;
  int i;
  mz_jmp_buf * volatile save = NULL, fresh;

  MZ_GC_DECL_REG(8);
  MZ_GC_VAR_IN_REG(0, e);
  MZ_GC_VAR_IN_REG(1, curout);
  MZ_GC_VAR_IN_REG(2, save);
  MZ_GC_VAR_IN_REG(3, config);
  MZ_GC_VAR_IN_REG(4, v);
  MZ_GC_ARRAY_VAR_IN_REG(5, a, 2);

  MZ_GC_REG();

  declare_modules(e);

  v = scheme_intern_symbol("racket/base");
  scheme_namespace_require(v);

  config = scheme_current_config();
  curout = scheme_get_param(config, MZCONFIG_OUTPUT_PORT);

  save = scheme_current_thread->error_buf;
  scheme_current_thread->error_buf = &fresh;
  if (scheme_setjmp(scheme_error_buf)) {
    scheme_current_thread->error_buf = save;
    return -1; /* There was an error */
  } else {
    /* read-eval-print loop, uses initial Scheme_Env: */
    a[0] = scheme_intern_symbol("racket/base");
    a[1] = scheme_intern_symbol("read-eval-print-loop");
    v = scheme_dynamic_require(2, a);
    scheme_apply(v, 0, NULL);
    scheme_current_thread->error_buf = save;
  }

  MZ_GC_UNREG();

  return 0;
}

int main(int argc, char *argv[])
{
  return scheme_main_setup(1, run, argc, argv);
}

不起作用的事情(及其错误信息):

从REPL调用(hw)

hw: undefined:
 cannot reference undefined identifier
  context...:
   /usr/local/share/racket/collects/racket/private/misc.rkt:87:7

((dynamic-require 'hw 'hw))

standard-module-name-resolver: collection not found
  for module path: hw
  collection: "hw"
  in collection directories:
  context...:
   show-collection-err
   standard-module-name-resolver
   /usr/local/share/racket/collects/racket/private/misc.rkt:87:7

((dynamic-require "hw.rkt" 'hw))

standard-module-name-resolver: collection not found
  for module path: racket/base/lang/reader
  collection: "racket/base/lang"
  in collection directories:
  context...:
   show-collection-err
   standard-module-name-resolver
   standard-module-name-resolver
   /usr/local/share/racket/collects/racket/private/misc.rkt:87:7

编辑示例代码

v = scheme_intern_symbol("racket/base");
scheme_namespace_require(v);
v = scheme_intern_symbol("hw");
scheme_namespace_require(v);

错误:

standard-module-name-resolver: collection not found
  for module path: hw
  collection: "hw"
  in collection directories:
  context...:
   show-collection-err
   standard-module-name-resolver
SIGSEGV MAPERR sicode 1 fault on addr 0xd0
Aborted

(段错误可能是因为我在尝试scheme_namespace_require之前没有检查' v'的价值。)

编辑示例代码mk。 2

v = scheme_intern_symbol("racket/base");
scheme_namespace_require(v);
v = scheme_intern_symbol("hw.rkt");
scheme_namespace_require(v);

错误:

hw.rkt: bad module path
  in: hw.rkt
  context...:
   standard-module-name-resolver
SIGSEGV MAPERR sicode 1 fault on addr 0xd0
Aborted

(re:segfault:如上所述)

编辑示例代码mk。 3

v = scheme_intern_symbol("racket/base");
scheme_namespace_require(v);
v = scheme_intern_symbol("./hw.rkt");
scheme_namespace_require(v);

(如上所述)

编辑示例代码mk。 4

/* read-eval-print-loop, uses initial Scheme_Env: */
a[0] = scheme_intern_symbol("hw");
a[1] = scheme_intern_symbol("hw");
v = scheme_dynamic_require(2, a);

(如mk.1,保存段错误)

编辑示例代码mk。 5

/* read-eval-print loop, uses initial Scheme_Env: */
a[0] = scheme_intern_symbol("hw");
a[1] = scheme_eval(a[0], e);
scheme_apply(a[1], 0, NULL);

错误:

hw: undefined;
 cannot reference undefined identifier

1 个答案:

答案 0 :(得分:3)

Matthew Flatt回答here。使用dynamic-require时,我需要引用模块的名称两次,而不是一次。感谢Flatt博士的帮助。