Skip to content

Commit a6e73ca

Browse files
authored
Handle serialization endianness transparently (#152)
Change JS_WriteObject() and JS_WriteObject2() to write little-endian data and update JS_ReadObject() to byte-swap data when running on a big-endian system. Obsoletes the JS_WRITE_OBJ_BSWAP flag, it is now a no-op. Fixes: #125
1 parent 0ecb2c8 commit a6e73ca

File tree

4 files changed

+18
-36
lines changed

4 files changed

+18
-36
lines changed

doc/quickjs.texi

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,6 @@ find the name of the dynamically loaded modules.
167167
Add initialization code for an external C module. See the
168168
@code{c_module} example.
169169

170-
@item -x
171-
Byte swapped output (only used for cross compilation).
172-
173170
@item -flto
174171
Use link time optimization. The compilation is slower but the
175172
executable is smaller and faster. This option is automatically set

qjsc.c

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ static namelist_t cname_list;
5151
static namelist_t cmodule_list;
5252
static namelist_t init_module_list;
5353
static FILE *outfile;
54-
static BOOL byte_swap;
5554
static const char *c_ident_prefix = "qjsc_";
5655

5756

@@ -152,11 +151,8 @@ static void output_object_code(JSContext *ctx,
152151
{
153152
uint8_t *out_buf;
154153
size_t out_buf_len;
155-
int flags;
156-
flags = JS_WRITE_OBJ_BYTECODE;
157-
if (byte_swap)
158-
flags |= JS_WRITE_OBJ_BSWAP;
159-
out_buf = JS_WriteObject(ctx, &out_buf_len, obj, flags);
154+
155+
out_buf = JS_WriteObject(ctx, &out_buf_len, obj, JS_WRITE_OBJ_BYTECODE);
160156
if (!out_buf) {
161157
js_std_dump_error(ctx);
162158
exit(1);
@@ -322,7 +318,6 @@ void help(void)
322318
"-m compile as Javascript module (default=autodetect)\n"
323319
"-D module_name compile a dynamically loaded module or worker\n"
324320
"-M module_name[,cname] add initialization code for an external C module\n"
325-
"-x byte swapped output\n"
326321
"-p prefix set the prefix of the generated C names\n"
327322
"-S n set the maximum stack size to 'n' bytes (default=%d)\n",
328323
JS_GetVersion(),
@@ -352,7 +347,6 @@ int main(int argc, char **argv)
352347
output_type = OUTPUT_C;
353348
cname = NULL;
354349
module = -1;
355-
byte_swap = FALSE;
356350
verbose = 0;
357351
stack_size = 0;
358352
memset(&dynamic_module_list, 0, sizeof(dynamic_module_list));
@@ -399,9 +393,6 @@ int main(int argc, char **argv)
399393
case 'D':
400394
namelist_add(&dynamic_module_list, optarg, NULL, 0);
401395
break;
402-
case 'x':
403-
byte_swap = TRUE;
404-
break;
405396
case 'v':
406397
verbose++;
407398
break;

quickjs.c

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31664,18 +31664,11 @@ typedef enum BCTagEnum {
3166431664
BC_TAG_OBJECT_REFERENCE,
3166531665
} BCTagEnum;
3166631666

31667-
#define BC_BASE_VERSION 2
31668-
#define BC_BE_VERSION 0x40
31669-
#ifdef WORDS_BIGENDIAN
31670-
#define BC_VERSION (BC_BASE_VERSION | BC_BE_VERSION)
31671-
#else
31672-
#define BC_VERSION BC_BASE_VERSION
31673-
#endif
31667+
#define BC_VERSION 3
3167431668

3167531669
typedef struct BCWriterState {
3167631670
JSContext *ctx;
3167731671
DynBuf dbuf;
31678-
BOOL byte_swap : 8;
3167931672
BOOL allow_bytecode : 8;
3168031673
BOOL allow_sab : 8;
3168131674
BOOL allow_reference : 8;
@@ -31717,28 +31710,37 @@ static const char * const bc_tag_str[] = {
3171731710
};
3171831711
#endif
3171931712

31713+
static inline BOOL is_be(void)
31714+
{
31715+
union {
31716+
uint16_t a;
31717+
uint8_t b;
31718+
} u = {0x100};
31719+
return u.b;
31720+
}
31721+
3172031722
static void bc_put_u8(BCWriterState *s, uint8_t v)
3172131723
{
3172231724
dbuf_putc(&s->dbuf, v);
3172331725
}
3172431726

3172531727
static void bc_put_u16(BCWriterState *s, uint16_t v)
3172631728
{
31727-
if (s->byte_swap)
31729+
if (is_be())
3172831730
v = bswap16(v);
3172931731
dbuf_put_u16(&s->dbuf, v);
3173031732
}
3173131733

3173231734
static __maybe_unused void bc_put_u32(BCWriterState *s, uint32_t v)
3173331735
{
31734-
if (s->byte_swap)
31736+
if (is_be())
3173531737
v = bswap32(v);
3173631738
dbuf_put_u32(&s->dbuf, v);
3173731739
}
3173831740

3173931741
static void bc_put_u64(BCWriterState *s, uint64_t v)
3174031742
{
31741-
if (s->byte_swap)
31743+
if (is_be())
3174231744
v = bswap64(v);
3174331745
dbuf_put(&s->dbuf, (uint8_t *)&v, sizeof(v));
3174431746
}
@@ -31908,7 +31910,7 @@ static int JS_WriteFunctionBytecode(BCWriterState *s,
3190831910
pos += len;
3190931911
}
3191031912

31911-
if (s->byte_swap)
31913+
if (is_be())
3191231914
bc_byte_swap(bc_buf, bc_len);
3191331915

3191431916
dbuf_put(&s->dbuf, bc_buf, bc_len);
@@ -32405,15 +32407,10 @@ static int JS_WriteObjectAtoms(BCWriterState *s)
3240532407
JSRuntime *rt = s->ctx->rt;
3240632408
DynBuf dbuf1;
3240732409
int i, atoms_size;
32408-
uint8_t version;
3240932410

3241032411
dbuf1 = s->dbuf;
3241132412
js_dbuf_init(s->ctx, &s->dbuf);
32412-
32413-
version = BC_VERSION;
32414-
if (s->byte_swap)
32415-
version ^= BC_BE_VERSION;
32416-
bc_put_u8(s, version);
32413+
bc_put_u8(s, BC_VERSION);
3241732414

3241832415
bc_put_leb128(s, s->idx_to_atom_count);
3241932416
for(i = 0; i < s->idx_to_atom_count; i++) {
@@ -32446,8 +32443,6 @@ uint8_t *JS_WriteObject2(JSContext *ctx, size_t *psize, JSValueConst obj,
3244632443

3244732444
memset(s, 0, sizeof(*s));
3244832445
s->ctx = ctx;
32449-
/* XXX: byte swapped output is untested */
32450-
s->byte_swap = ((flags & JS_WRITE_OBJ_BSWAP) != 0);
3245132446
s->allow_bytecode = ((flags & JS_WRITE_OBJ_BYTECODE) != 0);
3245232447
s->allow_sab = ((flags & JS_WRITE_OBJ_SAB) != 0);
3245332448
s->allow_reference = ((flags & JS_WRITE_OBJ_REFERENCE) != 0);
@@ -33531,7 +33526,6 @@ static int JS_ReadObjectAtoms(BCReaderState *s)
3353133526

3353233527
if (bc_get_u8(s, &v8))
3353333528
return -1;
33534-
/* XXX: could support byte swapped input */
3353533529
if (v8 != BC_VERSION) {
3353633530
JS_ThrowSyntaxError(s->ctx, "invalid version (%d expected=%d)",
3353733531
v8, BC_VERSION);

quickjs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,7 @@ JS_EXTERN int JS_ExecutePendingJob(JSRuntime *rt, JSContext **pctx);
838838

839839
/* Object Writer/Reader (currently only used to handle precompiled code) */
840840
#define JS_WRITE_OBJ_BYTECODE (1 << 0) /* allow function/module */
841-
#define JS_WRITE_OBJ_BSWAP (1 << 1) /* byte swapped output */
841+
#define JS_WRITE_OBJ_BSWAP (0) /* byte swapped output (obsolete, handled transparently) */
842842
#define JS_WRITE_OBJ_SAB (1 << 2) /* allow SharedArrayBuffer */
843843
#define JS_WRITE_OBJ_REFERENCE (1 << 3) /* allow object references to
844844
encode arbitrary object

0 commit comments

Comments
 (0)