Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace wasm engine with walrus #1192

Merged
merged 2 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 3 additions & 13 deletions .github/workflows/es-actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
brew install cmake ninja pkg-config icu4c
- name: Build x64
env:
BUILD_OPTIONS: -DESCARGOT_MODE=debug -DESCARGOT_LIBICU_SUPPORT_WITH_DLOPEN=OFF -DESCARGOT_TEMPORAL=ON -DESCARGOT_TCO=ON -DESCARGOT_TEST=ON -DESCARGOT_OUTPUT=shell -GNinja
BUILD_OPTIONS: -DESCARGOT_MODE=debug -DESCARGOT_LIBICU_SUPPORT_WITH_DLOPEN=OFF -DESCARGOT_WASM=ON -DESCARGOT_TEMPORAL=ON -DESCARGOT_TCO=ON -DESCARGOT_TEST=ON -DESCARGOT_OUTPUT=shell -GNinja
run: |
export PKG_CONFIG_PATH="/usr/local/opt/icu4c/lib/pkgconfig"
cmake -H. -Bout/mac/x64 $BUILD_OPTIONS
Expand Down Expand Up @@ -102,11 +102,6 @@ jobs:
run: |
sudo apt-get update
sudo apt-get install -y ninja-build gcc-multilib g++-multilib libicu-dev
- name: Patch WABT
working-directory: ./third_party/wasm/wabt
run: |
cp ../../../tools/test/wasm-js/wabt_patch .
patch -p0 < wabt_patch
- name: Build x86
env:
BUILD_OPTIONS: -DCMAKE_SYSTEM_NAME=Linux -DCMAKE_SYSTEM_PROCESSOR=x86 -DESCARGOT_MODE=debug -DESCARGOT_WASM=ON -DESCARGOT_TEMPORAL=ON -DESCARGOT_TCO=ON -DESCARGOT_TEST=ON -DESCARGOT_OUTPUT=shell -GNinja
Expand Down Expand Up @@ -159,7 +154,7 @@ jobs:
sdk: "10.0.20348.0"
- name: Build ${{ matrix.arch.cpu }} Release
run: |
CMake -DCMAKE_SYSTEM_NAME=Windows -DCMAKE_SYSTEM_VERSION:STRING="10.0" -DCMAKE_SYSTEM_PROCESSOR=${{ matrix.arch.cpu }} -Bout/ -DESCARGOT_OUTPUT=shell -DESCARGOT_LIBICU_SUPPORT=ON -DESCARGOT_LIBICU_SUPPORT_WITH_DLOPEN=NO -DESCARGOT_THREADING=ON -DESCARGOT_TCO=ON -DESCARGOT_TEST=ON -G Ninja -DCMAKE_C_COMPILER=clang-cl -DCMAKE_CXX_COMPILER=clang-cl -DCMAKE_BUILD_TYPE=release -DCMAKE_C_FLAGS="${{ matrix.arch.flag }}" -DCMAKE_CXX_FLAGS="${{ matrix.arch.flag }}"
CMake -DCMAKE_SYSTEM_NAME=Windows -DCMAKE_SYSTEM_VERSION:STRING="10.0" -DCMAKE_SYSTEM_PROCESSOR=${{ matrix.arch.cpu }} -Bout/ -DESCARGOT_OUTPUT=shell -DESCARGOT_LIBICU_SUPPORT=ON -DESCARGOT_LIBICU_SUPPORT_WITH_DLOPEN=NO -DESCARGOT_WASM=ON -DESCARGOT_THREADING=ON -DESCARGOT_TCO=ON -DESCARGOT_TEST=ON -G Ninja -DCMAKE_C_COMPILER=clang-cl -DCMAKE_CXX_COMPILER=clang-cl -DCMAKE_BUILD_TYPE=release -DCMAKE_C_FLAGS="${{ matrix.arch.flag }}" -DCMAKE_CXX_FLAGS="${{ matrix.arch.flag }}"
CMake --build out/ --config Release
- name: Run octane
run: |
Expand Down Expand Up @@ -210,7 +205,7 @@ jobs:
sdk: "10.0.20348.0"
- name: Build ${{ matrix.arch }} Release
run: |
CMake -DCMAKE_SYSTEM_NAME=Windows -DCMAKE_SYSTEM_VERSION:STRING="10.0" -DCMAKE_SYSTEM_PROCESSOR=${{ matrix.arch }} -DESCARGOT_ARCH=${{ matrix.arch }} -Bout/ -DESCARGOT_OUTPUT=shell -DESCARGOT_LIBICU_SUPPORT=ON -DESCARGOT_LIBICU_SUPPORT_WITH_DLOPEN=OFF -DESCARGOT_THREADING=ON -DESCARGOT_TCO=ON -DESCARGOT_TEST=ON -G Ninja -DCMAKE_C_COMPILER=cl -DCMAKE_CXX_COMPILER=cl -DCMAKE_BUILD_TYPE=release
CMake -DCMAKE_SYSTEM_NAME=Windows -DCMAKE_SYSTEM_VERSION:STRING="10.0" -DCMAKE_SYSTEM_PROCESSOR=${{ matrix.arch }} -DESCARGOT_ARCH=${{ matrix.arch }} -Bout/ -DESCARGOT_OUTPUT=shell -DESCARGOT_LIBICU_SUPPORT=ON -DESCARGOT_LIBICU_SUPPORT_WITH_DLOPEN=OFF -DESCARGOT_WASM=ON -DESCARGOT_THREADING=ON -DESCARGOT_TCO=ON -DESCARGOT_TEST=ON -G Ninja -DCMAKE_C_COMPILER=cl -DCMAKE_CXX_COMPILER=cl -DCMAKE_BUILD_TYPE=release
CMake --build out/ --config Release
# windows internal ICU doesn't support Temporal and intl402 well
# github action windows runner only have 2 CPUs. that's why I disable Atomics(timeout occured with some tests)
Expand Down Expand Up @@ -486,11 +481,6 @@ jobs:
run: |
sudo apt-get update
sudo apt-get install -y ninja-build gcc-multilib g++-multilib
- name: Patch WABT
working-directory: ./third_party/wasm/wabt
run: |
cp ../../../tools/test/wasm-js/wabt_patch .
patch -p0 < wabt_patch
- name: Build x86
env:
BUILD_OPTIONS: -DCMAKE_SYSTEM_NAME=Linux -DCMAKE_SYSTEM_PROCESSOR=x86 -DESCARGOT_MODE=debug -DESCARGOT_WASM=ON -DESCARGOT_TEMPORAL=ON -DESCARGOT_TCO=ON -DESCARGOT_TEST=ON -DESCARGOT_OUTPUT=shell -GNinja
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,4 @@ cmake_install.cmake
#etc
.vscode
EscargotInfo.h
third_party/wasm/config.h
escargot.pc
8 changes: 4 additions & 4 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,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
ignore = untracked
[submodule "third_party/walrus"]
path = third_party/walrus
url = https://github.com/Samsung/walrus.git
ignore = untracked
16 changes: 10 additions & 6 deletions build/escargot.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -135,20 +135,24 @@ IF (ESCARGOT_LIBICU_SUPPORT_WITH_DLOPEN)
SET (ESCARGOT_LIBRARIES ${ESCARGOT_LIBRARIES} runtime-icu-binder-static)
ENDIF()

