Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc75d428

Browse files
committed
[PySDK] Fix: function overloading issue
* Temporary workaroundSigned-off-by: Shreyas Atre <shreyasatre16@gmail.com>
1 parent3909b97 commitc75d428

File tree

3 files changed

+290
-47
lines changed

3 files changed

+290
-47
lines changed

‎bindings/python/pywasmedge/include/WasmEdge.hpp‎

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -75,23 +75,6 @@ class result {
7575
intget_code();
7676
};
7777

78-
classVM {
79-
private:
80-
WasmEdge_VMContext *VMCxt;
81-
82-
public:
83-
VM();
84-
VM(Store &);
85-
VM(Configure &);
86-
VM(Configure &, Store &);
87-
~VM();
88-
constchar *doc() {return pysdk::vm_doc; };
89-
90-
pybind11::tuplerun(pybind11::object, pybind11::object, pybind11::object,
91-
pybind11::object, pybind11::object);
92-
pybind11::tuplerun(pybind11::object, pybind11::object, pybind11::object);
93-
};
94-
9578
WasmEdge_Resulthost_function(void *, WasmEdge_MemoryInstanceContext *,
9679
const WasmEdge_Value *, WasmEdge_Value *);
9780

@@ -112,6 +95,7 @@ class function {
11295
public:
11396
function(pybind11::function);
11497
~function();
98+
WasmEdge_FunctionInstanceContext *get();
11599
};
116100

117101
classmodule {
@@ -120,10 +104,33 @@ class module {
120104

121105
public:
122106
module(std::string name ="Unnamed");
123-
module(pysdk::module &);
124107
~module();
125108
WasmEdge_ImportObjectContext *get();
126-
// void add();
109+
voidadd(pysdk::function &, std::string name ="Function_Name");
110+
};
111+
112+
classVM {
113+
private:
114+
WasmEdge_VMContext *VMCxt;
115+
116+
public:
117+
VM();
118+
VM(Store &);
119+
VM(Configure &);
120+
VM(Configure &, Store &);
121+
~VM();
122+
constchar *doc() {return pysdk::vm_doc; };
123+
124+
pysdk::resultadd(pysdk::module &);
125+
126+
pybind11::tuplerun(pybind11::object, pybind11::object, pybind11::object,
127+
pybind11::object, pybind11::object);
128+
pybind11::tuplerun(pybind11::object, pybind11::object, pybind11::object);
129+
130+
pybind11::tuplerun(pybind11::object, pybind11::object, pybind11::str,
131+
pybind11::int_);
132+
133+
pybind11::listlist_functions();
127134
};
128135

129136
}// namespace pysdk

‎bindings/python/pywasmedge/src/WasmEdge.cpp‎

Lines changed: 161 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,110 @@ pybind11::tuple pysdk::VM::run(pybind11::object _FileName,
241241
returnpybind11::make_tuple(res, returns);
242242
}
243243

