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
@@ -0,0 +1,364 @@
// The Go native back end end to end: Go functions BAML calls back (host
// functions), BAML function values, methods, host values, deadlines, panics
// and failures nobody awaited. The Go tests are in ../go.

class Person {
name string
age int
}

class MyError {
code int
message string
}

enum Mood {
Happy
Sad
}

function echo_int(x: int) -> int {
x
}

function sleep_ms(ms: int) -> int {
baml.sys.sleep(baml.time.Duration.from_milliseconds(ms));
ms
}

// Computes without waiting: a deadline or a cancel cannot stop it.
function spin(n: int) -> int {
let i = 0;
while (i < n) {
i += 1;
}
i
}

function divide(a: int, b: int) -> int {
a / b
}

function throw_my_error(code: int) -> int throws MyError {
throw MyError { code: code, message: "thrown" }
}

function apply(f: (int) -> int, x: int) -> int {
f(x)
}

function apply_twice(f: (int) -> int, x: int) -> int {
f(f(x))
}

function apply_person(f: (Person) -> Person, p: Person) -> string {
f(p).name
}

function apply_mood(f: (Mood) -> Mood, m: Mood) -> Mood {
f(m)
}

function apply_list(f: (int[]) -> int[], xs: int[]) -> int[] {
f(xs)
}

function apply_union(f: (int) -> string | int, x: int) -> string | int {
f(x)
}

function apply_void(f: (int) -> null, x: int) -> int {
f(x);
x
}

function apply_never(f: (int) -> int throws never, x: int) -> int {
f(x)
}

function call_many(f: (int) -> int, n: int) -> int {
let i = 0;
let s = 0;
while (i < n) {
s += f(i);
i += 1;
}
s
}

// The callback runs in a task the call spawned.
function call_in_task(f: (int) -> int, x: int) -> int {
let fut = spawn { f(x) };
await fut
}

// The callback runs after the call has returned.
function call_later(f: (int) -> int, ms: int) -> int {
let _t = spawn {
baml.sys.sleep(baml.time.Duration.from_milliseconds(ms));
f(ms)
};
0
}

function nap_then_apply(f: (int) -> int, ms: int) -> int {
baml.sys.sleep(baml.time.Duration.from_milliseconds(ms));
f(ms)
}

// BAML code catches what the callback throws (baml_go.Throw of MyError).
function call_catch(f: (int) -> int throws MyError, x: int) -> string {
let r = {
let v = f(x);
`ok ${v}`
} catch (e) {
MyError => `caught ${e.code}`,
};
r
}

function call_propagate(f: (int) -> int throws MyError, x: int) -> int throws MyError {
f(x)
}

// BAML code catches a Go error as baml.errors.HostCallable.
function call_catch_host(f: (int) -> int throws baml.errors.HostCallable, x: int) -> string {
let r = {
let v = f(x);
`ok ${v}`
} catch (e) {
_ => "caught " + e.class_name + ": " + e.message,
};
r
}

function pass_host_error(f: (int) -> int throws baml.errors.HostCallable, x: int) -> int throws baml.errors.HostCallable {
f(x)
}

// A callback without a throws clause: a Go panic is catchable.
function call_open(f: (int) -> int, x: int) -> string {
let r = {
let v = f(x);
`ok ${v}`
} catch (e) {
_ => "caught",
};
r
}

function make_adder(n: int) -> (x: int) -> int throws never {
(x: int) -> int { x + n }
}

function make_greeter(greeting: string) -> (name: string, times: int) -> string throws never {
(name: string, times: int) -> string {
let out = "";
for (let i = 0; i < times; i += 1) {
out = out + greeting + " " + name + ";";
}
out
}
}

function make_sleeper() -> (ms: int) -> int throws baml.errors.Io {
(ms: int) -> int throws baml.errors.Io {
baml.sys.sleep(baml.time.Duration.from_milliseconds(ms));
ms
}
}

function make_thrower() -> (code: int) -> int throws MyError {
(code: int) -> int throws MyError {
throw MyError { code: code, message: "from a function value" }
}
}

// A function value that calls the Go function it captured.
function wrap(f: (int) -> int throws never) -> (x: int) -> int throws never {
(x: int) -> int { f(x) + 1 }
}

