Compare commits

...

1 commit

Author SHA1 Message Date
HyukWoo Park
9be9cbc5c1 Replace WebAssembly engine by wamr
* fix build script
* add wamr submodule
* fix wasm related code to match with the new wasm-c-api

Signed-off-by: HyukWoo Park <hyukwoo.park@samsung.com>
2022-08-24 14:03:49 +09:00
19 changed files with 138 additions and 1295 deletions

6
.gitmodules vendored
View file

@ -26,7 +26,7 @@
path = third_party/googletest
url = https://github.com/google/googletest.git
ignore = untracked
[submodule "third_party/wasm/wabt"]
path = third_party/wasm/wabt
url = https://github.com/WebAssembly/wabt
[submodule "third_party/wasm/wamr"]
path = third_party/wasm/wamr
url = https://github.com/bytecodealliance/wasm-micro-runtime.git
ignore = untracked

View file

@ -135,15 +135,12 @@ ADD_SUBDIRECTORY (third_party/runtime_icu_binder)
SET (ESCARGOT_LIBRARIES ${ESCARGOT_LIBRARIES} runtime-icu-binder-static)
# WebAssembly (wabt)
# WebAssembly (wamr)
IF (ESCARGOT_WASM)
SET (WASM_CXX_FLAGS
${ESCARGOT_THIRDPARTY_CFLAGS} # we can share flags with gcutil
-g3)
SET (WASM_ARCH ${ESCARGOT_ARCH})
# share flags with gcutil
SET (WASM_CFLAGS ${ESCARGOT_GCUTIL_CFLAGS})
ADD_SUBDIRECTORY (third_party/wasm)
SET (ESCARGOT_LIBRARIES ${ESCARGOT_LIBRARIES} wasm)
ENDIF()

View file

@ -4509,11 +4509,6 @@ ObjectRef* WASMOperationsRef::instantiatePromiseOfModuleWithImportObject(Executi
{
return toRef(WASMOperations::instantiatePromiseOfModuleWithImportObject(*toImpl(state), toImpl(promiseOfModule), toImpl(importObj)));
}
void WASMOperationsRef::collectHeap()
{
WASMOperations::collectHeap();
}
#else
ValueRef* WASMOperationsRef::copyStableBufferBytes(ExecutionStateRef* state, ValueRef* source)
{
@ -4535,12 +4530,6 @@ ObjectRef* WASMOperationsRef::instantiatePromiseOfModuleWithImportObject(Executi
RELEASE_ASSERT_NOT_REACHED();
return nullptr;
}
void WASMOperationsRef::collectHeap()
{
ESCARGOT_LOG_ERROR("If you want to use this function, you should enable WASM");
RELEASE_ASSERT_NOT_REACHED();
}
#endif
} // namespace Escargot

View file

@ -2166,7 +2166,6 @@ public:
static ValueRef* copyStableBufferBytes(ExecutionStateRef* state, ValueRef* source);
static ObjectRef* asyncCompileModule(ExecutionStateRef* state, ValueRef* source);
static ObjectRef* instantiatePromiseOfModuleWithImportObject(ExecutionStateRef* state, PromiseObjectRef* promiseOfModule, ValueRef* importObj);
static void collectHeap();
};
} // namespace Escargot

View file

@ -25,7 +25,7 @@
#include "parser/ASTAllocator.h"
#include "BumpPointerAllocator.h"
#if defined(ENABLE_WASM)
#include "wasm.h"
#include "wasm_c_api.h"
#endif
namespace Escargot {
@ -145,7 +145,6 @@ void ThreadLocal::initialize()
// g_wasmContext
g_wasmContext.engine = wasm_engine_new();
g_wasmContext.store = wasm_store_new(g_wasmContext.engine);
g_wasmContext.lastGCCheckTime = 0;
#endif
// g_gcEventListenerSet
@ -193,7 +192,6 @@ void ThreadLocal::finalize()
wasm_engine_delete(g_wasmContext.engine);
g_wasmContext.store = nullptr;
g_wasmContext.engine = nullptr;
g_wasmContext.lastGCCheckTime = 0;
#endif
// g_gcEventListenerSet
@ -211,14 +209,4 @@ void ThreadLocal::finalize()
called_once = false;
inited = false;
}
#if defined(ENABLE_WASM)
void ThreadLocal::wasmGC(uint64_t lastCheckTime)
{
ASSERT(inited && !!g_wasmContext.store);
wasm_store_gc(g_wasmContext.store);
g_wasmContext.lastGCCheckTime = lastCheckTime;
}
#endif
} // namespace Escargot

View file

@ -30,7 +30,6 @@ struct wasm_store_t;
struct WASMContext {
wasm_engine_t* engine;
wasm_store_t* store;
uint64_t lastGCCheckTime;
};
#endif
@ -119,19 +118,11 @@ public:
}
#if defined(ENABLE_WASM)
static void wasmGC(uint64_t lastCheckTime);
static wasm_store_t* wasmStore()
{
ASSERT(inited && !!g_wasmContext.store);
return g_wasmContext.store;
}
static uint64_t wasmLastGCCheckTime()
{
ASSERT(inited && !!g_wasmContext.store);
return g_wasmContext.lastGCCheckTime;
}
#endif
static GCEventListenerSet& gcEventListenerSet()

View file

