Skip to content

Commit 4137bbd

Browse files
committed
name for wasm instance
1 parent 1a8de1f commit 4137bbd

File tree

2 files changed

+49
-40
lines changed

2 files changed

+49
-40
lines changed

sources/include/cage-core/wasm.h

+49-39
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,36 @@ namespace cage
227227
template<class T>
228228
struct WasmFunction;
229229

230+
template<privat::WasmParameterConcept R, privat::WasmParameterConcept... Ts>
231+
struct WasmFunction<R(Ts...)>;
232+
233+
class CAGE_CORE_API WasmInstance : private Immovable
234+
{
235+
public:
236+
String instanceName;
237+
238+
Holder<WasmModule> module() const;
239+
240+
void *wasm2native(uint32 address, uint32 size = 0);
241+
uint32 native2wasm(void *address, uint32 size = 0);
242+
uint32 malloc(uint32 size);
243+
void free(uint32 wasmAddr);
244+
uint32 strLen(uint32 wasmAddr);
245+
WasmBuffer allocate(uint32 size, uint32 capacity = 0, bool owned = true);
246+
WasmBuffer adapt(uint32 wasmAddr, uint32 size, bool owned = false);
247+
248+
template<class T>
249+
CAGE_FORCE_INLINE WasmFunction<T> function(const WasmName &name)
250+
{
251+
return WasmFunction<T>(function_(name));
252+
}
253+
254+
WasmBuffer temporary; // use for passing data in function calls
255+
256+
private:
257+
Holder<privat::WasmFunctionInternal> function_(const WasmName &name);
258+
};
259+
230260
template<privat::WasmParameterConcept R, privat::WasmParameterConcept... Ts>
231261
struct WasmFunction<R(Ts...)> : private Noncopyable
232262
{
@@ -254,7 +284,7 @@ namespace cage
254284
}
255285
catch (...)
256286
{
257-
CAGE_LOG_THROW(func->name());
287+
CAGE_LOG_THROW(Stringizer() + func->instance()->instanceName + ":" + func->name());
258288
throw;
259289
}
260290
}
@@ -264,51 +294,31 @@ namespace cage
264294
R operator()(Ts... vs)
265295
{
266296
CAGE_ASSERT(func);
267-
PointerRange<uint32> data = func->data();
268-
269-
uint32 offset = 0;
270-
const auto &fill = [&]<class T>(T v) -> void
297+
try
271298
{
272-
*(T *)(void *)(data.data() + offset) = v;
273-
offset += sizeof(T) / 4;
274-
};
275-
(fill(std::forward<Ts>(vs)), ...);
276-
277-
func->call();
278-
279-
if constexpr (!std::is_same_v<R, void>)
280-
return *(R *)(void *)data.data();
299+
PointerRange<uint32> data = func->data();
300+
uint32 offset = 0;
301+
const auto &fill = [&]<class T>(T v) -> void
302+
{
303+
*(T *)(void *)(data.data() + offset) = v;
304+
offset += sizeof(T) / 4;
305+
};
306+
(fill(std::forward<Ts>(vs)), ...);
307+
func->call();
308+
if constexpr (!std::is_same_v<R, void>)
309+
return *(R *)(void *)data.data();
310+
}
311+
catch (...)
312+
{
313+
CAGE_LOG_THROW(Stringizer() + func->instance()->instanceName + ":" + func->name());
314+
throw;
315+
}
281316
}
282317

283318
private:
284319
Holder<privat::WasmFunctionInternal> func;
285320
};
286321

287-
class CAGE_CORE_API WasmInstance : private Immovable
288-
{
289-
public:
290-
Holder<WasmModule> module() const;
291-
292-
void *wasm2native(uint32 address, uint32 size = 0);
293-
uint32 native2wasm(void *address, uint32 size = 0);
294-
uint32 malloc(uint32 size);
295-
void free(uint32 wasmAddr);
296-
uint32 strLen(uint32 wasmAddr);
297-
WasmBuffer allocate(uint32 size, uint32 capacity = 0, bool owned = true);
298-
WasmBuffer adapt(uint32 wasmAddr, uint32 size, bool owned = false);
299-
300-
template<class T>
301-
CAGE_FORCE_INLINE WasmFunction<T> function(const WasmName &name)
302-
{
303-
return WasmFunction<T>(function_(name));
304-
}
305-
306-
WasmBuffer temporary; // use for passing data in function calls
307-
308-
private:
309-
Holder<privat::WasmFunctionInternal> function_(const WasmName &name);
310-
};
311-
312322
CAGE_CORE_API Holder<WasmInstance> wasmInstantiate(Holder<WasmModule> &&module);
313323
}
314324

sources/libcore/wasm.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,6 @@ namespace cage
485485
{
486486
const String e = wasm_runtime_get_exception(impl->instance->instance);
487487
CAGE_LOG_THROW(e);
488-
CAGE_LOG_THROW(Stringizer() + "function: " + impl->name);
489488
CAGE_THROW_ERROR(Exception, "wasm function call failed");
490489
}
491490
}

0 commit comments

Comments
 (0)