Skip to content

improve exception creation #277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 20, 2016
Merged
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
2 changes: 1 addition & 1 deletion jscomp/js_config.ml
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ let runtime_set = String_set.of_list [
let prim = "Caml_primitive"

let builtin_exceptions = "Caml_builtin_exceptions"

let exceptions = "Caml_exceptions"

let io = "Caml_io"

Expand Down
2 changes: 1 addition & 1 deletion jscomp/js_config.mli
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ val stdlib_set : String_set.t
val prim : string

val builtin_exceptions : string

val exceptions : string
val io : string

val oo : string
Expand Down
23 changes: 9 additions & 14 deletions jscomp/js_of_lam_exception.ml
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,15 @@ let match_exception_def (args : J.expression list) =
None
| _ -> None

(* Sync up with [caml_set_oo_id] *)
(* Sync up with [caml_set_oo_id]
Note if we inline {!Caml_exceptions.create},
it seems can be useful for optimizations in theory,
in practice, it never happen, since the pattern match
never dig into it internally, so maybe {!Obj.set_tag}
is not necessary at all
*)
let make_exception exception_str mutable_flag : J.expression =
{ expression_desc =
Caml_block (
[exception_str ;
E.runtime_call Js_config.builtin_exceptions "get_id" []
],
mutable_flag,
(* TODO: combined with `_001` optimization *)
E.obj_int_tag_literal (* (Obj.object_tag) *),
Blk_na
);
comment = None
}
E.runtime_call Js_config.exceptions Literals.create [exception_str]



Expand All @@ -70,5 +65,5 @@ let caml_set_oo_id args =
If we can guarantee this code path is never hit, we can do
a better job for encoding of exception and extension?
*)
E.runtime_call Js_config.builtin_exceptions "caml_set_oo_id" args
E.runtime_call Js_config.exceptions "caml_set_oo_id" args
end
2 changes: 2 additions & 0 deletions jscomp/literals.ml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ let prim = "prim"
let param = "param"
let partial_arg = "partial_arg"
let tmp = "tmp"

let create = "create" (* {!Caml_exceptions.create}*)
2 changes: 2 additions & 0 deletions jscomp/literals.mli
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ val prim : string

(**temporary varaible used in {!Js_ast_util} *)
val tmp : string

val create : string
5 changes: 5 additions & 0 deletions jscomp/runtime/.depend
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
caml_array.cmi :
caml_bigarray.cmi :
caml_builtin_exceptions.cmi :
caml_exceptions.cmi : caml_builtin_exceptions.cmi
caml_float.cmi :
caml_format.cmi :
caml_hash.cmi :
Expand All @@ -23,6 +24,8 @@ caml_builtin_exceptions.cmo : caml_builtin_exceptions.cmi
caml_builtin_exceptions.cmx : caml_builtin_exceptions.cmi
caml_curry.cmo : js.cmo caml_oo.cmi
caml_curry.cmx : js.cmx caml_oo.cmx
caml_exceptions.cmo : caml_builtin_exceptions.cmi caml_exceptions.cmi
caml_exceptions.cmx : caml_builtin_exceptions.cmx caml_exceptions.cmi
caml_float.cmo : typed_array.cmo js.cmo caml_float.cmi
caml_float.cmx : typed_array.cmx js.cmx caml_float.cmi
caml_format.cmo : js.cmo caml_utils.cmi caml_format.cmi
Expand Down Expand Up @@ -69,6 +72,8 @@ caml_builtin_exceptions.cmo : caml_builtin_exceptions.cmi
caml_builtin_exceptions.cmj : caml_builtin_exceptions.cmi
caml_curry.cmo : js.cmo caml_oo.cmi
caml_curry.cmj : js.cmj caml_oo.cmj
caml_exceptions.cmo : caml_builtin_exceptions.cmi caml_exceptions.cmi
caml_exceptions.cmj : caml_builtin_exceptions.cmj caml_exceptions.cmi
caml_float.cmo : typed_array.cmo js.cmo caml_float.cmi
caml_float.cmj : typed_array.cmj js.cmj caml_float.cmi
caml_format.cmo : js.cmo caml_utils.cmi caml_format.cmi
Expand Down
27 changes: 0 additions & 27 deletions jscomp/runtime/caml_builtin_exceptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,87 +2,62 @@
'use strict';