@ -34,6 +34,9 @@
#if defined(ENABLE_CODE_CACHE)
#include "codecache/CodeCache.h"
#endif
#if defined(ENABLE_WASM)
#include "wasm_c_api.h"
#endif
#if defined(OS_WINDOWS)
#include <Windows.h>
@ -54,12 +57,6 @@ NT_TIB* getTIB()
namespace Escargot {
#if defined(ENABLE_WASM)
#ifndef ESCARGOT_WASM_GC_CHECK_INTERVAL
#define ESCARGOT_WASM_GC_CHECK_INTERVAL 10000
#endif
#endif
Value VMInstance::functionPrototypeNativeGetter(ExecutionState& state, Object* self, const Value& receiver, const EncodedValue& privateDataFromObjectPrivateArea)
{
ASSERT(self->isFunctionObject());
@ -240,11 +237,6 @@ void vmReclaimEndCallback(void* data)
self->m_lastCompressibleStringsTestTime = currentTick;
}
#endif
#if defined(ENABLE_WASM)
if (currentTick - ThreadLocal::wasmLastGCCheckTime() > ESCARGOT_WASM_GC_CHECK_INTERVAL) {
ThreadLocal::wasmGC(currentTick);
}
#endif
#endif
auto& currentCodeSizeTotal = self->compiledByteCodeSize();

View file

@ -20,7 +20,7 @@
#if defined(ENABLE_WASM)
#include "Escargot.h"
#include "wasm.h"
#include "wasm_c_api.h"
#include "runtime/GlobalObject.h"
#include "runtime/Context.h"
#include "runtime/ThreadLocal.h"
@ -347,7 +347,7 @@ static Value builtinWASMInstanceConstructor(ExecutionState& state, Value thisVal
// Instantiate the core of a WebAssembly module module with imports, and let instance be the result.
own wasm_trap_t* trap = nullptr;
own wasm_instance_t* instance = wasm_instance_new(ThreadLocal::wasmStore(), module, imports.data, &trap);
own wasm_instance_t* instance = wasm_instance_new(ThreadLocal::wasmStore(), module, &imports, &trap);
wasm_extern_vec_delete(&imports);
if (!instance) {

View file

@ -20,7 +20,7 @@
#if defined(ENABLE_WASM)
#include "Escargot.h"
#include "wasm.h"
#include "wasm_c_api.h"
#include "runtime/Context.h"
#include "runtime/Object.h"
#include "runtime/ArrayObject.h"
@ -76,7 +76,7 @@ static Value callExportedFunction(ExecutionState& state, Value thisValue, size_t
// Let args be << >>
wasm_val_t* argsBuffer = ALLOCA(parameters->size * sizeof(wasm_val_t), wasm_val_t, state);
wasm_val_vec_t args = { parameters->size, argsBuffer };
wasm_val_vec_t args = { parameters->size, argsBuffer, parameters->size, sizeof(wasm_val_t), nullptr };
// For each t of parameters,
for (size_t i = 0; i < parameters->size; i++) {
@ -89,12 +89,12 @@ static Value callExportedFunction(ExecutionState& state, Value thisValue, size_t
}
wasm_val_t* retBuffer = ALLOCA(results->size * sizeof(wasm_val_t), wasm_val_t, state);
wasm_val_vec_t ret = { results->size, retBuffer };
wasm_val_vec_t ret = { results->size, retBuffer, results->size, sizeof(wasm_val_t), nullptr };
wasm_functype_delete(functype);
// Let (store, ret) be the result of func_invoke(store, funcaddr, args).
own wasm_trap_t* trap = wasm_func_call(funcaddr, args.data, ret.data);
own wasm_trap_t* trap = wasm_func_call(funcaddr, &args, &ret);
// If ret is error, throw an exception. This exception should be a WebAssembly RuntimeError exception, unless otherwise indicated by the WebAssembly error mapping.
if (trap) {

View file

@ -20,7 +20,7 @@
#if defined(ENABLE_WASM)
#include "Escargot.h"
#include "wasm.h"
#include "wasm_c_api.h"
#include "runtime/Context.h"
#include "runtime/Object.h"
#include "runtime/ArrayBufferObject.h"

View file

@ -22,7 +22,8 @@
#ifndef __EscargotWASMObject__
#define __EscargotWASMObject__
struct wasm_module_t;
struct WASMModuleCommon;
typedef struct WASMModuleCommon* wasm_module_t;
struct wasm_instance_t;
struct wasm_memory_t;
struct wasm_table_t;

View file

@ -20,7 +20,7 @@
#if defined(ENABLE_WASM)
#include "Escargot.h"
#include "wasm.h"
#include "wasm_c_api.h"
#include "runtime/ThreadLocal.h"
#include "runtime/VMInstance.h"
#include "runtime/Job.h"
@ -40,7 +40,7 @@
namespace Escargot {
static own wasm_trap_t* callbackHostFunction(void* env, const wasm_val_t args[], wasm_val_t results[])
static own wasm_trap_t* callbackHostFunction(void* env, const wasm_val_vec_t* args, wasm_val_vec_t* results)
{
WASMHostFunctionEnvironment* funcEnv = (WASMHostFunctionEnvironment*)env;
@ -64,7 +64,7 @@ static own wasm_trap_t* callbackHostFunction(void* env, const wasm_val_t args[],
// For each arg of arguments,
for (size_t i = 0; i < argSize; i++) {
// Append ! ToJSValue(arg) to jsArguments.
jsArguments[i] = WASMValueConverter::wasmToJSValue(state, args[i]);
jsArguments[i] = WASMValueConverter::wasmToJSValue(state, args->data[i]);
}
// Let ret be ? Call(func, undefined, jsArguments).
@ -79,7 +79,7 @@ static own wasm_trap_t* callbackHostFunction(void* env, const wasm_val_t args[],
if (resultsSize == 1) {
// Otherwise, if resultsSize is 1, return << ToWebAssemblyValue(ret, results[0]) >>
results[0] = WASMValueConverter::wasmToWebAssemblyValue(state, ret, wasm_valtype_kind(res->data[0]));
results->data[0] = WASMValueConverter::wasmToWebAssemblyValue(state, ret, wasm_valtype_kind(res->data[0]));
} else {
// Otherwise,
// Let method be ? GetMethod(ret, @@iterator).
@ -99,7 +99,7 @@ static own wasm_trap_t* callbackHostFunction(void* env, const wasm_val_t args[],
// For each value and resultType in values and results, paired linearly,
// Append ToWebAssemblyValue(value, resultType) to wasmValues.
for (size_t i = 0; i < resultsSize; i++) {
results[i] = WASMValueConverter::wasmToWebAssemblyValue(state, values[i], wasm_valtype_kind(res->data[i]));
results->data[i] = WASMValueConverter::wasmToWebAssemblyValue(state, values[i], wasm_valtype_kind(res->data[i]));
}
}
@ -147,7 +147,7 @@ static Value wasmInstantiateModule(ExecutionState& state, Value thisValue, size_
// Instantiate the core of a WebAssembly module module with imports, and let instance be the result.
own wasm_trap_t* trap = nullptr;
own wasm_instance_t* instance = wasm_instance_new(ThreadLocal::wasmStore(), module, imports.data, &trap);
own wasm_instance_t* instance = wasm_instance_new(ThreadLocal::wasmStore(), module, &imports, &trap);
wasm_extern_vec_delete(&imports);
if (!instance) {
@ -273,9 +273,10 @@ Object* WASMOperations::createExportsObject(ExecutionState& state, wasm_module_t
// Let func funcaddr be externval.
wasm_func_t* funcaddr = wasm_extern_as_func(externval);
// FIXME getting a function index from instance which is not a standard (we should obtain the index from module)
uint32_t funcIndex = wasm_instance_func_index(instance, funcaddr);
ASSERT(funcIndex != wasm_limits_max_default);
// FIXME getting a function index from wasm_func_t.name
//int funcIndex = atoi(funcaddr->name->data);
uint32_t funcIndex = 0;
ASSERT(funcIndex < wasm_limits_max_default);
// Let func be the result of creating a new Exported Function from funcaddr.
// Let value be func.
@ -359,7 +360,6 @@ void WASMOperations::readImportsOfModule(ExecutionState& state, wasm_module_t* m
// Let imports be << >>
wasm_extern_vec_new_uninitialized(imports, import_types.size);
size_t importsSize = 0;
// For each (moduleName, componentName, externtype) of module_imports(module),
for (size_t i = 0; i < import_types.size; i++) {
@ -375,7 +375,7 @@ void WASMOperations::readImportsOfModule(ExecutionState& state, wasm_module_t* m
// If Type(o) is not Object, throw a TypeError exception.
if (!o.isObject()) {
wasm_importtype_vec_delete(&import_types);
wasm_extern_vec_delete_with_size(imports, importsSize);
wasm_extern_vec_delete(imports);
ErrorObject::throwBuiltinError(state, ErrorObject::TypeError, ErrorObject::Messages::WASM_ReadImportsError);
}
@ -499,12 +499,10 @@ void WASMOperations::readImportsOfModule(ExecutionState& state, wasm_module_t* m
if (UNLIKELY(throwLinkError)) {
wasm_importtype_vec_delete(&import_types);
wasm_extern_vec_delete_with_size(imports, importsSize);
wasm_extern_vec_delete(imports);
ErrorObject::throwBuiltinError(state, ErrorObject::WASMLinkError, ErrorObject::Messages::WASM_ReadImportsError);
}
importsSize++;
}
wasm_importtype_vec_delete(&import_types);
@ -522,12 +520,12 @@ Value WASMOperations::instantiateCoreModule(ExecutionState& state, Value thisVal
ExtendedNativeFunctionObject* callee = state.resolveCallee()->asExtendedNativeFunctionObject();
size_t importsSize = callee->internalSlot(0).asUInt32();
wasm_extern_t** importsData = callee->internalSlotAsPointer<wasm_extern_t*>(1);
own wasm_extern_vec_t imports = { importsSize, importsData };
own wasm_extern_vec_t imports = { importsSize, importsData, importsSize, sizeof(wasm_extern_t*), nullptr };
callee->setInternalSlotAsPointer(1, nullptr);
// Instantiate the core of a WebAssembly module module with imports, and let instance be the result.
own wasm_trap_t* trap = nullptr;
own wasm_instance_t* instance = wasm_instance_new(ThreadLocal::wasmStore(), module, imports.data, &trap);
own wasm_instance_t* instance = wasm_instance_new(ThreadLocal::wasmStore(), module, &imports, &trap);
wasm_extern_vec_delete(&imports);
if (!instance) {
@ -559,12 +557,6 @@ Object* WASMOperations::instantiatePromiseOfModuleWithImportObject(ExecutionStat
return promiseOfModule->then(state, moduleInstantiator);
}
void WASMOperations::collectHeap()
{
// collect (GC) WASM Objects allocated inside WASM heap
wasm_store_gc(ThreadLocal::wasmStore());
}
} // namespace Escargot
#endif // ENABLE_WASM

View file

@ -22,7 +22,8 @@
#ifndef __EscargotWASMOperations__
#define __EscargotWASMOperations__
struct wasm_module_t;
struct WASMModuleCommon;
typedef struct WASMModuleCommon* wasm_module_t;
struct wasm_instance_t;
struct wasm_extern_vec_t;
@ -38,8 +39,6 @@ public:
static void readImportsOfModule(ExecutionState& state, wasm_module_t* module, const Value& importObj, wasm_extern_vec_t* imports);
static Value instantiateCoreModule(ExecutionState& state, Value thisValue, size_t argc, Value* argv, Optional<Object*> newTarget);
static Object* instantiatePromiseOfModuleWithImportObject(ExecutionState& state, PromiseObject* promiseOfModule, Value importObj);
static void collectHeap();
};
} // namespace Escargot

View file

@ -20,7 +20,7 @@
#if defined(ENABLE_WASM)
#include "Escargot.h"
#include "wasm.h"
#include "wasm_c_api.h"
#include "runtime/Context.h"
#include "runtime/Value.h"
#include "runtime/BigInt.h"
@ -28,6 +28,26 @@
namespace Escargot {
// redefine macros to resolve build errors
#ifdef WASM_I32_VAL
#undef WASM_I32_VAL
#endif
#ifdef WASM_I64_VAL
#undef WASM_I64_VAL
#endif
#ifdef WASM_F32_VAL
#undef WASM_F32_VAL
#endif
#ifdef WASM_F64_VAL
#undef WASM_F64_VAL
#endif
#ifdef WASM_REF_VAL
#undef WASM_REF_VAL
#endif
#ifdef WASM_INIT_VAL
#undef WASM_INIT_VAL
#endif
#define WASM_I32_VAL(result, i) \
result.kind = WASM_I32; \
result.of.i32 = i;

View file

@ -1,192 +1,90 @@
PROJECT(WASM)
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
project (WASM)
cmake_minimum_required (VERSION 2.8)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
IF (NOT CMAKE_BUILD_TYPE)
SET (CMAKE_BUILD_TYPE Debug)
ENDIF()
if (NOT "${CMAKE_PROJECT_VERSION}")
set(CMAKE_PROJECT_VERSION "1.0.29")
set(CMAKE_CXX_STANDARD 14)
string (TOLOWER ${CMAKE_HOST_SYSTEM_NAME} WAMR_BUILD_PLATFORM)
if (APPLE)
add_definitions(-DBH_PLATFORM_DARWIN)
endif ()
# Resetdefault linker flags
set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
# WAMR features switch
# Set WAMR_BUILD_TARGET, currently values supported:
# "X86_64", "AMD_64", "X86_32", "AARCH64[sub]", "ARM[sub]", "THUMB[sub]",
# "MIPS", "XTENSA", "RISCV64[sub]", "RISCV32[sub]"
if (NOT DEFINED WAMR_BUILD_TARGET)
if (CMAKE_SYSTEM_PROCESSOR MATCHES "^(arm64|aarch64)")
set (WAMR_BUILD_TARGET "AARCH64")
elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL "riscv64")
set (WAMR_BUILD_TARGET "RISCV64")
elseif (CMAKE_SIZEOF_VOID_P EQUAL 8)
# Build as X86_64 by default in 64-bit platform
set (WAMR_BUILD_TARGET "X86_64")
elseif (CMAKE_SIZEOF_VOID_P EQUAL 4)
# Build as X86_32 by default in 32-bit platform
set (WAMR_BUILD_TARGET "X86_32")
else ()
message(SEND_ERROR "Unsupported build target platform!")
endif ()
endif ()
# enable interpreter mode by default
set(WAMR_BUILD_INTERP 1)
# disable AOT and JIT by default
set(WAMR_BUILD_AOT 0)
set(WAMR_BUILD_JIT 0)
set(WAMR_BUILD_LIBC_BUILTIN 1)
set(WAMR_BUILD_LIBC_WASI 0)
set(WAMR_BUILD_MULTI_MODULE 1)
set(WAMR_BUILD_DUMP_CALL_STACK 1)
set(WAMR_BUILD_REF_TYPES 1)
if(NOT DEFINED WAMR_BUILD_FAST_INTERP)
set(WAMR_BUILD_FAST_INTERP 1)
endif()
if (NOT MSVC)
# compiling and linking flags
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pie -fPIE")
if (NOT (CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang"))
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
endif ()
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wformat -Wformat-security")
if (WAMR_BUILD_TARGET MATCHES "X86_.*" OR WAMR_BUILD_TARGET STREQUAL "AMD_64")
if (NOT (CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang"))
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mindirect-branch-register")
endif ()
endif ()
endif()
# set(WASM_CFLAGS_FROM_ENV $ENV{CFLAGS})
# separate_arguments(WASM_CFLAGS_FROM_ENV)
# add_compile_options(${WASM_CFLAGS_FROM_ENV})
# build out wamr lib
set(WAMR_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/wamr)
include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)
add_library(wasm SHARED ${WAMR_RUNTIME_LIB_SOURCE})
if (MSVC)
set(COMPILER_IS_CLANG 0)
set(COMPILER_IS_GNU 0)
set(COMPILER_IS_MSVC 1)
elseif (CMAKE_C_COMPILER_ID MATCHES "Clang")
set(COMPILER_IS_CLANG 1)
set(COMPILER_IS_GNU 0)
set(COMPILER_IS_MSVC 0)
elseif (CMAKE_C_COMPILER_ID STREQUAL "GNU")
set(COMPILER_IS_CLANG 0)
set(COMPILER_IS_GNU 1)
set(COMPILER_IS_MSVC 0)
elseif (CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
set(COMPILER_IS_CLANG 1)
set(COMPILER_IS_GNU 0)
set(COMPILER_IS_MSVC 0)
else ()
set(COMPILER_IS_CLANG 0)
set(COMPILER_IS_GNU 0)
set(COMPILER_IS_MSVC 0)
target_compile_definitions(wasm PRIVATE WASM_API_EXTERN=)
endif()
target_link_libraries (wasm ${LLVM_AVAILABLE_LIBS} ${UV_A_LIBS} -lm -ldl -lpthread)
target_include_directories(wasm PUBLIC ${WAMR_ROOT_DIR}/core/iwasm/include)
include(CheckIncludeFile)
include(CheckSymbolExists)
check_include_file("alloca.h" HAVE_ALLOCA_H)
check_include_file("unistd.h" HAVE_UNISTD_H)
check_symbol_exists(snprintf "stdio.h" HAVE_SNPRINTF)
check_symbol_exists(strcasecmp "strings.h" HAVE_STRCASECMP)
# TARGET_COMPILE_DEFINITIONS(wasm PRIVATE "WASM_API_EXTERN=__attribute__((visibility(\"default\")))")
# SET_TARGET_PROPERTIES(wasm PROPERTIES CXX_VISIBILITY_PRESET hidden)
include(CheckTypeSize)
check_type_size(ssize_t SSIZE_T)
#check_type_size(size_t SIZEOF_SIZE_T)
# Escargot specific option (for the case of compilation to 32bit from 64bit)
if (ESCARGOT_BUILD_32BIT)
set(SIZEOF_SIZE_T 4)
else ()
set(SIZEOF_SIZE_T 8)
endif ()
set(WABT_ROOT ${PROJECT_SOURCE_DIR}/wabt)
configure_file(${WABT_ROOT}/src/config.h.in ${PROJECT_SOURCE_DIR}/config.h)
include_directories(${WABT_ROOT} ${PROJECT_SOURCE_DIR})
set(WABT_SRC
${PROJECT_SOURCE_DIR}/config.h
wabt/src/apply-names.h
wabt/src/apply-names.cc
wabt/src/binary.h
wabt/src/binary.cc
wabt/src/binary-reader.h
wabt/src/binary-reader.cc
wabt/src/binary-reader-ir.h
wabt/src/binary-reader-ir.cc
wabt/src/binary-reader-logging.h
wabt/src/binary-reader-logging.cc
wabt/src/binary-writer.h
wabt/src/binary-writer.cc
wabt/src/binary-writer-spec.h
wabt/src/binary-writer-spec.cc
wabt/src/binding-hash.h
wabt/src/binding-hash.cc
wabt/src/color.h
wabt/src/color.cc
wabt/src/common.h
wabt/src/common.cc
wabt/src/config.cc
wabt/src/decompiler.h
wabt/src/decompiler-ast.h
wabt/src/decompiler-ls.h
wabt/src/decompiler-naming.h
wabt/src/decompiler.cc
wabt/src/error-formatter.h
wabt/src/error-formatter.cc
wabt/src/expr-visitor.h
wabt/src/expr-visitor.cc
wabt/src/feature.h
wabt/src/feature.cc
wabt/src/filenames.h
wabt/src/filenames.cc
wabt/src/generate-names.h
wabt/src/generate-names.cc
wabt/src/ir.h
wabt/src/ir.cc
wabt/src/ir-util.h
wabt/src/ir-util.cc
wabt/src/leb128.h
wabt/src/leb128.cc
wabt/src/lexer-source.h
wabt/src/lexer-source.cc
wabt/src/lexer-source-line-finder.h
wabt/src/lexer-source-line-finder.cc
wabt/src/literal.h
wabt/src/literal.cc
wabt/src/opcode.h
wabt/src/opcode.cc
wabt/src/opcode-code-table.h
wabt/src/opcode-code-table.c
wabt/src/option-parser.h
wabt/src/option-parser.cc
wabt/src/resolve-names.h
wabt/src/resolve-names.cc
wabt/src/shared-validator.h
wabt/src/shared-validator.cc
wabt/src/stream.h
wabt/src/stream.cc
wabt/src/string-util.h
wabt/src/token.h
wabt/src/token.cc
wabt/src/tracing.h
wabt/src/tracing.cc
wabt/src/type.h
wabt/src/type-checker.h
wabt/src/type-checker.cc
wabt/src/utf8.h
wabt/src/utf8.cc
wabt/src/validator.h
wabt/src/validator.cc
wabt/src/wast-lexer.h
wabt/src/wast-lexer.cc
wabt/src/wast-parser.h
wabt/src/wast-parser.cc
wabt/src/wat-writer.h
wabt/src/wat-writer.cc
# TODO(binji): Move this into its own library?
wabt/src/interp/binary-reader-interp.h
wabt/src/interp/binary-reader-interp.cc
wabt/src/interp/interp.h
wabt/src/interp/interp.cc
wabt/src/interp/interp-inl.h
wabt/src/interp/interp-math.h
wabt/src/interp/interp-util.h
wabt/src/interp/interp-util.cc
wabt/src/interp/istream.h
wabt/src/interp/istream.cc
)
# disable -Wpointer-arith: this is a GCC extension, and doesn't work in MSVC.
set(WASM_CXX_FLAGS_INTERNAL
-Wall -Wextra -Werror -Wno-unused-parameter -Wpointer-arith
-Wuninitialized -fPIC -fdata-sections -ffunction-sections
)
# set c++ flags
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wold-style-cast")
# Need to define __STDC_*_MACROS because C99 specifies that C++ shouldn't
# define format (e.g. PRIu64) or limit (e.g. UINT32_MAX) macros without the
# definition, and some libcs (e.g. glibc2.17 and earlier) follow that.
set(WASM_CXX_FLAGS_INTERNAL ${WASM_CXX_FLAGS_INTERNAL} -D__STDC_LIMIT_MACROS=1 -D__STDC_FORMAT_MACROS=1 -DNDEBUG)
if (COMPILER_IS_GNU)
# disable -Wclobbered: it seems to be guessing incorrectly about a local
# variable being clobbered by longjmp.
set(WASM_CXX_FLAGS_INTERNAL ${WASM_CXX_FLAGS_INTERNAL} -Wno-clobbered)
endif ()
if (WASM_ARCH STREQUAL "x86")
# wasm doesn't allow for x87 floating point math
set(WASM_CXX_FLAGS_INTERNAL ${WASM_CXX_FLAGS_INTERNAL} -msse2 -mfpmath=sse)
endif ()
add_compile_options(${WASM_CXX_FLAGS_INTERNAL})
add_compile_options(${WASM_CXX_FLAGS})
set(WASM_CFLAGS_FROM_ENV $ENV{CFLAGS})
separate_arguments(WASM_CFLAGS_FROM_ENV)
add_compile_options(${WASM_CFLAGS_FROM_ENV})
# build wabt lib
add_library(wabt STATIC ${WABT_SRC})
# build wasm lib
add_library(wasm SHARED wabt/src/interp/interp-wasm-c-api.cc)
target_link_libraries(wasm wabt -Wl,--gc-sections ${WASM_CXX_FLAGS})
target_include_directories(wasm PUBLIC ${PROJECT_SOURCE_DIR}/include)
target_compile_options(wasm PRIVATE $<$<COMPILE_LANGUAGE:CXX>:-Wno-old-style-cast>)
target_compile_definitions(wasm PRIVATE "WASM_API_EXTERN=__attribute__((visibility(\"default\")))")
set_target_properties(wasm PROPERTIES CXX_VISIBILITY_PRESET hidden)
# add compile options from parent scope
target_compile_options(wasm PRIVATE ${WASM_CFLAGS})

View file

@ -1,202 +0,0 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "{}"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright {yyyy} {name of copyright owner}
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

View file

@ -1,112 +0,0 @@
# WebAssembly C and C++ API
Work in progress! No docs yet.
### Design Goals
* Provide a "black box" API for embedding a Wasm engine in other C/C++ applications.
* Be completely agnostic to VM specifics.
* Non-goal: "white box" interoperability with embedder (such as combined GC instead of mere finalisation) -- *much* more difficult to achieve.
* Allow creation of bindings for other languages through typical C foreign function interfaces.
* Support a plain C API.
* Stick to mostly manual memory management of interface objects.
* Avoid language features that raise barrier to use.
* E.g., no exceptions or post-C++11 features in C++ API.
* E.g., no passing of structs by-value or post-C99 features in C API.
* Achieve link-time compatibility between different implementations.
* All implementation-dependent API classes are abstract and can be instantiated through factory methods only.
### Interfaces
* C++ API:
* See `include/wasm.hh` for interface.
* See `example/*.cc` for example usages.
* C API:
* See `include/wasm.h` for interface.
* See `example/*.c` for example usages.
Some random explanations:
* The VM must be initialised by creating an instance of an *engine* (`wasm::Engine`/`wasm_engine_t`) and is shut down by deleting it. Such an instance may only be created once per process.
* All runtime objects are tied to a specific *store* (`wasm::Store`/`wasm_store_t`). Multiple stores can be created, but their objects cannot interact. Every store and its objects must only be accessed in a single thread.
* To exchange module objects between threads, create a *shared* module (`wasm::Shared<Module>`/`wasm_shared_module_t`). Other objects cannot be shared in current Wasm.
* *Vector* structures (`wasm::vec<X>`/`wasm_x_vec_t`) are lightweight abstractions of a pair of a plain array and its length. The C++ API does not use `std::vector` because that does not support adopting pre-existing arrays.
* *References* point to runtime objects, but may involve internal indirections, which may or may not be cached. Thus, pointer equality on `Ref*` or subclasses cannot be used to compare identity of the underlying objects (`Ref::eq` may be added later). However, `nullptr`/`NULL` uniquely represents null references.
* The API already encompasses current proposals like [multiple return values](https://github.com/WebAssembly/multi-value/blob/master/proposals/multi-value/Overview.md) and [reference types](https://github.com/WebAssembly/reference-types/blob/master/proposals/reference-types/Overview.md), but not yet [threads](https://github.com/WebAssembly/threads/blob/master/proposals/threads/Overview.md).
### Prototype Implementation
* This repo contains a prototype implementation based on V8 is in `src`.
* Note that this requires adding a module to V8, so it patches V8's build file.
* The C API is implemented on top of the C++ API.
* See `Makefile` for build recipe. Canonical steps to run examples:
1. `make v8-checkout`
2. `make v8`
3. `make all`
#### Limitations
V8 implementation:
* Currently requires patching V8 by adding a module.
* Host functions (`Func::make`) create a JavaScript function internally, since V8 cannot handle raw C imports yet.
* As a consequence, does not support multiple results in external calls or host functions.
* Host functions and host globals are created through auxiliary modules constructed on the fly, to work around limitations in JS API.
* `Shared<Module>` is currently implemented via serialisation, since V8 does not currently have direct support for cross-isolate sharing.
### Other Implementations
Currently, known implementations of this API are included in
* V8 natively (both C and C++)
* Wabt (only C?)
* Wasmtime (only C?)
* [Wasmer](https://github.com/wasmerio/wasmer/tree/master/lib/c-api) (only C, C++ coming soon)
### TODO
Possible API tweaks:
* Add `Ref::eq` (or better, a subclass `EqRef::eq`) for reference equality?
* Add a way to return error messages from `Module::make` and `Module::validate`.
* Use `restrict` in C API?
* Find a way to perform C callbacks through C++ without extra wrapper?
* Add iterators to `vec` class?

View file

@ -1,710 +0,0 @@
// WebAssembly C API
#ifndef WASM_H
#define WASM_H
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <assert.h>
#ifndef WASM_API_EXTERN
#ifdef _WIN32
#define WASM_API_EXTERN __declspec(dllimport)
#else
#define WASM_API_EXTERN
#endif
#endif
#ifdef __cplusplus
extern "C" {
#endif
///////////////////////////////////////////////////////////////////////////////
// Auxiliaries
// Machine types
inline void assertions() {
static_assert(sizeof(float) == sizeof(uint32_t), "incompatible float type");
static_assert(sizeof(double) == sizeof(uint64_t), "incompatible double type");
static_assert(sizeof(intptr_t) == sizeof(uint32_t) ||
sizeof(intptr_t) == sizeof(uint64_t),
"incompatible pointer type");
}
typedef char byte_t;
typedef float float32_t;
typedef double float64_t;
// Ownership
#define own
// The qualifier `own` is used to indicate ownership of data in this API.
// It is intended to be interpreted similar to a `const` qualifier:
//
// - `own wasm_xxx_t*` owns the pointed-to data
// - `own wasm_xxx_t` distributes to all fields of a struct or union `xxx`
// - `own wasm_xxx_vec_t` owns the vector as well as its elements(!)
// - an `own` function parameter passes ownership from caller to callee
// - an `own` function result passes ownership from callee to caller
// - an exception are `own` pointer parameters named `out`, which are copy-back
// output parameters passing back ownership from callee to caller
//
// Own data is created by `wasm_xxx_new` functions and some others.
// It must be released with the corresponding `wasm_xxx_delete` function.
//
// Deleting a reference does not necessarily delete the underlying object,
// it merely indicates that this owner no longer uses it.
//
// For vectors, `const wasm_xxx_vec_t` is used informally to indicate that
// neither the vector nor its elements should be modified.
// TODO: introduce proper `wasm_xxx_const_vec_t`?
#define WASM_DECLARE_OWN(name) \
typedef struct wasm_##name##_t wasm_##name##_t; \
\
WASM_API_EXTERN void wasm_##name##_delete(own wasm_##name##_t*);
// Vectors
#define WASM_DECLARE_VEC(name, ptr_or_none) \
typedef struct wasm_##name##_vec_t { \
size_t size; \
wasm_##name##_t ptr_or_none* data; \
} wasm_##name##_vec_t; \
\
WASM_API_EXTERN void wasm_##name##_vec_new_empty(own wasm_##name##_vec_t* out); \
WASM_API_EXTERN void wasm_##name##_vec_new_uninitialized( \
own wasm_##name##_vec_t* out, size_t); \
WASM_API_EXTERN void wasm_##name##_vec_new( \
own wasm_##name##_vec_t* out, \
size_t, own wasm_##name##_t ptr_or_none const[]); \
WASM_API_EXTERN void wasm_##name##_vec_copy( \
own wasm_##name##_vec_t* out, const wasm_##name##_vec_t*); \
WASM_API_EXTERN void wasm_##name##_vec_delete(own wasm_##name##_vec_t*); \
WASM_API_EXTERN void wasm_##name##_vec_delete_with_size( \
own wasm_##name##_vec_t*, size_t);
// Byte vectors
typedef byte_t wasm_byte_t;
WASM_DECLARE_VEC(byte, )
typedef wasm_byte_vec_t wasm_name_t;
#define wasm_name wasm_byte_vec
#define wasm_name_new wasm_byte_vec_new
#define wasm_name_new_empty wasm_byte_vec_new_empty
#define wasm_name_new_new_uninitialized wasm_byte_vec_new_uninitialized
#define wasm_name_copy wasm_byte_vec_copy
#define wasm_name_delete wasm_byte_vec_delete
static inline void wasm_name_new_from_string(
own wasm_name_t* out, const char* s
) {
wasm_name_new(out, strlen(s) + 1, s);
}
///////////////////////////////////////////////////////////////////////////////
// Runtime Environment
// Configuration
WASM_DECLARE_OWN(config)
WASM_API_EXTERN own wasm_config_t* wasm_config_new();
// Embedders may provide custom functions for manipulating configs.
// Engine
WASM_DECLARE_OWN(engine)
WASM_API_EXTERN own wasm_engine_t* wasm_engine_new();
WASM_API_EXTERN own wasm_engine_t* wasm_engine_new_with_config(own wasm_config_t*);
// Store
WASM_DECLARE_OWN(store)
WASM_API_EXTERN own wasm_store_t* wasm_store_new(wasm_engine_t*);
WASM_API_EXTERN void wasm_store_gc(wasm_store_t*);
///////////////////////////////////////////////////////////////////////////////
// Type Representations
// Type attributes
typedef uint8_t wasm_mutability_t;
enum wasm_mutability_enum {
WASM_CONST,
WASM_VAR,
};
typedef struct wasm_limits_t {
uint32_t min;
uint32_t max;
} wasm_limits_t;
static const uint32_t wasm_limits_max_default = 0xffffffff;
// Generic
#define WASM_DECLARE_TYPE(name) \
WASM_DECLARE_OWN(name) \
WASM_DECLARE_VEC(name, *) \
\
WASM_API_EXTERN own wasm_##name##_t* wasm_##name##_copy(wasm_##name##_t*);
// Value Types
WASM_DECLARE_TYPE(valtype)
typedef uint8_t wasm_valkind_t;
enum wasm_valkind_enum {
WASM_I32,
WASM_I64,
WASM_F32,
WASM_F64,
WASM_ANYREF = 128,
WASM_FUNCREF,
};
WASM_API_EXTERN own wasm_valtype_t* wasm_valtype_new(wasm_valkind_t);
WASM_API_EXTERN wasm_valkind_t wasm_valtype_kind(const wasm_valtype_t*);
static inline bool wasm_valkind_is_num(wasm_valkind_t k) {
return k < WASM_ANYREF;
}
static inline bool wasm_valkind_is_ref(wasm_valkind_t k) {
return k >= WASM_ANYREF;
}
static inline bool wasm_valtype_is_num(const wasm_valtype_t* t) {
return wasm_valkind_is_num(wasm_valtype_kind(t));
}
static inline bool wasm_valtype_is_ref(const wasm_valtype_t* t) {
return wasm_valkind_is_ref(wasm_valtype_kind(t));
}
// Function Types
WASM_DECLARE_TYPE(functype)
WASM_API_EXTERN own wasm_functype_t* wasm_functype_new(
own wasm_valtype_vec_t* params, own wasm_valtype_vec_t* results);
WASM_API_EXTERN const wasm_valtype_vec_t* wasm_functype_params(const wasm_functype_t*);
WASM_API_EXTERN const wasm_valtype_vec_t* wasm_functype_results(const wasm_functype_t*);
// Global Types
WASM_DECLARE_TYPE(globaltype)
WASM_API_EXTERN own wasm_globaltype_t* wasm_globaltype_new(
own wasm_valtype_t*, wasm_mutability_t);
WASM_API_EXTERN const wasm_valtype_t* wasm_globaltype_content(const wasm_globaltype_t*);
WASM_API_EXTERN wasm_mutability_t wasm_globaltype_mutability(const wasm_globaltype_t*);
// Table Types
WASM_DECLARE_TYPE(tabletype)
WASM_API_EXTERN own wasm_tabletype_t* wasm_tabletype_new(
own wasm_valtype_t*, const wasm_limits_t*);
WASM_API_EXTERN const wasm_valtype_t* wasm_tabletype_element(const wasm_tabletype_t*);
WASM_API_EXTERN const wasm_limits_t* wasm_tabletype_limits(const wasm_tabletype_t*);
// Memory Types
WASM_DECLARE_TYPE(memorytype)
WASM_API_EXTERN own wasm_memorytype_t* wasm_memorytype_new(const wasm_limits_t*);
WASM_API_EXTERN const wasm_limits_t* wasm_memorytype_limits(const wasm_memorytype_t*);
// Extern Types
WASM_DECLARE_TYPE(externtype)
typedef uint8_t wasm_externkind_t;
enum wasm_externkind_enum {
WASM_EXTERN_FUNC,
WASM_EXTERN_GLOBAL,
WASM_EXTERN_TABLE,
WASM_EXTERN_MEMORY,
};
WASM_API_EXTERN wasm_externkind_t wasm_externtype_kind(const wasm_externtype_t*);
WASM_API_EXTERN wasm_externtype_t* wasm_functype_as_externtype(wasm_functype_t*);
WASM_API_EXTERN wasm_externtype_t* wasm_globaltype_as_externtype(wasm_globaltype_t*);
WASM_API_EXTERN wasm_externtype_t* wasm_tabletype_as_externtype(wasm_tabletype_t*);
WASM_API_EXTERN wasm_externtype_t* wasm_memorytype_as_externtype(wasm_memorytype_t*);
WASM_API_EXTERN wasm_functype_t* wasm_externtype_as_functype(wasm_externtype_t*);
WASM_API_EXTERN wasm_globaltype_t* wasm_externtype_as_globaltype(wasm_externtype_t*);
WASM_API_EXTERN wasm_tabletype_t* wasm_externtype_as_tabletype(wasm_externtype_t*);
WASM_API_EXTERN wasm_memorytype_t* wasm_externtype_as_memorytype(wasm_externtype_t*);
WASM_API_EXTERN const wasm_externtype_t* wasm_functype_as_externtype_const(const wasm_functype_t*);
WASM_API_EXTERN const wasm_externtype_t* wasm_globaltype_as_externtype_const(const wasm_globaltype_t*);
WASM_API_EXTERN const wasm_externtype_t* wasm_tabletype_as_externtype_const(const wasm_tabletype_t*);
WASM_API_EXTERN const wasm_externtype_t* wasm_memorytype_as_externtype_const(const wasm_memorytype_t*);
WASM_API_EXTERN const wasm_functype_t* wasm_externtype_as_functype_const(const wasm_externtype_t*);
WASM_API_EXTERN const wasm_globaltype_t* wasm_externtype_as_globaltype_const(const wasm_externtype_t*);
WASM_API_EXTERN const wasm_tabletype_t* wasm_externtype_as_tabletype_const(const wasm_externtype_t*);
WASM_API_EXTERN const wasm_memorytype_t* wasm_externtype_as_memorytype_const(const wasm_externtype_t*);
// Import Types
WASM_DECLARE_TYPE(importtype)
WASM_API_EXTERN own wasm_importtype_t* wasm_importtype_new(
own wasm_name_t* module, own wasm_name_t* name, own wasm_externtype_t*);
WASM_API_EXTERN const wasm_name_t* wasm_importtype_module(const wasm_importtype_t*);
WASM_API_EXTERN const wasm_name_t* wasm_importtype_name(const wasm_importtype_t*);
WASM_API_EXTERN const wasm_externtype_t* wasm_importtype_type(const wasm_importtype_t*);
// Export Types
WASM_DECLARE_TYPE(exporttype)
WASM_API_EXTERN own wasm_exporttype_t* wasm_exporttype_new(
own wasm_name_t*, own wasm_externtype_t*);
WASM_API_EXTERN const wasm_name_t* wasm_exporttype_name(const wasm_exporttype_t*);
WASM_API_EXTERN const wasm_externtype_t* wasm_exporttype_type(const wasm_exporttype_t*);
///////////////////////////////////////////////////////////////////////////////
// Runtime Objects
// Values
struct wasm_ref_t;
typedef struct wasm_val_t {
wasm_valkind_t kind;
union {
int32_t i32;
int64_t i64;
float32_t f32;
float64_t f64;
struct wasm_ref_t* ref;
} of;
} wasm_val_t;
WASM_API_EXTERN void wasm_val_delete(own wasm_val_t* v);
WASM_API_EXTERN void wasm_val_copy(own wasm_val_t* out, const wasm_val_t*);
WASM_DECLARE_VEC(val, )
// References
#define WASM_DECLARE_REF_BASE(name) \
WASM_DECLARE_OWN(name) \
\
WASM_API_EXTERN own wasm_##name##_t* wasm_##name##_copy(const wasm_##name##_t*); \
WASM_API_EXTERN bool wasm_##name##_same(const wasm_##name##_t*, const wasm_##name##_t*); \
\
WASM_API_EXTERN void* wasm_##name##_get_host_info(const wasm_##name##_t*); \
WASM_API_EXTERN void wasm_##name##_set_host_info(wasm_##name##_t*, void*); \
WASM_API_EXTERN void wasm_##name##_set_host_info_with_finalizer( \
wasm_##name##_t*, void*, void (*)(void*));
#define WASM_DECLARE_REF(name) \
WASM_DECLARE_REF_BASE(name) \
\
WASM_API_EXTERN wasm_ref_t* wasm_##name##_as_ref(wasm_##name##_t*); \
WASM_API_EXTERN wasm_##name##_t* wasm_ref_as_##name(wasm_ref_t*); \
WASM_API_EXTERN const wasm_ref_t* wasm_##name##_as_ref_const(const wasm_##name##_t*); \
WASM_API_EXTERN const wasm_##name##_t* wasm_ref_as_##name##_const(const wasm_ref_t*);
#define WASM_DECLARE_SHARABLE_REF(name) \
WASM_DECLARE_REF(name) \
WASM_DECLARE_OWN(shared_##name) \
\
WASM_API_EXTERN own wasm_shared_##name##_t* wasm_##name##_share(const wasm_##name##_t*); \
WASM_API_EXTERN own wasm_##name##_t* wasm_##name##_obtain(wasm_store_t*, const wasm_shared_##name##_t*);
WASM_DECLARE_REF_BASE(ref)
// Frames
WASM_DECLARE_OWN(frame)
WASM_DECLARE_VEC(frame, *)
WASM_API_EXTERN own wasm_frame_t* wasm_frame_copy(const wasm_frame_t*);
WASM_API_EXTERN struct wasm_instance_t* wasm_frame_instance(const wasm_frame_t*);
WASM_API_EXTERN uint32_t wasm_frame_func_index(const wasm_frame_t*);
WASM_API_EXTERN size_t wasm_frame_func_offset(const wasm_frame_t*);
WASM_API_EXTERN size_t wasm_frame_module_offset(const wasm_frame_t*);
// Traps
typedef wasm_name_t wasm_message_t; // null terminated
WASM_DECLARE_REF(trap)
WASM_API_EXTERN own wasm_trap_t* wasm_trap_new(wasm_store_t* store, const wasm_message_t*);
WASM_API_EXTERN void wasm_trap_message(const wasm_trap_t*, own wasm_message_t* out);
WASM_API_EXTERN own wasm_frame_t* wasm_trap_origin(const wasm_trap_t*);
WASM_API_EXTERN void wasm_trap_trace(const wasm_trap_t*, own wasm_frame_vec_t* out);
// Foreign Objects
WASM_DECLARE_REF(foreign)
WASM_API_EXTERN own wasm_foreign_t* wasm_foreign_new(wasm_store_t*);
// Modules
WASM_DECLARE_SHARABLE_REF(module)
WASM_API_EXTERN own wasm_module_t* wasm_module_new(
wasm_store_t*, const wasm_byte_vec_t* binary);
WASM_API_EXTERN bool wasm_module_validate(wasm_store_t*, const wasm_byte_vec_t* binary);
WASM_API_EXTERN void wasm_module_imports(const wasm_module_t*, own wasm_importtype_vec_t* out);
WASM_API_EXTERN void wasm_module_exports(const wasm_module_t*, own wasm_exporttype_vec_t* out);
WASM_API_EXTERN void wasm_module_serialize(const wasm_module_t*, own wasm_byte_vec_t* out);
WASM_API_EXTERN own wasm_module_t* wasm_module_deserialize(wasm_store_t*, const wasm_byte_vec_t*);
// Function Instances
WASM_DECLARE_REF(func)
typedef own wasm_trap_t* (*wasm_func_callback_t)(
const wasm_val_t args[], wasm_val_t results[]);
typedef own wasm_trap_t* (*wasm_func_callback_with_env_t)(
void* env, const wasm_val_t args[], wasm_val_t results[]);
WASM_API_EXTERN own wasm_func_t* wasm_func_new(
wasm_store_t*, const wasm_functype_t*, wasm_func_callback_t);
WASM_API_EXTERN own wasm_func_t* wasm_func_new_with_env(
wasm_store_t*, const wasm_functype_t* type, wasm_func_callback_with_env_t,
void* env, void (*finalizer)(void*));
WASM_API_EXTERN own wasm_functype_t* wasm_func_type(const wasm_func_t*);
WASM_API_EXTERN size_t wasm_func_param_arity(const wasm_func_t*);
WASM_API_EXTERN size_t wasm_func_result_arity(const wasm_func_t*);
WASM_API_EXTERN own wasm_trap_t* wasm_func_call(
const wasm_func_t*, const wasm_val_t args[], wasm_val_t results[]);
// Global Instances
WASM_DECLARE_REF(global)
WASM_API_EXTERN own wasm_global_t* wasm_global_new(
wasm_store_t*, const wasm_globaltype_t*, const wasm_val_t*);
WASM_API_EXTERN own wasm_globaltype_t* wasm_global_type(const wasm_global_t*);
WASM_API_EXTERN void wasm_global_get(const wasm_global_t*, own wasm_val_t* out);
WASM_API_EXTERN void wasm_global_set(wasm_global_t*, const wasm_val_t*);
// Table Instances
WASM_DECLARE_REF(table)
typedef uint32_t wasm_table_size_t;
WASM_API_EXTERN own wasm_table_t* wasm_table_new(
wasm_store_t*, const wasm_tabletype_t*, wasm_ref_t* init);
WASM_API_EXTERN own wasm_tabletype_t* wasm_table_type(const wasm_table_t*);
WASM_API_EXTERN own wasm_ref_t* wasm_table_get(const wasm_table_t*, wasm_table_size_t index);
WASM_API_EXTERN bool wasm_table_set(wasm_table_t*, wasm_table_size_t index, wasm_ref_t*);
WASM_API_EXTERN wasm_table_size_t wasm_table_size(const wasm_table_t*);
WASM_API_EXTERN bool wasm_table_grow(wasm_table_t*, wasm_table_size_t delta, wasm_ref_t* init);
// Memory Instances
WASM_DECLARE_REF(memory)
typedef uint32_t wasm_memory_pages_t;
static const size_t MEMORY_PAGE_SIZE = 0x10000;
WASM_API_EXTERN own wasm_memory_t* wasm_memory_new(wasm_store_t*, const wasm_memorytype_t*);
WASM_API_EXTERN own wasm_memorytype_t* wasm_memory_type(const wasm_memory_t*);
WASM_API_EXTERN byte_t* wasm_memory_data(wasm_memory_t*);
WASM_API_EXTERN size_t wasm_memory_data_size(const wasm_memory_t*);
WASM_API_EXTERN wasm_memory_pages_t wasm_memory_size(const wasm_memory_t*);
WASM_API_EXTERN bool wasm_memory_grow(wasm_memory_t*, wasm_memory_pages_t delta);
// Externals
WASM_DECLARE_REF(extern)
WASM_DECLARE_VEC(extern, *)
WASM_API_EXTERN wasm_externkind_t wasm_extern_kind(const wasm_extern_t*);
WASM_API_EXTERN own wasm_externtype_t* wasm_extern_type(const wasm_extern_t*);
WASM_API_EXTERN wasm_extern_t* wasm_func_as_extern(wasm_func_t*);
WASM_API_EXTERN wasm_extern_t* wasm_global_as_extern(wasm_global_t*);
WASM_API_EXTERN wasm_extern_t* wasm_table_as_extern(wasm_table_t*);
WASM_API_EXTERN wasm_extern_t* wasm_memory_as_extern(wasm_memory_t*);
WASM_API_EXTERN wasm_func_t* wasm_extern_as_func(wasm_extern_t*);
WASM_API_EXTERN wasm_global_t* wasm_extern_as_global(wasm_extern_t*);
WASM_API_EXTERN wasm_table_t* wasm_extern_as_table(wasm_extern_t*);
WASM_API_EXTERN wasm_memory_t* wasm_extern_as_memory(wasm_extern_t*);
WASM_API_EXTERN const wasm_extern_t* wasm_func_as_extern_const(const wasm_func_t*);
WASM_API_EXTERN const wasm_extern_t* wasm_global_as_extern_const(const wasm_global_t*);
WASM_API_EXTERN const wasm_extern_t* wasm_table_as_extern_const(const wasm_table_t*);
WASM_API_EXTERN const wasm_extern_t* wasm_memory_as_extern_const(const wasm_memory_t*);
WASM_API_EXTERN const wasm_func_t* wasm_extern_as_func_const(const wasm_extern_t*);
WASM_API_EXTERN const wasm_global_t* wasm_extern_as_global_const(const wasm_extern_t*);
WASM_API_EXTERN const wasm_table_t* wasm_extern_as_table_const(const wasm_extern_t*);
WASM_API_EXTERN const wasm_memory_t* wasm_extern_as_memory_const(const wasm_extern_t*);
// Module Instances
WASM_DECLARE_REF(instance)
WASM_API_EXTERN own wasm_instance_t* wasm_instance_new(
wasm_store_t*, const wasm_module_t*, const wasm_extern_t* const imports[],
own wasm_trap_t**
);
WASM_API_EXTERN void wasm_instance_exports(const wasm_instance_t*, own wasm_extern_vec_t* out);
WASM_API_EXTERN uint32_t wasm_instance_func_index(const wasm_instance_t*, const wasm_func_t*);
///////////////////////////////////////////////////////////////////////////////
// Convenience
// Value Type construction short-hands
static inline own wasm_valtype_t* wasm_valtype_new_i32() {
return wasm_valtype_new(WASM_I32);
}
static inline own wasm_valtype_t* wasm_valtype_new_i64() {
return wasm_valtype_new(WASM_I64);
}
static inline own wasm_valtype_t* wasm_valtype_new_f32() {
return wasm_valtype_new(WASM_F32);
}
static inline own wasm_valtype_t* wasm_valtype_new_f64() {
return wasm_valtype_new(WASM_F64);
}
static inline own wasm_valtype_t* wasm_valtype_new_anyref() {
return wasm_valtype_new(WASM_ANYREF);
}
static inline own wasm_valtype_t* wasm_valtype_new_funcref() {
return wasm_valtype_new(WASM_FUNCREF);
}
// Function Types construction short-hands
static inline own wasm_functype_t* wasm_functype_new_0_0() {
wasm_valtype_vec_t params, results;
wasm_valtype_vec_new_empty(&params);
wasm_valtype_vec_new_empty(&results);
return wasm_functype_new(&params, &results);
}
static inline own wasm_functype_t* wasm_functype_new_1_0(
own wasm_valtype_t* p
) {
wasm_valtype_t* ps[1] = {p};
wasm_valtype_vec_t params, results;
wasm_valtype_vec_new(&params, 1, ps);
wasm_valtype_vec_new_empty(&results);
return wasm_functype_new(&params, &results);
}
static inline own wasm_functype_t* wasm_functype_new_2_0(
own wasm_valtype_t* p1, own wasm_valtype_t* p2
) {
wasm_valtype_t* ps[2] = {p1, p2};
wasm_valtype_vec_t params, results;
wasm_valtype_vec_new(&params, 2, ps);
wasm_valtype_vec_new_empty(&results);
return wasm_functype_new(&params, &results);
}
static inline own wasm_functype_t* wasm_functype_new_3_0(
own wasm_valtype_t* p1, own wasm_valtype_t* p2, own wasm_valtype_t* p3
) {
wasm_valtype_t* ps[3] = {p1, p2, p3};
wasm_valtype_vec_t params, results;
wasm_valtype_vec_new(&params, 3, ps);
wasm_valtype_vec_new_empty(&results);
return wasm_functype_new(&params, &results);
}
static inline own wasm_functype_t* wasm_functype_new_0_1(
own wasm_valtype_t* r
) {
wasm_valtype_t* rs[1] = {r};
wasm_valtype_vec_t params, results;
wasm_valtype_vec_new_empty(&params);
wasm_valtype_vec_new(&results, 1, rs);
return wasm_functype_new(&params, &results);
}
static inline own wasm_functype_t* wasm_functype_new_1_1(
own wasm_valtype_t* p, own wasm_valtype_t* r
) {
wasm_valtype_t* ps[1] = {p};
wasm_valtype_t* rs[1] = {r};
wasm_valtype_vec_t params, results;
wasm_valtype_vec_new(&params, 1, ps);
wasm_valtype_vec_new(&results, 1, rs);
return wasm_functype_new(&params, &results);
}
static inline own wasm_functype_t* wasm_functype_new_2_1(
own wasm_valtype_t* p1, own wasm_valtype_t* p2, own wasm_valtype_t* r
) {
wasm_valtype_t* ps[2] = {p1, p2};
wasm_valtype_t* rs[1] = {r};
wasm_valtype_vec_t params, results;
wasm_valtype_vec_new(&params, 2, ps);
wasm_valtype_vec_new(&results, 1, rs);
return wasm_functype_new(&params, &results);
}
static inline own wasm_functype_t* wasm_functype_new_3_1(
own wasm_valtype_t* p1, own wasm_valtype_t* p2, own wasm_valtype_t* p3,
own wasm_valtype_t* r
) {
wasm_valtype_t* ps[3] = {p1, p2, p3};
wasm_valtype_t* rs[1] = {r};
wasm_valtype_vec_t params, results;
wasm_valtype_vec_new(&params, 3, ps);
wasm_valtype_vec_new(&results, 1, rs);
return wasm_functype_new(&params, &results);
}
static inline own wasm_functype_t* wasm_functype_new_0_2(
own wasm_valtype_t* r1, own wasm_valtype_t* r2
) {
wasm_valtype_t* rs[2] = {r1, r2};
wasm_valtype_vec_t params, results;
wasm_valtype_vec_new_empty(&params);
wasm_valtype_vec_new(&results, 2, rs);
return wasm_functype_new(&params, &results);
}
static inline own wasm_functype_t* wasm_functype_new_1_2(
own wasm_valtype_t* p, own wasm_valtype_t* r1, own wasm_valtype_t* r2
) {
wasm_valtype_t* ps[1] = {p};
wasm_valtype_t* rs[2] = {r1, r2};
wasm_valtype_vec_t params, results;
wasm_valtype_vec_new(&params, 1, ps);
wasm_valtype_vec_new(&results, 2, rs);
return wasm_functype_new(&params, &results);
}
static inline own wasm_functype_t* wasm_functype_new_2_2(
own wasm_valtype_t* p1, own wasm_valtype_t* p2,
own wasm_valtype_t* r1, own wasm_valtype_t* r2
) {
wasm_valtype_t* ps[2] = {p1, p2};
wasm_valtype_t* rs[2] = {r1, r2};
wasm_valtype_vec_t params, results;
wasm_valtype_vec_new(&params, 2, ps);
wasm_valtype_vec_new(&results, 2, rs);
return wasm_functype_new(&params, &results);
}
static inline own wasm_functype_t* wasm_functype_new_3_2(
own wasm_valtype_t* p1, own wasm_valtype_t* p2, own wasm_valtype_t* p3,
own wasm_valtype_t* r1, own wasm_valtype_t* r2
) {
wasm_valtype_t* ps[3] = {p1, p2, p3};
wasm_valtype_t* rs[2] = {r1, r2};
wasm_valtype_vec_t params, results;
wasm_valtype_vec_new(&params, 3, ps);
wasm_valtype_vec_new(&results, 2, rs);
return wasm_functype_new(&params, &results);
}
// Value construction short-hands
static inline void wasm_val_init_ptr(own wasm_val_t* out, void* p) {
#if UINTPTR_MAX == UINT32_MAX
out->kind = WASM_I32;
out->of.i32 = (intptr_t)p;
#elif UINTPTR_MAX == UINT64_MAX
out->kind = WASM_I64;
out->of.i64 = (intptr_t)p;
#endif
}
static inline void* wasm_val_ptr(const wasm_val_t* val) {
#if UINTPTR_MAX == UINT32_MAX
return (void*)(intptr_t)val->of.i32;
#elif UINTPTR_MAX == UINT64_MAX
return (void*)(intptr_t)val->of.i64;
#endif
}
///////////////////////////////////////////////////////////////////////////////
#undef own
#ifdef __cplusplus
} // extern "C"
#endif
#endif // #ifdef WASM_H

1
third_party/wasm/wamr vendored Submodule

@ -0,0 +1 @@
Subproject commit a9658c245fdf0eb2ce0cbdb117184b1fb25f83db