2017-05-11 1 views

答えて

2

Cでduktapeを使用すると、簡単にネイティブのECMAScript関数を作成することができますし、グローバルオブジェクトを経由して、それへの参照を作成します。

 
#include "duktape.h" 

int main() { 
    /* create heap */ 
    duk_context* heap = duk_create_heap(NULL,NULL,NULL,NULL, 
             NULL); /* preferably, set an error callback here */ 

    /* push the native function on the stack */ 
    duk_push_c_function(ctx, /* heap context */ 
         &native_js_shell, /* native function pointer */ 
         1); /* accepted arguments */ 

    /* make this javascript function a property of the global object */ 
    duk_put_global_string(ctx, /* heap context*/ 
          "shell"); /* function name in js */ 

    return 0; 
} 

/* Your native C function */ 
duk_ret_t native_js_shell(duk_context* ctx) { 
    /* obtain the argument from javascript */ 
    const char* cmd = duk_safe_to_string(ctx, /* heap context */ 
             -1); /* position on stack */ 

    /* run the shell command, etc. */ 
    /* ... */ 
} 

duk_*機能のためのすべての説明は、duktape APIで見つけることができますが、おそらく、これはどのように構造化されているかのアイデアを提供します。

p.s.スタックオーバーフローへようこそ!あなたの質問は、誰かがあなたのためにすべてのコードを書かなければならないことをかなり必要とするので、下げられているかもしれません。一般的には、将来、あなた自身で研究を行い、特定の質問をしてください。 :)

+0

私の悪い..投稿した前にduktapeについて多くは行っていません..将来的に覚えています。ありがとう... – Ram

+0

確かに!お力になれて、嬉しいです。質問に答えたら、それを受け入れることができます。 – Codesmith

関連する問題