Base Framework
Classes | Public Types | Public Member Functions | Static Public Member Functions | List of all members
WebAssembly Class Reference

#include <base/webassembly/WebAssembly.h>

Inherits Object.

Classes

class  Arguments
 
class  FunctionContext
 
class  FunctionType
 
class  MapType
 
class  MapType< bool >
 
class  MapType< const char * >
 
class  MapType< const TYPE * >
 
class  MapType< const wchar * >
 
class  MapType< double >
 
class  MapType< float >
 
class  MapType< int >
 
class  MapType< int64 >
 
class  MapType< long double >
 
class  MapType< short >
 
class  MapType< String >
 
class  MapType< TYPE * >
 
class  MapType< uint64 >
 
class  MapType< unsigned char >
 
class  MapType< unsigned int >
 
class  MapType< unsigned short >
 
class  MapType< void >
 
class  MapType< WideString >
 
class  Module
 
class  OpaqueContext
 
class  Symbol
 
class  WASMCallback
 
class  WASMValue
 
class  WebAssemblyException
 

Public Types

enum  Type {
  TYPE_UNSPECIFIED, TYPE_FUNCTION, TYPE_i32, TYPE_i64,
  TYPE_f32, TYPE_f64
}
 
enum  ExternType { EXTERN_FUNCTION, EXTERN_GLOBAL, EXTERN_TABLE, EXTERN_MEMORY }
 
enum  Format { FORMAT_UNSPECIFIED, FORMAT_WAT, FORMAT_WASM }
 
enum  WASMABI { WASM32, WASM64 }
 
typedef void(* WASMFunction) (AnyReference context, WebAssembly &wasm, const WASMValue *arguments, WASMValue *results)
 

Public Member Functions

 WebAssembly ()
 
String getEngine ()
 
uint64 getProcessingTime () const
 
MemorySize getMaximumMemoryUsage () const
 
void setMaximumMemoryUsage (MemorySize size)
 
MemorySize getMemoryUsage () const
 
void setUseLog (bool useLog)
 
void garbageCollect ()
 
void registerFunction (WASMFunction func, AnyReference context, const FunctionType &type, const String &name, const String &module=String())
 
template<typename RESULT , typename... ARGS>
void registerFunction (WASMFunction func, AnyReference context, RESULT(*functype)(ARGS...), const String &name, const String &module=String())
 
void registerCallback (R< WASMCallback > callack, const FunctionType &type, const String &name, const String &module=String())
 
bool loadFile (const String &path)
 
bool load (const uint8 *wasm, MemorySize size)
 
bool load (const String &path)
 
bool loadAny (const String &bytes)
 
WASMABI getWASMABI () const
 
bool isValid (const uint8 *wasm, MemorySize size)
 
bool isValid (const String &bytes)
 
void setArguments (const Array< String > &arguments)
 
void setEnvironment (const Map< String, String > &environment)
 
void setMountedFolders (const Map< String, String > &folders)
 
void setUseProfiler (bool useProfiler)
 
bool makeInstance (bool fake=false)
 
bool makeWASIInstance (InputStream *_stdin, OutputStream *_stdout, OutputStream *_stderr)
 
ConstSpan< uint8 > getMemory () const
 
Span< uint8 > getMemory ()
 
String getString (MemorySize address, MemorySize size) const
 
MemorySize getNumberOfImports () const
 
MemorySize getNumberOfExports () const
 
Array< SymbolgetImports ()
 
Array< SymbolgetExports ()
 
Symbol getSymbol (MemorySize index)
 
Symbol getSymbol (const String &name)
 
MemorySize getFunctionIndex (const String &id) const
 
void callEntry ()
 
void call (unsigned int id, const WASMValue *arguments, WASMValue *results)
 
AnyValue call (const String &id, const AnyValue *arguments, MemorySize size)
 
AnyValue call (const String &id)
 
AnyValue call (const String &id, const Array< AnyValue > &arguments)
 
AnyValue call (unsigned int id, const Array< AnyValue > &arguments)
 
WebAssemblyFunction getFunction (const String &id)
 
template<typename RESULT , typename... ARGS>
RESULT invoke (const String &id, ARGS... args)
 
FunctionType getFunctionType (unsigned int id)
 
WebAssemblyFunction getFunction (unsigned int id)
 
template<typename... ARGS>
void forward (void(*function)(ARGS...), const WASMValue *_arguments, WASMValue *results)
 
template<typename RESULT , typename... ARGS>
void forward (RESULT(*function)(ARGS...), const WASMValue *_arguments, WASMValue *results)
 
