Skip to content

Commit f7e0968

Browse files
liamappelbecommit-bot@chromium.org
authored andcommitted
[vm] Remove redundant classes from wasm.cc
These classes just thinly wrap wasmer objects, so we don't need them. Bug: #37882 Change-Id: I06d715cc823c48101da3730ee68dd11adf5eb523 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/117420 Commit-Queue: Liam Appelbe <[email protected]> Reviewed-by: Alexander Markov <[email protected]>
1 parent b0fa0ae commit f7e0968

File tree

1 file changed

+48
-69
lines changed

1 file changed

+48
-69
lines changed

runtime/lib/wasm.cc

+48-69
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ static void Finalize(void* isolate_callback_data,
3434
delete reinterpret_cast<T*>(peer);
3535
}
3636

37+
static void FinalizeWasmModule(void* isolate_callback_data,
38+
Dart_WeakPersistentHandle handle,
39+
void* module) {
40+
wasmer_module_destroy(reinterpret_cast<wasmer_module_t*>(module));
41+
}
42+
43+
static void FinalizeWasmMemory(void* isolate_callback_data,
44+
Dart_WeakPersistentHandle handle,
45+
void* memory) {
46+
wasmer_memory_destroy(reinterpret_cast<wasmer_memory_t*>(memory));
47+
}
48+
3749
static std::unique_ptr<char[]> ToUTF8(const String& str) {
3850
const intptr_t str_size = Utf8::Length(str);
3951
auto str_raw = std::unique_ptr<char[]>(new char[str_size + 1]);
@@ -87,53 +99,11 @@ static RawObject* ToDartObject(wasmer_value_t ret) {
8799
}
88100
}
89101

90-
class WasmModule {
91-
public:
92-
WasmModule(uint8_t* data, intptr_t len) {
93-
ThrowIfFailed(wasmer_compile(&_module, data, len));
94-
}
95-
96-
~WasmModule() { wasmer_module_destroy(_module); }
97-
wasmer_module_t* module() { return _module; }
98-
99-
private:
100-
wasmer_module_t* _module;
101-
102-
DISALLOW_COPY_AND_ASSIGN(WasmModule);
103-
};
104-
105-
class WasmMemory {
106-
public:
107-
WasmMemory(uint32_t init, int64_t max) {
108-
wasmer_limits_t descriptor;
109-
descriptor.min = init;
110-
if (max < 0) {
111-
descriptor.max.has_some = false;
112-
} else {
113-
descriptor.max.has_some = true;
114-
descriptor.max.some = max;
115-
}
116-
ThrowIfFailed(wasmer_memory_new(&_memory, descriptor));
117-
}
118-
119-
~WasmMemory() { wasmer_memory_destroy(_memory); }
120-
wasmer_memory_t* memory() { return _memory; }
121-
122-
void Grow(intptr_t delta) {
123-
ThrowIfFailed(wasmer_memory_grow(_memory, delta));
124-
}
125-
126-
RawExternalTypedData* ToExternalTypedData() {
127-
uint8_t* data = wasmer_memory_data(_memory);
128-
uint32_t size = wasmer_memory_data_length(_memory);
129-
return ExternalTypedData::New(kExternalTypedDataUint8ArrayCid, data, size);
130-
}
131-
132-
private:
133-
wasmer_memory_t* _memory;
134-
135-
DISALLOW_COPY_AND_ASSIGN(WasmMemory);
136-
};
102+
RawExternalTypedData* WasmMemoryToExternalTypedData(wasmer_memory_t* memory) {
103+
uint8_t* data = wasmer_memory_data(memory);
104+
uint32_t size = wasmer_memory_data_length(memory);
105+
return ExternalTypedData::New(kExternalTypedDataUint8ArrayCid, data, size);
106+
}
137107

138108
class WasmImports {
139109
public:
@@ -152,9 +122,9 @@ class WasmImports {
152122
size_t NumImports() const { return _imports.length(); }
153123
wasmer_import_t* RawImports() { return _imports.data(); }
154124

155-
void AddMemory(std::unique_ptr<char[]> name, WasmMemory* memory) {
125+
void AddMemory(std::unique_ptr<char[]> name, wasmer_memory_t* memory) {
156126
AddImport(std::move(name), wasmer_import_export_kind::WASM_MEMORY)->memory =
157-
memory->memory();
127+
memory;
158128
}
159129

160130
void AddGlobal(std::unique_ptr<char[]> name,
@@ -256,11 +226,10 @@ class WasmFunction {
256226

257227
class WasmInstance {
258228
public:
259-
explicit WasmInstance(WasmModule* module, WasmImports* imports) {
229+
explicit WasmInstance(wasmer_module_t* module, WasmImports* imports) {
260230
// Instantiate module.
261-
ThrowIfFailed(wasmer_module_instantiate(module->module(), &_instance,
262-
imports->RawImports(),
263-
imports->NumImports()));
231+
ThrowIfFailed(wasmer_module_instantiate(
232+
module, &_instance, imports->RawImports(), imports->NumImports()));
264233

265234
// Load all functions.
266235
wasmer_instance_exports(_instance, &_exports);
@@ -381,15 +350,15 @@ DEFINE_NATIVE_ENTRY(Wasm_initModule, 0, 2) {
381350
memcpy(data_copy.get(), data.DataAddr(0), len); // NOLINT
382351
}
383352

384-
WasmModule* module;
353+
wasmer_module_t* module;
385354
{
386355
TransitionVMToNative transition(thread);
387-
module = new WasmModule(data_copy.get(), len);
356+
ThrowIfFailed(wasmer_compile(&module, data_copy.get(), len));
388357
}
389358

390359
mod_wrap.SetNativeField(0, reinterpret_cast<intptr_t>(module));
391360
FinalizablePersistentHandle::New(thread->isolate(), mod_wrap, module,
392-
Finalize<WasmModule>, sizeof(WasmModule));
361+
FinalizeWasmModule, len);
393362

394363
return Object::null();
395364
}
@@ -419,8 +388,8 @@ DEFINE_NATIVE_ENTRY(Wasm_addMemoryImport, 0, 3) {
419388

420389
WasmImports* imports =
421390
reinterpret_cast<WasmImports*>(imp_wrap.GetNativeField(0));
422-
WasmMemory* memory =
423-
reinterpret_cast<WasmMemory*>(mem_wrap.GetNativeField(0));
391+
wasmer_memory_t* memory =
392+
reinterpret_cast<wasmer_memory_t*>(mem_wrap.GetNativeField(0));
424393

425394
imports->AddMemory(ToUTF8(name), memory);
426395

@@ -455,13 +424,23 @@ DEFINE_NATIVE_ENTRY(Wasm_initMemory, 0, 3) {
455424
GET_NATIVE_ARGUMENT(Integer, max, arguments->NativeArgAt(2));
456425

457426
ASSERT(mem_wrap.NumNativeFields() == 1);
458-
459-
WasmMemory* memory = new WasmMemory(init.AsInt64Value(),
460-
max.IsNull() ? -1 : max.AsInt64Value());
427+
const int64_t init_size = init.AsInt64Value();
428+
const int64_t max_size = max.AsInt64Value();
429+
430+
wasmer_memory_t* memory;
431+
wasmer_limits_t descriptor;
432+
descriptor.min = init_size;
433+
if (max_size < 0) {
434+
descriptor.max.has_some = false;
435+
} else {
436+
descriptor.max.has_some = true;
437+
descriptor.max.some = max_size;
438+
}
439+
ThrowIfFailed(wasmer_memory_new(&memory, descriptor));
461440
mem_wrap.SetNativeField(0, reinterpret_cast<intptr_t>(memory));
462441
FinalizablePersistentHandle::New(thread->isolate(), mem_wrap, memory,
463-
Finalize<WasmMemory>, sizeof(WasmMemory));
464-
return memory->ToExternalTypedData();
442+
FinalizeWasmMemory, init_size);
443+
return WasmMemoryToExternalTypedData(memory);
465444
}
466445

467446
DEFINE_NATIVE_ENTRY(Wasm_growMemory, 0, 2) {
@@ -470,10 +449,10 @@ DEFINE_NATIVE_ENTRY(Wasm_growMemory, 0, 2) {
470449

471450
ASSERT(mem_wrap.NumNativeFields() == 1);
472451

473-
WasmMemory* memory =
474-
reinterpret_cast<WasmMemory*>(mem_wrap.GetNativeField(0));
475-
memory->Grow(delta.AsInt64Value());
476-
return memory->ToExternalTypedData();
452+
wasmer_memory_t* memory =
453+
reinterpret_cast<wasmer_memory_t*>(mem_wrap.GetNativeField(0));
454+
ThrowIfFailed(wasmer_memory_grow(memory, delta.AsInt64Value()));
455+
return WasmMemoryToExternalTypedData(memory);
477456
}
478457

479458
DEFINE_NATIVE_ENTRY(Wasm_initInstance, 0, 3) {
@@ -485,8 +464,8 @@ DEFINE_NATIVE_ENTRY(Wasm_initInstance, 0, 3) {
485464
ASSERT(mod_wrap.NumNativeFields() == 1);
486465
ASSERT(imp_wrap.NumNativeFields() == 1);
487466

488-
WasmModule* module =
489-
reinterpret_cast<WasmModule*>(mod_wrap.GetNativeField(0));
467+
wasmer_module_t* module =
468+
reinterpret_cast<wasmer_module_t*>(mod_wrap.GetNativeField(0));
490469
WasmImports* imports =
491470
reinterpret_cast<WasmImports*>(imp_wrap.GetNativeField(0));
492471

0 commit comments

Comments
 (0)