dreamworkAPI
Ctrl K
GitHub
classDocs / WASM

WASM

classclientmenusource
Object → 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

NameTypeDescription
exportstable<string, async fun( ...: any ): ...: any>Async callable proxy: wasm.exports.add( 2, 3 ) behaves like wasm:call( "add", 2, 3 ).
clientWebClient | nilThe 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.

#WASM:__isvalid

clientmenunodiscardsource
function WASM:__isvalid(): boolean

Returns true while the hosting client is alive.

Returns
TypeNameDescription
booleanis_valid

#WASM:await

clientmenuasyncsource
async function WASM:await(): WASM

Waits until the module is instantiated; throws a WASMError if compiling or linking failed.

Returns
TypeNameDescription
WASMself

#WASM:call

clientmenuasyncsource
async function WASM:call(name: string, ...: any): any

Calls the exported function name and waits for its results.

lua
futures.run( function()
    print( wasm:call( "add", 2, 3 ) ) -- 5
end )
Parameters
NameTypeDescription
namestringThe export name.
...anyArguments: numbers, BigIntegers (i64), strings or booleans.
Returns
TypeNameDescription
any...The results, multi-value returns are unpacked.

#WASM:close

clientmenusource
function WASM:close()

Destroys the hosting client, pending requests fail with a WASMError.

#WASM:getExport

clientmenunodiscardsource
function WASM:getExport(name: string): WASM.Export | nil

Returns the description of the export name, nil if missing or not ready yet.

Parameters
NameTypeDescription
namestring
Returns
TypeNameDescription
WASM.Export | nilexport

#WASM:getExports

clientmenunodiscardsource
function WASM:getExports(): WASM.Export[] | nil

Returns the module exports, nil until ready. Do not modify the returned tables.

Returns
TypeNameDescription
WASM.Export[] | nilexports

#WASM:getGlobal

clientmenuasyncsource
async function WASM:getGlobal(name: string): number | BigInteger

Returns the value of the exported global name.

Parameters
NameTypeDescription
namestringThe global export name.
Returns
TypeNameDescription
number | BigIntegervalue

#WASM:getLoadFuture

clientmenunodiscardsource
function WASM:getLoadFuture(): Future<boolean>

Returns the Future resolved once the module is instantiated, failed with a WASMError on compile / link errors.

Returns
TypeNameDescription
Future<boolean>future

#WASM:getMemorySize

clientmenuasyncsource
async function WASM:getMemorySize(memory?: string): integer

Returns the linear memory size in bytes.

Parameters
NameTypeDescription
memoryoptionalstringThe memory export name, defaults to the first exported memory.
Returns
TypeNameDescription
integersize

#WASM:growMemory

clientmenuasyncsource
async function WASM:growMemory(pages: integer, memory?: string): integer

Grows linear memory by pages 64 KiB pages, returns the previous size in pages.

Parameters
NameTypeDescription
pagesintegerThe number of pages to add.
memoryoptionalstringThe memory export name, defaults to the first exported memory.
Returns
TypeNameDescription
integerprevious_pages

#WASM:invoke

clientmenusource
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
NameTypeDescription
namestringThe export name.
...anyArguments: numbers, BigIntegers (i64), strings or booleans.
Returns
TypeNameDescription
Futurefuture

#WASM:isReady

clientmenunodiscardsource
function WASM:isReady(): boolean

Returns true once the module is compiled and instantiated.

Returns
TypeNameDescription
booleanis_ready

#WASM:read

clientmenuasyncsource
async function WASM:read(offset: integer, length: integer, memory?: string): string

Reads length bytes of linear memory starting at offset.

Parameters
NameTypeDescription
offsetintegerThe byte offset.
lengthintegerThe number of bytes.
memoryoptionalstringThe memory export name, defaults to the first exported memory.
Returns
TypeNameDescription
stringdata

#WASM:readFuture

clientmenusource
function WASM:readFuture(offset: integer, length: integer, memory?: string): Future<string>

Reads length bytes of linear memory starting at offset.

Parameters
NameTypeDescription
offsetintegerThe byte offset.
lengthintegerThe number of bytes.
memoryoptionalstringThe memory export name, defaults to the first exported memory.
Returns
TypeNameDescription
Future<string>future

#WASM:setGlobal

clientmenuasyncsource
async function WASM:setGlobal(name: string, value: number | BigInteger)

Sets the value of the exported mutable global name.

Parameters
NameTypeDescription
namestringThe global export name.
valuenumber | BigIntegerThe new value.

#WASM:write

clientmenuasyncsource
async function WASM:write(offset: integer, data: string, memory?: string)

Writes data into linear memory at offset and waits until it is written.

Parameters
NameTypeDescription
offsetintegerThe byte offset.
datastringThe bytes to write.
memoryoptionalstringThe memory export name, defaults to the first exported memory.

#WASM:writeFuture

clientmenusource
function WASM:writeFuture(offset: integer, data: string, memory?: string): Future

Writes data into linear memory at offset.

Parameters
NameTypeDescription
offsetintegerThe byte offset.
datastringThe bytes to write.
memoryoptionalstringThe memory export name, defaults to the first exported memory.
Returns
TypeNameDescription
Futurefuture
Generated from the dreamwork meta branch.