Skip to content
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

Add melange version of Promise (promise.js) #80

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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 dune
Original file line number Diff line number Diff line change
@@ -1 +1 @@
(ignored_subdirs (doc esy.lock lib node_modules))
(dirs src test)
3 changes: 2 additions & 1 deletion dune-project
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
(lang dune 1.0)
(lang dune 3.8)
(using melange 0.1)
1 change: 1 addition & 0 deletions promise.opam
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ depends: [
"ocaml"
"reason" {build & >= "3.3.2"}
"result"
"melange"
]

build: [
Expand Down
10 changes: 10 additions & 0 deletions src/js/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
(library
(name promise_js)
(public_name promise.melange)
(modes melange)
(wrapped false)
(libraries melange.belt)
(preprocess
(pps melange.ppx))
(flags
(:standard -w +A-102-20)))
16 changes: 4 additions & 12 deletions src/js/promise.re
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ let onUnhandledException = ref(exn => {



[%%bs.raw {|
[%%mel.raw {|
function PromiseBox(p) {
this.nested = p;
};
Expand Down Expand Up @@ -90,7 +90,6 @@ module Js_ = {

external relax: promise('a) => rejectable('a, _) = "%identity";

[@bs.val]
external jsNew:
(('a => unit) => ('e => unit) => unit) => rejectable('a, 'e) = "make";

Expand All @@ -105,10 +104,8 @@ module Js_ = {
(p, resolve^, reject^);
};

[@bs.val]
external resolved: 'a => rejectable('a, _) = "resolved";

[@bs.val]
external flatMap:
(rejectable('a, 'e), 'a => rejectable('b, 'e)) => rejectable('b, 'e) =
"then";
Expand All @@ -125,20 +122,16 @@ module Js_ = {
v
});

[@bs.scope "Promise"]
[@bs.val]
[@mel.scope "Promise"]
external rejected: 'e => rejectable(_, 'e) = "reject";

[@bs.val]
external catch:
(rejectable('a, 'e), 'e => rejectable('a, 'e2)) => rejectable('a, 'e2) =
"catch_";

[@bs.val]
external unbox: 'a => 'a = "unbox";

[@bs.scope "Promise"]
[@bs.val]
[@mel.scope "Promise"]
external jsAll: 'a => 'b = "all";

let allArray = promises =>
Expand All @@ -162,8 +155,7 @@ module Js_ = {
let all6 = (p1, p2, p3, p4, p5, p6) =>
jsAll((p1, p2, p3, p4, p5, p6));

[@bs.scope "Promise"]
[@bs.val]
[@mel.scope "Promise"]
external jsRace: array(rejectable('a, 'e)) => rejectable('a, 'e) = "race";

let race = promises =>
Expand Down
4 changes: 4 additions & 0 deletions test/bundle/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(library
(name bundle)
(modes melange)
(libraries melange.belt promise.melange))
3 changes: 0 additions & 3 deletions test/dune

This file was deleted.

11 changes: 11 additions & 0 deletions test/ffi/js/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(library
(name test_ffi_js)
(modes melange)
(libraries framework_js)
(wrapped false)
(preprocess
(pps melange.ppx)))

(copy_files
(mode fallback)
(files ../native/*.re))
13 changes: 4 additions & 9 deletions test/js/test_ffi.re → test/ffi/js/test_ffi.re
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@



[%%bs.raw {|
[%%mel.raw {|
function isPromise (p) {
return (p instanceof Promise);
}
Expand All @@ -14,13 +14,8 @@ function isPromiseLike(v) {
};
|}];

[@bs.val]
external isPromise: Promise.Js.t(_, _) => bool = "isPromise";

[@bs.val]
external jsPromiseIsPromise: Js.Promise.t(_) => bool = "isPromise";

[@bs.val]
external jsPromiseIsPromiseLike: Js.Promise.t(_) => bool = "isPromiseLike";


Expand Down Expand Up @@ -70,15 +65,15 @@ let interopTests = Framework.suite("interop", [
}),

test("js promise is reason-promise", () => {
let js_promise: Promise.t(int) = [%bs.raw {|Promise.resolve(1)|}];
let js_promise: Promise.t(int) = [%mel.raw {|Promise.resolve(1)|}];
js_promise
->Promise.flatMap(n => Promise.resolved(n + 1))
->Promise.flatMap(n => Promise.resolved(n == 2));
}),

test("reason-promise as js argument", () => {
module Then = {
[@bs.send.pipe: Promise.t('a)]
[@mel.send.pipe: Promise.t('a)]
external js_then: ('a => Promise.t('b)) => Promise.t('b) =
"then";
};
Expand Down Expand Up @@ -243,7 +238,7 @@ let soundnessTests = Framework.suite("soundness", [

[@ocaml.warning "-33"]
test("resolved: Almost-Promise-like", () => {
let open Js_OO;
/* let open Js_OO; */
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I commented this out since didn't seem needed, but wanted to acknowledge that I have no clue what's Js_OO

Promise.resolved(makeAlmostPromiseLike(42))
->Promise.flatMap(x => Promise.resolved(x##_then == 42));
}),
Expand Down
3 changes: 3 additions & 0 deletions test/ffi/native/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(library
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ffi is a lib under test now, with the 2 separate versions

(name test_ffi)
(libraries framework))
File renamed without changes.
3 changes: 0 additions & 3 deletions test/framework/dune

This file was deleted.

9 changes: 9 additions & 0 deletions test/framework/js/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
(library
(name framework_js)
(modes melange)
(wrapped false)
(libraries promise_js))

(copy_files
(mode fallback)
(files ../native/*.re))
2 changes: 1 addition & 1 deletion test/framework/native/dune
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
(library
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The run file/lib isn't needed anymore since we can share framework

(name run)
(name framework)
(libraries promise))
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions test/isoresult/js/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(library
(name isoresult_js)
(modes melange)
(wrapped false)
(libraries melange.belt))
1 change: 0 additions & 1 deletion test/js/benchmark.re
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ let test = Framework.test;



[@bs.val]
external hrtime: unit => (int, int) = "process.hrtime";

let hrtime = () => {
Expand Down
9 changes: 9 additions & 0 deletions test/js/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
(copy_files
(mode fallback)
(files ../native/*.re))

(melange.emit
(target test)
(libraries framework_js promise_js isoresult_js test_ffi_js)
(preprocess
(pps melange.ppx)))
6 changes: 3 additions & 3 deletions test/native/dune
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
(library
(name test_ffi)
(libraries framework))
(executable
(name test_main)
(libraries isoresult promise test_ffi))
File renamed without changes.
File renamed without changes.