Skip to content

Commit

Permalink
deploy: 2d04645
Browse files Browse the repository at this point in the history
  • Loading branch information
kateinoigakukun committed Feb 15, 2024
1 parent 734921a commit 0627f4f
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 82 deletions.
70 changes: 30 additions & 40 deletions examples/exporting-function.html
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ <h1 class="menu-title">Swift and WebAssembly</h1>
<main>
<h1><a class="header" href="#exporting-function-for-host-environment" id="exporting-function-for-host-environment">Exporting function for host environment</a></h1>
<p>You can expose a Swift function for host environment using special attribute and linker option.</p>
<pre><code class="language-swift">@_cdecl(&quot;add&quot;)
<pre><code class="language-swift">// File name: lib.swift
@_cdecl(&quot;add&quot;)
func add(_ lhs: Int, _ rhs: Int) -&gt; Int {
return lhs + rhs
}
Expand All @@ -173,51 +174,40 @@ <h1><a class="header" href="#exporting-function-for-host-environment" id="export
The default execution model is <em>command</em>, so you need to pass <code>-mexec-model=reactor</code> to linker.</li>
<li>Call <code>_initialize</code> function before interacting with the instance.</li>
</ol>
<p>If your code has any top-level code, you need to export <code>main</code> function as well, and call it after <code>_initialize</code> function.</p>
<pre><code class="language-bash">$ swiftc \
-target wasm32-unknown-wasi \
-parse-as-library \
lib.swift -o lib.wasm \
-Xlinker --export=add \
-Xclang-linker -mexec-model=reactor \
-Xlinker --export=main # Optional
-Xclang-linker -mexec-model=reactor
</code></pre>
<p>Then, you can use the exported function from host environment.</p>
<pre><code class="language-javascript">const WASI = require(&quot;@wasmer/wasi&quot;).WASI;
const WasmFs = require(&quot;@wasmer/wasmfs&quot;).WasmFs;

const promisify = require(&quot;util&quot;).promisify;
const fs = require(&quot;fs&quot;);
const readFile = promisify(fs.readFile);

const main = async () =&gt; {
// Instantiate a new WASI Instance
const wasmFs = new WasmFs();
let wasi = new WASI({
args: [],
env: {},
bindings: {
...WASI.defaultBindings,
fs: wasmFs.fs,
},
});

const wasmBinary = await readFile(&quot;lib.wasm&quot;);

// Instantiate the WebAssembly file
const { instance } = await WebAssembly.instantiate(wasmBinary, {
wasi_snapshot_preview1: wasi.wasiImport,
});
// Initialize the instance by following WASI reactor ABI
instance.exports._initialize();
// (Optional) Run the top-level code
instance.exports.main();
// Get the exported function
const addFn = instance.exports.add;
console.log(&quot;2 + 3 = &quot; + addFn(2, 3))

};

main()
<pre><code class="language-javascript">// File name: main.mjs
import { WASI, File, OpenFile, ConsoleStdout } from &quot;@bjorn3/browser_wasi_shim&quot;;
import fs from &quot;fs/promises&quot;;

// Instantiate a new WASI Instance
// See https://github.com/bjorn3/browser_wasi_shim/ for more detail about constructor options
let wasi = new WASI([], [],
[
new OpenFile(new File([])), // stdin
ConsoleStdout.lineBuffered(msg =&gt; console.log(`[WASI stdout] ${msg}`)),
ConsoleStdout.lineBuffered(msg =&gt; console.warn(`[WASI stderr] ${msg}`)),
],
{ debug: false }
);

const wasmBinary = await fs.readFile(&quot;lib.wasm&quot;);

// Instantiate the WebAssembly file
const { instance } = await WebAssembly.instantiate(wasmBinary, {
wasi_snapshot_preview1: wasi.wasiImport,
});
// Initialize the instance by following WASI reactor ABI
wasi.initialize(instance);
// Get the exported function
const addFn = instance.exports.add;
console.log(&quot;2 + 3 = &quot; + addFn(2, 3))
</code></pre>
<p>If you use SwiftPM package, you can omit linker flag using clang's <code>__atribute__</code>. Please see <a href="https://github.com/swiftwasm/JavaScriptKit/pull/91/files">swiftwasm/JavaScriptKit#91</a> for more detail info</p>

