Skip to content

Commit

Permalink
Migrate tests to Orb.Import.register/2
Browse files Browse the repository at this point in the history
  • Loading branch information
RoyalIcing committed Jun 23, 2024
1 parent 960ffa1 commit cb0a94e
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 63 deletions.
4 changes: 2 additions & 2 deletions lib/orb.ex
Original file line number Diff line number Diff line change
Expand Up @@ -502,10 +502,10 @@ defmodule Orb do

quote do
# TODO: don’t import types/1
import Orb, only: [export: 1, global: 1, global: 2, global: 3, importw: 2, types: 1]
import Orb, only: [export: 1, global: 1, global: 2, global: 3, types: 1]
import Orb.DSL.Defw
alias Orb.{I32, I64, S32, U32, F32, F64, Memory, Table}
require Orb.{I32, I64, Stack, Table, Memory}
require Orb.{I32, I64, Stack, Table, Memory, Import}

unquote(def_quoted)
end
Expand Down
2 changes: 1 addition & 1 deletion test/examples/wasi.exs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ defmodule Examples.ClockConsumer do

Memory.pages(1)

Orb.importw(WasiUnstable, :wasi_unstable)
Orb.Import.register(WasiUnstable, :wasi_unstable)

defw get_seconds, I32 do
0
Expand Down
2 changes: 1 addition & 1 deletion test/if_else_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ defmodule IfElseTest do
defw(found_alphanumeric(), nil)
end

importw(Log, :log)
Orb.Import.register(Log, :log)

defw url_search_params_count(url_params: I32.U8.UnsafePointer), I32,
char: I32.U8,
Expand Down
60 changes: 60 additions & 0 deletions test/import_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
defmodule ImportTest do
use ExUnit.Case, async: true

test "Orb.Import.register/1" do
defmodule ImportsExample do
use Orb

defmodule Echo do
use Orb.Import, name: :echo

defw(int32(a: I32), I32)
defw(int64(a: I64), I64)
end

defmodule Log do
use Orb.Import

defw(int32(a: I32))
defw(int64(a: I64))
end

defmodule Time do
use Orb.Import

defw(unix_time(), I64)
end

# TODO:
# use Orb.Import, name: :echo
# Echo.import()
# Log.import()
# Time.import()

Orb.Import.register(Echo, :echo)
Orb.Import.register(Log, :log)
Orb.Import.register(Time, :time)

defw test() do
Echo.int32(42) |> Orb.Stack.drop()
Echo.int64(42) |> Orb.Stack.drop()
end
end

assert """
(module $ImportsExample
(import "echo" "int32" (func $ImportTest.ImportsExample.Echo.int32 (param $a i32) (result i32)))
(import "echo" "int64" (func $ImportTest.ImportsExample.Echo.int64 (param $a i64) (result i64)))
(import "log" "int32" (func $ImportTest.ImportsExample.Log.int32 (param $a i32)))
(import "log" "int64" (func $ImportTest.ImportsExample.Log.int64 (param $a i64)))
(import "time" "unix_time" (func $ImportTest.ImportsExample.Time.unix_time (result i64)))
(func $test (export "test")
(call $ImportTest.ImportsExample.Echo.int32 (i32.const 42))
drop
(call $ImportTest.ImportsExample.Echo.int64 (i64.const 42))
drop
)
)
""" = Orb.to_wat(ImportsExample)
end
end
59 changes: 0 additions & 59 deletions test/orb_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -120,65 +120,6 @@ defmodule OrbTest do
end
end

describe "imports" do
test "importw/2" do
defmodule ImportsTest do
use Orb

defmodule Echo do
use Orb.Import

defw(int32(a: I32), I32)
defw(int64(a: I64), I64)
end

defmodule Log do
use Orb.Import

defw(int32(a: I32))
defw(int64(a: I64))
end

defmodule Time do
use Orb.Import

defw(unix_time(), I64)
end

# TODO:
# use Orb.Import, name: :echo
# Echo.import()
# Log.import()
# Time.import()

Orb.importw(Echo, :echo)
Orb.importw(Log, :log)
Orb.importw(Time, :time)

defw test() do
Echo.int32(42) |> Orb.Stack.drop()
Echo.int64(42) |> Orb.Stack.drop()
end
end

assert """
(module $ImportsTest
(import "echo" "int32" (func $OrbTest.ImportsTest.Echo.int32 (param $a i32) (result i32)))
(import "echo" "int64" (func $OrbTest.ImportsTest.Echo.int64 (param $a i64) (result i64)))
(import "log" "int32" (func $OrbTest.ImportsTest.Log.int32 (param $a i32)))
(import "log" "int64" (func $OrbTest.ImportsTest.Log.int64 (param $a i64)))
(import "time" "unix_time" (func $OrbTest.ImportsTest.Time.unix_time (result i64)))
(func $test (export "test")
(call $OrbTest.ImportsTest.Echo.int32 (i32.const 42))
drop
(call $OrbTest.ImportsTest.Echo.int64 (i64.const 42))
drop
)
)
""" = to_wat(ImportsTest)
end
end

defmodule SingleFunc do
use Orb

Expand Down

0 comments on commit cb0a94e

Please sign in to comment.