Skip to content

Commit

Permalink
Merge pull request #89 from uran0sH/fix-export
Browse files Browse the repository at this point in the history
fix: export public function in lib
  • Loading branch information
liweijian authored Oct 7, 2023
2 parents 75a519f + bff6142 commit 84599f3
Show file tree
Hide file tree
Showing 16 changed files with 221 additions and 171 deletions.
8 changes: 4 additions & 4 deletions examples/game_of_life/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@
WebAssembly.instantiateStreaming(fetch("target/game_of_life.wasm"), importObject).then(
(obj) => {
obj.instance.exports._start();
const drawGrid = obj.instance.exports["game_of_life/lib::draw_grid"];
const drawCell = obj.instance.exports["game_of_life/lib::draw_cell"];
const universe_new = obj.instance.exports["game_of_life/lib::new"];
const universe_tick = obj.instance.exports["game_of_life/lib::@game_of_life/lib.Universe::tick"];
const drawGrid = obj.instance.exports["game_of_life/main::draw_grid"];
const drawCell = obj.instance.exports["game_of_life/main::draw_cell"];
const universe_new = obj.instance.exports["game_of_life/main::new"];
const universe_tick = obj.instance.exports["game_of_life/main::tick"];
const universe = universe_new();
const renderLoop = () => {
universe_tick(universe);
Expand Down
3 changes: 2 additions & 1 deletion examples/game_of_life/lib/draw.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ pub func draw_grid(canvas : Canvas_ctx) {
canvas.stroke()
}

pub func draw_cell(canvas : Canvas_ctx, universe : Universe, dead : Int, alive : Int) {
pub func draw_cell(canvas : Canvas_ctx, universe : Universe, dead : Int,
alive : Int) {
canvas.begin_path()
var row = 0
while row < 64 {
Expand Down
3 changes: 1 addition & 2 deletions examples/game_of_life/lib/game_of_life.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,7 @@ pub func to_string(self : Universe) -> String {
let idx = self.get_index(r, c)
let cell = self.cells[idx]
// No buffer implementation?
cells = cells +
match cell {
cells = cells + match cell {
Alive => "▅"
Dead => "☐"
}
Expand Down
17 changes: 17 additions & 0 deletions examples/game_of_life/main/main.mbt
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
pub func draw_grid(canvas : @lib.Canvas_ctx) {
@lib.draw_grid(canvas)
}

pub func draw_cell(canvas : @lib.Canvas_ctx, universe : @lib.Universe,
dead : Int, alive : Int) {
@lib.draw_cell(canvas, universe, dead, alive)
}

pub func tick(universe : @lib.Universe) {
@lib.tick(universe)
}

pub func new() -> @lib.Universe {
@lib.new()
}

func init {
()
}
Expand Down
5 changes: 1 addition & 4 deletions examples/koch_snowflake/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@
WebAssembly.instantiateStreaming(fetch("target/draw.wasm"), spectest).then(
(obj) => {
obj.instance.exports._start();
const koch = obj.instance.exports[
"koch_snowflake/lib::koch"
];
const draw = obj.instance.exports["koch_snowflake/lib::draw"];
const draw = obj.instance.exports["koch_snowflake/main::draw"];
var ctx = canvas.getContext("2d");
ctx.strikeStyle="#000";
draw(ctx, 1);
Expand Down
2 changes: 1 addition & 1 deletion examples/koch_snowflake/lib/koch.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func cos(x : Float64) -> Float64 = "canvas" "cos"

func get_pi() -> Float64 = "canvas" "get_pi"

pub func koch(ctx : Canvas_ctx, x1 : Float64, y1 : Float64, x2 : Float64,
func koch(ctx : Canvas_ctx, x1 : Float64, y1 : Float64, x2 : Float64,
y2 : Float64, n : Float64, m : Float64) {
ctx.clear_rect(0.0, 0.0, 400.0, 400.0)
let x3 = (x2 - x1) / 3.0 + x1
Expand Down
4 changes: 4 additions & 0 deletions examples/koch_snowflake/main/main.mbt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
pub func draw(ctx : @lib.Canvas_ctx, deep : Float64) {
@lib.draw(ctx, deep)
}

func init {
()
}
Expand Down
4 changes: 2 additions & 2 deletions examples/mandelbrot/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
WebAssembly.instantiateStreaming(fetch("target/mandelbrot.wasm"), spectest).then(
(obj) => {
obj.instance.exports._start();
const calcColor = obj.instance.exports["mandelbrot/lib::calc_color"];
const drawColor = obj.instance.exports["mandelbrot/lib::draw_color"];
const calcColor = obj.instance.exports["mandelbrot/main::calc_color"];
const drawColor = obj.instance.exports["mandelbrot/main::draw_color"];

function saveImage() {
context.putImageData(imagedata, 0, 0);
Expand Down
5 changes: 3 additions & 2 deletions examples/mandelbrot/lib/mandelbrot.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,9 @@ pub func calc_color(col : Int, row : Int, ox : Float64, oy : Float64,
}

pub func draw_color(image_data : ImageData, col : Int, row : Int, rgb : Int) {
let pindex = ((image_height - float_of_int(row) - 1.0) * image_width +
float_of_int(col)) * 4.0
let pindex = ((image_height - float_of_int(row) - 1.0) * image_width + float_of_int(
col,
)) * 4.0
let pindex = floor(pindex)
set_image_data(image_data, pindex, rgb.lsr(16).land(0xFF))
set_image_data(image_data, pindex + 1, rgb.lsr(8).land(0xFF))
Expand Down
9 changes: 9 additions & 0 deletions examples/mandelbrot/main/main.mbt
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
pub func calc_color(col : Int, row : Int, ox : Float64, oy : Float64,
width : Float64) -> Int {
@lib.calc_color(col, row, ox, oy, width)
}

pub func draw_color(image_data : @lib.ImageData, col : Int, row : Int, rgb : Int) {
@lib.draw_color(image_data, col, row, rgb)
}

func init {
()
}
Expand Down
8 changes: 4 additions & 4 deletions examples/tetris/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@
WebAssembly.instantiateStreaming(fetch("target/tetris.wasm"), importObject).then(
(obj) => {
obj.instance.exports._start();
tetris_draw = obj.instance.exports["tetris/lib::draw"];
tetris_new = obj.instance.exports["tetris/lib::new"];
tetris_step = obj.instance.exports["tetris/lib::step"];
tetris_score = obj.instance.exports["tetris/lib::get_score"]
tetris_draw = obj.instance.exports["tetris/main::draw"];
tetris_new = obj.instance.exports["tetris/main::new"];
tetris_step = obj.instance.exports["tetris/main::step"];
tetris_score = obj.instance.exports["tetris/main::get_score"]
tetris = tetris_new();
requestAnimationFrameId = requestAnimationFrame(update);
}
Expand Down
24 changes: 10 additions & 14 deletions examples/tetris/lib/config.mbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
let grid_row_count = 20

let grid_col_count = 10

enum PIECE {
Expand All @@ -11,20 +12,15 @@ enum PIECE {
O
}

pub func pice_shap(self:PIECE) -> Array[Array[Int]] {
pub func pice_shap(self : PIECE) -> Array[Array[Int]] {
match self {
I => [[1, 1, 1, 1]]
L => [[0, 0, 2],
[2, 2, 2]]
J => [[3, 0, 0],
[3, 3, 3]]
S => [[0, 4, 4],
[4, 4, 0]]
Z => [[5, 5, 0],
[0, 5, 5]]
T => [[6, 6, 6],
[0, 6, 0]]
O => [[7, 7],
[7, 7]]
L => [[0, 0, 2], [2, 2, 2]]
J => [[3, 0, 0], [3, 3, 3]]
S => [[0, 4, 4], [4, 4, 0]]
Z => [[5, 5, 0], [0, 5, 5]]
T => [[6, 6, 6], [0, 6, 0]]
O => [[7, 7], [7, 7]]
}
}
}

76 changes: 39 additions & 37 deletions examples/tetris/lib/draw.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -10,51 +10,53 @@ func fill_rect(self : Canvas_ctx, x : Int, y : Int, width : Int, height : Int) =

func set_fill_style(self : Canvas_ctx, color : Int) = "canvas" "set_fill_style"

func draw_game_over(self: Canvas_ctx) = "canvas" "draw_game_over"


func draw_game_over(self : Canvas_ctx) = "canvas" "draw_game_over"

pub func draw(canvas : Canvas_ctx, tetris : Tetris) {
var c = 0

//draw backgroud
while c < grid_col_count {
let color = if (c%2) == 0 {0} else {1}
canvas.set_fill_style(color)
canvas.fill_rect(c, 0, 1, grid_row_count)
c = c + 1
}

draw_piece(canvas, tetris.grid, (0, 0))
draw_piece(canvas, tetris.pice_shap.stream(), (tetris.pice_x, tetris.pice_y))

if tetris.dead {
canvas.draw_game_over()
var c = 0

// draw backgroud
while c < grid_col_count {
let color = if c % 2 == 0 {
0
} else {
1
}
canvas.set_fill_style(color)
canvas.fill_rect(c, 0, 1, grid_row_count)
c = c + 1
}
draw_piece(canvas, tetris.grid, (0, 0))
draw_piece(canvas, tetris.pice_shap.stream(), (tetris.pice_x, tetris.pice_y))
if tetris.dead {
canvas.draw_game_over()
}
}

pub func draw_piece(canvas:Canvas_ctx, matrix:List[Array[Int]], offset:(Int, Int)) {

fn go(l:List[Array[Int]], r:Int, canvas:Canvas_ctx) {
match l {
Cons(v, n) => {
var c = 0
while c < v.length() {
if v[c] == 0 {
c = c+1
continue
}
canvas.set_fill_style(v[c]+1)
canvas.fill_rect(offset.0 + c, offset.1 + r, 1, 1)
canvas.set_stroke_color(0)
canvas.set_line_width(0.1)
canvas.stroke_rect(offset.0 + c, offset.1 + r, 1, 1)
pub func draw_piece(canvas : Canvas_ctx, matrix : List[Array[Int]],
offset : (Int, Int)) {
fn go(l : List[Array[Int]], r : Int, canvas : Canvas_ctx) {
match l {
Cons(v, n) => {
var c = 0
while c < v.length() {
if v[c] == 0 {
c = c + 1
continue
}
go(n, r+1, canvas)
canvas.set_fill_style(v[c] + 1)
canvas.fill_rect(offset.0 + c, offset.1 + r, 1, 1)
canvas.set_stroke_color(0)
canvas.set_line_width(0.1)
canvas.stroke_rect(offset.0 + c, offset.1 + r, 1, 1)
c = c + 1
}
Nil => ()
go(n, r + 1, canvas)
}
Nil => ()
}
go(matrix, 0, canvas)
}

go(matrix, 0, canvas)
}

Loading

0 comments on commit 84599f3

Please sign in to comment.