From b256b9138494ef620edfee5a02cc33e3476842ed Mon Sep 17 00:00:00 2001 From: zihang Date: Wed, 3 Apr 2024 11:52:10 +0800 Subject: [PATCH] fix: koch_snowflake --- examples/koch_snowflake/Makefile | 5 ++--- examples/koch_snowflake/index.html | 4 ++-- examples/koch_snowflake/lib/koch.mbt | 4 ++-- examples/koch_snowflake/lib/moon.pkg.json | 8 +++++++- examples/koch_snowflake/main/main.mbt | 8 -------- examples/koch_snowflake/main/moon.pkg.json | 6 ------ 6 files changed, 13 insertions(+), 22 deletions(-) delete mode 100644 examples/koch_snowflake/main/main.mbt delete mode 100644 examples/koch_snowflake/main/moon.pkg.json diff --git a/examples/koch_snowflake/Makefile b/examples/koch_snowflake/Makefile index 12c6ee04..b684f68b 100644 --- a/examples/koch_snowflake/Makefile +++ b/examples/koch_snowflake/Makefile @@ -1,4 +1,3 @@ run: - moon build - wat2wasm target/build/main/main.wat --output=target/draw.wasm - npx serve ./ \ No newline at end of file + moon build --target wasm-gc + python3 -m http.server 8000 \ No newline at end of file diff --git a/examples/koch_snowflake/index.html b/examples/koch_snowflake/index.html index ce7cbde1..4fd0f085 100644 --- a/examples/koch_snowflake/index.html +++ b/examples/koch_snowflake/index.html @@ -30,10 +30,10 @@ var canvas = document.getElementById("canvas"); - WebAssembly.instantiateStreaming(fetch("target/draw.wasm"), spectest).then( + WebAssembly.instantiateStreaming(fetch("target/wasm-gc/release/build/lib/lib.wasm"), spectest).then( (obj) => { obj.instance.exports._start(); - const draw = obj.instance.exports["koch_snowflake/main::draw"]; + const draw = obj.instance.exports["draw"]; var ctx = canvas.getContext("2d"); ctx.strikeStyle="#000"; draw(ctx, 1); diff --git a/examples/koch_snowflake/lib/koch.mbt b/examples/koch_snowflake/lib/koch.mbt index d5bed923..d7a697e3 100644 --- a/examples/koch_snowflake/lib/koch.mbt +++ b/examples/koch_snowflake/lib/koch.mbt @@ -18,7 +18,7 @@ fn cos(x : Double) -> Double = "canvas" "cos" fn get_pi() -> Double = "canvas" "get_pi" fn koch(ctx : Canvas_ctx, x1 : Double, y1 : Double, x2 : Double, - y2 : Double, n : Double, m : Double) { + y2 : Double, n : Double, m : Double) -> Unit { ctx.clear_rect(0.0, 0.0, 400.0, 400.0) let x3 = (x2 - x1) / 3.0 + x1 let y3 = (y2 - y1) / 3.0 + y1 @@ -42,7 +42,7 @@ fn koch(ctx : Canvas_ctx, x1 : Double, y1 : Double, x2 : Double, koch(ctx, x4, y4, x2, y2, n, m) } -pub fn draw(ctx : Canvas_ctx, deep : Double) { +pub fn draw(ctx : Canvas_ctx, deep : Double) -> Unit { ctx.begin_path() let y = 80.0 + cos(get_pi() / 6.0) * 200.0 koch(ctx, 150.0, y, 250.0, 80.0, 0.0, deep) diff --git a/examples/koch_snowflake/lib/moon.pkg.json b/examples/koch_snowflake/lib/moon.pkg.json index 47b3a8cb..9ad96493 100644 --- a/examples/koch_snowflake/lib/moon.pkg.json +++ b/examples/koch_snowflake/lib/moon.pkg.json @@ -1,3 +1,9 @@ { - "name": "lib" + "link": { + "wasm-gc": { + "exports": [ + "draw" + ] + } + } } \ No newline at end of file diff --git a/examples/koch_snowflake/main/main.mbt b/examples/koch_snowflake/main/main.mbt deleted file mode 100644 index 9b7b8121..00000000 --- a/examples/koch_snowflake/main/main.mbt +++ /dev/null @@ -1,8 +0,0 @@ -pub fn draw(ctx : @lib.Canvas_ctx, deep : Double) { - @lib.draw(ctx, deep) -} - -fn init { - () -} - diff --git a/examples/koch_snowflake/main/moon.pkg.json b/examples/koch_snowflake/main/moon.pkg.json deleted file mode 100644 index bba4fd13..00000000 --- a/examples/koch_snowflake/main/moon.pkg.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "name": "main", - "import": { - "koch_snowflake/lib": "" - } -} \ No newline at end of file