# WebAssembly (wabt)
# WebAssembly (walrus)
IF (ESCARGOT_WASM)
SET (WASM_CXX_FLAGS
SET (WALRUS_CXXFLAGS
${ESCARGOT_THIRDPARTY_CFLAGS} # we can share flags with gcutil
)
-g3)
SET (WASM_ARCH ${ESCARGOT_ARCH})
SET (WALRUS_HOST ${ESCARGOT_HOST})
SET (WALRUS_ARCH ${ESCARGOT_ARCH})
SET (WALRUS_MODE ${ESCARGOT_MODE})
SET (WALRUS_OUTPUT "shared_lib")

IF (${ESCARGOT_MODE} STREQUAL "release")
SET (WASM_CXX_FLAGS ${WASM_CXX_FLAGS} ${ESCARGOT_CXXFLAGS_RELEASE})
SET (WALRUS_CXXFLAGS ${WALRUS_CXXFLAGS} ${ESCARGOT_CXXFLAGS_RELEASE})
ENDIF()

ADD_SUBDIRECTORY (third_party/wasm)
ADD_SUBDIRECTORY (third_party/walrus)

SET (ESCARGOT_LIBRARIES ${ESCARGOT_LIBRARIES} wasm)
SET (ESCARGOT_LIBRARIES ${ESCARGOT_LIBRARIES} walrus)
ENDIF()

