quickjs加载字节码文件

一、使用qjsc编译字节码

hello.js

 

console.log("Hello World")

 

qjsc.exe -c hello.js
 1 const uint32_t qjsc_hello_size = 78;
 2 
 3 const uint8_t qjsc_hello[78] = {
 4  0x02, 0x04, 0x0e, 0x63, 0x6f, 0x6e, 0x73, 0x6f,
 5  0x6c, 0x65, 0x06, 0x6c, 0x6f, 0x67, 0x16, 0x48,
 6  0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72,
 7  0x6c, 0x64, 0x10, 0x68, 0x65, 0x6c, 0x6c, 0x6f,
 8  0x2e, 0x6a, 0x73, 0x0e, 0x00, 0x06, 0x00, 0xa0,
 9  0x01, 0x00, 0x01, 0x00, 0x03, 0x00, 0x00, 0x14,
10  0x01, 0xa2, 0x01, 0x00, 0x00, 0x00, 0x38, 0xe1,
11  0x00, 0x00, 0x00, 0x42, 0xe2, 0x00, 0x00, 0x00,
12  0x04, 0xe3, 0x00, 0x00, 0x00, 0x24, 0x01, 0x00,
13  0xcd, 0x28, 0xc8, 0x03, 0x01, 0x00,
14 };

二、字节码写入到bin文件、读取bin文件并运行 

 1 #include "quickjs-libc.h"
 2 #include <stdio.h>
 3 #include <inttypes.h>
 4 #include <string.h>
 5 
 6 const uint32_t qjsc_hello_size = 78;
 7 
 8 const uint8_t qjsc_hello[78] = {
 9  0x02, 0x04, 0x0e, 0x63, 0x6f, 0x6e, 0x73, 0x6f,
10  0x6c, 0x65, 0x06, 0x6c, 0x6f, 0x67, 0x16, 0x48,
11  0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72,
12  0x6c, 0x64, 0x10, 0x68, 0x65, 0x6c, 0x6c, 0x6f,
13  0x2e, 0x6a, 0x73, 0x0e, 0x00, 0x06, 0x00, 0xa0,
14  0x01, 0x00, 0x01, 0x00, 0x03, 0x00, 0x00, 0x14,
15  0x01, 0xa2, 0x01, 0x00, 0x00, 0x00, 0x38, 0xe1,
16  0x00, 0x00, 0x00, 0x42, 0xe2, 0x00, 0x00, 0x00,
17  0x04, 0xe3, 0x00, 0x00, 0x00, 0x24, 0x01, 0x00,
18  0xcd, 0x28, 0xc8, 0x03, 0x01, 0x00,
19 };
20 
21 int writeBinFile()
22 {
23   FILE *f = NULL;
24   f = fopen("hello.bin", "wb");
25   if (f)
26   {
27     fwrite(qjsc_hello, 1, qjsc_hello_size, f);
28     fclose(f);
29   }
30   else
31   {
32     printf("Failed to open file\n");
33   }
34   return qjsc_hello_size;
35 }
36 char * readBinFile()
37 {
38   FILE *f = NULL;
39   f = fopen("hello.bin", "rb");
40   if(f == NULL){
41     printf("Failed to open file\n");
42     return NULL;
43   }
44   fseek(f, 0, SEEK_END);
45   int size = ftell(f);
46   fseek(f, 0, SEEK_SET);
47   printf("hello.bin size: %d\n", size);
48   size = size + 1;
49   char *buf = (char *)malloc(size);
50   memset(buf, 0, size);
51   fread(buf, 1, size, f);
52   fclose(f);
53   return buf;
54 }
55 
56 int main(int argc, char **argv)
57 {
58   (void) argv;
59   writeBinFile();
60   char * buf = readBinFile();
61   if(buf == NULL){
62     printf("Failed to read file\n");
63     return -1;
64   }
65 
66   JSRuntime * rt = JS_NewRuntime();
67   js_std_init_handlers(rt);
68   //设置模块加载函数,后续这里可以自定义模块加载
69   JS_SetModuleLoaderFunc(rt, NULL, js_module_loader, NULL);
70   JSContext * ctx = JS_NewContext(rt);
71   js_std_add_helpers(ctx, argc, argv);
72 
73   js_std_eval_binary(ctx, buf, qjsc_hello_size, 0);
74   // JS_Eval(ctx, "console.log('Hello World!');", 27, "hello.js", 0);
75   
76   js_std_loop(ctx);
77   JS_FreeContext(ctx);
78   JS_FreeRuntime(rt);
79   return 0;
80 }
gcc .\main.c .\libquickjs.a -I ..\..\ -o main

三、函数

hello.js

1 function test(msg){
2   console.log("Hello World: " + msg);
3 }

编译成bin字节码后,执行

 1 int main(int argc, char **argv)
 2 {
 3   (void) argv;
 4   writeBinFile();
 5   char * buf = readBinFile();
 6   if(buf == NULL){
 7     printf("Failed to read file\n");
 8     return -1;
 9   }
10 
11   JSRuntime * rt = JS_NewRuntime();
12   js_std_init_handlers(rt);
13   //设置模块加载函数,后续这里可以自定义模块加载
14   JS_SetModuleLoaderFunc(rt, NULL, js_module_loader, NULL);
15   JSContext * ctx = JS_NewContext(rt);
16   js_std_add_helpers(ctx, argc, argv);
17 
18   js_std_eval_binary(ctx, buf, qjsc_hello_size, 0);
19   char * cmd = "test('wunaozai');";
20   JS_Eval(ctx, cmd, strlen(cmd), "hello.js", 0);
21   
22   js_std_loop(ctx);
23   JS_FreeContext(ctx);
24   JS_FreeRuntime(rt);
25   return 0;
26 }

打印输出结果

PS D:\test\quickjs\examples\byte> .\main
hello.bin size: 133
Hello World: wunaozai

 

 

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

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

posted @ 2023-12-04 17:48  无脑仔的小明  阅读(79)  评论(0编辑  收藏  举报