From 2b90ae3210a31236f0498a02cd85cf4571da29a6 Mon Sep 17 00:00:00 2001 From: ceifa Date: Thu, 19 Nov 2026 22:50:03 -0300 Subject: [PATCH] build the bindings through locals the minifier can shorten The generated constructor spelled out this.exported( 147 times and this.wrapWithStringArguments( 37 times, and a minifier has to leave a method name alone. Two locals in the constructor take their place, so the named stores stay as they were and dist/index.js goes from 113.4 kB to 110.7 kB. The README size table was stale (108 + 194) and now carries the measured numbers. --- README.md | 4 +- src/bindings.gen.ts | 283 ++++++++++++++++++++------------------- utils/create-bindings.js | 9 +- 3 files changed, 153 insertions(+), 143 deletions(-) diff --git a/README.md b/README.md index d32cb3e..e42558d 100644 --- a/README.md +++ b/README.md @@ -285,8 +285,8 @@ Both minified, and wasmoon counted as its JS plus `glue.wasm`: | | wasmoon | fengari | | ----------- | ----------------- | ------- | -| **plain** | 302kB (108 + 194) | 228kB | -| **gzipped** | 128kB (34 + 94) | 74kB | +| **plain** | 305kB (111 + 194) | 228kB | +| **gzipped** | 129kB (35 + 94) | 74kB | Almost all of wasmoon's weight is the wasm, which is a separate file: it is fetched in parallel with your JS rather than parsed as part of it, and it caches on its own across releases of your app. diff --git a/src/bindings.gen.ts b/src/bindings.gen.ts index 3bca714..b1e8b52 100644 --- a/src/bindings.gen.ts +++ b/src/bindings.gen.ts @@ -163,150 +163,155 @@ export default abstract class LuaBindings { protected constructor(emscripten: LuaEmscriptenModule) { this.emscripten = emscripten - this.luaL_checkversion_ = this.exported('luaL_checkversion_') - this.luaL_getmetafield = this.wrapWithStringArguments(this.exported('luaL_getmetafield'), 'nnns') - this.luaL_callmeta = this.wrapWithStringArguments(this.exported('luaL_callmeta'), 'nnns') - this.luaL_argerror = this.wrapWithStringArguments(this.exported('luaL_argerror'), 'nnns') - this.luaL_typeerror = this.wrapWithStringArguments(this.exported('luaL_typeerror'), 'nnns') - this.luaL_checklstring = this.wrapWithStringArguments(this.exported('luaL_checklstring'), 'snnn') - this.luaL_optlstring = this.wrapWithStringArguments(this.exported('luaL_optlstring'), 'snnsn') - this.luaL_checknumber = this.exported('luaL_checknumber') - this.luaL_optnumber = this.exported('luaL_optnumber') - this.luaL_checkinteger = this.exported('luaL_checkinteger') - this.luaL_optinteger = this.exported('luaL_optinteger') - this.luaL_checkstack = this.wrapWithStringArguments(this.exported('luaL_checkstack'), 'nnns') - this.luaL_checktype = this.exported('luaL_checktype') - this.luaL_checkany = this.exported('luaL_checkany') - this.luaL_newmetatable = this.wrapWithStringArguments(this.exported('luaL_newmetatable'), 'nns') - this.luaL_setmetatable = this.wrapWithStringArguments(this.exported('luaL_setmetatable'), 'nns') - this.luaL_testudata = this.wrapWithStringArguments(this.exported('luaL_testudata'), 'nnns') - this.luaL_checkudata = this.wrapWithStringArguments(this.exported('luaL_checkudata'), 'nnns') - this.luaL_where = this.exported('luaL_where') - this.luaL_fileresult = this.wrapWithStringArguments(this.exported('luaL_fileresult'), 'nnns') - this.luaL_execresult = this.exported('luaL_execresult') - this.luaL_ref = this.exported('luaL_ref') - this.luaL_unref = this.exported('luaL_unref') - this.luaL_loadfilex = this.wrapWithStringArguments(this.exported('luaL_loadfilex'), 'nnss') - this.luaL_loadbufferx = this.wrapWithStringArguments(this.exported('luaL_loadbufferx'), 'nnSnSs') - this.luaL_loadstring = this.wrapWithStringArguments(this.exported('luaL_loadstring'), 'nns') - this.luaL_newstate = this.exported('luaL_newstate') - this.luaL_len = this.exported('luaL_len') - this.luaL_addgsub = this.wrapWithStringArguments(this.exported('luaL_addgsub'), 'nnsss') - this.luaL_gsub = this.wrapWithStringArguments(this.exported('luaL_gsub'), 'snsss') - this.luaL_setfuncs = this.exported('luaL_setfuncs') - this.luaL_getsubtable = this.wrapWithStringArguments(this.exported('luaL_getsubtable'), 'nnns') - this.luaL_traceback = this.wrapWithStringArguments(this.exported('luaL_traceback'), 'nnnsn') - this.luaL_requiref = this.wrapWithStringArguments(this.exported('luaL_requiref'), 'nnsnn') - this.luaL_buffinit = this.exported('luaL_buffinit') - this.luaL_prepbuffsize = this.wrapWithStringArguments(this.exported('luaL_prepbuffsize'), 'snn') - this.luaL_addlstring = this.wrapWithStringArguments(this.exported('luaL_addlstring'), 'nnsn') - this.luaL_addstring = this.wrapWithStringArguments(this.exported('luaL_addstring'), 'nns') - this.luaL_addvalue = this.exported('luaL_addvalue') - this.luaL_pushresult = this.exported('luaL_pushresult') - this.luaL_pushresultsize = this.exported('luaL_pushresultsize') - this.luaL_buffinitsize = this.wrapWithStringArguments(this.exported('luaL_buffinitsize'), 'snnn') - this.lua_newstate = this.exported('lua_newstate') - this.lua_close = this.exported('lua_close') - this.lua_newthread = this.exported('lua_newthread') - this.lua_closethread = this.exported('lua_closethread') - this.lua_atpanic = this.exported('lua_atpanic') - this.lua_version = this.exported('lua_version') - this.lua_absindex = this.exported('lua_absindex') - this.lua_gettop = this.exported('lua_gettop') - this.lua_settop = this.exported('lua_settop') - this.lua_pushvalue = this.exported('lua_pushvalue') - this.lua_rotate = this.exported('lua_rotate') - this.lua_copy = this.exported('lua_copy') - this.lua_checkstack = this.exported('lua_checkstack') - this.lua_xmove = this.exported('lua_xmove') - this.lua_isnumber = this.exported('lua_isnumber') - this.lua_isstring = this.exported('lua_isstring') - this.lua_iscfunction = this.exported('lua_iscfunction') - this.lua_isinteger = this.exported('lua_isinteger') - this.lua_isuserdata = this.exported('lua_isuserdata') - this.lua_type = this.exported('lua_type') - this.lua_typename = this.wrapWithStringArguments(this.exported('lua_typename'), 'snn') - this.lua_tonumberx = this.exported('lua_tonumberx') - this.lua_tointegerx = this.exported('lua_tointegerx') - this.lua_toboolean = this.exported('lua_toboolean') - this.lua_rawlen = this.exported('lua_rawlen') - this.lua_tocfunction = this.exported('lua_tocfunction') - this.lua_touserdata = this.exported('lua_touserdata') - this.lua_tothread = this.exported('lua_tothread') - this.lua_topointer = this.exported('lua_topointer') - this.lua_arith = this.exported('lua_arith') - this.lua_rawequal = this.exported('lua_rawequal') - this.lua_compare = this.exported('lua_compare') - this.lua_pushnil = this.exported('lua_pushnil') - this.lua_pushnumber = this.exported('lua_pushnumber') + // Locals rather than the methods themselves, which nearly every line below would otherwise + // spell out in full: a minifier shortens a local and has to leave a method name alone. + const raw = (name: string): Binding => this.exported(name) + const withStrings = (name: string, signature: string): Binding => this.wrapWithStringArguments(raw(name), signature) + + this.luaL_checkversion_ = raw('luaL_checkversion_') + this.luaL_getmetafield = withStrings('luaL_getmetafield', 'nnns') + this.luaL_callmeta = withStrings('luaL_callmeta', 'nnns') + this.luaL_argerror = withStrings('luaL_argerror', 'nnns') + this.luaL_typeerror = withStrings('luaL_typeerror', 'nnns') + this.luaL_checklstring = withStrings('luaL_checklstring', 'snnn') + this.luaL_optlstring = withStrings('luaL_optlstring', 'snnsn') + this.luaL_checknumber = raw('luaL_checknumber') + this.luaL_optnumber = raw('luaL_optnumber') + this.luaL_checkinteger = raw('luaL_checkinteger') + this.luaL_optinteger = raw('luaL_optinteger') + this.luaL_checkstack = withStrings('luaL_checkstack', 'nnns') + this.luaL_checktype = raw('luaL_checktype') + this.luaL_checkany = raw('luaL_checkany') + this.luaL_newmetatable = withStrings('luaL_newmetatable', 'nns') + this.luaL_setmetatable = withStrings('luaL_setmetatable', 'nns') + this.luaL_testudata = withStrings('luaL_testudata', 'nnns') + this.luaL_checkudata = withStrings('luaL_checkudata', 'nnns') + this.luaL_where = raw('luaL_where') + this.luaL_fileresult = withStrings('luaL_fileresult', 'nnns') + this.luaL_execresult = raw('luaL_execresult') + this.luaL_ref = raw('luaL_ref') + this.luaL_unref = raw('luaL_unref') + this.luaL_loadfilex = withStrings('luaL_loadfilex', 'nnss') + this.luaL_loadbufferx = withStrings('luaL_loadbufferx', 'nnSnSs') + this.luaL_loadstring = withStrings('luaL_loadstring', 'nns') + this.luaL_newstate = raw('luaL_newstate') + this.luaL_len = raw('luaL_len') + this.luaL_addgsub = withStrings('luaL_addgsub', 'nnsss') + this.luaL_gsub = withStrings('luaL_gsub', 'snsss') + this.luaL_setfuncs = raw('luaL_setfuncs') + this.luaL_getsubtable = withStrings('luaL_getsubtable', 'nnns') + this.luaL_traceback = withStrings('luaL_traceback', 'nnnsn') + this.luaL_requiref = withStrings('luaL_requiref', 'nnsnn') + this.luaL_buffinit = raw('luaL_buffinit') + this.luaL_prepbuffsize = withStrings('luaL_prepbuffsize', 'snn') + this.luaL_addlstring = withStrings('luaL_addlstring', 'nnsn') + this.luaL_addstring = withStrings('luaL_addstring', 'nns') + this.luaL_addvalue = raw('luaL_addvalue') + this.luaL_pushresult = raw('luaL_pushresult') + this.luaL_pushresultsize = raw('luaL_pushresultsize') + this.luaL_buffinitsize = withStrings('luaL_buffinitsize', 'snnn') + this.lua_newstate = raw('lua_newstate') + this.lua_close = raw('lua_close') + this.lua_newthread = raw('lua_newthread') + this.lua_closethread = raw('lua_closethread') + this.lua_atpanic = raw('lua_atpanic') + this.lua_version = raw('lua_version') + this.lua_absindex = raw('lua_absindex') + this.lua_gettop = raw('lua_gettop') + this.lua_settop = raw('lua_settop') + this.lua_pushvalue = raw('lua_pushvalue') + this.lua_rotate = raw('lua_rotate') + this.lua_copy = raw('lua_copy') + this.lua_checkstack = raw('lua_checkstack') + this.lua_xmove = raw('lua_xmove') + this.lua_isnumber = raw('lua_isnumber') + this.lua_isstring = raw('lua_isstring') + this.lua_iscfunction = raw('lua_iscfunction') + this.lua_isinteger = raw('lua_isinteger') + this.lua_isuserdata = raw('lua_isuserdata') + this.lua_type = raw('lua_type') + this.lua_typename = withStrings('lua_typename', 'snn') + this.lua_tonumberx = raw('lua_tonumberx') + this.lua_tointegerx = raw('lua_tointegerx') + this.lua_toboolean = raw('lua_toboolean') + this.lua_rawlen = raw('lua_rawlen') + this.lua_tocfunction = raw('lua_tocfunction') + this.lua_touserdata = raw('lua_touserdata') + this.lua_tothread = raw('lua_tothread') + this.lua_topointer = raw('lua_topointer') + this.lua_arith = raw('lua_arith') + this.lua_rawequal = raw('lua_rawequal') + this.lua_compare = raw('lua_compare') + this.lua_pushnil = raw('lua_pushnil') + this.lua_pushnumber = raw('lua_pushnumber') this.lua_pushinteger = this.withIntegerArgument('lua_pushinteger', true) - this.lua_pushlstring = this.exported('lua_pushlstring') - this.lua_pushcclosure = this.exported('lua_pushcclosure') - this.lua_pushboolean = this.exported('lua_pushboolean') - this.lua_pushlightuserdata = this.exported('lua_pushlightuserdata') - this.lua_pushthread = this.exported('lua_pushthread') - this.lua_getglobal = this.wrapWithStringArguments(this.exported('lua_getglobal'), 'nns') - this.lua_gettable = this.exported('lua_gettable') - this.lua_getfield = this.wrapWithStringArguments(this.exported('lua_getfield'), 'nnns') + this.lua_pushlstring = raw('lua_pushlstring') + this.lua_pushcclosure = raw('lua_pushcclosure') + this.lua_pushboolean = raw('lua_pushboolean') + this.lua_pushlightuserdata = raw('lua_pushlightuserdata') + this.lua_pushthread = raw('lua_pushthread') + this.lua_getglobal = withStrings('lua_getglobal', 'nns') + this.lua_gettable = raw('lua_gettable') + this.lua_getfield = withStrings('lua_getfield', 'nnns') this.lua_geti = this.withIntegerArgument('lua_geti') - this.lua_rawget = this.exported('lua_rawget') + this.lua_rawget = raw('lua_rawget') this.lua_rawgeti = this.withIntegerArgument('lua_rawgeti') - this.lua_rawgetp = this.exported('lua_rawgetp') - this.lua_createtable = this.exported('lua_createtable') - this.lua_newuserdatauv = this.exported('lua_newuserdatauv') - this.lua_getmetatable = this.exported('lua_getmetatable') - this.lua_getiuservalue = this.exported('lua_getiuservalue') - this.lua_setglobal = this.wrapWithStringArguments(this.exported('lua_setglobal'), 'nns') - this.lua_settable = this.exported('lua_settable') - this.lua_setfield = this.wrapWithStringArguments(this.exported('lua_setfield'), 'nnns') + this.lua_rawgetp = raw('lua_rawgetp') + this.lua_createtable = raw('lua_createtable') + this.lua_newuserdatauv = raw('lua_newuserdatauv') + this.lua_getmetatable = raw('lua_getmetatable') + this.lua_getiuservalue = raw('lua_getiuservalue') + this.lua_setglobal = withStrings('lua_setglobal', 'nns') + this.lua_settable = raw('lua_settable') + this.lua_setfield = withStrings('lua_setfield', 'nnns') this.lua_seti = this.withIntegerArgument('lua_seti') - this.lua_rawset = this.exported('lua_rawset') + this.lua_rawset = raw('lua_rawset') this.lua_rawseti = this.withIntegerArgument('lua_rawseti') - this.lua_rawsetp = this.exported('lua_rawsetp') - this.lua_setmetatable = this.exported('lua_setmetatable') - this.lua_setiuservalue = this.exported('lua_setiuservalue') - this.lua_callk = this.exported('lua_callk') - this.lua_pcallk = this.exported('lua_pcallk') - this.lua_load = this.wrapWithStringArguments(this.exported('lua_load'), 'nnnnss') - this.lua_dump = this.exported('lua_dump') - this.lua_yieldk = this.exported('lua_yieldk') - this.lua_resume = this.exported('lua_resume') - this.lua_status = this.exported('lua_status') - this.lua_isyieldable = this.exported('lua_isyieldable') - this.lua_setwarnf = this.exported('lua_setwarnf') - this.lua_warning = this.wrapWithStringArguments(this.exported('lua_warning'), 'nnsn') - this.lua_error = this.exported('lua_error') - this.lua_next = this.exported('lua_next') - this.lua_concat = this.exported('lua_concat') - this.lua_len = this.exported('lua_len') - this.lua_stringtonumber = this.wrapWithStringArguments(this.exported('lua_stringtonumber'), 'nns') - this.lua_getallocf = this.exported('lua_getallocf') - this.lua_setallocf = this.exported('lua_setallocf') - this.lua_toclose = this.exported('lua_toclose') - this.lua_closeslot = this.exported('lua_closeslot') - this.lua_getstack = this.exported('lua_getstack') - this.lua_getinfo = this.wrapWithStringArguments(this.exported('lua_getinfo'), 'nnsn') - this.lua_getlocal = this.wrapWithStringArguments(this.exported('lua_getlocal'), 'snnn') - this.lua_setlocal = this.wrapWithStringArguments(this.exported('lua_setlocal'), 'snnn') - this.lua_getupvalue = this.wrapWithStringArguments(this.exported('lua_getupvalue'), 'snnn') - this.lua_setupvalue = this.wrapWithStringArguments(this.exported('lua_setupvalue'), 'snnn') - this.lua_upvalueid = this.exported('lua_upvalueid') - this.lua_upvaluejoin = this.exported('lua_upvaluejoin') - this.lua_sethook = this.exported('lua_sethook') - this.lua_gethook = this.exported('lua_gethook') - this.lua_gethookmask = this.exported('lua_gethookmask') - this.lua_gethookcount = this.exported('lua_gethookcount') - this.luaopen_base = this.exported('luaopen_base') - this.luaopen_package = this.exported('luaopen_package') - this.luaopen_coroutine = this.exported('luaopen_coroutine') - this.luaopen_debug = this.exported('luaopen_debug') - this.luaopen_io = this.exported('luaopen_io') - this.luaopen_math = this.exported('luaopen_math') - this.luaopen_os = this.exported('luaopen_os') - this.luaopen_string = this.exported('luaopen_string') - this.luaopen_table = this.exported('luaopen_table') - this.luaopen_utf8 = this.exported('luaopen_utf8') - this.luaL_openselectedlibs = this.exported('luaL_openselectedlibs') + this.lua_rawsetp = raw('lua_rawsetp') + this.lua_setmetatable = raw('lua_setmetatable') + this.lua_setiuservalue = raw('lua_setiuservalue') + this.lua_callk = raw('lua_callk') + this.lua_pcallk = raw('lua_pcallk') + this.lua_load = withStrings('lua_load', 'nnnnss') + this.lua_dump = raw('lua_dump') + this.lua_yieldk = raw('lua_yieldk') + this.lua_resume = raw('lua_resume') + this.lua_status = raw('lua_status') + this.lua_isyieldable = raw('lua_isyieldable') + this.lua_setwarnf = raw('lua_setwarnf') + this.lua_warning = withStrings('lua_warning', 'nnsn') + this.lua_error = raw('lua_error') + this.lua_next = raw('lua_next') + this.lua_concat = raw('lua_concat') + this.lua_len = raw('lua_len') + this.lua_stringtonumber = withStrings('lua_stringtonumber', 'nns') + this.lua_getallocf = raw('lua_getallocf') + this.lua_setallocf = raw('lua_setallocf') + this.lua_toclose = raw('lua_toclose') + this.lua_closeslot = raw('lua_closeslot') + this.lua_getstack = raw('lua_getstack') + this.lua_getinfo = withStrings('lua_getinfo', 'nnsn') + this.lua_getlocal = withStrings('lua_getlocal', 'snnn') + this.lua_setlocal = withStrings('lua_setlocal', 'snnn') + this.lua_getupvalue = withStrings('lua_getupvalue', 'snnn') + this.lua_setupvalue = withStrings('lua_setupvalue', 'snnn') + this.lua_upvalueid = raw('lua_upvalueid') + this.lua_upvaluejoin = raw('lua_upvaluejoin') + this.lua_sethook = raw('lua_sethook') + this.lua_gethook = raw('lua_gethook') + this.lua_gethookmask = raw('lua_gethookmask') + this.lua_gethookcount = raw('lua_gethookcount') + this.luaopen_base = raw('luaopen_base') + this.luaopen_package = raw('luaopen_package') + this.luaopen_coroutine = raw('luaopen_coroutine') + this.luaopen_debug = raw('luaopen_debug') + this.luaopen_io = raw('luaopen_io') + this.luaopen_math = raw('luaopen_math') + this.luaopen_os = raw('luaopen_os') + this.luaopen_string = raw('luaopen_string') + this.luaopen_table = raw('luaopen_table') + this.luaopen_utf8 = raw('luaopen_utf8') + this.luaL_openselectedlibs = raw('luaL_openselectedlibs') } /** The wasm export `name`. */ diff --git a/utils/create-bindings.js b/utils/create-bindings.js index bbe41b8..03a0ae4 100644 --- a/utils/create-bindings.js +++ b/utils/create-bindings.js @@ -163,9 +163,9 @@ for (const fn of bound) { if (integerTwin) { assignments.push(` this.${fn.name} = this.withIntegerArgument('${fn.name}'${fn.params.length === 2 ? ', true' : ''})`) } else if (signature.includes('s') && !override.raw) { - assignments.push(` this.${fn.name} = this.wrapWithStringArguments(this.exported('${fn.name}'), '${signature}')`) + assignments.push(` this.${fn.name} = withStrings('${fn.name}', '${signature}')`) } else { - assignments.push(` this.${fn.name} = this.exported('${fn.name}')`) + assignments.push(` this.${fn.name} = raw('${fn.name}')`) } } @@ -191,6 +191,11 @@ ${declarations.join('\n')} protected constructor(emscripten: LuaEmscriptenModule) { this.emscripten = emscripten + // Locals rather than the methods themselves, which nearly every line below would otherwise + // spell out in full: a minifier shortens a local and has to leave a method name alone. + const raw = (name: string): Binding => this.exported(name) + const withStrings = (name: string, signature: string): Binding => this.wrapWithStringArguments(raw(name), signature) + ${assignments.join('\n')} }