2017-05-11 9 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