template<class TYPE , typename... ARGS>
void forward (void(*function)(ARGS...), TYPE *object, const WASMValue *_arguments, WASMValue *results)
 
template<class TYPE , typename RESULT , typename... ARGS>
void forward (RESULT(TYPE::*function)(ARGS...), TYPE *object, const WASMValue *_arguments, WASMValue *results)
 
 ~WebAssembly ()
 

Static Public Member Functions

static bool isSupported () noexcept
 
static const char * toString (Type type) noexcept
 
static String convertWATToWASM (const String &text)
 
template<typename RESULT , typename... TYPES>
static constexpr size_t getNumberOfArguments (RESULT(*f)(TYPES ...))
 
static Format getFormat (const String &bytes)
 
template<typename RESULT , typename... ARGS>
static FunctionType getFunctionType (RESULT(*)(ARGS... args))
 
static String toString (const FunctionType &functionType, const String &name=String(), const String &module=String(), bool colorize=false)
 
static String toString (const Symbol &s, bool colorize=false)
 

Detailed Description

WebAssembly integration.

Member Typedef Documentation

◆ WASMFunction

typedef void(* WebAssembly::WASMFunction) (AnyReference context, WebAssembly &wasm, const WASMValue *arguments, WASMValue *results)

Extern WASM function.

Member Enumeration Documentation

◆ Format

Enumerator
FORMAT_UNSPECIFIED 

Unknown format/any format.

FORMAT_WAT 

WAT type.

FORMAT_WASM 

WASM type.

◆ Type

Types.

◆ WASMABI

ABI type.

Enumerator
WASM32 

32-bit WASM.

WASM64 

64-bit WASM.

Constructor & Destructor Documentation

◆ WebAssembly()

WebAssembly::WebAssembly ( )

Initializes WebAssembly engine.

◆ ~WebAssembly()

WebAssembly::~WebAssembly ( )

Releases WASM handle.

Member Function Documentation

◆ call() [1/5]

AnyValue WebAssembly::call ( const String id)
inline

Calls the function with the given id and arguments.

◆ call() [2/5]

AnyValue WebAssembly::call ( const String id,
const AnyValue arguments,
MemorySize  size 
)

Calls the function with the given id and arguments.

◆ call() [3/5]

AnyValue WebAssembly::call ( const String id,
const Array< AnyValue > &  arguments 
)

Calls the function with the given id and arguments.

◆ call() [4/5]

AnyValue WebAssembly::call ( unsigned int  id,
const Array< AnyValue > &  arguments 
)

Calls the exported function with the given id and arguments.

◆ call() [5/5]

void WebAssembly::call ( unsigned int  id,
const WASMValue arguments,
WASMValue results 
)

Calls the function with the given id and arguments and results.

◆ callEntry()

void WebAssembly::callEntry ( )

Calls the entry function without arguments.

◆ convertWATToWASM()

static String WebAssembly::convertWATToWASM ( const String text)
static

Returns the WASM for the given text (WAT).

◆ garbageCollect()

void WebAssembly::garbageCollect ( )

Garbage collects any unused memory.

◆ getEngine()

String WebAssembly::getEngine ( )

Returns info about the WebAssembly engine.

◆ getExports()

Array<Symbol> WebAssembly::getExports ( )

Returns the exported symbols.

◆ getFormat()

static Format WebAssembly::getFormat ( const String bytes)
static

Returns the format for the given buffer. Only looks at header.

◆ getFunction() [1/2]

WebAssemblyFunction WebAssembly::getFunction ( const String id)

Returns function reference.

◆ getFunction() [2/2]

WebAssemblyFunction WebAssembly::getFunction ( unsigned int  id)

Returns function reference.

◆ getFunctionIndex()

MemorySize WebAssembly::getFunctionIndex ( const String id) const

Returns the function index for the given name.

◆ getFunctionType() [1/2]

template<typename RESULT , typename... ARGS>
static FunctionType WebAssembly::getFunctionType ( RESULT(*)(ARGS... args)  )
inlinestatic

Get function type from given function pointer.

◆ getFunctionType() [2/2]

FunctionType WebAssembly::getFunctionType ( unsigned int  id)

Returns function type.

◆ getImports()

Array<Symbol> WebAssembly::getImports ( )

Returns the imported symbols for the module.

◆ getMaximumMemoryUsage()

MemorySize WebAssembly::getMaximumMemoryUsage ( ) const

Returns the memory limit.

◆ getMemory() [1/2]

Span<uint8> WebAssembly::getMemory ( )

Write access memory.

◆ getMemory() [2/2]

ConstSpan<uint8> WebAssembly::getMemory ( ) const

