Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Revert "Revert "Library: change Js.wrap_meth_callback to allow method…
…s with no parameter.""

This reverts commit 865587d.
  • Loading branch information
hhugo committed Sep 30, 2014
1 parent 865587d commit ca9ad2b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
4 changes: 2 additions & 2 deletions lib/dom.ml
Expand Up @@ -225,7 +225,7 @@ let window_event () : 'a #event t = Js.Unsafe.variable "event"
(* The function preventDefault must be called explicitely when
using addEventListener... *)
let handler f =
Js.some (Js.wrap_callback
Js.some (Js.Unsafe.callback
(fun e ->
(* depending on the internet explorer version, e can be null or undefined. *)
if not (Js.Opt.test (some e))
Expand All @@ -241,7 +241,7 @@ let handler f =
(Js.Unsafe.coerce e)##preventDefault ();
res))
let full_handler f =
Js.some (Js.wrap_meth_callback
Js.some (Js.Unsafe.meth_callback
(fun this e ->
(* depending on the internet explorer version, e can be null or undefined *)
if not (Js.Opt.test (some e))
Expand Down
8 changes: 6 additions & 2 deletions lib/js.ml
Expand Up @@ -20,6 +20,8 @@

type +'a t

type (-'a, +'b) meth_callback

module Unsafe = struct
external variable : string -> 'a = "caml_js_var"

Expand Down Expand Up @@ -48,6 +50,9 @@ module Unsafe = struct
external pure_js_expr : string -> 'a = "caml_pure_js_expr"

let global = variable "joo_global_object"

external callback : ('a -> 'b) -> ('c, 'a -> 'b) meth_callback = "%identity"
external meth_callback : ('b -> 'a) -> ('b, 'a) meth_callback = "caml_js_wrap_meth_callback_unsafe"
end

(****)
Expand Down Expand Up @@ -123,11 +128,10 @@ type +'a constr

(****)

type (-'a, +'b) meth_callback
type 'a callback = (unit, 'a) meth_callback

external wrap_callback : ('a -> 'b) -> ('c, 'a -> 'b) meth_callback = "caml_js_wrap_callback"
external wrap_meth_callback : ('a -> 'b -> 'c) -> ('a, 'b -> 'c) meth_callback = "caml_js_wrap_meth_callback"
external wrap_meth_callback : ('a -> 'b) -> ('a, 'b) meth_callback = "caml_js_wrap_meth_callback"

(****)

Expand Down
18 changes: 17 additions & 1 deletion lib/js.mli
Expand Up @@ -141,7 +141,7 @@ external wrap_callback : ('a -> 'b) -> ('c, 'a -> 'b) meth_callback =
(** Wrap an OCaml function so that it can be invoked from
Javascript. *)
external wrap_meth_callback :
('c -> 'a -> 'b) -> ('c, 'a -> 'b) meth_callback =
('b -> 'a) -> ('b, 'a) meth_callback =
"caml_js_wrap_meth_callback"
(** Wrap an OCaml function so that it can be invoked from
Javascript. The first parameter of the function will be bound
Expand Down Expand Up @@ -607,6 +607,22 @@ module Unsafe : sig
val global : < .. > t
(** Javascript global object *)

external callback : ('a -> 'b) -> ('c, 'a -> 'b) meth_callback = "%identity"
(** Wrap an OCaml function so that it can be invoked from
Javascript. Contrary to [Js.wrap_callback], partial
application and over-application are not supported: missing
arguments will be set to [undefined] and extra arguments are
lost. *)

external meth_callback : ('b -> 'a) -> ('b, 'a) meth_callback =
"caml_js_wrap_meth_callback_unsafe"
(** Wrap an OCaml function so that it can be invoked from
Javascript. The first parameter of the function will be bound
to the value of the [this] implicit parameter. Contrary to
[Js.wrap_meth_callback], partial application and
over-application is not supported: missing arguments will be
set to [undefined] and extra arguments are lost. *)

(*FIX also, array literals *)
end

Expand Down
11 changes: 6 additions & 5 deletions runtime/jslib_js_of_ocaml.js
Expand Up @@ -93,13 +93,14 @@ function caml_js_wrap_callback(f) {
//Requires: caml_call_gen,raw_array_cons
function caml_js_wrap_meth_callback(f) {
return function () {
if (arguments.length > 0) {
return caml_call_gen(f,raw_array_cons(arguments,this));
} else {
return caml_call_gen(f,[this,undefined]);
}
return caml_call_gen(f,raw_array_cons(arguments,this));
}
}
//Provides: caml_js_wrap_meth_callback_unsafe const
//Requires: caml_call_gen,raw_array_cons
function caml_js_wrap_meth_callback_unsafe(f) {
return function () { f.apply(null, raw_array_cons(arguments,this)); }
}
//Provides: caml_js_equals mutable
function caml_js_equals (x, y) { return +(x == y); }
//Provides: caml_js_to_byte_string const
Expand Down

0 comments on commit ca9ad2b

Please sign in to comment.