class Counter {
n int

function zero() -> Counter {
Counter { n: 0 }
}

function bump(self, by: int) -> Counter {
Counter { n: self.n + by }
}

function wait_and_read(self, ms: int) -> int {
baml.sys.sleep(baml.time.Duration.from_milliseconds(ms));
self.n
}

function apply(self, f: (int) -> int) -> int {
f(self.n)
}
}

function pick_lists(x: string[] | int[]) -> string[] | int[] {
x
}

function list_kind(x: string[] | int[]) -> string {
match (x) {
let s: string[] => "strings",
let i: int[] => "ints",
}
}

function map_kind(x: map<string, int> | map<string, string>) -> string {
match (x) {
let i: map<string, int> => "ints",
let s: map<string, string> => "strings",
}
}

function echo_host(v: baml.host.Value) -> baml.host.Value {
v
}

function twice_host(v: baml.host.Value) -> baml.host.Value[] {
[v, v]
}

function host_through(f: (baml.host.Value) -> baml.host.Value, v: baml.host.Value) -> baml.host.Value {
f(v)
}

function fail_unawaited(code: int) -> int {
let _f = spawn {
throw MyError { code: code, message: "lost" }
};
0
}

// Allocates enough to make the collector run.
function churn(n: int) -> int {
let i = 0;
let total = 0;
while (i < n) {
let xs = [i, i + 1, i + 2];
total += xs.length();
i += 1;
}
total
}

// Re-entry: the callback calls back into BAML.
function reenter(f: (int) -> int, depth: int) -> int {
f(depth)
}

// Values that cross as handles: an opaque class of the standard library.
function new_token() -> baml.spawn.CancelToken throws never {
baml.spawn.CancelToken.new()
}

function fire_token(t: baml.spawn.CancelToken) -> int throws never {
t.cancel()
}

function token_fired(t: baml.spawn.CancelToken) -> bool throws never {
t.is_cancelled()
}

// Reads the token after a nap: the handle must stay valid while the call
// runs, even when Go lets it go meanwhile.
function token_fired_later(t: baml.spawn.CancelToken, ms: int) -> bool {
baml.sys.sleep(baml.time.Duration.from_milliseconds(ms));
t.is_cancelled()
}

function tokens(n: int) -> baml.spawn.CancelToken[] throws never {
let out: baml.spawn.CancelToken[] = [];
for (let i = 0; i < n; i += 1) {
out.push(baml.spawn.CancelToken.new());
}
out
}

// Handles inside function types: an argument a callback receives (with a
// value after it), the result a callback returns, the argument and the
// result of a function value.
function token_to_callback(f: (baml.spawn.CancelToken, int) -> int, t: baml.spawn.CancelToken, x: int) -> int {
f(t, x)
}

function token_from_callback(f: (int) -> baml.spawn.CancelToken, x: int) -> bool {
f(x).is_cancelled()
}

function make_token_reader() -> (t: baml.spawn.CancelToken, x: int) -> int throws never {
(t: baml.spawn.CancelToken, x: int) -> int {
if (t.is_cancelled()) {
x + 1000
} else {
x
}
}
}

function make_token_maker() -> (x: int) -> baml.spawn.CancelToken throws never {
(x: int) -> baml.spawn.CancelToken {
baml.spawn.CancelToken.new()
}
}

// A callback result only the member form can name: an empty list under two
// list members.
function lists_from_callback(f: (int) -> string[] | int[], x: int) -> int {
let r = f(x);
match (r) {
let s: string[] => s.length(),
let i: int[] => i.length(),
}
}

// A function value whose argument only the member form can name.
function make_lists_reader() -> (xs: string[] | int[]) -> int throws never {
(xs: string[] | int[]) -> int {
match (xs) {
let s: string[] => s.length() + 100,
let i: int[] => i.length() + 200,
}
}
}

// Empty containers sent to the host under two list or two map members: only
// the member form says which member they are.
function lists_to_callback(f: (xs: string[] | int[]) -> int, which: int) -> int {
if (which == 0) {
let xs: string[] = [];
f(xs)
} else {
let xs: int[] = [];
f(xs)
}
}

function maps_to_callback(f: (m: map<string, int> | map<string, string>) -> int, which: int) -> int {
if (which == 0) {
let m: map<string, int> = {};
f(m)
} else {
let m: map<string, string> = {};
f(m)
}
}

function make_lists_maker() -> (which: int) -> string[] | int[] throws never {
(which: int) -> string[] | int[] {
if (which == 0) {
let xs: string[] = [];
xs
} else {
let xs: int[] = [];
xs
}
}
}
Loading
Loading