Skip to content

Commit ad1844b

Browse files
fix: update legacy examples
1 parent e2e506f commit ad1844b

File tree

26 files changed

+403
-196
lines changed

26 files changed

+403
-196
lines changed

legacy/examples/bytes-buffer/lib/buffer.mbt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub fn Buffer::new(size : Int) -> Buffer {
2424
}
2525

2626
///|
27-
pub fn op_get(self : Buffer, i : Int) -> Byte? {
27+
pub fn Buffer::op_get(self : Buffer, i : Int) -> Byte? {
2828
if i < self.data.length() {
2929
Some(self.data[i])
3030
} else {
@@ -33,17 +33,17 @@ pub fn op_get(self : Buffer, i : Int) -> Byte? {
3333
}
3434

3535
///|
36-
pub fn capacity(self : Buffer) -> Int {
36+
pub fn Buffer::capacity(self : Buffer) -> Int {
3737
self.data.length()
3838
}
3939

4040
///|
41-
pub fn length(self : Buffer) -> Int {
41+
pub fn Buffer::length(self : Buffer) -> Int {
4242
self.len
4343
}
4444

4545
///|
46-
fn expand_size(self : Buffer) -> Unit {
46+
fn Buffer::expand_size(self : Buffer) -> Unit {
4747
let new_capacity = if self.data.length() != 0 {
4848
self.data.length() * 2
4949
} else {
@@ -59,7 +59,7 @@ fn expand_size(self : Buffer) -> Unit {
5959
}
6060

6161
///|
62-
pub fn append_int(self : Buffer, value : Int) -> Unit {
62+
pub fn Buffer::append_int(self : Buffer, value : Int) -> Unit {
6363
if self.len + 4 >= self.data.length() {
6464
self.expand_size()
6565
}
@@ -72,7 +72,7 @@ pub fn append_int(self : Buffer, value : Int) -> Unit {
7272
}
7373

7474
///|
75-
pub fn truncate(self : Buffer, another : Buffer) -> Unit {
75+
pub fn Buffer::truncate(self : Buffer, another : Buffer) -> Unit {
7676
let mut index = 0
7777
while index < another.len {
7878
if self.len >= self.data.length() {
@@ -85,7 +85,7 @@ pub fn truncate(self : Buffer, another : Buffer) -> Unit {
8585
}
8686

8787
///|
88-
pub fn clear(self : Buffer) -> Unit {
88+
pub fn Buffer::clear(self : Buffer) -> Unit {
8989
let mut index = 0
9090
while index < self.len {
9191
self.data[index] = b'\x00'
@@ -95,12 +95,12 @@ pub fn clear(self : Buffer) -> Unit {
9595
}
9696

9797
///|
98-
pub fn reset(self : Buffer, capacity : Int) -> Unit {
98+
pub fn Buffer::reset(self : Buffer, capacity : Int) -> Unit {
9999
self.len = 0
100100
self.data = FixedArray::make(capacity, b'\x00')
101101
}
102102

103103
///|
104-
pub fn to_bytes(self : Buffer) -> Bytes {
105-
self.data |> Bytes::from_fixedarray
104+
pub fn Buffer::to_bytes(self : Buffer) -> Bytes {
105+
self.data[:self.len] |> Bytes::from_array
106106
}

legacy/examples/bytes-buffer/lib/buffer_test.mbt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ test {
1818
buf.append_int(256)
1919
buf.append_int(255)
2020
let content = buf.to_bytes()
21-
inspect(content[:].to_int_be(), content="256")
22-
inspect(content[4:].to_int_be(), content="255")
21+
guard content is [i32_be(a), i32_be(b)]
22+
inspect(a, content="256")
23+
inspect(b, content="255")
2324
let buf1 = @lib.Buffer::new(4)
2425
buf1.append_int(257)
2526
buf.truncate(buf1)
2627
let content = buf.to_bytes()
27-
inspect(content[8:].to_int_be(), content="257")
28+
guard content[8:] is [i32_be(c)]
29+
inspect(c, content="257")
2830
}

legacy/examples/game_of_life/lib/draw.mbt

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,31 @@
1717
type Canvas_ctx
1818

1919
///|
20-
fn set_stroke_color(self : Canvas_ctx, color : Int) = "canvas" "set_stroke_color"
20+
fn Canvas_ctx::set_stroke_color(self : Canvas_ctx, color : Int) = "canvas" "set_stroke_color"
2121

2222
///|
23-
fn move_to(self : Canvas_ctx, x : Int, y : Int) = "canvas" "move_to"
23+
fn Canvas_ctx::move_to(self : Canvas_ctx, x : Int, y : Int) = "canvas" "move_to"
2424

2525
///|
26-
fn line_to(self : Canvas_ctx, x : Int, y : Int) = "canvas" "line_to"
26+
fn Canvas_ctx::line_to(self : Canvas_ctx, x : Int, y : Int) = "canvas" "line_to"
2727

2828
///|
29-
fn begin_path(self : Canvas_ctx) = "canvas" "begin_path"
29+
fn Canvas_ctx::begin_path(self : Canvas_ctx) = "canvas" "begin_path"
3030

3131
///|
32-
fn stroke(self : Canvas_ctx) = "canvas" "stroke"
32+
fn Canvas_ctx::stroke(self : Canvas_ctx) = "canvas" "stroke"
3333

3434
///|
35-
fn fill_rect(self : Canvas_ctx, x : Int, y : Int, width : Int, height : Int) = "canvas" "fill_rect"
35+
fn Canvas_ctx::fill_rect(
36+
self : Canvas_ctx,
37+
x : Int,
38+
y : Int,
39+
width : Int,
40+
height : Int,
41+
) = "canvas" "fill_rect"
3642

3743
///|
38-
fn set_fill_style(self : Canvas_ctx, color : Int) = "canvas" "set_fill_style"
44+
fn Canvas_ctx::set_fill_style(self : Canvas_ctx, color : Int) = "canvas" "set_fill_style"
3945

4046
///|
4147
let cell_size = 5

legacy/examples/game_of_life/lib/game_of_life.mbt

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,29 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
///|
1516
enum Cell {
1617
Dead
1718
Alive
1819
}
1920

21+
///|
2022
struct Universe {
2123
width : Int
2224
height : Int
2325
mut cells : Array[Cell]
2426
}
2527

26-
fn get_index(self : Universe, row : Int, column : Int) -> Int {
28+
///|
29+
fn Universe::get_index(self : Universe, row : Int, column : Int) -> Int {
2730
if row * self.width + column < self.width * self.height {
2831
row * self.width + column
2932
} else {
3033
0
3134
}
3235
}
3336

37+
///|
3438
pub fn new() -> Universe {
3539
let width = 64
3640
let height = 64
@@ -47,26 +51,35 @@ pub fn new() -> Universe {
4751
{ width, height, cells }
4852
}
4953

50-
pub fn get_width(self : Universe) -> Int {
54+
///|
55+
pub fn Universe::get_width(self : Universe) -> Int {
5156
self.width
5257
}
5358

54-
pub fn get_height(self : Universe) -> Int {
59+
///|
60+
pub fn Universe::get_height(self : Universe) -> Int {
5561
self.height
5662
}
5763

58-
pub fn get_cells(self : Universe) -> Array[Cell] {
64+
///|
65+
pub fn Universe::get_cells(self : Universe) -> Array[Cell] {
5966
self.cells
6067
}
6168

62-
pub fn get_cell(self : Universe, idx : Int) -> Int {
69+
///|
70+
pub fn Universe::get_cell(self : Universe, idx : Int) -> Int {
6371
match self.cells[idx] {
6472
Alive => 1
6573
Dead => 0
6674
}
6775
}
6876

69-
fn live_neighbor_count(self : Universe, row : Int, column : Int) -> Int {
77+
///|
78+
fn Universe::live_neighbor_count(
79+
self : Universe,
80+
row : Int,
81+
column : Int,
82+
) -> Int {
7083
let mut count = 0
7184
let delta_rows = [self.height - 1, 0, 1]
7285
let delta_cols = [self.width - 1, 0, 1]
@@ -89,7 +102,8 @@ fn live_neighbor_count(self : Universe, row : Int, column : Int) -> Int {
89102
count
90103
}
91104

92-
pub fn tick(self : Universe) -> Unit {
105+
///|
106+
pub fn Universe::tick(self : Universe) -> Unit {
93107
let next : Array[Cell] = Array::make(self.width * self.height, Dead)
94108
let mut r = 0
95109
while r < self.height {
@@ -118,7 +132,8 @@ pub fn tick(self : Universe) -> Unit {
118132
self.cells = next
119133
}
120134

121-
pub fn to_string(self : Universe) -> String {
135+
///|
136+
pub fn Universe::to_string(self : Universe) -> String {
122137
let mut r = 0
123138
let mut cells = ""
124139
while r < self.height {

legacy/examples/koch_snowflake/lib/koch.mbt

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,50 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
///|
1516
#external
1617
type Canvas_ctx
1718

18-
fn begin_path(self : Canvas_ctx) = "canvas" "begin_path"
19+
///|
20+
fn Canvas_ctx::begin_path(self : Canvas_ctx) = "canvas" "begin_path"
1921

20-
fn clear_rect(self : Canvas_ctx, x : Double, y : Double, w : Double,
21-
h : Double) = "canvas" "clear_rect"
22+
///|
23+
fn Canvas_ctx::clear_rect(
24+
self : Canvas_ctx,
25+
x : Double,
26+
y : Double,
27+
w : Double,
28+
h : Double,
29+
) = "canvas" "clear_rect"
2230

23-
fn move_to(self : Canvas_ctx, x : Double, y : Double) = "canvas" "move_to"
31+
///|
32+
fn Canvas_ctx::move_to(self : Canvas_ctx, x : Double, y : Double) = "canvas" "move_to"
2433

25-
fn line_to(self : Canvas_ctx, x : Double, y : Double) = "canvas" "line_to"
34+
///|
35+
fn Canvas_ctx::line_to(self : Canvas_ctx, x : Double, y : Double) = "canvas" "line_to"
2636

27-
fn stroke(self : Canvas_ctx) = "canvas" "stroke"
37+
///|
38+
fn Canvas_ctx::stroke(self : Canvas_ctx) = "canvas" "stroke"
2839

40+
///|
2941
fn sqrt(x : Double) -> Double = "canvas" "sqrt"
3042

43+
///|
3144
fn cos(x : Double) -> Double = "canvas" "cos"
3245

46+
///|
3347
fn get_pi() -> Double = "canvas" "get_pi"
3448

35-
fn koch(ctx : Canvas_ctx, x1 : Double, y1 : Double, x2 : Double,
36-
y2 : Double, n : Double, m : Double) -> Unit {
49+
///|
50+
fn koch(
51+
ctx : Canvas_ctx,
52+
x1 : Double,
53+
y1 : Double,
54+
x2 : Double,
55+
y2 : Double,
56+
n : Double,
57+
m : Double,
58+
) -> Unit {
3759
ctx.clear_rect(0.0, 0.0, 400.0, 400.0)
3860
let x3 = (x2 - x1) / 3.0 + x1
3961
let y3 = (y2 - y1) / 3.0 + y1
@@ -57,11 +79,11 @@ fn koch(ctx : Canvas_ctx, x1 : Double, y1 : Double, x2 : Double,
5779
koch(ctx, x4, y4, x2, y2, n, m)
5880
}
5981

82+
///|
6083
pub fn draw(ctx : Canvas_ctx, deep : Double) -> Unit {
6184
ctx.begin_path()
6285
let y = 80.0 + cos(get_pi() / 6.0) * 200.0
6386
koch(ctx, 150.0, y, 250.0, 80.0, 0.0, deep)
6487
koch(ctx, 250.0, 80.0, 50.0, 80.0, 0.0, deep)
6588
koch(ctx, 50.0, 80.0, 150.0, y, 0.0, deep)
6689
}
67-

legacy/examples/path/lib/path.mbt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub fn Path::new(s : String, platform : Platform, file_type : FileType) -> Path
3939
}
4040

4141
///|
42-
pub fn parent(self : Path) -> String {
42+
pub fn Path::parent(self : Path) -> String {
4343
match self.platform {
4444
Posix => {
4545
let parts = self.inner.split("/").collect()
@@ -77,37 +77,37 @@ pub fn parent(self : Path) -> String {
7777
}
7878

7979
///|
80-
pub fn is_absolute(self : Path) -> Bool {
80+
pub fn Path::is_absolute(self : Path) -> Bool {
8181
self.has_root()
8282
}
8383

8484
///|
85-
pub fn is_relative(self : Path) -> Bool {
85+
pub fn Path::is_relative(self : Path) -> Bool {
8686
not(self.has_root())
8787
}
8888

8989
///|
90-
pub fn has_root(self : Path) -> Bool {
90+
pub fn Path::has_root(self : Path) -> Bool {
9191
match self.platform {
9292
Posix => self.inner is ['/', ..]
9393
Windows => self.inner is ['a'..='z' | 'A'..='Z', ':', ..]
9494
}
9595
}
9696

9797
///|
98-
pub fn file_name(self : Path) -> String? {
98+
pub fn Path::file_name(self : Path) -> String? {
9999
match self.file_type {
100100
FileType::File =>
101101
match self.platform {
102-
Posix => self.inner.split("/").last().map(@string.View::to_string)
103-
Windows => self.inner.split("\\").last().map(@string.View::to_string)
102+
Posix => self.inner.split("/").last().map(StringView::to_string)
103+
Windows => self.inner.split("\\").last().map(StringView::to_string)
104104
}
105105
_ => None
106106
}
107107
}
108108

109109
///|
110-
pub fn append(self : Path, p : String) -> Unit {
110+
pub fn Path::append(self : Path, p : String) -> Unit {
111111
match self.platform {
112112
Posix => {
113113
if self.inner[self.inner.length() - 1] != '/' {
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
1-
{}
1+
{
2+
"targets": {
3+
"string.js.mbt": ["js"],
4+
"string.wasm-gc.mbt": ["wasm-gc"],
5+
"string.wasm.mbt": ["wasm"]
6+
}
7+
}

legacy/examples/snake/extern/string.js.mbt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
///|
2+
#external
13
// Copyright 2024 International Digital Economy Academy
24
//
35
// Licensed under the Apache License, Version 2.0 (the "License");
@@ -12,8 +14,10 @@
1214
// See the License for the specific language governing permissions and
1315
// limitations under the License.
1416

15-
pub extern type JsString
17+
pub type JsString
1618

19+
///|
1720
pub fn JsString::from_string(str : String) -> JsString = "%identity"
1821

22+
///|
1923
pub fn JsString::to_string(str : JsString) -> String = "%identity"

legacy/examples/snake/extern/string.wasm-gc.mbt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ priv type JsArray
2020
fn JsArray::new() -> JsArray = "array" "new"
2121

2222
///|
23-
fn push(self : JsArray, charCode : Int) = "array" "push"
23+
fn JsArray::push(self : JsArray, charCode : Int) = "array" "push"
2424

2525
///|
2626
#external
@@ -30,7 +30,7 @@ pub(all) type JsString
3030
fn JsString::from_code_point(charCode : JsArray) -> JsString = "string" "fromCodePoint"
3131

3232
///|
33-
fn iter(self : JsString, f : (Int) -> Unit) -> Unit = "string" "iter"
33+
fn JsString::iter(self : JsString, f : (Int) -> Unit) -> Unit = "string" "iter"
3434

3535
///|
3636
pub fn JsString::from_string(str : String) -> JsString {

0 commit comments

Comments
 (0)