Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ let runtime =
; backtrace
; bigarray
; bigstring
; block_index
; compare
; fail
; format
Expand Down
2 changes: 2 additions & 0 deletions compiler/lib-runtime-files/tests/all.ml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ let%expect_test _ =
+bigarray.js
+bigstring.js
+blake2.js
+block_index.js
+compare.js
+domain.js
+dynlink.js
Expand Down Expand Up @@ -63,6 +64,7 @@ let%expect_test _ =
+bigarray.js
+bigstring.js
+blake2.js
+block_index.js
+compare.js
+domain.js
+effect.js
Expand Down
5 changes: 5 additions & 0 deletions compiler/lib/linker.ml
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,16 @@ module Fragment = struct
; deprecated : string option
}

let oxcaml = true [@@if oxcaml]

let oxcaml = false [@@if not oxcaml]

let allowed_flags =
List.fold_left
~f:(fun m (k, v) -> StringMap.add k v m)
~init:StringMap.empty
[ "js-string", Config.Flag.use_js_string
; ("oxcaml", fun () -> oxcaml)
; ( "effects"
, fun () ->
match Config.effects () with
Expand Down
8 changes: 7 additions & 1 deletion compiler/tests-check-prim/main.5.2+ox.output
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ caml_bytes_setf32_indexed_by_int64
caml_bytes_setf32_indexed_by_int8
caml_bytes_setf32_indexed_by_nativeint
caml_continuation_use
caml_deepen_idx_bytecode
caml_drop_continuation
caml_dynamic_get
caml_dynamic_make
Expand Down Expand Up @@ -259,6 +258,13 @@ caml_bigstring_blit_string_to_ba
caml_bigstring_memcmp
caml_hash_mix_bigstring

From +block_index.js:
caml_get_idx_bytecode
caml_set_idx_bytecode

From +domain.js:
caml_max_domain_count

From +effect.js:
caml_continuation_use_and_update_handler_noexc
jsoo_effect_not_supported
Expand Down
8 changes: 7 additions & 1 deletion compiler/tests-check-prim/unix-Unix.5.2+ox.output
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ caml_bytes_setf32_indexed_by_int64
caml_bytes_setf32_indexed_by_int8
caml_bytes_setf32_indexed_by_nativeint
caml_continuation_use
caml_deepen_idx_bytecode
caml_drop_continuation
caml_dynamic_get
caml_dynamic_make
Expand Down Expand Up @@ -335,6 +334,13 @@ caml_bigstring_blit_string_to_ba
caml_bigstring_memcmp
caml_hash_mix_bigstring

From +block_index.js:
caml_get_idx_bytecode
caml_set_idx_bytecode

From +domain.js:
caml_max_domain_count

From +effect.js:
caml_continuation_use_and_update_handler_noexc
jsoo_effect_not_supported
Expand Down
2 changes: 1 addition & 1 deletion runtime/js/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ function caml_array_create_float(len) {
//Provides: caml_array_create_float const (const)
//Requires: caml_array_bound_error
//Version: >= 5.2, < 5.3
//OxCaml
//If: oxcaml
function caml_array_create_float(len) {
if (len >>> 0 >= ((0x7fffffff / 8) | 0)) caml_array_bound_error();
var len = (len + 1) | 0;
Expand Down
88 changes: 88 additions & 0 deletions runtime/js/block_index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Js_of_ocaml runtime support
// http://www.ocsigen.org/js_of_ocaml/
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, with linking exception;
// either version 2.1 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

///////////// Block indices
// In bytecode, JSOO, and WASM, valid block indices are represented as tag-0
// blocks containing the sequence of field positions.
//
// On JSOO, blocks are arrays whose [0] element is the tag and whose subsequent
// elements are the fields.
//
// For more information on block indices and their bytecode representation, see
// https://github.com/oxcaml/oxcaml/blob/main/jane/doc/extensions/_03-unboxed-types/03-block-indices.md#representation-of-block-indices
//
// We additionally adopt the convention in JSOO that some known-invalid indices
// are represented as a tag-1 block whose only field is the number of times the
// pointer has been advanced. Reads/writes from/to such an index raise.
//
// Our motivation for permitting a representation of these invalid indices is
// that pointers should generally be safe to create and manipulate, and the
// only points of unsafety should be reads and writes.

//Provides: caml_get_idx_bytecode mutable (mutable, const)
//Requires: caml_invalid_argument
//Version: >= 5.2
//If: oxcaml
function caml_get_idx_bytecode(base, idx) {
if (idx[0] !== 0) {
caml_invalid_argument(
"caml_get_idx_bytecode: attempted to read from an invalid index",
);
}
var depth = idx.length - 1;
var res = base;
for (var i = 1; i <= depth; i++) {
res = res[idx[i] + 1];
}
return res;
}

//Provides: caml_set_idx_bytecode (mutable, const, mutable)
//Requires: caml_invalid_argument
//Version: >= 5.2
//If: oxcaml
function caml_set_idx_bytecode(base, idx, v) {
if (idx[0] !== 0) {
caml_invalid_argument(
"caml_set_idx_bytecode: attempted to write to an invalid index",
);
}
var depth = idx.length - 1;
var dst = base;
for (var i = 1; i < depth; i++) {
dst = dst[idx[i] + 1];
}
dst[idx[depth] + 1] = v;
return 0;
}

//Provides: caml_deepen_idx_bytecode const (const, const)
//Version: >= 5.2
//If: oxcaml
function caml_deepen_idx_bytecode(idx_prefix, idx_suffix) {
var prefix_depth = idx_prefix.length - 1;
var suffix_depth = idx_suffix.length - 1;
var block = new Array(1 + prefix_depth + suffix_depth);
block[0] = 0;
for (var i = 0; i < prefix_depth; i++) {
block[1 + i] = idx_prefix[1 + i];
}
for (var i = 0; i < suffix_depth; i++) {
block[1 + prefix_depth + i] = idx_suffix[1 + i];
}
return block;
}
Loading
Loading