2525#include < utility>
2626#include < vector>
2727
28- #include " src/common/bytecode_util.h"
29-
3028#include " v8.h"
3129#include " v8-version.h"
3230#include " wasm-api/wasm.hh"
@@ -64,8 +62,8 @@ class V8 : public WasmVm {
6462 // WasmVm
6563 std::string_view runtime () override { return " v8" ; }
6664
67- bool load (const std::string &code, bool allow_precompiled) override ;
68- AbiVersion getAbiVersion ( ) override ;
65+ bool load (std::string_view bytecode, std::string_view precompiled,
66+ const std::unordered_map< uint32_t , std::string> function_names ) override ;
6967 std::string_view getPrecompiledSectionName () override ;
7068 bool link (std::string_view debug_name) override ;
7169
@@ -122,8 +120,6 @@ class V8 : public WasmVm {
122120
123121 std::unordered_map<std::string, FuncDataPtr> host_functions_;
124122 std::unordered_map<std::string, wasm::own<wasm::Func>> module_functions_;
125-
126- AbiVersion abi_version_;
127123 std::unordered_map<uint32_t , std::string> function_names_index_;
128124};
129125
@@ -251,58 +247,31 @@ template <typename T, typename U> constexpr T convertValTypesToArgsTuple(const U
251247
252248// V8 implementation.
253249
254- bool V8::load (const std::string &code, bool allow_precompiled) {
250+ bool V8::load (std::string_view bytecode, std::string_view precompiled,
251+ const std::unordered_map<uint32_t , std::string> function_names) {
255252 store_ = wasm::Store::make (engine ());
256253
257- // Get ABI version from bytecode.
258- if (!common::BytecodeUtil::getAbiVersion (code, abi_version_)) {
259- fail (FailState::UnableToInitializeCode, " Failed to parse corrupted Wasm module" );
260- return false ;
261- }
254+ if (!precompiled.empty ()) {
255+ auto vec = wasm::vec<byte_t >::make_uninitialized (precompiled.size ());
256+ ::memcpy (vec.get(), precompiled.data(), precompiled.size());
257+ module_ = wasm::Module::deserialize (store_.get (), vec);
262258
263- if (allow_precompiled) {
264- const auto section_name = getPrecompiledSectionName ();
265- if (!section_name.empty ()) {
266- std::string_view precompiled = {};
267- if (!common::BytecodeUtil::getCustomSection (code, section_name, precompiled)) {
268- fail (FailState::UnableToInitializeCode, " Failed to parse corrupted Wasm module" );
269- return false ;
270- }
271- if (!precompiled.empty ()) {
272- auto vec = wasm::vec<byte_t >::make_uninitialized (precompiled.size ());
273- ::memcpy (vec.get(), precompiled.data(), precompiled.size());
274-
275- module_ = wasm::Module::deserialize (store_.get (), vec);
276- if (!module_) {
277- // Precompiled module that cannot be loaded is considered a hard error,
278- // so don't fallback to compiling the bytecode.
279- return false ;
280- }
281- }
282- }
259+ } else {
260+ auto vec = wasm::vec<byte_t >::make_uninitialized (bytecode.size ());
261+ ::memcpy (vec.get(), bytecode.data(), bytecode.size());
262+ module_ = wasm::Module::make (store_.get (), vec);
283263 }
284264
285265 if (!module_) {
286- std::string stripped;
287- if (!common::BytecodeUtil::getStrippedSource (code, stripped)) {
288- fail (FailState::UnableToInitializeCode, " Failed to parse corrupted Wasm module" );
289- return false ;
290- };
291- wasm::vec<byte_t > stripped_vec = wasm::vec<byte_t >::make (stripped.size (), stripped.data ());
292- module_ = wasm::Module::make (store_.get (), stripped_vec);
266+ return false ;
293267 }
294268
295- if (module_) {
296- shared_module_ = module_->share ();
297- assert ((shared_module_ != nullptr ));
298- }
269+ shared_module_ = module_->share ();
270+ assert (shared_module_ != nullptr );
299271
300- if (!common::BytecodeUtil::getFunctionNameIndex (code, function_names_index_)) {
301- fail (FailState::UnableToInitializeCode, " Failed to parse corrupted Wasm module" );
302- return false ;
303- };
272+ function_names_index_ = function_names;
304273
305- return module_ != nullptr ;
274+ return true ;
306275}
307276
308277std::unique_ptr<WasmVm> V8::clone () {
@@ -314,7 +283,6 @@ std::unique_ptr<WasmVm> V8::clone() {
314283
315284 clone->module_ = wasm::Module::obtain (clone->store_ .get (), shared_module_.get ());
316285 clone->function_names_index_ = function_names_index_;
317- clone->abi_version_ = abi_version_;
318286
319287 return clone;
320288}
@@ -335,8 +303,6 @@ std::string_view V8::getPrecompiledSectionName() {
335303 return name;
336304}
337305
338- AbiVersion V8::getAbiVersion () { return abi_version_; }
339-
340306bool V8::link (std::string_view debug_name) {
341307 assert (module_ != nullptr );
342308
0 commit comments