# BUILD
Expand Down
11 changes: 0 additions & 11 deletions src/api/EscargotPublic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4629,11 +4629,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)
{
Expand All @@ -4655,12 +4650,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
1 change: 0 additions & 1 deletion src/api/EscargotPublic.h
Original file line number Diff line number Diff line change
Expand Up @@ -2216,7 +2216,6 @@ class ESCARGOT_EXPORT WASMOperationsRef {
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
Expand Down
9 changes: 0 additions & 9 deletions src/runtime/ThreadLocal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,4 @@ void ThreadLocal::finalize()
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
8 changes: 0 additions & 8 deletions src/runtime/ThreadLocal.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,19 +123,11 @@ class ThreadLocal {
}

#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()
Expand Down
11 changes: 0 additions & 11 deletions src/runtime/VMInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,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());
Expand Down Expand Up @@ -245,11 +239,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();
Expand Down
2 changes: 1 addition & 1 deletion src/wasm/BuiltinWASM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,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) {
Expand Down
2 changes: 1 addition & 1 deletion src/wasm/ExportedFunctionObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ static Value callExportedFunction(ExecutionState& state, Value thisValue, size_t
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) {
Expand Down
52 changes: 34 additions & 18 deletions src/wasm/WASMOperations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -63,11 +63,37 @@ 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).
Value ret = Object::call(state, func, Value(), argSize, jsArguments);
SandBox sb(funcEnv->realm);

struct CallBackData {
Object* func;
size_t argSize;
Value* arguments;
} callBackData;
callBackData.func = func;
callBackData.argSize = argSize;
callBackData.arguments = jsArguments;

auto result = sb.run([](ExecutionState& state, void* data) -> Value {
CallBackData* callBackData = (CallBackData*)data;
return Object::call(state, callBackData->func, Value(), callBackData->argSize, callBackData->arguments);
},
&callBackData);

// Assert: result.[[Type]] is throw or normal.
// If result.[[Type]] is throw, then trigger a WebAssembly trap, and propagate result.[[Value]] to the enclosing JavaScript.
// Otherwise, return result.[[Value]].
if (UNLIKELY(!result.error.isEmpty())) {
std::string errMessage = result.error.toStringWithoutException(state)->toNonGCUTF8StringData();
// throw a WASM exception
wasm_trap_trigger(errMessage.data(), errMessage.length());
ASSERT_NOT_REACHED();
return nullptr;
}

// Let resultsSize be results?s size.
size_t resultsSize = res->size;
Expand All @@ -76,9 +102,10 @@ static own wasm_trap_t* callbackHostFunction(void* env, const wasm_val_t args[],
return nullptr;
}

Value ret = result.result;
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).
Expand All @@ -98,15 +125,10 @@ 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]));
}
}

// TODO
// Assert: result.[[Type]] is throw or normal.
// If result.[[Type]] is throw, then trigger a WebAssembly trap, and propagate result.[[Value]] to the enclosing JavaScript.
// Otherwise, return result.[[Value]].

return nullptr;
}

Expand Down Expand Up @@ -146,7 +168,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) {
Expand Down Expand Up @@ -526,7 +548,7 @@ Value WASMOperations::instantiateCoreModule(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) {
Expand Down Expand Up @@ -558,12 +580,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
2 changes: 0 additions & 2 deletions src/wasm/WASMOperations.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ class WASMOperations {
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
Expand Down
7 changes: 7 additions & 0 deletions src/wasm/WASMValueConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@

namespace Escargot {

// redefine macros due to compile error in msvc
#undef WASM_I32_VAL
#undef WASM_I64_VAL
#undef WASM_F32_VAL
#undef WASM_F64_VAL
#undef WASM_REF_VAL
#undef WASM_INIT_VAL
#define WASM_I32_VAL(result, i) \
result.kind = WASM_I32; \
result.of.i32 = i;
Expand Down
1 change: 1 addition & 0 deletions third_party/walrus
Submodule walrus added at af827e
Loading