diff --git a/lib/orb.ex b/lib/orb.ex index aa88db6..ab8b428 100644 --- a/lib/orb.ex +++ b/lib/orb.ex @@ -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 diff --git a/test/examples/wasi.exs b/test/examples/wasi.exs index 17dae11..49249de 100644 --- a/test/examples/wasi.exs +++ b/test/examples/wasi.exs @@ -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 diff --git a/test/if_else_test.exs b/test/if_else_test.exs index a463467..9614c74 100644 --- a/test/if_else_test.exs +++ b/test/if_else_test.exs @@ -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, diff --git a/test/import_test.exs b/test/import_test.exs new file mode 100644 index 0000000..8cf4e27 --- /dev/null +++ b/test/import_test.exs @@ -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 diff --git a/test/orb_test.exs b/test/orb_test.exs index 5e19625..c22ff27 100644 --- a/test/orb_test.exs +++ b/test/orb_test.exs @@ -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