Read access memory.

◆ getMemoryUsage()

MemorySize WebAssembly::getMemoryUsage ( ) const

Returns the current memory usage.

◆ getNumberOfArguments()

template<typename RESULT , typename... TYPES>
static constexpr size_t WebAssembly::getNumberOfArguments ( RESULT(*)(TYPES ...)  f)
inlinestaticconstexpr

Returns the number of arguments.

◆ getNumberOfExports()

MemorySize WebAssembly::getNumberOfExports ( ) const

Returns the number of exported symbols.

◆ getNumberOfImports()

MemorySize WebAssembly::getNumberOfImports ( ) const

Returns the number of imported symbols.

◆ getProcessingTime()

uint64 WebAssembly::getProcessingTime ( ) const

Returns the processing time.

◆ getString()

String WebAssembly::getString ( MemorySize  address,
MemorySize  size 
) const

Get memory as string.

◆ getSymbol() [1/2]

Symbol WebAssembly::getSymbol ( const String name)

Returns information about the given name.

◆ getSymbol() [2/2]

Symbol WebAssembly::getSymbol ( MemorySize  index)

Returns information about the given index.

◆ getWASMABI()

WASMABI WebAssembly::getWASMABI ( ) const

Returns the WASM ABI.

◆ invoke()

template<typename RESULT , typename... ARGS>
RESULT WebAssembly::invoke ( const String id,
ARGS...  args 
)
inline

Calls function with given arguments.

◆ isSupported()

static bool WebAssembly::isSupported ( )
staticnoexcept

Returns true if WASM is supported by the runtime.

◆ isValid() [1/2]

bool WebAssembly::isValid ( const String bytes)
inline

Returns true if the given WASM module is valid.

◆ isValid() [2/2]

bool WebAssembly::isValid ( const uint8 *  wasm,
MemorySize  size 
)

Returns true if the given WASM module is valid.

◆ load() [1/2]

bool WebAssembly::load ( const String path)
inline

Loads the given WASM module in buffer.

◆ load() [2/2]

bool WebAssembly::load ( const uint8 *  wasm,
MemorySize  size 
)

Loads the given WASM module in buffer.

◆ loadAny()

bool WebAssembly::loadAny ( const String bytes)

Loads the given WAT or WASM module in buffer.

◆ loadFile()

bool WebAssembly::loadFile ( const String path)

Loads the given WASM module.

◆ makeInstance()

bool WebAssembly::makeInstance ( bool  fake = false)

Makes instance for loaded module.

Parameters
fakeIf, true dummy imports will be registered automatically.

◆ makeWASIInstance()

bool WebAssembly::makeWASIInstance ( InputStream _stdin,
OutputStream _stdout,
OutputStream _stderr 
)

Makes a WASI instance.

◆ registerCallback()

void WebAssembly::registerCallback ( R< WASMCallback callack,
const FunctionType type,
const String name,
const String module = String() 
)

Registers global callback.

◆ registerFunction() [1/2]

void WebAssembly::registerFunction ( WASMFunction  func,
AnyReference  context,
const FunctionType type,
const String name,
const String module = String() 
)

Registers global function.

◆ registerFunction() [2/2]

template<typename RESULT , typename... ARGS>
void WebAssembly::registerFunction ( WASMFunction  func,
AnyReference  context,
RESULT(*)(ARGS...)  functype,
const String name,
const String module = String() 
)
inline

Registers global function.

◆ setArguments()

void WebAssembly::setArguments ( const Array< String > &  arguments)

Sets the command line arguments.

◆ setEnvironment()

void WebAssembly::setEnvironment ( const Map< String, String > &  environment)

Sets the initial environment.

◆ setMaximumMemoryUsage()

void WebAssembly::setMaximumMemoryUsage ( MemorySize  size)

Sets the memory limit.

◆ setMountedFolders()

void WebAssembly::setMountedFolders ( const Map< String, String > &  folders)

Sets the initial environment.

◆ setUseLog()

void WebAssembly::setUseLog ( bool  useLog)

Enables logging.

◆ setUseProfiler()

void WebAssembly::setUseProfiler ( bool  useProfiler)

Enables profiling.

◆ toString() [1/3]

static String WebAssembly::toString ( const FunctionType functionType,
const String name = String(),
const String module = String(),
bool  colorize = false 
)
static

Returns function type as string.

◆ toString() [2/3]

static String WebAssembly::toString ( const Symbol s,
bool  colorize = false 
)
static

Returns symbol as string.

◆ toString() [3/3]

static const char* WebAssembly::toString ( Type  type)
staticnoexcept

Returns type as string.