Expand Down
70 changes: 30 additions & 40 deletions print.html
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,8 @@ <h1><a class="header" href="#importing-a-function-from-host-environments" id="im
SwiftWasm toolchain.</p>
<h1><a class="header" href="#exporting-function-for-host-environment" id="exporting-function-for-host-environment">Exporting function for host environment</a></h1>
<p>You can expose a Swift function for host environment using special attribute and linker option.</p>
<pre><code class="language-swift">@_cdecl(&quot;add&quot;)
<pre><code class="language-swift">// File name: lib.swift
@_cdecl(&quot;add&quot;)
func add(_ lhs: Int, _ rhs: Int) -&gt; Int {
return lhs + rhs
}
Expand All @@ -767,51 +768,40 @@ <h1><a class="header" href="#exporting-function-for-host-environment" id="export
The default execution model is <em>command</em>, so you need to pass <code>-mexec-model=reactor</code> to linker.</li>
<li>Call <code>_initialize</code> function before interacting with the instance.</li>
</ol>
<p>If your code has any top-level code, you need to export <code>main</code> function as well, and call it after <code>_initialize</code> function.</p>
<pre><code class="language-bash">$ swiftc \
-target wasm32-unknown-wasi \
-parse-as-library \
lib.swift -o lib.wasm \
-Xlinker --export=add \
-Xclang-linker -mexec-model=reactor \
-Xlinker --export=main # Optional
-Xclang-linker -mexec-model=reactor
</code></pre>
<p>Then, you can use the exported function from host environment.</p>
<pre><code class="language-javascript">const WASI = require(&quot;@wasmer/wasi&quot;).WASI;
const WasmFs = require(&quot;@wasmer/wasmfs&quot;).WasmFs;

const promisify = require(&quot;util&quot;).promisify;
const fs = require(&quot;fs&quot;);
const readFile = promisify(fs.readFile);

const main = async () =&gt; {
// Instantiate a new WASI Instance
const wasmFs = new WasmFs();
let wasi = new WASI({
args: [],
env: {},
bindings: {
...WASI.defaultBindings,
fs: wasmFs.fs,
},
});

const wasmBinary = await readFile(&quot;lib.wasm&quot;);

// Instantiate the WebAssembly file
const { instance } = await WebAssembly.instantiate(wasmBinary, {
wasi_snapshot_preview1: wasi.wasiImport,
});
// Initialize the instance by following WASI reactor ABI
instance.exports._initialize();
// (Optional) Run the top-level code
instance.exports.main();
// Get the exported function
const addFn = instance.exports.add;
console.log(&quot;2 + 3 = &quot; + addFn(2, 3))

};

main()
<pre><code class="language-javascript">// File name: main.mjs
import { WASI, File, OpenFile, ConsoleStdout } from &quot;@bjorn3/browser_wasi_shim&quot;;
import fs from &quot;fs/promises&quot;;

// Instantiate a new WASI Instance
// See https://github.com/bjorn3/browser_wasi_shim/ for more detail about constructor options
let wasi = new WASI([], [],
[
new OpenFile(new File([])), // stdin
ConsoleStdout.lineBuffered(msg =&gt; console.log(`[WASI stdout] ${msg}`)),
ConsoleStdout.lineBuffered(msg =&gt; console.warn(`[WASI stderr] ${msg}`)),
],
{ debug: false }
);

const wasmBinary = await fs.readFile(&quot;lib.wasm&quot;);

// Instantiate the WebAssembly file
const { instance } = await WebAssembly.instantiate(wasmBinary, {
wasi_snapshot_preview1: wasi.wasiImport,
});
// Initialize the instance by following WASI reactor ABI
wasi.initialize(instance);
// Get the exported function
const addFn = instance.exports.add;
console.log(&quot;2 + 3 = &quot; + addFn(2, 3))
</code></pre>
<p>If you use SwiftPM package, you can omit linker flag using clang's <code>__atribute__</code>. Please see <a href="https://github.com/swiftwasm/JavaScriptKit/pull/91/files">swiftwasm/JavaScriptKit#91</a> for more detail info</p>
<h2><a class="header" href="#example-projects" id="example-projects">Example Projects</a></h2>
Expand Down
2 changes: 1 addition & 1 deletion searchindex.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion searchindex.json

Large diffs are not rendered by default.

0 comments on commit 0627f4f

Please sign in to comment.