Forráskód Böngészése

* Demo for wasmedge

Michaël Van Canneyt 3 hónapja
szülő
commit
8353fa6c81

BIN
packages/wasmedge/examples/fibonacci.wasm


+ 21 - 0
packages/wasmedge/examples/fibonacci.wat

@@ -0,0 +1,21 @@
+(module
+  (func $fib (export "fib") (param $n i32) (result i32)
+    local.get $n
+    i32.const 2
+    i32.lt_s
+    if
+      i32.const 1
+      return
+    end
+    local.get $n
+    i32.const 2
+    i32.sub
+    call $fib
+    local.get $n
+    i32.const 1
+    i32.sub
+    call $fib
+    i32.add
+    return
+  )
+)

+ 62 - 0
packages/wasmedge/examples/runfib.lpi

@@ -0,0 +1,62 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<CONFIG>
+  <ProjectOptions>
+    <Version Value="12"/>
+    <General>
+      <Flags>
+        <MainUnitHasCreateFormStatements Value="False"/>
+        <MainUnitHasTitleStatement Value="False"/>
+        <MainUnitHasScaledStatement Value="False"/>
+      </Flags>
+      <SessionStorage Value="InProjectDir"/>
+      <Title Value="runfib"/>
+      <UseAppBundle Value="False"/>
+      <ResourceType Value="res"/>
+    </General>
+    <BuildModes>
+      <Item Name="Default" Default="True"/>
+    </BuildModes>
+    <PublishOptions>
+      <Version Value="2"/>
+      <UseFileFilters Value="True"/>
+    </PublishOptions>
+    <RunParams>
+      <FormatVersion Value="2"/>
+    </RunParams>
+    <Units>
+      <Unit>
+        <Filename Value="runfib.lpr"/>
+        <IsPartOfProject Value="True"/>
+      </Unit>
+    </Units>
+  </ProjectOptions>
+  <CompilerOptions>
+    <Version Value="11"/>
+    <Target>
+      <Filename Value="runfib"/>
+    </Target>
+    <SearchPaths>
+      <IncludeFiles Value="$(ProjOutDir)"/>
+      <OtherUnitFiles Value="../src"/>
+      <UnitOutputDirectory Value="lib/$(TargetCPU)-$(TargetOS)"/>
+    </SearchPaths>
+    <Other>
+      <ConfigFile>
+        <WriteConfigFilePath Value=""/>
+      </ConfigFile>
+    </Other>
+  </CompilerOptions>
+  <Debugging>
+    <Exceptions>
+      <Item>
+        <Name Value="EAbort"/>
+      </Item>
+      <Item>
+        <Name Value="ECodetoolError"/>
+      </Item>
+      <Item>
+        <Name Value="EFOpenError"/>
+      </Item>
+    </Exceptions>
+  </Debugging>
+</CONFIG>

+ 35 - 0
packages/wasmedge/examples/runfib.lpr

@@ -0,0 +1,35 @@
+program runfib;
+
+uses ctypes, libwasmedge;
+
+var
+  ConfCxt : PWasmEdge_ConfigureContext;
+  VMCxt : PWasmEdge_VMContext;
+  Returns, Params : Array[0..0] of TWasmEdge_Value;
+  FuncName : TWasmEdge_String;
+  Res : TWasmEdge_Result;
+
+begin
+  Loadlibwasmedge('libwasmedge.so');
+  { Create the configure context and add the WASI support.
+    This step is not necessary unless you need WASI support.}
+  ConfCxt:=WasmEdge_ConfigureCreate();
+  WasmEdge_ConfigureAddHostRegistration(ConfCxt, WasmEdge_HostRegistration_Wasi);
+  { The configure and store context to the VM creation can be NULL. }
+  VMCxt:=WasmEdge_VMCreate(ConfCxt,Nil);
+  { The parameters }
+  Params[0] := WasmEdge_ValueGenI32(32);
+  { Function name. }
+  FuncName:=WasmEdge_StringCreateByCString(Pcchar(Pansichar('fib')));
+  { Run the WASM function from file. }
+  Res := WasmEdge_VMRunWasmFromFile(VMCxt, pcchar(PAnsiChar(ParamStr(1))), FuncName, @Params, 1, @Returns, 1);
+  if (WasmEdge_ResultOK(Res)) then
+    Writeln('Get result: ', WasmEdge_ValueGetI32(Returns[0]))
+  else
+    Writeln('Error message: ', PAnsiChar(WasmEdge_ResultGetMessage(Res)));
+  { Resources deallocations. }
+  WasmEdge_VMDelete(VMCxt);
+  WasmEdge_ConfigureDelete(ConfCxt);
+  WasmEdge_StringDelete(FuncName);
+end.
+