Skip to content

Commit

Permalink
Merge pull request #744 from bloomberg/fix_bytes_blit_corner
Browse files Browse the repository at this point in the history
bug fix for #743
  • Loading branch information
bobzhang committed Sep 9, 2016
2 parents 1f46d14 + 6a87e0c commit 469c365
Show file tree
Hide file tree
Showing 8 changed files with 213 additions and 25 deletions.
22 changes: 21 additions & 1 deletion docs/Manual.html
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,7 @@ <h1><a href="https://github.com/bloomberg/bucklescript">BuckleScript</a> User Ma
</ul>
</li>
<li><a href="#_changes">Appendix A: CHANGES</a></li>
<li><a href="#_1_02_dev">1.02 (dev)</a></li>
<li><a href="#_1_01">1.01</a></li>
<li><a href="#_1_0">1.0</a></li>
</ul>
Expand Down Expand Up @@ -4096,6 +4097,25 @@ <h3 id="_difference_from_a_href_https_github_com_ocsigen_js_of_ocaml_js_of_ocaml
<h2 id="_changes"><a class="anchor" href="#_changes"></a>Appendix A: CHANGES</h2>
<div class="sectionbody">

</div>
</div>
<div class="sect1">
<h2 id="_1_02_dev"><a class="anchor" href="#_1_02_dev"></a>1.02 (dev)</h2>
<div class="sectionbody">
<div class="ulist">
<ul>
<li>
<p>Bug fixes and enhancement</p>
<div class="ulist">
<ul>
<li>
<p>Fix Bytes.blit when src==dst <a href="https://github.com/bloomberg/bucklescript/issues743">#743</a></p>
</li>
</ul>
</div>
</li>
</ul>
</div>
</div>
</div>
<div class="sect1">
Expand All @@ -4119,7 +4139,7 @@ <h2 id="_1_01"><a class="anchor" href="#_1_01"></a>1.01</h2>
</div>
</li>
<li>
<p>Bug fixes and enhancment</p>
<p>Bug fixes and enhancement</p>
<div class="ulist">
<ul>
<li>
Expand Down
44 changes: 32 additions & 12 deletions jscomp/runtime/caml_string.ml
Original file line number Diff line number Diff line change
Expand Up @@ -87,25 +87,45 @@ let caml_blit_string s1 i1 s2 i2 (len : int ) =
done
end

(** Same as {!Array.prototype.copyWithin} *)
let copyWithin s1 i1 i2 len =
if i1 < i2 then (* nop for i1 = i2 *)
let range_a = length s1 - i2 - 1 in
let range_b = len - 1 in
let range = if range_a > range_b then range_b else range_a in
for j = range downto 0 do
unsafe_set s1 (i2 + j) (unsafe_get s1 (i1 + j))
done
else if i1 > i2 then
let range_a = length s1 - i1 - 1 in
let range_b = len - 1 in
let range = if range_a > range_b then range_b else range_a in
for k = 0 to range do
unsafe_set s1 (i2 + k) (unsafe_get s1 (i1 + k))
done

(* TODO: when the compiler could optimize small function calls,
use high order functions instead
*)
let caml_blit_bytes s1 i1 s2 i2 len =
if len > 0 then
let off1 = length s1 - i1 in
if len <= off1 then
for i = 0 to len - 1 do
unsafe_set s2 (i2 + i) (unsafe_get s1 (i1 + i))
done
else
begin
for i = 0 to off1 - 1 do
if s1 == s2 then
copyWithin s1 i1 i2 len
else
let off1 = length s1 - i1 in
if len <= off1 then
for i = 0 to len - 1 do
unsafe_set s2 (i2 + i) (unsafe_get s1 (i1 + i))
done;
for i = off1 to len - 1 do
unsafe_set s2 (i2 + i) '\000'
done
end
else
begin
for i = 0 to off1 - 1 do
unsafe_set s2 (i2 + i) (unsafe_get s1 (i1 + i))
done;
for i = off1 to len - 1 do
unsafe_set s2 (i2 + i) '\000'
done
end

