@@ -241,6 +241,110 @@ pybind11::tuple pysdk::VM::run(pybind11::object _FileName,
241241return pybind11::make_tuple (res, returns);
242242}
243243
244+ pysdk::resultpysdk::VM::add (pysdk::module &mod) {
245+ return WasmEdge_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+ }else if (pybind11::isinstance<pybind11::float_>(params[i])) {
272+ Params[i] =WasmEdge_ValueGenF32 (params[i].cast <float >());
273+ }else {
274+ // TODO: Handle Errors
275+ return pybind11::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+ return pybind11::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+ return pybind11::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) {
352456WasmEdge_ImportObjectCreate (WasmEdge_StringCreateByCString (name.c_str ()));
353457}
354458
355- pysdk::module::module (pysdk::module &mod) { ModCxt = mod.get (); }
459+ void pysdk::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
357465pysdk::module::~module () {WasmEdge_ImportObjectDelete (ModCxt); }
358466
467+ WasmEdge_ImportObjectContext *pysdk::module::get () {return ModCxt; }
468+
359469/* --------------- Module End ----------------------------------------*/
360470
361471/* --------------- Function ----------------------------------------*/
362472
363473pysdk::function::function (pybind11::function func_) : func(func_) {
364474 pybind11::dict annotations = func.attr (" __annotations__" );
365475auto 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
369479size_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- }else if (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+ }else if (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- }else if (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+ }else if (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,
429536size_t param_len = casted_data->param_len ;
430537
431538for (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 }
434550auto params_tup = params.cast <pybind11::tuple>();
435551
@@ -446,6 +562,10 @@ WasmEdge_Result pysdk::host_function(void *Data,
446562return WasmEdge_Result_Success;
447563};
448564
565+ WasmEdge_FunctionInstanceContext *pysdk::function::get () {
566+ return this ->HostFuncCxt ;
567+ }
568+
449569pysdk::function::~function () {
450570WasmEdge_FunctionInstanceDelete (HostFuncCxt);
451571WasmEdge_FunctionTypeDelete (HostFType);
@@ -563,6 +683,9 @@ PYBIND11_MODULE(WasmEdge, module) {
563683pybind11::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 ----------------------------------------*/