var id = [0];

function caml_set_oo_id(b) {
b[1] = id[0];
id[0] += 1;
return b;
}

function get_id() {
id[0] += 1;
return id[0];
}

var out_of_memory = /* tuple */[
248,
"Out_of_memory",
0
];

var sys_error = /* tuple */[
248,
"Sys_error",
-1
];

var failure = /* tuple */[
248,
"Failure",
-2
];

var invalid_argument = /* tuple */[
248,
"Invalid_argument",
-3
];

var end_of_file = /* tuple */[
248,
"End_of_file",
-4
];

var division_by_zero = /* tuple */[
248,
"Division_by_zero",
-5
];

var not_found = /* tuple */[
248,
"Not_found",
-6
];

var match_failure = /* tuple */[
248,
"Match_failure",
-7
];

var stack_overflow = /* tuple */[
248,
"Stack_overflow",
-8
];

var sys_blocked_io = /* tuple */[
248,
"Sys_blocked_io",
-9
];

var assert_failure = /* tuple */[
248,
"Assert_failure",
-10
];

var undefined_recursive_module = /* tuple */[
248,
"Undefined_recursive_module",
-11
];
Expand All @@ -99,6 +74,4 @@ exports.stack_overflow = stack_overflow;
exports.sys_blocked_io = sys_blocked_io;
exports.assert_failure = assert_failure;
exports.undefined_recursive_module = undefined_recursive_module;
exports.caml_set_oo_id = caml_set_oo_id;
exports.get_id = get_id;
/* No side effect */
60 changes: 20 additions & 40 deletions jscomp/runtime/caml_builtin_exceptions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -18,43 +18,23 @@

(* Author: Hongbo Zhang *)

type exception_block = int * string * int
let out_of_memory = 248, "Out_of_memory", 0
let sys_error = 248, "Sys_error", -1
let failure = 248, "Failure", -2
let invalid_argument = 248, "Invalid_argument", -3
let end_of_file = 248, "End_of_file",-4
let division_by_zero = 248, "Division_by_zero", -5
let not_found = 248, "Not_found", -6
let match_failure = 248, "Match_failure", -7
let stack_overflow = 248, "Stack_overflow",-8
let sys_blocked_io = 248, "Sys_blocked_io", -9
let assert_failure = 248, "Assert_failure", -10
let undefined_recursive_module = 248, "Undefined_recursive_module", -11



