6
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ExAtomVMでElixirの関数の互換性を確認してみる

Last updated at Posted at 2025-11-26

ExAtomVMはElixirの関数をすべて使えるわけではありません
どんな関数が使えるか気になりますよね?

結論

exavmlibのソースを見ましょう

今回はElixirでよく使われるEnumを確認しましょう

実験用の環境作成

今回はUbuntuで動かす前提にします

下記を参考に環境を作成してください

実験用のプロジェクト作成

$ mix new exploring_atom_vm

mix.exsを上書き

mix.exs
defmodule ExploringAtomVm.MixProject do
  use Mix.Project

  def project do
    [
      app: :exploring_atom_vm,
      version: "0.1.0",
      elixir: "~> 1.17",
      start_permanent: Mix.env() == :prod,
      deps: deps(),
      atomvm: [
        start: ExploringAtomVm,
        flash_offset: 0x210000
      ]
    ]
  end

  # Run "mix help compile.app" to learn about applications.
  def application do
    [
      extra_applications: [:logger]
    ]
  end

  # Run "mix help deps" to learn about dependencies.
  defp deps do
    [
      {:exatomvm, git: "https://github.com/atomvm/ExAtomVM/"}
      # {:dep_from_hexpm, "~> 0.3.0"},
      # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
    ]
  end
end

ライブラリを追加

依存関係を取得・ダウンロードする

$ mix deps.get

AtomVM固有のライブラリ

atomvmlibはビルド時に必要です
これがないと実機で実行できません

atomvmlib-v0.6.6.avmをダウンロード

プロジェクトの直下にavm_depsフォルダーを作成
ダウンロードしたatomvmlib-v0.6.6.avmをavm_depsにコピーする

検証用コード作成

exploring_atom_vm.exを上書き

lib/exploring_atom_vm.ex
defmodule ExploringAtomVm do
  @moduledoc """
  Documentation for `ExploringAtomVm`.
  """

  def start do
     Enum.at([2, 4, 6], 1, :none)
  end
end

実行

  • mix atomvm.packbeam でコンパイル
  • AtomVM で実行

実行結果

$ mix atomvm.uf2create
Created exploring_atom_vm.uf2
$ AtomVM exploring_atom_vm.avm
Unable to open init.beam
Failed load module: init.beam
Return value: 4

exavmlibのソース libs/exavmlib/lib/Enum.exを見ながら検証

exavmlibのソースの例を参照

libs/exavmlib/lib/Enum.ex 
      iex> Enum.all?([1, 2, 3])
      true

      iex> Enum.all?([1, nil, 3])
      false

      iex> Enum.all?([])
      true

実験用ソース

lib/exploring_atom_vm.ex
defmodule ExploringAtomVm do
  @moduledoc """
  Documentation for `ExploringAtomVm`.
  """

  def start do
     Enum.all?([1, 2, 3])
  end
end

実行

$ mix atomvm.uf2create
Created exploring_atom_vm.uf2
$ AtomVM exploring_atom_vm.avm
Unable to open init.beam
Failed load module: init.beam
Return value: true

Return value: trueで期待値ですね

ElixirにはあってもExAtomVMにないもの

Enum.sumで実験してみます

lib/exploring_atom_vm.ex
defmodule ExploringAtomVm do
  @moduledoc """
  Documentation for `ExploringAtomVm`.
  """

  def start do
     Enum.sum(1..10)
  end
end

コンパイル時に警告がでます

$ mix atomvm.packbeam
Warning: following modules or functions are not available on AtomVM:
* Elixir.Enum:sum/1

(Using them may not be supported; make sure ExAtomVM is fully updated.)

実行してみます

$ AtomVM exploring_atom_vm.avm
Unable to open init.beam
Failed load module: init.beam
Warning: function Elixir.Enum:sum/1 cannot be resolved.
CRASH 
======
pid: <0.1.0>

Stacktrace:
[{Elixir.ExploringAtomVm,start,0,[]}]

cp: #CP<module: 0, label: 18, offset: 0>

x[0]: error
x[1]: undef
x[2]: {1,0,0,0,[{0,102}],error}

Stack 
-----



Mailbox
-------


Monitors
--------


**End Of Crash Report**
Return value: error

クラッシュしました
Enum:sumはExAtomVMで使えないことがわかりました

おまけ 毎回実行はめんどくさい

スクリプトを作りました

test.sh
#/bin/sh
set -x
mix atomvm.packbeam
AtomVM exploring_atom_vm.avm

test.shに実行権限がある前提で

実行権限の与え方 chmod u+x test.sh

$ ./test.sh 
++ mix atomvm.packbeam
Compiling 1 file (.ex)
++ AtomVM exploring_atom_vm.avm
Unable to open init.beam
Failed load module: init.beam
Return value: true

と楽に検証できます

検証GitHub

6
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?