WASM
A WebAssembly module instantiated inside a headless WebClient (Chromium).
Every call crosses the CEF bridge, which is asynchronous: methods marked @async must run inside a coroutine (futures.run ), the invoke / *Future style methods return a Future for plain callbacks.
Values: i32 / f32 / f64 are Lua numbers, i64 is a BigInteger (integers and decimal strings are accepted as arguments). Multi-value results come back as an array.
Imports are either JavaScript expressions (evaluated in the page) or Lua functions; Lua imports are fire-and-forget, the bridge cannot return a value to WebAssembly synchronously, so they act as undefined (0 for numeric results, a trap for i64).
Requires the x86-64 (Chromium) branch of Garry's Mod, Awesomium has no WebAssembly.
Constructor WASM (binary : string , imports : WASM.Imports ?): WASM Fields Name Type Description exportstable <string , async fun ( ...: any ): ...: any >Async callable proxy: wasm.exports.add( 2, 3 ) behaves like wasm:call( "add", 2, 3 ). clientWebClient | nil The hosting WebClient , nil once closed.
Methods __isvalidReturns true while the hosting client is alive. awaitWaits until the module is instantiated; throws a WASMError if compiling or linking failed. callCalls the exported function name and waits for its results. closeDestroys the hosting client, pending requests fail with a WASMError. getExportReturns the description of the export name, nil if missing or not ready yet. getExportsReturns the module exports, nil until ready. getGlobalReturns the value of the exported global name. getLoadFutureReturns the Future resolved once the module is instantiated, failed with a WASMError on compile / link errors. getMemorySizeReturns the linear memory size in bytes. growMemoryGrows linear memory by pages 64 KiB pages, returns the previous size in pages. invokeCalls the exported function name, returning a Future with its result (nil, the single value, or an array for multi-valu… isReadyReturns true once the module is compiled and instantiated. readReads length bytes of linear memory starting at offset. readFutureReads length bytes of linear memory starting at offset. setGlobalSets the value of the exported mutable global name. writeWrites data into linear memory at offset and waits until it is written. writeFutureWrites data into linear memory at offset.
function WASM:__isvalid (): boolean
Returns true while the hosting client is alive.
Returns
Type Name Description boolean is_valid
async function WASM:await (): WASM
Waits until the module is instantiated; throws a WASMError if compiling or linking failed.
Returns
Type Name Description WASM self
async function WASM:call (name : string , ... : any ): any
Calls the exported function name and waits for its results.
lua Copy
futures.run ( function ()
print ( wasm:call ( "add" , 2 , 3 ) ) -- 5
end )
Parameters
Name Type Description namestring The export name. ...any Arguments: numbers, BigInteger s (i64), strings or booleans.
Returns
Type Name Description any ...The results, multi-value returns are unpacked.
function WASM:close ()
Destroys the hosting client, pending requests fail with a WASMError .
function WASM:getExport (name : string ): WASM.Export | nil
Returns the description of the export name, nil if missing or not ready yet.
Parameters
Name Type Description namestring
Returns
function WASM:getExports (): WASM.Export[] | nil
Returns the module exports, nil until ready. Do not modify the returned tables.
Returns
async function WASM:getGlobal (name : string ): number | BigInteger
Returns the value of the exported global name.
Parameters
Name Type Description namestring The global export name.
Returns
function WASM:getLoadFuture (): Future <boolean >
Returns the Future resolved once the module is instantiated, failed with a WASMError on compile / link errors.
Returns
Type Name Description Future <boolean >future
async function WASM:getMemorySize (memory? : string ): integer
Returns the linear memory size in bytes.
Parameters
Name Type Description memoryoptional string The memory export name, defaults to the first exported memory.
Returns
Type Name Description integer size
async function WASM:growMemory (pages : integer , memory? : string ): integer
Grows linear memory by pages 64 KiB pages, returns the previous size in pages.
Parameters
Name Type Description pagesinteger The number of pages to add. memoryoptional string The memory export name, defaults to the first exported memory.
Returns
Type Name Description integer previous_pages
function WASM:invoke (name : string , ... : any ): Future
Calls the exported function name, returning a Future with its result (nil, the single value, or an array for multi-value returns).
Calls made before the module is ready are queued in the page.
Parameters
Name Type Description namestring The export name. ...any Arguments: numbers, BigInteger s (i64), strings or booleans.
Returns
Type Name Description Future future
function WASM:isReady (): boolean
Returns true once the module is compiled and instantiated.
Returns
Type Name Description boolean is_ready
async function WASM:read (offset : integer , length : integer , memory? : string ): string
Reads length bytes of linear memory starting at offset.
Parameters
Name Type Description offsetinteger The byte offset. lengthinteger The number of bytes. memoryoptional string The memory export name, defaults to the first exported memory.
Returns
Type Name Description string data
function WASM:readFuture (offset : integer , length : integer , memory? : string ): Future <string >
Reads length bytes of linear memory starting at offset.
Parameters
Name Type Description offsetinteger The byte offset. lengthinteger The number of bytes. memoryoptional string The memory export name, defaults to the first exported memory.
Returns
Type Name Description Future <string >future
async function WASM:setGlobal (name : string , value : number | BigInteger )
Sets the value of the exported mutable global name.
Parameters
Name Type Description namestring The global export name. valuenumber | BigInteger The new value.
async function WASM:write (offset : integer , data : string , memory? : string )
Writes data into linear memory at offset and waits until it is written.
Parameters
Name Type Description offsetinteger The byte offset. datastring The bytes to write. memoryoptional string The memory export name, defaults to the first exported memory.
function WASM:writeFuture (offset : integer , data : string , memory? : string ): Future
Writes data into linear memory at offset.
Parameters
Name Type Description offsetinteger The byte offset. datastring The bytes to write. memoryoptional string The memory export name, defaults to the first exported memory.
Returns
Type Name Description Future future