mirror of
https://github.com/DoneJS-Runtime/quickjs-done-nextgen.git
synced 2025-01-09 17:43:15 +00:00
Add strip option in qjsc
to reduce object size (#388)
- `-s` strips the source code - `-ss` strips source and line/column numbers information - `qjsc repl.js` generates an object size of **105726** bytes - `qjsc -s repl.js` generates an object size of **20853** bytes - `qjsc -ss repl.js` generates an object size of only **16147** bytes - compile repl.js with `-ss` - bump byte code version to 12
This commit is contained in:
parent
43dc65d605
commit
f326a7a195
9 changed files with 47 additions and 17 deletions
2
Makefile
2
Makefile
|
@ -61,7 +61,7 @@ clean:
|
||||||
cmake --build $(BUILD_DIR) --target clean
|
cmake --build $(BUILD_DIR) --target clean
|
||||||
|
|
||||||
codegen: $(QJSC)
|
codegen: $(QJSC)
|
||||||
$(QJSC) -o gen/repl.c -m repl.js
|
$(QJSC) -ss -o gen/repl.c -m repl.js
|
||||||
$(QJSC) -e -o gen/function_source.c tests/function_source.js
|
$(QJSC) -e -o gen/function_source.c tests/function_source.js
|
||||||
$(QJSC) -e -o gen/hello.c examples/hello.js
|
$(QJSC) -e -o gen/hello.c examples/hello.js
|
||||||
$(QJSC) -e -o gen/hello_module.c -m examples/hello_module.js
|
$(QJSC) -e -o gen/hello_module.c -m examples/hello_module.js
|
||||||
|
|
Binary file not shown.
BIN
gen/hello.c
BIN
gen/hello.c
Binary file not shown.
Binary file not shown.
BIN
gen/repl.c
BIN
gen/repl.c
Binary file not shown.
BIN
gen/test_fib.c
BIN
gen/test_fib.c
Binary file not shown.
17
qjsc.c
17
qjsc.c
|
@ -56,7 +56,7 @@ static namelist_t cmodule_list;
|
||||||
static namelist_t init_module_list;
|
static namelist_t init_module_list;
|
||||||
static FILE *outfile;
|
static FILE *outfile;
|
||||||
static const char *c_ident_prefix = "qjsc_";
|
static const char *c_ident_prefix = "qjsc_";
|
||||||
|
static int strip;
|
||||||
|
|
||||||
void namelist_add(namelist_t *lp, const char *name, const char *short_name,
|
void namelist_add(namelist_t *lp, const char *name, const char *short_name,
|
||||||
int flags)
|
int flags)
|
||||||
|
@ -155,8 +155,15 @@ static void output_object_code(JSContext *ctx,
|
||||||
{
|
{
|
||||||
uint8_t *out_buf;
|
uint8_t *out_buf;
|
||||||
size_t out_buf_len;
|
size_t out_buf_len;
|
||||||
|
int flags = JS_WRITE_OBJ_BYTECODE;
|
||||||
|
|
||||||
out_buf = JS_WriteObject(ctx, &out_buf_len, obj, JS_WRITE_OBJ_BYTECODE);
|
if (strip) {
|
||||||
|
flags |= JS_WRITE_OBJ_STRIP_SOURCE;
|
||||||
|
if (strip > 1)
|
||||||
|
flags |= JS_WRITE_OBJ_STRIP_DEBUG;
|
||||||
|
}
|
||||||
|
|
||||||
|
out_buf = JS_WriteObject(ctx, &out_buf_len, obj, flags);
|
||||||
if (!out_buf) {
|
if (!out_buf) {
|
||||||
js_std_dump_error(ctx);
|
js_std_dump_error(ctx);
|
||||||
exit(1);
|
exit(1);
|
||||||
|
@ -354,6 +361,7 @@ int main(int argc, char **argv)
|
||||||
cname = NULL;
|
cname = NULL;
|
||||||
module = -1;
|
module = -1;
|
||||||
verbose = 0;
|
verbose = 0;
|
||||||
|
strip = 0;
|
||||||
stack_size = 0;
|
stack_size = 0;
|
||||||
memset(&dynamic_module_list, 0, sizeof(dynamic_module_list));
|
memset(&dynamic_module_list, 0, sizeof(dynamic_module_list));
|
||||||
|
|
||||||
|
@ -362,7 +370,7 @@ int main(int argc, char **argv)
|
||||||
namelist_add(&cmodule_list, "os", "os", 0);
|
namelist_add(&cmodule_list, "os", "os", 0);
|
||||||
|
|
||||||
for(;;) {
|
for(;;) {
|
||||||
c = getopt(argc, argv, "ho:N:mxevM:p:S:D:");
|
c = getopt(argc, argv, "ho:N:mxesvM:p:S:D:");
|
||||||
if (c == -1)
|
if (c == -1)
|
||||||
break;
|
break;
|
||||||
switch(c) {
|
switch(c) {
|
||||||
|
@ -399,6 +407,9 @@ int main(int argc, char **argv)
|
||||||
case 'D':
|
case 'D':
|
||||||
namelist_add(&dynamic_module_list, optarg, NULL, 0);
|
namelist_add(&dynamic_module_list, optarg, NULL, 0);
|
||||||
break;
|
break;
|
||||||
|
case 's':
|
||||||
|
strip++;
|
||||||
|
break;
|
||||||
case 'v':
|
case 'v':
|
||||||
verbose++;
|
verbose++;
|
||||||
break;
|
break;
|
||||||
|
|
39
quickjs.c
39
quickjs.c
|
@ -32601,7 +32601,7 @@ typedef enum BCTagEnum {
|
||||||
BC_TAG_OBJECT_REFERENCE,
|
BC_TAG_OBJECT_REFERENCE,
|
||||||
} BCTagEnum;
|
} BCTagEnum;
|
||||||
|
|
||||||
#define BC_VERSION 11
|
#define BC_VERSION 12
|
||||||
|
|
||||||
typedef struct BCWriterState {
|
typedef struct BCWriterState {
|
||||||
JSContext *ctx;
|
JSContext *ctx;
|
||||||
|
@ -32609,6 +32609,8 @@ typedef struct BCWriterState {
|
||||||
BOOL allow_bytecode : 8;
|
BOOL allow_bytecode : 8;
|
||||||
BOOL allow_sab : 8;
|
BOOL allow_sab : 8;
|
||||||
BOOL allow_reference : 8;
|
BOOL allow_reference : 8;
|
||||||
|
BOOL allow_source : 1;
|
||||||
|
BOOL allow_debug : 1;
|
||||||
uint32_t first_atom;
|
uint32_t first_atom;
|
||||||
uint32_t *atom_to_idx;
|
uint32_t *atom_to_idx;
|
||||||
int atom_to_idx_size;
|
int atom_to_idx_size;
|
||||||
|
@ -32971,6 +32973,7 @@ static int JS_WriteFunctionTag(BCWriterState *s, JSValue obj)
|
||||||
bc_set_flags(&flags, &idx, b->super_allowed, 1);
|
bc_set_flags(&flags, &idx, b->super_allowed, 1);
|
||||||
bc_set_flags(&flags, &idx, b->arguments_allowed, 1);
|
bc_set_flags(&flags, &idx, b->arguments_allowed, 1);
|
||||||
bc_set_flags(&flags, &idx, b->backtrace_barrier, 1);
|
bc_set_flags(&flags, &idx, b->backtrace_barrier, 1);
|
||||||
|
bc_set_flags(&flags, &idx, s->allow_debug, 1);
|
||||||
assert(idx <= 16);
|
assert(idx <= 16);
|
||||||
bc_put_u16(s, flags);
|
bc_put_u16(s, flags);
|
||||||
bc_put_u8(s, b->js_mode);
|
bc_put_u8(s, b->js_mode);
|
||||||
|
@ -33027,13 +33030,19 @@ static int JS_WriteFunctionTag(BCWriterState *s, JSValue obj)
|
||||||
if (JS_WriteFunctionBytecode(s, b))
|
if (JS_WriteFunctionBytecode(s, b))
|
||||||
goto fail;
|
goto fail;
|
||||||
|
|
||||||
bc_put_atom(s, b->filename);
|
if (s->allow_debug) {
|
||||||
bc_put_leb128(s, b->line_num);
|
bc_put_atom(s, b->filename);
|
||||||
bc_put_leb128(s, b->col_num);
|
bc_put_leb128(s, b->line_num);
|
||||||
bc_put_leb128(s, b->pc2line_len);
|
bc_put_leb128(s, b->col_num);
|
||||||
dbuf_put(&s->dbuf, b->pc2line_buf, b->pc2line_len);
|
bc_put_leb128(s, b->pc2line_len);
|
||||||
bc_put_leb128(s, b->source_len);
|
dbuf_put(&s->dbuf, b->pc2line_buf, b->pc2line_len);
|
||||||
dbuf_put(&s->dbuf, b->source, b->source_len);
|
if (s->allow_source && b->source) {
|
||||||
|
bc_put_leb128(s, b->source_len);
|
||||||
|
dbuf_put(&s->dbuf, b->source, b->source_len);
|
||||||
|
} else {
|
||||||
|
bc_put_leb128(s, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
fail:
|
fail:
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -33409,6 +33418,8 @@ uint8_t *JS_WriteObject2(JSContext *ctx, size_t *psize, JSValue obj,
|
||||||
s->allow_bytecode = ((flags & JS_WRITE_OBJ_BYTECODE) != 0);
|
s->allow_bytecode = ((flags & JS_WRITE_OBJ_BYTECODE) != 0);
|
||||||
s->allow_sab = ((flags & JS_WRITE_OBJ_SAB) != 0);
|
s->allow_sab = ((flags & JS_WRITE_OBJ_SAB) != 0);
|
||||||
s->allow_reference = ((flags & JS_WRITE_OBJ_REFERENCE) != 0);
|
s->allow_reference = ((flags & JS_WRITE_OBJ_REFERENCE) != 0);
|
||||||
|
s->allow_source = ((flags & JS_WRITE_OBJ_STRIP_SOURCE) == 0);
|
||||||
|
s->allow_debug = ((flags & JS_WRITE_OBJ_STRIP_DEBUG) == 0);
|
||||||
/* XXX: could use a different version when bytecode is included */
|
/* XXX: could use a different version when bytecode is included */
|
||||||
if (s->allow_bytecode)
|
if (s->allow_bytecode)
|
||||||
s->first_atom = JS_ATOM_END;
|
s->first_atom = JS_ATOM_END;
|
||||||
|
@ -33864,7 +33875,7 @@ static JSValue JS_ReadFunctionTag(BCReaderState *s)
|
||||||
JSValue obj = JS_UNDEFINED;
|
JSValue obj = JS_UNDEFINED;
|
||||||
uint16_t v16;
|
uint16_t v16;
|
||||||
uint8_t v8;
|
uint8_t v8;
|
||||||
int idx, i, local_count;
|
int idx, i, local_count, has_debug_info;
|
||||||
int function_size, cpool_offset, byte_code_offset;
|
int function_size, cpool_offset, byte_code_offset;
|
||||||
int closure_var_offset, vardefs_offset;
|
int closure_var_offset, vardefs_offset;
|
||||||
uint32_t ic_len;
|
uint32_t ic_len;
|
||||||
|
@ -33887,6 +33898,7 @@ static JSValue JS_ReadFunctionTag(BCReaderState *s)
|
||||||
bc.super_allowed = bc_get_flags(v16, &idx, 1);
|
bc.super_allowed = bc_get_flags(v16, &idx, 1);
|
||||||
bc.arguments_allowed = bc_get_flags(v16, &idx, 1);
|
bc.arguments_allowed = bc_get_flags(v16, &idx, 1);
|
||||||
bc.backtrace_barrier = bc_get_flags(v16, &idx, 1);
|
bc.backtrace_barrier = bc_get_flags(v16, &idx, 1);
|
||||||
|
has_debug_info = bc_get_flags(v16, &idx, 1);
|
||||||
if (bc_get_u8(s, &v8))
|
if (bc_get_u8(s, &v8))
|
||||||
goto fail;
|
goto fail;
|
||||||
bc.js_mode = v8;
|
bc.js_mode = v8;
|
||||||
|
@ -34041,6 +34053,9 @@ static JSValue JS_ReadFunctionTag(BCReaderState *s)
|
||||||
goto fail;
|
goto fail;
|
||||||
bc_read_trace(s, "}\n");
|
bc_read_trace(s, "}\n");
|
||||||
}
|
}
|
||||||
|
if (!has_debug_info)
|
||||||
|
goto nodebug;
|
||||||
|
|
||||||
/* read optional debug information */
|
/* read optional debug information */
|
||||||
bc_read_trace(s, "debug {\n");
|
bc_read_trace(s, "debug {\n");
|
||||||
if (bc_get_atom(s, &b->filename))
|
if (bc_get_atom(s, &b->filename))
|
||||||
|
@ -34078,8 +34093,11 @@ static JSValue JS_ReadFunctionTag(BCReaderState *s)
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
bc_read_trace(s, "}\n");
|
bc_read_trace(s, "}\n");
|
||||||
|
|
||||||
|
nodebug:
|
||||||
b->realm = JS_DupContext(ctx);
|
b->realm = JS_DupContext(ctx);
|
||||||
return obj;
|
return obj;
|
||||||
|
|
||||||
fail:
|
fail:
|
||||||
JS_FreeAtom(ctx, bc.func_name);
|
JS_FreeAtom(ctx, bc.func_name);
|
||||||
JS_FreeValue(ctx, obj);
|
JS_FreeValue(ctx, obj);
|
||||||
|
@ -36510,7 +36528,8 @@ static JSValue js_function_toString(JSContext *ctx, JSValue this_val,
|
||||||
p = JS_VALUE_GET_OBJ(this_val);
|
p = JS_VALUE_GET_OBJ(this_val);
|
||||||
if (js_class_has_bytecode(p->class_id)) {
|
if (js_class_has_bytecode(p->class_id)) {
|
||||||
JSFunctionBytecode *b = p->u.func.function_bytecode;
|
JSFunctionBytecode *b = p->u.func.function_bytecode;
|
||||||
return JS_NewStringLen(ctx, b->source, b->source_len);
|
if (b->source)
|
||||||
|
return JS_NewStringLen(ctx, b->source, b->source_len);
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
JSValue name;
|
JSValue name;
|
||||||
|
|
|
@ -816,9 +816,9 @@ JS_EXTERN int JS_ExecutePendingJob(JSRuntime *rt, JSContext **pctx);
|
||||||
#define JS_WRITE_OBJ_BYTECODE (1 << 0) /* allow function/module */
|
#define JS_WRITE_OBJ_BYTECODE (1 << 0) /* allow function/module */
|
||||||
#define JS_WRITE_OBJ_BSWAP (0) /* byte swapped output (obsolete, handled transparently) */
|
#define JS_WRITE_OBJ_BSWAP (0) /* byte swapped output (obsolete, handled transparently) */
|
||||||
#define JS_WRITE_OBJ_SAB (1 << 2) /* allow SharedArrayBuffer */
|
#define JS_WRITE_OBJ_SAB (1 << 2) /* allow SharedArrayBuffer */
|
||||||
#define JS_WRITE_OBJ_REFERENCE (1 << 3) /* allow object references to
|
#define JS_WRITE_OBJ_REFERENCE (1 << 3) /* allow object references to encode arbitrary object graph */
|
||||||
encode arbitrary object
|
#define JS_WRITE_OBJ_STRIP_SOURCE (1 << 4) /* do not write source code information */
|
||||||
graph */
|
#define JS_WRITE_OBJ_STRIP_DEBUG (1 << 5) /* do not write debug information */
|
||||||
JS_EXTERN uint8_t *JS_WriteObject(JSContext *ctx, size_t *psize, JSValue obj, int flags);
|
JS_EXTERN uint8_t *JS_WriteObject(JSContext *ctx, size_t *psize, JSValue obj, int flags);
|
||||||
JS_EXTERN uint8_t *JS_WriteObject2(JSContext *ctx, size_t *psize, JSValue obj,
|
JS_EXTERN uint8_t *JS_WriteObject2(JSContext *ctx, size_t *psize, JSValue obj,
|
||||||
int flags, uint8_t ***psab_tab, size_t *psab_tab_len);
|
int flags, uint8_t ***psab_tab, size_t *psab_tab_len);
|
||||||
|
|
Loading…
Reference in a new issue