0
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?

More than 1 year has passed since last update.

NimでWASMを試そうとしたら、SharedArrayBuffer が無いと言われたので

Posted at

こちらの記事 NimでWebAssemblyをやる【2021年版】 を参考に、MacBook Pro M2 Maxにて試したところ、ビルドはうまくいくもののブラウザで表示するとJavascriptにてエラーが発生。

wasm_sample.js:1 Uncaught ReferenceError: SharedArrayBuffer is not defined

いろいろと調べると、httpのレスポンスヘッダーに、

  • Cross-Origin-Embedder-Policy
  • Cross-Origin-Opener-Policy

を追加すれば良いとのこと。

なので、以下のPythonスクリプトを作成・実行することで、無事に動作しました。

http_server.py
import http.server
import socketserver
import sys

PORT = 8000
if len(sys.argv) == 2:
    PORT = int(sys.argv[1])


class CustomHandler(http.server.SimpleHTTPRequestHandler):
    def end_headers(self):
        self.send_header(
            'Cache-Control', 'no-cache, no-store, must-revalidate')
        self.send_header('Cross-Origin-Resource-Policy','cross-origin')
        self.send_header('Cross-Origin-Embedder-Policy', 'require-corp')
        self.send_header('Cross-Origin-Opener-Policy', 'same-origin')
        super().end_headers()


Handler = CustomHandler

with socketserver.TCPServer(("", PORT), Handler) as httpd:
    print("serving at port", PORT)
    httpd.serve_forever()
python http_server.py 8001

いろいろと使えそうなので、PyInstallerにてモジュール作成して、~/bin/に移動

pip install pyinstaller

pyinstller -F http_server.py
mv dist/http_server ~/bin/

使ってみる

❯ http_server 8001
serving at port 8001
127.0.0.1 - - [23/Nov/2023 13:15:02] "GET /wasm_sample.html HTTP/1.1" 200 -
127.0.0.1 - - [23/Nov/2023 13:15:02] "GET /wasm_sample.js HTTP/1.1" 200 -
127.0.0.1 - - [23/Nov/2023 13:15:02] "GET /wasm_sample.wasm HTTP/1.1" 200 -
0
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
0
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?