2019-07-03 11:32:46 +00:00
|
|
|
// Copyright 2019 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#ifndef TEST_WASM_API_TESTS_WASM_API_TEST_H_
|
|
|
|
#define TEST_WASM_API_TESTS_WASM_API_TEST_H_
|
|
|
|
|
|
|
|
#include "src/wasm/wasm-module-builder.h"
|
|
|
|
#include "src/wasm/wasm-opcodes.h"
|
|
|
|
#include "src/zone/accounting-allocator.h"
|
|
|
|
#include "src/zone/zone.h"
|
|
|
|
#include "test/common/wasm/wasm-macro-gen.h"
|
|
|
|
#include "testing/gtest/include/gtest/gtest.h"
|
|
|
|
#include "third_party/wasm-api/wasm.hh"
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
namespace wasm {
|
|
|
|
|
|
|
|
using ::wasm::Engine;
|
|
|
|
using ::wasm::Extern;
|
|
|
|
using ::wasm::Foreign;
|
|
|
|
using ::wasm::Func;
|
|
|
|
using ::wasm::FuncType;
|
2019-07-08 11:55:29 +00:00
|
|
|
using ::wasm::Global;
|
2019-07-03 11:32:46 +00:00
|
|
|
using ::wasm::Instance;
|
2019-07-08 16:07:51 +00:00
|
|
|
using ::wasm::Memory;
|
2019-07-03 11:32:46 +00:00
|
|
|
using ::wasm::Module;
|
|
|
|
using ::wasm::own;
|
2019-08-19 14:35:47 +00:00
|
|
|
using ::wasm::ownvec;
|
2019-07-03 11:32:46 +00:00
|
|
|
using ::wasm::Ref;
|
|
|
|
using ::wasm::Store;
|
2019-07-11 13:48:21 +00:00
|
|
|
using ::wasm::Table;
|
2019-07-03 11:32:46 +00:00
|
|
|
using ::wasm::Trap;
|
|
|
|
using ::wasm::Val;
|
|
|
|
using ::wasm::ValType;
|
|
|
|
using ::wasm::vec;
|
|
|
|
|
|
|
|
class WasmCapiTest : public ::testing::Test {
|
|
|
|
public:
|
|
|
|
WasmCapiTest()
|
|
|
|
: Test(),
|
2020-07-30 12:37:48 +00:00
|
|
|
engine_(Engine::make()),
|
2019-07-03 11:32:46 +00:00
|
|
|
zone_(&allocator_, ZONE_NAME),
|
2019-08-08 15:59:35 +00:00
|
|
|
wire_bytes_(&zone_),
|
2019-07-03 11:32:46 +00:00
|
|
|
builder_(&zone_),
|
2019-08-19 14:35:47 +00:00
|
|
|
exports_(ownvec<Extern>::make()),
|
2019-07-03 11:32:46 +00:00
|
|
|
wasm_i_i_sig_(1, 1, wasm_i_i_sig_types_) {
|
|
|
|
store_ = Store::make(engine_.get());
|
|
|
|
cpp_i_i_sig_ =
|
2019-08-19 14:35:47 +00:00
|
|
|
FuncType::make(ownvec<ValType>::make(ValType::make(::wasm::I32)),
|
|
|
|
ownvec<ValType>::make(ValType::make(::wasm::I32)));
|
2019-07-03 11:32:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Compile() {
|
2019-08-08 15:59:35 +00:00
|
|
|
builder_.WriteTo(&wire_bytes_);
|
|
|
|
size_t size = wire_bytes_.end() - wire_bytes_.begin();
|
2019-07-03 11:32:46 +00:00
|
|
|
vec<byte_t> binary = vec<byte_t>::make(
|
2019-08-08 15:59:35 +00:00
|
|
|
size,
|
|
|
|
reinterpret_cast<byte_t*>(const_cast<byte*>(wire_bytes_.begin())));
|
2019-07-03 11:32:46 +00:00
|
|
|
|
|
|
|
module_ = Module::make(store_.get(), binary);
|
|
|
|
DCHECK_NE(module_.get(), nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Instantiate(Extern* imports[]) {
|
|
|
|
Compile();
|
|
|
|
instance_ = Instance::make(store_.get(), module_.get(), imports);
|
|
|
|
DCHECK_NE(instance_.get(), nullptr);
|
|
|
|
exports_ = instance_->exports();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddExportedFunction(Vector<const char> name, byte code[],
|
2019-07-08 11:55:29 +00:00
|
|
|
size_t code_size, FunctionSig* sig) {
|
|
|
|
WasmFunctionBuilder* fun = builder()->AddFunction(sig);
|
2019-07-03 11:32:46 +00:00
|
|
|
fun->EmitCode(code, static_cast<uint32_t>(code_size));
|
|
|
|
fun->Emit(kExprEnd);
|
|
|
|
builder()->AddExport(name, fun);
|
|
|
|
}
|
|
|
|
|
|
|
|
Func* GetExportedFunction(size_t index) {
|
|
|
|
DCHECK_GT(exports_.size(), index);
|
2019-08-19 14:35:47 +00:00
|
|
|
Extern* exported = exports_[index].get();
|
2019-07-03 11:32:46 +00:00
|
|
|
DCHECK_EQ(exported->kind(), ::wasm::EXTERN_FUNC);
|
|
|
|
Func* func = exported->func();
|
|
|
|
DCHECK_NE(func, nullptr);
|
|
|
|
return func;
|
|
|
|
}
|
|
|
|
|
2019-07-08 11:55:29 +00:00
|
|
|
Global* GetExportedGlobal(size_t index) {
|
|
|
|
DCHECK_GT(exports_.size(), index);
|
2019-08-19 14:35:47 +00:00
|
|
|
Extern* exported = exports_[index].get();
|
2019-07-08 11:55:29 +00:00
|
|
|
DCHECK_EQ(exported->kind(), ::wasm::EXTERN_GLOBAL);
|
|
|
|
Global* global = exported->global();
|
|
|
|
DCHECK_NE(global, nullptr);
|
|
|
|
return global;
|
|
|
|
}
|
|
|
|
|
2019-07-08 16:07:51 +00:00
|
|
|
Memory* GetExportedMemory(size_t index) {
|
|
|
|
DCHECK_GT(exports_.size(), index);
|
2019-08-19 14:35:47 +00:00
|
|
|
Extern* exported = exports_[index].get();
|
2019-07-08 16:07:51 +00:00
|
|
|
DCHECK_EQ(exported->kind(), ::wasm::EXTERN_MEMORY);
|
|
|
|
Memory* memory = exported->memory();
|
|
|
|
DCHECK_NE(memory, nullptr);
|
|
|
|
return memory;
|
|
|
|
}
|
|
|
|
|
2019-07-11 13:48:21 +00:00
|
|
|
Table* GetExportedTable(size_t index) {
|
|
|
|
DCHECK_GT(exports_.size(), index);
|
2019-08-19 14:35:47 +00:00
|
|
|
Extern* exported = exports_[index].get();
|
2019-07-11 13:48:21 +00:00
|
|
|
DCHECK_EQ(exported->kind(), ::wasm::EXTERN_TABLE);
|
|
|
|
Table* table = exported->table();
|
|
|
|
DCHECK_NE(table, nullptr);
|
|
|
|
return table;
|
|
|
|
}
|
|
|
|
|
2019-07-03 11:32:46 +00:00
|
|
|
void Shutdown() {
|
2019-08-26 12:56:05 +00:00
|
|
|
exports_.reset();
|
2019-07-03 11:32:46 +00:00
|
|
|
instance_.reset();
|
|
|
|
module_.reset();
|
|
|
|
store_.reset();
|
|
|
|
engine_.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
WasmModuleBuilder* builder() { return &builder_; }
|
2019-07-12 14:00:53 +00:00
|
|
|
Engine* engine() { return engine_.get(); }
|
2019-07-03 11:32:46 +00:00
|
|
|
Store* store() { return store_.get(); }
|
|
|
|
Module* module() { return module_.get(); }
|
2019-08-08 15:59:35 +00:00
|
|
|
Instance* instance() { return instance_.get(); }
|
2019-08-19 14:35:47 +00:00
|
|
|
const ownvec<Extern>& exports() { return exports_; }
|
2019-08-08 15:59:35 +00:00
|
|
|
ZoneBuffer* wire_bytes() { return &wire_bytes_; }
|
2019-07-03 11:32:46 +00:00
|
|
|
|
|
|
|
FunctionSig* wasm_i_i_sig() { return &wasm_i_i_sig_; }
|
|
|
|
FuncType* cpp_i_i_sig() { return cpp_i_i_sig_.get(); }
|
|
|
|
|
|
|
|
private:
|
2020-07-30 12:37:48 +00:00
|
|
|
own<Engine> engine_;
|
2019-07-03 11:32:46 +00:00
|
|
|
AccountingAllocator allocator_;
|
|
|
|
Zone zone_;
|
2019-08-08 15:59:35 +00:00
|
|
|
ZoneBuffer wire_bytes_;
|
2019-07-03 11:32:46 +00:00
|
|
|
WasmModuleBuilder builder_;
|
2019-08-19 14:35:47 +00:00
|
|
|
own<Store> store_;
|
|
|
|
own<Module> module_;
|
|
|
|
own<Instance> instance_;
|
|
|
|
ownvec<Extern> exports_;
|
|
|
|
own<FuncType> cpp_i_i_sig_;
|
2019-07-03 11:32:46 +00:00
|
|
|
ValueType wasm_i_i_sig_types_[2] = {kWasmI32, kWasmI32};
|
|
|
|
FunctionSig wasm_i_i_sig_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace wasm
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|
|
|
|
|
|
|
|
#endif // TEST_WASM_API_TESTS_WASM_API_TEST_H_
|