Browse Source

Use varuint for more initdata encodings

pull/1152/head
Sami Vaarala 8 years ago
parent
commit
d3b568b722
  1. 18
      src-input/duk_hobject.h
  2. 6
      src-input/duk_hobject_class.c
  3. 46
      src-input/duk_hthread_builtins.c
  4. 25
      src-input/duk_util_bitdecoder.c

18
src-input/duk_hobject.h

@ -79,16 +79,16 @@
/* E5 Section 8.6.2 + custom classes */
#define DUK_HOBJECT_CLASS_NONE 0
#define DUK_HOBJECT_CLASS_ARGUMENTS 1
#define DUK_HOBJECT_CLASS_OBJECT 1
#define DUK_HOBJECT_CLASS_ARRAY 2
#define DUK_HOBJECT_CLASS_BOOLEAN 3
#define DUK_HOBJECT_CLASS_DATE 4
#define DUK_HOBJECT_CLASS_ERROR 5
#define DUK_HOBJECT_CLASS_FUNCTION 6
#define DUK_HOBJECT_CLASS_JSON 7
#define DUK_HOBJECT_CLASS_MATH 8
#define DUK_HOBJECT_CLASS_NUMBER 9
#define DUK_HOBJECT_CLASS_OBJECT 10
#define DUK_HOBJECT_CLASS_FUNCTION 3
#define DUK_HOBJECT_CLASS_ARGUMENTS 4
#define DUK_HOBJECT_CLASS_BOOLEAN 5
#define DUK_HOBJECT_CLASS_DATE 6
#define DUK_HOBJECT_CLASS_ERROR 7
#define DUK_HOBJECT_CLASS_JSON 8
#define DUK_HOBJECT_CLASS_MATH 9
#define DUK_HOBJECT_CLASS_NUMBER 10
#define DUK_HOBJECT_CLASS_REGEXP 11
#define DUK_HOBJECT_CLASS_STRING 12
#define DUK_HOBJECT_CLASS_GLOBAL 13

6
src-input/duk_hobject_class.c

