-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdump_load0_nocleanup.c
52 lines (43 loc) · 1.21 KB
/
dump_load0_nocleanup.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
* This assume the first dep cell contains the actual JS code to load,
* then loads it directly. It is a tradeoff that sacrifices the first
* cell, but frees script args part for script to use.
*/
#include "glue.h"
#define SCRIPT_SIZE (128 * 1024)
int main() {
duk_context *ctx = duk_create_heap_default();
ckb_init(ctx);
syscall(4097, 0, 0, 0, 0, 0, 0);
unsigned char script[SCRIPT_SIZE];
uint64_t len = SCRIPT_SIZE;
int ret = ckb_load_cell_data(script, &len, 0, 0, CKB_SOURCE_CELL_DEP);
if (ret != CKB_SUCCESS) {
return ret;
}
if (len > SCRIPT_SIZE) {
return -1;
}
if (script[0] == 0xbf) {
/* Bytecode */
void *buf = duk_push_fixed_buffer(ctx, len);
memcpy(buf, script, len);
duk_load_function(ctx);
} else {
/* Source */
if (duk_pcompile_lstring(ctx, 0, (const char *) script, len) != 0) {
ckb_debug(duk_safe_to_string(ctx, -1));
return -2;
}
}
/* Provide a this value for convenience */
duk_push_global_object(ctx);
if (duk_pcall_method(ctx, 0) != 0) {
ckb_debug(duk_safe_to_string(ctx, -1));
return -3;
}
/* Skipping cleanup step to further save cycles */
/* duk_pop(ctx); */
/* duk_destroy_heap(ctx); */
return 0;
}