Skip to content

Merge pull request #112 from goesm-dev/release/v0.0.1-beta.8

Pre-release
Pre-release

Choose a tag to compare

@github-actions github-actions released this 21 Oct 00:25
· 51 commits to main since this release
acaf086

Tagged on 2026-10-21. Changes since v0.0.1-beta.7: v0.0.1-beta.7...v0.0.1-beta.8.

JS calling ABI

  • A //goesm:import function may have a body, such as { panic("goesm:import") }, which goesm replaces with the import, so that go build and go vet accept the package without an empty assembly file.
  • An entry whose exported functions take a pointer to a struct type of another package, whose methods return a pointer to the same type, gave that type its JS methods twice, which esbuild rejected ("The symbol ... has already been declared"). It gives them once.
  • //goesm:import "<module>" <export> lazy imports the module with a dynamic import() on the first call of the function, so that a bundler splits it into a chunk of its own. The function blocks like an await import (docs/js-imports.md).
  • A nil slice or map that Go passes to JavaScript, as an exported function's result or a //goesm:import function's argument, is an empty array, Uint8Array, object or Map instead of null, as JavaScript code would return, and result types no longer include | null. JavaScript may still pass null for one.
  • A js struct tag names a field in JavaScript ahead of its json tag: ID int `json:"id" js:"ID"` is ID in JavaScript, and js:"-" leaves a field out.

syscall/js

  • Get, Index, Length and Call work on booleans, numbers and strings as property accesses do in JavaScript (js.ValueOf("abc").Length() is 3) instead of panicking. undefined and null still panic with a *js.ValueError.
  • The new package github.com/goesm-dev/goesm/promise waits for a Promise: promise.Await(p) returns its value, or its rejection reason as a js.Error, so that errors.As reaches an AbortError. goesm compiles it to await; with Go's WebAssembly port it waits with then callbacks (docs/dom.md).
  • The new package github.com/goesm-dev/goesm/jscompat gives Go code JavaScript's behaviour where Go's differs: RegExp for patterns in JavaScript's syntax (lookaround, backreferences), Normalize (NFC, NFD, NFKC, NFKD), Round as Math.round, the engine's Cos, Sin and other Math functions, and UTF16Offset, ByteOffset and UTF16Len for UTF-16 offsets such as selectionStart (docs/go-and-js.md).

Tools

  • goesm emit-ts -dts also writes a declaration file of each built package's exports, <package>.d.ts, which imports no other file (docs/js-exports.md).
  • The warning about functions goesm cannot lower yet lists only those that code reachable from the program's roots may call, and names them without -v, which adds each one's position and reason. A program printing with fmt was warned about 19 functions of internal/abi and internal/bytealg it never calls.

regexp

  • A constant pattern is matched with the engine's RegExp also when a search by backtracking can take time quadratic in the input, instead of only linear: a pattern that can start anywhere and rescans, such as \d+x or (\w+)@(\w+), and one that tries each split of a run once, such as ^(\S+)\s+(.*)$ or unicode-range:\s*([^;]+);. A program whose patterns are all of this kind no longer keeps Go's parser and engines, about 55 KB gzip. Patterns that can take longer, such as (a+)+b or \d+\d+$, still keep them. The Image app of richinosan/toolbox, with its 14 patterns compiled by regexp.MustCompile instead of its own wrapper of JS RegExp, goes from 120.0 KB to 81.9 KB gzip (79.2 KB with the wrapper).

Bundle size

  • A string constant with non-ASCII text, such as Japanese, is a JS string literal of that text, converted to Go's bytes once when the module loads, instead of a \x escape per byte. A string that a syscall/js call takes is the text itself, with no conversion. goesm build writes such text as UTF-8 instead of \u escapes. A package of 300 Japanese messages went from 62.0 KB to 27.5 KB minified, and from 8.1 KB to 7.5 KB gzip.
  • A //goesm:import function that returns js.Value wraps the value directly, instead of going through the converter of JS values to Go values, which a program that imports only such functions no longer keeps.
  • syscall gets the host's fs, process and path from the runtime once, and js.Value.Get is a plain property read. Before, every Get checked for those three names.
  • time keeps the parser of a time zone's rule string (the TZ string after the last transition) only when the program loads time zone data, such as with time.LoadLocation.
  • A call of strconv.FormatFloat or AppendFloat with bitSize 64 and a constant format 'e', 'E', 'f', 'g' or 'G', and of strconv.ParseFloat with bitSize 64, converts with the engine's number formatting and parsing, giving Go's results (checked against Go on about 17,000 cases). A program that converts only such floats no longer keeps strconv's float conversions and their tables, about 25 KB minified.
  • fmt.Print, Println and Printf, and Fprint, Fprintln and Fprintf to os.Stdout or os.Stderr, write the formatted string to the host's standard output or error directly when the program never assigns those variables, takes their address or closes them, instead of through *os.File, internal/poll and syscall's file table. Println and Print of strings, booleans and numbers of the predeclared types are concatenated as Sprintf is. A package that calls fmt.Println("n =", n) goes from 49.4 KB to 30.7 KB gzip.
  • strings.Replacer is one JS RegExp of its old strings, with Go's order of matching, including empty old strings. A program that uses it no longer keeps Go's trie and byte replacers.

With these, the Image app of richinosan/toolbox went from 92.1 KB to 79.3 KB gzip and the Calendar app from 31.5 KB to 30.4 KB.

日本語

2026-10-21 にタグを付けました。v0.0.1-beta.7 からの差分は v0.0.1-beta.7...v0.0.1-beta.8 で確認できます。

JS calling ABI

  • //goesm:import の関数には { panic("goesm:import") } のような本体を書けるようになりました。goesm はその本体を取り込んだ関数に置き換えます。空のアセンブリファイルがなくても、go build と go vet がパッケージを受け付けます。
  • 別のパッケージの struct 型へのポインタを引数に取る関数を export し、その型のメソッドが同じ型へのポインタを返す場合、entry はその型に JS のメソッドを 2 回付けていたため、esbuild が「The symbol ... has already been declared」で拒否していました。1 回だけ付けるようになりました。
  • //goesm:import "<モジュール>" <エクスポート名> lazy は、関数を最初に呼んだ時点で動的な import() によってモジュールを読み込みます。バンドラーはそのモジュールを別のチャンクに分けます。関数は await の import と同じくブロックします (docs/js-imports.ja.md)。
  • Go から JavaScript に渡す nil のスライスとマップは、null ではなく空の配列、Uint8Array、オブジェクト、Map になりました。対象は、export した関数の戻り値と //goesm:import の関数の引数です。JavaScript のコードが返す値と同じ形であり、戻り値の型からも | null がなくなりました。JavaScript からは、これまでどおり null も渡せます。
  • js の struct タグで、JavaScript でのフィールド名を json タグより優先して指定できるようになりました。ID int `json:"id" js:"ID"` は JavaScript では ID になり、js:"-" を付けたフィールドは JavaScript に渡りません。

syscall/js

  • Get、Index、Length、Call は、真偽値、数値、文字列に対して panic せず、JavaScript のプロパティアクセスと同じように動くようになりました (js.ValueOf("abc").Length() は 3)。undefined と null に対しては、これまでどおり *js.ValueError で panic します。
  • Promise を待つパッケージ github.com/goesm-dev/goesm/promise を追加しました。promise.Await(p) は Promise の値を返し、拒否された場合は拒否の理由を js.Error として返します。そのため、errors.As で AbortError を取り出せます。goesm はこの関数を await に変換し、Go の WebAssembly 版では then のコールバックで待ちます (docs/dom.ja.md)。
  • パッケージ github.com/goesm-dev/goesm/jscompat を追加しました。Go と JavaScript で振る舞いが違う処理について、Go のコードに JavaScript と同じ振る舞いを提供します。JavaScript の構文 (先読み、後読み、後方参照) のパターンを扱う RegExp、Normalize (NFC、NFD、NFKC、NFKD)、Math.round と同じく丸める Round、エンジンの Cos、Sin などの Math の関数、selectionStart などの UTF-16 の位置を変換する UTF16Offset、ByteOffset、UTF16Len があります (docs/go-and-js.ja.md)。

ツール

  • goesm emit-ts -dts は、ビルドしたパッケージのエクスポートの宣言ファイル <パッケージ>.d.ts も書き出します。このファイルはほかのファイルを import しません (docs/js-exports.ja.md)。
  • goesm がまだ変換できない関数の警告は、プログラムの起点から到達できるコードが呼び得る関数だけを数え、-v なしでも名前を表示するようになりました。-v を付けると、それぞれの位置と理由も表示します。これまでは fmt で出力するプログラムに対して、呼ばれることのない internal/abi と internal/bytealg の 19 個の関数について警告していました。

regexp

  • 定数のパターンは、バックトラックによる検索に入力の長さの 2 乗に比例する時間がかかり得る場合も、線形時間の場合と同じく engine の RegExp で照合するようになりました。\d+x や (\w+)@(\w+) のようにどこからでも始まり同じ範囲を走査し直すパターンと、^(\S+)\s+(.*)$ や unicode-range:\s*([^;]+); のように並びの分け方を 1 回ずつ試すパターンが該当します。こうしたパターンだけを使うプログラムには、Go の parser とエンジン (gzip 後約 55 KB) が残らなくなりました。(a+)+b や \d+\d+$ のようにさらに時間がかかり得るパターンでは、これまでどおり残ります。richinosan/toolbox の Image アプリで、独自の JS RegExp の wrapper の代わりに 14 個のパターンを regexp.MustCompile でコンパイルすると、gzip 後の大きさは 120.0 KB から 81.9 KB になります (wrapper では 79.2 KB)。

バンドルサイズ

  • 日本語など ASCII 以外の文字を含む文字列定数は、1 バイトごとの \x エスケープではなく、その文字列の JS の文字列 literal になりました。Go のバイト列への変換は module を読み込むときに 1 回だけ行います。syscall/js の呼び出しに渡す文字列は変換せずにそのまま渡します。goesm build はこうした文字を \u エスケープではなく UTF-8 で書き出します。日本語のメッセージを 300 件持つパッケージは、minify 後 62.0 KB から 27.5 KB に、gzip 後 8.1 KB から 7.5 KB になりました。
  • js.Value を返す //goesm:import 関数は、JS の値を Go の値に変換する converter を通さずに値を直接包むようになりました。そうした関数だけを import するプログラムに converter が残らなくなりました。
  • syscall は host の fs、process、path を runtime から 1 回だけ受け取り、js.Value.Get は単なるプロパティの読み出しになりました。これまでは Get のたびにこの 3 つの名前を確認していました。
  • time は、time.LoadLocation などでタイムゾーンのデータを読み込むプログラムにだけ、タイムゾーンの規則の文字列 (最後の遷移より後の TZ 文字列) の parser を残すようになりました。
  • bitSize が 64 で書式が定数の 'e'、'E'、'f'、'g'、'G' である strconv.FormatFloat と AppendFloat の呼び出し、および bitSize が 64 の strconv.ParseFloat の呼び出しは、engine の数値の書式化と解析で変換し、Go と同じ結果を返すようになりました (約 17,000 件で Go と比較しました)。こうした float だけを変換するプログラムに、strconv の float 変換とその表 (minify 後約 25 KB) が残らなくなりました。
  • fmt.Print、Println、Printf と、os.Stdout か os.Stderr に書く Fprint、Fprintln、Fprintf は、プログラムがこれらの変数に代入せず、アドレスも取らず、閉じもしない場合、*os.File、internal/poll、syscall のファイルの表を通さずに、書式化した文字列を host の標準出力か標準エラーに直接書くようになりました。文字列、bool、事前宣言された型の数値の Println と Print は、Sprintf と同じく文字列の連結になります。fmt.Println("n =", n) を呼ぶパッケージは gzip 後 49.4 KB から 30.7 KB になりました。
  • strings.Replacer は、古い文字列を並べた 1 つの JS の RegExp になりました。空の古い文字列を含め、照合の順序は Go と同じです。これを使うプログラムに Go の trie と byte の replacer が残らなくなりました。

これらにより、richinosan/toolbox の Image アプリは gzip 後 92.1 KB から 79.3 KB に、Calendar アプリは 31.5 KB から 30.4 KB になりました。