I released QjsIpc ð
March 5, 2022â¢277 words
Learning WASM 4 ~ 8 ã®ææãã©ã€ãã©ãªåããŠå ¬éããã
JSåŽã§ã¯ä»åäœæããQuickJSã®æ¡åŒµã¢ãžã¥ãŒã«ã§ãipc
ã¢ãžã¥ãŒã«ã䜿çšããããã·ãŒãžã£ãç»é²ããŠãæçµçã«listen
ã¡ãœãããå®è¡ãã
import * as ipc from 'ipc';
ipc.register('echo', function (payload) {
return payload;
});
ipc.listen();
.NETåŽã§ã¯ãQjsIpcEngineã®ã€ã³ã¹ã¿ã³ã¹ãäœæããJSãã¡ã€ã«ãšå®è¡ãã©ã«ããæå®ããŠQuickJSãå®è¡ããã
å®è¡ããåŸã¯ãInvokeAsync
ã¡ãœããã§JSåŽã«ç»é²ããããã·ãŒãžã£ãå®è¡ã§ããã
using QjsIpc;
await using var engine = new QjsIpcEngine();
engine.Start(new QjsIpcOptions
{
ScriptFileName = "main.js",
AllowedDirectoryPath = Environment.CurrentDirectory,
});
var message = await engine.InvokeAsync<string>("echo", "Hello, World!");
Console.WriteLine(message);
.NETåŽã«ããã·ãŒãžã£ãçšæããJSåŽããåŒã³åºãããšãã§ããã
.NETåŽã«ããã·ãŒãžã£ãã¹ããšãªãã¯ã©ã¹ãäœæãããã®ã€ã³ã¹ã¿ã³ã¹ãMethodHost
ãªãã·ã§ã³ã«èšå®ããã
çããã¡ãœããã¯ãå€ã§ãTaskã§ãValueTaskã§ã倧äžå€«ã
public class Host
{
public int HostAdd(int a, int b) => a + b;
public Task<string> HostStringTask() => Task.FromResult("host value");
public async Task<string> HostStringAsync() {
await Task.Delay(100);
return "host value";
}
public async ValueTask<string> HostStringValueAsync() {
await Task.Delay(100);
return "host value";
}
}
public class Program
{
public static async Task Main(string[] args)
{
await using var engine = new QjsIpcEngine();
engine.Start(new QjsIpcOptions
{
ScriptFileName = "main.js",
AllowedDirectoryPath = Environment.CurrentDirectory,
MethodHost = new Host()
});
await engine.InvokeAsync('createMessage', "iwate");
}
}
JSåŽã§ã¯ipc.invoke
ã¡ãœããã§åŒã³åºãããæ»ãå€ã¯Promise
ã
import * as ipc from 'ipc';
ipc.register('createMessage', async function (name) {
const value = ipc.invoke('HostAdd', 100, 200);
return `Hi, ${name}. The result of HostAdd(100, 200) is ${value}`;
});
ipc.listen();
ãã³ãããŒã¯ããšã£ãŠã¿ãããæ³åéãéãã¯ãªãããã©äœ¿ããªãã»ã©ã§ã¯ãªãã
èªåã®å®è£
ã®äžã§é
ãéšåã®æ€èšã¯ã€ããŠãŠã.NET<->JSéã1ãã€ããã€ããåãããŠãã®ã§ãŸãé
ãã
wasmã®memory䜿ã£ãŠããŒãž(64kb)ããšã«ããåãããã°ãã£ãšæ©ããªãããªãä»åŸã«èª²é¡ã
// * Summary *
BenchmarkDotNet=v0.13.1, OS=Windows 10.0.22000
11th Gen Intel Core i7-1165G7 2.80GHz, 1 CPU, 8 logical and 4 physical cores
.NET SDK=6.0.200-preview.21617.4
[Host] : .NET 6.0.1 (6.0.121.56705), X64 RyuJIT [AttachedDebugger]
DefaultJob : .NET 6.0.1 (6.0.121.56705), X64 RyuJIT
| Method | Mean | Error | StdDev |
|----------------- |-----------:|----------:|----------:|
| Echo | 303.7 us | 28.22 us | 81.42 us |
| HostValue | 456.2 us | 41.25 us | 120.97 us |
| Ejs | 1,414.2 us | 91.87 us | 269.43 us |
| EjsWithHostValue | 1,499.9 us | 121.33 us | 340.22 us |