Skip to content

Commit

Permalink
wrap top-level statements in an async function
Browse files Browse the repository at this point in the history
Summary:
When converting notebooks to Hack, put top-level statements in an `async` function.
This enables the use of `await` and matches how notebooks are type-checked and executed (as if top-level statements are in an async function).

context: https://fburl.com/source-controlled-notebooks-for-hack-project-2024

Reviewed By: lindseyjin

Differential Revision: D66460185

fbshipit-source-id: 10da589b8ffa20327f47cbc2b9e27a1093ec3a20
  • Loading branch information
mheiber authored and facebook-github-bot committed Nov 25, 2024
1 parent 5674ee3 commit c652960
Show file tree
Hide file tree
Showing 13 changed files with 31 additions and 11 deletions.
3 changes: 2 additions & 1 deletion hphp/hack/src/notebook_convert/internal/hack_to_notebook.ml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ let hack_to_notebook_exn
};
_;
}
when String.is_prefix ~prefix:"notebook_main"
when String.is_prefix
~prefix:Notebook_convert_constants.main_function_prefix
@@ String.strip
@@ Syn.text function_name ->
Either.First (Syn.leading_text header, compound_statements)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the "hack" directory of this source tree.
*
*)
let main_function_prefix = "gen_notebook_main_"
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the "hack" directory of this source tree.
*
*)
val main_function_prefix : string
3 changes: 2 additions & 1 deletion hphp/hack/src/notebook_convert/internal/notebook_to_hack.ml
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ let hack_of_ipynb_exn
block |> String.split_lines |> List.map ~f:(Printf.sprintf " %s"))
|> String.concat ~sep:"\n"
|> Printf.sprintf
"function notebook_main_%s(): void {\n%s\n}"
"async function %s%s(): Awaitable<void> {\n%s\n}"
Notebook_convert_constants.main_function_prefix
notebook_number
in
let unformatted =
Expand Down
2 changes: 1 addition & 1 deletion hphp/hack/test/notebook_convert/hack_to_notebook/nb1.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function hello(): void {
}
}

function notebook_main_n1234(): void {
async function gen_notebook_main_n1234(): Awaitable<void> {
//@bento-cell:{"cell_bento_metadata":{"output": {"id":1247934846418027,"loadingStatus":"loaded"}, "collapsed": true},"cell_type":"code","id":1}
$m = new MyClass();
echo "hi1";
Expand Down
2 changes: 1 addition & 1 deletion hphp/hack/test/notebook_convert/hack_to_notebook/nb5.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
test this is markdown
*/

function notebook_main_N6203962(): void {
async function gen_notebook_main_N6203962(): Awaitable<void> {
//@bento-cell:{"id": 2, "cell_type": "code"}
$a = 10;
//@bento-cell:{"id": 4, "cell_type": "code"}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
"metadata": {},
"outputs": [],
"source": [
"echo \"this is cell 4\";"
"echo \"this is cell 4\";\n",
"await (async () ==> {})();"
]
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class MyClass {}
I am a *markdown* **cell**
*/

function notebook_main_N1234(): void {
async function gen_notebook_main_N1234(): Awaitable<void> {
//@bento-cell:{"cell_bento_metadata":{"collapsed":true,"output":{"id":1247934846418027,"loadingStatus":"loaded"}},"cell_type":"code","id":1}
echo "hi from cell 1";
//@bento-cell:{"cell_bento_metadata":{"collapsed":true,"output":{"id":1247934846418027,"loadingStatus":"loaded"}},"cell_type":"code","id":1}
Expand All @@ -25,4 +25,5 @@ function notebook_main_N1234(): void {
echo "end of cell 2";
//@bento-cell:{"cell_bento_metadata":{},"cell_type":"code","id":4}
echo "this is cell 4";
await (async () ==> {})();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// copyright header here
//@bento-notebook:{"notebook_number":"N1234","kernelspec":{"display_name":"hack","language":"hack","name":"bento_kernel_hack"}}

function notebook_main_N1234(): void {
async function gen_notebook_main_N1234(): Awaitable<void> {
//@bento-cell:{"cell_bento_metadata":{"collapsed":true},"cell_type":"code","id":1}
// ill-formatted code
echo "hi0";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class MyClass {}


function notebook_main_N1234(): void {
async function gen_notebook_main_N1234(): Awaitable<void> {
//@bento-cell:{"cell_bento_metadata":{},"cell_type":"code","id":2}
$x = new MyClass();
// The next line is not valid Hack:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
I am a *markdown* **cell**
*/

function notebook_main_N1234(): void {
async function gen_notebook_main_N1234(): Awaitable<void> {

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
// copyright header here
//@bento-notebook:{"notebook_number":"N1234","kernelspec":{"display_name":"hack","language":"hack","name":"bento_kernel_hack"}}

function notebook_main_N1234(): void {
async function gen_notebook_main_N1234(): Awaitable<void> {

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
// copyright header here
//@bento-notebook:{"notebook_number":"N1234","kernelspec":{"display_name":"hack","language":"hack","name":"bento_kernel_hack"}}

function notebook_main_N1234(): void {
async function gen_notebook_main_N1234(): Awaitable<void> {

}

0 comments on commit c652960

Please sign in to comment.