quickjs调用C函数

QuickJS调用C函数,这个可以参考官方的std、os进行实现。

外部库

core.h

1 #include <string.h>
2 #include "quickjs-libc.h"
3 #include "cutils.h"
4 
5 extern JSModuleDef *js_init_module_core(JSContext *ctx, const char *module_name);

core.c

 1 #include "core.h"
 2 
 3 static char version_build_string[32] = {0};
 4 static void js_core_gen_version(){
 5   char s_month[5];
 6   int month, day, year;
 7   int hour, min, sec;
 8 
 9   static const char month_names[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
10   sscanf(__DATE__, "%s %d %d", s_month, &day, &year);
11   month = (strstr(month_names, s_month) - month_names) / 3 + 1;
12 
13   sscanf(__TIME__, "%d:%d:%d", &hour, &min, &sec);
14   snprintf(version_build_string, sizeof(version_build_string), "%04d-%02d-%02dT%02d:%02d:%02dZ",
15            year, month, day, hour, min, sec);
16 }
17 static JSValue js_core_read(JSContext *ctx, JSValueConst this_val,
18                             int argc, JSValueConst *argv)
19 {
20   return JS_NewString(ctx, version_build_string);
21 }
22 static JSValue js_core_write(JSContext *ctx, JSValueConst this_val,
23                             int argc, JSValueConst *argv)
24 {
25   const char * str = JS_ToCString(ctx, argv[0]);
26   strcpy(version_build_string, str);
27   return JS_NewString(ctx, version_build_string);
28 }
29 
30 static const JSCFunctionListEntry js_core_functions[] = {
31     JS_CFUNC_DEF("readVersion", 0, js_core_read),
32     JS_CFUNC_DEF("writeVersion", 1, js_core_write),
33     JS_PROP_STRING_DEF("version", version_build_string, JS_PROP_C_W_E),
34 };
35 
36 static int js_core_init(JSContext *ctx, JSModuleDef *m)
37 {
38   js_core_gen_version(); // generate version string
39   return JS_SetModuleExportList(ctx, m, js_core_functions,
40                                 countof(js_core_functions));
41 }
42 
43 JSModuleDef *js_init_module_core(JSContext *ctx, const char *module_name)
44 {
45   JSModuleDef *m;
46   m = JS_NewCModule(ctx, module_name, js_core_init);
47   if (!m)
48   {
49     return NULL;
50   }
51   JS_AddModuleExportList(ctx, m, js_core_functions, countof(js_core_functions));
52   return m;
53 }

test.c

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <stdlib.h>
 4 #include "quickjs-libc.h"
 5 #include "core.h"
 6 
 7 int main(int argc, char *argv[])
 8 {
 9   printf("HELLO\n");
10 
11   JSRuntime *rt = JS_NewRuntime();
12   js_std_init_handlers(rt);
13   JSContext *ctx = JS_NewContext(rt);
14   /* system modules */
15   js_init_module_std(ctx, "std");
16   js_init_module_os(ctx, "os");
17   js_init_module_core(ctx, "core");
18   
19   const char *str =
20       "import * as std from 'std';\n"
21       "import * as os from 'os';\n"
22       "globalThis.std = std;\n"
23       "globalThis.os = os;\n"
24       "var console = {};\n"
25       "console.log = value => std.printf(value);\n"
26       "";
27   JSValue init_compile =
28       JS_Eval(ctx, str, strlen(str), "<input>", JS_EVAL_TYPE_MODULE | JS_EVAL_FLAG_COMPILE_ONLY);
29 
30   js_module_set_import_meta(ctx, init_compile, 1, 1);
31   JSValue init_run = JS_EvalFunction(ctx, init_compile);
32   
33   const char *test_val = R"(
34     import * as core from 'core';
35     var console = {};
36     console.log = value => globalThis.std.printf(value + "\n");
37     console.log(core.writeVersion("0.0.1"));
38     console.log(core.readVersion());
39     console.log("ver:" + core.version)
40     var obj = {"a": 1,"b": 2,"c": 3}
41     for(var key in obj){
42       console.log("-->" + key + ":" + obj[key]);
43     }
44     var props = Object.keys(obj);
45     console.log(props);
46 
47     os.setTimeout(()=>{std.printf('AAB\n')}, 2000)
48   )";
49   JSValue result = JS_Eval(ctx, test_val, strlen(test_val), "test", JS_EVAL_TYPE_MODULE);
50   printf("loop\n");
51   js_std_loop(ctx);
52 
53   printf("%s\n", __TIME__);
54   int32_t len;
55   int to_int_32;
56   if ((to_int_32 = JS_ToInt32(ctx, &len, result)) != 0) {
57     js_std_dump_error(ctx);
58   }
59 
60   JS_FreeContext(ctx);
61   JS_FreeRuntime(rt);
62   return 0;
63 }

编译 Makefile

1 main: test.c 
2         gcc test.c core.c libquickjs.a -o main.exe -I .

输出打印

 1 HELLO
 2 0.0.1
 3 0.0.1
 4 ver:2023-11-23T19:20:33Z
 5 -->a:1
 6 -->b:2
 7 -->c:3
 8 a,b,c
 9 loop
10 AAB
11 19:20:33

 

 参考资料

  https://github.com/quickjs-zh/QuickJS/blob/master/examples/fib.c

  https://github.com/quickjs-zh/QuickJS/blob/master/tests/bjson.c

本文地址:https://www.cnblogs.com/wunaozai/p/17851826.html

系列目录:https://www.cnblogs.com/wunaozai/p/17853962.html

posted @ 2023-11-28 19:35  无脑仔的小明  阅读(446)  评论(0编辑  收藏  举报