(* Exported for better inlining *)
(* It's common that we have <code> a = caml_set_oo_id([248,"string",0])</code> *)
(* This can be inlined as <code> a = caml_set_oo_id([248,"tag", caml_oo_last_id++])</code> *)
(* @type {number} *)

let id = ref 0n


(* see #251
{[
CAMLprim value caml_set_oo_id (value obj) {
Field(obj, 1) = oo_last_id;
oo_last_id += 2;
return obj;
}

]}*)
let caml_set_oo_id (b : exception_block) =
Obj.set_field (Obj.repr b) 1 (Obj.repr !id);
id := Nativeint.add !id 1n;
b

let get_id () =
id := Nativeint.add !id 1n; !id
type exception_block = string * nativeint


let out_of_memory = "Out_of_memory", 0n
let sys_error = "Sys_error", -1n
let failure = "Failure", -2n
let invalid_argument = "Invalid_argument", -3n
let end_of_file = "End_of_file",-4n
let division_by_zero = "Division_by_zero", -5n
let not_found = "Not_found", -6n
let match_failure = "Match_failure", -7n
let stack_overflow = "Stack_overflow",-8n
let sys_blocked_io = "Sys_blocked_io", -9n
let assert_failure = "Assert_failure", -10n
let undefined_recursive_module = "Undefined_recursive_module", -11n

(* TODO:
1. is it necessary to tag [248] here
2. is it okay to remove the negative value
*)
5 changes: 2 additions & 3 deletions jscomp/runtime/caml_builtin_exceptions.mli
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type exception_block = int * string * int
type exception_block = string * nativeint
val out_of_memory : exception_block
val sys_error : exception_block
val failure : exception_block
Expand All @@ -12,6 +12,5 @@ val sys_blocked_io : exception_block
val assert_failure : exception_block
val undefined_recursive_module : exception_block

val caml_set_oo_id : exception_block -> exception_block
val get_id : unit -> nativeint


63 changes: 19 additions & 44 deletions jscomp/runtime/caml_exceptions.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,31 @@
// Generated CODE, PLEASE EDIT WITH CARE
'use strict';

var Caml_builtin_exceptions = require("./caml_builtin_exceptions");

function caml_raise_sys_error(msg) {
throw [
Caml_builtin_exceptions.sys_error,
msg
];
}

function caml_failwith(s) {
throw [
Caml_builtin_exceptions.failure,
s
];
}

function caml_invalid_argument(s) {
throw [
Caml_builtin_exceptions.invalid_argument,
s
];
}

function caml_array_bound_error() {
throw [
Caml_builtin_exceptions.invalid_argument,
"index out of bounds"
];
}
var id = [0];

function caml_raise_zero_divide() {
throw Caml_builtin_exceptions.division_by_zero;
function caml_set_oo_id(b) {
b[1] = id[0];
id[0] += 1;
return b;
}

function caml_raise_not_found() {
throw Caml_builtin_exceptions.not_found;
function get_id() {
id[0] += 1;
return id[0];
}

function caml_undef_module(loc) {
throw [
Caml_builtin_exceptions.undefined_recursive_module,
loc
];
function create(str) {
var v_001 = get_id(/* () */0);
var v = /* tuple */[
str,
v_001
];
v.tag = 248;
return v;
}

exports.caml_raise_sys_error = caml_raise_sys_error;
exports.caml_failwith = caml_failwith;
exports.caml_invalid_argument = caml_invalid_argument;
exports.caml_array_bound_error = caml_array_bound_error;
exports.caml_raise_zero_divide = caml_raise_zero_divide;
exports.caml_raise_not_found = caml_raise_not_found;
exports.caml_undef_module = caml_undef_module;
exports.caml_set_oo_id = caml_set_oo_id;
exports.get_id = get_id;
exports.create = create;
/* No side effect */
52 changes: 52 additions & 0 deletions jscomp/runtime/caml_exceptions.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
(* BuckleScript compiler
* Copyright (C) 2015-2016 Bloomberg Finance L.P.
*
* 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.
*)

(* Author: Hongbo Zhang *)

(**
Could be exported for better inlining
It's common that we have
{[ a = caml_set_oo_id([248,"string",0]) ]}
This can be inlined as
{[ a = caml_set_oo_id([248,"tag", caml_oo_last_id++]) ]}
*)

let id = ref 0n


(* see #251
{[
CAMLprim value caml_set_oo_id (value obj) {
Field(obj, 1) = oo_last_id;
oo_last_id += 2;
return obj;
}

]}*)
let caml_set_oo_id (b : Caml_builtin_exceptions.exception_block) =
Obj.set_field (Obj.repr b) 1 (Obj.repr !id);
id := Nativeint.add !id 1n;
b

let get_id () =
id := Nativeint.add !id 1n; !id

let create (str : string) : Caml_builtin_exceptions.exception_block =
let v = ( str, get_id ()) in
Obj.set_tag (Obj.repr v) 248 (* Obj.object_tag*);
v
Loading