244+
pysdk::resultpysdk::VM::add(pysdk::module &mod) {
245+
returnWasmEdge_VMRegisterModuleFromImport(VMCxt, mod.get());
246+
}
247+
248+
pybind11::tuplepysdk::VM::run(pybind11::object wasm_buffer_,
249+
pybind11::object params_,
250+
pybind11::str function_name_,
251+
pybind11::int_ temp) {
252+
253+
auto function_name = function_name_.cast<std::string>();
254+
255+
auto wasm_buffer = wasm_buffer_.cast<pybind11::tuple>();
256+
pybind11::list returns;
257+
258+
auto params = params_.cast<pybind11::tuple>();
259+
auto param_len =pybind11::len(params);
260+
auto size = wasm_buffer.size();
261+
uint8_t WASM_Buffer[size];
262+
263+
for (size_t i =0; i < size; i++) {
264+
WASM_Buffer[i] = wasm_buffer[i].cast<uint8_t>();
265+
}
266+
267+
WasmEdge_Value Params[param_len];
268+
for (int i =0; i < param_len; i++) {
269+
if (pybind11::isinstance<pybind11::int_>(params[i])) {
270+
Params[i] =WasmEdge_ValueGenI32(params[i].cast<int32_t>());
271+
}elseif (pybind11::isinstance<pybind11::float_>(params[i])) {
272+
Params[i] =WasmEdge_ValueGenF32(params[i].cast<float>());
273+
}else {
274+
// TODO: Handle Errors
275+
returnpybind11::make_tuple(NULL,NULL);
276+
}
277+
}
278+
279+
WasmEdge_String func_name_wasm =
280+
WasmEdge_StringCreateByCString(function_name.c_str());
281+
282+
WasmEdge_FunctionTypeContext *FuncTypeCxt =
283+
(WasmEdge_FunctionTypeContext *)WasmEdge_VMGetFunctionType(
284+
VMCxt, func_name_wasm);
285+
286+
auto param_len_api =WasmEdge_FunctionTypeGetParametersLength(FuncTypeCxt);
287+
auto return_len =WasmEdge_FunctionTypeGetReturnsLength(FuncTypeCxt);
288+
289+
if (param_len != param_len_api) {
290+
/* TODO: Handle errors gracefully*/
291+
WasmEdge_FunctionTypeDelete(FuncTypeCxt);
292+
returnpybind11::make_tuple(NULL,NULL);
293+
}
294+
295+
WasmEdge_Value Returns[return_len];
296+
297+
pysdk::resultres(
298+
WasmEdge_VMRunWasmFromBuffer(VMCxt, WASM_Buffer, size, func_name_wasm,
299+
Params, param_len, Returns, return_len));
300+
301+
WasmEdge_StringDelete(func_name_wasm);
302+
303+
for (int i =0; i < return_len; i++) {
304+
switch (Returns[i].Type) {
305+
case WasmEdge_ValType_I32:
306+
returns.append(pybind11::cast(WasmEdge_ValueGetI32(Returns[i])));
307+
break;
308+
case WasmEdge_ValType_I64:
309+
returns.append(pybind11::cast(WasmEdge_ValueGetI64(Returns[i])));
310+
break;
311+
case WasmEdge_ValType_F32:
312+
returns.append(pybind11::cast(WasmEdge_ValueGetF32(Returns[i])));
313+
break;
314+
case WasmEdge_ValType_F64:
315+
returns.append(pybind11::cast(WasmEdge_ValueGetF64(Returns[i])));
316+
break;
317+
case WasmEdge_ValType_V128:
318+
returns.append(pybind11::cast(WasmEdge_ValueGetV128(Returns[i])));
319+
break;
320+
case WasmEdge_ValType_FuncRef:
321+
returns.append(pybind11::cast(WasmEdge_ValueGetFuncIdx(Returns[i])));
322+
break;
323+
// TODO: Handle Void Pointer
324+
// case WasmEdge_ValType_ExternRef:
325+
// returns.append(pybind11::cast(WasmEdge_ValueGetExternRef(Returns[i])));
326+
// break;
327+
default:
328+
break;
329+
}
330+
}
331+
332+
returnpybind11::make_tuple(res, returns);
333+
}
334+
335+
pybind11::listpysdk::VM::list_functions() {
336+
pybind11::list returns;
337+
338+
auto len =WasmEdge_VMGetFunctionListLength(VMCxt);
339+
340+
WasmEdge_String str[len];
341+
auto w =WasmEdge_VMGetFunctionList(VMCxt, str,NULL, str[0].Length);
342+
for (size_t i =0; i < len; i++) {
343+
returns.append(str[i].Buf);
344+
}
345+
return returns;
346+
}
347+
244348
/* --------------- VM End --------------------------------*/
245349

246350
/* --------------- Store --------------------------------*/
@@ -352,18 +456,24 @@ pysdk::module::module(std::string name) {
352456
WasmEdge_ImportObjectCreate(WasmEdge_StringCreateByCString(name.c_str()));
353457
}
354458

355-
pysdk::module::module(pysdk::module &mod) { ModCxt = mod.get(); }
459+
voidpysdk::module::add(pysdk::function &func, std::string name) {
460+
WasmEdge_String function_name =WasmEdge_StringCreateByCString(name.c_str());
461+
WasmEdge_ImportObjectAddFunction(ModCxt, function_name, func.get());
462+
WasmEdge_StringDelete(function_name);
463+
}
356464

357465
pysdk::module::~module() {WasmEdge_ImportObjectDelete(ModCxt); }
358466

467+
WasmEdge_ImportObjectContext *pysdk::module::get() {return ModCxt; }
468+
359469
/* --------------- Module End ----------------------------------------*/
360470

361471
/* --------------- Function ----------------------------------------*/
362472