(** checkout [Bytes.empty] -- to be inlined? *)
let bytes_of_string s =
Expand Down
4 changes: 4 additions & 0 deletions jscomp/test/.depend
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ buffer_test.cmj : ../stdlib/string.cmi mt.cmi ../stdlib/bytes.cmi \
../stdlib/buffer.cmi
buffer_test.cmx : ../stdlib/string.cmx mt.cmx ../stdlib/bytes.cmx \
../stdlib/buffer.cmx
bytes_split_gpr_743_test.cmj : mt.cmi ../runtime/js.cmj ../stdlib/bytes.cmi
bytes_split_gpr_743_test.cmx : mt.cmx ../runtime/js.cmx ../stdlib/bytes.cmx
caml_compare_test.cmj : mt.cmi
caml_compare_test.cmx : mt.cmx
caml_format_test.cmj : ../stdlib/printf.cmi mt.cmi ../stdlib/int64.cmi \
Expand Down Expand Up @@ -926,6 +928,8 @@ buffer_test.cmo : ../stdlib/string.cmi mt.cmi ../stdlib/bytes.cmi \
../stdlib/buffer.cmi
buffer_test.cmj : ../stdlib/string.cmj mt.cmj ../stdlib/bytes.cmj \
../stdlib/buffer.cmj
bytes_split_gpr_743_test.cmo : mt.cmi ../runtime/js.cmo ../stdlib/bytes.cmi
bytes_split_gpr_743_test.cmj : mt.cmj ../runtime/js.cmj ../stdlib/bytes.cmj
caml_compare_test.cmo : mt.cmi
caml_compare_test.cmj : mt.cmj
caml_format_test.cmo : ../stdlib/printf.cmi mt.cmi ../stdlib/int64.cmi \
Expand Down
3 changes: 2 additions & 1 deletion jscomp/test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ OTHERS := literals a test_ari test_export2 test_internalOO test_obj_simple_ffi t
watch_test bs_node_string_buffer_test \
bs_ignore_test bs_ignore_effect \
ppx_this_obj_field single_module_alias \
CCSexpM CCSexpM_test CCSexp js_json_test array_subtle_test
CCSexpM CCSexpM_test CCSexp js_json_test array_subtle_test bytes_split_gpr_743_test




Expand Down
74 changes: 74 additions & 0 deletions jscomp/test/bytes_split_gpr_743_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
'use strict';

var Bytes = require("../../lib/js/bytes");
var Mt = require("./mt");
var Block = require("../../lib/js/block");
var Caml_string = require("../../lib/js/caml_string");

var suites = [/* [] */0];

var test_id = [0];

function eq(loc, param) {
var y = param[1];
var x = param[0];
test_id[0] = test_id[0] + 1 | 0;
suites[0] = /* :: */[
/* tuple */[
loc + (" id " + test_id[0]),
function () {
return /* Eq */Block.__(0, [
x,
y
]);
}
],
suites[0]
];
return /* () */0;
}

var b = new Array(3);

b[0] = /* "a" */97;

b[1] = /* "b" */98;

b[2] = /* "c" */99;

Bytes.blit(b, 0, b, 1, 2);

var res = Caml_string.bytes_to_string(b);

console.log(res);

eq('File "bytes_split_gpr_743_test.ml", line 17, characters 5-12', /* tuple */[
"aab",
res
]);

var b$1 = new Array(3);

b$1[0] = /* "a" */97;

b$1[1] = /* "b" */98;

b$1[2] = /* "c" */99;

Bytes.blit(b$1, 1, b$1, 0, 2);

var res2 = Caml_string.bytes_to_string(b$1);

console.log(res2);

eq('File "bytes_split_gpr_743_test.ml", line 32, characters 5-12', /* tuple */[
"bcc",
res2
]);

Mt.from_pair_suites("bytes_split_gpr_743_test.ml", suites[0]);

exports.suites = suites;
exports.test_id = test_id;
exports.eq = eq;
/* Not a pure module */
35 changes: 35 additions & 0 deletions jscomp/test/bytes_split_gpr_743_test.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
let suites : Mt.pair_suites ref = ref []
let test_id = ref 0
let eq loc (x, y) =
incr test_id ;
suites :=
(loc ^" id " ^ (string_of_int !test_id), (fun _ -> Mt.Eq(x,y))) :: !suites


let () =
let b = Bytes.create 3 in
Bytes.set b 0 'a';
Bytes.set b 1 'b';
Bytes.set b 2 'c';
Bytes.blit b 0 b 1 2;
let res = (Bytes.unsafe_to_string b) in
Js.log res; (* "aab"*)
eq __LOC__ ("aab", res)
(* assert('a' = Bytes.get b 0); *)
(* assert('a' = Bytes.get b 1); *)
(* assert('b' = Bytes.get b 2); *)
(* () *)


let () =
let b = Bytes.create 3 in
Bytes.set b 0 'a';
Bytes.set b 1 'b';
Bytes.set b 2 'c';
Bytes.blit b 1 b 0 2;
let res2 = (Bytes.unsafe_to_string b) in
Js.log res2 (* "bcc"*);
eq __LOC__ ("bcc", res2)


let () = Mt.from_pair_suites __FILE__ !suites
49 changes: 39 additions & 10 deletions lib/js/caml_string.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,21 +80,50 @@ function caml_blit_string(s1, i1, s2, i2, len) {

function caml_blit_bytes(s1, i1, s2, i2, len) {
if (len > 0) {
var off1 = s1.length - i1 | 0;
if (len <= off1) {
for(var i = 0 ,i_finish = len - 1 | 0; i <= i_finish; ++i){
s2[i2 + i | 0] = s1[i1 + i | 0];
if (s1 === s2) {
var s1$1 = s1;
var i1$1 = i1;
var i2$1 = i2;
var len$1 = len;
if (i1$1 < i2$1) {
var range_a = (s1$1.length - i2$1 | 0) - 1 | 0;
var range_b = len$1 - 1 | 0;
var range = range_a > range_b ? range_b : range_a;
for(var j = range; j >= 0; --j){
s1$1[i2$1 + j | 0] = s1$1[i1$1 + j | 0];
}
return /* () */0;
}
else if (i1$1 > i2$1) {
var range_a$1 = (s1$1.length - i1$1 | 0) - 1 | 0;
var range_b$1 = len$1 - 1 | 0;
var range$1 = range_a$1 > range_b$1 ? range_b$1 : range_a$1;
for(var k = 0; k <= range$1; ++k){
s1$1[i2$1 + k | 0] = s1$1[i1$1 + k | 0];
}
return /* () */0;
}
else {
return 0;
}
return /* () */0;
}
else {
for(var i$1 = 0 ,i_finish$1 = off1 - 1 | 0; i$1 <= i_finish$1; ++i$1){
s2[i2 + i$1 | 0] = s1[i1 + i$1 | 0];
var off1 = s1.length - i1 | 0;
if (len <= off1) {
for(var i = 0 ,i_finish = len - 1 | 0; i <= i_finish; ++i){
s2[i2 + i | 0] = s1[i1 + i | 0];
}
return /* () */0;
}
for(var i$2 = off1 ,i_finish$2 = len - 1 | 0; i$2 <= i_finish$2; ++i$2){
s2[i2 + i$2 | 0] = /* "\000" */0;
else {
for(var i$1 = 0 ,i_finish$1 = off1 - 1 | 0; i$1 <= i_finish$1; ++i$1){
s2[i2 + i$1 | 0] = s1[i1 + i$1 | 0];
}
for(var i$2 = off1 ,i_finish$2 = len - 1 | 0; i$2 <= i_finish$2; ++i$2){
s2[i2 + i$2 | 0] = /* "\000" */0;
}
return /* () */0;
}
return /* () */0;
}
}
else {
Expand Down
7 changes: 6 additions & 1 deletion site/docsource/CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
## CHANGES
:issues: https://github.com/bloomberg/bucklescript/issues

== 1.02 (dev)

* Bug fixes and enhancement

- Fix Bytes.blit when src==dst {issues}743[#743]

== 1.01

Expand All @@ -12,7 +17,7 @@
- Introduce phantom arguments (`bs.ignore`) for ad-hoc
polymorphism {issues}/686[#686]

* Bug fixes and enhancment
* Bug fixes and enhancement

- Enforce `#=` always return unit {issues}718[#718] for better error messages

Expand Down

0 comments on commit 469c365

Please sign in to comment.