Skip to content

Instantly share code, notes, and snippets.

@MasterDuke17
Created March 29, 2020 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MasterDuke17/4290bdf90a53eb5ab206b6db27fdec0e to your computer and use it in GitHub Desktop.
Save MasterDuke17/4290bdf90a53eb5ab206b6db27fdec0e to your computer and use it in GitHub Desktop.
diff --git a/src/6model/reprs/VMArray.c b/src/6model/reprs/VMArray.c
index 7f5f79b99..03374c67a 100644
--- a/src/6model/reprs/VMArray.c
+++ b/src/6model/reprs/VMArray.c
@@ -986,8 +986,11 @@ static void asplice(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, void *
tail * repr_data->elem_size);
}
+ fprintf(stderr, "\n%s\n", MVM_exception_backtrace_line(tc, tc->cur_frame, 0, *(tc->interp_cur_op)));
+ MVMuint64 e = body->elems, s = body->start, ss = body->ssize;
/* now resize the array */
set_size_internal(tc, body, offset + elems1 + tail, repr_data);
+ fprintf(stderr, "increased by: elems = %lu, start = %lu, ssize = %lu\n\n", body->elems - e, body->start - s, body->ssize - ss);
start = body->start;
if (tail > 0 && count < elems1) {
diff --git a/src/strings/ops.c b/src/strings/ops.c
index a59bad6bd..ed828f4ac 100644
--- a/src/strings/ops.c
+++ b/src/strings/ops.c
@@ -1785,8 +1785,6 @@ MVMObject * MVM_string_encode_to_buf_config(MVMThreadContext *tc, MVMString *s,
}
if (!elem_size)
MVM_exception_throw_adhoc(tc, "encode requires a native int array");
- if (((MVMArray *)buf)->body.slots.any)
- MVM_exception_throw_adhoc(tc, "encode requires an empty array");
/* At least find_encoding may allocate on first call, so root just
* in case. */
@@ -1797,10 +1795,18 @@ MVMObject * MVM_string_encode_to_buf_config(MVMThreadContext *tc, MVMString *s,
});
/* Stash the encoded data in the VMArray. */
- ((MVMArray *)buf)->body.slots.i8 = (MVMint8 *)encoded;
- ((MVMArray *)buf)->body.start = 0;
- ((MVMArray *)buf)->body.ssize = output_size / elem_size;
- ((MVMArray *)buf)->body.elems = output_size / elem_size;
+ if (((MVMArray *)buf)->body.slots.any) {
+ MVMuint32 prev_elems = ((MVMArray *)buf)->body.elems;
+ MVM_repr_pos_set_elems(tc, buf, ((MVMArray *)buf)->body.elems + output_size / elem_size);
+ memmove(((MVMArray *)buf)->body.slots.i8 + prev_elems, (MVMint8 *)encoded, output_size);
+ }
+ else {
+ ((MVMArray *)buf)->body.slots.i8 = (MVMint8 *)encoded;
+ ((MVMArray *)buf)->body.start = 0;
+ ((MVMArray *)buf)->body.ssize = output_size / elem_size;
+ ((MVMArray *)buf)->body.elems = output_size / elem_size;
+ }
+
return buf;
}
MVMObject * MVM_string_encode_to_buf(MVMThreadContext *tc, MVMString *s, MVMString *enc_name,
diff --git a/src/vm/moar/QAST/QASTCompilerMAST.nqp b/src/vm/moar/QAST/QASTCompilerMAST.nqp
index 712289781..0950d8491 100644
--- a/src/vm/moar/QAST/QASTCompilerMAST.nqp
+++ b/src/vm/moar/QAST/QASTCompilerMAST.nqp
@@ -2302,13 +2302,14 @@ class MoarVM::StringHeap {
$utf8 := 1 if $g < 0 || $g >= 0xff || $g == 0x0d;
}
- my $encoded := nqp::encode($s, ($utf8 ?? "utf8" !! "iso-8859-1"), nqp::create(MAST::Bytecode));
- my int $encoded_size := nqp::elems($encoded);
+ my int $prev_total_size := nqp::elems($!strings);
+ nqp::setelems($!strings, $prev_total_size + 4);
+ nqp::encode($s, ($utf8 ?? "utf8" !! "iso-8859-1"), $!strings);
+ my int $encoded_size := nqp::elems($!strings) - 4 - $prev_total_size;
my int $pad := 4 - $encoded_size % 4;
$pad := 0 if $pad == 4;
- $!strings.write_uint32($encoded_size * 2 + $utf8); # LSB is UTF-8 flag
- $!strings.write_buf($encoded);
+ $!strings.write_uint32_at($encoded_size * 2 + $utf8, $prev_total_size); # LSB is UTF-8 flag
$!strings.write_uint8(0) while $pad--;
nqp::push_s(@!orig-strings, $s);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment