Embedded 如何从duktape执行shell命令

Embedded 如何从duktape执行shell命令,embedded,iot,duktape,Embedded,Iot,Duktape,我需要为嵌入式设备构建一个javascript引擎(duktape或jerryscript),它应该能够执行shell命令。如何实现这一点?使用C中的duktape,您可以轻松创建本机ECMAScript函数,并通过全局对象创建对它的引用: #include "duktape.h" int main() { /* create heap */ duk_context* heap = duk_create_heap(NULL,NULL,NULL,NULL,

我需要为嵌入式设备构建一个javascript引擎(duktape或jerryscript),它应该能够执行shell命令。如何实现这一点?

使用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. */ /* ... */ } #包括“duktape.h” int main(){ /*创建堆*/ duk_context*heap=duk_create_heap(NULL,NULL,NULL,NULL, NULL);/*最好在此设置错误回调*/ /*将本机函数推送到堆栈上*/ duk_push_c_函数(ctx,/*堆上下文*/ &本机_js_shell,/*本机函数指针*/ 1) /*公认的论点*/ /*使此javascript函数成为全局对象的属性*/ duk_put_global_字符串(ctx,/*堆上下文*/ js中的“shell”);/*函数名*/ 返回0; } /*您的原生C函数*/ duk_ret_t native_js_shell(duk_context*ctx){ /*从javascript获取参数*/ const char*cmd=duk_safe_to_string(ctx,/*堆上下文*/ -1) ;/*堆栈上的位置*/ /*运行shell命令等*/ /* ... */ } 关于
duk.*
函数的所有解释都可以在中找到,但这可能会让您了解它的结构


p、 欢迎来到Stack Overflow!你的问题可能被贬低了,因为它几乎需要有人为你编写所有的代码。一般来说,在将来,试着自己做研究,遇到困难时问一些具体的问题。:)

使用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. */ /* ... */ } #包括“duktape.h” int main(){ /*创建堆*/ duk_context*heap=duk_create_heap(NULL,NULL,NULL,NULL, NULL);/*最好在此处设置错误回调*/ /*将本机函数推送到堆栈上*/ duk_push_c_函数(ctx,/*堆上下文*/ &本机_js_shell,/*本机函数指针*/ 1) /*公认的论点*/ /*使此javascript函数成为全局对象的属性*/ duk_put_global_字符串(ctx,/*堆上下文*/ js中的“shell”);/*函数名*/ 返回0; } /*您的原生C函数*/ duk_ret_t native_js_shell(duk_context*ctx){ /*从javascript获取参数*/ const char*cmd=duk_safe_to_string(ctx,/*堆上下文*/ -1) ;/*堆栈上的位置*/ /*运行shell命令等*/ /* ... */ } 关于
duk.*
函数的所有解释都可以在中找到,但这可能会让您了解它的结构


p、 欢迎来到Stack Overflow!你的问题可能被贬低了,因为它几乎需要有人为你编写所有的代码。一般来说,在将来,试着自己做研究,遇到困难时问一些具体的问题。:)

我的错。。在发布之前,我没有详细了解duktape。。将来会记得。。谢谢…当然!很乐意帮忙。如果它回答了问题,你可以接受。我的错。。在发布之前,我没有详细了解duktape。。将来会记得。。谢谢…当然!很乐意帮忙。如果它回答了这个问题,你就可以接受它。