changed the compiler to set bytearray literals and array literals read-only in more contexts

This commit is contained in:
hyunghwan.chung 2018-05-07 14:33:07 +00:00
parent 8c0787608f
commit a78594d24f
2 changed files with 19 additions and 18 deletions

View File

@ -4551,7 +4551,7 @@ static int add_to_array_literal_buffer (moo_t* moo, moo_oop_t item)
return 0;
}
static int read_byte_array_literal (moo_t* moo, moo_oop_t* xlit)
static int read_byte_array_literal (moo_t* moo, int rdonly, moo_oop_t* xlit)
{
moo_ooi_t tmp;
moo_oop_t ba;
@ -4640,6 +4640,11 @@ static int read_byte_array_literal (moo_t* moo, moo_oop_t* xlit)
ba = moo_instantiate (moo, moo->_byte_array, &moo->c->balit.ptr[saved_balit_count], moo->c->balit.count - saved_balit_count);
if (!ba) goto oops;
if (rdonly)
{
MOO_ASSERT (moo, MOO_OOP_IS_POINTER(ba));
MOO_OBJ_SET_FLAGS_RDONLY (ba, 1);
}
*xlit = ba;
moo->c->balit.count = saved_balit_count;
@ -4684,6 +4689,12 @@ static int read_array_literal (moo_t* moo, int rdonly, moo_oop_t* xlit)
((moo_oop_oop_t)a)->slot[j] = moo->c->arlit.ptr[i];
}
if (rdonly)
{
MOO_ASSERT (moo, MOO_OOP_IS_POINTER(a));
MOO_OBJ_SET_FLAGS_RDONLY (a, 1);
}
*xlit = a;
moo->c->arlit.count = saved_arlit_count;
@ -4702,7 +4713,7 @@ static int compile_byte_array_literal (moo_t* moo)
MOO_ASSERT (moo, moo->c->balit.count == 0);
MOO_ASSERT (moo, TOKEN_TYPE(moo) == MOO_IOTOK_HASHBRACK);
if (read_byte_array_literal(moo, &lit) <= -1 ||
if (read_byte_array_literal(moo, 1, &lit) <= -1 ||
add_literal(moo, lit, &index) <= -1 ||
emit_single_param_instruction(moo, BCODE_PUSH_LITERAL_0, index) <= -1) return -1;
@ -4718,7 +4729,7 @@ static int compile_array_literal (moo_t* moo)
MOO_ASSERT (moo, moo->c->arlit.count == 0);
MOO_ASSERT (moo, TOKEN_TYPE(moo) == MOO_IOTOK_HASHPAREN);
if (read_array_literal(moo, 0, &lit) <= -1 ||
if (read_array_literal(moo, 1, &lit) <= -1 ||
add_literal(moo, lit, &index) <= -1 ||
emit_single_param_instruction(moo, BCODE_PUSH_LITERAL_0, index) <= -1) return -1;
@ -7870,12 +7881,7 @@ static moo_oop_t token_to_literal (moo_t* moo, int rdonly)
case MOO_IOTOK_HASHBRACK: /* #[ - byte array literal parenthesis */
{
moo_oop_t lit;
if (read_byte_array_literal(moo, &lit) <= -1) return MOO_NULL;
if (rdonly)
{
MOO_ASSERT (moo, lit && MOO_OOP_IS_POINTER(lit));
MOO_OBJ_SET_FLAGS_RDONLY (lit, 1);
}
if (read_byte_array_literal(moo, rdonly, &lit) <= -1) return MOO_NULL;
return lit;
}
@ -7883,11 +7889,6 @@ static moo_oop_t token_to_literal (moo_t* moo, int rdonly)
{
moo_oop_t lit;
if (read_array_literal(moo, rdonly, &lit) <= -1) return MOO_NULL;
if (rdonly)
{
MOO_ASSERT (moo, lit && MOO_OOP_IS_POINTER(lit));
MOO_OBJ_SET_FLAGS_RDONLY (lit, 1);
}
return lit;
}

View File

@ -566,7 +566,7 @@ static int print_object (moo_t* moo, moo_bitmask_t mask, moo_oop_t oop, outbfmt_
}
else
{
if (outbfmt(moo, mask, "<<%.*js>>", MOO_OBJ_GET_SIZE(c->name), ((moo_oop_char_t)c->name)->slot) <= -1) return -1;
if (outbfmt(moo, mask, "<<%.*js:%p>>", MOO_OBJ_GET_SIZE(c->name), ((moo_oop_char_t)c->name)->slot, oop) <= -1) return -1;
}
}