@ -95,16 +95,16 @@
/* Note: assumes that these string indexes are 8-bit, genstrings.py must ensure that */
DUK_INTERNAL duk_uint8_t duk_class_number_to_stridx[32] = {
DUK_STRIDX_EMPTY_STRING, /* NONE, intentionally empty */
DUK_STRIDX_UC_ARGUMENTS,
DUK_STRIDX_UC_OBJECT,
DUK_STRIDX_ARRAY,
DUK_STRIDX_UC_FUNCTION,
DUK_STRIDX_UC_ARGUMENTS,
DUK_STRIDX_UC_BOOLEAN,
DUK_STRIDX_DATE,
DUK_STRIDX_UC_ERROR,
DUK_STRIDX_UC_FUNCTION,
DUK_STRIDX_JSON,
DUK_STRIDX_MATH,
DUK_STRIDX_UC_NUMBER,
DUK_STRIDX_UC_OBJECT,
DUK_STRIDX_REG_EXP,
DUK_STRIDX_UC_STRING,
DUK_STRIDX_GLOBAL,

46
src-input/duk_hthread_builtins.c

@ -10,19 +10,12 @@
* Encoding constants, must match genbuiltins.py
*/
#define DUK__CLASS_BITS 5
#define DUK__BIDX_BITS 7
#define DUK__STRIDX_BITS 9 /* XXX: try to optimize to 8 (would now be possible, <200 used) */
#define DUK__NATIDX_BITS 8
#define DUK__PROP_FLAGS_BITS 3
#define DUK__LENGTH_PROP_BITS 3
#define DUK__NARGS_BITS 3
#define DUK__PROP_TYPE_BITS 3
#define DUK__NARGS_VARARGS_MARKER 0x07
#define DUK__NO_CLASS_MARKER 0x00 /* 0 = DUK_HOBJECT_CLASS_NONE */
#define DUK__NO_BIDX_MARKER 0x7f
#define DUK__NO_STRIDX_MARKER 0xff
#define DUK__PROP_TYPE_DOUBLE 0
#define DUK__PROP_TYPE_STRING 1
@ -161,7 +154,7 @@ DUK_INTERNAL void duk_hthread_create_builtin_objects(duk_hthread *thr) {
DUK_LOCAL void duk__push_stridx(duk_context *ctx, duk_bitdecoder_ctx *bd) {
duk_small_uint_t n;
n = (duk_small_uint_t) duk_bd_decode(bd, DUK__STRIDX_BITS);
n = (duk_small_uint_t) duk_bd_decode_varuint(bd);
DUK_ASSERT_DISABLE(n >= 0); /* unsigned */
DUK_ASSERT(n < DUK_HEAP_NUM_STRINGS);
duk_push_hstring_stridx(ctx, n);
@ -177,10 +170,15 @@ DUK_LOCAL void duk__push_string(duk_context *ctx, duk_bitdecoder_ctx *bd) {
duk_push_lstring(ctx, (const char *) tmp, (duk_size_t) len);
}
DUK_LOCAL void duk__push_stridx_or_string(duk_context *ctx, duk_bitdecoder_ctx *bd) {
if (duk_bd_decode_flag(bd)) {
duk_small_uint_t n;
n = (duk_small_uint_t) duk_bd_decode_varuint(bd);
if (n == 0) {
duk__push_string(ctx, bd);
} else {
duk__push_stridx(ctx, bd);
n--;
DUK_ASSERT(n < DUK_HEAP_NUM_STRINGS);
duk_push_hstring_stridx(ctx, n);
}
}
DUK_LOCAL void duk__push_double(duk_context *ctx, duk_bitdecoder_ctx *bd) {
@ -231,7 +229,7 @@ DUK_INTERNAL void duk_hthread_create_builtin_objects(duk_hthread *thr) {
duk_small_uint_t class_num;
duk_small_int_t len = -1; /* must be signed */
class_num = (duk_small_uint_t) duk_bd_decode(bd, DUK__CLASS_BITS);
class_num = (duk_small_uint_t) duk_bd_decode_varuint(bd);
len = (duk_small_int_t) duk_bd_decode_flagged(bd, DUK__LENGTH_PROP_BITS, (duk_int32_t) -1 /*def_value*/);
if (class_num == DUK_HOBJECT_CLASS_FUNCTION) {
@ -243,7 +241,7 @@ DUK_INTERNAL void duk_hthread_create_builtin_objects(duk_hthread *thr) {
DUK_DDD(DUK_DDDPRINT("len=%ld", (long) len));
DUK_ASSERT(len >= 0);
natidx = (duk_small_uint_t) duk_bd_decode(bd, DUK__NATIDX_BITS);
natidx = (duk_small_uint_t) duk_bd_decode_varuint(bd);
DUK_ASSERT(natidx != 0);
c_func = duk_bi_native_functions[natidx];
DUK_ASSERT(c_func != NULL);
@ -373,30 +371,33 @@ DUK_INTERNAL void duk_hthread_create_builtin_objects(duk_hthread *thr) {
DUK_DDD(DUK_DDDPRINT("initializing built-in object at index %ld", (long) i));
h = duk_known_hobject(ctx, i);
t = (duk_small_uint_t) duk_bd_decode(bd, DUK__BIDX_BITS);
if (t != DUK__NO_BIDX_MARKER) {
t = (duk_small_uint_t) duk_bd_decode_varuint(bd);
if (t > 0) {
t--;
DUK_DDD(DUK_DDDPRINT("set internal prototype: built-in %ld", (long) t));
DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, h, duk_known_hobject(ctx, t));
}
t = (duk_small_uint_t) duk_bd_decode(bd, DUK__BIDX_BITS);
if (t != DUK__NO_BIDX_MARKER) {
t = (duk_small_uint_t) duk_bd_decode_varuint(bd);
if (t > 0) {
/* 'prototype' property for all built-in objects (which have it) has attributes:
* [[Writable]] = false,
* [[Enumerable]] = false,
* [[Configurable]] = false
*/
t--;
DUK_DDD(DUK_DDDPRINT("set external prototype: built-in %ld", (long) t));
duk_xdef_prop_stridx_builtin(ctx, i, DUK_STRIDX_PROTOTYPE, t, DUK_PROPDESC_FLAGS_NONE);
}
t = (duk_small_uint_t) duk_bd_decode(bd, DUK__BIDX_BITS);
if (t != DUK__NO_BIDX_MARKER) {
t = (duk_small_uint_t) duk_bd_decode_varuint(bd);
if (t > 0) {
/* 'constructor' property for all built-in objects (which have it) has attributes:
* [[Writable]] = true,
* [[Enumerable]] = false,
* [[Configurable]] = true
*/
t--;
DUK_DDD(DUK_DDDPRINT("set external constructor: built-in %ld", (long) t));
duk_xdef_prop_stridx_builtin(ctx, i, DUK_STRIDX_CONSTRUCTOR, t, DUK_PROPDESC_FLAGS_WC);
}
@ -455,8 +456,7 @@ DUK_INTERNAL void duk_hthread_create_builtin_objects(duk_hthread *thr) {
case DUK__PROP_TYPE_BUILTIN: {
duk_small_uint_t bidx;
bidx = (duk_small_uint_t) duk_bd_decode(bd, DUK__BIDX_BITS);
DUK_ASSERT(bidx != DUK__NO_BIDX_MARKER);
bidx = (duk_small_uint_t) duk_bd_decode_varuint(bd);
duk_dup(ctx, (duk_idx_t) bidx);
break;
}
@ -473,8 +473,8 @@ DUK_INTERNAL void duk_hthread_create_builtin_objects(duk_hthread *thr) {
break;
}
case DUK__PROP_TYPE_ACCESSOR: {
duk_small_uint_t natidx_getter = (duk_small_uint_t) duk_bd_decode(bd, DUK__NATIDX_BITS);
duk_small_uint_t natidx_setter = (duk_small_uint_t) duk_bd_decode(bd, DUK__NATIDX_BITS);
duk_small_uint_t natidx_getter = (duk_small_uint_t) duk_bd_decode_varuint(bd);
duk_small_uint_t natidx_setter = (duk_small_uint_t) duk_bd_decode_varuint(bd);
duk_small_uint_t accessor_magic = (duk_small_uint_t) duk_bd_decode_varuint(bd);
duk_c_function c_func_getter;
duk_c_function c_func_setter;
@ -530,7 +530,7 @@ DUK_INTERNAL void duk_hthread_create_builtin_objects(duk_hthread *thr) {
duk__push_stridx_or_string(ctx, bd);
h_key = duk_known_hstring(ctx, -1);
DUK_UNREF(h_key);
natidx = (duk_small_uint_t) duk_bd_decode(bd, DUK__NATIDX_BITS);
natidx = (duk_small_uint_t) duk_bd_decode_varuint(bd);
c_length = (duk_small_uint_t) duk_bd_decode(bd, DUK__LENGTH_PROP_BITS);
c_nargs = (duk_int_t) duk_bd_decode_flagged(bd, DUK__NARGS_BITS, (duk_int32_t) c_length /*def_value*/);

25
src-input/duk_util_bitdecoder.c

@ -73,21 +73,22 @@ DUK_INTERNAL duk_uint32_t duk_bd_decode_flagged(duk_bitdecoder_ctx *ctx, duk_sma
DUK_INTERNAL duk_uint32_t duk_bd_decode_varuint(duk_bitdecoder_ctx *ctx) {
duk_small_uint_t t;
/* Numbers 0-3 dominate input heavily, so use an initial flag byte for
* them, encoding into 3 bits. Other numbers are most often in [4,63]
* so encode that range efficiently with 7 bits. Reserve a few special
* values for rare long encodings.
/* The bit encoding choices here are based on manual testing against
* the actual varuints generated by genbuiltins.py.
*/
if (duk_bd_decode_flag(ctx)) {
t = duk_bd_decode(ctx, 6);
switch (duk_bd_decode(ctx, 2)) {
case 0:
return 0; /* [0,0] */
case 1:
return duk_bd_decode(ctx, 2) + 1; /* [1,4] */
case 2:
return duk_bd_decode(ctx, 5) + 5; /* [5,36] */
default:
t = duk_bd_decode(ctx, 7);
if (t == 0) {
return duk_bd_decode(ctx, 16);
} else if (t == 1) {
return duk_bd_decode(ctx, 32);
return duk_bd_decode(ctx, 20);
}
return (t - 2) + 4; /* Covers [4,65] */
} else {
return duk_bd_decode(ctx, 2);
return (t - 1) + 37; /* [37,163] */
}
}

Loading…
Cancel
Save