363473
pysdk::function::function(pybind11::function func_) : func(func_) {
364474
pybind11::dict annotations = func.attr("__annotations__");
365475
auto total =pybind11::len(annotations);
366-
ret_len =pybind11::len(annotations["return"]);
476+
ret_len =pybind11::len(pybind11::make_tuple(annotations["return"]));
367477
param_len = total - ret_len;
368478

369479
size_t i =0;
@@ -374,33 +484,30 @@ pysdk::function::function(pybind11::function func_) : func(func_) {
374484
param_types =new WasmEdge_ValType[param_len];
375485
return_types =new WasmEdge_ValType[ret_len];
376486

377-
for (auto e : annotations) {
378-
if (e.first.cast<std::string>() =="return") {
379-
i =0;
380-
for (auto ret : annotations["return"].cast<pybind11::tuple>()) {
381-
auto type_str = ret.cast<pybind11::type>();
382-
if (type_str.is(temp_int.get_type())) {
383-
return_types[i] = WasmEdge_ValType_I32;
384-
}elseif (type_str.is(temp_float.get_type())) {
385-
return_types[i] = WasmEdge_ValType_F32;
386-
}else {
387-
// TODO: Handle Errors
388-
}
389-
i++;
390-
}
391-
i =0;
487+
for (auto ret :pybind11::make_tuple(annotations["return"])) {
488+
auto type_str = ret.cast<pybind11::type>();
489+
if (type_str.is(temp_int.get_type())) {
490+
return_types[i] = WasmEdge_ValType_I32;
491+
}elseif (type_str.is(temp_float.get_type())) {
492+
return_types[i] = WasmEdge_ValType_F32;
392493
}else {
494+
// TODO: Handle Errors
495+
}
496+
i++;
497+
}
498+
499+
i =0;
393500

394-
auto type_str = e.second.cast<pybind11::type>();
395-
if (type_str.is(temp_int.get_type())) {
396-
param_types[i] = WasmEdge_ValType_I32;
397-
}elseif (type_str.is(temp_float.get_type())) {
398-
param_types[i] = WasmEdge_ValType_F32;
399-
}else {
400-
// TODO: Handle Errors
401-
}
402-
i++;
501+
for (auto e : annotations) {
502+
auto type_str = e.second.cast<pybind11::type>();
503+
if (type_str.is(temp_int.get_type())) {
504+
param_types[i] = WasmEdge_ValType_I32;
505+
}elseif (type_str.is(temp_float.get_type())) {
506+
param_types[i] = WasmEdge_ValType_F32;
507+
}else {
508+
// TODO: Handle Errors
403509
}
510+
i++;
404511
}
405512

406513
HostFType =WasmEdge_FunctionTypeCreate(param_types, param_len, return_types,
@@ -429,7 +536,16 @@ WasmEdge_Result pysdk::host_function(void *Data,
429536
size_t param_len = casted_data->param_len;
430537

431538
for (size_t i =0; i < param_len; i++) {
432-
params.append(In[i]);
539+
switch (In[i].Type) {
540+
case WasmEdge_ValType_I32:
541+
params.append(WasmEdge_ValueGetI32(In[i]));
542+
break;
543+
case WasmEdge_ValType_F32:
544+
params.append(WasmEdge_ValueGetF32(In[i]));
545+
break;
546+
default:
547+
break;
548+
}
433549
}
434550
auto params_tup = params.cast<pybind11::tuple>();
435551

@@ -446,6 +562,10 @@ WasmEdge_Result pysdk::host_function(void *Data,
446562
return WasmEdge_Result_Success;
447563
};
448564

565+
WasmEdge_FunctionInstanceContext *pysdk::function::get() {
566+
returnthis->HostFuncCxt;
567+
}
568+
449569
pysdk::function::~function() {
450570
WasmEdge_FunctionInstanceDelete(HostFuncCxt);
451571
WasmEdge_FunctionTypeDelete(HostFType);
@@ -563,6 +683,9 @@ PYBIND11_MODULE(WasmEdge, module) {
563683
pybind11::tuple (pysdk::VM::*run)(pybind11::object, pybind11::object,
564684
pybind11::object, pybind11::object,
565685
pybind11::object) = &pysdk::VM::run;
686+
pybind11::tuple (pysdk::VM::*run_wasm_buffer)(
687+
pybind11::object, pybind11::object, pybind11::str, pybind11::int_) =
688+
&pysdk::VM::run;
566689

567690
pybind11::class_<pysdk::VM>(module,"VM")
568691
.def(pybind11::init())
@@ -571,7 +694,17 @@ PYBIND11_MODULE(WasmEdge, module) {
571694
.def(pybind11::init<pysdk::Configure &, pysdk::Store &>())
572695
.def("__doc__", &pysdk::VM::doc)
573696
.def("run", run)
574-
.def("run", run_step_by_step);
697+
.def("run", run_step_by_step)
698+
.def("run", run_wasm_buffer)
699+
.def("add", &pysdk::VM::add)
700+
.def("list", &pysdk::VM::list_functions);
701+
702+
pybind11::class_<pysdk::function>(module,"Function")
703+
.def(pybind11::init<pybind11::function>());
704+
705+
pybind11::class_<pysdk::module>(module,"Module")
706+
.def(pybind11::init<std::string>())
707+
.def("add", &pysdk::module::add);
575708
};
576709

577710
/* --------------- Python Module End ----------------------------------------*/

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp