Repository navigation
Merge pull request #112 from goesm-dev/release/v0.0.1-beta.8
Pre-release
Pre-release
·
51 commits
to main
since this release
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:importfunction may have a body, such as{ panic("goesm:import") }, which goesm replaces with the import, so thatgo buildandgo vetaccept 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> lazyimports the module with a dynamicimport()on the first call of the function, so that a bundler splits it into a chunk of its own. The function blocks like anawaitimport (docs/js-imports.md).- A nil slice or map that Go passes to JavaScript, as an exported function's result or a
//goesm:importfunction's argument, is an empty array,Uint8Array, object orMapinstead ofnull, as JavaScript code would return, and result types no longer include| null. JavaScript may still passnullfor one. - A
jsstruct tag names a field in JavaScript ahead of itsjsontag:ID int `json:"id" js:"ID"`isIDin JavaScript, andjs:"-"leaves a field out.
syscall/js
Get,Index,LengthandCallwork on booleans, numbers and strings as property accesses do in JavaScript (js.ValueOf("abc").Length()is 3) instead of panicking.undefinedandnullstill panic with a*js.ValueError.- The new package
github.com/goesm-dev/goesm/promisewaits for a Promise:promise.Await(p)returns its value, or its rejection reason as ajs.Error, so thaterrors.Asreaches anAbortError. goesm compiles it toawait; with Go's WebAssembly port it waits withthencallbacks (docs/dom.md). - The new package
github.com/goesm-dev/goesm/jscompatgives Go code JavaScript's behaviour where Go's differs:RegExpfor patterns in JavaScript's syntax (lookaround, backreferences),Normalize(NFC, NFD, NFKC, NFKD),RoundasMath.round, the engine'sCos,Sinand otherMathfunctions, andUTF16Offset,ByteOffsetandUTF16Lenfor UTF-16 offsets such asselectionStart(docs/go-and-js.md).
Tools
goesm emit-ts -dtsalso 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 withfmtwas warned about 19 functions ofinternal/abiandinternal/bytealgit never calls.
regexp
- A constant pattern is matched with the engine's
RegExpalso 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+xor(\w+)@(\w+), and one that tries each split of a run once, such as^(\S+)\s+(.*)$orunicode-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+)+bor\d+\d+$, still keep them. The Image app of richinosan/toolbox, with its 14 patterns compiled byregexp.MustCompileinstead of its own wrapper of JSRegExp, 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
\xescape per byte. A string that asyscall/jscall takes is the text itself, with no conversion.goesm buildwrites such text as UTF-8 instead of\uescapes. 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:importfunction that returnsjs.Valuewraps 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. syscallgets the host'sfs,processandpathfrom the runtime once, andjs.Value.Getis a plain property read. Before, everyGetchecked for those three names.timekeeps 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 withtime.LoadLocation.- A call of
strconv.FormatFloatorAppendFloatwith bitSize 64 and a constant format'e','E','f','g'or'G', and ofstrconv.ParseFloatwith 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 keepsstrconv's float conversions and their tables, about 25 KB minified. fmt.Print,PrintlnandPrintf, andFprint,FprintlnandFprintftoos.Stdoutoros.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/pollandsyscall's file table.PrintlnandPrintof strings, booleans and numbers of the predeclared types are concatenated asSprintfis. A package that callsfmt.Println("n =", n)goes from 49.4 KB to 30.7 KB gzip.strings.Replaceris one JSRegExpof 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 アプリで、独自の JSRegExpの 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 になりました。